Skip to content

Commit

Permalink
core: save new blocks during reorg (#599)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzliudan authored Aug 2, 2024
1 parent 824c531 commit 31098e7
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ func (bc *BlockChain) ResetWithGenesisBlock(genesis *types.Block) error {
log.Crit("Failed to write genesis block", "err", err)
}
bc.genesisBlock = genesis
bc.insert(bc.genesisBlock)
bc.insert(bc.genesisBlock, false)
bc.currentBlock.Store(bc.genesisBlock)
bc.hc.SetGenesis(bc.genesisBlock.Header())
bc.hc.SetCurrentHeader(bc.genesisBlock.Header())
Expand Down Expand Up @@ -680,7 +680,7 @@ func (bc *BlockChain) ExportN(w io.Writer, first uint64, last uint64) error {
// or if they are on a different side chain.
//
// Note, this function assumes that the `mu` mutex is held!
func (bc *BlockChain) insert(block *types.Block) {
func (bc *BlockChain) insert(block *types.Block, writeBlock bool) {
// If the block is on a side chain or an unknown one, force other heads onto it too
updateHeads := GetCanonicalHash(bc.db, block.NumberU64()) != block.Hash()

Expand All @@ -691,6 +691,11 @@ func (bc *BlockChain) insert(block *types.Block) {
if err := WriteHeadBlockHash(bc.db, block.Hash()); err != nil {
log.Crit("Failed to insert head block hash", "err", err)
}
if writeBlock {
if err := WriteBlock(bc.db, block); err != nil {
log.Crit("Failed to insert block", "err", err)
}
}
bc.currentBlock.Store(block)

// save cache BlockSigners
Expand Down Expand Up @@ -1422,7 +1427,8 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.

// Set new head.
if status == CanonStatTy {
bc.insert(block)
// WriteBlock has already been called, no need to write again
bc.insert(block, false)
// prepare set of masternodes for the next epoch
if bc.chainConfig.XDPoS != nil && ((block.NumberU64() % bc.chainConfig.XDPoS.Epoch) == (bc.chainConfig.XDPoS.Epoch - bc.chainConfig.XDPoS.Gap)) {
err := bc.UpdateM1()
Expand Down Expand Up @@ -2265,7 +2271,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
var addedTxs types.Transactions
for i := len(newChain) - 1; i >= 0; i-- {
// insert the block in the canonical way, re-writing history
bc.insert(newChain[i])
bc.insert(newChain[i], true)
// write lookup entries for hash based transaction/receipt searches
if err := WriteTxLookupEntries(bc.db, newChain[i]); err != nil {
return err
Expand Down

0 comments on commit 31098e7

Please sign in to comment.