eth/protocols/eth: remove useless checks in the eth protocol handler

This commit is contained in:
Gary Rong 2025-12-09 13:41:16 +08:00
parent b2490e3f20
commit fb1ce79543
3 changed files with 13 additions and 15 deletions

View file

@ -218,7 +218,8 @@ func (p *Peer) dispatcher() {
// Stop tracking the request // Stop tracking the request
delete(pending, cancelOp.id) 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() p.receiptBufferLock.Lock()
delete(p.receiptBuffer, cancelOp.id) delete(p.receiptBuffer, cancelOp.id)
p.receiptBufferLock.Unlock() p.receiptBufferLock.Unlock()

View file

@ -35,6 +35,10 @@ const (
// softResponseLimit is the target maximum size of replies to data retrievals. // softResponseLimit is the target maximum size of replies to data retrievals.
softResponseLimit = 2 * 1024 * 1024 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 // maxHeadersServe is the maximum number of block headers to serve. This number
// is there to limit the number of disk lookups. // is there to limit the number of disk lookups.
maxHeadersServe = 1024 maxHeadersServe = 1024
@ -49,8 +53,6 @@ const (
// containing 200+ transactions nowadays, the practical limit will always // containing 200+ transactions nowadays, the practical limit will always
// be softResponseLimit. // be softResponseLimit.
maxReceiptsServe = 1024 maxReceiptsServe = 1024
maxPacketSize = 10 * 1024 * 1024
) )
// Handler is a callback to invoke from an outside runner after the boilerplate // Handler is a callback to invoke from an outside runner after the boilerplate

View file

@ -286,9 +286,8 @@ func ServiceGetReceiptsQuery68(chain *core.BlockChain, query GetReceiptsRequest)
bytes int bytes int
receipts []rlp.RawValue receipts []rlp.RawValue
) )
for lookups, hash := range query { for _, hash := range query {
if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe || if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe {
lookups >= 2*maxReceiptsServe {
break break
} }
// Retrieve the requested block's receipts // Retrieve the requested block's receipts
@ -323,9 +322,8 @@ func serviceGetReceiptsQuery69(chain *core.BlockChain, query GetReceiptsRequest)
bytes int bytes int
receipts []rlp.RawValue receipts []rlp.RawValue
) )
for lookups, hash := range query { for _, hash := range query {
if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe || if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe {
lookups >= 2*maxReceiptsServe {
break break
} }
// Retrieve the requested block's receipts // Retrieve the requested block's receipts
@ -362,13 +360,10 @@ func serviceGetReceiptsQuery70(chain *core.BlockChain, query GetReceiptsRequest,
receipts []rlp.RawValue receipts []rlp.RawValue
lastBlockIncomplete bool lastBlockIncomplete bool
) )
for i, hash := range query {
for lookups, hash := range query { if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe {
if bytes >= softResponseLimit || len(receipts) >= maxReceiptsServe ||
lookups >= 2*maxReceiptsServe {
break break
} }
results := chain.GetReceiptsRLP(hash) results := chain.GetReceiptsRLP(hash)
if results == nil { if results == nil {
if header := chain.GetHeaderByHash(hash); header == nil || header.ReceiptHash != types.EmptyRootHash { 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) results, lastBlockIncomplete = trimReceiptsRLP(results, int(firstBlockReceiptIndex), maxPacketSize)
} else if bytes+len(results) > maxPacketSize { } else if bytes+len(results) > maxPacketSize {
results, lastBlockIncomplete = trimReceiptsRLP(results, 0, maxPacketSize-bytes) results, lastBlockIncomplete = trimReceiptsRLP(results, 0, maxPacketSize-bytes)