Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simulators/ethereum/engine: Increase tests coverage #700

Merged
merged 9 commits into from
Feb 6, 2023
2 changes: 1 addition & 1 deletion simulators/ethereum/engine/client/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type Engine interface {
GetPayloadV1(ctx context.Context, payloadId *api.PayloadID) (api.ExecutableData, error)
GetPayloadV2(ctx context.Context, payloadId *api.PayloadID) (api.ExecutableData, *big.Int, error)

NewPayloadV1(ctx context.Context, payload *api.ExecutableData) (api.PayloadStatusV1, error)
NewPayloadV1(ctx context.Context, payload *client_types.ExecutableDataV1) (api.PayloadStatusV1, error)
NewPayloadV2(ctx context.Context, payload *api.ExecutableData) (api.PayloadStatusV1, error)

GetPayloadBodiesByRangeV1(ctx context.Context, start uint64, count uint64) ([]*client_types.ExecutionPayloadBodyV1, error)
Expand Down
14 changes: 8 additions & 6 deletions simulators/ethereum/engine/client/hive_rpc/hive_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ func (ec *HiveRPCEngineClient) GetPayloadBodiesByRangeV1(ctx context.Context, st
return nil, err
}

err = ec.c.CallContext(ctx, &result, "engine_getPayloadBodiesByRangeV1", start, count)
err = ec.c.CallContext(ctx, &result, "engine_getPayloadBodiesByRangeV1", hexutil.Uint64(start), hexutil.Uint64(count))
return result, err
}

Expand All @@ -383,23 +383,25 @@ func (ec *HiveRPCEngineClient) GetPayloadBodiesByHashV1(ctx context.Context, has
return result, err
}

