From 292d1f9d58c47ba33a4604f663d892ee4154a01c Mon Sep 17 00:00:00 2001 From: harkamal Date: Tue, 28 Mar 2023 14:18:15 +0530 Subject: [PATCH 1/2] Use finalized state id when no checkpoint provided in checkpoint sync --- .../cli/src/cmds/beacon/initBeaconState.ts | 23 ++++------- packages/cli/src/networks/index.ts | 38 ++++++++++++------- 2 files changed, 32 insertions(+), 29 deletions(-) diff --git a/packages/cli/src/cmds/beacon/initBeaconState.ts b/packages/cli/src/cmds/beacon/initBeaconState.ts index 07ef5e3926ee..bbfce46a7ea1 100644 --- a/packages/cli/src/cmds/beacon/initBeaconState.ts +++ b/packages/cli/src/cmds/beacon/initBeaconState.ts @@ -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, @@ -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, diff --git a/packages/cli/src/networks/index.ts b/packages/cli/src/networks/index.ts index 7dad5fcfcec1..13ef24ee08be 100644 --- a/packages/cli/src/networks/index.ts +++ b/packages/cli/src/networks/index.ts @@ -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"; @@ -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, @@ -165,10 +167,11 @@ export async function fetchWeakSubjectivityState( }); logger.info("Download completed"); + 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); @@ -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), + }; +} From 6b324d508b8cab1deb5ab490249b8ad0feafbf4c Mon Sep 17 00:00:00 2001 From: harkamal Date: Tue, 28 Mar 2023 14:36:11 +0530 Subject: [PATCH 2/2] add stateid to log --- packages/cli/src/networks/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/src/networks/index.ts b/packages/cli/src/networks/index.ts index 13ef24ee08be..8e5e9c460a81 100644 --- a/packages/cli/src/networks/index.ts +++ b/packages/cli/src/networks/index.ts @@ -166,7 +166,7 @@ export async function fetchWeakSubjectivityState( return res.response; }); - logger.info("Download completed"); + logger.info("Download completed", {stateId}); const wsState = getStateTypeFromBytes(config, stateBytes).deserializeToViewDU(stateBytes); return {