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:
		
							parent
							
								
									211a8aba96
								
							
						
					
					
						commit
						54c41c300a
					
				| @ -25,7 +25,7 @@ func (r Receipt) MarshalJSON() ([]byte, error) { | |||||||
| 		TxHash            core.Hash      `json:"transactionHash" gencodec:"required"` | 		TxHash            core.Hash      `json:"transactionHash" gencodec:"required"` | ||||||
| 		ContractAddress   core.Address   `json:"contractAddress"` | 		ContractAddress   core.Address   `json:"contractAddress"` | ||||||
| 		GasUsed           hexutil.Uint64 `json:"gasUsed" gencodec:"required"` | 		GasUsed           hexutil.Uint64 `json:"gasUsed" gencodec:"required"` | ||||||
| 		EffectiveGasPrice *hexutil.Big   `json:"effectiveGasPrice,omitempty"` | 		EffectiveGasPrice *hexutil.Big   `json:"effectiveGasPrice"` | ||||||
| 		BlockHash         core.Hash      `json:"blockHash,omitempty"` | 		BlockHash         core.Hash      `json:"blockHash,omitempty"` | ||||||
| 		BlockNumber       *hexutil.Big   `json:"blockNumber,omitempty"` | 		BlockNumber       *hexutil.Big   `json:"blockNumber,omitempty"` | ||||||
| 		TransactionIndex  hexutil.Uint   `json:"transactionIndex"` | 		TransactionIndex  hexutil.Uint   `json:"transactionIndex"` | ||||||
| @ -59,7 +59,7 @@ func (r *Receipt) UnmarshalJSON(input []byte) error { | |||||||
| 		TxHash            *core.Hash      `json:"transactionHash" gencodec:"required"` | 		TxHash            *core.Hash      `json:"transactionHash" gencodec:"required"` | ||||||
| 		ContractAddress   *core.Address   `json:"contractAddress"` | 		ContractAddress   *core.Address   `json:"contractAddress"` | ||||||
| 		GasUsed           *hexutil.Uint64 `json:"gasUsed" gencodec:"required"` | 		GasUsed           *hexutil.Uint64 `json:"gasUsed" gencodec:"required"` | ||||||
| 		EffectiveGasPrice *hexutil.Big    `json:"effectiveGasPrice,omitempty"` | 		EffectiveGasPrice *hexutil.Big    `json:"effectiveGasPrice"` | ||||||
| 		BlockHash         *core.Hash      `json:"blockHash,omitempty"` | 		BlockHash         *core.Hash      `json:"blockHash,omitempty"` | ||||||
| 		BlockNumber       *hexutil.Big    `json:"blockNumber,omitempty"` | 		BlockNumber       *hexutil.Big    `json:"blockNumber,omitempty"` | ||||||
| 		TransactionIndex  *hexutil.Uint   `json:"transactionIndex"` | 		TransactionIndex  *hexutil.Uint   `json:"transactionIndex"` | ||||||
|  | |||||||
| @ -63,7 +63,7 @@ type Receipt struct { | |||||||
| 	TxHash            core.Hash    `json:"transactionHash" gencodec:"required"` | 	TxHash            core.Hash    `json:"transactionHash" gencodec:"required"` | ||||||
| 	ContractAddress   core.Address `json:"contractAddress"` | 	ContractAddress   core.Address `json:"contractAddress"` | ||||||
| 	GasUsed           uint64       `json:"gasUsed" gencodec:"required"` | 	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
 | 	// Inclusion information: These fields provide information about the inclusion of the
 | ||||||
| 	// transaction corresponding to this receipt.
 | 	// transaction corresponding to this receipt.
 | ||||||
| @ -78,6 +78,7 @@ type receiptMarshaling struct { | |||||||
| 	Status            hexutil.Uint64 | 	Status            hexutil.Uint64 | ||||||
| 	CumulativeGasUsed hexutil.Uint64 | 	CumulativeGasUsed hexutil.Uint64 | ||||||
| 	GasUsed           hexutil.Uint64 | 	GasUsed           hexutil.Uint64 | ||||||
|  | 	EffectiveGasPrice *hexutil.Big | ||||||
| 	BlockNumber       *hexutil.Big | 	BlockNumber       *hexutil.Big | ||||||
| 	TransactionIndex  hexutil.Uint | 	TransactionIndex  hexutil.Uint | ||||||
| } | } | ||||||
|  | |||||||
| @ -83,25 +83,15 @@ var ( | |||||||
| 		}, | 		}, | ||||||
| 		Type: DynamicFeeTxType, | 		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
 | 	// Create a few transactions to have receipts for
 | ||||||
| 	to2 := core.HexToAddress("0x2") | 	to2 = core.HexToAddress("0x2") | ||||||
| 	to3 := core.HexToAddress("0x3") | 	to3 = core.HexToAddress("0x3") | ||||||
| 	to4 := core.HexToAddress("0x4") | 	to4 = core.HexToAddress("0x4") | ||||||
| 	to5 := core.HexToAddress("0x5") | 	to5 = core.HexToAddress("0x5") | ||||||
| 	txs := Transactions{ | 	to6 = core.HexToAddress("0x6") | ||||||
|  | 	to7 = core.HexToAddress("0x7") | ||||||
|  | 	txs = Transactions{ | ||||||
| 		NewTx(&LegacyTx{ | 		NewTx(&LegacyTx{ | ||||||
| 			Nonce:    1, | 			Nonce:    1, | ||||||
| 			Value:    big.NewInt(1), | 			Value:    big.NewInt(1), | ||||||
| @ -141,17 +131,18 @@ func TestDeriveFields(t *testing.T) { | |||||||
| 		}), | 		}), | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	blockNumber := big.NewInt(1) | 	blockNumber = big.NewInt(1) | ||||||
| 	blockHash := core.BytesToHash([]byte{0x03, 0x14}) | 	blockHash   = core.BytesToHash([]byte{0x03, 0x14}) | ||||||
| 
 | 
 | ||||||
| 	// Create the corresponding receipts
 | 	// Create the corresponding receipts
 | ||||||
| 	receipts := Receipts{ | 	receipts = Receipts{ | ||||||
| 		&Receipt{ | 		&Receipt{ | ||||||
| 			Status:            ReceiptStatusFailed, | 			Status:            ReceiptStatusFailed, | ||||||
| 			CumulativeGasUsed: 1, | 			CumulativeGasUsed: 1, | ||||||
| 			Logs: []*Log{ | 			Logs: []*Log{ | ||||||
| 				{ | 				{ | ||||||
| 					Address: core.BytesToAddress([]byte{0x11}), | 					Address: core.BytesToAddress([]byte{0x11}), | ||||||
|  | 					Topics:  []core.Hash{core.HexToHash("dead"), core.HexToHash("beef")}, | ||||||
| 					// derived fields:
 | 					// derived fields:
 | ||||||
| 					BlockNumber: blockNumber.Uint64(), | 					BlockNumber: blockNumber.Uint64(), | ||||||
| 					TxHash:      txs[0].Hash(), | 					TxHash:      txs[0].Hash(), | ||||||
| @ -161,6 +152,7 @@ func TestDeriveFields(t *testing.T) { | |||||||
| 				}, | 				}, | ||||||
| 				{ | 				{ | ||||||
| 					Address: core.BytesToAddress([]byte{0x01, 0x11}), | 					Address: core.BytesToAddress([]byte{0x01, 0x11}), | ||||||
|  | 					Topics:  []core.Hash{core.HexToHash("dead"), core.HexToHash("beef")}, | ||||||
| 					// derived fields:
 | 					// derived fields:
 | ||||||
| 					BlockNumber: blockNumber.Uint64(), | 					BlockNumber: blockNumber.Uint64(), | ||||||
| 					TxHash:      txs[0].Hash(), | 					TxHash:      txs[0].Hash(), | ||||||
| @ -184,6 +176,7 @@ func TestDeriveFields(t *testing.T) { | |||||||
| 			Logs: []*Log{ | 			Logs: []*Log{ | ||||||
| 				{ | 				{ | ||||||
| 					Address: core.BytesToAddress([]byte{0x22}), | 					Address: core.BytesToAddress([]byte{0x22}), | ||||||
|  | 					Topics:  []core.Hash{core.HexToHash("dead"), core.HexToHash("beef")}, | ||||||
| 					// derived fields:
 | 					// derived fields:
 | ||||||
| 					BlockNumber: blockNumber.Uint64(), | 					BlockNumber: blockNumber.Uint64(), | ||||||
| 					TxHash:      txs[1].Hash(), | 					TxHash:      txs[1].Hash(), | ||||||
| @ -193,6 +186,7 @@ func TestDeriveFields(t *testing.T) { | |||||||
| 				}, | 				}, | ||||||
| 				{ | 				{ | ||||||
| 					Address: core.BytesToAddress([]byte{0x02, 0x22}), | 					Address: core.BytesToAddress([]byte{0x02, 0x22}), | ||||||
|  | 					Topics:  []core.Hash{core.HexToHash("dead"), core.HexToHash("beef")}, | ||||||
| 					// derived fields:
 | 					// derived fields:
 | ||||||
| 					BlockNumber: blockNumber.Uint64(), | 					BlockNumber: blockNumber.Uint64(), | ||||||
| 					TxHash:      txs[1].Hash(), | 					TxHash:      txs[1].Hash(), | ||||||
| @ -249,7 +243,19 @@ func TestDeriveFields(t *testing.T) { | |||||||
| 			TransactionIndex:  4, | 			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.
 | 	// Re-derive receipts.
 | ||||||
| 	basefee := big.NewInt(1000) | 	basefee := big.NewInt(1000) | ||||||
| 	derivedReceipts := clearComputedFieldsOnReceipts(receipts) | 	derivedReceipts := clearComputedFieldsOnReceipts(receipts) | ||||||
| @ -263,6 +269,7 @@ func TestDeriveFields(t *testing.T) { | |||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal("error marshaling input receipts:", err) | 		t.Fatal("error marshaling input receipts:", err) | ||||||
| 	} | 	} | ||||||
|  | 
 | ||||||
| 	r2, err := json.MarshalIndent(derivedReceipts, "", "  ") | 	r2, err := json.MarshalIndent(derivedReceipts, "", "  ") | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		t.Fatal("error marshaling derived receipts:", err) | 		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
 | // TestTypedReceiptEncodingDecoding reproduces a flaw that existed in the receipt
 | ||||||
| // rlp decoder, which failed due to a shadowing error.
 | // rlp decoder, which failed due to a shadowing error.
 | ||||||
| func TestTypedReceiptEncodingDecoding(t *testing.T) { | func TestTypedReceiptEncodingDecoding(t *testing.T) { | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user