Skip to content

Commit fb435eb

Browse files
authored
fix: allow fast node to rewind after abnormal shutdown (bnb-chain#2401)
1 parent 5cc253a commit fb435eb

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

core/blockchain.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
414414
// Make sure the state associated with the block is available, or log out
415415
// if there is no available state, waiting for state sync.
416416
head := bc.CurrentBlock()
417-
if !bc.NoTries() && !bc.HasState(head.Root) {
417+
if !bc.HasState(head.Root) {
418418
if head.Number.Uint64() == 0 {
419419
// The genesis state is missing, which is only possible in the path-based
420420
// scheme. This situation occurs when the initial state sync is not finished
@@ -430,7 +430,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis
430430
if bc.cacheConfig.SnapshotLimit > 0 {
431431
diskRoot = rawdb.ReadSnapshotRoot(bc.db)
432432
}
433-
if bc.triedb.Scheme() == rawdb.PathScheme {
433+
if bc.triedb.Scheme() == rawdb.PathScheme && !bc.NoTries() {
434434
recoverable, _ := bc.triedb.Recoverable(diskRoot)
435435
if !bc.HasState(diskRoot) && !recoverable {
436436
diskRoot = bc.triedb.Head()
@@ -993,7 +993,7 @@ func (bc *BlockChain) rewindPathHead(head *types.Header, root common.Hash) (*typ
993993
// then block number zero is returned, indicating that snapshot recovery is disabled
994994
// and the whole snapshot should be auto-generated in case of head mismatch.
995995
func (bc *BlockChain) rewindHead(head *types.Header, root common.Hash) (*types.Header, uint64) {
996-
if bc.triedb.Scheme() == rawdb.PathScheme {
996+
if bc.triedb.Scheme() == rawdb.PathScheme && !bc.NoTries() {
997997
return bc.rewindPathHead(head, root)
998998
}
999999
return bc.rewindHashHead(head, root)
@@ -1030,6 +1030,19 @@ func (bc *BlockChain) setHeadBeyondRoot(head uint64, time uint64, root common.Ha
10301030
// block. Note, depth equality is permitted to allow using SetHead as a
10311031
// chain reparation mechanism without deleting any data!
10321032
if currentBlock := bc.CurrentBlock(); currentBlock != nil && header.Number.Uint64() <= currentBlock.Number.Uint64() {
1033+
// load bc.snaps for the judge `HasState`
1034+
if bc.NoTries() {
1035+
if bc.cacheConfig.SnapshotLimit > 0 {
1036+
snapconfig := snapshot.Config{
1037+
CacheSize: bc.cacheConfig.SnapshotLimit,
1038+
NoBuild: bc.cacheConfig.SnapshotNoBuild,
1039+
AsyncBuild: !bc.cacheConfig.SnapshotWait,
1040+
}
1041+
bc.snaps, _ = snapshot.New(snapconfig, bc.db, bc.triedb, header.Root, int(bc.cacheConfig.TriesInMemory), bc.NoTries())
1042+
}
1043+
defer func() { bc.snaps = nil }()
1044+
}
1045+
10331046
var newHeadBlock *types.Header
10341047
newHeadBlock, rootNumber = bc.rewindHead(header, root)
10351048
rawdb.WriteHeadBlockHash(db, newHeadBlock.Hash())

0 commit comments

Comments
 (0)