core: revert log emission order

This commit is contained in:
Péter Szilágyi 2024-10-16 16:37:57 +03:00
parent 3da0fa5ec2
commit b0799c24e7

View file

@ -2268,15 +2268,14 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
// rewind the canonical chain to a lower point. // rewind the canonical chain to a lower point.
log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "oldblocks", len(oldChain), "newnum", newBlock.Number(), "newhash", newBlock.Hash(), "newblocks", len(newChain)) log.Error("Impossible reorg, please file an issue", "oldnum", oldBlock.Number(), "oldhash", oldBlock.Hash(), "oldblocks", len(oldChain), "newnum", newBlock.Number(), "newhash", newBlock.Hash(), "newblocks", len(newChain))
} }
// Acquire the tx-lookup lock before mutation. This step is essential
//var stats runtime.MemStats // as the txlookups should be changed atomically, and all subsequent
//runtime.ReadMemStats(&stats) // reads should be blocked until the mutation is complete.
//panic(stats.HeapInuse) bc.txLookupLock.Lock()
// Insert the new chain segment in incremental order, from the old // Insert the new chain segment in incremental order, from the old
// to the new. The new chain head (newChain[0]) is not inserted here, // to the new. The new chain head (newChain[0]) is not inserted here,
// as it will be handled separately outside of this function // as it will be handled separately outside of this function
var rebirthLogs []*types.Log
for i := len(newChain) - 1; i >= 1; i-- { for i := len(newChain) - 1; i >= 1; i-- {
// Insert the block in the canonical way, re-writing history // Insert the block in the canonical way, re-writing history
newBlock = bc.GetBlock(newChain[i].Hash(), newChain[i].Number.Uint64()) newBlock = bc.GetBlock(newChain[i].Hash(), newChain[i].Number.Uint64())
@ -2286,24 +2285,7 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
for _, tx := range newBlock.Transactions() { for _, tx := range newBlock.Transactions() {
addedTxs = append(addedTxs, tx.Hash()) addedTxs = append(addedTxs, tx.Hash())
} }
// Collect the logs and send them in batches of 512.
if logs := bc.collectLogs(newBlock, false); len(logs) > 0 {
rebirthLogs = append(rebirthLogs, logs...)
} }
if len(rebirthLogs) > 512 {
bc.logsFeed.Send(rebirthLogs)
rebirthLogs = nil
}
}
if len(rebirthLogs) > 0 {
bc.logsFeed.Send(rebirthLogs)
}
// Acquire the tx-lookup lock before mutation. This step is essential
// as the txlookups should be changed atomically, and all subsequent
// reads should be blocked until the mutation is complete.
bc.txLookupLock.Lock()
// Delete useless indexes right now which includes the non-canonical // Delete useless indexes right now which includes the non-canonical
// transaction indexes, canonical chain indexes which above the head. // transaction indexes, canonical chain indexes which above the head.
var ( var (
@ -2336,9 +2318,9 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
// Release the tx-lookup lock after mutation. // Release the tx-lookup lock after mutation.
bc.txLookupLock.Unlock() bc.txLookupLock.Unlock()
// Send out events for logs from the old canon chain. // Send out events for logs from the old canon chain, and 'reborn'
// The number of logs can be very high, // logs from the new canon chain. The number of logs can be very
// so the events are sent in batches of size around 512. // high, so the events are sent in batches of size around 512.
// Deleted logs + blocks: // Deleted logs + blocks:
var deletedLogs []*types.Log var deletedLogs []*types.Log
@ -2356,6 +2338,21 @@ func (bc *BlockChain) reorg(oldHead *types.Header, newHead *types.Block) error {
if len(deletedLogs) > 0 { if len(deletedLogs) > 0 {
bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs}) bc.rmLogsFeed.Send(RemovedLogsEvent{deletedLogs})
} }
// New logs:
var rebirthLogs []*types.Log
for i := len(newChain) - 1; i >= 1; i-- {
newBlock = bc.GetBlock(newChain[i].Hash(), newChain[i].Number.Uint64())
if logs := bc.collectLogs(newBlock, false); len(logs) > 0 {
rebirthLogs = append(rebirthLogs, logs...)
}
if len(rebirthLogs) > 512 {
bc.logsFeed.Send(rebirthLogs)
rebirthLogs = nil
}
}
if len(rebirthLogs) > 0 {
bc.logsFeed.Send(rebirthLogs)
}
return nil return nil
} }