From 74304d2799f1862e84aa469ce86e4d78fdf70feb Mon Sep 17 00:00:00 2001 From: wizeguyy Date: Thu, 21 Sep 2023 14:16:58 -0500 Subject: [PATCH] Drop peers who broadcast stale blocks --- eth/fetcher/block_fetcher.go | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/eth/fetcher/block_fetcher.go b/eth/fetcher/block_fetcher.go index f2d4e04f64..1f89cde662 100644 --- a/eth/fetcher/block_fetcher.go +++ b/eth/fetcher/block_fetcher.go @@ -39,10 +39,11 @@ const ( ) const ( - maxUncleDist = 100 // Maximum allowed backward distance from the chain head - maxQueueDist = 32 // Maximum allowed distance from the chain head to queue - hashLimit = 256 // Maximum number of unique blocks or headers a peer may have announced - blockLimit = 64 // Maximum number of unique blocks a peer may have delivered + maxUncleDist = 100 // Maximum allowed backward distance from the chain head + maxQueueDist = 32 // Maximum allowed distance from the chain head to queue + hashLimit = 256 // Maximum number of unique blocks or headers a peer may have announced + blockLimit = 64 // Maximum number of unique blocks a peer may have delivered + MaxStaleBroadcastDist = 3500 // Drop peers who broadcast new blocks that are more than 3500 blocks stale ) var ( @@ -250,6 +251,14 @@ func (f *BlockFetcher) Notify(peer string, hash common.Hash, number uint64, time // Enqueue tries to fill gaps the fetcher's future import queue. func (f *BlockFetcher) Enqueue(peer string, block *types.Block) error { + // If this is a newly mined block from a much lower height, drop the peer. Peers should not mine blocks until they are in sync with the network. + currentNum := f.chainHeight() + bcastNum := block.NumberU64() + if currentNum > MaxStaleBroadcastDist && (currentNum-MaxStaleBroadcastDist) > bcastNum { + // The broadcast blocks is stale. Drop the peer. + return errors.New("stale broadcast") + } + op := &blockOrHeaderInject{ origin: peer, block: block,