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

feat: implement EIP-7742 #7232

Draft
wants to merge 2 commits into
base: unstable
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ export async function verifyBlockExecutionPayload(
const parentBlockRoot = ForkSeq[fork] >= ForkSeq.deneb ? block.message.parentRoot : undefined;
const executionRequests =
ForkSeq[fork] >= ForkSeq.electra ? (block.message.body as electra.BeaconBlockBody).executionRequests : undefined;
const targetBlobsPerBlock = ForkSeq[fork] >= ForkSeq.electra ? 0 : undefined;

const logCtx = {slot: block.message.slot, executionBlock: executionPayloadEnabled.blockNumber};
chain.logger.debug("Call engine api newPayload", logCtx);
Expand All @@ -312,7 +313,8 @@ export async function verifyBlockExecutionPayload(
executionPayloadEnabled,
versionedHashes,
parentBlockRoot,
executionRequests
executionRequests,
targetBlobsPerBlock
);
chain.logger.debug("Receive engine api newPayload result", {...logCtx, status: execResult.status});

Expand Down
4 changes: 4 additions & 0 deletions packages/beacon-node/src/chain/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export type IChainOptions = BlockProcessOpts &
archiveBlobEpochs?: number;
nHistoricalStates?: boolean;
nHistoricalStatesFileDataStore?: boolean;
targetBlobsPerBlock?: number;
maxBlobsPerBlock?: number;
};

