eth/protocols/eth: fix tests

This commit is contained in:
Marius van der Wijden 2025-03-14 15:25:32 +01:00 committed by Felix Lange
parent fc4e4b3a5b
commit ad8fe2f675
2 changed files with 43 additions and 36 deletions

View file

@ -155,35 +155,39 @@ func (rl *ReceiptList69) EncodeForDatabase() []byte {
if rl.buf == nil { if rl.buf == nil {
rl.buf = new(receiptListBuffers) rl.buf = new(receiptListBuffers)
} }
// TODO this is probably wrong
var ( var result []byte
r = &rl.items[i] var b = new(bytes.Buffer)
bloom = r.bloom(&rl.buf.bloom) for _, r := range rl.items {
w = &rl.buf.enc var (
) bloom = r.bloom(&rl.buf.bloom)
// encode receipt list: [postStateOrStatus, gasUsed, bloom, logs] w = &rl.buf.enc
w.Reset(b) )
l := w.List() // encode receipt list: [postStateOrStatus, gasUsed, bloom, logs]
w.WriteBytes(r.PostStateOrStatus) w.Reset(b)
w.WriteUint64(r.GasUsed) l := w.List()
w.WriteBytes(bloom[:]) w.WriteBytes(r.PostStateOrStatus)
w.Write(r.Logs) w.WriteUint64(r.GasUsed)
w.ListEnd(l) w.WriteBytes(bloom[:])
if err := w.Flush(); err != nil { w.Write(r.Logs)
return w.ListEnd(l)
if err := w.Flush(); err != nil {
return []byte{}
}
// if this is a legacy transaction receipt, we are done.
if r.TxType == 0 {
continue
}
// 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()
result = append(result, b.Bytes()...)
} }
// if this is a legacy transaction receipt, we are done. return result
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()
} }
func (rl *ReceiptList69) toReceipts() []*types.Receipt { func (rl *ReceiptList69) toReceipts() []*types.Receipt {
@ -202,7 +206,7 @@ func (rl *ReceiptList69) toReceipts() []*types.Receipt {
// blockReceiptsToNetwork takes a slice of rlp-encoded receipts, and transactions, // blockReceiptsToNetwork 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}
func blockReceiptsToNetwork(blockReceipts []byte, txs []*types.Transaction) []byte { func blockReceiptsToNetwork(blockReceipts rlp.RawValue, txs []*types.Transaction) []byte {
var ( var (
out bytes.Buffer out bytes.Buffer
enc = rlp.NewEncoderBuffer(&out) enc = rlp.NewEncoderBuffer(&out)
@ -212,7 +216,7 @@ func blockReceiptsToNetwork(blockReceipts []byte, txs []*types.Transaction) []by
for i := 0; it.Next(); i++ { for i := 0; it.Next(); i++ {
content, _, _ := rlp.SplitList(it.Value()) content, _, _ := rlp.SplitList(it.Value())
receiptList := enc.List() receiptList := enc.List()
enc.Write([]byte{txs[i].Type()}) enc.WriteUint64(uint64(txs[i].Type()))
enc.Write(content) enc.Write(content)
enc.ListEnd(receiptList) enc.ListEnd(receiptList)
} }

View file

@ -26,6 +26,9 @@ import (
) )
func TestTransformReceipts(t *testing.T) { func TestTransformReceipts(t *testing.T) {
logs := []*types.Log{{Address: common.Address{1}, Topics: []common.Hash{{1}}}}
encLogs, _ := rlp.EncodeToBytes(logs)
tests := []struct { tests := []struct {
input []types.ReceiptForStorage input []types.ReceiptForStorage
txs []*types.Transaction txs []*types.Transaction
@ -34,22 +37,22 @@ func TestTransformReceipts(t *testing.T) {
{ {
input: []types.ReceiptForStorage{{CumulativeGasUsed: 123, Status: 1, Logs: nil}}, input: []types.ReceiptForStorage{{CumulativeGasUsed: 123, Status: 1, Logs: nil}},
txs: []*types.Transaction{types.NewTx(&types.LegacyTx{})}, txs: []*types.Transaction{types.NewTx(&types.LegacyTx{})},
output: []Receipt{{GasUsed: 123, PostStateOrStatus: []byte{1}, Logs: nil}}, output: []Receipt{{GasUsed: 123, PostStateOrStatus: []byte{1}, Logs: rlp.EmptyList}},
}, },
{ {
input: []types.ReceiptForStorage{{CumulativeGasUsed: 123, Status: 1, Logs: nil}}, input: []types.ReceiptForStorage{{CumulativeGasUsed: 123, Status: 1, Logs: nil}},
txs: []*types.Transaction{types.NewTx(&types.DynamicFeeTx{})}, txs: []*types.Transaction{types.NewTx(&types.DynamicFeeTx{})},
output: []Receipt{{GasUsed: 123, PostStateOrStatus: []byte{1}, Logs: nil, TxType: 2}}, output: []Receipt{{GasUsed: 123, PostStateOrStatus: []byte{1}, Logs: rlp.EmptyList, TxType: 2}},
}, },
{ {
input: []types.ReceiptForStorage{{CumulativeGasUsed: 123, Status: 1, Logs: nil}}, input: []types.ReceiptForStorage{{CumulativeGasUsed: 123, Status: 1, Logs: nil}},
txs: []*types.Transaction{types.NewTx(&types.AccessListTx{})}, txs: []*types.Transaction{types.NewTx(&types.AccessListTx{})},
output: []Receipt{{GasUsed: 123, PostStateOrStatus: []byte{1}, Logs: nil, TxType: 1}}, output: []Receipt{{GasUsed: 123, PostStateOrStatus: []byte{1}, Logs: rlp.EmptyList, TxType: 1}},
}, },
{ {
input: []types.ReceiptForStorage{{CumulativeGasUsed: 123, Status: 1, Logs: []*types.Log{{Address: common.Address{1}, Topics: []common.Hash{{1}}}}}}, input: []types.ReceiptForStorage{{CumulativeGasUsed: 123, Status: 1, Logs: logs}},
txs: []*types.Transaction{types.NewTx(&types.AccessListTx{})}, txs: []*types.Transaction{types.NewTx(&types.AccessListTx{})},
output: []Receipt{{GasUsed: 123, PostStateOrStatus: []byte{1}, Logs: []*types.Log{{Address: common.Address{1}, Topics: []common.Hash{{1}}}}, Type: 1}}, output: []Receipt{{GasUsed: 123, PostStateOrStatus: []byte{1}, Logs: encLogs, TxType: 1}},
}, },
} }
@ -58,7 +61,7 @@ func TestTransformReceipts(t *testing.T) {
have := blockReceiptsToNetwork(in, test.txs) have := blockReceiptsToNetwork(in, test.txs)
out, _ := rlp.EncodeToBytes(test.output) out, _ := rlp.EncodeToBytes(test.output)
if !bytes.Equal(have, out) { if !bytes.Equal(have, out) {
t.Fatalf("transforming receipt mismatch, test %v: want %v have %v", i, out, have) t.Fatalf("transforming receipt mismatch, test %v: want %v have %v, in %v", i, out, have, in)
} }
} }
} }