From 63dc2328e53cc41ef6bf43117f86212b9951b223 Mon Sep 17 00:00:00 2001 From: allformless <213398294+allformless@users.noreply.github.com> Date: Thu, 6 Nov 2025 10:28:23 +0800 Subject: [PATCH] eth: fix stuck when handleBlockBroadcast --- eth/handler_eth.go | 6 +++++- eth/sync.go | 8 ++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/eth/handler_eth.go b/eth/handler_eth.go index ff8cfdf5a6..0099fb0873 100644 --- a/eth/handler_eth.go +++ b/eth/handler_eth.go @@ -149,7 +149,9 @@ func (h *ethHandler) handleBlockBroadcast(peer *eth.Peer, packet *eth.NewBlockPa log.Debug("handleBlockBroadcast", "peer", peer.ID(), "block", block.Number(), "hash", block.Hash()) h.blockFetcher.Enqueue(peer.ID(), block) stats := h.chain.GetBlockStats(block.Hash()) + blockFirstReceived := false if stats.RecvNewBlockTime.Load() == 0 { + blockFirstReceived = true stats.RecvNewBlockTime.Store(time.Now().UnixMilli()) addr := peer.RemoteAddr() if addr != nil { @@ -166,7 +168,9 @@ func (h *ethHandler) handleBlockBroadcast(peer *eth.Peer, packet *eth.NewBlockPa // Update the peer's total difficulty if better than the previous if _, td := peer.Head(); trueTD.Cmp(td) > 0 { peer.SetHead(trueHead, trueTD) - h.chainSync.handlePeerEvent() + if blockFirstReceived { + h.chainSync.handlePeerEvent() + } } return nil } diff --git a/eth/sync.go b/eth/sync.go index 3bf2046741..9c608f374c 100644 --- a/eth/sync.go +++ b/eth/sync.go @@ -79,7 +79,7 @@ type chainSyncOp struct { func newChainSyncer(handler *handler) *chainSyncer { return &chainSyncer{ handler: handler, - peerEventCh: make(chan struct{}), + peerEventCh: make(chan struct{}, 10), } } @@ -186,9 +186,9 @@ func (cs *chainSyncer) nextSyncOp() *chainSyncOp { } else if op.td.Cmp(new(big.Int).Add(ourTD, common.Big2)) <= 0 { // common.Big2: difficulty of an in-turn block // On BSC, blocks are produced much faster than on Ethereum. // If the node is only slightly behind (e.g., 1 block), syncing is unnecessary. - // It's likely still processing broadcasted blocks or block hash announcements. - // In most cases, the node will catch up within 3 seconds. - time.Sleep(3 * time.Second) + // It's likely still processing broadcasted blocks(such as including a big tx) or block hash announcements. + // In most cases, the node will catch up within 2 seconds. + time.Sleep(2 * time.Second) // Re-check local head to see if it has caught up if _, latestTD := cs.modeAndLocalHead(); ourTD.Cmp(latestTD) < 0 {