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 30, 2022
1 parent 28add78 commit 6ad3284
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions core/blockchain.go
Expand Up @@ -2029,20 +2029,16 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
}
// Insert the new chain(except the head block(reverse order)),
// taking care of the proper incremental order.
log.Warn("Long reorg", "newchain length", len(newChain), "from", newChain[0].number, "to", newChain[len(newChain)-1].number)
for i := len(newChain) - 1; i >= 1; i-- {
// Insert the block in the canonical way, re-writing history
block := bc.GetBlock(newChain[i].hash, newChain[i].number)

bc.writeHeadBlock(block)

// Collect reborn logs due to chain reorg
logs := bc.collectLogs(block.Hash(), false)
if len(logs) > 0 {
rebirthLogs = append(rebirthLogs, logs)
}
// Collect the new added 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.
indexesBatch := bc.db.NewBatch()
Expand All @@ -2061,6 +2057,15 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
if err := indexesBatch.Write(); err != nil {
log.Crit("Failed to delete useless indexes", "err", err)
}

// Collect the logs
for i := len(newChain) - 1; i >= 1; i-- {
// Collect reborn logs due to chain reorg
logs := bc.collectLogs(newChain[i].hash, false)
if len(logs) > 0 {
rebirthLogs = append(rebirthLogs, logs)
}
}
// If any logs need to be fired, do it now. In theory we could avoid creating
// this goroutine if there are no events to fire, but realistcally that only
// ever happens if we're reorging empty blocks, which will only happen on idle
Expand Down

0 comments on commit 6ad3284

Please sign in to comment.