Skip to content

Commit bfc878e

Browse files
committed
chore: split validation and reconstruct logic
1 parent 65debe9 commit bfc878e

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

eth/protocols/eth/peer.go

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -434,45 +434,22 @@ func (p *Peer) ReconstructReceiptsPacket(packet *ReceiptsPacket70) (int, error)
434434
// Trim and buffer the last block when the response is incomplete.
435435
if packet.LastBlockIncomplete {
436436
lastReceipts := packet.List[len(packet.List)-1]
437-
if lastReceipts == nil {
438-
return 0, fmt.Errorf("nil partial receipt")
439-
}
440-
441-
var previousTxs int
442-
var previousLog uint64
443-
if buffer, ok := p.receiptBuffer[requestId]; ok {
444-
previousTxs = len(buffer.list.items)
445-
previousLog = buffer.size
446-
}
447-
448-
// 1. Verify that the total number of transactions delivered is under the limit.
449-
if uint64(previousTxs+len(lastReceipts.items)) > lastReceipts.items[0].GasUsed/21_000 {
450-
// should be dropped, don't clear the buffer
451-
return 0, fmt.Errorf("total number of tx exceeded limit")
452-
}
453437

454-
// 2. Verify the size of each receipt against the maximum transaction size.
455-
for _, rc := range lastReceipts.items {
456-
if uint64(len(rc.Logs)) > params.MaxTxGas/params.LogDataGas {
457-
return 0, fmt.Errorf("total size of receipt exceeded limit")
458-
}
459-
previousLog += uint64(len(rc.Logs))
460-
}
461-
// 3. Verify that the overall downloaded receipt size does not exceed the block gas limit.
462-
if previousLog > params.MaxGasLimit/params.LogDataGas {
463-
return 0, fmt.Errorf("total download receipt size exceeded the limit")
438+
logSize, err := p.validateLastBlockReceipt(lastReceipts, packet.RequestId)
439+
if err != nil {
440+
return 0, err
464441
}
465442

466443
// Update the buffered data and trim the packet to exclude the incomplete block.
467444
if buffer, ok := p.receiptBuffer[requestId]; ok {
468445
buffer.idx = buffer.idx + len(packet.List) - 1
469446
buffer.list.items = append(buffer.list.items, lastReceipts.items...)
470-
buffer.size = previousLog
447+
buffer.size = buffer.size + logSize
471448
} else {
472449
p.receiptBuffer[requestId] = &partialReceipt{
473450
idx: len(packet.List) - 1,
474451
list: lastReceipts,
475-
size: previousLog,
452+
size: logSize,
476453
}
477454
}
478455
packet.List = packet.List[:len(packet.List)-1]
@@ -481,6 +458,41 @@ func (p *Peer) ReconstructReceiptsPacket(packet *ReceiptsPacket70) (int, error)
481458
return from, nil
482459
}
483460

461+
// validateLastBlockReceipt validates receipts and return log size of last block receipt
462+
func (p *Peer) validateLastBlockReceipt(lastReceipts *ReceiptList69, id uint64) (uint64, error) {
463+
if lastReceipts == nil {
464+
return 0, fmt.Errorf("nil partial receipt")
465+
}
466+
467+
var previousTxs int
468+
var previousLog uint64
469+
var log uint64
470+
if buffer, ok := p.receiptBuffer[id]; ok {
471+
previousTxs = len(buffer.list.items)
472+
previousLog = buffer.size
473+
}
474+
475+
// 1. Verify that the total number of transactions delivered is under the limit.
476+
if uint64(previousTxs+len(lastReceipts.items)) > lastReceipts.items[0].GasUsed/21_000 {
477+
// should be dropped, don't clear the buffer
478+
return 0, fmt.Errorf("total number of tx exceeded limit")
479+
}
480+
481+
// 2. Verify the size of each receipt against the maximum transaction size.
482+
for _, rc := range lastReceipts.items {
483+
if uint64(len(rc.Logs)) > params.MaxTxGas/params.LogDataGas {
484+
return 0, fmt.Errorf("total size of receipt exceeded limit")
485+
}
486+
log += uint64(len(rc.Logs))
487+
}
488+
// 3. Verify that the overall downloaded receipt size does not exceed the block gas limit.
489+
if previousLog+log > params.MaxGasLimit/params.LogDataGas {
490+
return 0, fmt.Errorf("total download receipt size exceeded the limit")
491+
}
492+
493+
return log, nil
494+
}
495+
484496
// RequestTxs fetches a batch of transactions from a remote node.
485497
func (p *Peer) RequestTxs(hashes []common.Hash) error {
486498
p.Log().Debug("Fetching batch of transactions", "count", len(hashes))

0 commit comments

Comments
 (0)