Skip to content
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
23 changes: 7 additions & 16 deletions packages/cli/src/cmds/beacon/initBeaconState.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import {ssz} from "@lodestar/types";
import {createBeaconConfig, BeaconConfig, ChainForkConfig} from "@lodestar/config";
import {Logger} from "@lodestar/utils";
import {
getLatestBlockRoot,
isWithinWeakSubjectivityPeriod,
BeaconStateAllForks,
computeCheckpointEpochAtStateSlot,
} from "@lodestar/state-transition";
import {isWithinWeakSubjectivityPeriod, BeaconStateAllForks} from "@lodestar/state-transition";
import {
IBeaconDb,
IBeaconNodeOptions,
Expand All @@ -18,18 +13,14 @@ import {Checkpoint} from "@lodestar/types/phase0";

import {downloadOrLoadFile} from "../../util/index.js";
import {defaultNetwork, GlobalArgs} from "../../options/globalOptions.js";
import {fetchWeakSubjectivityState, getCheckpointFromArg, getGenesisFileUrl} from "../../networks/index.js";
import {
fetchWeakSubjectivityState,
getCheckpointFromArg,
getGenesisFileUrl,
getCheckpointFromState,
} from "../../networks/index.js";
import {BeaconArgs} from "./options.js";

export function getCheckpointFromState(state: BeaconStateAllForks): Checkpoint {
return {
// 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),
};
}

async function initAndVerifyWeakSubjectivityState(
config: BeaconConfig,
db: IBeaconDb,
Expand Down
40 changes: 26 additions & 14 deletions packages/cli/src/networks/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import fs from "node:fs";
import got from "got";
import {SLOTS_PER_EPOCH, ForkName} from "@lodestar/params";
import {SLOTS_PER_EPOCH} from "@lodestar/params";
import {ApiError, getClient} from "@lodestar/api";
import {getStateTypeFromBytes} from "@lodestar/beacon-node";
import {ChainConfig, ChainForkConfig} from "@lodestar/config";
import {Checkpoint} from "@lodestar/types/phase0";
import {Slot} from "@lodestar/types";
import {fromHex, callFnWhenAwait, Logger} from "@lodestar/utils";
import {BeaconStateAllForks} from "@lodestar/state-transition";
import {BeaconStateAllForks, getLatestBlockRoot, computeCheckpointEpochAtStateSlot} from "@lodestar/state-transition";
import {parseBootnodesFile} from "../util/format.js";
import * as mainnet from "./mainnet.js";
import * as dev from "./dev.js";
Expand Down Expand Up @@ -140,20 +141,21 @@ export async function fetchWeakSubjectivityState(
{checkpointSyncUrl, wssCheckpoint}: {checkpointSyncUrl: string; wssCheckpoint?: string}
): Promise<{wsState: BeaconStateAllForks; wsCheckpoint: Checkpoint}> {
try {
let wsCheckpoint;
let wsCheckpoint: Checkpoint | null;
let stateId: Slot | "finalized";

const api = getClient({baseUrl: checkpointSyncUrl}, {config});
if (wssCheckpoint) {
wsCheckpoint = getCheckpointFromArg(wssCheckpoint);
stateId = wsCheckpoint.epoch * SLOTS_PER_EPOCH;
} else {
const res = await api.beacon.getStateFinalityCheckpoints("head");
ApiError.assert(res, "Can not fetch finalized checkpoint");
wsCheckpoint = res.response.data.finalized;
// Fetch current finalized state and extract checkpoint from it
stateId = "finalized";
wsCheckpoint = null;
}
const stateSlot = wsCheckpoint.epoch * SLOTS_PER_EPOCH;
const getStatePromise =
config.getForkName(stateSlot) === ForkName.phase0
? api.debug.getState(`${stateSlot}`, "ssz")
: api.debug.getStateV2(`${stateSlot}`, "ssz");

// getStateV2 should be available for all forks including phase0
const getStatePromise = api.debug.getStateV2(stateId, "ssz");

const stateBytes = await callFnWhenAwait(
getStatePromise,
Expand All @@ -164,11 +166,12 @@ export async function fetchWeakSubjectivityState(
return res.response;
});

logger.info("Download completed");
logger.info("Download completed", {stateId});
const wsState = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes);

return {
wsState: getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes),
wsCheckpoint,
wsState,
wsCheckpoint: wsCheckpoint ?? getCheckpointFromState(wsState),
};
} catch (e) {
throw new Error("Unable to fetch weak subjectivity state: " + (e as Error).message);
Expand All @@ -183,3 +186,12 @@ export function getCheckpointFromArg(checkpointStr: string): Checkpoint {
}
return {root: fromHex(match[1]), epoch: parseInt(match[2])};
}

export function getCheckpointFromState(state: BeaconStateAllForks): Checkpoint {
return {
// 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),
};
}