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

weak subjecitivity checks #3391

Merged
merged 5 commits into from
Nov 4, 2021
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
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;
}
28 changes: 25 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,32 @@ 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"
);
}

// Pick the state which is ahead as an anchor to initialize the beacon chain
let anchorState = wsState;
let anchorCheckpoint = wsCheckpoint;
if (store.slot > wsState.slot) {
anchorState = store;
anchorCheckpoint = getCheckpointFromState(config, store);
logger.verbose(
"Db state is ahead of the provided checkpoint state, using the db state to initialize the beacon chain"
);
}

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

return await initStateFromAnchorState(config, db, logger, anchorState);
}

/**
Expand Down