Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into fetch-active-set
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Dec 21, 2023
2 parents f8001e3 + c580fba commit 44d50ce
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 14 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,34 @@ for more information on how to configure the node to work with the PoST service.
section. The non-conditional changes include values/provides support on all of the nodes, which will enable DHT to
function efficiently for routing discovery.

* [#5367](https://github.com/spacemeshos/go-spacemesh/pull/5367) Add `no-main-override` toplevel config option and
`--no-main-override` CLI option that makes it possible to run "nomain" builds on mainnet.

* [#5384](https://github.com/spacemeshos/go-spacemesh/pull/5384) to improve network stability and performance allow the
active set to be set in advance for an epoch. This allows the network to start consensus on the first layer of an epoch.

## Release v1.2.12

### Improvements

* [#5373](https://github.com/spacemeshos/go-spacemesh/pull/5373) automatic scaling of post verifying workers to a lower
value (1 by default) when POST proving starts. The workers are scaled up when POST proving finishes.

* [#5382](https://github.com/spacemeshos/go-spacemesh/pull/5382) avoid processing same (gossiped/fetched) ATX many times
in parallel

## Release v1.2.11

### Improvements

* increased the max response data size in p2p to 40MiB

## Release v1.2.10

### Improvements

* further increased cache sizes and and p2p timeouts to compensate for the increased number of nodes on the network.

## Release v1.2.9

### Improvements
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ func AddCommands(cmd *cobra.Command) {
cmd.PersistentFlags().DurationVar(&cfg.DatabasePruneInterval, "db-prune-interval",
cfg.DatabasePruneInterval, "configure interval for database pruning")

cmd.PersistentFlags().BoolVar(&cfg.NoMainOverride, "no-main-override",
cfg.NoMainOverride, "force 'nomain' builds to run on the mainnet")

/** ======================== P2P Flags ========================== **/

cmd.PersistentFlags().Var(flags.NewAddressListValue(cfg.P2P.Listen, &cfg.P2P.Listen),
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ type BaseConfig struct {
// ATXGradeDelay is used to grade ATXs for selection in tortoise active set.
// See grading fuction in miner/proposals_builder.go
ATXGradeDelay time.Duration `mapstructure:"atx-grade-delay"`

// NoMainOverride forces the "nomain" builds to run on the mainnet
NoMainOverride bool `mapstructure:"no-main-override"`
}

type PublicMetrics struct {
Expand Down
1 change: 1 addition & 0 deletions config/mainnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func MainnetConfig() Config {
Standalone: false,
GossipDuration: 50 * time.Second,
OutOfSyncThresholdLayers: 36, // 3h
DisableAtxReconciliation: true,
},
Recovery: checkpoint.DefaultConfig(),
Cache: datastore.DefaultConfig(),
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ require (
github.com/stretchr/testify v1.8.4
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7
github.com/zeebo/blake3 v0.2.3
go.uber.org/mock v0.3.0
go.uber.org/mock v0.4.0
go.uber.org/zap v1.26.0
golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb
golang.org/x/sync v0.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -720,8 +720,8 @@ go.uber.org/goleak v1.1.11-0.20210813005559-691160354723/go.mod h1:cwTWslyiVhfpK
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
go.uber.org/mock v0.3.0 h1:3mUxI1No2/60yUYax92Pt8eNOEecx2D3lcXZh2NEZJo=
go.uber.org/mock v0.3.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
go.uber.org/mock v0.4.0 h1:VcM4ZOtdbR4f6VXfiOpwpVJDL6lCReaZ6mw31wqh7KU=
go.uber.org/mock v0.4.0/go.mod h1:a6FSlNadKUHUa9IP5Vyt1zh4fC7uAwxMutEAscFbkZc=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
Expand Down
17 changes: 15 additions & 2 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import (
"github.com/spacemeshos/go-spacemesh/sql/localsql"
dbmetrics "github.com/spacemeshos/go-spacemesh/sql/metrics"
"github.com/spacemeshos/go-spacemesh/syncer"
"github.com/spacemeshos/go-spacemesh/syncer/atxsync"
"github.com/spacemeshos/go-spacemesh/syncer/blockssync"
"github.com/spacemeshos/go-spacemesh/system"
"github.com/spacemeshos/go-spacemesh/timesync"
Expand Down Expand Up @@ -136,7 +137,7 @@ func GetCommand() *cobra.Command {
log.JSONLog(true)
}

if cmd.NoMainNet && onMainNet(conf) {
if cmd.NoMainNet && onMainNet(conf) && !conf.NoMainOverride {
log.With().Fatal("this is a testnet-only build not intended for mainnet")
}

Expand Down Expand Up @@ -1174,7 +1175,19 @@ func (app *App) listenToUpdates(ctx context.Context) {
app.hOracle.UpdateActiveSet(epoch, set)
app.proposalBuilder.UpdateActiveSet(epoch, set)

Check warning on line 1177 in node/node.go

View check run for this annotation

Codecov / codecov/patch

node/node.go#L1165-L1177

Added lines #L1165 - L1177 were not covered by tests
// TODO(mafa): fetch atxs (see #5377)
app.eg.Go(func() error {
if err := atxsync.Download(
ctx,
10*time.Second,
app.addLogger(SyncLogger, app.log).Zap(),
app.db,
app.fetcher,
set,
); err != nil {
app.errCh <- err
}
return nil
})
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions p2p/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,14 +205,6 @@ func New(
}
lp2plog.SetPrimaryCore(logger.Core())
lp2plog.SetAllLoggers(lp2plog.LogLevel(cfg.LogLevel))
cm, err := connmgr.NewConnManager(
cfg.LowPeers,
cfg.HighPeers,
connmgr.WithGracePeriod(cfg.GracePeersShutdown),
)
if err != nil {
return nil, fmt.Errorf("p2p create conn mgr: %w", err)
}
streamer := *yamux.DefaultTransport
streamer.Config().ConnectionWriteTimeout = 25 * time.Second // should be NOT exposed in the config
ps, err := pstoremem.NewPeerstore()
Expand Down Expand Up @@ -299,7 +291,17 @@ func New(
)
}
if !cfg.DisableConnectionManager {
cm, err := connmgr.NewConnManager(
cfg.LowPeers,
cfg.HighPeers,
connmgr.WithGracePeriod(cfg.GracePeersShutdown),
)
if err != nil {
return nil, fmt.Errorf("p2p create conn mgr: %w", err)
}
lopts = append(lopts, libp2p.ConnectionManager(cm))
} else {
lopts = append(lopts, libp2p.ConnectionManager(&ccmgr.NullConnMgr{}))
}
if len(cfg.AdvertiseAddress) > 0 {
lopts = append(
Expand Down
75 changes: 75 additions & 0 deletions syncer/atxsync/atxsync.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package atxsync

import (
"context"
"math/rand"
"time"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
)

//go:generate mockgen -typed -package=mocks -destination=./mocks/mocks.go -source=./atxsync.go
type atxFetcher interface {
GetAtxs(context.Context, []types.ATXID) error
}

func getMissing(db *sql.Database, set []types.ATXID) ([]types.ATXID, error) {
missing := []types.ATXID{}
for _, atx := range set {
exist, err := atxs.Has(db, atx)
if err != nil {
return nil, err
}
if !exist {
missing = append(missing, atx)
}
}
return missing, nil
}

// Download specified set of atxs from peers in the network.
//
// actual retry interval will be between [retryInterval, 2*retryInterval].
func Download(
ctx context.Context,
retryInterval time.Duration,
logger *zap.Logger,
db *sql.Database,
fetcher atxFetcher,
set []types.ATXID,
) error {
total := len(set)
for {
missing, err := getMissing(db, set)
if err != nil {
return err
}
set = missing
downloaded := total - len(missing)
logger.Info("downloaded atxs",
zap.Int("total", total),
zap.Int("downloaded", downloaded),
zap.Array("missing", zapcore.ArrayMarshalerFunc(func(enc zapcore.ArrayEncoder) error {
for _, atx := range missing {
enc.AppendString(atx.ShortString())
}
return nil
})))
if len(missing) == 0 {
return nil
}
if err := fetcher.GetAtxs(ctx, missing); err != nil {
logger.Debug("failed to fetch atxs", zap.Error(err))
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(retryInterval + time.Duration(rand.Int63n(int64(retryInterval)))):
}
}
}
}
126 changes: 126 additions & 0 deletions syncer/atxsync/atxsync_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package atxsync

import (
"context"
"errors"
"testing"
"time"

"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/log/logtest"
"github.com/spacemeshos/go-spacemesh/sql"
"github.com/spacemeshos/go-spacemesh/sql/atxs"
"github.com/spacemeshos/go-spacemesh/syncer/atxsync/mocks"
)

func atx(id types.ATXID) *types.VerifiedActivationTx {
atx := &types.ActivationTx{InnerActivationTx: types.InnerActivationTx{
NIPostChallenge: types.NIPostChallenge{
PublishEpoch: 1,
},
NumUnits: 1,
}}
atx.SetID(id)
atx.SetEffectiveNumUnits(1)
atx.SetReceived(time.Now())
copy(atx.SmesherID[:], id[:])
vatx, err := atx.Verify(0, 1)
if err != nil {
panic(err)
}
return vatx
}

func id(id ...byte) types.ATXID {
return types.BytesToATXID(id)
}

type fetchRequest struct {
request []types.ATXID
result []*types.VerifiedActivationTx
error error
}

func TestDownload(t *testing.T) {
canceled, cancel := context.WithCancel(context.Background())
cancel()
for _, tc := range []struct {
desc string
ctx context.Context
retry time.Duration
existing []*types.VerifiedActivationTx
set []types.ATXID
fetched []fetchRequest
rst error
}{
{
desc: "all existing",
ctx: context.Background(),
existing: []*types.VerifiedActivationTx{atx(id(1)), atx(id(2)), atx(id(3))},
set: []types.ATXID{id(1), id(2), id(3)},
},
{
desc: "with multiple requests",
ctx: context.Background(),
existing: []*types.VerifiedActivationTx{atx(id(1))},
retry: 1,
fetched: []fetchRequest{
{
request: []types.ATXID{id(2), id(3)},
error: errors.New("test"),
result: []*types.VerifiedActivationTx{atx(id(2))},
},
{request: []types.ATXID{id(3)}, result: []*types.VerifiedActivationTx{atx(id(3))}},
},
set: []types.ATXID{id(1), id(2), id(3)},
},
{
desc: "continue on error",
ctx: context.Background(),
retry: 1,
existing: []*types.VerifiedActivationTx{atx(id(1))},
fetched: []fetchRequest{
{request: []types.ATXID{id(2)}, error: errors.New("test")},
{request: []types.ATXID{id(2)}, result: []*types.VerifiedActivationTx{atx(id(2))}},
},
set: []types.ATXID{id(1), id(2)},
},
{
desc: "exit on context",
ctx: canceled,
retry: 1,
existing: []*types.VerifiedActivationTx{atx(id(1))},
fetched: []fetchRequest{
{request: []types.ATXID{id(2)}, error: errors.New("test")},
},
set: []types.ATXID{id(1), id(2)},
rst: context.Canceled,
},
} {
t.Run(tc.desc, func(t *testing.T) {
logger := logtest.New(t)
db := sql.InMemory()
ctrl := gomock.NewController(t)
fetcher := mocks.NewMockatxFetcher(ctrl)
for _, atx := range tc.existing {
require.NoError(t, atxs.Add(db, atx))
}
for i := range tc.fetched {
req := tc.fetched[i]
fetcher.EXPECT().
GetAtxs(tc.ctx, req.request).
Times(1).
DoAndReturn(func(_ context.Context, _ []types.ATXID) error {
for _, atx := range req.result {
require.NoError(t, atxs.Add(db, atx))
}
return req.error
})
}
require.Equal(t, tc.rst, Download(tc.ctx, tc.retry, logger.Zap(), db, fetcher, tc.set))
})
}
}
Loading

0 comments on commit 44d50ce

Please sign in to comment.