Skip to content

Commit

Permalink
simulators/ethereum/engine: major refactor to allow blob RLP txs
Browse files Browse the repository at this point in the history
  • Loading branch information
marioevz committed Jun 3, 2023
1 parent a721a3e commit 9b63952
Show file tree
Hide file tree
Showing 21 changed files with 916 additions and 681 deletions.
15 changes: 7 additions & 8 deletions simulators/ethereum/engine/client/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"

client_types "github.com/ethereum/hive/simulators/ethereum/engine/client/types"
e_typ "github.com/ethereum/hive/simulators/ethereum/engine/types"
)

type Eth interface {
Expand All @@ -20,8 +19,8 @@ type Eth interface {
BlockNumber(ctx context.Context) (uint64, error)
BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error)
HeaderByNumber(ctx context.Context, number *big.Int) (*types.Header, error)
SendTransaction(ctx context.Context, tx *types.Transaction) error
SendTransactions(ctx context.Context, txs []*types.Transaction) []error
SendTransaction(ctx context.Context, tx e_typ.Transaction) error
SendTransactions(ctx context.Context, txs ...e_typ.Transaction) []error
StorageAt(ctx context.Context, account common.Address, key common.Hash, blockNumber *big.Int) ([]byte, error)
StorageAtKeys(ctx context.Context, account common.Address, keys []common.Hash, blockNumber *big.Int) (map[common.Hash]*common.Hash, error)
TransactionReceipt(ctx context.Context, txHash common.Hash) (*types.Receipt, error)
Expand All @@ -36,15 +35,15 @@ 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)
GetPayloadV3(ctx context.Context, payloadId *api.PayloadID) (api.ExecutableData, *big.Int, *api.BlobsBundle, error)
GetPayloadV3(ctx context.Context, payloadId *api.PayloadID) (api.ExecutableData, *big.Int, *e_typ.BlobsBundle, error)

NewPayload(ctx context.Context, version int, payload interface{}, versionedHashes []common.Hash) (api.PayloadStatusV1, error)
NewPayloadV1(ctx context.Context, payload *client_types.ExecutableDataV1) (api.PayloadStatusV1, error)
NewPayloadV1(ctx context.Context, payload *e_typ.ExecutableDataV1) (api.PayloadStatusV1, error)
NewPayloadV2(ctx context.Context, payload *api.ExecutableData) (api.PayloadStatusV1, error)
NewPayloadV3(ctx context.Context, payload *api.ExecutableData, versionedHashes []common.Hash) (api.PayloadStatusV1, error)

GetPayloadBodiesByRangeV1(ctx context.Context, start uint64, count uint64) ([]*client_types.ExecutionPayloadBodyV1, error)
GetPayloadBodiesByHashV1(ctx context.Context, hashes []common.Hash) ([]*client_types.ExecutionPayloadBodyV1, error)
GetPayloadBodiesByRangeV1(ctx context.Context, start uint64, count uint64) ([]*e_typ.ExecutionPayloadBodyV1, error)
GetPayloadBodiesByHashV1(ctx context.Context, hashes []common.Hash) ([]*e_typ.ExecutionPayloadBodyV1, error)

LatestForkchoiceSent() (fcState *api.ForkchoiceStateV1, pAttributes *api.PayloadAttributes)
LatestNewPayloadSent() (payload *api.ExecutableData)
Expand Down
38 changes: 25 additions & 13 deletions simulators/ethereum/engine/client/hive_rpc/hive_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/hive/hivesim"
"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"
e_typ "github.com/ethereum/hive/simulators/ethereum/engine/types"
"github.com/golang-jwt/jwt/v4"
"github.com/pkg/errors"
)
Expand All @@ -37,6 +37,8 @@ type HiveRPCEngineStarter struct {
JWTSecret []byte
}

var _ client.EngineStarter = (*HiveRPCEngineStarter)(nil)

