Skip to content

Commit

Permalink
ethapi/receipt: Use hexutil for JSON marshalling (#315)
Browse files Browse the repository at this point in the history
This switches *big.Int to *hexutil.Big and *uint32 to *hexutil.Uint64 for
marshalling the post-ecotone fields. Hexutil is what is expected on the read side.

I had to switch from uint32 to uint64 for the scalar fields in the JSON because
there is no hexutil.Uint32. This conversion is safe & internally it still uses
a uint32 type.
  • Loading branch information
trianglesphere authored May 15, 2024
1 parent 3653ceb commit 6b2bf0f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
24 changes: 12 additions & 12 deletions core/types/gen_receipt_json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 15 additions & 4 deletions core/types/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ type Receipt struct {
L1GasUsed *big.Int `json:"l1GasUsed,omitempty"` // Present from pre-bedrock
L1Fee *big.Int `json:"l1Fee,omitempty"` // Present from pre-bedrock
FeeScalar *big.Float `json:"l1FeeScalar,omitempty"` // Present from pre-bedrock to Ecotone. Nil after Ecotone
L1BaseFeeScalar *uint32 `json:"l1BaseFeeScalar,omitempty"` // Always nil prior to the Ecotone hardfork
L1BlobBaseFeeScalar *uint32 `json:"l1BlobBaseFeeScalar,omitempty"` // Always nil prior to the Ecotone hardfork
L1BaseFeeScalar *uint64 `json:"l1BaseFeeScalar,omitempty"` // Always nil prior to the Ecotone hardfork
L1BlobBaseFeeScalar *uint64 `json:"l1BlobBaseFeeScalar,omitempty"` // Always nil prior to the Ecotone hardfork
}

type receiptMarshaling struct {
Expand All @@ -108,9 +108,12 @@ type receiptMarshaling struct {

// Optimism
L1GasPrice *hexutil.Big
L1BlobBaseFee *hexutil.Big
L1GasUsed *hexutil.Big
L1Fee *hexutil.Big
FeeScalar *big.Float
L1BaseFeeScalar *hexutil.Uint64
L1BlobBaseFeeScalar *hexutil.Uint64
DepositNonce *hexutil.Uint64
DepositReceiptVersion *hexutil.Uint64
}
Expand Down Expand Up @@ -585,9 +588,17 @@ func (rs Receipts) DeriveFields(config *params.ChainConfig, hash common.Hash, nu
rs[i].L1BlobBaseFee = gasParams.l1BlobBaseFee
rs[i].L1Fee, rs[i].L1GasUsed = gasParams.costFunc(txs[i].RollupCostData())
rs[i].FeeScalar = gasParams.feeScalar
rs[i].L1BaseFeeScalar = gasParams.l1BaseFeeScalar
rs[i].L1BlobBaseFeeScalar = gasParams.l1BlobBaseFeeScalar
rs[i].L1BaseFeeScalar = u32ptrTou64ptr(gasParams.l1BaseFeeScalar)
rs[i].L1BlobBaseFeeScalar = u32ptrTou64ptr(gasParams.l1BlobBaseFeeScalar)
}
}
return nil
}

func u32ptrTou64ptr(a *uint32) *uint64 {
if a == nil {
return nil
}
b := uint64(*a)
return &b
}
8 changes: 4 additions & 4 deletions core/types/receipt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ func clearComputedFieldsOnLogs(logs []*Log) []*Log {
return l
}

func getOptimismEcotoneTxReceipts(l1AttributesPayload []byte, l1GasPrice, l1BlobGasPrice, l1GasUsed, l1Fee *big.Int, baseFeeScalar, blobBaseFeeScalar *uint32) ([]*Transaction, []*Receipt) {
func getOptimismEcotoneTxReceipts(l1AttributesPayload []byte, l1GasPrice, l1BlobGasPrice, l1GasUsed, l1Fee *big.Int, baseFeeScalar, blobBaseFeeScalar *uint64) ([]*Transaction, []*Receipt) {
// Create a few transactions to have receipts for
txs := Transactions{
NewTx(&DepositTx{
Expand Down Expand Up @@ -868,9 +868,9 @@ func TestDeriveOptimismEcotoneTxReceipts(t *testing.T) {
// Ecotone style l1 attributes with baseFeeScalar=2, blobBaseFeeScalar=3, baseFee=1000*1e6, blobBaseFee=10*1e6
payload := common.Hex2Bytes("440a5e20000000020000000300000000000004d200000000000004d200000000000004d2000000000000000000000000000000000000000000000000000000003b9aca00000000000000000000000000000000000000000000000000000000000098968000000000000000000000000000000000000000000000000000000000000004d200000000000000000000000000000000000000000000000000000000000004d2")
// the parameters we use below are defined in rollup_test.go
baseFeeScalarUint32 := uint32(baseFeeScalar.Uint64())
blobBaseFeeScalarUint32 := uint32(blobBaseFeeScalar.Uint64())
txs, receipts := getOptimismEcotoneTxReceipts(payload, baseFee, blobBaseFee, ecotoneGas, ecotoneFee, &baseFeeScalarUint32, &blobBaseFeeScalarUint32)
baseFeeScalarUint64 := baseFeeScalar.Uint64()
blobBaseFeeScalarUint64 := blobBaseFeeScalar.Uint64()
txs, receipts := getOptimismEcotoneTxReceipts(payload, baseFee, blobBaseFee, ecotoneGas, ecotoneFee, &baseFeeScalarUint64, &blobBaseFeeScalarUint64)

// Re-derive receipts.
baseFee := big.NewInt(1000)
Expand Down

0 comments on commit 6b2bf0f

Please sign in to comment.