Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ export async function importExecutionPayload(
blockRootHex,
blockHashHex,
envelope.payload.blockNumber,
envelope.payload.gasLimit,
execStatus,
dataAvailabilityStatus
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export enum ExecutionPayloadBidErrorCode {
BID_TOO_HIGH = "EXECUTION_PAYLOAD_BID_ERROR_BID_TOO_HIGH",
TOO_MANY_KZG_COMMITMENTS = "EXECUTION_PAYLOAD_BID_ERROR_TOO_MANY_KZG_COMMITMENTS",
UNKNOWN_BLOCK_ROOT = "EXECUTION_PAYLOAD_BID_ERROR_UNKNOWN_BLOCK_ROOT",
UNKNOWN_PARENT_BLOCK_HASH = "EXECUTION_PAYLOAD_BID_ERROR_UNKNOWN_PARENT_BLOCK_HASH",
INVALID_SLOT = "EXECUTION_PAYLOAD_BID_ERROR_INVALID_SLOT",
INVALID_SIGNATURE = "EXECUTION_PAYLOAD_BID_ERROR_INVALID_SIGNATURE",
NO_MATCHING_PROPOSER_PREFERENCES = "EXECUTION_PAYLOAD_BID_ERROR_NO_MATCHING_PROPOSER_PREFERENCES",
Expand Down Expand Up @@ -38,6 +39,7 @@ export type ExecutionPayloadBidErrorType =
commitmentLimit: number;
}
| {code: ExecutionPayloadBidErrorCode.UNKNOWN_BLOCK_ROOT; parentBlockRoot: RootHex}
| {code: ExecutionPayloadBidErrorCode.UNKNOWN_PARENT_BLOCK_HASH; parentBlockHash: RootHex}
| {code: ExecutionPayloadBidErrorCode.INVALID_SLOT; builderIndex: BuilderIndex; slot: Slot}
| {code: ExecutionPayloadBidErrorCode.INVALID_SIGNATURE; builderIndex: BuilderIndex; slot: Slot}
| {
Expand All @@ -56,7 +58,8 @@ export type ExecutionPayloadBidErrorType =
code: ExecutionPayloadBidErrorCode.PROPOSER_PREFERENCES_GAS_LIMIT_MISMATCH;
builderIndex: BuilderIndex;
bidGasLimit: number;
expectedGasLimit: number;
parentGasLimit: number;
targetGasLimit: number;
};

export class ExecutionPayloadBidError extends GossipActionError<ExecutionPayloadBidErrorType> {}
18 changes: 14 additions & 4 deletions packages/beacon-node/src/chain/forkChoice/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ export function initializeForkChoiceFromFinalizedState(
executionPayloadBlockHash: isStatePostGloas(state)
? toRootHex(state.latestBlockHash)
: toRootHex(state.latestExecutionPayloadHeader.blockHash),
// TODO GLOAS: executionPayloadNumber is not tracked in BeaconState post-gloas (EIP-7732 removed
// latestExecutionPayloadHeader). Using 0 as unavailable fallback until a solution is found.
// TODO GLOAS: executionPayloadNumber/GasLimit are not tracked in BeaconState post-gloas
// (EIP-7732 removed latestExecutionPayloadHeader). Using 0 as unavailable fallback —
// see initializeForkChoiceFromUnfinalizedState for the same caveat on validation.
executionPayloadNumber: isStatePostGloas(state) ? 0 : state.payloadBlockNumber,
executionPayloadGasLimit: isStatePostGloas(state) ? 0 : state.latestExecutionPayloadHeader.gasLimit,
Comment on lines +143 to +147

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I am bit confused by this comment, why is it not tracked in the state?

it's in state.latestExecutionPayloadBid.gasLimit, or is there a reason this value is not viable to use?

executionStatus: blockHeader.slot === GENESIS_SLOT ? ExecutionStatus.Valid : ExecutionStatus.Syncing,
}
: {executionPayloadBlockHash: null, executionStatus: ExecutionStatus.PreMerge}),
Expand Down Expand Up @@ -232,9 +234,17 @@ export function initializeForkChoiceFromUnfinalizedState(
executionPayloadBlockHash: isStatePostGloas(unfinalizedState)
? toRootHex(unfinalizedState.latestBlockHash)
: toRootHex(unfinalizedState.latestExecutionPayloadHeader.blockHash),
// TODO GLOAS: executionPayloadNumber is not tracked in BeaconState post-gloas (EIP-7732 removed
// latestExecutionPayloadHeader). Using 0 as unavailable fallback until a solution is found.
// TODO GLOAS: executionPayloadNumber/GasLimit are not tracked in BeaconState post-gloas
// (EIP-7732 removed latestExecutionPayloadHeader). Using 0 as unavailable fallback until
// a solution is found. The 0 doesn't gate validation in practice: at boot the head's
// PENDING variant's `executionPayloadBlockHash` is the *parent's* payload hash (per the
// PENDING/EMPTY convention), so gossip bids that reference the head's *own* payload
// hash won't match this variant anyway and will IGNORE until `onExecutionPayload`
// upgrades the head to FULL with real values.
executionPayloadNumber: isStatePostGloas(unfinalizedState) ? 0 : unfinalizedState.payloadBlockNumber,
executionPayloadGasLimit: isStatePostGloas(unfinalizedState)
? 0
: unfinalizedState.latestExecutionPayloadHeader.gasLimit,
executionStatus: blockHeader.slot === GENESIS_SLOT ? ExecutionStatus.Valid : ExecutionStatus.Syncing,
}
: {executionPayloadBlockHash: null, executionStatus: ExecutionStatus.PreMerge}),
Expand Down
40 changes: 24 additions & 16 deletions packages/beacon-node/src/chain/produceBlock/produceBlockBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import {
G2_POINT_AT_INFINITY,
IBeaconStateView,
type IBeaconStateViewBellatrix,
type IBeaconStateViewGloas,
computeEpochAtSlot,
computeTimeAtSlot,
getExpectedGasLimit,
isStatePostBellatrix,
isStatePostCapella,
isStatePostGloas,
Expand Down Expand Up @@ -53,13 +53,7 @@ import {
import {Logger, byteArrayEquals, fromHex, sleep, toHex, toPubkeyHex, toRootHex} from "@lodestar/utils";
import {ZERO_HASH_HEX} from "../../constants/index.js";
import {numToQuantity} from "../../execution/engine/utils.js";
import {
IExecutionBuilder,
IExecutionEngine,
PayloadAttributes,
PayloadId,
getExpectedGasLimit,
} from "../../execution/index.js";
import {IExecutionBuilder, IExecutionEngine, PayloadAttributes, PayloadId} from "../../execution/index.js";
import {getShufflingDependentRoot} from "../../util/dependentRoot.js";
import {fromGraffitiBytes} from "../../util/graffiti.js";
import {kzg} from "../../util/kzg.js";
Expand Down Expand Up @@ -869,9 +863,9 @@ function preparePayloadAttributes(
(payloadAttributes as gloas.SSEPayloadAttributes["payloadAttributes"]).slotNumber = prepareSlot;
(payloadAttributes as gloas.SSEPayloadAttributes["payloadAttributes"]).targetGasLimit = getProposerTargetGasLimit(
chain,
prepareState,
prepareSlot,
parentBlockRoot
parentBlockRoot,
parentBlockHash
);
}

Expand All @@ -886,14 +880,20 @@ function preparePayloadAttributes(
* (same `(slot, dependent_root)` lookup as gossip bid validation). When no matching
* preferences are pooled, target the parent payload's gas limit so the gas limit stays
* unchanged (`is_gas_limit_target_compatible` then requires `gas_limit == parent_gas_limit`).
*
* The parent payload's gas_limit is read from fork choice — the variant matching
* `(parentBlockRoot, parentBlockHash)` carries the correct value for both FULL parents
* (FULL.executionPayloadGasLimit = delivered payload's gas_limit) and EMPTY parents
* (EMPTY.executionPayloadGasLimit = inherited grandparent's gas_limit).
*/
function getProposerTargetGasLimit(
chain: {forkChoice: IForkChoice; proposerPreferencesPool: ProposerPreferencesPool},
state: IBeaconStateViewGloas,
prepareSlot: Slot,
parentBlockRoot: Root
parentBlockRoot: Root,
parentBlockHash: Bytes32
): number {
const parentBlock = chain.forkChoice.getBlockHexDefaultStatus(toRootHex(parentBlockRoot));
const parentBlockRootHex = toRootHex(parentBlockRoot);
const parentBlock = chain.forkChoice.getBlockHexDefaultStatus(parentBlockRootHex);
const dependentRootHex = (() => {
if (parentBlock === null) {
return null;
Expand All @@ -911,9 +911,17 @@ function getProposerTargetGasLimit(
})();

const pref = dependentRootHex !== null ? chain.proposerPreferencesPool.get(prepareSlot, dependentRootHex) : null;
// TODO GLOAS: state.latestExecutionPayloadBid is the latest *bid*, not the latest *executed*
// payload — for EMPTY parents this drifts. Consider having a default value like Prysm's DefaultBuilderGasLimit.
return Number(pref ? pref.message.targetGasLimit : state.latestExecutionPayloadBid.gasLimit);
if (pref !== null) {
return pref.message.targetGasLimit;
}

const parentPayloadVariant = chain.forkChoice.getBlockHexAndBlockHash(parentBlockRootHex, toRootHex(parentBlockHash));
if (parentPayloadVariant === null || parentPayloadVariant.executionPayloadBlockHash === null) {
throw new Error(
`Cannot resolve parent payload gas_limit for proposer targetGasLimit fallback parentBlockRoot=${parentBlockRootHex} parentBlockHash=${toRootHex(parentBlockHash)}`
);
}
return parentPayloadVariant.executionPayloadGasLimit;
}

export async function produceCommonBlockBody<T extends BlockType>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createSingleSignatureSetFromComponents,
getExecutionPayloadBidSigningRoot,
isActiveBuilder,
isGasLimitTargetCompatible,
isStatePostGloas,
} from "@lodestar/state-transition";
import {gloas} from "@lodestar/types";
Expand Down Expand Up @@ -128,14 +129,33 @@ async function validateExecutionPayloadBid(
});
}

// [REJECT] `bid.gas_limit == proposer_preferences.target_gas_limit`.
// [IGNORE] `bid.parent_block_hash` is the block hash of a known execution payload in fork
// choice. Looks up the variant of `bid.parent_block_root` whose payload hash matches
// `bid.parent_block_hash` — works for both FULL parents (FULL variant carries the delivered
// payload's hash) and EMPTY parents (EMPTY/PENDING variants carry the inherited parent
// payload's hash, since the new block doesn't have its own payload). Variant carries the
// executed payload's gas_limit, which we use as `parent_gas_limit` below.
const parentPayloadVariant = chain.forkChoice.getBlockHexAndBlockHash(parentBlockRootHex, parentBlockHashHex);
if (parentPayloadVariant === null || parentPayloadVariant.executionPayloadBlockHash === null) {
throw new ExecutionPayloadBidError(GossipAction.IGNORE, {
code: ExecutionPayloadBidErrorCode.UNKNOWN_PARENT_BLOCK_HASH,
parentBlockHash: parentBlockHashHex,
});
}

// [REJECT] `is_gas_limit_target_compatible(parent_gas_limit, bid.gas_limit, target_gas_limit)`,
// where `parent_gas_limit` is the `gas_limit` of the parent execution payload and
// `target_gas_limit` is `proposer_preferences.target_gas_limit`.
const bidGasLimit = Number(bid.gasLimit);
if (bidGasLimit !== proposerPreferences.message.targetGasLimit) {
const parentGasLimit = parentPayloadVariant.executionPayloadGasLimit;
const targetGasLimit = proposerPreferences.message.targetGasLimit;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

The targetGasLimit from proposerPreferences.message is likely a bigint (representing a uint64 in the spec). Since isGasLimitTargetCompatible expects a number, it is safer and more consistent with other parts of the codebase (e.g., produceBlockBody.ts) to explicitly convert it using Number().

Suggested change
const targetGasLimit = proposerPreferences.message.targetGasLimit;
const targetGasLimit = Number(proposerPreferences.message.targetGasLimit);

if (!isGasLimitTargetCompatible(parentGasLimit, bidGasLimit, targetGasLimit)) {
throw new ExecutionPayloadBidError(GossipAction.REJECT, {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

code: ExecutionPayloadBidErrorCode.PROPOSER_PREFERENCES_GAS_LIMIT_MISMATCH,
builderIndex: bid.builderIndex,
bidGasLimit,
expectedGasLimit: proposerPreferences.message.targetGasLimit,
parentGasLimit,
targetGasLimit,
});
}

Expand Down Expand Up @@ -183,10 +203,6 @@ async function validateExecutionPayloadBid(
});
}

// [IGNORE] `bid.parent_block_hash` is the block hash of a known execution
// payload in fork choice.
// TODO GLOAS: implement this

// [REJECT] `signed_execution_payload_bid.signature` is valid with respect to the `bid.builder_index`.
const signatureSet = createSingleSignatureSetFromComponents(
PublicKey.fromBytes(builder.pubkey),
Expand Down
5 changes: 1 addition & 4 deletions packages/beacon-node/src/execution/builder/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import {ChainForkConfig} from "@lodestar/config";
import {Logger} from "@lodestar/logger";
import {Metrics} from "../../metrics/metrics.js";
import {IExecutionBuilder} from "./interface.js";

export {getExpectedGasLimit} from "./utils.js";

import {ExecutionBuilderHttp, ExecutionBuilderHttpOpts, defaultExecutionBuilderHttpOpts} from "./http.js";
import {IExecutionBuilder} from "./interface.js";

export {ExecutionBuilderHttp, defaultExecutionBuilderHttpOpts};

Expand Down
19 changes: 0 additions & 19 deletions packages/beacon-node/src/execution/builder/utils.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ describe("sync / checkpoint sync optimistic flow for gloas", () => {
bn2Head.blockRoot,
nodeAHeadFull.executionPayloadBlockHash,
nodeAHeadFull.executionPayloadNumber,
nodeAHeadFull.executionPayloadGasLimit,
ExecutionStatus.Valid,
DataAvailabilityStatus.Available
);
Expand Down
2 changes: 2 additions & 0 deletions packages/beacon-node/test/spec/presets/fork_choice.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ const forkChoiceTest =
const beaconBlockRoot = toHex(envelope.message.beaconBlockRoot);
const blockHash = toHex(envelope.message.payload.blockHash);
const blockNumber = envelope.message.payload.blockNumber;
const gasLimit = envelope.message.payload.gasLimit;

// Verify envelope against the state
const protoBlock = chain.forkChoice.getBlockHexDefaultStatus(beaconBlockRoot);
Expand Down Expand Up @@ -433,6 +434,7 @@ const forkChoiceTest =
beaconBlockRoot,
blockHash,
blockNumber,
gasLimit,
ExecutionStatus.Valid,
DataAvailabilityStatus.Available
);
Expand Down
52 changes: 32 additions & 20 deletions packages/fork-choice/src/forkChoice/forkChoice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,40 +764,50 @@ export class ForkChoice implements IForkChoice {
unrealizedFinalizedRoot: unrealizedFinalizedCheckpoint.rootHex,

...(isGloasBeaconBlock(block)
? {
executionPayloadBlockHash: toRootHex(block.body.signedExecutionPayloadBid.message.parentBlockHash), // post-gloas, we don't know payload hash until we import execution payload. Set to parent payload hash for now
executionPayloadNumber: (() => {
// Determine parent's execution payload number based on which variant the block extends
const parentBlockHashFromBid = toRootHex(block.body.signedExecutionPayloadBid.message.parentBlockHash);

// If parent is pre-merge, return 0
? (() => {
// post-gloas, we don't know payload hash until we import execution payload. Set to
// parent payload hash for now, along with the gas limit/number of that parent payload
// (which is what bids built on top of this block will reference until a payload arrives).
const parentBlockHashFromBid = toRootHex(block.body.signedExecutionPayloadBid.message.parentBlockHash);

// Inherit parent payload's (number, gasLimit) for the PENDING/EMPTY variants:
// - pre-merge parent: zero/no payload
// - pre-Gloas parent: single variant carries the values
// - Gloas parent: look up the variant matching parentBlockHashFromBid
const parentMeta = ((): {number: number; gasLimit: number} => {
if (parentBlock.executionPayloadBlockHash === null) {
return 0;
return {number: 0, gasLimit: 0};
}

// If parent is pre-Gloas, it only has FULL variant
if (parentBlock.parentBlockHash === null) {
return parentBlock.executionPayloadNumber;
return {number: parentBlock.executionPayloadNumber, gasLimit: parentBlock.executionPayloadGasLimit};
}

// Parent is Gloas: get the variant that matches the parentBlockHash from bid
const parentVariant = this.getBlockHexAndBlockHash(parentRootHex, parentBlockHashFromBid);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this is not necessary
parentVariant is exactly the same to parentBlock at line 607 above

if (parentVariant && parentVariant.executionPayloadBlockHash !== null) {
return parentVariant.executionPayloadNumber;
return {
number: parentVariant.executionPayloadNumber,
gasLimit: parentVariant.executionPayloadGasLimit,
};
}
// Fallback to parent block's number (we know it's post-merge from check above)
return parentBlock.executionPayloadNumber;
})(),
executionStatus: this.getPostMergeExecStatus(executionStatus),
dataAvailabilityStatus,
}
// Fallback to parent block's values (we know it's post-merge from check above)
return {number: parentBlock.executionPayloadNumber, gasLimit: parentBlock.executionPayloadGasLimit};
})();

return {
executionPayloadBlockHash: parentBlockHashFromBid,
executionPayloadNumber: parentMeta.number,
executionPayloadGasLimit: parentMeta.gasLimit,
executionStatus: this.getPostMergeExecStatus(executionStatus),
dataAvailabilityStatus,
};
})()
: isExecutionBlockBodyType(block.body) &&
isStatePostBellatrix(state) &&
state.isExecutionStateType &&
state.isExecutionEnabled(block)
? {
executionPayloadBlockHash: toRootHex(block.body.executionPayload.blockHash),
executionPayloadNumber: block.body.executionPayload.blockNumber,
executionPayloadGasLimit: block.body.executionPayload.gasLimit,
executionStatus: this.getPostMergeExecStatus(executionStatus),
dataAvailabilityStatus,
}
Expand Down Expand Up @@ -947,6 +957,7 @@ export class ForkChoice implements IForkChoice {
blockRoot: RootHex,
executionPayloadBlockHash: RootHex,
executionPayloadNumber: number,
executionPayloadGasLimit: number,
executionStatus: PayloadExecutionStatus,
dataAvailabilityStatus: DataAvailabilityStatus
): void {
Expand All @@ -955,6 +966,7 @@ export class ForkChoice implements IForkChoice {
this.fcStore.currentSlot,
executionPayloadBlockHash,
executionPayloadNumber,
executionPayloadGasLimit,
this.proposerBoostRoot,
executionStatus,
dataAvailabilityStatus
Expand Down
2 changes: 2 additions & 0 deletions packages/fork-choice/src/forkChoice/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,13 @@ export interface IForkChoice {
* @param blockRoot - The beacon block root for which the payload arrived
* @param executionPayloadBlockHash - The block hash of the execution payload
* @param executionPayloadNumber - The block number of the execution payload
* @param executionPayloadGasLimit - The gas limit of the execution payload
*/
onExecutionPayload(
blockRoot: RootHex,
executionPayloadBlockHash: RootHex,
executionPayloadNumber: number,
executionPayloadGasLimit: number,
executionStatus: PayloadExecutionStatus,
dataAvailabilityStatus: DataAvailabilityStatus
): void;
Expand Down
8 changes: 8 additions & 0 deletions packages/fork-choice/src/protoArray/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,14 @@ export type BlockExtraMeta =
// - payload block hash for FULL variant
executionPayloadBlockHash: RootHex;
executionPayloadNumber: UintNum64;
// Gas limit of the executed payload identified by executionPayloadBlockHash. Set on
// pre-Gloas blocks (from block.body.executionPayload.gasLimit) and on Gloas variants:
// - PENDING/EMPTY: inherited from the parent payload that the bid commits to extend
// (matches executionPayloadBlockHash, which also points to that parent payload)
// - FULL: the actual delivered payload's gasLimit (set in onExecutionPayload)
// Consumers (e.g. Gloas bid gas-limit validation) can read this without re-deriving from
// state.
executionPayloadGasLimit: UintNum64;
executionStatus: Exclude<ExecutionStatus, ExecutionStatus.PreMerge>;
dataAvailabilityStatus: DataAvailabilityStatus;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/fork-choice/src/protoArray/protoArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ export class ProtoArray {
currentSlot: Slot,
executionPayloadBlockHash: RootHex,
executionPayloadNumber: number,
executionPayloadGasLimit: number,
proposerBoostRoot: RootHex | null,
executionStatus: PayloadExecutionStatus,
dataAvailabilityStatus: DataAvailabilityStatus
Expand Down Expand Up @@ -613,6 +614,7 @@ export class ProtoArray {
executionStatus,
executionPayloadBlockHash,
executionPayloadNumber,
executionPayloadGasLimit,
dataAvailabilityStatus,
};

Expand Down
Loading
Loading