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: add proposer boost reorg flag #6652

Merged
merged 18 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions packages/beacon-node/src/api/impl/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export function getValidatorApi({
// forkChoice.updateTime() might have already been called by the onSlot clock
// handler, in which case this should just return.
chain.forkChoice.updateTime(slot);
parentBlockRoot = fromHexString(chain.recomputeForkChoiceHead().blockRoot);
parentBlockRoot = fromHexString(chain.getProposerHead(slot).blockRoot);
} else {
parentBlockRoot = inParentBlockRoot;
}
Expand Down Expand Up @@ -430,7 +430,7 @@ export function getValidatorApi({
// forkChoice.updateTime() might have already been called by the onSlot clock
// handler, in which case this should just return.
chain.forkChoice.updateTime(slot);
parentBlockRoot = fromHexString(chain.recomputeForkChoiceHead().blockRoot);
parentBlockRoot = fromHexString(chain.getProposerHead(slot).blockRoot);
} else {
parentBlockRoot = inParentBlockRoot;
}
Expand Down Expand Up @@ -508,7 +508,7 @@ export function getValidatorApi({
// forkChoice.updateTime() might have already been called by the onSlot clock
// handler, in which case this should just return.
chain.forkChoice.updateTime(slot);
const parentBlockRoot = fromHexString(chain.recomputeForkChoiceHead().blockRoot);
const parentBlockRoot = fromHexString(chain.getProposerHead(slot).blockRoot);

const fork = config.getForkName(slot);
// set some sensible opts
Expand Down
56 changes: 51 additions & 5 deletions packages/beacon-node/src/chain/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
bellatrix,
isBlindedBeaconBlock,
} from "@lodestar/types";
import {CheckpointWithHex, ExecutionStatus, IForkChoice, ProtoBlock} from "@lodestar/fork-choice";
import {CheckpointWithHex, ExecutionStatus, IForkChoice, ProtoBlock, UpdateHeadOpt} from "@lodestar/fork-choice";
import {ProcessShutdownCallback} from "@lodestar/validator";
import {Logger, gweiToWei, isErrorAborted, pruneSetToMax, sleep, toHex} from "@lodestar/utils";
import {ForkSeq, SLOTS_PER_EPOCH} from "@lodestar/params";
Expand All @@ -45,7 +45,14 @@ import {isOptimisticBlock} from "../util/forkChoice.js";
import {BufferPool} from "../util/bufferPool.js";
import {BlockProcessor, ImportBlockOpts} from "./blocks/index.js";
import {ChainEventEmitter, ChainEvent} from "./emitter.js";
import {IBeaconChain, ProposerPreparationData, BlockHash, StateGetOpts, CommonBlockBody} from "./interface.js";
import {
IBeaconChain,
ProposerPreparationData,
BlockHash,
StateGetOpts,
CommonBlockBody,
FindHeadFnName,
} from "./interface.js";
import {IChainOptions} from "./options.js";
import {QueuedStateRegenerator, RegenCaller} from "./regen/index.js";
import {initializeForkChoice} from "./forkChoice/index.js";
Expand Down Expand Up @@ -279,7 +286,8 @@ export class BeaconChain implements IBeaconChain {
clock.currentSlot,
cachedState,
opts,
this.justifiedBalancesGetter.bind(this)
this.justifiedBalancesGetter.bind(this),
logger
);
const regen = new QueuedStateRegenerator({
config,
Expand Down Expand Up @@ -691,10 +699,48 @@ export class BeaconChain implements IBeaconChain {

recomputeForkChoiceHead(): ProtoBlock {
this.metrics?.forkChoice.requests.inc();
const timer = this.metrics?.forkChoice.findHead.startTimer();
const timer = this.metrics?.forkChoice.findHead.startTimer({entrypoint: FindHeadFnName.recomputeForkChoiceHead});

try {
return this.forkChoice.updateAndGetHead({mode: UpdateHeadOpt.GetCanonicialHead}).head;
} catch (e) {
this.metrics?.forkChoice.errors.inc();
throw e;
} finally {
timer?.();
}
}

predictProposerHead(slot: Slot): ProtoBlock {
twoeths marked this conversation as resolved.
Show resolved Hide resolved
this.metrics?.forkChoice.requests.inc();
const timer = this.metrics?.forkChoice.findHead.startTimer({entrypoint: FindHeadFnName.predictProposerHead});

try {
return this.forkChoice.updateAndGetHead({mode: UpdateHeadOpt.GetPredictedProposerHead, slot}).head;
} catch (e) {
this.metrics?.forkChoice.errors.inc();
ensi321 marked this conversation as resolved.
Show resolved Hide resolved
throw e;
} finally {
timer?.();
}
}

getProposerHead(slot: Slot): ProtoBlock {
this.metrics?.forkChoice.requests.inc();
const timer = this.metrics?.forkChoice.findHead.startTimer({entrypoint: FindHeadFnName.getProposerHead});
const secFromSlot = this.clock.secFromSlot(slot);

try {
return this.forkChoice.updateHead();
const {head, isHeadTimely, notReorgedReason} = this.forkChoice.updateAndGetHead({
mode: UpdateHeadOpt.GetProposerHead,
secFromSlot,
slot,
});
this.metrics?.forkChoice.isBlockTimely.set(isHeadTimely ? 1 : 0);
ensi321 marked this conversation as resolved.
Show resolved Hide resolved
if (notReorgedReason !== undefined) {
this.metrics?.forkChoice.notReorgedReason.set(notReorgedReason);
}
return head;
} catch (e) {
this.metrics?.forkChoice.errors.inc();
throw e;
Expand Down
12 changes: 12 additions & 0 deletions packages/beacon-node/src/chain/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ export type StateGetOpts = {
allowRegen: boolean;
};

export enum FindHeadFnName {
recomputeForkChoiceHead = "recomputeForkChoiceHead",
predictProposerHead = "predictProposerHead",
getProposerHead = "getProposerHead",
}

/**
* The IBeaconChain service deals with processing incoming blocks, advancing a state transition
* and applying the fork choice rule to update the chain head
Expand Down Expand Up @@ -186,6 +192,12 @@ export interface IBeaconChain {

recomputeForkChoiceHead(): ProtoBlock;

/** When proposerBoostReorg is enabled, this is called at slot n-1 to predict the head block to build on if we are proposing at slot n */
predictProposerHead(slot: Slot): ProtoBlock;

/** When proposerBoostReorg is enabled and we are proposing a block, this is called to determine which head block to build on */
getProposerHead(slot: Slot): ProtoBlock;

waitForBlock(slot: Slot, root: RootHex): Promise<boolean>;

updateBeaconProposerData(epoch: Epoch, proposers: ProposerPreparationData[]): Promise<void>;
Expand Down
1 change: 1 addition & 0 deletions packages/beacon-node/src/chain/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const defaultChainOptions: IChainOptions = {
blsVerifyAllMultiThread: false,
disableBlsBatchVerify: false,
proposerBoostEnabled: true,
proposerBoostReorgEnabled: false,
computeUnrealized: true,
safeSlotsToImportOptimistically: SAFE_SLOTS_TO_IMPORT_OPTIMISTICALLY,
suggestedFeeRecipient: defaultValidatorOptions.suggestedFeeRecipient,
Expand Down
29 changes: 26 additions & 3 deletions packages/beacon-node/src/chain/prepareNextSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
computeEpochAtSlot,
isExecutionStateType,
computeTimeAtSlot,
CachedBeaconStateExecutions,
StateHashTreeRootSource,
} from "@lodestar/state-transition";
import {ChainForkConfig} from "@lodestar/config";
Expand Down Expand Up @@ -144,7 +145,29 @@ export class PrepareNextSlotScheduler {
if (isExecutionStateType(prepareState)) {
const proposerIndex = prepareState.epochCtx.getBeaconProposer(prepareSlot);
const feeRecipient = this.chain.beaconProposerCache.get(proposerIndex);
let updatedPrepareState = prepareState;
let updatedHeadRoot = headRoot;

if (feeRecipient) {
// If we are proposing next slot, we need to predict if we can proposer-boost-reorg or not
const {slot: proposerHeadSlot, blockRoot: proposerHeadRoot} = this.chain.predictProposerHead(clockSlot);
twoeths marked this conversation as resolved.
Show resolved Hide resolved

// If we predict we can reorg, update prepareState with proposer head block
if (proposerHeadRoot !== headRoot || proposerHeadSlot !== headSlot) {
this.logger.verbose("Weak head detected. May build on this block instead:", {
wemeetagain marked this conversation as resolved.
Show resolved Hide resolved
proposerHeadSlot,
proposerHeadRoot,
});
this.metrics?.weakHeadDetected.inc();
updatedPrepareState = (await this.chain.regen.getBlockSlotState(
proposerHeadRoot,
prepareSlot,
{dontTransferCache: !isEpochTransition},
RegenCaller.precomputeEpoch
)) as CachedBeaconStateExecutions;
updatedHeadRoot = proposerHeadRoot;
}

// Update the builder status, if enabled shoot an api call to check status
this.chain.updateBuilderStatus(clockSlot);
if (this.chain.executionBuilder?.status) {
Expand All @@ -167,10 +190,10 @@ export class PrepareNextSlotScheduler {
this.chain,
this.logger,
fork as ForkExecution, // State is of execution type
fromHex(headRoot),
fromHex(updatedHeadRoot),
safeBlockHash,
finalizedBlockHash,
prepareState,
updatedPrepareState,
feeRecipient
);
this.logger.verbose("PrepareNextSlotScheduler prepared new payload", {
Expand All @@ -183,7 +206,7 @@ export class PrepareNextSlotScheduler {
// If emitPayloadAttributes is true emit a SSE payloadAttributes event
if (this.chain.opts.emitPayloadAttributes === true) {
const data = await getPayloadAttributesForSSE(fork as ForkExecution, this.chain, {
prepareState,
prepareState: updatedPrepareState,
prepareSlot,
parentBlockRoot: fromHex(headRoot),
// The likely consumers of this API are builders and will anyway ignore the
Expand Down
16 changes: 15 additions & 1 deletion packages/beacon-node/src/metrics/metrics/beacon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ export function createBeaconMetrics(register: RegistryMetricCreator) {
// Non-spec'ed

forkChoice: {
findHead: register.histogram({
findHead: register.histogram<{entrypoint: string}>({
name: "beacon_fork_choice_find_head_seconds",
help: "Time taken to find head in seconds",
buckets: [0.1, 1, 10],
labelNames: ["entrypoint"],
}),
requests: register.gauge({
name: "beacon_fork_choice_requests_total",
Expand Down Expand Up @@ -109,6 +110,14 @@ export function createBeaconMetrics(register: RegistryMetricCreator) {
name: "beacon_fork_choice_indices_count",
help: "Current count of indices in fork choice data structures",
}),
isBlockTimely: register.gauge({
name: "beacon_fork_choice_is_block_timely",
help: "Whether the current head (or original head if re-orged out because it is late) is timely or not",
}),
notReorgedReason: register.gauge({
name: "beacon_fork_choice_not_reorged_reason",
ensi321 marked this conversation as resolved.
Show resolved Hide resolved
help: "Reason why the current head is not re-orged out",
}),
},

parentBlockDistance: register.histogram({
Expand Down Expand Up @@ -198,5 +207,10 @@ export function createBeaconMetrics(register: RegistryMetricCreator) {
name: "beacon_clock_epoch",
help: "Current clock epoch",
}),

weakHeadDetected: register.gauge({
name: "beacon_weak_head_detected",
ensi321 marked this conversation as resolved.
Show resolved Hide resolved
help: "Detected current head block is weak. May reorg it out when proposing next slot. See proposer boost reorg for more",
}),
};
}
140 changes: 140 additions & 0 deletions packages/beacon-node/test/e2e/chain/proposerBoostReorg.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import {describe, it, afterEach, expect} from "vitest";
import {SLOTS_PER_EPOCH} from "@lodestar/params";
import {TimestampFormatCode} from "@lodestar/logger";
import {ChainConfig} from "@lodestar/config";
import {RootHex, Slot} from "@lodestar/types";
import {routes} from "@lodestar/api";
import {toHexString} from "@lodestar/utils";
import {LogLevel, TestLoggerOpts, testLogger} from "../../utils/logger.js";
import {getDevBeaconNode} from "../../utils/node/beacon.js";
import {TimelinessForkChoice} from "../../mocks/fork-choice/timeliness.js";
import {getAndInitDevValidators} from "../../utils/node/validator.js";
import {waitForEvent} from "../../utils/events/resolver.js";
import {ReorgEventData} from "../../../src/chain/emitter.js";

describe(
"proposer boost reorg",
function () {
const validatorCount = 8;
const testParams: Pick<ChainConfig, "SECONDS_PER_SLOT" | "REORG_PARENT_WEIGHT_THRESHOLD" | "PROPOSER_SCORE_BOOST"> =
{
// eslint-disable-next-line @typescript-eslint/naming-convention
SECONDS_PER_SLOT: 2,
// need this to make block `reorgSlot - 1` strong enough
// eslint-disable-next-line @typescript-eslint/naming-convention
REORG_PARENT_WEIGHT_THRESHOLD: 80,
// need this to make block `reorgSlot + 1` to become the head
// eslint-disable-next-line @typescript-eslint/naming-convention
PROPOSER_SCORE_BOOST: 120,
};

const afterEachCallbacks: (() => Promise<unknown> | void)[] = [];
afterEach(async () => {
while (afterEachCallbacks.length > 0) {
const callback = afterEachCallbacks.pop();
if (callback) await callback();
}
});

const reorgSlot = 10;
const proposerBoostReorgEnabled = true;
/**
* reorgSlot
* /
* reorgSlot - 1 ------------ reorgSlot + 1
*
* Note that in additional of being not timely, there are other criterion that
ensi321 marked this conversation as resolved.
Show resolved Hide resolved
* the block needs to satisfied before being re-orged out. This test assumes
* other criterion are satisfied except timeliness.
* Note that in additional of being not timely, there are other criterion that
* the block needs to satisfy before being re-orged out. This test assumes
* other criterion are already satisfied
*/
it(`should reorg a late block at slot ${reorgSlot}`, async () => {
// the node needs time to transpile/initialize bls worker threads
const genesisSlotsDelay = 7;
const genesisTime = Math.floor(Date.now() / 1000) + genesisSlotsDelay * testParams.SECONDS_PER_SLOT;
const testLoggerOpts: TestLoggerOpts = {
level: LogLevel.debug,
timestampFormat: {
format: TimestampFormatCode.EpochSlot,
genesisTime,
slotsPerEpoch: SLOTS_PER_EPOCH,
secondsPerSlot: testParams.SECONDS_PER_SLOT,
},
};
const logger = testLogger("BeaconNode", testLoggerOpts);
const bn = await getDevBeaconNode({
params: testParams,
options: {
sync: {isSingleNode: true},
network: {allowPublishToZeroPeers: true, mdns: true, useWorker: false},
// run the first bn with ReorgedForkChoice, no nHistoricalStates flag so it does not have to reload
ensi321 marked this conversation as resolved.
Show resolved Hide resolved
chain: {
blsVerifyAllMainThread: true,
forkchoiceConstructor: TimelinessForkChoice,
proposerBoostEnabled: true,
proposerBoostReorgEnabled,
},
},
validatorCount,
genesisTime,
logger,
});

(bn.chain.forkChoice as TimelinessForkChoice).lateSlot = reorgSlot;
afterEachCallbacks.push(async () => bn.close());
const {validators} = await getAndInitDevValidators({
node: bn,
logPrefix: "vc-0",
validatorsPerClient: validatorCount,
validatorClientCount: 1,
startIndex: 0,
useRestApi: false,
testLoggerOpts,
});
afterEachCallbacks.push(() => Promise.all(validators.map((v) => v.close())));

const commonAncestor = await waitForEvent<{slot: Slot; block: RootHex}>(
bn.chain.emitter,
routes.events.EventType.head,
240000,
({slot}) => slot === reorgSlot - 1
);
// reorgSlot
// /
// commonAncestor ------------ newBlock
const commonAncestorRoot = commonAncestor.block;
const reorgBlockEventData = await waitForEvent<{slot: Slot; block: RootHex}>(
bn.chain.emitter,
routes.events.EventType.head,
240000,
({slot}) => slot === reorgSlot
);
const reorgBlockRoot = reorgBlockEventData.block;
const [newBlockEventData, reorgEventData] = await Promise.all([
waitForEvent<{slot: Slot; block: RootHex}>(
bn.chain.emitter,
routes.events.EventType.block,
240000,
({slot}) => slot === reorgSlot + 1
),
waitForEvent<ReorgEventData>(bn.chain.emitter, routes.events.EventType.chainReorg, 240000),
]);
expect(reorgEventData.slot).toEqual(reorgSlot + 1);
const newBlock = await bn.chain.getBlockByRoot(newBlockEventData.block);
if (newBlock == null) {
throw Error(`Block ${reorgSlot + 1} not found`);
}
expect(reorgEventData.oldHeadBlock).toEqual(reorgBlockRoot);
expect(reorgEventData.newHeadBlock).toEqual(newBlockEventData.block);
expect(reorgEventData.depth).toEqual(2);
expect(toHexString(newBlock?.block.message.parentRoot)).toEqual(commonAncestorRoot);
logger.info("New block", {
slot: newBlock.block.message.slot,
parentRoot: toHexString(newBlock.block.message.parentRoot),
});
});
},
{timeout: 60000}
);
Loading
Loading