mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-26 06:36:43 +00:00
eth/protocols/eth: fix receipt encoding for storage
This commit is contained in:
parent
00846fbfb4
commit
fa854ca7be
2 changed files with 32 additions and 6 deletions
|
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue