eth/protocols/eth: fix receipt hashing

This commit is contained in:
Felix Lange 2025-04-16 01:05:05 +02:00
parent f96cfcece0
commit 5943631173
2 changed files with 28 additions and 17 deletions

View file

@ -96,6 +96,7 @@ func (r *Receipt) bloom(buffer *[6]byte) types.Bloom {
} }
for logsIter.Next() { for logsIter.Next() {
log := logsIter.Value() log := logsIter.Value()
log, _, _ = rlp.SplitList(log)
address, log, _ := rlp.SplitString(log) address, log, _ := rlp.SplitString(log)
b.AddWithBuffer(address, buffer) b.AddWithBuffer(address, buffer)
topicsIter, err := rlp.NewListIterator(log) topicsIter, err := rlp.NewListIterator(log)
@ -103,7 +104,8 @@ func (r *Receipt) bloom(buffer *[6]byte) types.Bloom {
return b return b
} }
for topicsIter.Next() { for topicsIter.Next() {
b.AddWithBuffer(topicsIter.Value(), buffer) topic, _, _ := rlp.SplitString(topicsIter.Value())
b.AddWithBuffer(topic, buffer)
} }
} }
return b return b
@ -137,7 +139,10 @@ func (rl *ReceiptList69) EncodeIndex(i int, b *bytes.Buffer) {
bloom = r.bloom(&rl.buf.bloom) bloom = r.bloom(&rl.buf.bloom)
w = &rl.buf.enc w = &rl.buf.enc
) )
// encode receipt list: [postStateOrStatus, gasUsed, bloom, logs] if r.TxType != 0 {
b.WriteByte(r.TxType)
}
// encode list = [postStateOrStatus, gasUsed, bloom, logs]
w.Reset(b) w.Reset(b)
l := w.List() l := w.List()
w.WriteBytes(r.PostStateOrStatus) w.WriteBytes(r.PostStateOrStatus)
@ -145,19 +150,6 @@ func (rl *ReceiptList69) EncodeIndex(i int, b *bytes.Buffer) {
w.WriteBytes(bloom[:]) w.WriteBytes(bloom[:])
w.Write(r.Logs) w.Write(r.Logs)
w.ListEnd(l) w.ListEnd(l)
if err := w.Flush(); err != nil {
return
}
// if this is a legacy transaction receipt, we are done.
if r.TxType == 0 {
return
}
// Otherwise it's a typed transaction receipt, which has the type prefix and
// the inner list as a byte-array: tx-type || rlp(list).
// Since b contains the correct inner list, we can reuse its content.
w.Reset(b)
w.WriteUint64(uint64(r.TxType))
w.WriteBytes(b.Bytes())
w.Flush() w.Flush()
} }
@ -227,8 +219,7 @@ func blockReceiptsToNetwork(blockReceipts, blockBody rlp.RawValue) ([]byte, erro
return out.Bytes(), nil return out.Bytes(), nil
} }
// txTypesInBody parses the transactions list of an encoded block body, // txTypesInBody parses the transactions list of an encoded block body, returning just the types.
// returning just the types.
func txTypesInBody(body rlp.RawValue) (iter.Seq[byte], error) { func txTypesInBody(body rlp.RawValue) (iter.Seq[byte], error) {
bodyFields, _, err := rlp.SplitList(body) bodyFields, _, err := rlp.SplitList(body)
if err != nil { if err != nil {

View file

@ -23,6 +23,7 @@ import (
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/trie"
) )
func TestTransformReceipts(t *testing.T) { func TestTransformReceipts(t *testing.T) {
@ -64,6 +65,7 @@ func TestTransformReceipts(t *testing.T) {
blockBody := types.Body{Transactions: test.txs} blockBody := types.Body{Transactions: test.txs}
encBlockBody, _ := rlp.EncodeToBytes(blockBody) encBlockBody, _ := rlp.EncodeToBytes(blockBody)
// convert from storage encoding to network encoding
have, err := blockReceiptsToNetwork(in, encBlockBody) have, err := blockReceiptsToNetwork(in, encBlockBody)
if err != nil { if err != nil {
t.Fatalf("test[%d]: blockReceiptsToNetwork error: %v", i, err) t.Fatalf("test[%d]: blockReceiptsToNetwork error: %v", i, err)
@ -73,6 +75,7 @@ func TestTransformReceipts(t *testing.T) {
t.Fatalf("test[%d]: blockReceiptsToNetwork mismatch\nhave: %x\nwant: %x\n in: %x", i, out, have, in) t.Fatalf("test[%d]: blockReceiptsToNetwork mismatch\nhave: %x\nwant: %x\n in: %x", i, out, have, in)
} }
// parse as Receipts response list from network encoding
var rl ReceiptList69 var rl ReceiptList69
if err := rlp.DecodeBytes(out, &rl); err != nil { if err := rlp.DecodeBytes(out, &rl); err != nil {
t.Fatalf("test[%d]: can't decode network receipts: %v", i, err) t.Fatalf("test[%d]: can't decode network receipts: %v", i, err)
@ -81,6 +84,23 @@ func TestTransformReceipts(t *testing.T) {
if !bytes.Equal(storageEnc, in) { if !bytes.Equal(storageEnc, in) {
t.Fatalf("test[%d]: re-encoded receipts not equal\nhave: %x\nwant: %x", i, storageEnc, in) t.Fatalf("test[%d]: re-encoded receipts not equal\nhave: %x\nwant: %x", i, storageEnc, in)
} }
// compute expected root hash
receipts := make(types.Receipts, len(test.input))
for i := range test.input {
r := types.Receipt(test.input[i])
// need to derive these fields to get the correct result from DeriveSha.
r.Type = test.txs[i].Type()
r.Bloom = types.CreateBloom(&r)
receipts[i] = &r
}
expectedHash := types.DeriveSha(receipts, trie.NewStackTrie(nil))
// compute root hash from ReceiptList69 and compare.
responseHash := types.DeriveSha(&rl, trie.NewStackTrie(nil))
if responseHash != expectedHash {
t.Fatalf("test[%d]: wrong root hash from ReceiptList69\nhave: %v\nwant: %v", i, responseHash, expectedHash)
}
} }
} }