From e7f6f96cbdd4a88bbbb4074a8668d37534a4dac5 Mon Sep 17 00:00:00 2001
From: gop <gameofpointers@proton.me>
Date: Fri, 29 Sep 2023 13:29:11 -0500
Subject: [PATCH] bugfix: Set the MaxStaleBroadcastDist based on Hierarchy

---
 eth/fetcher/block_fetcher.go | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/eth/fetcher/block_fetcher.go b/eth/fetcher/block_fetcher.go
index 9ca434b411..4bc7a4e98b 100644
--- a/eth/fetcher/block_fetcher.go
+++ b/eth/fetcher/block_fetcher.go
@@ -39,11 +39,13 @@ 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
-	MaxStaleBroadcastDist = 3500 // Drop peers who broadcast new blocks that are more than 3500 blocks stale
+	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
+	PrimeMaxStaleBroadcastDist  = 63   // Drop peers who broadcast new blocks that are more than 63 blocks stale in Prime
+	RegionMaxStaleBroadcastDist = 500  // Drop peers who broadcast new blocks that are more than 500 blocks stale in Region
+	ZoneMaxStaleBroadcastDist   = 3500 // Drop peers who broadcast new blocks that are more than 3500 blocks stale in Zone
 )
 
 var (
@@ -698,7 +700,17 @@ func (f *BlockFetcher) ImportBlocks(peer string, block *types.Block, relay bool)
 	// 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 relay && currentNum > MaxStaleBroadcastDist && currentNum > bcastNum+MaxStaleBroadcastDist {
+	nodeCtx := common.NodeLocation.Context()
+	var maxStaleBroadcastDist uint64
+	switch nodeCtx {
+	case common.ZONE_CTX:
+		maxStaleBroadcastDist = ZoneMaxStaleBroadcastDist
+	case common.REGION_CTX:
+		maxStaleBroadcastDist = RegionMaxStaleBroadcastDist
+	case common.PRIME_CTX:
+		maxStaleBroadcastDist = PrimeMaxStaleBroadcastDist
+	}
+	if relay && currentNum > maxStaleBroadcastDist && currentNum > bcastNum+maxStaleBroadcastDist {
 		// The broadcast blocks is stale. Drop the peer.
 		log.Warn("Peer is mining while not synced, dropping peer", "Id", peer)
 		f.dropPeer(peer)