Skip to content

Commit

Permalink
core: minor refactor, send block-drop feed earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
holiman committed Sep 9, 2022
1 parent 4169105 commit 45d1d22
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 19 deletions.
26 changes: 8 additions & 18 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2121,15 +2121,16 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
log.Crit("Failed to delete useless indexes", "err", err)
}

// Send out events for deleted logs. The number of
// restored logs can be very high, so the events are sent in batches of
// size < 512.
// Send out events for deleted and new logs. The number of logs can be very
// high, so the events are sent in batches of size around 512.

// Deleted logs + blocks
var deletedLogs []*types.Log
for i := len(oldChain) - 1; i >= 0; i-- {
oldBlock := oldChain[i]
// Also send event for blocks removed from the canon chain.
bc.chainSideFeed.Send(ChainSideEvent{Block: oldChain[i]})
// Collect deleted logs for notification
if logs := bc.collectLogs(oldBlock.Hash(), true); len(logs) > 0 {
//deletedLogs = append(deletedLogs, logs)
if logs := bc.collectLogs(oldChain[i].Hash(), true); len(logs) > 0 {
deletedLogs = append(deletedLogs, logs...)
}
if len(deletedLogs) > 512 {
Expand All @@ -2140,11 +2141,7 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
if len(deletedLogs) > 0 {
bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs})
}

// Send events for 'reborn' logs, i.e. logs that were previously
// removed but now got restored in the new chain branch. The number of
// restored logs can be very high, so the events are sent in batches of
// size < 512.
// New logs
var rebirthLogs []*types.Log
for i := len(newChain) - 1; i >= 1; i-- {
// Collect reborn logs due to chain reorg
Expand All @@ -2159,13 +2156,6 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
if len(rebirthLogs) > 0 {
bc.logsFeed.Send(rebirthLogs)
}

// Send notifications for blocks removed from the canon chain.
if len(oldChain) > 0 {
for i := len(oldChain) - 1; i >= 0; i-- {
bc.chainSideFeed.Send(ChainSideEvent{Block: oldChain[i]})
}
}
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,7 @@ func TestLogRebirth(t *testing.T) {
blockchain.SubscribeLogsEvent(newLogCh)
blockchain.SubscribeRemovedLogsEvent(rmLogsCh)

// This chain contains a single log.
// This chain contains 10 logs.
genDb, chain, _ := GenerateChainWithGenesis(gspec, engine, 3, func(i int, gen *BlockGen) {
if i < 2 {
for ii := 0; ii < 5; ii++ {
Expand Down

0 comments on commit 45d1d22

Please sign in to comment.