Skip to content

Commit

Permalink
rectifying wssync check and state initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Oct 31, 2021
1 parent aafc5d7 commit 10d7aa6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import {ssz} from "@chainsafe/lodestar-types";
import {Checkpoint} from "@chainsafe/lodestar-types/phase0";
import {toHexString} from "@chainsafe/ssz";
import {CachedBeaconState} from ".";
import {getActiveValidatorIndices, getCurrentEpoch, computeEpochAtSlot, ZERO_HASH, getChurnLimit} from "../..";
import {
getActiveValidatorIndices,
getCurrentEpoch,
computeEpochAtSlot,
ZERO_HASH,
getChurnLimit,
getCurrentSlot,
} from "../..";

export const ETH_TO_GWEI = 10 ** 9;
const SAFETY_DECAY = BigInt(10);
Expand Down Expand Up @@ -109,7 +116,6 @@ export function getLatestBlockRoot(config: IChainForkConfig, state: allForks.Bea

export function isWithinWeakSubjectivityPeriod(
config: IBeaconConfig,
store: allForks.BeaconState,
wsState: allForks.BeaconState,
wsCheckpoint: Checkpoint
): boolean {
Expand All @@ -124,6 +130,6 @@ export function isWithinWeakSubjectivityPeriod(
throw new Error(`Epochs do not match. expected=${wsCheckpoint.epoch}, actual=${wsStateEpoch}`);
}
const wsPeriod = computeWeakSubjectivityPeriod(config, wsState);
const currentEpoch = getCurrentEpoch(store);
return currentEpoch <= wsStateEpoch + wsPeriod;
const clockEpoch = computeEpochAtSlot(getCurrentSlot(config, wsState.genesisTime));
return clockEpoch <= wsStateEpoch + wsPeriod;
}
19 changes: 16 additions & 3 deletions packages/cli/src/cmds/beacon/initBeaconState.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AbortSignal} from "@chainsafe/abort-controller";

import {ssz} from "@chainsafe/lodestar-types";
import {TreeBacked} from "@chainsafe/ssz";
import {createIBeaconConfig, IBeaconConfig, IChainForkConfig} from "@chainsafe/lodestar-config";
import {fromHex, ILogger} from "@chainsafe/lodestar-utils";
Expand Down Expand Up @@ -39,10 +39,23 @@ async function initAndVerifyWeakSubjectivityState(
wsState: TreeBacked<allForks.BeaconState>,
wsCheckpoint: Checkpoint
): Promise<TreeBacked<allForks.BeaconState>> {
if (!allForks.isWithinWeakSubjectivityPeriod(config, store, wsState, wsCheckpoint)) {
// Check if the store's state and wsState are compatible
if (
store.genesisTime !== wsState.genesisTime ||
!ssz.Root.equals(store.genesisValidatorsRoot, wsState.genesisValidatorsRoot)
) {
throw new Error(
"Db state and checkpoint state are not compatible, either clear the db or verify your checkpoint source"
);
}

if (!allForks.isWithinWeakSubjectivityPeriod(config, wsState, wsCheckpoint)) {
throw new Error("Fetched weak subjectivity checkpoint not within weak subjectivity period.");
}
return await initStateFromAnchorState(config, db, logger, wsState);

// Pick the state which is ahead as an anchor to initialize the beacon chain
const anchorState = wsState.slot > store.slot ? wsState : store;
return await initStateFromAnchorState(config, db, logger, anchorState);
}

/**
Expand Down

0 comments on commit 10d7aa6

Please sign in to comment.