Skip to content

Commit

Permalink
weak subjecitivity checks (#3391)
Browse files Browse the repository at this point in the history
* rectifying wssync check and state initialization

* lint

* checking db state for also wss period check

* Update packages/cli/src/cmds/beacon/initBeaconState.ts

* lint - prettier

Co-authored-by: Cayman <caymannava@gmail.com>
  • Loading branch information
g11tech and wemeetagain authored Nov 4, 2021
1 parent da26111 commit 70a534d
Show file tree
Hide file tree
Showing 2 changed files with 35 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;
}
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

0 comments on commit 70a534d

Please sign in to comment.