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

refactor: flatten fork logic in notifyNewPayload #7385

Open
wants to merge 3 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 77 additions & 51 deletions packages/beacon-node/src/execution/engine/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ import {PayloadIdCache} from "./payloadIdCache.js";
import {
EngineApiRpcParamTypes,
EngineApiRpcReturnTypes,
EngineNewPayloadMethod,
ExecutionPayloadBody,
assertReqSizeLimit,
deserializeBlobAndProofs,
deserializeExecutionPayloadBody,
getPayloadMethodByFork,
newPayloadMethodByFork,
parseExecutionPayload,
serializeBeaconBlockRoot,
serializeExecutionPayload,
Expand Down Expand Up @@ -204,58 +207,44 @@ export class ExecutionEngineHttp implements IExecutionEngine {
parentBlockRoot?: Root,
executionRequests?: ExecutionRequests
): Promise<ExecutePayloadResponse> {
const method =
ForkSeq[fork] >= ForkSeq.electra
? "engine_newPayloadV4"
: ForkSeq[fork] >= ForkSeq.deneb
? "engine_newPayloadV3"
: ForkSeq[fork] >= ForkSeq.capella
? "engine_newPayloadV2"
: "engine_newPayloadV1";

const serializedExecutionPayload = serializeExecutionPayload(fork, executionPayload);
const method = newPayloadMethodByFork(fork);

let engineRequest: EngineRequest;
if (ForkSeq[fork] >= ForkSeq.deneb) {
if (versionedHashes === undefined) {
throw Error(`versionedHashes required in notifyNewPayload for fork=${fork}`);
}
if (parentBlockRoot === undefined) {
throw Error(`parentBlockRoot required in notifyNewPayload for fork=${fork}`);
}

const serializedVersionedHashes = serializeVersionedHashes(versionedHashes);
const parentBeaconBlockRoot = serializeBeaconBlockRoot(parentBlockRoot);

if (ForkSeq[fork] >= ForkSeq.electra) {
if (executionRequests === undefined) {
throw Error(`executionRequests required in notifyNewPayload for fork=${fork}`);
}
const serializedExecutionRequests = serializeExecutionRequests(executionRequests);
switch (method) {
case "engine_newPayloadV1":
case "engine_newPayloadV2":
engineRequest = {
method: "engine_newPayloadV4",
params: [
serializedExecutionPayload,
serializedVersionedHashes,
parentBeaconBlockRoot,
serializedExecutionRequests,
],
method,
params: generateSerializedNewPayloadParams(fork, method, {executionPayload}),
Copy link
Member

@nflaig nflaig Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while this looks much cleaner here, we lose a lot of type safety

eg. it's possible to call it like this without complaints

params: generateSerializedNewPayloadParams(fork, method, {}),

and then you will just notice this at runtime

methodOpts: notifyNewPayloadOpts,
};
} else {
break;
case "engine_newPayloadV3":
engineRequest = {
method: "engine_newPayloadV3",
params: [serializedExecutionPayload, serializedVersionedHashes, parentBeaconBlockRoot],
method,
params: generateSerializedNewPayloadParams(fork, method, {
executionPayload,
versionedHashes,
parentBlockRoot,
}),
methodOpts: notifyNewPayloadOpts,
};
}
} else {
const method = ForkSeq[fork] >= ForkSeq.capella ? "engine_newPayloadV2" : "engine_newPayloadV1";
engineRequest = {
method,
params: [serializedExecutionPayload],
methodOpts: notifyNewPayloadOpts,
};
break;
case "engine_newPayloadV4":
engineRequest = {
method,
params: generateSerializedNewPayloadParams(fork, method, {
executionPayload,
versionedHashes,
parentBlockRoot,
executionRequests,
}),
methodOpts: notifyNewPayloadOpts,
};
break;
default:
throw Error(`Unsupported method: ${method}`);
}

const {status, latestValidHash, validationError} = await (
Expand Down Expand Up @@ -415,14 +404,7 @@ export class ExecutionEngineHttp implements IExecutionEngine {
executionRequests?: ExecutionRequests;
shouldOverrideBuilder?: boolean;
}> {
const method =
ForkSeq[fork] >= ForkSeq.electra
? "engine_getPayloadV4"
: ForkSeq[fork] >= ForkSeq.deneb
? "engine_getPayloadV3"
: ForkSeq[fork] >= ForkSeq.capella
? "engine_getPayloadV2"
: "engine_getPayloadV1";
const method = getPayloadMethodByFork(fork);
const payloadResponse = await this.rpc.fetchWithRetries<
EngineApiRpcReturnTypes[typeof method],
EngineApiRpcParamTypes[typeof method]
Expand Down Expand Up @@ -578,3 +560,47 @@ type EngineRequestByKey = {
type EngineRequest = EngineRequestByKey[EngineRequestKey];
type EngineResponseByKey = {[K in EngineRequestKey]: EngineApiRpcReturnTypes[K]};
type EngineResponse = EngineResponseByKey[EngineRequestKey];

/**
*
* Generate serialized params according to `method`.
*
* `params` must not allow numeric keys because we rely Object.keys() to return keys in insertion order.
* eg. {versionedHashes, parentBlockRoot, executionRequests} has to return [versionedHashes, parentBlockRoot, executionRequests]
* Having numeric key will not guarantee keys in insertion order.
*/
function generateSerializedNewPayloadParams<T extends EngineNewPayloadMethod>(
fork: ForkName,
method: T,
params: Record<Exclude<string, `${number}`>, unknown>
): EngineApiRpcParamTypes[T] {
for (const [key, value] of Object.entries(params)) {
if (value === undefined) {
throw new Error(`${key} is required for method=${method} in fork=${fork}`);
}
}

const keys = Object.keys(params);
const result = [];

for (const key of keys) {
switch (key) {
case "executionPayload":
result.push(serializeExecutionPayload(fork, params[key] as ExecutionPayload));
break;
case "versionedHashes":
result.push(serializeVersionedHashes(params[key] as VersionedHashes));
break;
case "parentBlockRoot":
result.push(serializeBeaconBlockRoot(params[key] as Root));
break;
case "executionRequests":
result.push(serializeExecutionRequests(params[key] as ExecutionRequests));
break;
default:
result.push(params[key]);
}
}

return result as EngineApiRpcParamTypes[T];
}
32 changes: 32 additions & 0 deletions packages/beacon-node/src/execution/engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
ForkName,
ForkSeq,
WITHDRAWAL_REQUEST_TYPE,
isForkBlobs,
isForkPostElectra,
isForkWithdrawals,
} from "@lodestar/params";
import {ExecutionPayload, ExecutionRequests, Root, Wei, bellatrix, capella, deneb, electra, ssz} from "@lodestar/types";
import {BlobAndProof} from "@lodestar/types/deneb";
Expand Down Expand Up @@ -82,6 +85,9 @@ export type EngineApiRpcParamTypes = {
engine_getBlobsV1: [DATA[]];
};

export type EngineGetPayloadMethod = keyof EngineApiRpcParamTypes & `engine_getPayloadV${number}`;
export type EngineNewPayloadMethod = keyof EngineApiRpcParamTypes & `engine_newPayloadV${number}`;

export type PayloadStatus = {
status: ExecutionPayloadStatus;
latestValidHash: DATA | null;
Expand Down Expand Up @@ -222,6 +228,32 @@ export interface BlobsBundleRpc {
proofs: DATA[]; // some ELs could also provide proofs, each 48 bytes
}

export function getPayloadMethodByFork(fork: ForkName): EngineGetPayloadMethod {
switch (true) {
case isForkPostElectra(fork):
return "engine_getPayloadV4";
case isForkBlobs(fork):
return "engine_getPayloadV3";
case isForkWithdrawals(fork):
return "engine_getPayloadV2";
default:
return "engine_getPayloadV1";
}
}

export function newPayloadMethodByFork(fork: ForkName): EngineNewPayloadMethod {
switch (true) {
case isForkPostElectra(fork):
return "engine_newPayloadV4";
case isForkBlobs(fork):
return "engine_newPayloadV3";
case isForkWithdrawals(fork):
return "engine_newPayloadV2";
default:
return "engine_newPayloadV1";
}
}

export function serializeExecutionPayload(fork: ForkName, data: ExecutionPayload): ExecutionPayloadRpc {
const payload: ExecutionPayloadRpc = {
parentHash: bytesToData(data.parentHash),
Expand Down
Loading