Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 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
125 changes: 87 additions & 38 deletions packages/beacon-node/src/api/impl/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,65 +913,114 @@ export function getValidatorApi(
notWhileSyncing();
await waitForSlot(slot);

// TODO GLOAS: support producing blocks from builder bids
const source = ProducedBlockSource.engine;

// TODO GLOAS: needs to be updated after fork choice changes are merged
const parentBlock = chain.getProposerHead(slot);
const {blockRoot: parentBlockRootHex, slot: parentSlot} = parentBlock;
const parentBlockRoot = fromHex(parentBlockRootHex);
notOnOutOfRangeData(parentBlockRoot);
metrics?.blockProductionSlotDelta.set(slot - parentSlot);
metrics?.blockProductionRequests.inc({source});

const graffitiBytes = toGraffitiBytes(
graffiti ?? getDefaultGraffiti(getLodestarClientVersion(), chain.executionEngine.clientVersion, {})
);

// TODO GLOAS: respect builderSelection (MaxProfit, BuilderAlways, ExecutionAlways, etc.) to let
// the user control bid source preferences and value comparison. Also add external builder api
// support when it is implemented.
const builderBid = chain.executionPayloadBidPool.getBestBid(
slot,
parentBlock.executionPayloadBlockHash,
parentBlockRootHex
);

const logCtx = {
slot,
parentSlot,
parentBlockRoot: parentBlockRootHex,
parentBlockHash: parentBlock.executionPayloadBlockHash,
fork,
...(builderBid !== null
? {
bidValue: builderBid.message.value,
builderIndex: builderBid.message.builderIndex,
bidBlockHash: toRootHex(builderBid.message.blockHash),
}
: {}),
};

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

let timer: undefined | ((opts: {source: ProducedBlockSource}) => number);
try {
timer = metrics?.blockProductionTime.startTimer();
const {block, executionPayloadValue, consensusBlockValue} = await chain.produceBlock({
slot,
parentBlock,
randaoReveal,
graffiti: graffitiBytes,
feeRecipient,
commonBlockBodyPromise,
});
const baseAttrs = {
slot,
parentBlock,
randaoReveal,
graffiti: graffitiBytes,
feeRecipient,
commonBlockBodyPromise,
};

metrics?.blockProductionSuccess.inc({source});
metrics?.blockProductionNumAggregated.observe({source}, block.body.attestations.length);
metrics?.blockProductionConsensusBlockValue.observe({source}, Number(formatWeiToEth(consensusBlockValue)));
metrics?.blockProductionExecutionPayloadValue.observe({source}, Number(formatWeiToEth(executionPayloadValue)));
metrics?.blockProductionRequests.inc({source: ProducedBlockSource.engine});
if (builderBid !== null) {
metrics?.blockProductionRequests.inc({source: ProducedBlockSource.builder});
}

const blockRoot = toRootHex(config.getForkTypes(slot).BeaconBlock.hashTreeRoot(block));
logger.verbose("Produced block", {
slot,
executionPayloadValue,
consensusBlockValue,
root: blockRoot,
});
if (chain.opts.persistProducedBlocks) {
void chain.persistBlock(block, "produced_engine_block");
const timed = <T>(source: ProducedBlockSource, fn: () => Promise<T>): Promise<T> => {
const t = metrics?.blockProductionTime.startTimer();
return fn().finally(() => t?.({source}));
};

// Always build local block. If builder bid available, also build with it in parallel and prefer it.
const [engineResult, bidResult] = await Promise.allSettled([
timed(ProducedBlockSource.engine, () => chain.produceBlock(baseAttrs)),
builderBid !== null
? timed(ProducedBlockSource.builder, () => chain.produceBlock({...baseAttrs, builderBid}))
: Promise.reject(),
]);

let bestResult: typeof engineResult | null = null;
let source: ProducedBlockSource = ProducedBlockSource.engine;
if (builderBid !== null && bidResult.status === "fulfilled") {
source = ProducedBlockSource.builder;
bestResult = bidResult;
logger.info("Selected builder bid block", logCtx);
} else if (engineResult.status === "fulfilled") {
source = ProducedBlockSource.engine;
bestResult = engineResult;
if (builderBid !== null) {
logger.warn("Builder bid block production failed, using local block", logCtx);
}
}

return {
data: block as gloas.BeaconBlock,
meta: {
version: fork,
consensusBlockValue,
},
};
} finally {
timer?.({source});
if (bestResult === null || bestResult.status !== "fulfilled") {
throw Error("Block production failed");
Comment thread
ensi321 marked this conversation as resolved.
Resolved
}

const {block, executionPayloadValue, consensusBlockValue} = bestResult.value;

metrics?.blockProductionSuccess.inc({source});
metrics?.blockProductionNumAggregated.observe({source}, block.body.attestations.length);
metrics?.blockProductionConsensusBlockValue.observe({source}, Number(formatWeiToEth(consensusBlockValue)));
metrics?.blockProductionExecutionPayloadValue.observe({source}, Number(formatWeiToEth(executionPayloadValue)));

const blockRoot = toRootHex(config.getForkTypes(slot).BeaconBlock.hashTreeRoot(block));
logger.verbose("Produced block", {
...logCtx,
executionPayloadValue,
consensusBlockValue,
root: blockRoot,
});
if (chain.opts.persistProducedBlocks) {
void chain.persistBlock(block, "produced_engine_block");
}

return {
data: block as gloas.BeaconBlock,
meta: {version: fork, consensusBlockValue},
};
},

async produceAttestationData({committeeIndex, slot}) {
Expand Down
2 changes: 2 additions & 0 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,7 @@ export class BeaconChain implements IBeaconChain {
feeRecipient,
commonBlockBodyPromise,
parentBlock,
builderBid,
}: BlockAttributes & {commonBlockBodyPromise: Promise<CommonBlockBody>}
): Promise<{
block: AssembledBlockType<T>;
Expand Down Expand Up @@ -1056,6 +1057,7 @@ export class BeaconChain implements IBeaconChain {
proposerIndex,
proposerPubKey,
commonBlockBodyPromise,
builderBid,
}
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ type BlockRootHex = string;
type BlockHashHex = string;

/**
* Store the best execution payload bid per slot / (parent block root, parent block hash).
* Store the best signed execution payload bid per slot / (parent block root, parent block hash).
*/
export class ExecutionPayloadBidPool {
private readonly bidByParentHashByParentRootBySlot = new MapDef<
Slot,
MapDef<BlockRootHex, Map<BlockHashHex, gloas.ExecutionPayloadBid>>
>(() => new MapDef<BlockRootHex, Map<BlockHashHex, gloas.ExecutionPayloadBid>>(() => new Map()));
MapDef<BlockRootHex, Map<BlockHashHex, gloas.SignedExecutionPayloadBid>>
>(() => new MapDef<BlockRootHex, Map<BlockHashHex, gloas.SignedExecutionPayloadBid>>(() => new Map()));
private lowestPermissibleSlot = 0;

get size(): number {
Expand All @@ -31,8 +31,8 @@ export class ExecutionPayloadBidPool {
return count;
}

add(bid: gloas.ExecutionPayloadBid): InsertOutcome {
const {slot, parentBlockRoot, parentBlockHash, value} = bid;
add(bid: gloas.SignedExecutionPayloadBid): InsertOutcome {
const {slot, parentBlockRoot, parentBlockHash, value} = bid.message;
const lowestPermissibleSlot = this.lowestPermissibleSlot;

if (slot < lowestPermissibleSlot) {
Expand All @@ -45,7 +45,7 @@ export class ExecutionPayloadBidPool {
const existing = bidByParentHash.get(parentHashHex);

if (existing) {
const existingValue = existing.value;
const existingValue = existing.message.value;
const newValue = value;
if (newValue > existingValue) {
bidByParentHash.set(parentHashHex, bid);
Expand All @@ -59,14 +59,15 @@ export class ExecutionPayloadBidPool {
}

/**
* Return the highest-value bid matching slot, parent block hash, and parent block root.
* Return the highest-value signed bid matching slot, parent block hash, and parent block root.
* Used for gossip validation and block production.
*/
getBestBid(
slot: Slot,
parentBlockHash: BlockHashHex,
parentBlockHash: BlockHashHex | null,
parentBlockRoot: BlockRootHex
): gloas.ExecutionPayloadBid | null {
): gloas.SignedExecutionPayloadBid | null {
if (parentBlockHash === null) return null;
const bidByParentHash = this.bidByParentHashByParentRootBySlot.get(slot)?.get(parentBlockRoot);
return bidByParentHash?.get(parentBlockHash) ?? null;
}
Expand Down
52 changes: 44 additions & 8 deletions packages/beacon-node/src/chain/produceBlock/produceBlockBody.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import {
gloas,
ssz,
} from "@lodestar/types";
import {Logger, byteArrayEquals, fromHex, sleep, toHex, toPubkeyHex, toRootHex} from "@lodestar/utils";
import {GWEI_TO_WEI, 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 {
Expand Down Expand Up @@ -93,6 +93,8 @@ export type BlockAttributes = {
slot: Slot;
parentBlock: ProtoBlock;
feeRecipient?: string;
/** When provided, build block with this builder bid instead of a self-build bid */
builderBid?: gloas.SignedExecutionPayloadBid;
};

export enum BlockType {
Expand Down Expand Up @@ -174,6 +176,7 @@ export async function produceBlockBody<T extends BlockType>(
proposerIndex,
proposerPubKey,
commonBlockBodyPromise,
builderBid,
} = blockAttr;
let executionPayloadValue: Wei;
let blockBody: AssembledBodyType<T>;
Expand All @@ -194,7 +197,36 @@ export async function produceBlockBody<T extends BlockType>(
};
this.logger.verbose("Producing beacon block body", logMeta);

if (isForkPostGloas(fork)) {
if (builderBid !== undefined) {
if (!isStatePostGloas(currentState)) {
throw new Error("Expected Gloas state for builder bid block production");
}

const isExtendingPayload = byteArrayEquals(
builderBid.message.parentBlockHash,
currentState.latestExecutionPayloadBid.blockHash
);
const parentExecutionRequests = isExtendingPayload
? await this.getParentExecutionRequests(parentBlock.slot, parentBlock.blockRoot)
: ssz.electra.ExecutionRequests.defaultValue();
executionPayloadValue = BigInt(builderBid.message.value) * GWEI_TO_WEI;

const commonBlockBody = await commonBlockBodyPromise;
const gloasBody = Object.assign({}, commonBlockBody) as gloas.BeaconBlockBody;
gloasBody.signedExecutionPayloadBid = builderBid;
gloasBody.payloadAttestations = this.payloadAttestationPool.getPayloadAttestationsForBlock(
parentBlock.blockRoot,
blockSlot - 1
);
gloasBody.parentExecutionRequests = parentExecutionRequests;
blockBody = gloasBody as AssembledBodyType<T>;
Comment thread
ensi321 marked this conversation as resolved.
Resolved

this.logger.verbose("Produced block with builder bid", {
slot: blockSlot,
builderIndex: builderBid.message.builderIndex,
bidValue: builderBid.message.value,
Comment thread
ensi321 marked this conversation as resolved.
Resolved
});
} else if (isForkPostGloas(fork)) {
if (!isStatePostGloas(currentState)) {
throw new Error("Expected Gloas state for Gloas block production");
}
Expand All @@ -208,12 +240,6 @@ export async function produceBlockBody<T extends BlockType>(

const endExecutionPayload = this.metrics?.executionBlockProductionTimeSteps.startTimer();

this.logger.verbose("Preparing execution payload from engine", {
slot: blockSlot,
parentBlockRoot: toRootHex(parentBlockRoot),
feeRecipient,
});

// Get execution payload from EL
let parentBlockHash: Bytes32;
let parentExecutionRequests: electra.ExecutionRequests;
Expand Down Expand Up @@ -243,6 +269,16 @@ export async function produceBlockBody<T extends BlockType>(
const {prepType, payloadId} = prepareRes;
Object.assign(logMeta, {executionPayloadPrepType: prepType});

this.logger.verbose("Prepared execution payload from engine", {
slot: blockSlot,
parentBlockRoot: toRootHex(parentBlockRoot),
parentBlockHash: toRootHex(parentBlockHash),
feeRecipient,
prepType,
payloadId,
isExtendingPayload,
});

if (prepType !== PayloadPreparationType.Cached) {
await sleep(PAYLOAD_GENERATION_TIME_MS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ async function validateExecutionPayloadBid(
// [IGNORE] this bid is the highest value bid seen for the tuple
// `(bid.slot, bid.parent_block_hash, bid.parent_block_root)`.
const bestBid = chain.executionPayloadBidPool.getBestBid(bid.slot, parentBlockHashHex, parentBlockRootHex);
if (bestBid !== null && bestBid.value >= bid.value) {
if (bestBid !== null && bestBid.message.value >= bid.value) {
throw new ExecutionPayloadBidError(GossipAction.IGNORE, {
code: ExecutionPayloadBidErrorCode.BID_TOO_LOW,
bidValue: bid.value,
currentHighestBid: bestBid.value,
currentHighestBid: bestBid.message.value,
});
}
// [IGNORE] `bid.value` is less or equal than the builder's excess balance --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ function getSequentialHandlers(modules: ValidatorFnsModules, options: GossipHand

// Handle valid payload bid by storing in a bid pool
try {
const insertOutcome = chain.executionPayloadBidPool.add(executionPayloadBid.message);
const insertOutcome = chain.executionPayloadBidPool.add(executionPayloadBid);
metrics?.opPool.executionPayloadBidPool.gossipInsertOutcome.inc({insertOutcome});
} catch (e) {
logger.error("Error adding to executionPayloadBid pool", {}, e as Error);
Expand Down
Loading
Loading