Skip to content

Commit

Permalink
Return block selection result
Browse files Browse the repository at this point in the history
  • Loading branch information
nflaig committed Oct 29, 2024
1 parent fa4eb2c commit a4cf741
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 64 deletions.
28 changes: 19 additions & 9 deletions packages/beacon-node/src/api/impl/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ export enum BuilderBlockSelectionReason {
BuilderPreferred = "builder_preferred",
}

export type BlockSelectionResult =
| {
source: ProducedBlockSource.engine;
reason: EngineBlockSelectionReason;
}
| {
source: ProducedBlockSource.builder;
reason: BuilderBlockSelectionReason;
};

/**
* Server implementation for handling validator duties.
* See `@lodestar/validator/src/api` for the client implementation).
Expand Down Expand Up @@ -782,15 +792,15 @@ export function getValidatorApi(
}

if (engine.status === "fulfilled" && builder.status === "fulfilled") {
const executionPayloadSource = selectBlockProductionSource(
{
builderBlockValue: builder.value.executionPayloadValue + builder.value.consensusBlockValue,
engineBlockValue: engine.value.executionPayloadValue + engine.value.consensusBlockValue,
builderBoostFactor,
builderSelection,
},
metrics
);
const result = selectBlockProductionSource({
builderBlockValue: builder.value.executionPayloadValue + builder.value.consensusBlockValue,
engineBlockValue: engine.value.executionPayloadValue + engine.value.consensusBlockValue,
builderBoostFactor,
builderSelection,
});
const executionPayloadSource = result.source;

metrics?.blockProductionSelectionResults.inc(result);

logger.info(`Selected ${executionPayloadSource} block`, {
...loggerContext,
Expand Down
70 changes: 20 additions & 50 deletions packages/beacon-node/src/api/impl/validator/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {ATTESTATION_SUBNET_COUNT} from "@lodestar/params";
import {routes} from "@lodestar/api";
import {BLSPubkey, CommitteeIndex, ProducedBlockSource, Slot, ValidatorIndex} from "@lodestar/types";
import {MAX_BUILDER_BOOST_FACTOR} from "@lodestar/validator";
import {Metrics} from "../../../metrics/index.js";
import {BuilderBlockSelectionReason, EngineBlockSelectionReason} from "./index.js";
import {BlockSelectionResult, BuilderBlockSelectionReason, EngineBlockSelectionReason} from "./index.js";

export function computeSubnetForCommitteesAtSlot(
slot: Slot,
Expand Down Expand Up @@ -46,70 +45,41 @@ export function getPubkeysForIndices(
return pubkeys;
}

export function selectBlockProductionSource(
{
builderSelection,
engineBlockValue,
builderBlockValue,
builderBoostFactor,
}: {
builderSelection: routes.validator.BuilderSelection;
engineBlockValue: bigint;
builderBlockValue: bigint;
builderBoostFactor: bigint;
},
metrics: Metrics | null
): ProducedBlockSource {
export function selectBlockProductionSource({
builderSelection,
engineBlockValue,
builderBlockValue,
builderBoostFactor,
}: {
builderSelection: routes.validator.BuilderSelection;
engineBlockValue: bigint;
builderBlockValue: bigint;
builderBoostFactor: bigint;
}): BlockSelectionResult {
switch (builderSelection) {
case routes.validator.BuilderSelection.ExecutionAlways:
case routes.validator.BuilderSelection.ExecutionOnly: {
metrics?.blockProductionSelectionResults.inc({
source: ProducedBlockSource.engine,
reason: EngineBlockSelectionReason.EnginePreferred,
});
return ProducedBlockSource.engine;
}
case routes.validator.BuilderSelection.ExecutionOnly:
return {source: ProducedBlockSource.engine, reason: EngineBlockSelectionReason.EnginePreferred};

case routes.validator.BuilderSelection.Default:
case routes.validator.BuilderSelection.MaxProfit: {
if (builderBoostFactor === MAX_BUILDER_BOOST_FACTOR) {
metrics?.blockProductionSelectionResults.inc({
source: ProducedBlockSource.builder,
reason: BuilderBlockSelectionReason.BuilderPreferred,
});
return ProducedBlockSource.builder;
return {source: ProducedBlockSource.builder, reason: BuilderBlockSelectionReason.BuilderPreferred};
}

if (builderBoostFactor === BigInt(0)) {
metrics?.blockProductionSelectionResults.inc({
source: ProducedBlockSource.engine,
reason: EngineBlockSelectionReason.EnginePreferred,
});
return ProducedBlockSource.engine;
return {source: ProducedBlockSource.engine, reason: EngineBlockSelectionReason.EnginePreferred};
}

if (engineBlockValue >= (builderBlockValue * builderBoostFactor) / BigInt(100)) {
metrics?.blockProductionSelectionResults.inc({
source: ProducedBlockSource.engine,
reason: EngineBlockSelectionReason.BlockValue,
});
return ProducedBlockSource.engine;
return {source: ProducedBlockSource.engine, reason: EngineBlockSelectionReason.BlockValue};
}

metrics?.blockProductionSelectionResults.inc({
source: ProducedBlockSource.builder,
reason: BuilderBlockSelectionReason.BlockValue,
});
return ProducedBlockSource.builder;
return {source: ProducedBlockSource.builder, reason: BuilderBlockSelectionReason.BlockValue};
}

case routes.validator.BuilderSelection.BuilderAlways:
case routes.validator.BuilderSelection.BuilderOnly: {
metrics?.blockProductionSelectionResults.inc({
source: ProducedBlockSource.builder,
reason: BuilderBlockSelectionReason.BuilderPreferred,
});
return ProducedBlockSource.builder;
}
case routes.validator.BuilderSelection.BuilderOnly:
return {source: ProducedBlockSource.builder, reason: BuilderBlockSelectionReason.BuilderPreferred};
}
}
11 changes: 6 additions & 5 deletions packages/beacon-node/src/metrics/metrics/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import {NotReorgedReason} from "@lodestar/fork-choice/lib/forkChoice/interface.j
import {UpdateHeadOpt} from "@lodestar/fork-choice";
import {RegistryMetricCreator} from "../utils/registryMetricCreator.js";
import {BlockProductionStep, PayloadPreparationType} from "../../chain/produceBlock/index.js";
import {BuilderBlockSelectionReason, EngineBlockSelectionReason} from "../../api/impl/validator/index.js";
import {
BlockSelectionResult,
BuilderBlockSelectionReason,
EngineBlockSelectionReason,
} from "../../api/impl/validator/index.js";

export type BeaconMetrics = ReturnType<typeof createBeaconMetrics>;

Expand Down Expand Up @@ -161,10 +165,7 @@ export function createBeaconMetrics(register: RegistryMetricCreator) {
help: "Count of blocks successfully produced",
labelNames: ["source"],
}),
blockProductionSelectionResults: register.gauge<
| {source: ProducedBlockSource.engine; reason: EngineBlockSelectionReason}
| {source: ProducedBlockSource.builder; reason: BuilderBlockSelectionReason}
>({
blockProductionSelectionResults: register.gauge<BlockSelectionResult>({
name: "beacon_block_production_selection_results_total",
help: "Count of all block production selection results",
labelNames: ["source", "reason"],
Expand Down

0 comments on commit a4cf741

Please sign in to comment.