Skip to content

Commit d9c13d4

Browse files
committed
core, eth/downloader: fix resetting below freezer threshold
1 parent 6f2c3f2 commit d9c13d4

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

core/blockchain.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
296296
if diskRoot != (common.Hash{}) {
297297
log.Warn("Head state missing, repairing", "number", head.Number(), "hash", head.Hash(), "snaproot", diskRoot)
298298

299-
snapDisk, err := bc.SetHeadBeyondRoot(head.NumberU64(), diskRoot)
299+
snapDisk, err := bc.setHeadBeyondRoot(head.NumberU64(), diskRoot, true)
300300
if err != nil {
301301
return nil, err
302302
}
@@ -306,7 +306,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
306306
}
307307
} else {
308308
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 {
310310
return nil, err
311311
}
312312
}
@@ -482,19 +482,19 @@ func (bc *BlockChain) loadLastState() error {
482482
// was fast synced or full synced and in which state, the method will try to
483483
// delete minimal data from disk whilst retaining chain consistency.
484484
func (bc *BlockChain) SetHead(head uint64) error {
485-
_, err := bc.SetHeadBeyondRoot(head, common.Hash{})
485+
_, err := bc.setHeadBeyondRoot(head, common.Hash{}, false)
486486
return err
487487
}
488488

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
490490
// that the rewind must pass the specified state root. This method is meant to be
491491
// used when rewinding with snapshots enabled to ensure that we go back further than
492492
// persistent disk layer. Depending on whether the node was fast synced or full, and
493493
// in which state, the method will try to delete minimal data from disk whilst
494494
// retaining chain consistency.
495495
//
496496
// 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) {
498498
if !bc.chainmu.TryLock() {
499499
return 0, errChainStopped
500500
}
@@ -509,7 +509,7 @@ func (bc *BlockChain) SetHeadBeyondRoot(head uint64, root common.Hash) (uint64,
509509
frozen, _ := bc.db.Ancients()
510510

511511
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
513513
// block. Note, depth equality is permitted to allow using SetHead as a
514514
// chain reparation mechanism without deleting any data!
515515
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,
610610
}
611611
// If SetHead was only called as a chain reparation method, try to skip
612612
// 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 {
615615
bc.hc.SetHead(target, updateFn, delFn)
616616
}
617617
} else {

eth/downloader/downloader.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ func (d *Downloader) syncWithPeer(p *peerConnection, hash common.Hash, td *big.I
535535
}
536536
// Rewind the ancient store and blockchain if reorg happens.
537537
if origin+1 < frozen {
538-
if err := d.lightchain.SetHead(origin + 1); err != nil {
538+
if err := d.lightchain.SetHead(origin); err != nil {
539539
return err
540540
}
541541
}

0 commit comments

Comments
 (0)