From 8656f6d15b78d4db5a76dc8d799d1132ae702d6e Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Wed, 2 Oct 2024 12:40:19 +0200 Subject: [PATCH] engineapi: Add pectra engine api changes (#12172) Cherry pick #10963 Co-authored-by: Somnath --- .../execution_client/execution_client_rpc.go | 4 +- turbo/engineapi/engine_server.go | 70 ++++++++++++------- turbo/engineapi/engine_types/jsonrpc.go | 9 ++- turbo/engineapi/interface.go | 6 +- 4 files changed, 58 insertions(+), 31 deletions(-) diff --git a/cl/phase1/execution_client/execution_client_rpc.go b/cl/phase1/execution_client/execution_client_rpc.go index 000b62760a4..a29df8a15bd 100644 --- a/cl/phase1/execution_client/execution_client_rpc.go +++ b/cl/phase1/execution_client/execution_client_rpc.go @@ -204,7 +204,7 @@ func (cc *ExecutionClientRpc) Ready(ctx context.Context) (bool, error) { // GetBodiesByRange gets block bodies in given block range func (cc *ExecutionClientRpc) GetBodiesByRange(ctx context.Context, start, count uint64) ([]*types.RawBody, error) { - result := []*engine_types.ExecutionPayloadBodyV1{} + result := []*engine_types.ExecutionPayloadBody{} if err := cc.client.CallContext(ctx, &result, rpc_helper.GetPayloadBodiesByRangeV1, hexutil.Uint64(start), hexutil.Uint64(count)); err != nil { return nil, err @@ -223,7 +223,7 @@ func (cc *ExecutionClientRpc) GetBodiesByRange(ctx context.Context, start, count // GetBodiesByHashes gets block bodies with given hashes func (cc *ExecutionClientRpc) GetBodiesByHashes(ctx context.Context, hashes []libcommon.Hash) ([]*types.RawBody, error) { - result := []*engine_types.ExecutionPayloadBodyV1{} + result := []*engine_types.ExecutionPayloadBody{} if err := cc.client.CallContext(ctx, &result, rpc_helper.GetPayloadBodiesByHashV1, hashes); err != nil { return nil, err diff --git a/turbo/engineapi/engine_server.go b/turbo/engineapi/engine_server.go index f15921997d7..fbe700d2a32 100644 --- a/turbo/engineapi/engine_server.go +++ b/turbo/engineapi/engine_server.go @@ -207,7 +207,9 @@ func (s *EngineServer) newPayload(ctx context.Context, req *engine_types.Executi } if (!s.config.IsCancun(header.Time) && version >= clparams.DenebVersion) || - (s.config.IsCancun(header.Time) && version < clparams.DenebVersion) { + (s.config.IsCancun(header.Time) && version < clparams.DenebVersion) || + (!s.config.IsPrague(header.Time) && version >= clparams.ElectraVersion) || + (s.config.IsPrague(header.Time) && version < clparams.ElectraVersion) { return nil, &rpc.UnsupportedForkError{Message: "Unsupported fork"} } @@ -447,7 +449,9 @@ func (s *EngineServer) getPayload(ctx context.Context, payloadId uint64, version ts := data.ExecutionPayload.Timestamp if (!s.config.IsCancun(ts) && version >= clparams.DenebVersion) || - (s.config.IsCancun(ts) && version < clparams.DenebVersion) { + (s.config.IsCancun(ts) && version < clparams.DenebVersion) || + (!s.config.IsPrague(ts) && version >= clparams.ElectraVersion) || + (s.config.IsPrague(ts) && version < clparams.ElectraVersion) { return nil, &rpc.UnsupportedForkError{Message: "Unsupported fork"} } @@ -551,20 +555,23 @@ func (s *EngineServer) forkchoiceUpdated(ctx context.Context, forkchoiceState *e }, nil } -func (s *EngineServer) getPayloadBodiesByHash(ctx context.Context, request []libcommon.Hash, _ clparams.StateVersion) ([]*engine_types.ExecutionPayloadBodyV1, error) { +func (s *EngineServer) getPayloadBodiesByHash(ctx context.Context, request []libcommon.Hash, version clparams.StateVersion) ([]*engine_types.ExecutionPayloadBody, error) { + if len(request) > 1024 { + return nil, &engine_helpers.TooLargeRequestErr + } bodies, err := s.chainRW.GetBodiesByHashes(ctx, request) if err != nil { return nil, err } - resp := make([]*engine_types.ExecutionPayloadBodyV1, len(bodies)) - for idx, body := range bodies { - resp[idx] = extractPayloadBodyFromBody(body) + resp := make([]*engine_types.ExecutionPayloadBody, len(bodies)) + for i, body := range bodies { + resp[i] = extractPayloadBodyFromBody(body, version) } return resp, nil } -func extractPayloadBodyFromBody(body *types.RawBody) *engine_types.ExecutionPayloadBodyV1 { +func extractPayloadBodyFromBody(body *types.RawBody, version clparams.StateVersion) *engine_types.ExecutionPayloadBody { if body == nil { return nil } @@ -574,18 +581,30 @@ func extractPayloadBodyFromBody(body *types.RawBody) *engine_types.ExecutionPayl bdTxs[idx] = body.Transactions[idx] } - return &engine_types.ExecutionPayloadBodyV1{Transactions: bdTxs, Withdrawals: body.Withdrawals} + ret := &engine_types.ExecutionPayloadBody{Transactions: bdTxs, Withdrawals: body.Withdrawals} + if version >= clparams.ElectraVersion && body.Requests != nil { + ret.DepositRequests = body.Requests.Deposits() + ret.WithdrawalRequests = body.Requests.Withdrawals() + ret.ConsolidationRequests = body.Requests.Consolidations() + } + return ret } -func (s *EngineServer) getPayloadBodiesByRange(ctx context.Context, start, count uint64, _ clparams.StateVersion) ([]*engine_types.ExecutionPayloadBodyV1, error) { +func (s *EngineServer) getPayloadBodiesByRange(ctx context.Context, start, count uint64, version clparams.StateVersion) ([]*engine_types.ExecutionPayloadBody, error) { + if start == 0 || count == 0 { + return nil, &rpc.InvalidParamsError{Message: fmt.Sprintf("invalid start or count, start: %v count: %v", start, count)} + } + if count > 1024 { + return nil, &engine_helpers.TooLargeRequestErr + } bodies, err := s.chainRW.GetBodiesByRange(ctx, start, count) if err != nil { return nil, err } - resp := make([]*engine_types.ExecutionPayloadBodyV1, len(bodies)) + resp := make([]*engine_types.ExecutionPayloadBody, len(bodies)) for idx, body := range bodies { - resp[idx] = extractPayloadBodyFromBody(body) + resp[idx] = extractPayloadBodyFromBody(body, version) } return resp, nil } @@ -700,27 +719,28 @@ func (e *EngineServer) ExchangeTransitionConfigurationV1(ctx context.Context, be // Returns an array of execution payload bodies referenced by their block hashes // See https://github.com/ethereum/execution-apis/blob/main/src/engine/shanghai.md#engine_getpayloadbodiesbyhashv1 -func (e *EngineServer) GetPayloadBodiesByHashV1(ctx context.Context, hashes []libcommon.Hash) ([]*engine_types.ExecutionPayloadBodyV1, error) { - if len(hashes) > 1024 { - return nil, &engine_helpers.TooLargeRequestErr - } - +func (e *EngineServer) GetPayloadBodiesByHashV1(ctx context.Context, hashes []libcommon.Hash) ([]*engine_types.ExecutionPayloadBody, error) { return e.getPayloadBodiesByHash(ctx, hashes, clparams.DenebVersion) } +// Returns an array of execution payload bodies referenced by their block hashes +// See https://github.com/ethereum/execution-apis/blob/main/src/engine/prague.md#engine_getpayloadbodiesbyhashv2 +func (e *EngineServer) GetPayloadBodiesByHashV2(ctx context.Context, hashes []libcommon.Hash) ([]*engine_types.ExecutionPayloadBody, error) { + return e.getPayloadBodiesByHash(ctx, hashes, clparams.ElectraVersion) +} + // Returns an ordered (as per canonical chain) array of execution payload bodies, with corresponding execution block numbers from "start", up to "count" // See https://github.com/ethereum/execution-apis/blob/main/src/engine/shanghai.md#engine_getpayloadbodiesbyrangev1 -func (e *EngineServer) GetPayloadBodiesByRangeV1(ctx context.Context, start, count hexutil.Uint64) ([]*engine_types.ExecutionPayloadBodyV1, error) { - if start == 0 || count == 0 { - return nil, &rpc.InvalidParamsError{Message: fmt.Sprintf("invalid start or count, start: %v count: %v", start, count)} - } - if count > 1024 { - return nil, &engine_helpers.TooLargeRequestErr - } - +func (e *EngineServer) GetPayloadBodiesByRangeV1(ctx context.Context, start, count hexutil.Uint64) ([]*engine_types.ExecutionPayloadBody, error) { return e.getPayloadBodiesByRange(ctx, uint64(start), uint64(count), clparams.CapellaVersion) } +// Returns an ordered (as per canonical chain) array of execution payload bodies, with corresponding execution block numbers from "start", up to "count" +// See https://github.com/ethereum/execution-apis/blob/main/src/engine/prague.md#engine_getpayloadbodiesbyrangev2 +func (e *EngineServer) GetPayloadBodiesByRangeV2(ctx context.Context, start, count hexutil.Uint64) ([]*engine_types.ExecutionPayloadBody, error) { + return e.getPayloadBodiesByRange(ctx, uint64(start), uint64(count), clparams.ElectraVersion) +} + var ourCapabilities = []string{ "engine_forkchoiceUpdatedV1", "engine_forkchoiceUpdatedV2", @@ -735,7 +755,9 @@ var ourCapabilities = []string{ "engine_getPayloadV4", "engine_exchangeTransitionConfigurationV1", "engine_getPayloadBodiesByHashV1", + "engine_getPayloadBodiesByHashV2", "engine_getPayloadBodiesByRangeV1", + "engine_getPayloadBodiesByRangeV2", } func (e *EngineServer) ExchangeCapabilities(fromCl []string) []string { diff --git a/turbo/engineapi/engine_types/jsonrpc.go b/turbo/engineapi/engine_types/jsonrpc.go index c8c41a77914..61de0d613eb 100644 --- a/turbo/engineapi/engine_types/jsonrpc.go +++ b/turbo/engineapi/engine_types/jsonrpc.go @@ -69,9 +69,12 @@ type BlobsBundleV1 struct { Blobs []hexutility.Bytes `json:"blobs" gencodec:"required"` } -type ExecutionPayloadBodyV1 struct { - Transactions []hexutility.Bytes `json:"transactions" gencodec:"required"` - Withdrawals []*types.Withdrawal `json:"withdrawals" gencodec:"required"` +type ExecutionPayloadBody struct { + Transactions []hexutility.Bytes `json:"transactions" gencodec:"required"` + Withdrawals []*types.Withdrawal `json:"withdrawals" gencodec:"required"` + DepositRequests types.DepositRequests `json:"depositRequests"` + WithdrawalRequests types.WithdrawalRequests `json:"withdrawalRequests"` + ConsolidationRequests types.ConsolidationRequests `json:"consolidationRequests"` } type PayloadStatus struct { diff --git a/turbo/engineapi/interface.go b/turbo/engineapi/interface.go index 6fc7b1127b0..7b1bd635a22 100644 --- a/turbo/engineapi/interface.go +++ b/turbo/engineapi/interface.go @@ -22,6 +22,8 @@ type EngineAPI interface { GetPayloadV3(ctx context.Context, payloadID hexutility.Bytes) (*engine_types.GetPayloadResponse, error) GetPayloadV4(ctx context.Context, payloadID hexutility.Bytes) (*engine_types.GetPayloadResponse, error) ExchangeTransitionConfigurationV1(ctx context.Context, transitionConfiguration *engine_types.TransitionConfiguration) (*engine_types.TransitionConfiguration, error) - GetPayloadBodiesByHashV1(ctx context.Context, hashes []common.Hash) ([]*engine_types.ExecutionPayloadBodyV1, error) - GetPayloadBodiesByRangeV1(ctx context.Context, start, count hexutil.Uint64) ([]*engine_types.ExecutionPayloadBodyV1, error) + GetPayloadBodiesByHashV1(ctx context.Context, hashes []common.Hash) ([]*engine_types.ExecutionPayloadBody, error) + GetPayloadBodiesByHashV2(ctx context.Context, hashes []common.Hash) ([]*engine_types.ExecutionPayloadBody, error) + GetPayloadBodiesByRangeV1(ctx context.Context, start, count hexutil.Uint64) ([]*engine_types.ExecutionPayloadBody, error) + GetPayloadBodiesByRangeV2(ctx context.Context, start, count hexutil.Uint64) ([]*engine_types.ExecutionPayloadBody, error) }