Skip to content

Commit

Permalink
firehose: prevent bsc emitting forked blocks passed LIB
Browse files Browse the repository at this point in the history
  • Loading branch information
sduchesneau committed Jan 29, 2024
1 parent 4286770 commit 3051b9b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
19 changes: 16 additions & 3 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -2071,7 +2071,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
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)
finalBlock := getFinalBlockForFirehose(bc, block)
firehoseContext.EndBlock(block, finalBlock, td)
}

stats.processed++
Expand Down Expand Up @@ -2147,7 +2148,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
// 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)
finalBlock := getFinalBlockForFirehose(bc, block)
firehoseContext.EndBlock(block, finalBlock, td)
}

bc.cacheReceipts(block.Hash(), receipts, block)
Expand Down Expand Up @@ -2200,7 +2202,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool) (int, error)
stats.usedGas += usedGas

trieDiffNodes, trieBufNodes, trieImmutableBufNodes, _ := bc.triedb.Size()
stats.report(chain, it.index, trieDiffNodes, trieBufNodes, trieImmutableBufNodes, setHead)
stats.report(chain, it.index, trieDiffNodes, trieBufNodes, trieImmutableBufNodes, setHead, bc.CurrentFinalBlock())

if !setHead {
// After merge we expect few side chains. Simply count
Expand Down Expand Up @@ -3199,3 +3201,14 @@ func (bc *BlockChain) SetTrieFlushInterval(interval time.Duration) {
func (bc *BlockChain) GetTrieFlushInterval() time.Duration {
return time.Duration(bc.flushInterval.Load())
}

// getFinalBlockForFirehose returns the current final block unless it is
// not canonical, then it returns nil
func getFinalBlockForFirehose(bc *BlockChain, block *types.Block) *types.Header {
if canon := bc.GetBlockByNumber(block.NumberU64()); canon != nil {
if canon.Hash() != block.Hash() { // block is not canonical, we ignore the final block indication
return nil
}
}
return bc.CurrentFinalBlock()
}
5 changes: 4 additions & 1 deletion core/blockchain_insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const statsReportLimit = 8 * time.Second

// report prints statistics if some number of blocks have been processed
// or more than a few seconds have passed since the last message.
func (st *insertStats) report(chain []*types.Block, index int, trieDiffNodes, trieBufNodes, trieImmutableBufNodes common.StorageSize, setHead bool) {
func (st *insertStats) report(chain []*types.Block, index int, trieDiffNodes, trieBufNodes, trieImmutableBufNodes common.StorageSize, setHead bool, final *types.Header) {
// Fetch the timings for the batch
var (
now = mclock.Now()
Expand Down Expand Up @@ -78,6 +78,9 @@ func (st *insertStats) report(chain []*types.Block, index int, trieDiffNodes, tr
context = append(context, []interface{}{"ignored", st.ignored}...)
}
if setHead {
if final != nil {
context = append(context, []interface{}{"final_number", final.Number, "final_hash", final.Hash()}...)
}
log.Info("Imported new chain segment", context...)
} else {
log.Info("Imported new potential chain segment", context...)
Expand Down

0 comments on commit 3051b9b

Please sign in to comment.