prysm-pulse/proto/engine/v1/json_marshal_unmarshal_test.go

664 lines
47 KiB
Go
Raw Normal View History

package enginev1_test
import (
"encoding/json"
"fmt"
"math/big"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
gethtypes "github.com/ethereum/go-ethereum/core/types"
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
enginev1 "github.com/prysmaticlabs/prysm/v5/proto/engine/v1"
"github.com/prysmaticlabs/prysm/v5/testing/require"
)
type withdrawalJSON struct {
Index *hexutil.Uint64 `json:"index"`
Validator *hexutil.Uint64 `json:"validatorIndex"`
Address *common.Address `json:"address"`
Amount *hexutil.Uint64 `json:"amount"`
}
func TestJsonMarshalUnmarshal(t *testing.T) {
t.Run("payload attributes", func(t *testing.T) {
random := bytesutil.PadTo([]byte("random"), fieldparams.RootLength)
feeRecipient := bytesutil.PadTo([]byte("feeRecipient"), fieldparams.FeeRecipientLength)
jsonPayload := &enginev1.PayloadAttributes{
Timestamp: 1,
PrevRandao: random,
SuggestedFeeRecipient: feeRecipient,
}
enc, err := json.Marshal(jsonPayload)
require.NoError(t, err)
payloadPb := &enginev1.PayloadAttributes{}
require.NoError(t, json.Unmarshal(enc, payloadPb))
require.DeepEqual(t, uint64(1), payloadPb.Timestamp)
require.DeepEqual(t, random, payloadPb.PrevRandao)
require.DeepEqual(t, feeRecipient, payloadPb.SuggestedFeeRecipient)
})
t.Run("payload status", func(t *testing.T) {
hash := bytesutil.PadTo([]byte("hash"), fieldparams.RootLength)
jsonPayload := &enginev1.PayloadStatus{
Status: enginev1.PayloadStatus_INVALID,
LatestValidHash: hash,
ValidationError: "failed validation",
}
enc, err := json.Marshal(jsonPayload)
require.NoError(t, err)
payloadPb := &enginev1.PayloadStatus{}
require.NoError(t, json.Unmarshal(enc, payloadPb))
require.DeepEqual(t, "INVALID", payloadPb.Status.String())
require.DeepEqual(t, hash, payloadPb.LatestValidHash)
require.DeepEqual(t, "failed validation", payloadPb.ValidationError)
})
t.Run("forkchoice state", func(t *testing.T) {
head := bytesutil.PadTo([]byte("head"), fieldparams.RootLength)
safe := bytesutil.PadTo([]byte("safe"), fieldparams.RootLength)
finalized := bytesutil.PadTo([]byte("finalized"), fieldparams.RootLength)
jsonPayload := &enginev1.ForkchoiceState{
HeadBlockHash: head,
SafeBlockHash: safe,
FinalizedBlockHash: finalized,
}
enc, err := json.Marshal(jsonPayload)
require.NoError(t, err)
payloadPb := &enginev1.ForkchoiceState{}
require.NoError(t, json.Unmarshal(enc, payloadPb))
require.DeepEqual(t, head, payloadPb.HeadBlockHash)
require.DeepEqual(t, safe, payloadPb.SafeBlockHash)
require.DeepEqual(t, finalized, payloadPb.FinalizedBlockHash)
})
t.Run("execution payload", func(t *testing.T) {
baseFeePerGas := big.NewInt(1770307273)
parentHash := bytesutil.PadTo([]byte("parent"), fieldparams.RootLength)
feeRecipient := bytesutil.PadTo([]byte("feeRecipient"), fieldparams.FeeRecipientLength)
stateRoot := bytesutil.PadTo([]byte("stateRoot"), fieldparams.RootLength)
receiptsRoot := bytesutil.PadTo([]byte("receiptsRoot"), fieldparams.RootLength)
logsBloom := bytesutil.PadTo([]byte("logs"), fieldparams.LogsBloomLength)
random := bytesutil.PadTo([]byte("random"), fieldparams.RootLength)
extra := bytesutil.PadTo([]byte("extraData"), fieldparams.RootLength)
hash := bytesutil.PadTo([]byte("hash"), fieldparams.RootLength)
jsonPayload := &enginev1.ExecutionPayload{
ParentHash: parentHash,
FeeRecipient: feeRecipient,
StateRoot: stateRoot,
ReceiptsRoot: receiptsRoot,
LogsBloom: logsBloom,
PrevRandao: random,
BlockNumber: 1,
GasLimit: 2,
GasUsed: 3,
Timestamp: 4,
ExtraData: extra,
BaseFeePerGas: baseFeePerGas.Bytes(),
BlockHash: hash,
Transactions: [][]byte{[]byte("hi")},
}
enc, err := json.Marshal(jsonPayload)
require.NoError(t, err)
payloadPb := &enginev1.ExecutionPayload{}
require.NoError(t, json.Unmarshal(enc, payloadPb))
require.DeepEqual(t, parentHash, payloadPb.ParentHash)
require.DeepEqual(t, feeRecipient, payloadPb.FeeRecipient)
require.DeepEqual(t, stateRoot, payloadPb.StateRoot)
require.DeepEqual(t, receiptsRoot, payloadPb.ReceiptsRoot)
require.DeepEqual(t, logsBloom, payloadPb.LogsBloom)
require.DeepEqual(t, random, payloadPb.PrevRandao)
require.DeepEqual(t, uint64(1), payloadPb.BlockNumber)
require.DeepEqual(t, uint64(2), payloadPb.GasLimit)
require.DeepEqual(t, uint64(3), payloadPb.GasUsed)
require.DeepEqual(t, uint64(4), payloadPb.Timestamp)
require.DeepEqual(t, extra, payloadPb.ExtraData)
require.DeepEqual(t, bytesutil.PadTo(baseFeePerGas.Bytes(), fieldparams.RootLength), payloadPb.BaseFeePerGas)
require.DeepEqual(t, hash, payloadPb.BlockHash)
require.DeepEqual(t, [][]byte{[]byte("hi")}, payloadPb.Transactions)
})
t.Run("execution payload Capella", func(t *testing.T) {
parentHash := common.BytesToHash([]byte("parent"))
feeRecipient := common.BytesToAddress([]byte("feeRecipient"))
stateRoot := common.BytesToHash([]byte("stateRoot"))
receiptsRoot := common.BytesToHash([]byte("receiptsRoot"))
logsBloom := hexutil.Bytes(bytesutil.PadTo([]byte("logs"), fieldparams.LogsBloomLength))
random := common.BytesToHash([]byte("random"))
extra := common.BytesToHash([]byte("extra"))
hash := common.BytesToHash([]byte("hash"))
bn := hexutil.Uint64(1)
gl := hexutil.Uint64(2)
gu := hexutil.Uint64(3)
ts := hexutil.Uint64(4)
resp := &enginev1.GetPayloadV2ResponseJson{
BlockValue: fmt.Sprint("0x123"),
ExecutionPayload: &enginev1.ExecutionPayloadCapellaJSON{
ParentHash: &parentHash,
FeeRecipient: &feeRecipient,
StateRoot: &stateRoot,
ReceiptsRoot: &receiptsRoot,
LogsBloom: &logsBloom,
PrevRandao: &random,
BlockNumber: &bn,
GasLimit: &gl,
GasUsed: &gu,
Timestamp: &ts,
ExtraData: hexutil.Bytes(extra[:]),
BaseFeePerGas: "0x123",
BlockHash: &hash,
Transactions: []hexutil.Bytes{{}},
Withdrawals: []*enginev1.Withdrawal{{
Index: 1,
ValidatorIndex: 1,
Address: bytesutil.PadTo([]byte("address"), 20),
Amount: 1,
}},
},
}
enc, err := json.Marshal(resp)
require.NoError(t, err)
pb := &enginev1.ExecutionPayloadCapellaWithValue{}
require.NoError(t, json.Unmarshal(enc, pb))
require.DeepEqual(t, parentHash.Bytes(), pb.Payload.ParentHash)
require.DeepEqual(t, feeRecipient.Bytes(), pb.Payload.FeeRecipient)
require.DeepEqual(t, stateRoot.Bytes(), pb.Payload.StateRoot)
require.DeepEqual(t, receiptsRoot.Bytes(), pb.Payload.ReceiptsRoot)
require.DeepEqual(t, logsBloom, hexutil.Bytes(pb.Payload.LogsBloom))
require.DeepEqual(t, random.Bytes(), pb.Payload.PrevRandao)
require.DeepEqual(t, uint64(1), pb.Payload.BlockNumber)
require.DeepEqual(t, uint64(2), pb.Payload.GasLimit)
require.DeepEqual(t, uint64(3), pb.Payload.GasUsed)
require.DeepEqual(t, uint64(4), pb.Payload.Timestamp)
require.DeepEqual(t, extra.Bytes(), pb.Payload.ExtraData)
feePerGas := new(big.Int).SetBytes(pb.Payload.BaseFeePerGas)
require.Equal(t, "15832716547479101977395928904157292820330083199902421483727713169783165812736", feePerGas.String())
require.DeepEqual(t, hash.Bytes(), pb.Payload.BlockHash)
require.DeepEqual(t, [][]byte{{}}, pb.Payload.Transactions)
require.Equal(t, 1, len(pb.Payload.Withdrawals))
withdrawal := pb.Payload.Withdrawals[0]
require.Equal(t, uint64(1), withdrawal.Index)
require.Equal(t, primitives.ValidatorIndex(1), withdrawal.ValidatorIndex)
require.DeepEqual(t, bytesutil.PadTo([]byte("address"), 20), withdrawal.Address)
require.Equal(t, uint64(1), withdrawal.Amount)
})
t.Run("execution payload deneb", func(t *testing.T) {
parentHash := common.BytesToHash([]byte("parent"))
feeRecipient := common.BytesToAddress([]byte("feeRecipient"))
stateRoot := common.BytesToHash([]byte("stateRoot"))
receiptsRoot := common.BytesToHash([]byte("receiptsRoot"))
logsBloom := hexutil.Bytes(bytesutil.PadTo([]byte("logs"), fieldparams.LogsBloomLength))
random := common.BytesToHash([]byte("random"))
extra := common.BytesToHash([]byte("extra"))
hash := common.BytesToHash([]byte("hash"))
bn := hexutil.Uint64(1)
gl := hexutil.Uint64(2)
gu := hexutil.Uint64(3)
ts := hexutil.Uint64(4)
bgu := hexutil.Uint64(5)
ebg := hexutil.Uint64(6)
resp := &enginev1.GetPayloadV3ResponseJson{
BlobsBundle: &enginev1.BlobBundleJSON{
Commitments: []hexutil.Bytes{{'a'}, {'b'}, {'c'}, {'d'}},
Proofs: []hexutil.Bytes{{'e'}, {'f'}, {'g'}, {'h'}},
Blobs: []hexutil.Bytes{{'i'}, {'j'}, {'k'}, {'l'}},
},
BlockValue: fmt.Sprint("0x123"),
ExecutionPayload: &enginev1.ExecutionPayloadDenebJSON{
ParentHash: &parentHash,
FeeRecipient: &feeRecipient,
StateRoot: &stateRoot,
ReceiptsRoot: &receiptsRoot,
LogsBloom: &logsBloom,
PrevRandao: &random,
BlockNumber: &bn,
GasLimit: &gl,
GasUsed: &gu,
Timestamp: &ts,
ExtraData: hexutil.Bytes(extra[:]),
BaseFeePerGas: "0x123",
BlockHash: &hash,
Transactions: []hexutil.Bytes{{}},
Withdrawals: []*enginev1.Withdrawal{{
Index: 1,
ValidatorIndex: 1,
Address: bytesutil.PadTo([]byte("address"), 20),
Amount: 1,
}},
BlobGasUsed: &bgu,
ExcessBlobGas: &ebg,
},
}
enc, err := json.Marshal(resp)
require.NoError(t, err)
pb := &enginev1.ExecutionPayloadDenebWithValueAndBlobsBundle{}
require.NoError(t, json.Unmarshal(enc, pb))
require.DeepEqual(t, parentHash.Bytes(), pb.Payload.ParentHash)
require.DeepEqual(t, feeRecipient.Bytes(), pb.Payload.FeeRecipient)
require.DeepEqual(t, stateRoot.Bytes(), pb.Payload.StateRoot)
require.DeepEqual(t, receiptsRoot.Bytes(), pb.Payload.ReceiptsRoot)
require.DeepEqual(t, logsBloom, hexutil.Bytes(pb.Payload.LogsBloom))
require.DeepEqual(t, random.Bytes(), pb.Payload.PrevRandao)
require.DeepEqual(t, uint64(1), pb.Payload.BlockNumber)
require.DeepEqual(t, uint64(2), pb.Payload.GasLimit)
require.DeepEqual(t, uint64(3), pb.Payload.GasUsed)
require.DeepEqual(t, uint64(4), pb.Payload.Timestamp)
require.DeepEqual(t, uint64(5), pb.Payload.BlobGasUsed)
require.DeepEqual(t, uint64(6), pb.Payload.ExcessBlobGas)
require.DeepEqual(t, extra.Bytes(), pb.Payload.ExtraData)
feePerGas := new(big.Int).SetBytes(pb.Payload.BaseFeePerGas)
require.Equal(t, "15832716547479101977395928904157292820330083199902421483727713169783165812736", feePerGas.String())
require.DeepEqual(t, hash.Bytes(), pb.Payload.BlockHash)
require.DeepEqual(t, [][]byte{{}}, pb.Payload.Transactions)
require.Equal(t, 1, len(pb.Payload.Withdrawals))
withdrawal := pb.Payload.Withdrawals[0]
require.Equal(t, uint64(1), withdrawal.Index)
require.Equal(t, primitives.ValidatorIndex(1), withdrawal.ValidatorIndex)
require.DeepEqual(t, bytesutil.PadTo([]byte("address"), 20), withdrawal.Address)
require.Equal(t, uint64(1), withdrawal.Amount)
require.DeepEqual(t, [][]byte{
bytesutil.PadTo([]byte{'e'}, 48),
bytesutil.PadTo([]byte{'f'}, 48),
bytesutil.PadTo([]byte{'g'}, 48),
bytesutil.PadTo([]byte{'h'}, 48)}, pb.BlobsBundle.Proofs)
require.DeepEqual(t, [][]byte{
bytesutil.PadTo([]byte{'a'}, 48),
bytesutil.PadTo([]byte{'b'}, 48),
bytesutil.PadTo([]byte{'c'}, 48),
bytesutil.PadTo([]byte{'d'}, 48)}, pb.BlobsBundle.KzgCommitments)
require.DeepEqual(t, [][]byte{
bytesutil.PadTo([]byte{'i'}, 131072),
bytesutil.PadTo([]byte{'j'}, 131072),
bytesutil.PadTo([]byte{'k'}, 131072),
bytesutil.PadTo([]byte{'l'}, 131072)}, pb.BlobsBundle.Blobs)
})
t.Run("execution block", func(t *testing.T) {
baseFeePerGas := big.NewInt(1770307273)
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),
}
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"
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)
})
t.Run("execution block with txs as hashes", func(t *testing.T) {
baseFeePerGas := big.NewInt(1770307273)
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),
}
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)
// Expect no transaction objects in the unmarshaled data.
require.Equal(t, 0, len(payloadPb.Transactions))
})
t.Run("execution block with full transaction data", func(t *testing.T) {
baseFeePerGas := big.NewInt(1770307273)
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),
}
enc, err := json.Marshal(want)
require.NoError(t, err)
payloadItems := make(map[string]interface{})
require.NoError(t, json.Unmarshal(enc, &payloadItems))
tx := gethtypes.NewTransaction(
1,
common.BytesToAddress([]byte("hi")),
big.NewInt(0),
21000,
big.NewInt(1e6),
[]byte{},
)
txs := []*gethtypes.Transaction{tx}
blockHash := want.Hash()
payloadItems["hash"] = blockHash.String()
payloadItems["totalDifficulty"] = "0x393a2e53de197c"
payloadItems["transactions"] = txs
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.Equal(t, 1, len(payloadPb.Transactions))
require.DeepEqual(t, txs[0].Hash(), payloadPb.Transactions[0].Hash())
})
t.Run("execution block with withdrawals", func(t *testing.T) {
baseFeePerGas := big.NewInt(1770307273)
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),
}
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"
withdrawalIndex1 := hexutil.Uint64(1)
withdrawalIndex2 := hexutil.Uint64(2)
withdrawalAmount1 := hexutil.Uint64(100)
withdrawalAmount2 := hexutil.Uint64(200)
withdrawalValidator1 := hexutil.Uint64(1)
withdrawalValidator2 := hexutil.Uint64(2)
address1 := common.Address(bytesutil.ToBytes20([]byte("address1")))
address2 := common.Address(bytesutil.ToBytes20([]byte("address2")))
payloadItems["withdrawals"] = []*withdrawalJSON{
{
Index: &withdrawalIndex1,
Validator: &withdrawalValidator1,
Address: &address1,
Amount: &withdrawalAmount1,
},
{
Index: &withdrawalIndex2,
Validator: &withdrawalValidator2,
Address: &address2,
Amount: &withdrawalAmount2,
},
}
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.Equal(t, 2, len(payloadPb.Withdrawals))
2022-12-15 23:42:07 +00:00
require.Equal(t, uint64(1), payloadPb.Withdrawals[0].Index)
require.Equal(t, primitives.ValidatorIndex(1), payloadPb.Withdrawals[0].ValidatorIndex)
2022-12-15 23:42:07 +00:00
require.DeepEqual(t, bytesutil.PadTo([]byte("address1"), 20), payloadPb.Withdrawals[0].Address)
require.Equal(t, uint64(100), payloadPb.Withdrawals[0].Amount)
2022-12-15 23:42:07 +00:00
require.Equal(t, uint64(2), payloadPb.Withdrawals[1].Index)
require.Equal(t, primitives.ValidatorIndex(2), payloadPb.Withdrawals[1].ValidatorIndex)
2022-12-15 23:42:07 +00:00
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) {
item := [8]byte{1, 0, 0, 0, 0, 0, 0, 0}
enc, err := json.Marshal(enginev1.PayloadIDBytes(item))
require.NoError(t, err)
require.DeepEqual(t, "\"0x0100000000000000\"", string(enc))
res := &enginev1.PayloadIDBytes{}
err = res.UnmarshalJSON(enc)
require.NoError(t, err)
require.Equal(t, true, item == *res)
}
func TestExecutionPayloadBody_MarshalUnmarshalJSON(t *testing.T) {
pBody := &enginev1.ExecutionPayloadBodyV1{
Transactions: [][]byte{[]byte("random1"), []byte("random2"), []byte("random3")},
Withdrawals: []*enginev1.Withdrawal{
{
Index: 200,
ValidatorIndex: 20303,
Amount: 3200000000,
Address: bytesutil.PadTo([]byte("junk"), 20),
},
{
Index: 200,
ValidatorIndex: 70303,
Amount: 3200000800,
Address: bytesutil.PadTo([]byte("junk2"), 20),
},
},
}
enc, err := json.Marshal(pBody)
require.NoError(t, err)
res := &enginev1.ExecutionPayloadBodyV1{}
err = res.UnmarshalJSON(enc)
require.NoError(t, err)
require.DeepEqual(t, pBody, res)
}
func TestExecutionBlock_MarshalUnmarshalJSON_MainnetBlock(t *testing.T) {
newBlock := &enginev1.ExecutionBlock{}
require.NoError(t, newBlock.UnmarshalJSON([]byte(blockJson)))
_, err := newBlock.MarshalJSON()
require.NoError(t, err)
newBlock = &enginev1.ExecutionBlock{}
require.NoError(t, newBlock.UnmarshalJSON([]byte(blockNoTxJson)))
_, err = newBlock.MarshalJSON()
require.NoError(t, err)
}
var blockJson = `{"baseFeePerGas":"0x42110b4f7","difficulty":"0x280ae66012087c","extraData":"0xe4b883e5bda9e7a59ee4bb99e9b1bc4b3021","gasLimit":"0x1c9c380","gasUsed":"0xf829e","hash":"0xf5bda634715a9d8af2693b600a725a0db285f0267f25b7f60f5b9c502691aef8","logsBloom":"0x002000000010100110000000800008200000000000000000000020001000200000040104000000000000101000000100820080800800080000a008000a01200000000000000001202042000c000000200841000000002001200004008000102002000000000200000000010440000042000000000000080000000010001000002000020000020000000000000000000002000001000010080020004008100000880001080000400000004080060200000800010000040002204000000000020000000002000000000000000001000008000000400000001002010804000000000020a40800000000070000000401080000000000000880400000000000001000","miner":"0x829bd824b016326a401d083b33d092293333a830","mixHash":"0xc1bcfb6dc83cdc106faad9870ab697dd6c7a5a05ca00b3a5f3c2e021b22e0747","nonce":"0xf09ffce459ff4a07","number":"0xe6f8db","parentHash":"0x5749469a59b1207d4b6d42dd9e31c059aa1586fe070573bf6e5442a626726959","receiptsRoot":"0x3b131e70a5d2e013c5946d6bf0290732ad1d195b05abd72bc0bfb7ed4be202b0","sha3Uncles":"0x4df8516d92fd18ca040f0af06d31afaa3a62dbc6ec7ec758336c81b719782a07","size":"0x18ad","stateRoot":"0xdff0d06049e5a7d5b4249eb2aa4b7c626f7a957733913786912441b89d20a3e1","timestamp":"0x62cf48c6","totalDifficulty":"0xb6c08f1eb97fd70fc5f","transactions":[{"blockHash":"0xf5bda634715a9d8af2693b600a725a0db285f0267f25b7f60f5b9c502691aef8","blockNumber":"0xe6f8db","from":"0x10121cb2b3f64f0a6231178336aca3e3b87d5ca5","gas":"0x222e0","gasPrice":"0x6be56a00f","hash":"0x7d503dbb3661532e9bf51a23eeb284bb0d3a1cb99212108ceae70730a2617d7c","input":"0xb31c01fb66054fe7e80881e2dfed6bdd67d09c6a50461013b2ff4b3e9684f57fb58a9f07543c63a826a769aad2d6e3bfacdda2a930f25782caeeb3b6a66c7e6cc5a4811c000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000419bb97c858f8c9d2ca3cf28f0236e15fa68a74c4263c28baecd00f603690dbf1c17bf2f4ad0767dbb92118e479b7a716ed465ed27a5b7decbcf9ba5cc1e911ae41b00000000000000000000000000000000000000000000000000000000000000","nonce":"0xc5f","to":"0x049b51e531fd8f90da6d92ea83dc4125002f20ef","transactionIndex":"0x0","value":"0x0","type":"0x0","v":"0x25","r":"0x8cb6e54a332bce463b2184ff252c35d400b5548fb5d5e1a711bf64d6bec5cd55","s":"0x42d5c57f90f5394814b10f1046e4188eebb72aff7f9c8e838b28159b9c47985f"},{"blockHash":"0xf5bda634715a9d8af2693b600a725a0db285f0267f25b7f60f5b9c502691aef8","blockNumber":"0xe6f8db","from":"0xc8231eb0f6be12cca4e8de38fbd36382f827b615","gas":"0x33f9d","gasPrice":"0x4b613adf7","maxFeePerGas":"0x8dffb706a","maxPriorityFeePerGas":"0x9502f900","hash":"0x3a3d2c7624c0029d4865ca8e92ff737d971bcee393a22f4e231a801774ae5cda","input":"0xfb0f3ee100000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000393bf5ab54e000000000000000000000000000e476199b37e70258d144a53d9522747c9d9cc82b000000000000000000000000004c00500000ad104d7dbd00e3ae0a5c00560c00000000000000000000000000dcaf23e44639daf29f6532da213999d737f15aa40000000000000000000000000000000000000000000000000000000000000937000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000062cf47430000000000000000000000000000000000000000000000000000000062f81edd00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000120bbba61bdc2df0000007b02230091a7ed01230072f7006a004d60a8d4e71d599b8104250f00000000007b02230091a7ed01230072f7006a004d60a8d4e71d599b8104250f00000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002e0000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000
var blockNoTxJson = `{"baseFeePerGas":"0x42110b4f7","difficulty":"0x280ae66012087c","extraData":"0xe4b883e5bda9e7a59ee4bb99e9b1bc4b3021","gasLimit":"0x1c9c380","gasUsed":"0xf829e","hash":"0xf5bda634715a9d8af2693b600a725a0db285f0267f25b7f60f5b9c502691aef8","logsBloom":"0x002000000010100110000000800008200000000000000000000020001000200000040104000000000000101000000100820080800800080000a008000a01200000000000000001202042000c000000200841000000002001200004008000102002000000000200000000010440000042000000000000080000000010001000002000020000020000000000000000000002000001000010080020004008100000880001080000400000004080060200000800010000040002204000000000020000000002000000000000000001000008000000400000001002010804000000000020a40800000000070000000401080000000000000880400000000000001000","miner":"0x829bd824b016326a401d083b33d092293333a830","mixHash":"0xc1bcfb6dc83cdc106faad9870ab697dd6c7a5a05ca00b3a5f3c2e021b22e0747","nonce":"0xf09ffce459ff4a07","number":"0xe6f8db","parentHash":"0x5749469a59b1207d4b6d42dd9e31c059aa1586fe070573bf6e5442a626726959","receiptsRoot":"0x3b131e70a5d2e013c5946d6bf0290732ad1d195b05abd72bc0bfb7ed4be202b0","sha3Uncles":"0x4df8516d92fd18ca040f0af06d31afaa3a62dbc6ec7ec758336c81b719782a07","size":"0x18ad","stateRoot":"0xdff0d06049e5a7d5b4249eb2aa4b7c626f7a957733913786912441b89d20a3e1","timestamp":"0x62cf48c6","totalDifficulty":"0xb6c08f1eb97fd70fc5f","transactions":["0x7d503dbb3661532e9bf51a23eeb284bb0d3a1cb99212108ceae70730a2617d7c","0x3a3d2c7624c0029d4865ca8e92ff737d971bcee393a22f4e231a801774ae5cda","0xe0bd91c32bc87146514a64f2cea7528a9d4e73d847a7ca03667a503cf52ba2cb","0x843f21fe25a934099f6f311665d1e211ff09d4dc8de02b589ddf6eac74d3dfcb","0xbf084d9e3a885bce9a27902aa394f572a1d3382eea003a19393aed9eb5a20be2","0x388fc716a00c94beae24f7e0b52aad43ac34060733890e9ea286273c7787a676","0xcf0e55b95af41c681d92a249a92f0aef8f023da25799efd7442b5c3ef6a52de6","0xa94eaf385588e9596a61851a1d25b0a0007c0e565ad4112bc7d0e91f83888cda","0xb360475e21e44e4d6b982387347c099ea8f2305773724db273128bbfdf82a1db","0xa95eba47cc617f16fa00735bd75cc245511e77c08efa8155ece7e59004265c2f","0xb7ca5adc1ba774c31d551d04aad1fb3c63729fdffe39d8cadf7305413df22f4c","0xa27ccc3bf5dca531769c79795dc74ffeb1161963eeeebaa7ef365303b47b697d","0x42bfe585b3c4974206570b01e01e904ad8e3be8f6ae021acf645116549ef56b3","0x03d033a7910eb2b5023ef9102805c06e30449b9926af32b47c6de3f5ccf45634"],"transactionsRoot":"0x46e27176677a4b37c1fa9bae97ffb48b86a316f9e6568b3320e10dd6954b5d1a","uncles":["0x0b15f885d283bb8044350ccb9b88fa42192926abb41302fefe0179051e4deadb"]}`