eth/protocols/eth: bring back Derivable method

This commit is contained in:
Felix Lange 2026-02-28 00:07:56 +01:00
parent 0d34003a40
commit 31b5d5aa3a
2 changed files with 17 additions and 14 deletions

View file

@ -435,16 +435,6 @@ func writeTxForHash(tx []byte, buf *bytes.Buffer) {
} }
} }
// writeReceiptForHash returns a write function that encode receipts for hash derivation.
func writeReceiptForHash(bloomBuf *[6]byte) func([]byte, *bytes.Buffer) {
return func(data []byte, outbuf *bytes.Buffer) {
var r Receipt
if rlp.DecodeBytes(data, &r) == nil {
r.EncodeForHash(bloomBuf, outbuf)
}
}
}
func handleReceipts(backend Backend, msg Decoder, peer *Peer) error { func handleReceipts(backend Backend, msg Decoder, peer *Peer) error {
// A batch of receipts arrived to one of our previous requests // A batch of receipts arrived to one of our previous requests
res := new(ReceiptsPacket) res := new(ReceiptsPacket)
@ -462,14 +452,11 @@ func handleReceipts(backend Backend, msg Decoder, peer *Peer) error {
return fmt.Errorf("Receipts: %w", err) return fmt.Errorf("Receipts: %w", err)
} }
var bloomBuf [6]byte
writeReceipt := writeReceiptForHash(&bloomBuf)
metadata := func() interface{} { metadata := func() interface{} {
hasher := trie.NewStackTrie(nil) hasher := trie.NewStackTrie(nil)
hashes := make([]common.Hash, len(receiptLists)) hashes := make([]common.Hash, len(receiptLists))
for i := range receiptLists { for i := range receiptLists {
receipts := newDerivableRawList(&receiptLists[i].items, writeReceipt) hashes[i] = types.DeriveSha(receiptLists[i].Derivable(), hasher)
hashes[i] = types.DeriveSha(receipts, hasher)
} }
return hashes return hashes
} }

View file

@ -146,6 +146,22 @@ func (rl *ReceiptList) EncodeRLP(w io.Writer) error {
return rl.items.EncodeRLP(w) return rl.items.EncodeRLP(w)
} }
func (rl *ReceiptList) Derivable() types.DerivableList {
var bloomBuf [6]byte
write := writeReceiptForHash(&bloomBuf)
return newDerivableRawList(&rl.items, write)
}
// writeReceiptForHash returns a write function that encodes receipts for hash derivation.
func writeReceiptForHash(bloomBuf *[6]byte) func([]byte, *bytes.Buffer) {
return func(data []byte, outbuf *bytes.Buffer) {
var r Receipt
if rlp.DecodeBytes(data, &r) == nil {
r.EncodeForHash(bloomBuf, outbuf)
}
}
}
// encodeTypes takes a slice of rlp-encoded receipts, and transactions, // encodeTypes takes a slice of rlp-encoded receipts, and transactions,
// and applies the type-encoding on the receipts (for non-legacy receipts). // and applies the type-encoding on the receipts (for non-legacy receipts).
// e.g. for non-legacy receipts: receipt-data -> {tx-type || receipt-data} // e.g. for non-legacy receipts: receipt-data -> {tx-type || receipt-data}