Skip to content

Commit ce9a289

Browse files
rjl493456442karalabe
authored andcommitted
core/types: fix cummulative gas bug and legacy decoding tests
1 parent 7221cb1 commit ce9a289

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

core/types/receipt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func decodeV3StoredReceiptRLP(r *ReceiptForStorage, blob []byte) error {
265265
if err := (*Receipt)(r).setStatus(stored.PostStateOrStatus); err != nil {
266266
return err
267267
}
268-
r.CumulativeGasUsed = stored.GasUsed
268+
r.CumulativeGasUsed = stored.CumulativeGasUsed
269269
r.Bloom = stored.Bloom
270270
r.TxHash = stored.TxHash
271271
r.ContractAddress = stored.ContractAddress

core/types/receipt_test.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ package types
1818

1919
import (
2020
"bytes"
21-
"encoding/hex"
22-
"encoding/json"
2321
"math"
2422
"math/big"
23+
"reflect"
2524
"testing"
2625

2726
"github.com/ethereum/go-ethereum/common"
@@ -54,39 +53,56 @@ func TestLegacyReceiptDecoding(t *testing.T) {
5453
Status: ReceiptStatusFailed,
5554
CumulativeGasUsed: 1,
5655
Logs: []*Log{
57-
{Address: common.BytesToAddress([]byte{0x11})},
58-
{Address: common.BytesToAddress([]byte{0x01, 0x11})},
56+
{
57+
Address: common.BytesToAddress([]byte{0x11}),
58+
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
59+
Data: []byte{0x01, 0x00, 0xff},
60+
},
61+
{
62+
Address: common.BytesToAddress([]byte{0x01, 0x11}),
63+
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
64+
Data: []byte{0x01, 0x00, 0xff},
65+
},
5966
},
6067
TxHash: tx.Hash(),
6168
ContractAddress: common.BytesToAddress([]byte{0x01, 0x11, 0x11}),
6269
GasUsed: 111111,
6370
}
6471
receipt.Bloom = CreateBloom(Receipts{receipt})
6572

66-
want, err := json.Marshal(receipt)
67-
if err != nil {
68-
t.Fatalf("Error encoding reference receipt to JSON: %v", err)
69-
}
70-
7173
for _, tc := range tests {
7274
t.Run(tc.name, func(t *testing.T) {
7375
enc, err := tc.encode(receipt)
7476
if err != nil {
7577
t.Fatalf("Error encoding receipt: %v", err)
7678
}
77-
7879
var dec ReceiptForStorage
7980
if err := rlp.DecodeBytes(enc, &dec); err != nil {
8081
t.Fatalf("Error decoding RLP receipt: %v", err)
8182
}
82-
83-
have, err := rlp.EncodeToBytes((*Receipt)(receipt))
84-
if err != nil {
85-
t.Fatalf("Error encoding receipt: %v", err)
83+
// Check whether all consensus fields are correct.
84+
if dec.Status != receipt.Status {
85+
t.Fatalf("Receipt status mismatch, want %v, have %v", receipt.Status, dec.Status)
8686
}
87-
88-
if !bytes.Equal(have, want) {
89-
t.Fatalf("receipt mismatch: have %s, want %s", hex.EncodeToString(have), hex.EncodeToString(want))
87+
if dec.CumulativeGasUsed != receipt.CumulativeGasUsed {
88+
t.Fatalf("Receipt CumulativeGasUsed mismatch, want %v, have %v", receipt.CumulativeGasUsed, dec.CumulativeGasUsed)
89+
}
90+
if dec.Bloom != receipt.Bloom {
91+
t.Fatalf("Bloom data mismatch, want %v, have %v", receipt.Bloom, dec.Bloom)
92+
}
93+
if len(dec.Logs) != len(receipt.Logs) {
94+
t.Fatalf("Receipt log number mismatch, want %v, have %v", len(receipt.Logs), len(dec.Logs))
95+
}
96+
for i := 0; i < len(dec.Logs); i++ {
97+
if dec.Logs[i].Address != receipt.Logs[i].Address {
98+
t.Fatalf("Receipt log %d address mismatch, want %v, have %v", i, receipt.Logs[i].Address, dec.Logs[i].Address)
99+
}
100+
if !reflect.DeepEqual(dec.Logs[i].Topics, receipt.Logs[i].Topics) {
101+
t.Fatalf("Receipt log %d topics mismatch, want %v, have %v", i, receipt.Logs[i].Topics, dec.Logs[i].Topics)
102+
}
103+
if !bytes.Equal(dec.Logs[i].Data, receipt.Logs[i].Data) {
104+
t.Fatalf("Receipt log %d data mismatch, want %v, have %v", i, receipt.Logs[i].Data, dec.Logs[i].Data)
105+
}
90106
}
91107
})
92108
}
@@ -136,7 +152,7 @@ func encodeAsV3StoredReceiptRLP(want *Receipt) ([]byte, error) {
136152
}
137153

138154
// Tests that receipt data can be correctly derived from the contextual infos
139-
func TestSetReceiptsData(t *testing.T) {
155+
func TestDeriveFields(t *testing.T) {
140156
// Create a few transactions to have receipts for
141157
txs := Transactions{
142158
NewContractCreation(1, big.NewInt(1), 1, big.NewInt(1), nil),
@@ -167,8 +183,6 @@ func TestSetReceiptsData(t *testing.T) {
167183
GasUsed: 2,
168184
},
169185
}
170-
receipts[1].Bloom = CreateBloom(Receipts{receipts[1]})
171-
172186
// Clear all the computed fields and re-derive them
173187
number := big.NewInt(1)
174188
hash := common.BytesToHash([]byte{0x03, 0x14})
@@ -198,7 +212,7 @@ func TestSetReceiptsData(t *testing.T) {
198212
t.Errorf("receipts[%d].GasUsed = %d, want %d", i, receipts[i].GasUsed, txs[i].Gas())
199213
}
200214
if txs[i].To() != nil && receipts[i].ContractAddress != (common.Address{}) {
201-
t.Errorf("receipts[%d].ContractAddress = %s, want %s", i, receipts[i].ContractAddress, (common.Address{}).String())
215+
t.Errorf("receipts[%d].ContractAddress = %s, want %s", i, receipts[i].ContractAddress.String(), (common.Address{}).String())
202216
}
203217
from, _ := Sender(signer, txs[i])
204218
contractAddress := crypto.CreateAddress(from, txs[i].Nonce())

0 commit comments

Comments
 (0)