@@ -93,11 +93,15 @@ type Module struct {
93
93
billet * mpt.Billet
94
94
95
95
jumpCallback func (p uint32 ) error
96
+
97
+ // stageChangedCallback is an optional callback that is triggered whenever
98
+ // the sync stage changes.
99
+ stageChangedCallback func ()
96
100
}
97
101
98
102
// NewModule returns new instance of statesync module.
99
103
func NewModule (bc Ledger , stateMod * stateroot.Module , log * zap.Logger , s * dao.Simple , jumpCallback func (p uint32 ) error ) * Module {
100
- if ! (bc .GetConfig ().P2PStateExchangeExtensions && bc .GetConfig ().Ledger .RemoveUntraceableBlocks ) {
104
+ if ! (bc .GetConfig ().P2PStateExchangeExtensions && bc .GetConfig ().Ledger .RemoveUntraceableBlocks ) && ! bc . GetConfig (). NeoFSStateSyncExtensions {
101
105
return & Module {
102
106
dao : s ,
103
107
bc : bc ,
@@ -120,7 +124,13 @@ func NewModule(bc Ledger, stateMod *stateroot.Module, log *zap.Logger, s *dao.Si
120
124
// Init initializes state sync module for the current chain's height with given
121
125
// callback for MPT nodes requests.
122
126
func (s * Module ) Init (currChainHeight uint32 ) error {
127
+ oldStage := s .syncStage
123
128
s .lock .Lock ()
129
+ defer func () {
130
+ if s .syncStage != oldStage {
131
+ s .notifyStageChanged ()
132
+ }
133
+ }()
124
134
defer s .lock .Unlock ()
125
135
126
136
if s .syncStage != none {
@@ -176,6 +186,20 @@ func (s *Module) Init(currChainHeight uint32) error {
176
186
return s .defineSyncStage ()
177
187
}
178
188
189
+ // SetOnStageChanged sets callback that is triggered whenever the sync stage changes.
190
+ func (s * Module ) SetOnStageChanged (cb func ()) {
191
+ s .lock .Lock ()
192
+ defer s .lock .Unlock ()
193
+ s .stageChangedCallback = cb
194
+ }
195
+
196
+ // notifyStageChanged triggers stage change callback if it's set.
197
+ func (s * Module ) notifyStageChanged () {
198
+ if s .stageChangedCallback != nil {
199
+ s .stageChangedCallback ()
200
+ }
201
+ }
202
+
179
203
// TemporaryPrefix accepts current storage prefix and returns prefix
180
204
// to use for storing intermediate items during synchronization.
181
205
func TemporaryPrefix (currPrefix storage.KeyPrefix ) storage.KeyPrefix {
@@ -287,7 +311,13 @@ func (s *Module) getLatestSavedBlock(p uint32) uint32 {
287
311
288
312
// AddHeaders validates and adds specified headers to the chain.
289
313
func (s * Module ) AddHeaders (hdrs ... * block.Header ) error {
314
+ oldStage := s .syncStage
290
315
s .lock .Lock ()
316
+ defer func () {
317
+ if s .syncStage != oldStage {
318
+ s .notifyStageChanged ()
319
+ }
320
+ }()
291
321
defer s .lock .Unlock ()
292
322
293
323
if s .syncStage != initialized {
@@ -306,7 +336,13 @@ func (s *Module) AddHeaders(hdrs ...*block.Header) error {
306
336
307
337
// AddBlock verifies and saves block skipping executable scripts.
308
338
func (s * Module ) AddBlock (block * block.Block ) error {
339
+ oldStage := s .syncStage
309
340
s .lock .Lock ()
341
+ defer func () {
342
+ if s .syncStage != oldStage {
343
+ s .notifyStageChanged ()
344
+ }
345
+ }()
310
346
defer s .lock .Unlock ()
311
347
312
348
if s .syncStage & headersSynced == 0 || s .syncStage & blocksSynced != 0 {
@@ -359,7 +395,13 @@ func (s *Module) AddBlock(block *block.Block) error {
359
395
// AddMPTNodes tries to add provided set of MPT nodes to the MPT billet if they are
360
396
// not yet collected.
361
397
func (s * Module ) AddMPTNodes (nodes [][]byte ) error {
398
+ oldStage := s .syncStage
362
399
s .lock .Lock ()
400
+ defer func () {
401
+ if s .syncStage != oldStage {
402
+ s .notifyStageChanged ()
403
+ }
404
+ }()
363
405
defer s .lock .Unlock ()
364
406
365
407
if s .syncStage & headersSynced == 0 || s .syncStage & mptSynced != 0 {
@@ -425,6 +467,12 @@ func (s *Module) restoreNode(n mpt.Node) error {
425
467
// If so, then jumping to P state sync point occurs. It is not protected by lock, thus caller
426
468
// should take care of it.
427
469
func (s * Module ) checkSyncIsCompleted () {
470
+ oldStage := s .syncStage
471
+ defer func () {
472
+ if s .syncStage != oldStage {
473
+ s .notifyStageChanged ()
474
+ }
475
+ }()
428
476
if s .syncStage != headersSynced | mptSynced | blocksSynced {
429
477
return
430
478
}
@@ -484,6 +532,14 @@ func (s *Module) NeedMPTNodes() bool {
484
532
return s .syncStage & headersSynced != 0 && s .syncStage & mptSynced == 0
485
533
}
486
534
535
+ // NeedBlocks returns whether the module hasn't completed blocks synchronisation.
536
+ func (s * Module ) NeedBlocks () bool {
537
+ s .lock .RLock ()
538
+ defer s .lock .RUnlock ()
539
+
540
+ return s .syncStage & headersSynced != 0 && s .syncStage & blocksSynced == 0
541
+ }
542
+
487
543
// Traverse traverses local MPT nodes starting from the specified root down to its
488
544
// children calling `process` for each serialised node until stop condition is satisfied.
489
545
func (s * Module ) Traverse (root util.Uint256 , process func (node mpt.Node , nodeBytes []byte ) bool ) error {
@@ -508,3 +564,13 @@ func (s *Module) GetUnknownMPTNodesBatch(limit int) []util.Uint256 {
508
564
509
565
return s .mptpool .GetBatch (limit )
510
566
}
567
+
568
+ // HeaderHeight returns the height of the latest stored header.
569
+ func (s * Module ) HeaderHeight () uint32 {
570
+ return s .bc .HeaderHeight ()
571
+ }
572
+
573
+ // GetConfig returns current blockchain configuration.
574
+ func (s * Module ) GetConfig () config.Blockchain {
575
+ return s .bc .GetConfig ()
576
+ }
0 commit comments