From c022c3f8ab281b0904ba8241ff655f93346f2e9d Mon Sep 17 00:00:00 2001 From: Csaba Kiraly Date: Fri, 25 Apr 2025 00:53:12 +0200 Subject: [PATCH] eth: stabilize peer selection for transaction broadcast When maxPeers was just above some perfect square, and a few peers dropped for some reason, we changed the peer selection function. When new peers were acquired, we changed again. This patch stabilizes the selection function under normal operating conditions close to saturation. Signed-off-by: Csaba Kiraly --- eth/handler.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/eth/handler.go b/eth/handler.go index aaea00e037..0512cc1611 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -482,10 +482,13 @@ func (h *handler) BroadcastTransactions(txs types.Transactions) { annos = make(map[*ethPeer][]common.Hash) // Set peer->hash to announce ) // Broadcast transactions to a batch of peers not knowing about it - direct := big.NewInt(int64(math.Sqrt(float64(h.peers.len())))) // Approximate number of peers to broadcast to - if direct.BitLen() == 0 { - direct = big.NewInt(1) - } + sqrtPeers := int64(math.Sqrt(float64(h.peers.len()))) // Approximate number of peers to broadcast to + + // If maxPeers is just above some perfect square, we need to stabilize + // the number to avoid frequent changes when a few peers drop. + maxDirect := int64(math.Sqrt(float64(h.maxPeers)) - 0.5) + direct := big.NewInt(max(min(sqrtPeers, maxDirect), 1)) + total := new(big.Int).Exp(direct, big.NewInt(2), nil) // Stabilise total peer count a bit based on sqrt peers var (