diff --git a/eth/protocols/eth/receipt.go b/eth/protocols/eth/receipt.go index b388eb5897..050c356036 100644 --- a/eth/protocols/eth/receipt.go +++ b/eth/protocols/eth/receipt.go @@ -155,35 +155,39 @@ func (rl *ReceiptList69) EncodeForDatabase() []byte { if rl.buf == nil { rl.buf = new(receiptListBuffers) } - - var ( - r = &rl.items[i] - bloom = r.bloom(&rl.buf.bloom) - w = &rl.buf.enc - ) - // encode receipt list: [postStateOrStatus, gasUsed, bloom, logs] - w.Reset(b) - l := w.List() - w.WriteBytes(r.PostStateOrStatus) - w.WriteUint64(r.GasUsed) - w.WriteBytes(bloom[:]) - w.Write(r.Logs) - w.ListEnd(l) - if err := w.Flush(); err != nil { - return + // TODO this is probably wrong + var result []byte + var b = new(bytes.Buffer) + for _, r := range rl.items { + var ( + bloom = r.bloom(&rl.buf.bloom) + w = &rl.buf.enc + ) + // encode receipt list: [postStateOrStatus, gasUsed, bloom, logs] + w.Reset(b) + l := w.List() + w.WriteBytes(r.PostStateOrStatus) + w.WriteUint64(r.GasUsed) + w.WriteBytes(bloom[:]) + w.Write(r.Logs) + 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. - 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() - + return result } 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, // and applies the type-encoding on the receipts (for non-legacy receipts). // 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 ( out bytes.Buffer enc = rlp.NewEncoderBuffer(&out) @@ -212,7 +216,7 @@ func blockReceiptsToNetwork(blockReceipts []byte, txs []*types.Transaction) []by for i := 0; it.Next(); i++ { content, _, _ := rlp.SplitList(it.Value()) receiptList := enc.List() - enc.Write([]byte{txs[i].Type()}) + enc.WriteUint64(uint64(txs[i].Type())) enc.Write(content) enc.ListEnd(receiptList) } diff --git a/eth/protocols/eth/receipt_test.go b/eth/protocols/eth/receipt_test.go index 0438d6be6e..e4551e316e 100644 --- a/eth/protocols/eth/receipt_test.go +++ b/eth/protocols/eth/receipt_test.go @@ -26,6 +26,9 @@ import ( ) func TestTransformReceipts(t *testing.T) { + logs := []*types.Log{{Address: common.Address{1}, Topics: []common.Hash{{1}}}} + encLogs, _ := rlp.EncodeToBytes(logs) + tests := []struct { input []types.ReceiptForStorage txs []*types.Transaction @@ -34,22 +37,22 @@ func TestTransformReceipts(t *testing.T) { { input: []types.ReceiptForStorage{{CumulativeGasUsed: 123, Status: 1, Logs: nil}}, 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}}, 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}}, 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{})}, - 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) out, _ := rlp.EncodeToBytes(test.output) 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) } } }