diff --git a/core/blockchain.go b/core/blockchain.go index 805197aab4..04da0bab8d 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -406,6 +406,11 @@ func (bc *BlockChain) SetHead(head uint64) error { } } rawdb.WriteHeadBlockHash(db, newHeadBlock.Hash()) + + // Degrade the chain markers if they are explicitly reverted. + // In thoery we should update all in-memory markers in the + // last step, however the direction of SetHead is from high + // to low, so it's safe the update in-memory markers directly. bc.currentBlock.Store(newHeadBlock) headBlockGauge.Update(int64(newHeadBlock.NumberU64())) } @@ -418,6 +423,11 @@ func (bc *BlockChain) SetHead(head uint64) error { newHeadFastBlock = bc.genesisBlock } rawdb.WriteHeadFastBlockHash(db, newHeadFastBlock.Hash()) + + // Degrade the chain markers if they are explicitly reverted. + // In thoery we should update all in-memory markers in the + // last step, however the direction of SetHead is from high + // to low, so it's safe the update in-memory markers directly. bc.currentFastBlock.Store(newHeadFastBlock) headFastBlockGauge.Update(int64(newHeadFastBlock.NumberU64())) } @@ -892,7 +902,7 @@ func (bc *BlockChain) Rollback(chain []common.Hash) { for i := len(chain) - 1; i >= 0; i-- { hash := chain[i] - // Degrade the chain markers if they are explictly reverted. + // Degrade the chain markers if they are explicitly reverted. // In thoery we should update all in-memory markers in the // last step, however the direction of rollback is from high // to low, so it's safe the update in-memory markers directly. @@ -904,13 +914,13 @@ func (bc *BlockChain) Rollback(chain []common.Hash) { } if currentFastBlock := bc.CurrentFastBlock(); currentFastBlock.Hash() == hash { newFastBlock := bc.GetBlock(currentFastBlock.ParentHash(), currentFastBlock.NumberU64()-1) - rawdb.WriteHeadFastBlockHash(batch, newFastBlock.Hash()) + rawdb.WriteHeadFastBlockHash(batch, currentFastBlock.ParentHash()) bc.currentFastBlock.Store(newFastBlock) headFastBlockGauge.Update(int64(newFastBlock.NumberU64())) } if currentBlock := bc.CurrentBlock(); currentBlock.Hash() == hash { newBlock := bc.GetBlock(currentBlock.ParentHash(), currentBlock.NumberU64()-1) - rawdb.WriteHeadBlockHash(batch, newBlock.Hash()) + rawdb.WriteHeadBlockHash(batch, currentBlock.ParentHash()) bc.currentBlock.Store(newBlock) headBlockGauge.Update(int64(newBlock.NumberU64())) } @@ -1246,7 +1256,7 @@ func (bc *BlockChain) InsertReceiptChain(blockChain types.Blocks, receiptChain [ var lastWrite uint64 -// writeBlockWithState writes only the block and its metadata to the database, +// writeBlockWithoutState writes only the block and its metadata to the database, // but does not write any state. This is used to construct competing side forks // up to the point where they exceed the canonical total difficulty. func (bc *BlockChain) writeBlockWithoutState(block *types.Block, td *big.Int) (err error) { diff --git a/core/headerchain.go b/core/headerchain.go index d129e479c4..f21dcf537e 100644 --- a/core/headerchain.go +++ b/core/headerchain.go @@ -519,6 +519,10 @@ func (hc *HeaderChain) SetHead(head uint64, updateFn UpdateHeadBlocksCallback, d if err := markerBatch.Write(); err != nil { log.Crit("Failed to update chain markers", "error", err) } + hc.currentHeader.Store(parent) + hc.currentHeaderHash = parentHash + headHeaderGauge.Update(parent.Number.Int64()) + // Remove the relative data from the database. if delFn != nil { delFn(batch, hash, num) @@ -527,10 +531,6 @@ func (hc *HeaderChain) SetHead(head uint64, updateFn UpdateHeadBlocksCallback, d rawdb.DeleteHeader(batch, hash, num) rawdb.DeleteTd(batch, hash, num) rawdb.DeleteCanonicalHash(batch, num) - - hc.currentHeader.Store(parent) - hc.currentHeaderHash = parentHash - headHeaderGauge.Update(parent.Number.Int64()) } // Flush all accumulated deletions. if err := batch.Write(); err != nil { diff --git a/light/lightchain.go b/light/lightchain.go index 864356cf93..6173bc7a29 100644 --- a/light/lightchain.go +++ b/light/lightchain.go @@ -206,6 +206,7 @@ func (lc *LightChain) ResetWithGenesisBlock(genesis *types.Block) { } lc.genesisBlock = genesis lc.hc.SetGenesis(lc.genesisBlock.Header()) + lc.hc.SetCurrentHeader(lc.genesisBlock.Header()) } // Accessors @@ -329,7 +330,7 @@ func (lc *LightChain) Rollback(chain []common.Hash) { for i := len(chain) - 1; i >= 0; i-- { hash := chain[i] - // Degrade the chain markers if they are explictly reverted. + // Degrade the chain markers if they are explicitly reverted. // In thoery we should update all in-memory markers in the // last step, however the direction of rollback is from high // to low, so it's safe the update in-memory markers directly.