@@ -1480,7 +1480,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool)
14801480 } else {
14811481 // We're post-merge and the parent is pruned, try to recover the parent state
14821482 log .Debug ("Pruned ancestor" , "number" , block .Number (), "hash" , block .Hash ())
1483- return it .index , bc .recoverAncestors (block )
1483+ _ , err := bc .recoverAncestors (block )
1484+ return it .index , err
14841485 }
14851486 // First block is future, shove it (and all children) to the future queue (unknown ancestor)
14861487 case errors .Is (err , consensus .ErrFutureBlock ) || (errors .Is (err , consensus .ErrUnknownAncestor ) && bc .futureBlocks .Contains (it .first ().ParentHash ())):
@@ -1849,7 +1850,8 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator) (i
18491850// recoverAncestors finds the closest ancestor with available state and re-execute
18501851// all the ancestor blocks since that.
18511852// recoverAncestors is only used post-merge.
1852- func (bc * BlockChain ) recoverAncestors (block * types.Block ) error {
1853+ // We return the hash of the latest block that we could correctly validate.
1854+ func (bc * BlockChain ) recoverAncestors (block * types.Block ) (common.Hash , error ) {
18531855 // Gather all the sidechain hashes (full blocks may be memory heavy)
18541856 var (
18551857 hashes []common.Hash
@@ -1864,18 +1866,18 @@ func (bc *BlockChain) recoverAncestors(block *types.Block) error {
18641866 // If the chain is terminating, stop iteration
18651867 if bc .insertStopped () {
18661868 log .Debug ("Abort during blocks iteration" )
1867- return errInsertionInterrupted
1869+ return common. Hash {}, errInsertionInterrupted
18681870 }
18691871 }
18701872 if parent == nil {
1871- return errors .New ("missing parent" )
1873+ return common. Hash {}, errors .New ("missing parent" )
18721874 }
18731875 // Import all the pruned blocks to make the state available
18741876 for i := len (hashes ) - 1 ; i >= 0 ; i -- {
18751877 // If the chain is terminating, stop processing blocks
18761878 if bc .insertStopped () {
18771879 log .Debug ("Abort during blocks processing" )
1878- return errInsertionInterrupted
1880+ return common. Hash {}, errInsertionInterrupted
18791881 }
18801882 var b * types.Block
18811883 if i == 0 {
@@ -1884,10 +1886,10 @@ func (bc *BlockChain) recoverAncestors(block *types.Block) error {
18841886 b = bc .GetBlock (hashes [i ], numbers [i ])
18851887 }
18861888 if _ , err := bc .insertChain (types.Blocks {b }, false , false ); err != nil {
1887- return err
1889+ return b . ParentHash (), err
18881890 }
18891891 }
1890- return nil
1892+ return block . Hash (), nil
18911893}
18921894
18931895// collectLogs collects the logs that were generated or removed during
@@ -2090,24 +2092,24 @@ func (bc *BlockChain) InsertBlockWithoutSetHead(block *types.Block) error {
20902092// SetCanonical rewinds the chain to set the new head block as the specified
20912093// block. It's possible that the state of the new head is missing, and it will
20922094// be recovered in this function as well.
2093- func (bc * BlockChain ) SetCanonical (head * types.Block ) error {
2095+ func (bc * BlockChain ) SetCanonical (head * types.Block ) (common. Hash , error ) {
20942096 if ! bc .chainmu .TryLock () {
2095- return errChainStopped
2097+ return common. Hash {}, errChainStopped
20962098 }
20972099 defer bc .chainmu .Unlock ()
20982100
20992101 // Re-execute the reorged chain in case the head state is missing.
21002102 if ! bc .HasState (head .Root ()) {
2101- if err := bc .recoverAncestors (head ); err != nil {
2102- return err
2103+ if latestValidHash , err := bc .recoverAncestors (head ); err != nil {
2104+ return latestValidHash , err
21032105 }
21042106 log .Info ("Recovered head state" , "number" , head .Number (), "hash" , head .Hash ())
21052107 }
21062108 // Run the reorg if necessary and set the given block as new head.
21072109 start := time .Now ()
21082110 if head .ParentHash () != bc .CurrentBlock ().Hash () {
21092111 if err := bc .reorg (bc .CurrentBlock (), head ); err != nil {
2110- return err
2112+ return common. Hash {}, err
21112113 }
21122114 }
21132115 bc .writeHeadBlock (head )
@@ -2130,7 +2132,7 @@ func (bc *BlockChain) SetCanonical(head *types.Block) error {
21302132 context = append (context , []interface {}{"age" , common .PrettyAge (timestamp )}... )
21312133 }
21322134 log .Info ("Chain head was updated" , context ... )
2133- return nil
2135+ return head . Hash (), nil
21342136}
21352137
21362138func (bc * BlockChain ) updateFutureBlocks () {
0 commit comments