restricted/types: fix discrepancy in receipt.EffectiveGasPrice json encoding tags

Port of https://github.com/ethereum/go-ethereum/pull/27114
This commit is contained in:
Roy Crihfield 2023-07-19 13:17:41 +08:00
parent 211a8aba96
commit 54c41c300a
3 changed files with 63 additions and 23 deletions

View File

@ -25,7 +25,7 @@ func (r Receipt) MarshalJSON() ([]byte, error) {
TxHash core.Hash `json:"transactionHash" gencodec:"required"`
ContractAddress core.Address `json:"contractAddress"`
GasUsed hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
EffectiveGasPrice *hexutil.Big `json:"effectiveGasPrice,omitempty"`
EffectiveGasPrice *hexutil.Big `json:"effectiveGasPrice"`
BlockHash core.Hash `json:"blockHash,omitempty"`
BlockNumber *hexutil.Big `json:"blockNumber,omitempty"`
TransactionIndex hexutil.Uint `json:"transactionIndex"`
@ -59,7 +59,7 @@ func (r *Receipt) UnmarshalJSON(input []byte) error {
TxHash *core.Hash `json:"transactionHash" gencodec:"required"`
ContractAddress *core.Address `json:"contractAddress"`
GasUsed *hexutil.Uint64 `json:"gasUsed" gencodec:"required"`
EffectiveGasPrice *hexutil.Big `json:"effectiveGasPrice,omitempty"`
EffectiveGasPrice *hexutil.Big `json:"effectiveGasPrice"`
BlockHash *core.Hash `json:"blockHash,omitempty"`
BlockNumber *hexutil.Big `json:"blockNumber,omitempty"`
TransactionIndex *hexutil.Uint `json:"transactionIndex"`

View File

@ -63,7 +63,7 @@ type Receipt struct {
TxHash core.Hash `json:"transactionHash" gencodec:"required"`
ContractAddress core.Address `json:"contractAddress"`
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
EffectiveGasPrice *big.Int `json:"effectiveGasPrice"`
EffectiveGasPrice *big.Int `json:"effectiveGasPrice"` // required, but tag omitted for backwards compatibility
// Inclusion information: These fields provide information about the inclusion of the
// transaction corresponding to this receipt.
@ -78,6 +78,7 @@ type receiptMarshaling struct {
Status hexutil.Uint64
CumulativeGasUsed hexutil.Uint64
GasUsed hexutil.Uint64
EffectiveGasPrice *hexutil.Big
BlockNumber *hexutil.Big
TransactionIndex hexutil.Uint
}

View File

@ -83,25 +83,15 @@ var (
},
Type: DynamicFeeTxType,
}
)
func TestDecodeEmptyTypedReceipt(t *testing.T) {
input := []byte{0x80}
var r Receipt
err := rlp.DecodeBytes(input, &r)
if err != errEmptyTypedReceipt {
t.Fatal("wrong error:", err)
}
}
// Tests that receipt data can be correctly derived from the contextual infos
func TestDeriveFields(t *testing.T) {
// Create a few transactions to have receipts for
to2 := core.HexToAddress("0x2")
to3 := core.HexToAddress("0x3")
to4 := core.HexToAddress("0x4")
to5 := core.HexToAddress("0x5")
txs := Transactions{
to2 = core.HexToAddress("0x2")
to3 = core.HexToAddress("0x3")
to4 = core.HexToAddress("0x4")
to5 = core.HexToAddress("0x5")
to6 = core.HexToAddress("0x6")
to7 = core.HexToAddress("0x7")
txs = Transactions{
NewTx(&LegacyTx{
Nonce: 1,
Value: big.NewInt(1),
@ -141,17 +131,18 @@ func TestDeriveFields(t *testing.T) {
}),
}
blockNumber := big.NewInt(1)
blockHash := core.BytesToHash([]byte{0x03, 0x14})
blockNumber = big.NewInt(1)
blockHash = core.BytesToHash([]byte{0x03, 0x14})
// Create the corresponding receipts
receipts := Receipts{
receipts = Receipts{
&Receipt{
Status: ReceiptStatusFailed,
CumulativeGasUsed: 1,
Logs: []*Log{
{
Address: core.BytesToAddress([]byte{0x11}),
Topics: []core.Hash{core.HexToHash("dead"), core.HexToHash("beef")},
// derived fields:
BlockNumber: blockNumber.Uint64(),
TxHash: txs[0].Hash(),
@ -161,6 +152,7 @@ func TestDeriveFields(t *testing.T) {
},
{
Address: core.BytesToAddress([]byte{0x01, 0x11}),
Topics: []core.Hash{core.HexToHash("dead"), core.HexToHash("beef")},
// derived fields:
BlockNumber: blockNumber.Uint64(),
TxHash: txs[0].Hash(),
@ -184,6 +176,7 @@ func TestDeriveFields(t *testing.T) {
Logs: []*Log{
{
Address: core.BytesToAddress([]byte{0x22}),
Topics: []core.Hash{core.HexToHash("dead"), core.HexToHash("beef")},
// derived fields:
BlockNumber: blockNumber.Uint64(),
TxHash: txs[1].Hash(),
@ -193,6 +186,7 @@ func TestDeriveFields(t *testing.T) {
},
{
Address: core.BytesToAddress([]byte{0x02, 0x22}),
Topics: []core.Hash{core.HexToHash("dead"), core.HexToHash("beef")},
// derived fields:
BlockNumber: blockNumber.Uint64(),
TxHash: txs[1].Hash(),
@ -249,7 +243,19 @@ func TestDeriveFields(t *testing.T) {
TransactionIndex: 4,
},
}
)
func TestDecodeEmptyTypedReceipt(t *testing.T) {
input := []byte{0x80}
var r Receipt
err := rlp.DecodeBytes(input, &r)
if err != errEmptyTypedReceipt {
t.Fatal("wrong error:", err)
}
}
// Tests that receipt data can be correctly derived from the contextual infos
func TestDeriveFields(t *testing.T) {
// Re-derive receipts.
basefee := big.NewInt(1000)
derivedReceipts := clearComputedFieldsOnReceipts(receipts)
@ -263,6 +269,7 @@ func TestDeriveFields(t *testing.T) {
if err != nil {
t.Fatal("error marshaling input receipts:", err)
}
r2, err := json.MarshalIndent(derivedReceipts, "", " ")
if err != nil {
t.Fatal("error marshaling derived receipts:", err)
@ -273,6 +280,38 @@ func TestDeriveFields(t *testing.T) {
}
}
// Test that we can marshal/unmarshal receipts to/from json without errors.
// This also confirms that our test receipts contain all the required fields.
func TestReceiptJSON(t *testing.T) {
for i := range receipts {
b, err := receipts[i].MarshalJSON()
if err != nil {
t.Fatal("error marshaling receipt to json:", err)
}
r := Receipt{}
err = r.UnmarshalJSON(b)
if err != nil {
t.Fatal("error unmarshaling receipt from json:", err)
}
}
}
// Test we can still parse receipt without EffectiveGasPrice for backwards compatibility, even
// though it is required per the spec.
func TestEffectiveGasPriceNotRequired(t *testing.T) {
r := *receipts[0]
r.EffectiveGasPrice = nil
b, err := r.MarshalJSON()
if err != nil {
t.Fatal("error marshaling receipt to json:", err)
}
r2 := Receipt{}
err = r2.UnmarshalJSON(b)
if err != nil {
t.Fatal("error unmarshaling receipt from json:", err)
}
}
// TestTypedReceiptEncodingDecoding reproduces a flaw that existed in the receipt
// rlp decoder, which failed due to a shadowing error.
func TestTypedReceiptEncodingDecoding(t *testing.T) {