From e0b00d2e89e67494d442c425734f04958095fa9d Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 8 Dec 2022 15:03:19 +0100 Subject: [PATCH 1/4] core: pass block number into collectLogs --- core/blockchain.go | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index f9b92192cc7..8bdb36c45de 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1995,13 +1995,8 @@ func (bc *BlockChain) recoverAncestors(block *types.Block) (common.Hash, error) // collectLogs collects the logs that were generated or removed during // the processing of the block that corresponds with the given hash. // These logs are later announced as deleted or reborn. -func (bc *BlockChain) collectLogs(hash common.Hash, removed bool) []*types.Log { - number := bc.hc.GetBlockNumber(hash) - if number == nil { - return nil - } - receipts := rawdb.ReadReceipts(bc.db, hash, *number, bc.chainConfig) - +func (bc *BlockChain) collectLogs(hash common.Hash, number uint64, removed bool) []*types.Log { + receipts := rawdb.ReadReceipts(bc.db, hash, number, bc.chainConfig) var logs []*types.Log for _, receipt := range receipts { for _, log := range receipt.Logs { @@ -2147,7 +2142,8 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { bc.chainSideFeed.Send(ChainSideEvent{Block: oldChain[i]}) // Collect deleted logs for notification - if logs := bc.collectLogs(oldChain[i].Hash(), true); len(logs) > 0 { + h := oldChain[i] + if logs := bc.collectLogs(h.Hash(), h.NumberU64(), true); len(logs) > 0 { deletedLogs = append(deletedLogs, logs...) } if len(deletedLogs) > 512 { @@ -2162,7 +2158,8 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { // New logs: var rebirthLogs []*types.Log for i := len(newChain) - 1; i >= 1; i-- { - if logs := bc.collectLogs(newChain[i].Hash(), false); len(logs) > 0 { + h := newChain[i] + if logs := bc.collectLogs(h.Hash(), h.NumberU64(), false); len(logs) > 0 { rebirthLogs = append(rebirthLogs, logs...) } if len(rebirthLogs) > 512 { @@ -2217,7 +2214,7 @@ func (bc *BlockChain) SetCanonical(head *types.Block) (common.Hash, error) { bc.writeHeadBlock(head) // Emit events - logs := bc.collectLogs(head.Hash(), false) + logs := bc.collectLogs(head.Hash(), head.NumberU64(), false) bc.chainFeed.Send(ChainEvent{Block: head, Hash: head.Hash(), Logs: logs}) if len(logs) > 0 { bc.logsFeed.Send(logs) From c29bda77f4731879272a387efa383a9903928902 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 8 Dec 2022 15:08:53 +0100 Subject: [PATCH 2/4] core: rename variable --- core/blockchain.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 8bdb36c45de..21f51932237 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -2142,8 +2142,8 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { bc.chainSideFeed.Send(ChainSideEvent{Block: oldChain[i]}) // Collect deleted logs for notification - h := oldChain[i] - if logs := bc.collectLogs(h.Hash(), h.NumberU64(), true); len(logs) > 0 { + b := oldChain[i] + if logs := bc.collectLogs(b.Hash(), b.NumberU64(), true); len(logs) > 0 { deletedLogs = append(deletedLogs, logs...) } if len(deletedLogs) > 512 { @@ -2158,8 +2158,8 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { // New logs: var rebirthLogs []*types.Log for i := len(newChain) - 1; i >= 1; i-- { - h := newChain[i] - if logs := bc.collectLogs(h.Hash(), h.NumberU64(), false); len(logs) > 0 { + b := newChain[i] + if logs := bc.collectLogs(b.Hash(), b.NumberU64(), false); len(logs) > 0 { rebirthLogs = append(rebirthLogs, logs...) } if len(rebirthLogs) > 512 { From f8dd6d68dffd5ffe6c001213f7ee6d5b4e327cb6 Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 8 Dec 2022 15:19:26 +0100 Subject: [PATCH 3/4] core: use ReadRawReceipts in collectLogs --- core/blockchain.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 21f51932237..3e1aae0a5a5 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1995,8 +1995,10 @@ func (bc *BlockChain) recoverAncestors(block *types.Block) (common.Hash, error) // collectLogs collects the logs that were generated or removed during // the processing of the block that corresponds with the given hash. // These logs are later announced as deleted or reborn. -func (bc *BlockChain) collectLogs(hash common.Hash, number uint64, removed bool) []*types.Log { - receipts := rawdb.ReadReceipts(bc.db, hash, number, bc.chainConfig) +func (bc *BlockChain) collectLogs(b *types.Block, removed bool) []*types.Log { + receipts := rawdb.ReadRawReceipts(bc.db, b.Hash(), b.NumberU64()) + receipts.DeriveFields(bc.chainConfig, b.Hash(), b.NumberU64(), b.Transactions()) + var logs []*types.Log for _, receipt := range receipts { for _, log := range receipt.Logs { @@ -2142,8 +2144,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { bc.chainSideFeed.Send(ChainSideEvent{Block: oldChain[i]}) // Collect deleted logs for notification - b := oldChain[i] - if logs := bc.collectLogs(b.Hash(), b.NumberU64(), true); len(logs) > 0 { + if logs := bc.collectLogs(oldChain[i], true); len(logs) > 0 { deletedLogs = append(deletedLogs, logs...) } if len(deletedLogs) > 512 { @@ -2158,8 +2159,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error { // New logs: var rebirthLogs []*types.Log for i := len(newChain) - 1; i >= 1; i-- { - b := newChain[i] - if logs := bc.collectLogs(b.Hash(), b.NumberU64(), false); len(logs) > 0 { + if logs := bc.collectLogs(newChain[i], false); len(logs) > 0 { rebirthLogs = append(rebirthLogs, logs...) } if len(rebirthLogs) > 512 { @@ -2214,7 +2214,7 @@ func (bc *BlockChain) SetCanonical(head *types.Block) (common.Hash, error) { bc.writeHeadBlock(head) // Emit events - logs := bc.collectLogs(head.Hash(), head.NumberU64(), false) + logs := bc.collectLogs(head, false) bc.chainFeed.Send(ChainEvent{Block: head, Hash: head.Hash(), Logs: logs}) if len(logs) > 0 { bc.logsFeed.Send(logs) From 980bc7e902ab54efecc77e0ee33170dbebeeb2ab Mon Sep 17 00:00:00 2001 From: Felix Lange Date: Thu, 8 Dec 2022 15:31:35 +0100 Subject: [PATCH 4/4] Update blockchain.go --- core/blockchain.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 3e1aae0a5a5..1daf2b5b548 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1993,8 +1993,7 @@ func (bc *BlockChain) recoverAncestors(block *types.Block) (common.Hash, error) } // collectLogs collects the logs that were generated or removed during -// the processing of the block that corresponds with the given hash. -// These logs are later announced as deleted or reborn. +// the processing of a block. These logs are later announced as deleted or reborn. func (bc *BlockChain) collectLogs(b *types.Block, removed bool) []*types.Log { receipts := rawdb.ReadRawReceipts(bc.db, b.Hash(), b.NumberU64()) receipts.DeriveFields(bc.chainConfig, b.Hash(), b.NumberU64(), b.Transactions())