func (s HiveRPCEngineStarter) StartClient(T *hivesim.T, testContext context.Context, genesis *core.Genesis, ClientParams hivesim.Params, ClientFiles hivesim.Params, bootClients ...client.EngineClient) (client.EngineClient, error) {
var (
clientType = s.ClientType
Expand Down Expand Up @@ -166,6 +168,8 @@ type HiveRPCEngineClient struct {
accTxInfoMap map[common.Address]*AccountTransactionInfo
}

var _ client.EngineClient = (*HiveRPCEngineClient)(nil)

// NewClient creates a engine client that uses the given RPC client.
func NewHiveRPCEngineClient(h *hivesim.Client, enginePort int, ethPort int, jwtSecretBytes []byte, ttd *big.Int, transport http.RoundTripper) *HiveRPCEngineClient {
// Prepare HTTP Client
Expand Down Expand Up @@ -358,11 +362,11 @@ func (ec *HiveRPCEngineClient) ForkchoiceUpdatedV2(ctx context.Context, fcState

// Get Payload API Calls

func (ec *HiveRPCEngineClient) GetPayload(ctx context.Context, version int, payloadId *api.PayloadID) (api.ExecutableData, *big.Int, *api.BlobsBundle, error) {
func (ec *HiveRPCEngineClient) GetPayload(ctx context.Context, version int, payloadId *api.PayloadID) (api.ExecutableData, *big.Int, *e_typ.BlobsBundle, error) {
var (
executableData api.ExecutableData
blockValue *big.Int
blobsBundle *api.BlobsBundle
blobsBundle *e_typ.BlobsBundle
err error
rpcString = fmt.Sprintf("engine_getPayloadV%d", version)
)
Expand All @@ -372,7 +376,7 @@ func (ec *HiveRPCEngineClient) GetPayload(ctx context.Context, version int, payl
}

if version >= 2 {
var response api.ExecutionPayloadEnvelope
var response e_typ.ExecutionPayloadEnvelope
err = ec.c.CallContext(ctx, &response, rpcString, payloadId)
if response.ExecutionPayload != nil {
executableData = *response.ExecutionPayload
Expand All @@ -396,14 +400,14 @@ func (ec *HiveRPCEngineClient) GetPayloadV2(ctx context.Context, payloadId *api.
return ed, bv, err
}

func (ec *HiveRPCEngineClient) GetPayloadV3(ctx context.Context, payloadId *api.PayloadID) (api.ExecutableData, *big.Int, *api.BlobsBundle, error) {
func (ec *HiveRPCEngineClient) GetPayloadV3(ctx context.Context, payloadId *api.PayloadID) (api.ExecutableData, *big.Int, *e_typ.BlobsBundle, error) {
return ec.GetPayload(ctx, 3, payloadId)
}

// Get Payload Bodies API Calls
func (ec *HiveRPCEngineClient) GetPayloadBodiesByRangeV1(ctx context.Context, start uint64, count uint64) ([]*client_types.ExecutionPayloadBodyV1, error) {
func (ec *HiveRPCEngineClient) GetPayloadBodiesByRangeV1(ctx context.Context, start uint64, count uint64) ([]*e_typ.ExecutionPayloadBodyV1, error) {
var (
result []*client_types.ExecutionPayloadBodyV1
result []*e_typ.ExecutionPayloadBodyV1
err error
)
if err = ec.PrepareDefaultAuthCallToken(); err != nil {
Expand All @@ -414,9 +418,9 @@ func (ec *HiveRPCEngineClient) GetPayloadBodiesByRangeV1(ctx context.Context, st
return result, err
}

func (ec *HiveRPCEngineClient) GetPayloadBodiesByHashV1(ctx context.Context, hashes []common.Hash) ([]*client_types.ExecutionPayloadBodyV1, error) {
func (ec *HiveRPCEngineClient) GetPayloadBodiesByHashV1(ctx context.Context, hashes []common.Hash) ([]*e_typ.ExecutionPayloadBodyV1, error) {
var (
result []*client_types.ExecutionPayloadBodyV1
result []*e_typ.ExecutionPayloadBodyV1
err error
)
if err = ec.PrepareDefaultAuthCallToken(); err != nil {
Expand All @@ -428,9 +432,9 @@ func (ec *HiveRPCEngineClient) GetPayloadBodiesByHashV1(ctx context.Context, has
}

// Get Blob Bundle API Calls
func (ec *HiveRPCEngineClient) GetBlobsBundleV1(ctx context.Context, payloadId *api.PayloadID) (*api.BlobsBundle, error) {
func (ec *HiveRPCEngineClient) GetBlobsBundleV1(ctx context.Context, payloadId *api.PayloadID) (*e_typ.BlobsBundle, error) {
var (
result api.BlobsBundle
result e_typ.BlobsBundle
err error
)
if err = ec.PrepareDefaultAuthCallToken(); err != nil {
Expand All @@ -456,7 +460,7 @@ func (ec *HiveRPCEngineClient) NewPayload(ctx context.Context, version int, payl
return result, err
}

func (ec *HiveRPCEngineClient) NewPayloadV1(ctx context.Context, payload *client_types.ExecutableDataV1) (api.PayloadStatusV1, error) {
func (ec *HiveRPCEngineClient) NewPayloadV1(ctx context.Context, payload *e_typ.ExecutableDataV1) (api.PayloadStatusV1, error) {
ed := payload.ToExecutableData()
ec.latestPayloadSent = &ed
return ec.NewPayload(ctx, 1, payload, nil)
Expand Down Expand Up @@ -553,7 +557,15 @@ func (ec *HiveRPCEngineClient) UpdateNonce(testCtx context.Context, account comm
return nil
}

func (ec *HiveRPCEngineClient) SendTransactions(ctx context.Context, txs []*types.Transaction) []error {
func (ec *HiveRPCEngineClient) SendTransaction(ctx context.Context, tx e_typ.Transaction) error {
data, err := tx.MarshalBinary()
if err != nil {
return err
}
return ec.cEth.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data))
}

func (ec *HiveRPCEngineClient) SendTransactions(ctx context.Context, txs ...e_typ.Transaction) []error {
reqs := make([]rpc.BatchElem, len(txs))
hashes := make([]common.Hash, len(txs))
for i := range reqs {
Expand Down
86 changes: 49 additions & 37 deletions simulators/ethereum/engine/client/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ import (
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/hive/hivesim"
"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"
e_typ "github.com/ethereum/hive/simulators/ethereum/engine/types"
)

type GethNodeTestConfiguration struct {
Expand Down Expand Up @@ -206,6 +206,8 @@ type GethNode struct {
config GethNodeTestConfiguration
}

var _ client.EngineClient = (*GethNode)(nil)

func newNode(config GethNodeTestConfiguration, bootnodes []string, genesis *core.Genesis) (*GethNode, error) {
// Define the basic configurations for the Ethereum node
datadir, _ := os.MkdirTemp("", "")
Expand Down Expand Up @@ -234,14 +236,14 @@ func restart(startConfig GethNodeTestConfiguration, bootnodes []string, datadir
return nil, err
}
econfig := &ethconfig.Config{
Genesis: genesis,
NetworkId: genesis.Config.ChainID.Uint64(),
SyncMode: downloader.FullSync,
DatabaseCache: 256,
DatabaseHandles: 256,
TxPool: txpool.DefaultConfig,
GPO: ethconfig.Defaults.GPO,
Ethash: ethconfig.Defaults.Ethash,
Genesis: genesis,
NetworkId: genesis.Config.ChainID.Uint64(),
SyncMode: downloader.FullSync,
DatabaseCache: 256,
DatabaseHandles: 256,
TxPool: txpool.DefaultConfig,
GPO: ethconfig.Defaults.GPO,
// Ethash: ethconfig.Defaults.Ethash,
Miner: ethconfig.Defaults.Miner,
LightServ: 100,
LightPeers: int(startConfig.MaxPeers.Int64()) - 1,
Expand Down Expand Up @@ -288,18 +290,20 @@ func restart(startConfig GethNodeTestConfiguration, bootnodes []string, datadir
}

g.running, g.closing = context.WithCancel(context.Background())
if startConfig.PoWMiner || startConfig.Ethash {
// Create the ethash consensus module
ethashConfig := ethconfig.Defaults.Ethash
ethashConfig.PowMode = ethash.ModeNormal
ethashConfig.CacheDir = "/ethash"
ethashConfig.DatasetDir = ethashConfig.CacheDir
g.ethashEngine = ethash.New(ethashConfig, nil, false)
}
if startConfig.PoWMiner {
// Enable mining
go g.EnablePoWMining()
}
/*
if startConfig.PoWMiner || startConfig.Ethash {
// Create the ethash consensus module
ethashConfig := ethconfig.Defaults.Ethash
ethashConfig.PowMode = ethash.ModeNormal
ethashConfig.CacheDir = "/ethash"
ethashConfig.DatasetDir = ethashConfig.CacheDir
g.ethashEngine = ethash.New(ethashConfig, nil, false)
}
if startConfig.PoWMiner {
// Enable mining
go g.EnablePoWMining()
}
*/

// Start thread to monitor the amount of gossiped blocks this node receives
go g.SubscribeP2PEvents()
Expand Down Expand Up @@ -457,7 +461,7 @@ func (n *GethNode) PoWMiningLoop() {
// Modify the sealed block if necessary
if n.config.BlockModifier != nil {
sealVerifier := func(h *types.Header) bool {
return n.ethashEngine.VerifyHeader(n.eth.BlockChain(), h, true) == nil
return n.ethashEngine.VerifyHeader(n.eth.BlockChain(), h) == nil
}
b, err = n.config.BlockModifier.ModifySealedBlock(sealVerifier, b)
if err != nil {
Expand Down Expand Up @@ -616,7 +620,7 @@ func (v *validator) ValidateState(block *types.Block, state *state.StateDB, rece

type processor struct{}

func (p *processor) Process(block *types.Block, excessDataGas *big.Int, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
func (p *processor) Process(block *types.Block, statedb *state.StateDB, cfg vm.Config) (types.Receipts, []*types.Log, uint64, error) {
return types.Receipts{}, []*types.Log{}, 21000, nil
}

Expand Down Expand Up @@ -656,7 +660,7 @@ func (n *GethNode) SetBlock(block *types.Block, parentNumber uint64, parentRoot
}
statedb.StartPrefetcher("chain")
var failedProcessing bool
receipts, _, _, err := n.eth.BlockChain().Processor().Process(block, n.eth.BlockChain().CurrentBlock().ExcessDataGas, statedb, *n.eth.BlockChain().GetVMConfig())
receipts, _, _, err := n.eth.BlockChain().Processor().Process(block, statedb, *n.eth.BlockChain().GetVMConfig())
if err != nil {
failedProcessing = true
}
Expand Down Expand Up @@ -698,7 +702,7 @@ func (n *GethNode) SetBlock(block *types.Block, parentNumber uint64, parentRoot
func (n *GethNode) NewPayload(ctx context.Context, version int, pl interface{}, vh []common.Hash) (beacon.PayloadStatusV1, error) {
switch version {
case 1:
if c, ok := pl.(*client_types.ExecutableDataV1); ok {
if c, ok := pl.(*e_typ.ExecutableDataV1); ok {
return n.NewPayloadV1(ctx, c)
} else {
return beacon.PayloadStatusV1{}, fmt.Errorf("wrong type %T", pl)
Expand All @@ -719,7 +723,7 @@ func (n *GethNode) NewPayload(ctx context.Context, version int, pl interface{},
return beacon.PayloadStatusV1{}, fmt.Errorf("unknown version %d", version)
}

func (n *GethNode) NewPayloadV1(ctx context.Context, pl *client_types.ExecutableDataV1) (beacon.PayloadStatusV1, error) {
func (n *GethNode) NewPayloadV1(ctx context.Context, pl *e_typ.ExecutableDataV1) (beacon.PayloadStatusV1, error) {
ed := pl.ToExecutableData()
n.latestPayloadSent = &ed
resp, err := n.api.NewPayloadV1(ed)
Expand Down Expand Up @@ -783,23 +787,23 @@ func (n *GethNode) GetPayloadV2(ctx context.Context, payloadId *beacon.PayloadID
return *p.ExecutionPayload, p.BlockValue, err
}

func (n *GethNode) GetPayloadV3(ctx context.Context, payloadId *beacon.PayloadID) (beacon.ExecutableData, *big.Int, *beacon.BlobsBundle, error) {
func (n *GethNode) GetPayloadV3(ctx context.Context, payloadId *beacon.PayloadID) (beacon.ExecutableData, *big.Int, *e_typ.BlobsBundle, error) {
p, err := n.api.GetPayloadV3(*payloadId)
if p == nil || err != nil {
return beacon.ExecutableData{}, nil, nil, err
}
return *p.ExecutionPayload, p.BlockValue, p.BlobsBundle, err
return *p.ExecutionPayload, p.BlockValue, nil, err
}

func (n *GethNode) GetPayloadBodiesByRangeV1(ctx context.Context, start uint64, count uint64) ([]*client_types.ExecutionPayloadBodyV1, error) {
func (n *GethNode) GetPayloadBodiesByRangeV1(ctx context.Context, start uint64, count uint64) ([]*e_typ.ExecutionPayloadBodyV1, error) {
return nil, fmt.Errorf("not implemented")
}

func (n *GethNode) GetPayloadBodiesByHashV1(ctx context.Context, hashes []common.Hash) ([]*client_types.ExecutionPayloadBodyV1, error) {
func (n *GethNode) GetPayloadBodiesByHashV1(ctx context.Context, hashes []common.Hash) ([]*e_typ.ExecutionPayloadBodyV1, error) {
return nil, fmt.Errorf("not implemented")
}

func (n *GethNode) GetBlobsBundleV1(ctx context.Context, payloadId *beacon.PayloadID) (*beacon.BlobsBundle, error) {
func (n *GethNode) GetBlobsBundleV1(ctx context.Context, payloadId *beacon.PayloadID) (*e_typ.BlobsBundle, error) {
return nil, fmt.Errorf("not implemented")
}

Expand Down Expand Up @@ -860,16 +864,24 @@ func (n *GethNode) HeaderByNumber(ctx context.Context, number *big.Int) (*types.
return b.Header(), err
}

func (n *GethNode) SendTransaction(ctx context.Context, tx *types.Transaction) error {
return n.eth.APIBackend.SendTx(ctx, tx)
func (n *GethNode) SendTransaction(ctx context.Context, tx e_typ.Transaction) error {
if v, ok := tx.(*types.Transaction); ok {
return n.eth.APIBackend.SendTx(ctx, v)
}
return fmt.Errorf("invalid transaction type")
}

func (n *GethNode) SendTransactions(ctx context.Context, txs []*types.Transaction) []error {
func (n *GethNode) SendTransactions(ctx context.Context, txs ...e_typ.Transaction) []error {
for _, tx := range txs {
err := n.eth.APIBackend.SendTx(ctx, tx)
if err != nil {
return []error{err}
if v, ok := tx.(*types.Transaction); ok {
err := n.eth.APIBackend.SendTx(ctx, v)
if err != nil {
return []error{err}
}
} else {
return []error{fmt.Errorf("invalid transaction type")}
}

}
return nil
}
Expand Down
6 changes: 3 additions & 3 deletions simulators/ethereum/engine/clmock/clmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

api "github.com/ethereum/go-ethereum/beacon/engine"
"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"
e_typ "github.com/ethereum/hive/simulators/ethereum/engine/types"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -91,7 +91,7 @@ type CLMocker struct {
LatestHeader *types.Header
LatestPayloadBuilt api.ExecutableData
LatestBlockValue *big.Int
LatestBlobBundle *api.BlobsBundle
LatestBlobBundle *e_typ.BlobsBundle
LatestPayloadAttributes api.PayloadAttributes
LatestExecutedPayload api.ExecutableData
LatestForkchoice api.ForkchoiceStateV1
Expand Down Expand Up @@ -642,7 +642,7 @@ func (cl *CLMocker) BroadcastNewPayload(payload *api.ExecutableData, versionedHa
} else if isShanghai(payload.Timestamp, cl.ShanghaiTimestamp) {
execPayloadResp, err = ec.NewPayloadV2(ctx, payload)
} else {
edv1 := &client_types.ExecutableDataV1{}
edv1 := &e_typ.ExecutableDataV1{}
edv1.FromExecutableData(payload)
execPayloadResp, err = ec.NewPayloadV1(ctx, edv1)
}
Expand Down
Loading

0 comments on commit 9b63952

Please sign in to comment.