Skip to content

Commit

Permalink
engineapi: Add pectra engine api changes (#12172)
Browse files Browse the repository at this point in the history
Cherry pick  #10963

Co-authored-by: Somnath <[email protected]>
  • Loading branch information
2 people authored and AskAlexSharov committed Oct 21, 2024
1 parent 72737b1 commit 8656f6d
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
4 changes: 2 additions & 2 deletions cl/phase1/execution_client/execution_client_rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
70 changes: 46 additions & 24 deletions turbo/engineapi/engine_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}
}

Expand Down Expand Up @@ -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"}
}

Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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",
Expand All @@ -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 {
Expand Down
9 changes: 6 additions & 3 deletions turbo/engineapi/engine_types/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 4 additions & 2 deletions turbo/engineapi/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 8656f6d

Please sign in to comment.