chore: split validation and reconstruct logic

This commit is contained in:
healthykim 2025-11-13 23:22:39 +09:00
parent 65debe9317
commit bfc878ed36

View file

@ -434,13 +434,40 @@ func (p *Peer) ReconstructReceiptsPacket(packet *ReceiptsPacket70) (int, error)
// Trim and buffer the last block when the response is incomplete. // Trim and buffer the last block when the response is incomplete.
if packet.LastBlockIncomplete { if packet.LastBlockIncomplete {
lastReceipts := packet.List[len(packet.List)-1] lastReceipts := packet.List[len(packet.List)-1]
logSize, err := p.validateLastBlockReceipt(lastReceipts, packet.RequestId)
if err != nil {
return 0, err
}
// Update the buffered data and trim the packet to exclude the incomplete block.
if buffer, ok := p.receiptBuffer[requestId]; ok {
buffer.idx = buffer.idx + len(packet.List) - 1
buffer.list.items = append(buffer.list.items, lastReceipts.items...)
buffer.size = buffer.size + logSize
} else {
p.receiptBuffer[requestId] = &partialReceipt{
idx: len(packet.List) - 1,
list: lastReceipts,
size: logSize,
}
}
packet.List = packet.List[:len(packet.List)-1]
}
return from, nil
}
// validateLastBlockReceipt validates receipts and return log size of last block receipt
func (p *Peer) validateLastBlockReceipt(lastReceipts *ReceiptList69, id uint64) (uint64, error) {
if lastReceipts == nil { if lastReceipts == nil {
return 0, fmt.Errorf("nil partial receipt") return 0, fmt.Errorf("nil partial receipt")
} }
var previousTxs int var previousTxs int
var previousLog uint64 var previousLog uint64
if buffer, ok := p.receiptBuffer[requestId]; ok { var log uint64
if buffer, ok := p.receiptBuffer[id]; ok {
previousTxs = len(buffer.list.items) previousTxs = len(buffer.list.items)
previousLog = buffer.size previousLog = buffer.size
} }
@ -456,29 +483,14 @@ func (p *Peer) ReconstructReceiptsPacket(packet *ReceiptsPacket70) (int, error)
if uint64(len(rc.Logs)) > params.MaxTxGas/params.LogDataGas { if uint64(len(rc.Logs)) > params.MaxTxGas/params.LogDataGas {
return 0, fmt.Errorf("total size of receipt exceeded limit") return 0, fmt.Errorf("total size of receipt exceeded limit")
} }
previousLog += uint64(len(rc.Logs)) log += uint64(len(rc.Logs))
} }
// 3. Verify that the overall downloaded receipt size does not exceed the block gas limit. // 3. Verify that the overall downloaded receipt size does not exceed the block gas limit.
if previousLog > params.MaxGasLimit/params.LogDataGas { if previousLog+log > params.MaxGasLimit/params.LogDataGas {
return 0, fmt.Errorf("total download receipt size exceeded the limit") return 0, fmt.Errorf("total download receipt size exceeded the limit")
} }
// Update the buffered data and trim the packet to exclude the incomplete block. return log, nil
if buffer, ok := p.receiptBuffer[requestId]; ok {
buffer.idx = buffer.idx + len(packet.List) - 1
buffer.list.items = append(buffer.list.items, lastReceipts.items...)
buffer.size = previousLog
} else {
p.receiptBuffer[requestId] = &partialReceipt{
idx: len(packet.List) - 1,
list: lastReceipts,
size: previousLog,
}
}
packet.List = packet.List[:len(packet.List)-1]
}
return from, nil
} }
// RequestTxs fetches a batch of transactions from a remote node. // RequestTxs fetches a batch of transactions from a remote node.