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

chore: ensureWithinWeakSubjectivityPeriod #5837

Merged
merged 2 commits into from
Aug 3, 2023
Merged
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
10 changes: 6 additions & 4 deletions packages/cli/src/cmds/beacon/initBeaconState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import {ssz} from "@lodestar/types";
import {createBeaconConfig, BeaconConfig, ChainForkConfig} from "@lodestar/config";
import {Logger} from "@lodestar/utils";
import {isWithinWeakSubjectivityPeriod, BeaconStateAllForks} from "@lodestar/state-transition";
import {
isWithinWeakSubjectivityPeriod,
ensureWithinWeakSubjectivityPeriod,
BeaconStateAllForks,
} from "@lodestar/state-transition";
import {
IBeaconDb,
IBeaconNodeOptions,
Expand Down Expand Up @@ -54,9 +58,7 @@ async function initAndVerifyWeakSubjectivityState(

// Instead of warning user of wss check failure, we throw because user explicity wants to use
// the checkpoint sync
if (!isWithinWeakSubjectivityPeriod(config, anchorState, anchorCheckpoint)) {
throw new Error("Fetched weak subjectivity checkpoint not within weak subjectivity period.");
}
ensureWithinWeakSubjectivityPeriod(config, anchorState, anchorCheckpoint);

anchorState = await initStateFromAnchorState(config, db, logger, anchorState, {
isWithinWeakSubjectivityPeriod: true,
Expand Down
19 changes: 18 additions & 1 deletion packages/state-transition/src/util/weakSubjectivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ export function isWithinWeakSubjectivityPeriod(
wsState: BeaconStateAllForks,
wsCheckpoint: Checkpoint
): boolean {
try {
ensureWithinWeakSubjectivityPeriod(config, wsState, wsCheckpoint);
return true;
} catch (_) {
return false;
}
}

export function ensureWithinWeakSubjectivityPeriod(
config: BeaconConfig,
wsState: BeaconStateAllForks,
wsCheckpoint: Checkpoint
): void {
const wsStateEpoch = computeCheckpointEpochAtStateSlot(wsState.slot);
const blockRoot = getLatestBlockRoot(wsState);
if (!ssz.Root.equals(blockRoot, wsCheckpoint.root)) {
Expand All @@ -123,5 +136,9 @@ export function isWithinWeakSubjectivityPeriod(
}
const wsPeriod = computeWeakSubjectivityPeriod(config, wsState);
const clockEpoch = computeEpochAtSlot(getCurrentSlot(config, wsState.genesisTime));
return clockEpoch <= wsStateEpoch + wsPeriod;
if (clockEpoch > wsStateEpoch + wsPeriod) {
throw new Error(
`The downloaded state with epoch ${wsStateEpoch} is not within subjectivity period of ${wsPeriod} from the current epoch ${clockEpoch}. Please verify your checkpoint source`
);
}
}
Loading