Skip to content

Commit

Permalink
core: make reorg use less memory
Browse files Browse the repository at this point in the history
  • Loading branch information
MariusVanDerWijden committed Mar 29, 2022
1 parent 0fffd3a commit 28add78
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1934,8 +1934,13 @@ func mergeLogs(logs [][]*types.Log, reverse bool) []*types.Log {
// Note the new head block won't be processed here, callers need to handle it
// externally.
func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
type block struct {
hash common.Hash
number uint64
}

var (
newChain types.Blocks
newChain []block
oldChain types.Blocks
commonBlock *types.Block

Expand All @@ -1961,7 +1966,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
} else {
// New chain is longer, stash all blocks away for subsequent insertion
for ; newBlock != nil && newBlock.NumberU64() != oldBlock.NumberU64(); newBlock = bc.GetBlock(newBlock.ParentHash(), newBlock.NumberU64()-1) {
newChain = append(newChain, newBlock)
newChain = append(newChain, block{hash: newBlock.Hash(), number: newBlock.NumberU64()})
}
}
if oldBlock == nil {
Expand All @@ -1987,7 +1992,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
if len(logs) > 0 {
deletedLogs = append(deletedLogs, logs)
}
newChain = append(newChain, newBlock)
newChain = append(newChain, block{hash: newBlock.Hash(), number: newBlock.NumberU64()})

// Step back with both chains
oldBlock = bc.GetBlock(oldBlock.ParentHash(), oldBlock.NumberU64()-1)
Expand All @@ -2008,14 +2013,14 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
logFn = log.Warn
}
logFn(msg, "number", commonBlock.Number(), "hash", commonBlock.Hash(),
"drop", len(oldChain), "dropfrom", oldChain[0].Hash(), "add", len(newChain), "addfrom", newChain[0].Hash())
"drop", len(oldChain), "dropfrom", oldChain[0].Hash(), "add", len(newChain), "addfrom", newChain[0].hash)
blockReorgAddMeter.Mark(int64(len(newChain)))
blockReorgDropMeter.Mark(int64(len(oldChain)))
blockReorgMeter.Mark(1)
} else if len(newChain) > 0 {
// Special case happens in the post merge stage that current head is
// the ancestor of new head while these two blocks are not consecutive
log.Info("Extend chain", "add", len(newChain), "number", newChain[0].NumberU64(), "hash", newChain[0].Hash())
log.Info("Extend chain", "add", len(newChain), "number", newChain[0].number, "hash", newChain[0].hash)
blockReorgAddMeter.Mark(int64(len(newChain)))
} else {
// len(newChain) == 0 && len(oldChain) > 0
Expand All @@ -2026,15 +2031,17 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
// taking care of the proper incremental order.
for i := len(newChain) - 1; i >= 1; i-- {
// Insert the block in the canonical way, re-writing history
bc.writeHeadBlock(newChain[i])
block := bc.GetBlock(newChain[i].hash, newChain[i].number)

bc.writeHeadBlock(block)

// Collect reborn logs due to chain reorg
logs := bc.collectLogs(newChain[i].Hash(), false)
logs := bc.collectLogs(block.Hash(), false)
if len(logs) > 0 {
rebirthLogs = append(rebirthLogs, logs)
}
// Collect the new added transactions.
addedTxs = append(addedTxs, newChain[i].Transactions()...)
addedTxs = append(addedTxs, block.Transactions()...)
}
// Delete useless indexes right now which includes the non-canonical
// transaction indexes, canonical chain indexes which above the head.
Expand Down

0 comments on commit 28add78

Please sign in to comment.