Skip to content
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
56 changes: 56 additions & 0 deletions packages/beacon-node/src/api/impl/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,9 +377,13 @@ export function getValidatorApi(
{
commonBlockBodyPromise,
parentBlock,
parentState,
parentStateWithPayload,
}: Omit<routes.validator.ExtraProduceBlockOpts, "builderSelection"> & {
commonBlockBodyPromise: Promise<CommonBlockBody>;
parentBlock: ProtoBlock;
parentState: IBeaconStateView;
parentStateWithPayload: IBeaconStateView;
}
): Promise<ProduceBlindedBlockRes> {
const version = config.getForkName(slot);
Expand Down Expand Up @@ -414,6 +418,8 @@ export function getValidatorApi(
randaoReveal,
graffiti,
commonBlockBodyPromise,
parentState,
parentStateWithPayload,
});

metrics?.blockProductionSuccess.inc({source});
Expand Down Expand Up @@ -446,9 +452,13 @@ export function getValidatorApi(
strictFeeRecipientCheck,
commonBlockBodyPromise,
parentBlock,
parentState,
parentStateWithPayload,
}: Omit<routes.validator.ExtraProduceBlockOpts, "builderSelection"> & {
commonBlockBodyPromise: Promise<CommonBlockBody>;
parentBlock: ProtoBlock;
parentState: IBeaconStateView;
parentStateWithPayload: IBeaconStateView;
}
): Promise<ProduceBlockContentsRes & {shouldOverrideBuilder?: boolean}> {
const source = ProducedBlockSource.engine;
Expand All @@ -464,6 +474,8 @@ export function getValidatorApi(
graffiti,
feeRecipient,
commonBlockBodyPromise,
parentState,
parentStateWithPayload,
});
const version = config.getForkName(block.slot);
if (strictFeeRecipientCheck && feeRecipient && isForkPostBellatrix(version)) {
Expand Down Expand Up @@ -548,6 +560,16 @@ export function getValidatorApi(
notOnOutOfRangeData(parentBlockRoot);
metrics?.blockProductionSlotDelta.set(slot - parentSlot);

// Fetch the production state once and thread it to all downstream production calls. Pre-gloas has no
// parent execution payload, so the applied state is the same view as parentState.
const parentState = await chain.regen.getBlockSlotState(
parentBlock,
slot,
{dontTransferCache: true},
RegenCaller.produceBlock
);
const parentStateWithPayload = parentState;

const fork = config.getForkName(slot);

const isBuilderEnabled =
Expand Down Expand Up @@ -591,6 +613,8 @@ export function getValidatorApi(
strictFeeRecipientCheck: false,
commonBlockBodyPromise,
parentBlock,
parentState,
parentStateWithPayload,
})
: Promise.reject(new Error("Builder disabled"));

Expand All @@ -599,6 +623,8 @@ export function getValidatorApi(
strictFeeRecipientCheck,
commonBlockBodyPromise,
parentBlock,
parentState,
parentStateWithPayload,
}).then((engineBlock) => {
// Once the engine returns a block, in the event of either:
// - suspected builder censorship
Expand Down Expand Up @@ -642,6 +668,8 @@ export function getValidatorApi(
parentBlock,
randaoReveal,
graffiti: graffitiBytes,
parentState,
parentStateWithPayload,
})
.then((commonBlockBody) => {
deferredCommonBlockBody.resolve(commonBlockBody);
Expand Down Expand Up @@ -910,11 +938,36 @@ export function getValidatorApi(
: {}),
};

// Fetch the production state once and apply the parent execution payload once here, so all
// downstream consumers share a single state: the un-applied `parentState` (for `computeNewStateRoot`)
// and the applied `parentStateWithPayload` (for attestation scoring and execution-payload prep).
const parentState = await chain.regen.getBlockSlotState(
parentBlock,
slot,
{dontTransferCache: true},
RegenCaller.produceBlock
);
// Fetch the parent's execution requests once here and thread them via blockAttributes: they are
// used both to apply the parent payload below and as the block's `parentExecutionRequests` field
// (so both are guaranteed consistent). Only needed when building on FULL; empty parent has none.
const parentExecutionRequests =
isBuildingOnFull && isStatePostGloas(parentState)
? await chain.getParentExecutionRequests(parentSlot, parentBlockRootHex)
: ssz.gloas.ExecutionRequests.defaultValue();
// When building on EMPTY there is nothing to apply, so it stays the same view as parentState
// (the availability bit for parentSlot stays false, which is correct).
const parentStateWithPayload =
isBuildingOnFull && isStatePostGloas(parentState)
? parentState.withParentPayloadApplied(parentExecutionRequests)
: parentState;

const commonBlockBodyPromise = chain.produceCommonBlockBody({
slot,
parentBlock,
randaoReveal,
graffiti: graffitiBytes,
parentState,
parentStateWithPayload,
});

const baseAttrs = {
Expand All @@ -924,6 +977,9 @@ export function getValidatorApi(
graffiti: graffitiBytes,
feeRecipient,
commonBlockBodyPromise,
parentState,
parentStateWithPayload,
parentExecutionRequests,
};

metrics?.blockProductionRequests.inc({source: ProducedBlockSource.engine});
Expand Down
42 changes: 8 additions & 34 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1031,18 +1031,10 @@ export class BeaconChain implements IBeaconChain {
}

async produceCommonBlockBody(blockAttributes: BlockAttributes): Promise<CommonBlockBody> {
const {slot, parentBlock} = blockAttributes;
const state = await this.regen.getBlockSlotState(
parentBlock,
slot,
{dontTransferCache: true},
RegenCaller.produceBlock
);

// TODO: To avoid breaking changes for metric define this attribute
const blockType = BlockType.Full;

return produceCommonBlockBody.call(this, blockType, state, blockAttributes);
return produceCommonBlockBody.call(this, blockType, blockAttributes);
}

produceBlock(blockAttributes: BlockAttributes & {commonBlockBodyPromise: Promise<CommonBlockBody>}): Promise<{
Expand All @@ -1064,44 +1056,24 @@ export class BeaconChain implements IBeaconChain {

async produceBlockWrapper<T extends BlockType>(
blockType: T,
{
randaoReveal,
graffiti,
slot,
feeRecipient,
commonBlockBodyPromise,
parentBlock,
builderBid,
}: BlockAttributes & {commonBlockBodyPromise: Promise<CommonBlockBody>}
blockAttributes: BlockAttributes & {commonBlockBodyPromise: Promise<CommonBlockBody>}
): Promise<{
block: AssembledBlockType<T>;
executionPayloadValue: Wei;
consensusBlockValue: Wei;
shouldOverrideBuilder?: boolean;
}> {
const state = await this.regen.getBlockSlotState(
parentBlock,
slot,
{dontTransferCache: true},
RegenCaller.produceBlock
);
const proposerIndex = state.getBeaconProposer(slot);
const {slot, parentBlock, parentState} = blockAttributes;
const proposerIndex = parentState.getBeaconProposer(slot);
const proposerPubKey = this.pubkeyCache.getOrThrow(proposerIndex).toBytes();

const {body, produceResult, executionPayloadValue, shouldOverrideBuilder} = await produceBlockBody.call(
this,
blockType,
state,
{
randaoReveal,
graffiti,
slot,
feeRecipient,
parentBlock,
...blockAttributes,
proposerIndex,
proposerPubKey,
commonBlockBodyPromise,
builderBid,
}
);

Expand All @@ -1126,7 +1098,9 @@ export class BeaconChain implements IBeaconChain {
body,
} as AssembledBlockType<T>;

const {newStateRoot, proposerReward} = computeNewStateRoot(this.metrics, state, block);
// `parentState` is the un-applied production state; `computeNewStateRoot` re-applies the parent
// payload during state transition, so it must not receive the already-applied state
const {newStateRoot, proposerReward} = computeNewStateRoot(this.metrics, parentState, block);
block.stateRoot = newStateRoot;
const blockRoot =
produceResult.type === BlockType.Full
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export class AggregatedAttestationPool {
const stateEpoch = state.epoch;
const statePrevEpoch = stateEpoch - 1;
const rootCache = new RootCache(state);
const gloasState = isStatePostGloas(state) ? state : null;

const notSeenValidatorsFn = getNotSeenValidatorsFn(this.config, shufflingCache, state);
const validateAttestationDataFn = getValidateAttestationDataFn(forkChoice, state);
Expand Down Expand Up @@ -361,7 +362,8 @@ export class AggregatedAttestationPool {
inclusionDistance,
stateEpoch,
rootCache,
isStatePostGloas(state) ? state.executionPayloadAvailability : null
gloasState?.executionPayloadAvailability ?? null,
gloasState?.latestExecutionPayloadBid.slot ?? null
);

const weight =
Expand Down
Loading
Loading