Skip to content

Commit 643fd0e

Browse files
authored
core/types: remove support for legacy receipt/log storage encoding (#22852)
* core/types: remove support for legacy receipt storage encoding * core/types: remove support for legacy log storage encoding
1 parent e536bb5 commit 643fd0e

File tree

3 files changed

+7
-244
lines changed

3 files changed

+7
-244
lines changed

core/types/log.go

Lines changed: 4 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,6 @@ type rlpLog struct {
7171
// rlpStorageLog is the storage encoding of a log.
7272
type rlpStorageLog rlpLog
7373

74-
// legacyRlpStorageLog is the previous storage encoding of a log including some redundant fields.
75-
type legacyRlpStorageLog struct {
76-
Address common.Address
77-
Topics []common.Hash
78-
Data []byte
79-
BlockNumber uint64
80-
TxHash common.Hash
81-
TxIndex uint
82-
BlockHash common.Hash
83-
Index uint
84-
}
85-
8674
// EncodeRLP implements rlp.Encoder.
8775
func (l *Log) EncodeRLP(w io.Writer) error {
8876
return rlp.Encode(w, rlpLog{Address: l.Address, Topics: l.Topics, Data: l.Data})
@@ -115,29 +103,10 @@ func (l *LogForStorage) EncodeRLP(w io.Writer) error {
115103
//
116104
// Note some redundant fields(e.g. block number, tx hash etc) will be assembled later.
117105
func (l *LogForStorage) DecodeRLP(s *rlp.Stream) error {
118-
blob, err := s.Raw()
119-
if err != nil {
120-
return err
121-
}
122106
var dec rlpStorageLog
123-
err = rlp.DecodeBytes(blob, &dec)
124-
if err == nil {
125-
*l = LogForStorage{
126-
Address: dec.Address,
127-
Topics: dec.Topics,
128-
Data: dec.Data,
129-
}
130-
} else {
131-
// Try to decode log with previous definition.
132-
var dec legacyRlpStorageLog
133-
err = rlp.DecodeBytes(blob, &dec)
134-
if err == nil {
135-
*l = LogForStorage{
136-
Address: dec.Address,
137-
Topics: dec.Topics,
138-
Data: dec.Data,
139-
}
140-
}
107+
if err := s.Decode(&dec); err != nil {
108+
return err
141109
}
142-
return err
110+
*l = LogForStorage{Address: dec.Address, Topics: dec.Topics, Data: dec.Data}
111+
return nil
143112
}

core/types/receipt.go

Lines changed: 3 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,6 @@ type storedReceiptRLP struct {
9797
Logs []*LogForStorage
9898
}
9999

100-
// v4StoredReceiptRLP is the storage encoding of a receipt used in database version 4.
101-
type v4StoredReceiptRLP struct {
102-
PostStateOrStatus []byte
103-
CumulativeGasUsed uint64
104-
TxHash common.Hash
105-
ContractAddress common.Address
106-
Logs []*LogForStorage
107-
GasUsed uint64
108-
}
109-
110-
// v3StoredReceiptRLP is the original storage encoding of a receipt including some unnecessary fields.
111-
type v3StoredReceiptRLP struct {
112-
PostStateOrStatus []byte
113-
CumulativeGasUsed uint64
114-
Bloom Bloom
115-
TxHash common.Hash
116-
ContractAddress common.Address
117-
Logs []*LogForStorage
118-
GasUsed uint64
119-
}
120-
121100
// NewReceipt creates a barebone transaction receipt, copying the init fields.
122101
// Deprecated: create receipts using a struct literal instead.
123102
func NewReceipt(root []byte, failed bool, cumulativeGasUsed uint64) *Receipt {
@@ -237,8 +216,7 @@ func (r *Receipt) Size() common.StorageSize {
237216
// entire content of a receipt, as opposed to only the consensus fields originally.
238217
type ReceiptForStorage Receipt
239218

240-
// EncodeRLP implements rlp.Encoder, and flattens all content fields of a receipt
241-
// into an RLP stream.
219+
// EncodeRLP implements rlp.Encoder.
242220
func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
243221
enc := &storedReceiptRLP{
244222
PostStateOrStatus: (*Receipt)(r).statusEncoding(),
@@ -251,82 +229,21 @@ func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
251229
return rlp.Encode(w, enc)
252230
}
253231

254-
// DecodeRLP implements rlp.Decoder, and loads both consensus and implementation
255-
// fields of a receipt from an RLP stream.
232+
// DecodeRLP implements rlp.Decoder.
256233
func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error {
257-
// Retrieve the entire receipt blob as we need to try multiple decoders
258-
blob, err := s.Raw()
259-
if err != nil {
260-
return err
261-
}
262-
// Try decoding from the newest format for future proofness, then the older one
263-
// for old nodes that just upgraded. V4 was an intermediate unreleased format so
264-
// we do need to decode it, but it's not common (try last).
265-
if err := decodeStoredReceiptRLP(r, blob); err == nil {
266-
return nil
267-
}
268-
if err := decodeV3StoredReceiptRLP(r, blob); err == nil {
269-
return nil
270-
}
271-
return decodeV4StoredReceiptRLP(r, blob)
272-
}
273-
274-
func decodeStoredReceiptRLP(r *ReceiptForStorage, blob []byte) error {
275234
var stored storedReceiptRLP
276-
if err := rlp.DecodeBytes(blob, &stored); err != nil {
277-
return err
278-
}
279-
if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil {
280-
return err
281-
}
282-
r.CumulativeGasUsed = stored.CumulativeGasUsed
283-
r.Logs = make([]*Log, len(stored.Logs))
284-
for i, log := range stored.Logs {
285-
r.Logs[i] = (*Log)(log)
286-
}
287-
r.Bloom = CreateBloom(Receipts{(*Receipt)(r)})
288-
289-
return nil
290-
}
291-
292-
func decodeV4StoredReceiptRLP(r *ReceiptForStorage, blob []byte) error {
293-
var stored v4StoredReceiptRLP
294-
if err := rlp.DecodeBytes(blob, &stored); err != nil {
235+
if err := s.Decode(&stored); err != nil {
295236
return err
296237
}
297238
if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil {
298239
return err
299240
}
300241
r.CumulativeGasUsed = stored.CumulativeGasUsed
301-
r.TxHash = stored.TxHash
302-
r.ContractAddress = stored.ContractAddress
303-
r.GasUsed = stored.GasUsed
304242
r.Logs = make([]*Log, len(stored.Logs))
305243
for i, log := range stored.Logs {
306244
r.Logs[i] = (*Log)(log)
307245
}
308246
r.Bloom = CreateBloom(Receipts{(*Receipt)(r)})
309-
310-
return nil
311-
}
312-
313-
func decodeV3StoredReceiptRLP(r *ReceiptForStorage, blob []byte) error {
314-
var stored v3StoredReceiptRLP
315-
if err := rlp.DecodeBytes(blob, &stored); err != nil {
316-
return err
317-
}
318-
if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil {
319-
return err
320-
}
321-
r.CumulativeGasUsed = stored.CumulativeGasUsed
322-
r.Bloom = stored.Bloom
323-
r.TxHash = stored.TxHash
324-
r.ContractAddress = stored.ContractAddress
325-
r.GasUsed = stored.GasUsed
326-
r.Logs = make([]*Log, len(stored.Logs))
327-
for i, log := range stored.Logs {
328-
r.Logs[i] = (*Log)(log)
329-
}
330247
return nil
331248
}
332249

core/types/receipt_test.go

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"bytes"
2121
"math"
2222
"math/big"
23-
"reflect"
2423
"testing"
2524

2625
"github.com/ethereum/go-ethereum/common"
@@ -38,128 +37,6 @@ func TestDecodeEmptyTypedReceipt(t *testing.T) {
3837
}
3938
}
4039

41-
func TestLegacyReceiptDecoding(t *testing.T) {
42-
tests := []struct {
43-
name string
44-
encode func(*Receipt) ([]byte, error)
45-
}{
46-
{
47-
"StoredReceiptRLP",
48-
encodeAsStoredReceiptRLP,
49-
},
50-
{
51-
"V4StoredReceiptRLP",
52-
encodeAsV4StoredReceiptRLP,
53-
},
54-
{
55-
"V3StoredReceiptRLP",
56-
encodeAsV3StoredReceiptRLP,
57-
},
58-
}
59-
60-
tx := NewTransaction(1, common.HexToAddress("0x1"), big.NewInt(1), 1, big.NewInt(1), nil)
61-
receipt := &Receipt{
62-
Status: ReceiptStatusFailed,
63-
CumulativeGasUsed: 1,
64-
Logs: []*Log{
65-
{
66-
Address: common.BytesToAddress([]byte{0x11}),
67-
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
68-
Data: []byte{0x01, 0x00, 0xff},
69-
},
70-
{
71-
Address: common.BytesToAddress([]byte{0x01, 0x11}),
72-
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
73-
Data: []byte{0x01, 0x00, 0xff},
74-
},
75-
},
76-
TxHash: tx.Hash(),
77-
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
78-
GasUsed: 111111,
79-
}
80-
receipt.Bloom = CreateBloom(Receipts{receipt})
81-
82-
for _, tc := range tests {
83-
t.Run(tc.name, func(t *testing.T) {
84-
enc, err := tc.encode(receipt)
85-
if err != nil {
86-
t.Fatalf("Error encoding receipt: %v", err)
87-
}
88-
var dec ReceiptForStorage
89-
if err := rlp.DecodeBytes(enc, &dec); err != nil {
90-
t.Fatalf("Error decoding RLP receipt: %v", err)
91-
}
92-
// Check whether all consensus fields are correct.
93-
if dec.Status != receipt.Status {
94-
t.Fatalf("Receipt status mismatch, want %v, have %v", receipt.Status, dec.Status)
95-
}
96-
if dec.CumulativeGasUsed != receipt.CumulativeGasUsed {
97-
t.Fatalf("Receipt CumulativeGasUsed mismatch, want %v, have %v", receipt.CumulativeGasUsed, dec.CumulativeGasUsed)
98-
}
99-
if dec.Bloom != receipt.Bloom {
100-
t.Fatalf("Bloom data mismatch, want %v, have %v", receipt.Bloom, dec.Bloom)
101-
}
102-
if len(dec.Logs) != len(receipt.Logs) {
103-
t.Fatalf("Receipt log number mismatch, want %v, have %v", len(receipt.Logs), len(dec.Logs))
104-
}
105-
for i := 0; i < len(dec.Logs); i++ {
106-
if dec.Logs[i].Address != receipt.Logs[i].Address {
107-
t.Fatalf("Receipt log %d address mismatch, want %v, have %v", i, receipt.Logs[i].Address, dec.Logs[i].Address)
108-
}
109-
if !reflect.DeepEqual(dec.Logs[i].Topics, receipt.Logs[i].Topics) {
110-
t.Fatalf("Receipt log %d topics mismatch, want %v, have %v", i, receipt.Logs[i].Topics, dec.Logs[i].Topics)
111-
}
112-
if !bytes.Equal(dec.Logs[i].Data, receipt.Logs[i].Data) {
113-
t.Fatalf("Receipt log %d data mismatch, want %v, have %v", i, receipt.Logs[i].Data, dec.Logs[i].Data)
114-
}
115-
}
116-
})
117-
}
118-
}
119-
120-
func encodeAsStoredReceiptRLP(want *Receipt) ([]byte, error) {
121-
stored := &storedReceiptRLP{
122-
PostStateOrStatus: want.statusEncoding(),
123-
CumulativeGasUsed: want.CumulativeGasUsed,
124-
Logs: make([]*LogForStorage, len(want.Logs)),
125-
}
126-
for i, log := range want.Logs {
127-
stored.Logs[i] = (*LogForStorage)(log)
128-
}
129-
return rlp.EncodeToBytes(stored)
130-
}
131-
132-
func encodeAsV4StoredReceiptRLP(want *Receipt) ([]byte, error) {
133-
stored := &v4StoredReceiptRLP{
134-
PostStateOrStatus: want.statusEncoding(),
135-
CumulativeGasUsed: want.CumulativeGasUsed,
136-
TxHash: want.TxHash,
137-
ContractAddress: want.ContractAddress,
138-
Logs: make([]*LogForStorage, len(want.Logs)),
139-
GasUsed: want.GasUsed,
140-
}
141-
for i, log := range want.Logs {
142-
stored.Logs[i] = (*LogForStorage)(log)
143-
}
144-
return rlp.EncodeToBytes(stored)
145-
}
146-
147-
func encodeAsV3StoredReceiptRLP(want *Receipt) ([]byte, error) {
148-
stored := &v3StoredReceiptRLP{
149-
PostStateOrStatus: want.statusEncoding(),
150-
CumulativeGasUsed: want.CumulativeGasUsed,
151-
Bloom: want.Bloom,
152-
TxHash: want.TxHash,
153-
ContractAddress: want.ContractAddress,
154-
Logs: make([]*LogForStorage, len(want.Logs)),
155-
GasUsed: want.GasUsed,
156-
}
157-
for i, log := range want.Logs {
158-
stored.Logs[i] = (*LogForStorage)(log)
159-
}
160-
return rlp.EncodeToBytes(stored)
161-
}
162-
16340
// Tests that receipt data can be correctly derived from the contextual infos
16441
func TestDeriveFields(t *testing.T) {
16542
// Create a few transactions to have receipts for

0 commit comments

Comments
 (0)