Skip to content

Commit

Permalink
Merge f49af09 into 23c88a2
Browse files Browse the repository at this point in the history
  • Loading branch information
g11tech committed Sep 21, 2022
2 parents 23c88a2 + f49af09 commit 03f53e3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
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

0 comments on commit 03f53e3

Please sign in to comment.