export type BlockProcessOpts = {
Expand Down Expand Up @@ -119,4 +121,6 @@ export const defaultChainOptions: IChainOptions = {
nHistoricalStatesFileDataStore: false,
maxBlockStates: DEFAULT_MAX_BLOCK_STATES,
maxCPStateEpochsInMemory: DEFAULT_MAX_CP_STATE_EPOCHS_IN_MEMORY,
targetBlobsPerBlock: 4, // This is arbitrary as we have not agreed on an appropriate value yet
maxBlobsPerBlock: 8, // This is arbitrary as we have not agreed on an appropriate value yet
};
11 changes: 11 additions & 0 deletions packages/beacon-node/src/chain/produceBlock/produceBlockBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {numToQuantity} from "../../eth1/provider/utils.js";
import {IExecutionBuilder, IExecutionEngine, PayloadAttributes, PayloadId} from "../../execution/index.js";
import type {BeaconChain} from "../chain.js";
import {CommonBlockBody} from "../interface.js";
import {IChainOptions} from "../options.js";
import {validateBlobsAndKzgCommitments} from "./validateBlobsAndKzgCommitments.js";

// Time to provide the EL to generate a payload from new payload id
Expand Down Expand Up @@ -375,6 +376,7 @@ export async function prepareExecutionPayload(
eth1: IEth1ForBlockProduction;
executionEngine: IExecutionEngine;
config: ChainForkConfig;
opts: IChainOptions;
},
logger: Logger,
fork: ForkExecution,
Expand Down Expand Up @@ -516,6 +518,7 @@ export async function getPayloadAttributesForSSE(
chain: {
eth1: IEth1ForBlockProduction;
config: ChainForkConfig;
opts: IChainOptions;
},
{
prepareState,
Expand Down Expand Up @@ -553,6 +556,7 @@ function preparePayloadAttributes(
fork: ForkExecution,
chain: {
config: ChainForkConfig;
opts: IChainOptions;
},
{
prepareState,
Expand Down Expand Up @@ -586,6 +590,13 @@ function preparePayloadAttributes(
(payloadAttributes as deneb.SSEPayloadAttributes["payloadAttributes"]).parentBeaconBlockRoot = parentBlockRoot;
}

if (ForkSeq[fork] >= ForkSeq.electra) {
(payloadAttributes as electra.SSEPayloadAttributes["payloadAttributes"]).targetBlobsPerBlock =
chain.opts.targetBlobsPerBlock ?? 4;
(payloadAttributes as electra.SSEPayloadAttributes["payloadAttributes"]).maxBlobsPerBlock =
chain.opts.maxBlobsPerBlock ?? 8;
}

return payloadAttributes;
}

Expand Down
20 changes: 14 additions & 6 deletions packages/beacon-node/src/execution/engine/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ export class ExecutionEngineHttp implements IExecutionEngine {
executionPayload: ExecutionPayload,
versionedHashes?: VersionedHashes,
parentBlockRoot?: Root,
executionRequests?: ExecutionRequests
executionRequests?: ExecutionRequests,
maxBlobsPerBlock?: number
): Promise<ExecutePayloadResponse> {
const method =
ForkSeq[fork] >= ForkSeq.electra
Expand Down Expand Up @@ -230,14 +231,19 @@ export class ExecutionEngineHttp implements IExecutionEngine {
if (executionRequests === undefined) {
throw Error(`executionRequests required in notifyNewPayload for fork=${fork}`);
}
if (maxBlobsPerBlock === undefined) {
throw Error(`maxBlobsPerBlock required in notifyNewPayload for fork=${fork}`);
}
const serializedExecutionRequests = serializeExecutionRequests(executionRequests);

engineRequest = {
method: "engine_newPayloadV4",
params: [
serializedExecutionPayload,
serializedVersionedHashes,
parentBeaconBlockRoot,
serializedExecutionRequests,
numToQuantity(maxBlobsPerBlock),
],
methodOpts: notifyNewPayloadOpts,
};
Expand Down Expand Up @@ -341,11 +347,13 @@ export class ExecutionEngineHttp implements IExecutionEngine {
// Once on capella, should this need to be permanently switched to v2 when payload attrs
// not provided
const method =
ForkSeq[fork] >= ForkSeq.deneb
? "engine_forkchoiceUpdatedV3"
: ForkSeq[fork] >= ForkSeq.capella
? "engine_forkchoiceUpdatedV2"
: "engine_forkchoiceUpdatedV1";
ForkSeq[fork] >= ForkSeq.electra
? "engine_forkchoiceUpdatedV4"
: ForkSeq[fork] >= ForkSeq.deneb
? "engine_forkchoiceUpdatedV3"
: ForkSeq[fork] >= ForkSeq.capella
? "engine_forkchoiceUpdatedV2"
: "engine_forkchoiceUpdatedV1";
const payloadAttributesRpc = payloadAttributes ? serializePayloadAttributes(payloadAttributes) : undefined;
// If we are just fcUing and not asking execution for payload, retry is not required
// and we can move on, as the next fcU will be issued soon on the new slot
Expand Down
5 changes: 4 additions & 1 deletion packages/beacon-node/src/execution/engine/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ export type PayloadAttributes = {
suggestedFeeRecipient: string;
withdrawals?: capella.Withdrawal[];
parentBeaconBlockRoot?: Uint8Array;
targetBlobsPerBlock?: number;
maxBlobsperBlock?: number;
};

export type BlobsBundle = {
Expand Down Expand Up @@ -135,7 +137,8 @@ export interface IExecutionEngine {
executionPayload: ExecutionPayload,
versionedHashes?: VersionedHashes,
parentBeaconBlockRoot?: Root,
executionRequests?: ExecutionRequests
executionRequests?: ExecutionRequests,
targetBlobsPerBlock?: number
): Promise<ExecutePayloadResponse>;

/**
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/execution/engine/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export class ExecutionEngineMockBackend implements JsonRpcBackend {
engine_forkchoiceUpdatedV1: this.notifyForkchoiceUpdate.bind(this),
engine_forkchoiceUpdatedV2: this.notifyForkchoiceUpdate.bind(this),
engine_forkchoiceUpdatedV3: this.notifyForkchoiceUpdate.bind(this),
engine_forkchoiceUpdatedV4: this.notifyForkchoiceUpdate.bind(this),
engine_getPayloadV1: this.getPayload.bind(this),
engine_getPayloadV2: this.getPayload.bind(this),
engine_getPayloadV3: this.getPayload.bind(this),
Expand Down
18 changes: 17 additions & 1 deletion packages/beacon-node/src/execution/engine/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export type EngineApiRpcParamTypes = {
engine_newPayloadV1: [ExecutionPayloadRpc];
engine_newPayloadV2: [ExecutionPayloadRpc];
engine_newPayloadV3: [ExecutionPayloadRpc, VersionedHashesRpc, DATA];
engine_newPayloadV4: [ExecutionPayloadRpc, VersionedHashesRpc, DATA, ExecutionRequestsRpc];
engine_newPayloadV4: [ExecutionPayloadRpc, VersionedHashesRpc, DATA, ExecutionRequestsRpc, QUANTITY];
/**
* 1. Object - Payload validity status with respect to the consensus rules:
* - blockHash: DATA, 32 Bytes - block hash value of the payload
Expand All @@ -45,6 +45,10 @@ export type EngineApiRpcParamTypes = {
forkChoiceData: {headBlockHash: DATA; safeBlockHash: DATA; finalizedBlockHash: DATA},
payloadAttributes?: PayloadAttributesRpc,
];
engine_forkchoiceUpdatedV4: [
forkChoiceData: {headBlockHash: DATA; safeBlockHash: DATA; finalizedBlockHash: DATA},
payloadAttributes?: PayloadAttributesRpc,
];
/**
* 1. payloadId: QUANTITY, 64 Bits - Identifier of the payload building process
*/
Expand Down Expand Up @@ -99,6 +103,10 @@ export type EngineApiRpcReturnTypes = {
payloadStatus: PayloadStatus;
payloadId: QUANTITY | null;
};
engine_forkchoiceUpdatedV4: {
payloadStatus: PayloadStatus;
payloadId: QUANTITY | null;
};
/**
* payloadId | Error: QUANTITY, 64 Bits - Identifier of the payload building process
*/
Expand Down Expand Up @@ -155,6 +163,7 @@ export type ExecutionPayloadRpc = {
blobGasUsed?: QUANTITY; // DENEB
excessBlobGas?: QUANTITY; // DENEB
parentBeaconBlockRoot?: QUANTITY; // DENEB
targetBlobsPerBlock?: QUANTITY; // ELECTRA
};

export type WithdrawalRpc = {
Expand Down Expand Up @@ -193,6 +202,9 @@ export type PayloadAttributesRpc = {
withdrawals?: WithdrawalRpc[];
/** DATA, 32 Bytes - value for the parentBeaconBlockRoot to be used for building block */
parentBeaconBlockRoot?: DATA;
// TODO
targetBlobsPerBlock?: QUANTITY;
maxBlobsPerBlock?: QUANTITY;
};

export type ClientVersionRpc = {
Expand Down Expand Up @@ -348,6 +360,8 @@ export function serializePayloadAttributes(data: PayloadAttributes): PayloadAttr
suggestedFeeRecipient: data.suggestedFeeRecipient,
withdrawals: data.withdrawals?.map(serializeWithdrawal),
parentBeaconBlockRoot: data.parentBeaconBlockRoot ? bytesToData(data.parentBeaconBlockRoot) : undefined,
targetBlobsPerBlock: data.targetBlobsPerBlock ? numToQuantity(data.targetBlobsPerBlock) : undefined,
maxBlobsPerBlock: data.maxBlobsperBlock ? numToQuantity(data.maxBlobsperBlock) : undefined,
};
}

Expand All @@ -364,6 +378,8 @@ export function deserializePayloadAttributes(data: PayloadAttributesRpc): Payloa
suggestedFeeRecipient: data.suggestedFeeRecipient,
withdrawals: data.withdrawals?.map((withdrawal) => deserializeWithdrawal(withdrawal)),
parentBeaconBlockRoot: data.parentBeaconBlockRoot ? dataToBytes(data.parentBeaconBlockRoot, 32) : undefined,
targetBlobsPerBlock: data.targetBlobsPerBlock ? quantityToNum(data.targetBlobsPerBlock) : undefined,
maxBlobsperBlock: data.maxBlobsPerBlock ? quantityToNum(data.maxBlobsPerBlock) : undefined,
};
}

Expand Down
20 changes: 20 additions & 0 deletions packages/cli/src/options/beaconNodeOptions/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ export type ChainArgs = {
"chain.nHistoricalStatesFileDataStore"?: boolean;
"chain.maxBlockStates"?: number;
"chain.maxCPStateEpochsInMemory"?: number;
"chain.targetBlobsPerBlock"?: number;
"chain.maxBlobsPerBlock"?: number;
};

export function parseArgs(args: ChainArgs): IBeaconNodeOptions["chain"] {
Expand Down Expand Up @@ -68,6 +70,8 @@ export function parseArgs(args: ChainArgs): IBeaconNodeOptions["chain"] {
args["chain.nHistoricalStatesFileDataStore"] ?? defaultOptions.chain.nHistoricalStatesFileDataStore,
maxBlockStates: args["chain.maxBlockStates"] ?? defaultOptions.chain.maxBlockStates,
maxCPStateEpochsInMemory: args["chain.maxCPStateEpochsInMemory"] ?? defaultOptions.chain.maxCPStateEpochsInMemory,
targetBlobsPerBlock: args["chain.targetBlobsPerBlock"] ?? defaultOptions.chain.targetBlobsPerBlock,
maxBlobsPerBlock: args["chain.maxBlobsPerBlock"] ?? defaultOptions.chain.maxBlobsPerBlock,
};
}

Expand Down Expand Up @@ -284,4 +288,20 @@ Will double processing times. Use only for debugging purposes.",
default: defaultOptions.chain.maxCPStateEpochsInMemory,
group: "chain",
},

"chain.targetBlobsPerBlock": {
Copy link
Member

Choose a reason for hiding this comment

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

these values will be part of spec config

hidden: true,
description: "Target blobs per block per EIP-7742",
type: "number",
default: defaultOptions.chain.targetBlobsPerBlock,
group: "chain",
},

"chain.maxBlobsPerBlock": {
hidden: true,
description: "Max blobs per block per EIP-7742",
type: "number",
default: defaultOptions.chain.maxBlobsPerBlock,
group: "chain",
},
};
5 changes: 3 additions & 2 deletions packages/types/src/electra/sszTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,9 @@ export const LightClientStore = new ContainerType(
// PayloadAttributes primarily for SSE event
export const PayloadAttributes = new ContainerType(
{
...capellaSsz.PayloadAttributes.fields,
parentBeaconBlockRoot: Root,
...denebSsz.PayloadAttributes.fields,
targetBlobsPerBlock: UintNum64,
maxBlobsPerBlock: UintNum64,
},
{typeName: "PayloadAttributes", jsonCase: "eth2"}
);
Expand Down
Loading