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,9 +155,11 @@ 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 result []byte
var b = new(bytes.Buffer)
for _, r := range rl.items {
var ( var (
r = &rl.items[i]
bloom = r.bloom(&rl.buf.bloom) bloom = r.bloom(&rl.buf.bloom)
w = &rl.buf.enc w = &rl.buf.enc
) )
@ -170,11 +172,11 @@ func (rl *ReceiptList69) EncodeForDatabase() []byte {
w.Write(r.Logs) w.Write(r.Logs)
w.ListEnd(l) w.ListEnd(l)
if err := w.Flush(); err != nil { if err := w.Flush(); err != nil {
return return []byte{}
} }
// if this is a legacy transaction receipt, we are done. // if this is a legacy transaction receipt, we are done.
if r.TxType == 0 { if r.TxType == 0 {
return continue
} }
// Otherwise it's a typed transaction receipt, which has the type prefix and // Otherwise it's a typed transaction receipt, which has the type prefix and
// the inner list as a byte-array: tx-type || rlp(list). // the inner list as a byte-array: tx-type || rlp(list).
@ -183,7 +185,9 @@ func (rl *ReceiptList69) EncodeForDatabase() []byte {
w.WriteUint64(uint64(r.TxType)) w.WriteUint64(uint64(r.TxType))
w.WriteBytes(b.Bytes()) w.WriteBytes(b.Bytes())
w.Flush() w.Flush()
result = append(result, b.Bytes()...)
}
return result
} }
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)
} }
} }
} }