From 6ad328470442fa2cae41794293675aa974039c38 Mon Sep 17 00:00:00 2001 From: Marius van der Wijden Date: Wed, 30 Mar 2022 10:27:19 +0200 Subject: [PATCH] core: make reorg use less memory --- core/blockchain.go | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 4b25860acabee..c3daaa3c3ca6c 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -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() @@ -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