Skip to content

Commit 33f0823

Browse files
authored
Merge pull request #2394 from CortexFoundation/dev
setupSnapshot
2 parents ade6bad + 6e1295a commit 33f0823

1 file changed

Lines changed: 27 additions & 23 deletions

File tree

core/blockchain.go

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -417,29 +417,7 @@ func NewBlockChain(db ctxcdb.Database, cacheConfig *CacheConfig, chainConfig *pa
417417
}
418418
}
419419

420-
// Load any existing snapshot, regenerating it if loading failed
421-
if bc.cacheConfig.SnapshotLimit > 0 {
422-
// If the chain was rewound past the snapshot persistent layer (causing
423-
// a recovery block number to be persisted to disk), check if we're still
424-
// in recovery mode and in that case, don't invalidate the snapshot on a
425-
// head mismatch.
426-
var recover bool
427-
428-
head := bc.CurrentBlock()
429-
if layer := rawdb.ReadSnapshotRecoveryNumber(bc.db); layer != nil && *layer > head.NumberU64() {
430-
log.Warn("Enabling snapshot recovery", "chainhead", head.NumberU64(), "diskbase", *layer)
431-
recover = true
432-
}
433-
snapconfig := snapshot.Config{
434-
CacheSize: bc.cacheConfig.SnapshotLimit,
435-
Recovery: recover,
436-
NoBuild: bc.cacheConfig.SnapshotNoBuild,
437-
AsyncBuild: !bc.cacheConfig.SnapshotWait,
438-
}
439-
bc.snaps, _ = snapshot.New(snapconfig, bc.db, bc.stateCache.TrieDB(), head.Root())
440-
441-
bc.stateCache.SetSnapshot(bc.snaps)
442-
}
420+
bc.setupSnapshot()
443421

444422
// Start future block processor.
445423
bc.wg.Add(1)
@@ -465,6 +443,32 @@ func NewBlockChain(db ctxcdb.Database, cacheConfig *CacheConfig, chainConfig *pa
465443
return bc, nil
466444
}
467445

446+
func (bc *BlockChain) setupSnapshot() {
447+
// Load any existing snapshot, regenerating it if loading failed
448+
if bc.cacheConfig.SnapshotLimit > 0 {
449+
// If the chain was rewound past the snapshot persistent layer (causing
450+
// a recovery block number to be persisted to disk), check if we're still
451+
// in recovery mode and in that case, don't invalidate the snapshot on a
452+
// head mismatch.
453+
var recover bool
454+
455+
head := bc.CurrentBlock()
456+
if layer := rawdb.ReadSnapshotRecoveryNumber(bc.db); layer != nil && *layer > head.NumberU64() {
457+
log.Warn("Enabling snapshot recovery", "chainhead", head.NumberU64(), "diskbase", *layer)
458+
recover = true
459+
}
460+
snapconfig := snapshot.Config{
461+
CacheSize: bc.cacheConfig.SnapshotLimit,
462+
Recovery: recover,
463+
NoBuild: bc.cacheConfig.SnapshotNoBuild,
464+
AsyncBuild: !bc.cacheConfig.SnapshotWait,
465+
}
466+
bc.snaps, _ = snapshot.New(snapconfig, bc.db, bc.stateCache.TrieDB(), head.Root())
467+
468+
bc.stateCache.SetSnapshot(bc.snaps)
469+
}
470+
}
471+
468472
// empty returns an indicator whether the blockchain is empty.
469473
// Note, it's a special case that we connect a non-empty ancient
470474
// database with an empty node, so that we can plugin the ancient

0 commit comments

Comments
 (0)