Skip to content

Commit

Permalink
Fix Execution Block Unmarshalling in Deneb (#12860)
Browse files Browse the repository at this point in the history
Co-authored-by: james-prysm <[email protected]>
  • Loading branch information
nisdas and james-prysm authored Sep 7, 2023
1 parent d557575 commit d506f9b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
12 changes: 0 additions & 12 deletions proto/engine/v1/json_marshal_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ type ExecutionBlock struct {
Transactions []*gethtypes.Transaction `json:"transactions"`
TotalDifficulty string `json:"totalDifficulty"`
Withdrawals []*Withdrawal `json:"withdrawals"`
BlobGasUsed *hexutil.Uint64 `json:"blobGasUsed"`
ExcessBlobGas *hexutil.Uint64 `json:"excessBlobGas"`
}

func (e *ExecutionBlock) MarshalJSON() ([]byte, error) {
Expand Down Expand Up @@ -110,21 +108,11 @@ func (e *ExecutionBlock) UnmarshalJSON(enc []byte) error {
edg, has := decoded["excessBlobGas"]
if has && edg != nil {
e.Version = version.Deneb
uedg, err := hexutil.DecodeUint64(edg.(string))
if err != nil {
return errors.Wrap(err, "unable to unmarshal excessBlobGas as hexutil.Uint64")
}
*e.ExcessBlobGas = hexutil.Uint64(uedg)
}

dgu, has := decoded["blobGasUsed"]
if has && dgu != nil {
e.Version = version.Deneb
udgu, err := hexutil.DecodeUint64(dgu.(string))
if err != nil {
return errors.Wrap(err, "blobGasUsed is not a string, can not decode")
}
*e.BlobGasUsed = hexutil.Uint64(udgu)
}
}

Expand Down
65 changes: 65 additions & 0 deletions proto/engine/v1/json_marshal_unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,71 @@ func TestJsonMarshalUnmarshal(t *testing.T) {
require.DeepEqual(t, bytesutil.PadTo([]byte("address2"), 20), payloadPb.Withdrawals[1].Address)
require.Equal(t, uint64(200), payloadPb.Withdrawals[1].Amount)
})
t.Run("execution block with deneb blob data", func(t *testing.T) {
baseFeePerGas := big.NewInt(1770307273)
blobGas := uint64(3000)
excessGas := uint64(7000)
want := &gethtypes.Header{
Number: big.NewInt(1),
ParentHash: common.BytesToHash([]byte("parent")),
UncleHash: common.BytesToHash([]byte("uncle")),
Coinbase: common.BytesToAddress([]byte("coinbase")),
Root: common.BytesToHash([]byte("uncle")),
TxHash: common.BytesToHash([]byte("txHash")),
ReceiptHash: common.BytesToHash([]byte("receiptHash")),
Bloom: gethtypes.BytesToBloom([]byte("bloom")),
Difficulty: big.NewInt(2),
GasLimit: 3,
GasUsed: 4,
Time: 5,
BaseFee: baseFeePerGas,
Extra: []byte("extraData"),
MixDigest: common.BytesToHash([]byte("mix")),
Nonce: gethtypes.EncodeNonce(6),
BlobGasUsed: &blobGas,
ExcessBlobGas: &excessGas,
}
enc, err := json.Marshal(want)
require.NoError(t, err)

payloadItems := make(map[string]interface{})
require.NoError(t, json.Unmarshal(enc, &payloadItems))

blockHash := want.Hash()
payloadItems["hash"] = blockHash.String()
payloadItems["totalDifficulty"] = "0x393a2e53de197c"
payloadItems["transactions"] = []string{"0xd57870623ea84ac3e2ffafbee9417fd1263b825b1107b8d606c25460dabeb693"}

encodedPayloadItems, err := json.Marshal(payloadItems)
require.NoError(t, err)

payloadPb := &enginev1.ExecutionBlock{}
require.NoError(t, json.Unmarshal(encodedPayloadItems, payloadPb))

require.DeepEqual(t, blockHash, payloadPb.Hash)
require.DeepEqual(t, want.Number, payloadPb.Number)
require.DeepEqual(t, want.ParentHash, payloadPb.ParentHash)
require.DeepEqual(t, want.UncleHash, payloadPb.UncleHash)
require.DeepEqual(t, want.Coinbase, payloadPb.Coinbase)
require.DeepEqual(t, want.Root, payloadPb.Root)
require.DeepEqual(t, want.TxHash, payloadPb.TxHash)
require.DeepEqual(t, want.ReceiptHash, payloadPb.ReceiptHash)
require.DeepEqual(t, want.Bloom, payloadPb.Bloom)
require.DeepEqual(t, want.Difficulty, payloadPb.Difficulty)
require.DeepEqual(t, payloadItems["totalDifficulty"], payloadPb.TotalDifficulty)
require.DeepEqual(t, want.GasUsed, payloadPb.GasUsed)
require.DeepEqual(t, want.GasLimit, payloadPb.GasLimit)
require.DeepEqual(t, want.Time, payloadPb.Time)
require.DeepEqual(t, want.BaseFee, payloadPb.BaseFee)
require.DeepEqual(t, want.Extra, payloadPb.Extra)
require.DeepEqual(t, want.MixDigest, payloadPb.MixDigest)
require.DeepEqual(t, want.Nonce, payloadPb.Nonce)
require.DeepEqual(t, want.BlobGasUsed, payloadPb.BlobGasUsed)
require.DeepEqual(t, want.ExcessBlobGas, payloadPb.ExcessBlobGas)

// Expect no transaction objects in the unmarshaled data.
require.Equal(t, 0, len(payloadPb.Transactions))
})
}

func TestPayloadIDBytes_MarshalUnmarshalJSON(t *testing.T) {
Expand Down

0 comments on commit d506f9b

Please sign in to comment.