88 "net"
99 "net/http"
1010 "strings"
11+ "sync"
1112 "time"
1213
1314 "github.com/ethereum/go-ethereum"
@@ -20,9 +21,9 @@ import (
2021 "github.com/ethereum/go-ethereum/rpc"
2122 "github.com/ethereum/hive/hivesim"
2223 "github.com/ethereum/hive/simulators/ethereum/engine/client"
23- client_types "github.com/ethereum/hive/simulators/ethereum/engine/client/types"
2424 "github.com/ethereum/hive/simulators/ethereum/engine/globals"
2525 "github.com/ethereum/hive/simulators/ethereum/engine/helper"
26+ typ "github.com/ethereum/hive/simulators/ethereum/engine/types"
2627 "github.com/golang-jwt/jwt/v4"
2728 "github.com/pkg/errors"
2829)
@@ -37,6 +38,8 @@ type HiveRPCEngineStarter struct {
3738 JWTSecret []byte
3839}
3940
41+ var _ client.EngineStarter = (* HiveRPCEngineStarter )(nil )
42+
4043func (s HiveRPCEngineStarter ) StartClient (T * hivesim.T , testContext context.Context , genesis * core.Genesis , ClientParams hivesim.Params , ClientFiles hivesim.Params , bootClients ... client.EngineClient ) (client.EngineClient , error ) {
4144 var (
4245 clientType = s .ClientType
@@ -156,16 +159,19 @@ type HiveRPCEngineClient struct {
156159
157160 // Engine updates info
158161 latestFcUStateSent * api.ForkchoiceStateV1
159- latestPAttrSent * api .PayloadAttributes
162+ latestPAttrSent * typ .PayloadAttributes
160163 latestFcUResponse * api.ForkChoiceResponse
161164
162- latestPayloadSent * api .ExecutableData
165+ latestPayloadSent * typ .ExecutableData
163166 latestPayloadStatusReponse * api.PayloadStatusV1
164167
165168 // Test account nonces
166- accTxInfoMap map [common.Address ]* AccountTransactionInfo
169+ accTxInfoMap map [common.Address ]* AccountTransactionInfo
170+ accTxInfoMapLock sync.Mutex
167171}
168172
173+ var _ client.EngineClient = (* HiveRPCEngineClient )(nil )
174+
169175// NewClient creates a engine client that uses the given RPC client.
170176func NewHiveRPCEngineClient (h * hivesim.Client , enginePort int , ethPort int , jwtSecretBytes []byte , ttd * big.Int , transport http.RoundTripper ) * HiveRPCEngineClient {
171177 // Prepare HTTP Client
@@ -330,7 +336,9 @@ func (ec *HiveRPCEngineClient) PrepareDefaultAuthCallToken() error {
330336}
331337
332338// Engine API Call Methods
333- func (ec * HiveRPCEngineClient ) ForkchoiceUpdated (ctx context.Context , version int , fcState * api.ForkchoiceStateV1 , pAttributes * api.PayloadAttributes ) (api.ForkChoiceResponse , error ) {
339+
340+ // Forkchoice Updated API Calls
341+ func (ec * HiveRPCEngineClient ) ForkchoiceUpdated (ctx context.Context , version int , fcState * api.ForkchoiceStateV1 , pAttributes * typ.PayloadAttributes ) (api.ForkChoiceResponse , error ) {
334342 var result api.ForkChoiceResponse
335343 if err := ec .PrepareDefaultAuthCallToken (); err != nil {
336344 return result , err
@@ -346,52 +354,66 @@ func (ec *HiveRPCEngineClient) ForkchoiceUpdated(ctx context.Context, version in
346354 return result , err
347355}
348356
349- func (ec * HiveRPCEngineClient ) ForkchoiceUpdatedV1 (ctx context.Context , fcState * api.ForkchoiceStateV1 , pAttributes * api .PayloadAttributes ) (api.ForkChoiceResponse , error ) {
357+ func (ec * HiveRPCEngineClient ) ForkchoiceUpdatedV1 (ctx context.Context , fcState * api.ForkchoiceStateV1 , pAttributes * typ .PayloadAttributes ) (api.ForkChoiceResponse , error ) {
350358 return ec .ForkchoiceUpdated (ctx , 1 , fcState , pAttributes )
351359}
352360
353- func (ec * HiveRPCEngineClient ) ForkchoiceUpdatedV2 (ctx context.Context , fcState * api.ForkchoiceStateV1 , pAttributes * api .PayloadAttributes ) (api.ForkChoiceResponse , error ) {
361+ func (ec * HiveRPCEngineClient ) ForkchoiceUpdatedV2 (ctx context.Context , fcState * api.ForkchoiceStateV1 , pAttributes * typ .PayloadAttributes ) (api.ForkChoiceResponse , error ) {
354362 return ec .ForkchoiceUpdated (ctx , 2 , fcState , pAttributes )
355363}
356364
357- func (ec * HiveRPCEngineClient ) GetPayload (ctx context.Context , version int , payloadId * api.PayloadID ) (api.ExecutableData , * big.Int , error ) {
365+ func (ec * HiveRPCEngineClient ) ForkchoiceUpdatedV3 (ctx context.Context , fcState * api.ForkchoiceStateV1 , pAttributes * typ.PayloadAttributes ) (api.ForkChoiceResponse , error ) {
366+ return ec .ForkchoiceUpdated (ctx , 3 , fcState , pAttributes )
367+ }
368+
369+ // Get Payload API Calls
370+
371+ func (ec * HiveRPCEngineClient ) GetPayload (ctx context.Context , version int , payloadId * api.PayloadID ) (typ.ExecutableData , * big.Int , * typ.BlobsBundle , error ) {
358372 var (
359- executableData api .ExecutableData
373+ executableData typ .ExecutableData
360374 blockValue * big.Int
375+ blobsBundle * typ.BlobsBundle
361376 err error
362377 rpcString = fmt .Sprintf ("engine_getPayloadV%d" , version )
363378 )
364379
365380 if err = ec .PrepareDefaultAuthCallToken (); err != nil {
366- return executableData , nil , err
381+ return executableData , nil , nil , err
367382 }
368383
369- if version = = 2 {
370- var response api .ExecutionPayloadEnvelope
384+ if version > = 2 {
385+ var response typ .ExecutionPayloadEnvelope
371386 err = ec .c .CallContext (ctx , & response , rpcString , payloadId )
372387 if response .ExecutionPayload != nil {
373388 executableData = * response .ExecutionPayload
374389 }
375390 blockValue = response .BlockValue
391+ blobsBundle = response .BlobsBundle
376392 } else {
377393 err = ec .c .CallContext (ctx , & executableData , rpcString , payloadId )
378394 }
379395
380- return executableData , blockValue , err
396+ return executableData , blockValue , blobsBundle , err
381397}
382398
383- func (ec * HiveRPCEngineClient ) GetPayloadV1 (ctx context.Context , payloadId * api.PayloadID ) (api .ExecutableData , error ) {
384- ed , _ , err := ec .GetPayload (ctx , 1 , payloadId )
399+ func (ec * HiveRPCEngineClient ) GetPayloadV1 (ctx context.Context , payloadId * api.PayloadID ) (typ .ExecutableData , error ) {
400+ ed , _ , _ , err := ec .GetPayload (ctx , 1 , payloadId )
385401 return ed , err
386402}
387403
388- func (ec * HiveRPCEngineClient ) GetPayloadV2 (ctx context.Context , payloadId * api.PayloadID ) (api.ExecutableData , * big.Int , error ) {
389- return ec .GetPayload (ctx , 2 , payloadId )
404+ func (ec * HiveRPCEngineClient ) GetPayloadV2 (ctx context.Context , payloadId * api.PayloadID ) (typ.ExecutableData , * big.Int , error ) {
405+ ed , bv , _ , err := ec .GetPayload (ctx , 2 , payloadId )
406+ return ed , bv , err
407+ }
408+
409+ func (ec * HiveRPCEngineClient ) GetPayloadV3 (ctx context.Context , payloadId * api.PayloadID ) (typ.ExecutableData , * big.Int , * typ.BlobsBundle , error ) {
410+ return ec .GetPayload (ctx , 3 , payloadId )
390411}
391412
392- func (ec * HiveRPCEngineClient ) GetPayloadBodiesByRangeV1 (ctx context.Context , start uint64 , count uint64 ) ([]* client_types.ExecutionPayloadBodyV1 , error ) {
413+ // Get Payload Bodies API Calls
414+ func (ec * HiveRPCEngineClient ) GetPayloadBodiesByRangeV1 (ctx context.Context , start uint64 , count uint64 ) ([]* typ.ExecutionPayloadBodyV1 , error ) {
393415 var (
394- result []* client_types .ExecutionPayloadBodyV1
416+ result []* typ .ExecutionPayloadBodyV1
395417 err error
396418 )
397419 if err = ec .PrepareDefaultAuthCallToken (); err != nil {
@@ -402,9 +424,9 @@ func (ec *HiveRPCEngineClient) GetPayloadBodiesByRangeV1(ctx context.Context, st
402424 return result , err
403425}
404426
405- func (ec * HiveRPCEngineClient ) GetPayloadBodiesByHashV1 (ctx context.Context , hashes []common.Hash ) ([]* client_types .ExecutionPayloadBodyV1 , error ) {
427+ func (ec * HiveRPCEngineClient ) GetPayloadBodiesByHashV1 (ctx context.Context , hashes []common.Hash ) ([]* typ .ExecutionPayloadBodyV1 , error ) {
406428 var (
407- result []* client_types .ExecutionPayloadBodyV1
429+ result []* typ .ExecutionPayloadBodyV1
408430 err error
409431 )
410432 if err = ec .PrepareDefaultAuthCallToken (); err != nil {
@@ -415,35 +437,52 @@ func (ec *HiveRPCEngineClient) GetPayloadBodiesByHashV1(ctx context.Context, has
415437 return result , err
416438}
417439
418- func (ec * HiveRPCEngineClient ) NewPayload (ctx context.Context , version int , payload interface {}, versionedHashes []common.Hash ) (result api.PayloadStatusV1 , err error ) {
440+ // Get Blob Bundle API Calls
441+ func (ec * HiveRPCEngineClient ) GetBlobsBundleV1 (ctx context.Context , payloadId * api.PayloadID ) (* typ.BlobsBundle , error ) {
442+ var (
443+ result typ.BlobsBundle
444+ err error
445+ )
446+ if err = ec .PrepareDefaultAuthCallToken (); err != nil {
447+ return nil , err
448+ }
449+
450+ err = ec .c .CallContext (ctx , & result , "engine_getBlobsBundleV1" , payloadId )
451+ return & result , err
452+ }
453+
454+ // New Payload API Call Methods
455+ func (ec * HiveRPCEngineClient ) NewPayload (ctx context.Context , version int , payload interface {}, versionedHashes * []common.Hash , beaconRoot * common.Hash ) (result api.PayloadStatusV1 , err error ) {
419456 if err := ec .PrepareDefaultAuthCallToken (); err != nil {
420457 return result , err
421458 }
422459
423- if versionedHashes != nil {
424- err = ec .c .CallContext (ctx , & result , fmt .Sprintf ("engine_newPayloadV%d" , version ), payload , versionedHashes )
460+ if version >= 3 {
461+ err = ec .c .CallContext (ctx , & result , fmt .Sprintf ("engine_newPayloadV%d" , version ), payload , versionedHashes , beaconRoot )
425462 } else {
426463 err = ec .c .CallContext (ctx , & result , fmt .Sprintf ("engine_newPayloadV%d" , version ), payload )
427464 }
428465 ec .latestPayloadStatusReponse = & result
429466 return result , err
430467}
431468
432- func (ec * HiveRPCEngineClient ) NewPayloadV1 (ctx context.Context , payload * client_types .ExecutableDataV1 ) (api.PayloadStatusV1 , error ) {
469+ func (ec * HiveRPCEngineClient ) NewPayloadV1 (ctx context.Context , payload * typ .ExecutableDataV1 ) (api.PayloadStatusV1 , error ) {
433470 ed := payload .ToExecutableData ()
434471 ec .latestPayloadSent = & ed
435- return ec .NewPayload (ctx , 1 , payload , nil )
472+ return ec .NewPayload (ctx , 1 , payload , nil , nil )
436473}
437474
438- func (ec * HiveRPCEngineClient ) NewPayloadV2 (ctx context.Context , payload * api .ExecutableData ) (api.PayloadStatusV1 , error ) {
475+ func (ec * HiveRPCEngineClient ) NewPayloadV2 (ctx context.Context , payload * typ .ExecutableData ) (api.PayloadStatusV1 , error ) {
439476 ec .latestPayloadSent = payload
440- return ec .NewPayload (ctx , 2 , payload , nil )
477+ return ec .NewPayload (ctx , 2 , payload , nil , nil )
441478}
442479
443- func (ec * HiveRPCEngineClient ) NewPayloadV3 (ctx context.Context , payload * api .ExecutableData , versionedHashes [] common.Hash ) (api.PayloadStatusV1 , error ) {
480+ func (ec * HiveRPCEngineClient ) NewPayloadV3 (ctx context.Context , payload * typ .ExecutableData , versionedHashes * []common. Hash , beaconRoot * common.Hash ) (api.PayloadStatusV1 , error ) {
444481 ec .latestPayloadSent = payload
445- return ec .NewPayload (ctx , 3 , payload , versionedHashes )
482+ return ec .NewPayload (ctx , 3 , payload , versionedHashes , beaconRoot )
446483}
484+
485+ // Exchange Transition Configuration API Call Methods
447486func (ec * HiveRPCEngineClient ) ExchangeTransitionConfigurationV1 (ctx context.Context , tConf * api.TransitionConfigurationV1 ) (api.TransitionConfigurationV1 , error ) {
448487 var result api.TransitionConfigurationV1
449488 err := ec .c .CallContext (ctx , & result , "engine_exchangeTransitionConfigurationV1" , tConf )
@@ -459,6 +498,26 @@ func (ec *HiveRPCEngineClient) ExchangeCapabilities(ctx context.Context, clCapab
459498 return result , err
460499}
461500
501+ // Account Nonce
502+ func (ec * HiveRPCEngineClient ) GetLastAccountNonce (testCtx context.Context , account common.Address ) (uint64 , error ) {
503+ // First get the current head of the client where we will send the tx
504+ ctx , cancel := context .WithTimeout (testCtx , globals .RPCTimeout )
505+ defer cancel ()
506+ head , err := ec .HeaderByNumber (ctx , nil )
507+ if err != nil {
508+ return 0 , err
509+ }
510+
511+ // Then check if we have any info about this account, and when it was last updated
512+ if accTxInfo , ok := ec .accTxInfoMap [account ]; ok && accTxInfo != nil && (accTxInfo .PreviousBlock == head .Hash () || accTxInfo .PreviousBlock == head .ParentHash ) {
513+ // We have info about this account and is up to date (or up to date until the very last block).
514+ // Return the previous nonce
515+ return accTxInfo .PreviousNonce , nil
516+ }
517+ // We don't have info about this account, so there is no previous nonce
518+ return 0 , fmt .Errorf ("no previous nonce for account %s" , account .String ())
519+ }
520+
462521func (ec * HiveRPCEngineClient ) GetNextAccountNonce (testCtx context.Context , account common.Address ) (uint64 , error ) {
463522 // First get the current head of the client where we will send the tx
464523 ctx , cancel := context .WithTimeout (testCtx , globals .RPCTimeout )
@@ -482,6 +541,8 @@ func (ec *HiveRPCEngineClient) GetNextAccountNonce(testCtx context.Context, acco
482541 if err != nil {
483542 return 0 , err
484543 }
544+ ec .accTxInfoMapLock .Lock ()
545+ defer ec .accTxInfoMapLock .Unlock ()
485546 ec .accTxInfoMap [account ] = & AccountTransactionInfo {
486547 PreviousBlock : head .Hash (),
487548 PreviousNonce : nonce ,
@@ -504,7 +565,15 @@ func (ec *HiveRPCEngineClient) UpdateNonce(testCtx context.Context, account comm
504565 return nil
505566}
506567
507- func (ec * HiveRPCEngineClient ) SendTransactions (ctx context.Context , txs []* types.Transaction ) []error {
568+ func (ec * HiveRPCEngineClient ) SendTransaction (ctx context.Context , tx typ.Transaction ) error {
569+ data , err := tx .MarshalBinary ()
570+ if err != nil {
571+ return err
572+ }
573+ return ec .cEth .CallContext (ctx , nil , "eth_sendRawTransaction" , hexutil .Encode (data ))
574+ }
575+
576+ func (ec * HiveRPCEngineClient ) SendTransactions (ctx context.Context , txs ... typ.Transaction ) []error {
508577 reqs := make ([]rpc.BatchElem , len (txs ))
509578 hashes := make ([]common.Hash , len (txs ))
510579 for i := range reqs {
@@ -534,11 +603,11 @@ func (ec *HiveRPCEngineClient) PostRunVerifications() error {
534603 return nil
535604}
536605
537- func (ec * HiveRPCEngineClient ) LatestForkchoiceSent () (fcState * api.ForkchoiceStateV1 , pAttributes * api .PayloadAttributes ) {
606+ func (ec * HiveRPCEngineClient ) LatestForkchoiceSent () (fcState * api.ForkchoiceStateV1 , pAttributes * typ .PayloadAttributes ) {
538607 return ec .latestFcUStateSent , ec .latestPAttrSent
539608}
540609
541- func (ec * HiveRPCEngineClient ) LatestNewPayloadSent () * api .ExecutableData {
610+ func (ec * HiveRPCEngineClient ) LatestNewPayloadSent () * typ .ExecutableData {
542611 return ec .latestPayloadSent
543612}
544613
0 commit comments