@@ -296,7 +296,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
296
296
if diskRoot != (common.Hash {}) {
297
297
log .Warn ("Head state missing, repairing" , "number" , head .Number (), "hash" , head .Hash (), "snaproot" , diskRoot )
298
298
299
- snapDisk , err := bc .SetHeadBeyondRoot (head .NumberU64 (), diskRoot )
299
+ snapDisk , err := bc .setHeadBeyondRoot (head .NumberU64 (), diskRoot , true )
300
300
if err != nil {
301
301
return nil , err
302
302
}
@@ -306,7 +306,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
306
306
}
307
307
} else {
308
308
log .Warn ("Head state missing, repairing" , "number" , head .Number (), "hash" , head .Hash ())
309
- if err := bc .SetHead (head .NumberU64 ()); err != nil {
309
+ if _ , err := bc .setHeadBeyondRoot (head .NumberU64 (), common. Hash {}, true ); err != nil {
310
310
return nil , err
311
311
}
312
312
}
@@ -482,19 +482,19 @@ func (bc *BlockChain) loadLastState() error {
482
482
// was fast synced or full synced and in which state, the method will try to
483
483
// delete minimal data from disk whilst retaining chain consistency.
484
484
func (bc * BlockChain ) SetHead (head uint64 ) error {
485
- _ , err := bc .SetHeadBeyondRoot (head , common.Hash {})
485
+ _ , err := bc .setHeadBeyondRoot (head , common.Hash {}, false )
486
486
return err
487
487
}
488
488
489
- // SetHeadBeyondRoot rewinds the local chain to a new head with the extra condition
489
+ // setHeadBeyondRoot rewinds the local chain to a new head with the extra condition
490
490
// that the rewind must pass the specified state root. This method is meant to be
491
491
// used when rewinding with snapshots enabled to ensure that we go back further than
492
492
// persistent disk layer. Depending on whether the node was fast synced or full, and
493
493
// in which state, the method will try to delete minimal data from disk whilst
494
494
// retaining chain consistency.
495
495
//
496
496
// The method returns the block number where the requested root cap was found.
497
- func (bc * BlockChain ) SetHeadBeyondRoot (head uint64 , root common.Hash ) (uint64 , error ) {
497
+ func (bc * BlockChain ) setHeadBeyondRoot (head uint64 , root common.Hash , repair bool ) (uint64 , error ) {
498
498
if ! bc .chainmu .TryLock () {
499
499
return 0 , errChainStopped
500
500
}
@@ -509,7 +509,7 @@ func (bc *BlockChain) SetHeadBeyondRoot(head uint64, root common.Hash) (uint64,
509
509
frozen , _ := bc .db .Ancients ()
510
510
511
511
updateFn := func (db ethdb.KeyValueWriter , header * types.Header ) (uint64 , bool ) {
512
- // Rewind the block chain , ensuring we don't end up with a stateless head
512
+ // Rewind the blockchain , ensuring we don't end up with a stateless head
513
513
// block. Note, depth equality is permitted to allow using SetHead as a
514
514
// chain reparation mechanism without deleting any data!
515
515
if currentBlock := bc .CurrentBlock (); currentBlock != nil && header .Number .Uint64 () <= currentBlock .NumberU64 () {
@@ -610,8 +610,8 @@ func (bc *BlockChain) SetHeadBeyondRoot(head uint64, root common.Hash) (uint64,
610
610
}
611
611
// If SetHead was only called as a chain reparation method, try to skip
612
612
// touching the header chain altogether, unless the freezer is broken
613
- if block := bc . CurrentBlock (); block . NumberU64 () == head {
614
- if target , force := updateFn (bc .db , block .Header ()); force {
613
+ if repair {
614
+ if target , force := updateFn (bc .db , bc . CurrentBlock () .Header ()); force {
615
615
bc .hc .SetHead (target , updateFn , delFn )
616
616
}
617
617
} else {
0 commit comments