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

Fix computed db checkpoint for weak subjectivity checks #4575

Merged
merged 4 commits into from
Sep 21, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/beacon-node/src/chain/initState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
computeEpochAtSlot,
BeaconStateAllForks,
CachedBeaconStateAllForks,
computeCheckpointEpochAtStateSlot,
} from "@lodestar/state-transition";
import {phase0, allForks, ssz} from "@lodestar/types";
import {IChainForkConfig} from "@lodestar/config";
import {ILogger} from "@lodestar/utils";
import {toHexString} from "@chainsafe/ssz";
import {SLOTS_PER_EPOCH} from "@lodestar/params";
import {GENESIS_SLOT, ZERO_HASH} from "../constants/index.js";
import {IBeaconDb} from "../db/index.js";
import {Eth1Provider} from "../eth1/index.js";
Expand Down Expand Up @@ -223,7 +223,7 @@ export function computeAnchorCheckpoint(
root,
// the checkpoint epoch = computeEpochAtSlot(anchorState.slot) + 1 if slot is not at epoch boundary
// this is similar to a process_slots() call
epoch: Math.ceil(anchorState.slot / SLOTS_PER_EPOCH),
epoch: computeCheckpointEpochAtStateSlot(anchorState.slot),
},
blockHeader,
};
Expand Down
6 changes: 4 additions & 2 deletions packages/cli/src/cmds/beacon/initBeaconState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import {ssz} from "@lodestar/types";
import {createIBeaconConfig, IBeaconConfig, IChainForkConfig} from "@lodestar/config";
import {ILogger} from "@lodestar/utils";
import {
computeEpochAtSlot,
getLatestBlockRoot,
isWithinWeakSubjectivityPeriod,
BeaconStateAllForks,
computeCheckpointEpochAtStateSlot,
} from "@lodestar/state-transition";
import {
IBeaconDb,
Expand All @@ -23,7 +23,9 @@ import {IBeaconArgs} from "./options.js";

export function getCheckpointFromState(state: BeaconStateAllForks): Checkpoint {
return {
epoch: computeEpochAtSlot(state.latestBlockHeader.slot),
// the correct checkpoint is based on state's slot, its latestBlockHeader's slot's epoch can be
// behind the state
epoch: computeCheckpointEpochAtStateSlot(state.slot),
root: getLatestBlockRoot(state),
};
}
Expand Down
10 changes: 10 additions & 0 deletions packages/state-transition/src/util/epoch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ export function computeEpochAtSlot(slot: Slot): Epoch {
return Math.floor(slot / SLOTS_PER_EPOCH);
}

/**
* Return the epoch at the state slot for purposes of checkpoint.
* Ideally this slot % SLOTS_PER_EPOCH === 0, but just to handle the improbable case of
* non boundary slot, using ceil so that the state's latestBlockHeader would always
* lie before this epooch
*/
export function computeCheckpointEpochAtStateSlot(slot: Slot): Epoch {
return Math.ceil(slot / SLOTS_PER_EPOCH);
}

/**
* Return the starting slot of the given epoch.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/state-transition/src/util/weakSubjectivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {Checkpoint} from "@lodestar/types/phase0";
import {toHexString} from "@chainsafe/ssz";
import {ZERO_HASH} from "../constants/constants.js";
import {CachedBeaconStateAllForks, BeaconStateAllForks} from "../types.js";
import {computeEpochAtSlot, getCurrentEpoch} from "./epoch.js";
import {computeEpochAtSlot, getCurrentEpoch, computeCheckpointEpochAtStateSlot} from "./epoch.js";
import {getCurrentSlot} from "./slot.js";
import {getActiveValidatorIndices, getChurnLimit} from "./validator.js";

Expand Down Expand Up @@ -111,7 +111,7 @@ export function isWithinWeakSubjectivityPeriod(
wsState: BeaconStateAllForks,
wsCheckpoint: Checkpoint
): boolean {
const wsStateEpoch = computeEpochAtSlot(wsState.slot);
const wsStateEpoch = computeCheckpointEpochAtStateSlot(wsState.slot);
const blockRoot = getLatestBlockRoot(wsState);
if (!ssz.Root.equals(blockRoot, wsCheckpoint.root)) {
throw new Error(
Expand Down