@@ -22,16 +22,14 @@ import (
22
22
"github.com/spacemeshos/go-spacemesh/sql/malsync"
23
23
)
24
24
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 ()
33
27
}
34
28
29
+ type noCounter struct {}
30
+
31
+ func (noCounter ) Inc () {}
32
+
35
33
type Opt func (* Syncer )
36
34
37
35
func WithLogger (logger * zap.Logger ) Opt {
@@ -40,14 +38,6 @@ func WithLogger(logger *zap.Logger) Opt {
40
38
}
41
39
}
42
40
43
- type counter interface {
44
- Inc ()
45
- }
46
-
47
- type noCounter struct {}
48
-
49
- func (noCounter ) Inc () {}
50
-
51
41
func WithPeerErrMetric (counter counter ) Opt {
52
42
return func (s * Syncer ) {
53
43
s .peerErrMetric = counter
@@ -219,18 +209,18 @@ type Syncer struct {
219
209
cfg Config
220
210
fetcher fetcher
221
211
db sql.Executor
222
- localdb sql.LocalDatabase
212
+ localDB sql.LocalDatabase
223
213
clock clockwork.Clock
224
214
peerErrMetric counter
225
215
}
226
216
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 {
228
218
s := & Syncer {
229
219
logger : zap .NewNop (),
230
220
cfg : DefaultConfig (),
231
221
fetcher : fetcher ,
232
222
db : db ,
233
- localdb : localdb ,
223
+ localDB : localDB ,
234
224
clock : clockwork .NewRealClock (),
235
225
peerErrMetric : noCounter {},
236
226
}
@@ -241,7 +231,7 @@ func New(fetcher fetcher, db sql.Executor, localdb sql.LocalDatabase, opts ...Op
241
231
}
242
232
243
233
func (s * Syncer ) shouldSyncLegacy (epochStart , epochEnd time.Time ) (bool , error ) {
244
- timestamp , err := malsync .LegacySyncState (s .localdb )
234
+ timestamp , err := malsync .LegacySyncState (s .localDB )
245
235
if err != nil {
246
236
return false , fmt .Errorf ("error getting malfeasance sync state: %w" , err )
247
237
}
@@ -253,7 +243,7 @@ func (s *Syncer) shouldSyncLegacy(epochStart, epochEnd time.Time) (bool, error)
253
243
}
254
244
255
245
func (s * Syncer ) shouldSync (epochStart , epochEnd time.Time ) (bool , error ) {
256
- timestamp , err := malsync .SyncState (s .localdb )
246
+ timestamp , err := malsync .SyncState (s .localDB )
257
247
if err != nil {
258
248
return false , fmt .Errorf ("error getting malfeasance sync state: %w" , err )
259
249
}
@@ -447,7 +437,7 @@ func (s *Syncer) downloadNodeIDs(ctx context.Context, initial bool, updates chan
447
437
}
448
438
449
439
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 {
451
441
return malsync .UpdateLegacySyncState (tx , s .clock .Now ())
452
442
}); err != nil {
453
443
if ctx .Err () != nil {
@@ -458,12 +448,11 @@ func (s *Syncer) updateLegacyState(ctx context.Context) error {
458
448
}
459
449
return fmt .Errorf ("error updating legacy malsync state: %w" , err )
460
450
}
461
-
462
451
return nil
463
452
}
464
453
465
454
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 {
467
456
return malsync .UpdateSyncState (tx , s .clock .Now ())
468
457
}); err != nil {
469
458
if ctx .Err () != nil {
@@ -688,14 +677,16 @@ func (s *Syncer) EnsureInSync(ctx context.Context, epochStart, epochEnd time.Tim
688
677
return s .download (ctx , true )
689
678
}
690
679
691
- func (s * Syncer ) DownloadLoop (parent context.Context ) error {
680
+ func (s * Syncer ) DownloadLoop (parent context.Context , malSyncEnabled bool ) error {
692
681
eg , ctx := errgroup .WithContext (parent )
693
682
eg .Go (func () error {
694
683
return s .downloadLegacy (ctx , false )
695
684
})
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
+ }
699
690
return eg .Wait ()
700
691
}
701
692
0 commit comments