Skip to content

Commit 4ee432f

Browse files
committed
Add check if ATXv2 is live already for malsync (#6715)
## Motivation This adds a check to malfeasance sync to not sync with other peers before the first ATXv2 epoch.
1 parent 5907a15 commit 4ee432f

File tree

10 files changed

+433
-218
lines changed

10 files changed

+433
-218
lines changed

config/presets/fastnet.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func fastnet() config.Config {
2222

2323
conf.BaseConfig.OptFilterThreshold = 90
2424
conf.BaseConfig.DatabasePruneInterval = time.Minute
25-
conf.BaseConfig.DatabaseConnections = 16
25+
conf.BaseConfig.DatabaseConnections = 20
2626

2727
// set for systest TestEquivocation
2828
conf.BaseConfig.MinerGoodAtxsPercent = 50

node/node.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ func (app *App) initServices(ctx context.Context) error {
838838
),
839839
syncer.WithConfig(syncerConf),
840840
syncer.WithLogger(app.syncLogger.Zap()),
841+
syncer.WithAtxVersions(app.Config.AtxVersions),
841842
)
842843
if err != nil {
843844
return fmt.Errorf("create syncer: %w", err)

syncer/interface.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type atxSyncer interface {
3838
type malSyncer interface {
3939
EnsureLegacyInSync(parent context.Context, epochStart, epochEnd time.Time) error
4040
EnsureInSync(parent context.Context, epochStart, epochEnd time.Time) error
41-
DownloadLoop(parent context.Context) error
41+
DownloadLoop(parent context.Context, malSyncEnabled bool) error
4242
}
4343

4444
// fetcher is the interface to the low-level fetching.

syncer/malsync/interfaces.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package malsync
2+
3+
import (
4+
"context"
5+
6+
"github.com/spacemeshos/go-spacemesh/common/types"
7+
"github.com/spacemeshos/go-spacemesh/p2p"
8+
)
9+
10+
//go:generate mockgen -typed -package=mocks -destination=./mocks/mocks.go -source=./interfaces.go
11+
12+
type fetcher interface {
13+
SelectBestShuffled(int) []p2p.Peer
14+
LegacyMaliciousIDs(context.Context, p2p.Peer) ([]types.NodeID, error)
15+
MaliciousIDs(context.Context, p2p.Peer) ([]types.NodeID, error)
16+
LegacyMalfeasanceProofs(context.Context, []types.NodeID) error
17+
MalfeasanceProofs(context.Context, []types.NodeID) error
18+
}

syncer/malsync/mocks/mocks.go

Lines changed: 2 additions & 62 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

syncer/malsync/syncer.go

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,14 @@ import (
2222
"github.com/spacemeshos/go-spacemesh/sql/malsync"
2323
)
2424

25-
//go:generate mockgen -typed -package=mocks -destination=./mocks/mocks.go -source=./syncer.go
26-
27-
type fetcher interface {
28-
SelectBestShuffled(int) []p2p.Peer
29-
LegacyMaliciousIDs(context.Context, p2p.Peer) ([]types.NodeID, error)
30-
MaliciousIDs(context.Context, p2p.Peer) ([]types.NodeID, error)
31-
LegacyMalfeasanceProofs(context.Context, []types.NodeID) error
32-
MalfeasanceProofs(context.Context, []types.NodeID) error
25+
type counter interface {
26+
Inc()
3327
}
3428

29+
type noCounter struct{}
30+
31+
func (noCounter) Inc() {}
32+
3533
type Opt func(*Syncer)
3634

3735
func WithLogger(logger *zap.Logger) Opt {
@@ -40,14 +38,6 @@ func WithLogger(logger *zap.Logger) Opt {
4038
}
4139
}
4240

43-
type counter interface {
44-
Inc()
45-
}
46-
47-
type noCounter struct{}
48-
49-
func (noCounter) Inc() {}
50-
5141
func WithPeerErrMetric(counter counter) Opt {
5242
return func(s *Syncer) {
5343
s.peerErrMetric = counter
@@ -219,18 +209,18 @@ type Syncer struct {
219209
cfg Config
220210
fetcher fetcher
221211
db sql.Executor
222-
localdb sql.LocalDatabase
212+
localDB sql.LocalDatabase
223213
clock clockwork.Clock
224214
peerErrMetric counter
225215
}
226216

227-
func New(fetcher fetcher, db sql.Executor, localdb sql.LocalDatabase, opts ...Opt) *Syncer {
217+
func New(fetcher fetcher, db sql.Executor, localDB sql.LocalDatabase, opts ...Opt) *Syncer {
228218
s := &Syncer{
229219
logger: zap.NewNop(),
230220
cfg: DefaultConfig(),
231221
fetcher: fetcher,
232222
db: db,
233-
localdb: localdb,
223+
localDB: localDB,
234224
clock: clockwork.NewRealClock(),
235225
peerErrMetric: noCounter{},
236226
}
@@ -241,7 +231,7 @@ func New(fetcher fetcher, db sql.Executor, localdb sql.LocalDatabase, opts ...Op
241231
}
242232

243233
func (s *Syncer) shouldSyncLegacy(epochStart, epochEnd time.Time) (bool, error) {
244-
timestamp, err := malsync.LegacySyncState(s.localdb)
234+
timestamp, err := malsync.LegacySyncState(s.localDB)
245235
if err != nil {
246236
return false, fmt.Errorf("error getting malfeasance sync state: %w", err)
247237
}
@@ -253,7 +243,7 @@ func (s *Syncer) shouldSyncLegacy(epochStart, epochEnd time.Time) (bool, error)
253243
}
254244

255245
func (s *Syncer) shouldSync(epochStart, epochEnd time.Time) (bool, error) {
256-
timestamp, err := malsync.SyncState(s.localdb)
246+
timestamp, err := malsync.SyncState(s.localDB)
257247
if err != nil {
258248
return false, fmt.Errorf("error getting malfeasance sync state: %w", err)
259249
}
@@ -447,7 +437,7 @@ func (s *Syncer) downloadNodeIDs(ctx context.Context, initial bool, updates chan
447437
}
448438

449439
func (s *Syncer) updateLegacyState(ctx context.Context) error {
450-
if err := s.localdb.WithTxImmediate(ctx, func(tx sql.Transaction) error {
440+
if err := s.localDB.WithTxImmediate(ctx, func(tx sql.Transaction) error {
451441
return malsync.UpdateLegacySyncState(tx, s.clock.Now())
452442
}); err != nil {
453443
if ctx.Err() != nil {
@@ -458,12 +448,11 @@ func (s *Syncer) updateLegacyState(ctx context.Context) error {
458448
}
459449
return fmt.Errorf("error updating legacy malsync state: %w", err)
460450
}
461-
462451
return nil
463452
}
464453

465454
func (s *Syncer) updateState(ctx context.Context) error {
466-
if err := s.localdb.WithTxImmediate(ctx, func(tx sql.Transaction) error {
455+
if err := s.localDB.WithTxImmediate(ctx, func(tx sql.Transaction) error {
467456
return malsync.UpdateSyncState(tx, s.clock.Now())
468457
}); err != nil {
469458
if ctx.Err() != nil {
@@ -688,14 +677,16 @@ func (s *Syncer) EnsureInSync(ctx context.Context, epochStart, epochEnd time.Tim
688677
return s.download(ctx, true)
689678
}
690679

691-
func (s *Syncer) DownloadLoop(parent context.Context) error {
680+
func (s *Syncer) DownloadLoop(parent context.Context, malSyncEnabled bool) error {
692681
eg, ctx := errgroup.WithContext(parent)
693682
eg.Go(func() error {
694683
return s.downloadLegacy(ctx, false)
695684
})
696-
eg.Go(func() error {
697-
return s.download(ctx, false)
698-
})
685+
if malSyncEnabled {
686+
eg.Go(func() error {
687+
return s.download(ctx, false)
688+
})
689+
}
699690
return eg.Wait()
700691
}
701692

0 commit comments

Comments
 (0)