eth/protocols/eth: fix receipt encoding for storage

This commit is contained in:
Felix Lange 2025-04-15 21:00:52 +02:00
parent 00846fbfb4
commit fa854ca7be
2 changed files with 32 additions and 6 deletions

View file

@ -62,6 +62,7 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
return s.ListEnd() return s.ListEnd()
} }
// EncodeRLP writes the network encoding of receipts.
func (r *Receipt) EncodeRLP(_w io.Writer) error { func (r *Receipt) EncodeRLP(_w io.Writer) error {
w := rlp.NewEncoderBuffer(_w) w := rlp.NewEncoderBuffer(_w)
list := w.List() list := w.List()
@ -73,6 +74,15 @@ func (r *Receipt) EncodeRLP(_w io.Writer) error {
return w.Flush() return w.Flush()
} }
// encodeStorage writes the network encoding of receipts.
func (r *Receipt) encodeStorage(w *rlp.EncoderBuffer) {
list := w.List()
w.WriteBytes(r.PostStateOrStatus)
w.WriteUint64(r.GasUsed)
w.Write(r.Logs)
w.ListEnd(list)
}
// bloom computes the bloom filter of the receipt. // bloom computes the bloom filter of the receipt.
// Note this doesn't check the validity of encoding, and will produce an invalid filter // Note this doesn't check the validity of encoding, and will produce an invalid filter
// for invalid input. This is acceptable for the purpose of this function, which is // for invalid input. This is acceptable for the purpose of this function, which is
@ -150,6 +160,7 @@ func (rl *ReceiptList69) EncodeIndex(i int, b *bytes.Buffer) {
w.Flush() w.Flush()
} }
// DecodeRLP decodes a list receipts from the network format.
func (rl *ReceiptList69) DecodeRLP(s *rlp.Stream) error { func (rl *ReceiptList69) DecodeRLP(s *rlp.Stream) error {
if _, err := s.List(); err != nil { if _, err := s.List(); err != nil {
return err return err
@ -165,17 +176,23 @@ func (rl *ReceiptList69) DecodeRLP(s *rlp.Stream) error {
return s.ListEnd() return s.ListEnd()
} }
// toStorageReceiptsRLP encodes the receipts for storage into the database.
func (rl *ReceiptList69) toStorageReceiptsRLP() rlp.RawValue { func (rl *ReceiptList69) toStorageReceiptsRLP() rlp.RawValue {
if rl.buf == nil {
rl.buf = new(receiptListBuffers)
}
var ( var (
out bytes.Buffer out bytes.Buffer
enc = rlp.NewEncoderBuffer(&out) w = &rl.buf.enc
) )
outer := enc.List() w.Reset(&out)
outer := w.List()
for _, receipts := range rl.items { for _, receipts := range rl.items {
receipts.EncodeRLP(enc) receipts.encodeStorage(w)
} }
enc.ListEnd(outer) w.ListEnd(outer)
enc.Flush() w.Flush()
return out.Bytes() return out.Bytes()
} }

View file

@ -61,7 +61,16 @@ 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, in %v", i, out, have, in) t.Fatalf("test[%d]: blockReceiptToNetwork mismatch\nhave: %x\nwant: %x\n in: %x", i, out, have, in)
}
var rl ReceiptList69
if err := rlp.DecodeBytes(out, &rl); err != nil {
t.Fatalf("test[%d]: can't decode network receipts: %v", i, err)
}
storageEnc := rl.toStorageReceiptsRLP()
if !bytes.Equal(storageEnc, in) {
t.Fatalf("test[%d]: re-encoded receipts not equal\nhave: %x\nwant: %x", i, storageEnc, in)
} }
} }
} }