Skip to content

Commit

Permalink
blockchain: don't rely on BlockHeightByHash for prune height
Browse files Browse the repository at this point in the history
calculations

Since BlockHeightByHash only returns the heights for blocks that are in
the main chain, when a block that is stale gets pruned, this will cause
an error in the block height lookup and cause an error in block
processing.

Look up the node directly from the index and if the node isn't found,
just skip that node. For utxoCache.lastFlushHash, if that isn't found,
just force a flush.
  • Loading branch information
kcalvinalvin committed Jul 19, 2024
1 parent 9bcf5e1 commit 3fc9c61
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions blockchain/utxocache.go
Original file line number Diff line number Diff line change
Expand Up @@ -726,10 +726,18 @@ func (b *BlockChain) flushNeededAfterPrune(earliestHeight int32) (bool, error) {
if earliestHeight < 0 {
return false, nil
}
lastFlushHeight, err := b.BlockHeightByHash(&b.utxoCache.lastFlushHash)
if err != nil {
return false, err
}

node := b.index.LookupNode(&b.utxoCache.lastFlushHash)
if node == nil {
// If we couldn't find the node where we last flushed at, have the utxo cache
// flush to be safe and that will set the last flush hash again.
//
// This realistically should never happen as nodes are never deleted from
// the block index. This happening likely means that there's a hardware
// error which is something we can't recover from. The best that we can
// do here is to just force a flush and hope that the newly set
// lastFlushHash doesn't error.
return true, nil
}
lastFlushHeight := node.height
return earliestHeight > lastFlushHeight, nil
}

0 comments on commit 3fc9c61

Please sign in to comment.