Skip to content

Commit

Permalink
Drop peers who broadcast stale blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
wizeguyy committed Sep 21, 2023
1 parent fea9ce1 commit e44ca12
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
17 changes: 13 additions & 4 deletions eth/fetcher/block_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions eth/handler_eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ func (h *ethHandler) handleBlockBroadcast(peer *eth.Peer, block *types.Block) er
log.Warn("Bad Hashes still exist on chain, cannot handle block broadcast yet")
return nil
}

// Schedule the block for import
h.blockFetcher.Enqueue(peer.ID(), block)

Expand Down

0 comments on commit e44ca12

Please sign in to comment.