diff --git a/eth/protocols/eth/peer.go b/eth/protocols/eth/peer.go index 1db1fa24d4..c8853fa0ce 100644 --- a/eth/protocols/eth/peer.go +++ b/eth/protocols/eth/peer.go @@ -524,7 +524,6 @@ func (p *Peer) validateLastBlockReceipt(receiptLists []*ReceiptList, id uint64, // This case happens only if len(receiptLists) == 1 && incomplete == true && buffered before. var previousTxs int var previousLog uint64 - var log uint64 if buffer, ok := p.receiptBuffer[id]; ok && len(buffer.list) > 0 && len(receiptLists) == 1 { previousTxs = buffer.list[len(buffer.list)-1].items.Len() previousLog = buffer.lastLogSize @@ -542,24 +541,9 @@ func (p *Peer) validateLastBlockReceipt(receiptLists []*ReceiptList, id uint64, return 0, fmt.Errorf("total number of tx exceeded limit") } // Count log size per receipt - it := lastReceipts.items.ContentIterator() - for it.Next() { - content, _, err := rlp.SplitList(it.Value()) - if err != nil { - return 0, fmt.Errorf("invalid receipt structure: %v", err) - } - rest := content - for range 3 { - _, _, rest, err = rlp.Split(rest) - if err != nil { - return 0, fmt.Errorf("invalid receipt structure: %v", err) - } - } - logsContent, _, err := rlp.SplitList(rest) - if err != nil { - return 0, fmt.Errorf("invalid receipt logs: %v", err) - } - log += uint64(len(logsContent)) + log, err := lastReceipts.LogsSize() + if err != nil { + return 0, err } // Verify that the overall downloaded receipt size does not exceed the block gas limit. if previousLog+log > gasUsed/params.LogDataGas { diff --git a/eth/protocols/eth/receipt.go b/eth/protocols/eth/receipt.go index d68d6e4ee1..a2f5fe52de 100644 --- a/eth/protocols/eth/receipt.go +++ b/eth/protocols/eth/receipt.go @@ -207,6 +207,31 @@ func (rl *ReceiptList) Append(other *ReceiptList) { rl.items.AppendList(&other.items) } +// LogsSize returns the total size of log data in this receipts. +func (rl *ReceiptList) LogsSize() (uint64, error) { + var size uint64 + it := rl.items.ContentIterator() + for it.Next() { + content, _, err := rlp.SplitList(it.Value()) + if err != nil { + return 0, fmt.Errorf("invalid receipt structure: %v", err) + } + rest := content + for range 3 { + _, _, rest, err = rlp.Split(rest) + if err != nil { + return 0, fmt.Errorf("invalid receipt structure: %v", err) + } + } + logsContent, _, err := rlp.SplitList(rest) + if err != nil { + return 0, fmt.Errorf("invalid receipt logs: %v", err) + } + size += uint64(len(logsContent)) + } + return size, nil +} + // blockReceiptsToNetwork takes a slice of rlp-encoded receipts, and transactions, // and re-encodes them for the network protocol. func blockReceiptsToNetwork(blockReceipts, blockBody rlp.RawValue) ([]byte, error) {