@@ -1465,17 +1465,21 @@ func (bc *BlockChain) insertSidechain(block *types.Block, it *insertIterator) (i
14651465 return 0 , nil , nil , nil
14661466}
14671467
1468- // reorgs takes two blocks, an old chain and a new chain and will reconstruct the blocks and inserts them
1469- // to be part of the new canonical chain and accumulates potential missing transactions and post an
1470- // event about them
1468+ // reorg takes two blocks, an old chain and a new chain and will reconstruct the
1469+ // blocks and inserts them to be part of the new canonical chain and accumulates
1470+ // potential missing transactions and post an event about them.
14711471func (bc * BlockChain ) reorg (oldBlock , newBlock * types.Block ) error {
14721472 var (
14731473 newChain types.Blocks
14741474 oldChain types.Blocks
14751475 commonBlock * types.Block
1476- deletedTxs types.Transactions
1476+
1477+ deletedTxs types.Transactions
1478+ addedTxs types.Transactions
1479+
14771480 deletedLogs []* types.Log
14781481 rebirthLogs []* types.Log
1482+
14791483 // collectLogs collects the logs that were generated during the
14801484 // processing of the block that corresponds with the given hash.
14811485 // These logs are later announced as deleted or reborn
@@ -1498,46 +1502,49 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
14981502 }
14991503 }
15001504 )
1501-
1502- // first reduce whoever is higher bound
1505+ // Reduce the longer chain to the same number as the shorter one
15031506 if oldBlock .NumberU64 () > newBlock .NumberU64 () {
1504- // reduce old chain
1507+ // Old chain is longer, gather all transactions and logs as deleted ones
15051508 for ; oldBlock != nil && oldBlock .NumberU64 () != newBlock .NumberU64 (); oldBlock = bc .GetBlock (oldBlock .ParentHash (), oldBlock .NumberU64 ()- 1 ) {
15061509 oldChain = append (oldChain , oldBlock )
15071510 deletedTxs = append (deletedTxs , oldBlock .Transactions ()... )
1508-
15091511 collectLogs (oldBlock .Hash (), true )
15101512 }
15111513 } else {
1512- // reduce new chain and append new chain blocks for inserting later on
1514+ // New chain is longer, stash all blocks away for subsequent insertion
15131515 for ; newBlock != nil && newBlock .NumberU64 () != oldBlock .NumberU64 (); newBlock = bc .GetBlock (newBlock .ParentHash (), newBlock .NumberU64 ()- 1 ) {
15141516 newChain = append (newChain , newBlock )
15151517 }
15161518 }
15171519 if oldBlock == nil {
1518- return fmt .Errorf ("Invalid old chain" )
1520+ return fmt .Errorf ("invalid old chain" )
15191521 }
15201522 if newBlock == nil {
1521- return fmt .Errorf ("Invalid new chain" )
1523+ return fmt .Errorf ("invalid new chain" )
15221524 }
1523-
1525+ // Both sides of the reorg are at the same number, reduce both until the common
1526+ // ancestor is found
15241527 for {
1528+ // If the common ancestor was found, bail out
15251529 if oldBlock .Hash () == newBlock .Hash () {
15261530 commonBlock = oldBlock
15271531 break
15281532 }
1529-
1533+ // Remove an old block as well as stash away a new block
15301534 oldChain = append (oldChain , oldBlock )
1531- newChain = append (newChain , newBlock )
15321535 deletedTxs = append (deletedTxs , oldBlock .Transactions ()... )
15331536 collectLogs (oldBlock .Hash (), true )
15341537
1535- oldBlock , newBlock = bc .GetBlock (oldBlock .ParentHash (), oldBlock .NumberU64 ()- 1 ), bc .GetBlock (newBlock .ParentHash (), newBlock .NumberU64 ()- 1 )
1538+ newChain = append (newChain , newBlock )
1539+
1540+ // Step back with both chains
1541+ oldBlock = bc .GetBlock (oldBlock .ParentHash (), oldBlock .NumberU64 ()- 1 )
15361542 if oldBlock == nil {
1537- return fmt .Errorf ("Invalid old chain" )
1543+ return fmt .Errorf ("invalid old chain" )
15381544 }
1545+ newBlock = bc .GetBlock (newBlock .ParentHash (), newBlock .NumberU64 ()- 1 )
15391546 if newBlock == nil {
1540- return fmt .Errorf ("Invalid new chain" )
1547+ return fmt .Errorf ("invalid new chain" )
15411548 }
15421549 }
15431550 // Ensure the user sees large reorgs
@@ -1552,42 +1559,46 @@ func (bc *BlockChain) reorg(oldBlock, newBlock *types.Block) error {
15521559 log .Error ("Impossible reorg, please file an issue" , "oldnum" , oldBlock .Number (), "oldhash" , oldBlock .Hash (), "newnum" , newBlock .Number (), "newhash" , newBlock .Hash ())
15531560 }
15541561 // Insert the new chain, taking care of the proper incremental order
1555- var addedTxs types.Transactions
15561562 for i := len (newChain ) - 1 ; i >= 0 ; i -- {
1557- // insert the block in the canonical way, re-writing history
1563+ // Insert the block in the canonical way, re-writing history
15581564 bc .insert (newChain [i ])
1559- // collect reborn logs due to chain reorg(except head block)
1565+
1566+ // Collect reborn logs due to chain reorg (except head block (reverse order))
15601567 if i != 0 {
15611568 collectLogs (newChain [i ].Hash (), false )
15621569 }
1563- // write lookup entries for hash based transaction/receipt searches
1570+ // Write lookup entries for hash based transaction/receipt searches
15641571 rawdb .WriteTxLookupEntries (bc .db , newChain [i ])
15651572 addedTxs = append (addedTxs , newChain [i ].Transactions ()... )
15661573 }
1567- // calculate the difference between deleted and added transactions
1568- diff := types .TxDifference (deletedTxs , addedTxs )
1569- // When transactions get deleted from the database that means the
1570- // receipts that were created in the fork must also be deleted
1574+ // When transactions get deleted from the database, the receipts that were
1575+ // created in the fork must also be deleted
15711576 batch := bc .db .NewBatch ()
1572- for _ , tx := range diff {
1577+ for _ , tx := range types . TxDifference ( deletedTxs , addedTxs ) {
15731578 rawdb .DeleteTxLookupEntry (batch , tx .Hash ())
15741579 }
15751580 batch .Write ()
15761581
1577- if len (deletedLogs ) > 0 {
1578- go bc .rmLogsFeed .Send (RemovedLogsEvent {deletedLogs })
1579- }
1580- if len (rebirthLogs ) > 0 {
1581- go bc .logsFeed .Send (rebirthLogs )
1582- }
1583- if len (oldChain ) > 0 {
1584- go func () {
1582+ // If any logs need to be fired, do it now. In theory we could avoid creating
1583+ // this goroutine if there are no events to fire, but realistcally that only
1584+ // ever happens if we're reorging empty blocks, which will only happen on idle
1585+ // networks where performance is not an issue either way.
1586+ //
1587+ // TODO(karalabe): Can we get rid of the goroutine somehow to guarantee correct
1588+ // event ordering?
1589+ go func () {
1590+ if len (deletedLogs ) > 0 {
1591+ bc .rmLogsFeed .Send (RemovedLogsEvent {deletedLogs })
1592+ }
1593+ if len (rebirthLogs ) > 0 {
1594+ bc .logsFeed .Send (rebirthLogs )
1595+ }
1596+ if len (oldChain ) > 0 {
15851597 for _ , block := range oldChain {
15861598 bc .chainSideFeed .Send (ChainSideEvent {Block : block })
15871599 }
1588- }()
1589- }
1590-
1600+ }
1601+ }()
15911602 return nil
15921603}
15931604
0 commit comments