func (ec *HiveRPCEngineClient) NewPayload(ctx context.Context, version int, payload *api.ExecutableData) (api.PayloadStatusV1, error) {
func (ec *HiveRPCEngineClient) newPayload(ctx context.Context, version int, payload interface{}) (api.PayloadStatusV1, error) {
var result api.PayloadStatusV1
if err := ec.PrepareDefaultAuthCallToken(); err != nil {
return result, err
}
ec.latestPayloadSent = payload
err := ec.c.CallContext(ctx, &result, fmt.Sprintf("engine_newPayloadV%d", version), payload)
ec.latestPayloadStatusReponse = &result
return result, err
}

func (ec *HiveRPCEngineClient) NewPayloadV1(ctx context.Context, payload *api.ExecutableData) (api.PayloadStatusV1, error) {
return ec.NewPayload(ctx, 1, payload)
func (ec *HiveRPCEngineClient) NewPayloadV1(ctx context.Context, payload *client_types.ExecutableDataV1) (api.PayloadStatusV1, error) {
ed := payload.ToExecutableData()
ec.latestPayloadSent = &ed
return ec.newPayload(ctx, 1, payload)
}

func (ec *HiveRPCEngineClient) NewPayloadV2(ctx context.Context, payload *api.ExecutableData) (api.PayloadStatusV1, error) {
return ec.NewPayload(ctx, 2, payload)
ec.latestPayloadSent = payload
return ec.newPayload(ctx, 2, payload)
}

func (ec *HiveRPCEngineClient) ExchangeTransitionConfigurationV1(ctx context.Context, tConf *api.TransitionConfigurationV1) (api.TransitionConfigurationV1, error) {
Expand Down
7 changes: 4 additions & 3 deletions simulators/ethereum/engine/client/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -690,9 +690,10 @@ func (n *GethNode) SetBlock(block *types.Block, parentNumber uint64, parentRoot
}

// Engine API
func (n *GethNode) NewPayloadV1(ctx context.Context, pl *beacon.ExecutableData) (beacon.PayloadStatusV1, error) {
n.latestPayloadSent = pl
resp, err := n.api.NewPayloadV1(*pl)
func (n *GethNode) NewPayloadV1(ctx context.Context, pl *client_types.ExecutableDataV1) (beacon.PayloadStatusV1, error) {
ed := pl.ToExecutableData()
n.latestPayloadSent = &ed
resp, err := n.api.NewPayloadV1(ed)
n.latestPayloadStatusReponse = &resp
return resp, err
}
Expand Down
139 changes: 139 additions & 0 deletions simulators/ethereum/engine/client/types/gen_edv1.go

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

75 changes: 75 additions & 0 deletions simulators/ethereum/engine/client/types/types.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package types

import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/go-ethereum/core/types"
)

Expand All @@ -15,3 +19,74 @@ type ExecutionPayloadBodyV1 struct {
type executionPayloadBodyV1Marshaling struct {
Transactions []hexutil.Bytes
}

// ExecutableData is the data necessary to execute an EL payload.
//
//go:generate go run github.com/fjl/gencodec -type ExecutableDataV1 -field-override executableDataV1Marshaling -out gen_edv1.go
type ExecutableDataV1 struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
FeeRecipient common.Address `json:"feeRecipient" gencodec:"required"`
StateRoot common.Hash `json:"stateRoot" gencodec:"required"`
ReceiptsRoot common.Hash `json:"receiptsRoot" gencodec:"required"`
LogsBloom []byte `json:"logsBloom" gencodec:"required"`
Random common.Hash `json:"prevRandao" gencodec:"required"`
Number uint64 `json:"blockNumber" gencodec:"required"`
GasLimit uint64 `json:"gasLimit" gencodec:"required"`
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
Timestamp uint64 `json:"timestamp" gencodec:"required"`
ExtraData []byte `json:"extraData" gencodec:"required"`
BaseFeePerGas *big.Int `json:"baseFeePerGas" gencodec:"required"`
BlockHash common.Hash `json:"blockHash" gencodec:"required"`
Transactions [][]byte `json:"transactions" gencodec:"required"`
}

// JSON type overrides for executableData.
type executableDataV1Marshaling struct {
Number hexutil.Uint64
GasLimit hexutil.Uint64
GasUsed hexutil.Uint64
Timestamp hexutil.Uint64
BaseFeePerGas *hexutil.Big
ExtraData hexutil.Bytes
LogsBloom hexutil.Bytes
Transactions []hexutil.Bytes
}

func (edv1 *ExecutableDataV1) ToExecutableData() beacon.ExecutableData {
return beacon.ExecutableData{
ParentHash: edv1.ParentHash,
FeeRecipient: edv1.FeeRecipient,
StateRoot: edv1.StateRoot,
ReceiptsRoot: edv1.ReceiptsRoot,
LogsBloom: edv1.LogsBloom,
Random: edv1.Random,
Number: edv1.Number,
GasLimit: edv1.GasLimit,
GasUsed: edv1.GasUsed,
Timestamp: edv1.Timestamp,
ExtraData: edv1.ExtraData,
BaseFeePerGas: edv1.BaseFeePerGas,
BlockHash: edv1.BlockHash,
Transactions: edv1.Transactions,
}
}

func (edv1 *ExecutableDataV1) FromExecutableData(ed *beacon.ExecutableData) {
if ed.Withdrawals != nil {
panic("source executable data contains withdrawals, not supported by V1")
}
edv1.ParentHash = ed.ParentHash
edv1.FeeRecipient = ed.FeeRecipient
edv1.StateRoot = ed.StateRoot
edv1.ReceiptsRoot = ed.ReceiptsRoot
edv1.LogsBloom = ed.LogsBloom
edv1.Random = ed.Random
edv1.Number = ed.Number
edv1.GasLimit = ed.GasLimit
edv1.GasUsed = ed.GasUsed
edv1.Timestamp = ed.Timestamp
edv1.ExtraData = ed.ExtraData
edv1.BaseFeePerGas = ed.BaseFeePerGas
edv1.BlockHash = ed.BlockHash
edv1.Transactions = ed.Transactions
}
38 changes: 34 additions & 4 deletions simulators/ethereum/engine/clmock/clmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

api "github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/hive/simulators/ethereum/engine/client"
client_types "github.com/ethereum/hive/simulators/ethereum/engine/client/types"
"github.com/ethereum/hive/simulators/ethereum/engine/globals"
"github.com/ethereum/hive/simulators/ethereum/engine/helper"

Expand All @@ -28,6 +29,32 @@ var (
DefaultPayloadProductionClientDelay = time.Second
)

type ExecutableDataHistory map[uint64]*api.ExecutableData

func (h ExecutableDataHistory) LatestPayloadNumber() uint64 {
latest := uint64(0)
for n := range h {
if n > latest {
latest = n
}
}
return latest
}

func (h ExecutableDataHistory) LatestWithdrawalsIndex() uint64 {
latest := uint64(0)
for _, p := range h {
if p.Withdrawals != nil {
for _, w := range p.Withdrawals {
if w.Index > latest {
latest = w.Index
}
}
}
}
return latest
}

// Consensus Layer Client Mock used to sync the Execution Clients once the TTD has been reached
type CLMocker struct {
*hivesim.T
Expand All @@ -53,7 +80,7 @@ type CLMocker struct {

// PoS Chain History Information
PrevRandaoHistory map[uint64]common.Hash
ExecutedPayloadHistory map[uint64]api.ExecutableData
ExecutedPayloadHistory ExecutableDataHistory
HeadHashHistory []common.Hash

// Latest broadcasted data using the PoS Engine API
Expand Down Expand Up @@ -103,7 +130,7 @@ func NewCLMocker(t *hivesim.T, slotsToSafe, slotsToFinalized, safeSlotsToImportO
T: t,
EngineClients: make([]client.EngineClient, 0),
PrevRandaoHistory: map[uint64]common.Hash{},
ExecutedPayloadHistory: map[uint64]api.ExecutableData{},
ExecutedPayloadHistory: ExecutableDataHistory{},
SlotsToSafe: slotsToSafe,
SlotsToFinalized: slotsToFinalized,
SafeSlotsToImportOptimistically: safeSlotsToImportOptimistically,
Expand Down Expand Up @@ -392,7 +419,8 @@ func (cl *CLMocker) broadcastNextNewPayload() {
}
}
cl.LatestExecutedPayload = cl.LatestPayloadBuilt
cl.ExecutedPayloadHistory[cl.LatestPayloadBuilt.Number] = cl.LatestPayloadBuilt
payload := cl.LatestPayloadBuilt
cl.ExecutedPayloadHistory[cl.LatestPayloadBuilt.Number] = &payload
}

func (cl *CLMocker) broadcastLatestForkchoice() {
Expand Down Expand Up @@ -581,7 +609,9 @@ func (cl *CLMocker) BroadcastNewPayload(payload *api.ExecutableData) []ExecutePa
if isShanghai(payload.Timestamp, cl.ShanghaiTimestamp) {
execPayloadResp, err = ec.NewPayloadV2(ctx, payload)
} else {
execPayloadResp, err = ec.NewPayloadV1(ctx, payload)
edv1 := &client_types.ExecutableDataV1{}
edv1.FromExecutableData(payload)
execPayloadResp, err = ec.NewPayloadV1(ctx, edv1)
}
if err != nil {
cl.Errorf("CLMocker: Could not ExecutePayloadV1: %v", err)
Expand Down
Loading