fix broken chain of ctx propagation thru insertSidechain() and partially thru recoverAncestors()

This commit is contained in:
jonny rhea 2026-01-17 15:36:51 -06:00
parent 5731ca5d2a
commit 402d36d805

View file

@ -1895,11 +1895,11 @@ func (bc *BlockChain) insertChain(ctx context.Context, chain types.Blocks, setHe
if setHead {
// First block is pruned, insert as sidechain and reorg only if TD grows enough
log.Debug("Pruned ancestor, inserting as sidechain", "number", block.Number(), "hash", block.Hash())
return bc.insertSideChain(block, it, makeWitness)
return bc.insertSideChain(ctx, block, it, makeWitness)
} else {
// We're post-merge and the parent is pruned, try to recover the parent state
log.Debug("Pruned ancestor", "number", block.Number(), "hash", block.Hash())
_, err := bc.recoverAncestors(block, makeWitness)
_, err := bc.recoverAncestors(ctx, block, makeWitness)
return nil, it.index, err
}
// Some other error(except ErrKnownBlock) occurred, abort.
@ -2274,7 +2274,7 @@ func (bc *BlockChain) ProcessBlock(ctx context.Context, parentRoot common.Hash,
// The method writes all (header-and-body-valid) blocks to disk, then tries to
// switch over to the new chain if the TD exceeded the current chain.
// insertSideChain is only used pre-merge.
func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator, makeWitness bool) (*stateless.Witness, int, error) {
func (bc *BlockChain) insertSideChain(ctx context.Context, block *types.Block, it *insertIterator, makeWitness bool) (*stateless.Witness, int, error) {
var current = bc.CurrentBlock()
// The first sidechain block error is already verified to be ErrPrunedAncestor.
@ -2355,7 +2355,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator, ma
// memory here.
if len(blocks) >= 2048 || memory > 64*1024*1024 {
log.Info("Importing heavy sidechain segment", "blocks", len(blocks), "start", blocks[0].NumberU64(), "end", block.NumberU64())
if _, _, err := bc.insertChain(context.Background(), blocks, true, false); err != nil {
if _, _, err := bc.insertChain(ctx, blocks, true, false); err != nil {
return nil, 0, err
}
blocks, memory = blocks[:0], 0
@ -2369,7 +2369,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator, ma
}
if len(blocks) > 0 {
log.Info("Importing sidechain segment", "start", blocks[0].NumberU64(), "end", blocks[len(blocks)-1].NumberU64())
return bc.insertChain(context.Background(), blocks, true, makeWitness)
return bc.insertChain(ctx, blocks, true, makeWitness)
}
return nil, 0, nil
}
@ -2378,7 +2378,7 @@ func (bc *BlockChain) insertSideChain(block *types.Block, it *insertIterator, ma
// all the ancestor blocks since that.
// recoverAncestors is only used post-merge.
// We return the hash of the latest block that we could correctly validate.
func (bc *BlockChain) recoverAncestors(block *types.Block, makeWitness bool) (common.Hash, error) {
func (bc *BlockChain) recoverAncestors(ctx context.Context, block *types.Block, makeWitness bool) (common.Hash, error) {
// Gather all the sidechain hashes (full blocks may be memory heavy)
var (
hashes []common.Hash
@ -2418,7 +2418,7 @@ func (bc *BlockChain) recoverAncestors(block *types.Block, makeWitness bool) (co
} else {
b = bc.GetBlock(hashes[i], numbers[i])
}
if _, _, err := bc.insertChain(context.Background(), types.Blocks{b}, false, makeWitness && i == 0); err != nil {
if _, _, err := bc.insertChain(ctx, types.Blocks{b}, false, makeWitness && i == 0); err != nil {
return b.ParentHash(), err
}
}
@ -2666,7 +2666,7 @@ func (bc *BlockChain) SetCanonical(head *types.Block) (common.Hash, error) {
// Re-execute the reorged chain in case the head state is missing.
if !bc.HasState(head.Root()) {
if latestValidHash, err := bc.recoverAncestors(head, false); err != nil {
if latestValidHash, err := bc.recoverAncestors(context.Background(), head, false); err != nil {
return latestValidHash, err
}
log.Info("Recovered head state", "number", head.Number(), "hash", head.Hash())