From cb554d2a23b3260b22518718f681ea10f997c47b Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Wed, 2 Aug 2023 13:22:53 +0700 Subject: [PATCH 1/2] chore: ensureWithinWeakSubjectivityPeriod --- .../cli/src/cmds/beacon/initBeaconState.ts | 10 ++++++---- .../src/util/weakSubjectivity.ts | 19 ++++++++++++++++++- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/cli/src/cmds/beacon/initBeaconState.ts b/packages/cli/src/cmds/beacon/initBeaconState.ts index f5e82a7fd987..0185406db443 100644 --- a/packages/cli/src/cmds/beacon/initBeaconState.ts +++ b/packages/cli/src/cmds/beacon/initBeaconState.ts @@ -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, @@ -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, diff --git a/packages/state-transition/src/util/weakSubjectivity.ts b/packages/state-transition/src/util/weakSubjectivity.ts index dec0c757d985..98d98cabfc16 100644 --- a/packages/state-transition/src/util/weakSubjectivity.ts +++ b/packages/state-transition/src/util/weakSubjectivity.ts @@ -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)) { @@ -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 please verify your checkpoint source` + ); + } } From 231396dd21067b56c59329929b864a62d44b14a9 Mon Sep 17 00:00:00 2001 From: Tuyen Nguyen Date: Wed, 2 Aug 2023 19:29:18 +0700 Subject: [PATCH 2/2] fix: error message --- packages/state-transition/src/util/weakSubjectivity.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/state-transition/src/util/weakSubjectivity.ts b/packages/state-transition/src/util/weakSubjectivity.ts index 98d98cabfc16..07f0fe764d6a 100644 --- a/packages/state-transition/src/util/weakSubjectivity.ts +++ b/packages/state-transition/src/util/weakSubjectivity.ts @@ -138,7 +138,7 @@ export function ensureWithinWeakSubjectivityPeriod( const clockEpoch = computeEpochAtSlot(getCurrentSlot(config, wsState.genesisTime)); 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 please verify your checkpoint source` + `The downloaded state with epoch ${wsStateEpoch} is not within subjectivity period of ${wsPeriod} from the current epoch ${clockEpoch}. Please verify your checkpoint source` ); } }