Skip to content

Commit

Permalink
attempt to fix issue #6 by preventing old forked blocks from being pr…
Browse files Browse the repository at this point in the history
…inted with currentFinalBlock too high
  • Loading branch information
sduchesneau committed Sep 1, 2023
1 parent d957d4d commit a240b42
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1995,7 +1995,25 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool)
firehoseContext.FinalizeBlock(block)
ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
td := new(big.Int).Add(block.Difficulty(), ptd)
firehoseContext.EndBlock(block, bc.CurrentFinalBlock(), td)

currentFinalBlock := bc.CurrentFinalBlock()
if currentFinalBlock != nil && currentFinalBlock.Number.Uint64() >= block.Number().Uint64() {
canonical := bc.GetBlockByNumber(block.NumberU64())
if canonical != nil && canonical.Hash() == block.Hash() {
firehoseContext.EndBlock(block, currentFinalBlock, td)
} else {
log.Warn("Skipping currentFinalBlock on firehose EndBlock (in skipBlock() condition) because we are processing an old block",
"block_num", block.Number().Uint64(),
"block_hash", block.Hash(),
"final_block_num", currentFinalBlock.Number.Uint64(),
"final_block_hash", currentFinalBlock.Hash(),
)
firehoseContext.EndBlock(block, nil, td)
}
} else {
firehoseContext.EndBlock(block, currentFinalBlock, td)
}

}

stats.processed++
Expand Down Expand Up @@ -2079,7 +2097,24 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals, setHead bool)
// Calculate the total difficulty of the block
ptd := bc.GetTd(block.ParentHash(), block.NumberU64()-1)
td := new(big.Int).Add(block.Difficulty(), ptd)
firehoseContext.EndBlock(block, bc.CurrentFinalBlock(), td)
currentFinalBlock := bc.CurrentFinalBlock()
if currentFinalBlock != nil && currentFinalBlock.Number.Uint64() >= block.Number().Uint64() {
canonical := bc.GetBlockByNumber(block.NumberU64())
if canonical != nil && canonical.Hash() == block.Hash() {
firehoseContext.EndBlock(block, currentFinalBlock, td)
} else {
log.Warn("Skipping currentFinalBlock on firehose EndBlock because we are processing an old block",
"block_num", block.Number().Uint64(),
"block_hash", block.Hash(),
"final_block_num", currentFinalBlock.Number.Uint64(),
"final_block_hash", currentFinalBlock.Hash(),
)
firehoseContext.EndBlock(block, nil, td)
}
} else {
firehoseContext.EndBlock(block, currentFinalBlock, td)
}

}

bc.cacheReceipts(block.Hash(), receipts)
Expand Down

0 comments on commit a240b42

Please sign in to comment.