diff --git a/eth/protocols/eth/receipt.go b/eth/protocols/eth/receipt.go index 9416850025..0a2f1cdb21 100644 --- a/eth/protocols/eth/receipt.go +++ b/eth/protocols/eth/receipt.go @@ -62,6 +62,7 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error { return s.ListEnd() } +// EncodeRLP writes the network encoding of receipts. func (r *Receipt) EncodeRLP(_w io.Writer) error { w := rlp.NewEncoderBuffer(_w) list := w.List() @@ -73,6 +74,15 @@ func (r *Receipt) EncodeRLP(_w io.Writer) error { 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. // 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 @@ -150,6 +160,7 @@ func (rl *ReceiptList69) EncodeIndex(i int, b *bytes.Buffer) { w.Flush() } +// DecodeRLP decodes a list receipts from the network format. func (rl *ReceiptList69) DecodeRLP(s *rlp.Stream) error { if _, err := s.List(); err != nil { return err @@ -165,17 +176,23 @@ func (rl *ReceiptList69) DecodeRLP(s *rlp.Stream) error { return s.ListEnd() } +// toStorageReceiptsRLP encodes the receipts for storage into the database. func (rl *ReceiptList69) toStorageReceiptsRLP() rlp.RawValue { + if rl.buf == nil { + rl.buf = new(receiptListBuffers) + } + var ( 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 { - receipts.EncodeRLP(enc) + receipts.encodeStorage(w) } - enc.ListEnd(outer) - enc.Flush() + w.ListEnd(outer) + w.Flush() return out.Bytes() } diff --git a/eth/protocols/eth/receipt_test.go b/eth/protocols/eth/receipt_test.go index 14dcc7e717..f73532a2e8 100644 --- a/eth/protocols/eth/receipt_test.go +++ b/eth/protocols/eth/receipt_test.go @@ -61,7 +61,16 @@ 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, 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) } } }