2022-02-02 03:30:35 +00:00
|
|
|
package enginev1_test
|
2022-02-01 01:47:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2022-02-09 20:54:56 +00:00
|
|
|
"math/big"
|
2022-02-01 01:47:34 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-07-06 22:06:00 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
gethtypes "github.com/ethereum/go-ethereum/core/types"
|
2022-02-12 01:58:09 +00:00
|
|
|
fieldparams "github.com/prysmaticlabs/prysm/config/fieldparams"
|
2022-02-16 00:56:23 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/config/params"
|
2022-02-12 01:58:09 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/encoding/bytesutil"
|
2022-02-02 03:30:35 +00:00
|
|
|
enginev1 "github.com/prysmaticlabs/prysm/proto/engine/v1"
|
2022-02-01 01:47:34 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/testing/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestJsonMarshalUnmarshal(t *testing.T) {
|
|
|
|
t.Run("payload attributes", func(t *testing.T) {
|
2022-02-12 01:58:09 +00:00
|
|
|
random := bytesutil.PadTo([]byte("random"), fieldparams.RootLength)
|
|
|
|
feeRecipient := bytesutil.PadTo([]byte("feeRecipient"), fieldparams.FeeRecipientLength)
|
2022-02-07 17:13:01 +00:00
|
|
|
jsonPayload := &enginev1.PayloadAttributes{
|
|
|
|
Timestamp: 1,
|
2022-02-26 03:47:16 +00:00
|
|
|
PrevRandao: random,
|
2022-02-12 01:58:09 +00:00
|
|
|
SuggestedFeeRecipient: feeRecipient,
|
2022-02-01 01:47:34 +00:00
|
|
|
}
|
|
|
|
enc, err := json.Marshal(jsonPayload)
|
|
|
|
require.NoError(t, err)
|
2022-02-02 03:30:35 +00:00
|
|
|
payloadPb := &enginev1.PayloadAttributes{}
|
2022-02-01 01:47:34 +00:00
|
|
|
require.NoError(t, json.Unmarshal(enc, payloadPb))
|
|
|
|
require.DeepEqual(t, uint64(1), payloadPb.Timestamp)
|
2022-02-26 03:47:16 +00:00
|
|
|
require.DeepEqual(t, random, payloadPb.PrevRandao)
|
2022-02-12 01:58:09 +00:00
|
|
|
require.DeepEqual(t, feeRecipient, payloadPb.SuggestedFeeRecipient)
|
2022-02-01 01:47:34 +00:00
|
|
|
})
|
|
|
|
t.Run("payload status", func(t *testing.T) {
|
2022-02-12 01:58:09 +00:00
|
|
|
hash := bytesutil.PadTo([]byte("hash"), fieldparams.RootLength)
|
2022-02-07 17:13:01 +00:00
|
|
|
jsonPayload := &enginev1.PayloadStatus{
|
|
|
|
Status: enginev1.PayloadStatus_INVALID,
|
2022-02-12 01:58:09 +00:00
|
|
|
LatestValidHash: hash,
|
2022-02-07 17:13:01 +00:00
|
|
|
ValidationError: "failed validation",
|
2022-02-01 01:47:34 +00:00
|
|
|
}
|
|
|
|
enc, err := json.Marshal(jsonPayload)
|
|
|
|
require.NoError(t, err)
|
2022-02-02 03:30:35 +00:00
|
|
|
payloadPb := &enginev1.PayloadStatus{}
|
2022-02-01 01:47:34 +00:00
|
|
|
require.NoError(t, json.Unmarshal(enc, payloadPb))
|
|
|
|
require.DeepEqual(t, "INVALID", payloadPb.Status.String())
|
2022-02-12 01:58:09 +00:00
|
|
|
require.DeepEqual(t, hash, payloadPb.LatestValidHash)
|
2022-02-01 01:47:34 +00:00
|
|
|
require.DeepEqual(t, "failed validation", payloadPb.ValidationError)
|
|
|
|
})
|
|
|
|
t.Run("forkchoice state", func(t *testing.T) {
|
2022-02-12 01:58:09 +00:00
|
|
|
head := bytesutil.PadTo([]byte("head"), fieldparams.RootLength)
|
|
|
|
safe := bytesutil.PadTo([]byte("safe"), fieldparams.RootLength)
|
|
|
|
finalized := bytesutil.PadTo([]byte("finalized"), fieldparams.RootLength)
|
2022-02-07 17:13:01 +00:00
|
|
|
jsonPayload := &enginev1.ForkchoiceState{
|
2022-02-12 01:58:09 +00:00
|
|
|
HeadBlockHash: head,
|
|
|
|
SafeBlockHash: safe,
|
|
|
|
FinalizedBlockHash: finalized,
|
2022-02-01 01:47:34 +00:00
|
|
|
}
|
|
|
|
enc, err := json.Marshal(jsonPayload)
|
|
|
|
require.NoError(t, err)
|
2022-02-02 03:30:35 +00:00
|
|
|
payloadPb := &enginev1.ForkchoiceState{}
|
2022-02-01 01:47:34 +00:00
|
|
|
require.NoError(t, json.Unmarshal(enc, payloadPb))
|
2022-02-12 01:58:09 +00:00
|
|
|
require.DeepEqual(t, head, payloadPb.HeadBlockHash)
|
|
|
|
require.DeepEqual(t, safe, payloadPb.SafeBlockHash)
|
|
|
|
require.DeepEqual(t, finalized, payloadPb.FinalizedBlockHash)
|
2022-02-01 01:47:34 +00:00
|
|
|
})
|
2022-02-16 00:56:23 +00:00
|
|
|
t.Run("transition configuration", func(t *testing.T) {
|
|
|
|
blockHash := []byte("head")
|
|
|
|
jsonPayload := &enginev1.TransitionConfiguration{
|
|
|
|
TerminalBlockHash: blockHash,
|
|
|
|
TerminalTotalDifficulty: params.BeaconConfig().TerminalTotalDifficulty,
|
|
|
|
TerminalBlockNumber: big.NewInt(0).Bytes(),
|
|
|
|
}
|
|
|
|
enc, err := json.Marshal(jsonPayload)
|
|
|
|
require.NoError(t, err)
|
|
|
|
payloadPb := &enginev1.TransitionConfiguration{}
|
|
|
|
require.NoError(t, json.Unmarshal(enc, payloadPb))
|
|
|
|
require.DeepEqual(t, blockHash, payloadPb.TerminalBlockHash)
|
|
|
|
|
|
|
|
require.DeepEqual(t, params.BeaconConfig().TerminalTotalDifficulty, payloadPb.TerminalTotalDifficulty)
|
|
|
|
require.DeepEqual(t, big.NewInt(0).Bytes(), payloadPb.TerminalBlockNumber)
|
|
|
|
})
|
2022-02-01 01:47:34 +00:00
|
|
|
t.Run("execution payload", func(t *testing.T) {
|
2022-03-16 22:12:08 +00:00
|
|
|
baseFeePerGas := big.NewInt(1770307273)
|
2022-02-12 01:58:09 +00:00
|
|
|
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)
|
2022-02-07 17:13:01 +00:00
|
|
|
jsonPayload := &enginev1.ExecutionPayload{
|
2022-02-12 01:58:09 +00:00
|
|
|
ParentHash: parentHash,
|
|
|
|
FeeRecipient: feeRecipient,
|
|
|
|
StateRoot: stateRoot,
|
|
|
|
ReceiptsRoot: receiptsRoot,
|
|
|
|
LogsBloom: logsBloom,
|
2022-02-26 03:47:16 +00:00
|
|
|
PrevRandao: random,
|
2022-02-07 17:13:01 +00:00
|
|
|
BlockNumber: 1,
|
|
|
|
GasLimit: 2,
|
|
|
|
GasUsed: 3,
|
|
|
|
Timestamp: 4,
|
2022-02-12 01:58:09 +00:00
|
|
|
ExtraData: extra,
|
2022-02-21 14:03:12 +00:00
|
|
|
BaseFeePerGas: baseFeePerGas.Bytes(),
|
2022-02-12 01:58:09 +00:00
|
|
|
BlockHash: hash,
|
2022-02-07 17:13:01 +00:00
|
|
|
Transactions: [][]byte{[]byte("hi")},
|
2022-02-01 01:47:34 +00:00
|
|
|
}
|
|
|
|
enc, err := json.Marshal(jsonPayload)
|
|
|
|
require.NoError(t, err)
|
2022-02-02 03:30:35 +00:00
|
|
|
payloadPb := &enginev1.ExecutionPayload{}
|
2022-02-01 01:47:34 +00:00
|
|
|
require.NoError(t, json.Unmarshal(enc, payloadPb))
|
2022-02-12 01:58:09 +00:00
|
|
|
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)
|
2022-02-26 03:47:16 +00:00
|
|
|
require.DeepEqual(t, random, payloadPb.PrevRandao)
|
2022-02-01 01:47:34 +00:00
|
|
|
require.DeepEqual(t, uint64(1), payloadPb.BlockNumber)
|
2022-02-07 17:13:01 +00:00
|
|
|
require.DeepEqual(t, uint64(2), payloadPb.GasLimit)
|
|
|
|
require.DeepEqual(t, uint64(3), payloadPb.GasUsed)
|
|
|
|
require.DeepEqual(t, uint64(4), payloadPb.Timestamp)
|
2022-02-12 01:58:09 +00:00
|
|
|
require.DeepEqual(t, extra, payloadPb.ExtraData)
|
|
|
|
require.DeepEqual(t, bytesutil.PadTo(baseFeePerGas.Bytes(), fieldparams.RootLength), payloadPb.BaseFeePerGas)
|
|
|
|
require.DeepEqual(t, hash, payloadPb.BlockHash)
|
2022-02-07 17:13:01 +00:00
|
|
|
require.DeepEqual(t, [][]byte{[]byte("hi")}, payloadPb.Transactions)
|
2022-02-01 01:47:34 +00:00
|
|
|
})
|
2022-02-08 18:06:02 +00:00
|
|
|
t.Run("execution block", func(t *testing.T) {
|
2022-03-30 17:17:18 +00:00
|
|
|
baseFeePerGas := big.NewInt(1770307273)
|
2022-07-06 22:06:00 +00:00
|
|
|
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),
|
2022-02-08 18:06:02 +00:00
|
|
|
}
|
2022-07-06 22:06:00 +00:00
|
|
|
enc, err := json.Marshal(want)
|
2022-02-08 18:06:02 +00:00
|
|
|
require.NoError(t, err)
|
2022-07-06 22:06:00 +00:00
|
|
|
|
|
|
|
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)
|
2022-04-14 02:47:04 +00:00
|
|
|
require.NoError(t, err)
|
2022-07-06 22:06:00 +00:00
|
|
|
|
2022-04-14 02:47:04 +00:00
|
|
|
payloadPb := &enginev1.ExecutionBlock{}
|
2022-07-06 22:06:00 +00:00
|
|
|
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)
|
2022-04-14 02:47:04 +00:00
|
|
|
})
|
2022-02-01 01:47:34 +00:00
|
|
|
}
|
2022-02-07 17:13:01 +00:00
|
|
|
|
2022-02-09 20:54:56 +00:00
|
|
|
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)
|
2022-02-07 17:13:01 +00:00
|
|
|
}
|