diff options
author | Péter Szilágyi <peterke@gmail.com> | 2018-01-22 20:07:47 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2018-01-22 20:11:07 +0800 |
commit | 84be00915416872b6136b06a6f7b12b095585e36 (patch) | |
tree | a7e1f9655c45588457f4a049a84ecc46b8e8527d /core/blockchain.go | |
parent | 02aeb3d76652a4c0451e5c3734e6881aefe46249 (diff) | |
download | dexon-84be00915416872b6136b06a6f7b12b095585e36.tar.gz dexon-84be00915416872b6136b06a6f7b12b095585e36.tar.zst dexon-84be00915416872b6136b06a6f7b12b095585e36.zip |
core: sorted reorg insertion order for proper head header updating
Diffstat (limited to 'core/blockchain.go')
-rw-r--r-- | core/blockchain.go | 13 |
1 files changed, 6 insertions, 7 deletions
diff --git a/core/blockchain.go b/core/blockchain.go index 737fbe3ee..f886ffe4e 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -465,7 +465,7 @@ func (bc *BlockChain) insert(block *types.Block) { } bc.currentBlock = block - // If the block is better than out head or is on a different chain, force update heads + // If the block is better than our head or is on a different chain, force update heads if updateHeads { bc.hc.SetCurrentHeader(block.Header()) @@ -1140,18 +1140,17 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { } else { log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "newnum", newBlock.Number(), "newhash", newBlock.Hash()) } + // Insert the new chain, taking care of the proper incremental order var addedTxs types.Transactions - // insert blocks. Order does not matter. Last block will be written in ImportChain itself which creates the new head properly - for _, block := range newChain { + for i := len(newChain) - 1; i >= 0; i-- { // insert the block in the canonical way, re-writing history - bc.insert(block) + bc.insert(newChain[i]) // write lookup entries for hash based transaction/receipt searches - if err := WriteTxLookupEntries(bc.chainDb, block); err != nil { + if err := WriteTxLookupEntries(bc.chainDb, newChain[i]); err != nil { return err } - addedTxs = append(addedTxs, block.Transactions()...) + addedTxs = append(addedTxs, newChain[i].Transactions()...) } - // calculate the difference between deleted and added transactions diff := types.TxDifference(deletedTxs, addedTxs) // When transactions get deleted from the database that means the |