From fb1ce7954319d69940baa502ba7ecf0278a409b0 Mon Sep 17 00:00:00 2001 From: Gary Rong Date: Tue, 9 Dec 2025 13:41:16 +0800 Subject: [PATCH] eth/protocols/eth: remove useless checks in the eth protocol handler --- eth/protocols/eth/dispatcher.go | 3 ++- eth/protocols/eth/handler.go | 6 ++++-- eth/protocols/eth/handlers.go | 19 +++++++------------ 3 files changed, 13 insertions(+), 15 deletions(-) diff --git a/eth/protocols/eth/dispatcher.go b/eth/protocols/eth/dispatcher.go index c81c43edeb..d5124378b5 100644 --- a/eth/protocols/eth/dispatcher.go +++ b/eth/protocols/eth/dispatcher.go @@ -218,7 +218,8 @@ func (p *Peer) dispatcher() { // Stop tracking the request delete(pending, cancelOp.id) - // Not sure if the request is about the receipt, but remove it anyway + // Not sure if the request is about the receipt, but remove it anyway. + // TODO(rjl493456442, bosul): investigate whether we can avoid leaking peer fields here. p.receiptBufferLock.Lock() delete(p.receiptBuffer, cancelOp.id) p.receiptBufferLock.Unlock() diff --git a/eth/protocols/eth/handler.go b/eth/protocols/eth/handler.go index 04d35fdb04..cecdd18b75 100644 --- a/eth/protocols/eth/handler.go +++ b/eth/protocols/eth/handler.go @@ -35,6 +35,10 @@ const ( // softResponseLimit is the target maximum size of replies to data retrievals. softResponseLimit = 2 * 1024 * 1024 + // maxPacketSize is the devp2p message size limit commonly enforced by clients. + // Any packet exceeding this limit must be rejected. + maxPacketSize = 10 * 1024 * 1024 + // maxHeadersServe is the maximum number of block headers to serve. This number // is there to limit the number of disk lookups. maxHeadersServe = 1024 @@ -49,8 +53,6 @@ const ( // containing 200+ transactions nowadays, the practical limit will always // be softResponseLimit. maxReceiptsServe = 1024 - - maxPacketSize = 10 * 1024 * 1024 ) // Handler is a callback to invoke from an outside runner after the boilerplate diff --git a/eth/protocols/eth/handlers.go b/eth/protocols/eth/handlers.go index 723d6244f7..805f072915 100644 --- a/eth/protocols/eth/handlers.go +++ b/eth/protocols/eth/handlers.go @@ -286,9 +286,8 @@ func ServiceGetReceiptsQuery68(chain *core.BlockChain, query GetReceiptsRequest) bytes int receipts []rlp.RawValue ) - for lookups, hash := range query { - if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe || - lookups >= 2*maxReceiptsServe { + for _, hash := range query { + if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe { break } // Retrieve the requested block's receipts @@ -323,9 +322,8 @@ func serviceGetReceiptsQuery69(chain *core.BlockChain, query GetReceiptsRequest) bytes int receipts []rlp.RawValue ) - for lookups, hash := range query { - if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe || - lookups >= 2*maxReceiptsServe { + for _, hash := range query { + if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe { break } // Retrieve the requested block's receipts @@ -362,13 +360,10 @@ func serviceGetReceiptsQuery70(chain *core.BlockChain, query GetReceiptsRequest, receipts []rlp.RawValue lastBlockIncomplete bool ) - - for lookups, hash := range query { - if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe || - lookups >= 2*maxReceiptsServe { + for i, hash := range query { + if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe { break } - results := chain.GetReceiptsRLP(hash) if results == nil { if header := chain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash { @@ -387,7 +382,7 @@ func serviceGetReceiptsQuery70(chain *core.BlockChain, query GetReceiptsRequest, } } - if firstBlockReceiptIndex > 0 && lookups == 0 { + if firstBlockReceiptIndex > 0 && i == 0 { results, lastBlockIncomplete = trimReceiptsRLP(results, int(firstBlockReceiptIndex), maxPacketSize) } else if bytes+len(results) > maxPacketSize { results, lastBlockIncomplete = trimReceiptsRLP(results, 0, maxPacketSize-bytes)