Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
2 changes: 1 addition & 1 deletion packages/api/test/unit/builder/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const testData: GenericServerTestCases<Endpoints> = {
},
submitBlindedBlock: {
args: {signedBlindedBlock: {data: ssz.electra.SignedBlindedBeaconBlock.defaultValue()}},
res: {data: ssz.deneb.ExecutionPayloadAndBlobsBundle.defaultValue(), meta: {version: ForkName.electra}},
res: {data: ssz.electra.ExecutionPayloadAndBlobsBundle.defaultValue(), meta: {version: ForkName.electra}},
},
submitBlindedBlockV2: {
args: {signedBlindedBlock: {data: ssz.fulu.SignedBlindedBeaconBlock.defaultValue()}},
Expand Down
28 changes: 18 additions & 10 deletions packages/beacon-node/src/api/impl/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isForkPostBellatrix,
isForkPostDeneb,
isForkPostElectra,
isForkPostFulu,
isForkPostGloas,
} from "@lodestar/params";
import {
Expand Down Expand Up @@ -1124,26 +1125,33 @@ export function getValidatorApi(
async getProposerDuties({epoch}, _context, opts?: {v2?: boolean}) {
notWhileSyncing();

// Early check that epoch is no more than current_epoch + 1, or allow for pre-genesis
const currentEpoch = currentEpochWithDisparity();
const nextEpoch = currentEpoch + 1;
if (currentEpoch >= 0 && epoch > nextEpoch) {
const startSlot = computeStartSlotAtEpoch(epoch);
const prepareNextSlotLookAheadMs =
config.SLOT_DURATION_MS - config.getSlotComponentDurationMs(PREPARE_NEXT_SLOT_BPS);
const toNextEpochMs = msToNextEpoch();
const nearNextEpoch = toNextEpochMs < prepareNextSlotLookAheadMs;
// Post-Fulu the proposer lookahead is deterministic and known a full epoch ahead, so
// close to the boundary `currentEpoch + 2` is serveable from the upcoming-epoch
// checkpoint state (its `nextProposers`). Pre-Fulu / mid-epoch: `currentEpoch + 1` max.
const isPostFulu = isForkPostFulu(config.getForkName(startSlot));
const maxFutureEpoch = isPostFulu && nearNextEpoch ? nextEpoch + 1 : nextEpoch;
if (currentEpoch >= 0 && epoch > maxFutureEpoch) {
Comment thread
wemeetagain marked this conversation as resolved.
Resolved
throw new ApiError(400, `Requested epoch ${epoch} must not be more than one epoch in the future`);
}

const head = chain.forkChoice.getHead();
let state: IBeaconStateView | undefined = undefined;
const startSlot = computeStartSlotAtEpoch(epoch);
const prepareNextSlotLookAheadMs =
config.SLOT_DURATION_MS - config.getSlotComponentDurationMs(PREPARE_NEXT_SLOT_BPS);
const toNextEpochMs = msToNextEpoch();
// validators may request next epoch's duties when it's close to next epoch
// this is to avoid missed block proposal due to 0 epoch look ahead
if (epoch === nextEpoch && toNextEpochMs < prepareNextSlotLookAheadMs) {
// this is to avoid missed block proposal due to 0 epoch look ahead.
// Post-Fulu, `nextEpoch + 1` is served from the same upcoming-epoch (`nextEpoch`)
// checkpoint state via its `nextProposers` (deterministic proposer lookahead).
if (nearNextEpoch && (epoch === nextEpoch || (isPostFulu && epoch === nextEpoch + 1))) {
// wait for maximum 1 slot for cp state which is the timeout of validator api
const cpState = await waitForCheckpointState({
rootHex: head.blockRoot,
epoch,
epoch: nextEpoch,
});
if (cpState) {
state = cpState;
Expand Down Expand Up @@ -1218,7 +1226,7 @@ export function getValidatorApi(
// It should be set to the latest block applied to `self` or the genesis block root.
const dependentRoot =
// In v2 the dependent root is different after fulu due to deterministic proposer lookahead
proposerShufflingDecisionRoot(opts?.v2 ? config.getForkName(startSlot) : ForkName.phase0, state) ||
proposerShufflingDecisionRoot(opts?.v2 ? config.getForkName(startSlot) : ForkName.phase0, state, epoch) ||
(await getGenesisBlockRoot(state));

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {afterEach, beforeEach, describe, expect, it, vi} from "vitest";
import {routes} from "@lodestar/api";
import {config} from "@lodestar/config/default";
import {MAX_EFFECTIVE_BALANCE, SLOTS_PER_EPOCH} from "@lodestar/params";
import {getConfig} from "@lodestar/config/test-utils";
import {ForkName, MAX_EFFECTIVE_BALANCE, SLOTS_PER_EPOCH} from "@lodestar/params";
import {BeaconStateAllForks, BeaconStateView} from "@lodestar/state-transition";
import {Slot} from "@lodestar/types";
import {SYNC_TOLERANCE_EPOCHS, getValidatorApi} from "../../../../../../src/api/impl/validator/index.js";
Expand All @@ -14,6 +14,8 @@ import {generateState, zeroProtoBlock} from "../../../../../utils/state.js";
import {generateValidators} from "../../../../../utils/validator.js";

describe("get proposers api impl", () => {
// mainnet is post-Fulu; test the realistic deterministic-proposer-lookahead path
const config = getConfig(ForkName.fulu);
const currentEpoch = 2;
const currentSlot = SLOTS_PER_EPOCH * currentEpoch;

Expand All @@ -25,7 +27,7 @@ describe("get proposers api impl", () => {
beforeEach(() => {
vi.useFakeTimers({now: 0});
vi.advanceTimersByTime(currentSlot * config.SLOT_DURATION_MS);
modules = getApiTestModules({clock: "real"});
modules = getApiTestModules({clock: "real", config});
api = getValidatorApi(defaultApiOptions, modules);

initializeState(currentSlot);
Expand Down Expand Up @@ -66,18 +68,18 @@ describe("get proposers api impl", () => {
vi.advanceTimersByTime((SYNC_TOLERANCE_EPOCHS * SLOTS_PER_EPOCH + 1) * config.SLOT_DURATION_MS);
vi.spyOn(modules.sync, "state", "get").mockReturnValue(SyncState.SyncingHead);

await expect(api.getProposerDuties({epoch: 1})).rejects.toThrow("Node is syncing - headSlot 0 currentSlot 97");
await expect(api.getProposerDutiesV2({epoch: 1})).rejects.toThrow("Node is syncing - headSlot 0 currentSlot 97");
});

it("should raise error if node stalled", async () => {
vi.advanceTimersByTime((SYNC_TOLERANCE_EPOCHS * SLOTS_PER_EPOCH + 1) * config.SLOT_DURATION_MS);
vi.spyOn(modules.sync, "state", "get").mockReturnValue(SyncState.Stalled);

await expect(api.getProposerDuties({epoch: 1})).rejects.toThrow("Node is syncing - waiting for peers");
await expect(api.getProposerDutiesV2({epoch: 1})).rejects.toThrow("Node is syncing - waiting for peers");
});

it("should get proposers for current epoch", async () => {
const {data: result} = (await api.getProposerDuties({epoch: currentEpoch})) as {
const {data: result} = (await api.getProposerDutiesV2({epoch: currentEpoch})) as {
data: routes.validator.ProposerDutyList;
};

Expand All @@ -92,7 +94,7 @@ describe("get proposers api impl", () => {

it("should get proposers for next epoch", async () => {
const nextEpoch = currentEpoch + 1;
const {data: result} = (await api.getProposerDuties({epoch: nextEpoch})) as {
const {data: result} = (await api.getProposerDutiesV2({epoch: nextEpoch})) as {
data: routes.validator.ProposerDutyList;
};

Expand All @@ -114,7 +116,7 @@ describe("get proposers api impl", () => {
finalized: true,
});

const {data: result} = (await api.getProposerDuties({epoch: historicalEpoch})) as {
const {data: result} = (await api.getProposerDutiesV2({epoch: historicalEpoch})) as {
data: routes.validator.ProposerDutyList;
};

Expand All @@ -126,16 +128,16 @@ describe("get proposers api impl", () => {
});

it("should raise error for more than one epoch in the future", async () => {
await expect(api.getProposerDuties({epoch: currentEpoch + 2})).rejects.toThrow(
await expect(api.getProposerDutiesV2({epoch: currentEpoch + 2})).rejects.toThrow(
"Requested epoch 4 must not be more than one epoch in the future"
);
});

it("should have different proposer validator public keys for current and next epoch", async () => {
const {data: currentProposers} = (await api.getProposerDuties({epoch: currentEpoch})) as {
const {data: currentProposers} = (await api.getProposerDutiesV2({epoch: currentEpoch})) as {
data: routes.validator.ProposerDutyList;
};
const {data: nextProposers} = (await api.getProposerDuties({epoch: currentEpoch + 1})) as {
const {data: nextProposers} = (await api.getProposerDutiesV2({epoch: currentEpoch + 1})) as {
data: routes.validator.ProposerDutyList;
};

Expand All @@ -144,21 +146,21 @@ describe("get proposers api impl", () => {
});

it("should have different proposer validator indexes for current and next epoch", async () => {
const {data: currentProposers} = (await api.getProposerDuties({epoch: currentEpoch})) as {
const {data: currentProposers} = (await api.getProposerDutiesV2({epoch: currentEpoch})) as {
data: routes.validator.ProposerDutyList;
};
const {data: nextProposers} = (await api.getProposerDuties({epoch: currentEpoch + 1})) as {
const {data: nextProposers} = (await api.getProposerDutiesV2({epoch: currentEpoch + 1})) as {
data: routes.validator.ProposerDutyList;
};

expect(currentProposers.map((p) => p.validatorIndex)).not.toEqual(nextProposers.map((p) => p.validatorIndex));
});

it("should have different proposer slots for current and next epoch", async () => {
const {data: currentProposers} = (await api.getProposerDuties({epoch: currentEpoch})) as {
const {data: currentProposers} = (await api.getProposerDutiesV2({epoch: currentEpoch})) as {
data: routes.validator.ProposerDutyList;
};
const {data: nextProposers} = (await api.getProposerDuties({epoch: currentEpoch + 1})) as {
const {data: nextProposers} = (await api.getProposerDutiesV2({epoch: currentEpoch + 1})) as {
data: routes.validator.ProposerDutyList;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/beacon-node/test/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function getApiTestModules(opts?: Partial<MockedBeaconChainOptions>): Api
const networkStub = getMockedNetwork();

return {
config,
config: opts?.config ?? config,
chain: chainStub,
sync: syncStub,
db: dbStub,
Expand Down
32 changes: 17 additions & 15 deletions packages/state-transition/src/util/shuffling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,31 @@ import {computeStartSlotAtEpoch} from "./epoch.js";
import {EpochShuffling} from "./epochShuffling.js";

/**
* Returns the block root which decided the proposer shuffling for the current epoch. This root
* can be used to key this proposer shuffling.
* Block root that decided the proposer shuffling for `proposalEpoch` (keys that shuffling).
* Computed for the requested epoch, not `state.epoch`, so it is correct when `state` is one
* epoch off the requested epoch (e.g. serving next-epoch duties from the current state).
*
* Returns `null` on the one-off scenario where the genesis block decides its own shuffling.
* It should be set to the latest block applied to this `state` or the genesis block root.
* Returns `null` when the genesis block decides its own shuffling (caller falls back to the
* genesis block root).
*/
export function proposerShufflingDecisionRoot(fork: ForkName, state: IBeaconStateView): Root | null {
const decisionSlot = proposerShufflingDecisionSlot(fork, state);
export function proposerShufflingDecisionRoot(
fork: ForkName,
state: IBeaconStateView,
proposalEpoch: Epoch
): Root | null {
const decisionSlot = proposerShufflingDecisionSlot(fork, proposalEpoch);
if (state.slot === decisionSlot) {
return null;
}
return state.getBlockRootAtSlot(decisionSlot);
}

/**
* Returns the slot at which the proposer shuffling was decided. The block root at this slot
* can be used to key the proposer shuffling for the current epoch.
*/
function proposerShufflingDecisionSlot(fork: ForkName, state: IBeaconStateView): Slot {
// After fulu, the decision slot is in previous epoch due to deterministic proposer lookahead
const epoch = isForkPostFulu(fork) ? state.epoch - 1 : state.epoch;
const startSlot = computeStartSlotAtEpoch(epoch);
return Math.max(startSlot - 1, 0);
/** Slot whose block root keys the proposer shuffling for `proposalEpoch`. */
function proposerShufflingDecisionSlot(fork: ForkName, proposalEpoch: Epoch): Slot {
// Post-Fulu the shuffling is decided one epoch earlier (deterministic proposer lookahead,
// MIN_SEED_LOOKAHEAD = 1); pre-Fulu it is the last block before `proposalEpoch`.
const decisionEpoch = isForkPostFulu(fork) ? proposalEpoch - 1 : proposalEpoch;
return Math.max(computeStartSlotAtEpoch(decisionEpoch) - 1, 0);
}

/**
Expand Down
67 changes: 50 additions & 17 deletions packages/validator/src/services/blockDuties.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ApiClient, routes} from "@lodestar/api";
import {ChainForkConfig} from "@lodestar/config";
import {isForkPostFulu} from "@lodestar/params";
import {computeEpochAtSlot, computeStartSlotAtEpoch} from "@lodestar/state-transition";
import {BLSPubkey, Epoch, RootHex, Slot} from "@lodestar/types";
import {sleep, toPubkeyHex} from "@lodestar/utils";
Expand All @@ -8,11 +9,14 @@ import {PubkeyHex} from "../types.js";
import {IClock, LoggerVc, differenceHex} from "../util/index.js";
import {ValidatorStore} from "./validatorStore.js";

/** This polls block duties 1s before the next epoch */
/**
* Epoch-boundary poll offset (~1s relative to the next slot) for next-epoch proposer duties.
* Pre-Fulu it is applied ~1s *before* the boundary; post-Gloas the same offset is mirrored
* to ~1s *after* the boundary. See `pollBeaconProposersNextEpochs`.
*/
// TODO: change to 8333 (5/6 of slot) to do it 2s before the next epoch
// once we have some improvement on epoch transition time
// see https://github.com/ChainSafe/lodestar/issues/5792#issuecomment-1647457442
// TODO GLOAS: re-evaluate timing
const BLOCK_DUTIES_LOOKAHEAD_BPS = 9167;
/** Only retain `HISTORICAL_DUTIES_EPOCHS` duties prior to the current epoch */
const HISTORICAL_DUTIES_EPOCHS = 2;
Expand Down Expand Up @@ -135,23 +139,22 @@ export class BlockDutiesService {
* However, we also have the slashing protection as a second line of defense. These two factors
* provide an acceptable level of safety.
*
* It's important to note that since there is a 0-epoch look-ahead (i.e., no look-ahead) for block
* proposers then it's very likely that a proposal for the first slot of the epoch will need go
* through the slow path every time. I.e., the proposal will only happen after we've been able to
* Pre-Fulu only: since proposer shuffling has a 0-epoch look-ahead (i.e., no look-ahead),
* it's very likely that a proposal for the first slot of the epoch will need to go through
* the slow path every time. I.e., the proposal will only happen after we've been able to
* download and process the duties from the BN. This means it is very important to ensure this
* function is as fast as possible.
* - Starting from Jul 2023, we poll proposers 1s before the next epoch thanks to PrepareNextSlotScheduler
* usually finishes in 3s.
* Post-Fulu the proposer lookahead is deterministic and known a full epoch ahead, so next-epoch
* duties are polled early/throughout the epoch (see `pollBeaconProposersNextEpochs`) and the
* first-slot-of-epoch slow-path penalty no longer applies.
*/
private async pollBeaconProposersAndNotify(currentSlot: Slot, signal: AbortSignal): Promise<void> {
const nextEpoch = computeEpochAtSlot(currentSlot) + 1;
const isLastSlotEpoch = computeStartSlotAtEpoch(nextEpoch) === currentSlot + 1;
if (isLastSlotEpoch) {
// no need to await for other steps, just poll proposers for next epoch
this.pollBeaconProposersNextEpoch(currentSlot, nextEpoch, signal).catch((e) => {
this.logger.error("Error on pollBeaconProposersNextEpoch", {}, e);
});
}
this.pollBeaconProposersNextEpochs(currentSlot, nextEpoch, signal).catch((e) => {
this.logger.error("Error on pollBeaconProposersNextEpochs", {}, e);
});

// Notify the block proposal service for any proposals that we have in our cache.
const initialBlockProposers = this.getblockProposersAtSlot(currentSlot);
Expand Down Expand Up @@ -180,16 +183,43 @@ export class BlockDutiesService {
}

/**
* This is to avoid some delay on the first slot of the epoch when validators have proposal duties.
* Pre-fulu: this is to avoid some delay on the first slot of the epoch when validators have proposal duties.
* See https://github.com/ChainSafe/lodestar/issues/5792
* Post-fulu:
* - if it's mid epoch, poll beacon proposers for the next epoch
* - if it's end of epoch, poll proposers for nextEpoch and (next Epoch + 1)
*/
private async pollBeaconProposersNextEpoch(currentSlot: Slot, nextEpoch: Epoch, signal: AbortSignal): Promise<void> {
private async pollBeaconProposersNextEpochs(currentSlot: Slot, nextEpoch: Epoch, signal: AbortSignal): Promise<void> {
const nextSlot = currentSlot + 1;
// `currentSlot` is the last slot of its epoch ⇒ `nextSlot` starts `nextEpoch`.
const isEpochBoundary = computeStartSlotAtEpoch(nextEpoch) === nextSlot;
const lookAheadMs =
this.config.SLOT_DURATION_MS - this.config.getSlotComponentDurationMs(BLOCK_DUTIES_LOOKAHEAD_BPS);
const fork = this.config.getForkName(nextSlot);

if (isForkPostFulu(fork)) {
if (isEpochBoundary) {
// Poll ~lookAheadMs before the boundary (same timing as pre-Fulu). The BN serves
// `nextEpoch + 1` from the upcoming-epoch checkpoint state (deterministic lookahead).
await sleep(this.clock.msToSlot(nextSlot) - lookAheadMs, signal);
this.logger.debug("Polling proposers for the next 2 epochs", {nextEpoch, currentSlot});
await this.pollBeaconProposers(nextEpoch);
await this.pollBeaconProposers(nextEpoch + 1);
return;
}

this.logger.debug("Polling proposers for the next epoch", {nextEpoch, currentSlot});
await this.pollBeaconProposers(nextEpoch);
return;
}

// Pre-Fulu: 0-epoch lookahead — only the last slot of the epoch matters. Sleep until
// ~1s before the boundary, then poll the next epoch once.
if (!isEpochBoundary) {
return;
}
await sleep(this.clock.msToSlot(nextSlot) - lookAheadMs, signal);
this.logger.debug("Polling proposers for next epoch", {nextEpoch, nextSlot});
// Poll proposers for the next epoch
this.logger.debug("Polling proposers for the next epoch", {nextEpoch, currentSlot});
await this.pollBeaconProposers(nextEpoch);
}

Expand All @@ -199,7 +229,10 @@ export class BlockDutiesService {
return;
}

const res = await this.api.validator.getProposerDuties({epoch});
// Post-Fulu the proposer dependent root changed (deterministic proposer lookahead)
const res = isForkPostFulu(this.config.getForkName(computeStartSlotAtEpoch(epoch)))
? await this.api.validator.getProposerDutiesV2({epoch})

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 means we will use v2 on mainnet with the next release, this could be problematic as this endpoint was added to the spec retroactively after fulu went live on mainnet, it might be fine but we should check if all clients implement the api already

ideally, we should avoid using v2 until after gloas

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yes this is a valid concern
added fallback mechanism in #9518

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.

but this is different from what I mean, we shouldn't be calling getProposerDutiesV2 at all from the vc before gloas

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.

: await this.api.validator.getProposerDuties({epoch});
const proposerDuties = res.value();
const {dependentRoot} = res.meta();
const relevantDuties = proposerDuties.filter((duty) => {
Expand Down
7 changes: 7 additions & 0 deletions packages/validator/test/unit/services/blockDuties.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ describe("BlockDutiesService", () => {
const secretKeys = Array.from({length: 3}, (_, i) => SecretKey.fromBytes(toBufferBE(BigInt(i + 1), 32)));
pubkeys = secretKeys.map((sk) => sk.toPublicKey().toBytes());
validatorStore = await initValidatorStore(secretKeys, api);

// Default config is pre-Fulu at the tested slots, so the v1 `getProposerDuties` path is
// exercised. Defensive default for the post-Fulu v2 branch so an unexpected fork config
// doesn't reject on an unstubbed mock.
api.validator.getProposerDutiesV2.mockResolvedValue(
mockApiResponse({data: [], meta: {dependentRoot: ZERO_HASH_HEX, executionOptimistic: false}})
);
});

let controller: AbortController; // To stop clock
Expand Down
1 change: 1 addition & 0 deletions packages/validator/test/utils/apiStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export function getApiClientStub(): ApiClientStub {
},
validator: {
getProposerDuties: vi.fn(),
getProposerDutiesV2: vi.fn(),
getAttesterDuties: vi.fn(),
getPtcDuties: vi.fn(),
prepareBeaconCommitteeSubnet: vi.fn(),
Expand Down
Loading