Skip to content

Commit

Permalink
Fix the init state logic
Browse files Browse the repository at this point in the history
  • Loading branch information
nazarhussain committed Oct 23, 2024
1 parent 1314976 commit f091f6a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
27 changes: 16 additions & 11 deletions packages/beacon-node/src/chain/initState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,26 @@ export async function persistGenesisResult({
]);
}

export async function persistAnchorState(
config: ChainForkConfig,
db: IBeaconDb,
anchorState: BeaconStateAllForks,
anchorStateBytes: Uint8Array
): Promise<void> {
export async function persistAnchorState({
config,
db,
anchorState,
archiveMode,
}: {
config: ChainForkConfig;
db: IBeaconDb;
anchorState: BeaconStateAllForks;
archiveMode: StateArchiveMode;
}): Promise<void> {
if (anchorState.slot === GENESIS_SLOT) {
const genesisBlock = createGenesisBlock(config, anchorState);
await Promise.all([
db.blockArchive.add(genesisBlock),
db.block.add(genesisBlock),
db.stateArchive.putBinary(anchorState.slot, anchorStateBytes),
storeGenesisState(anchorState.toValue(), {db, forkConfig: config, archiveMode}),
]);
} else {
await db.stateArchive.putBinary(anchorState.slot, anchorStateBytes);
await storeGenesisState(anchorState.toValue(), {db, forkConfig: config, archiveMode});
}
}

Expand Down Expand Up @@ -146,11 +151,11 @@ export async function checkAndPersistAnchorState(
db: IBeaconDb,
logger: Logger,
anchorState: BeaconStateAllForks,
anchorStateBytes: Uint8Array,
{
isWithinWeakSubjectivityPeriod,
isCheckpointState,
}: {isWithinWeakSubjectivityPeriod: boolean; isCheckpointState: boolean}
archiveMode,
}: {isWithinWeakSubjectivityPeriod: boolean; isCheckpointState: boolean; archiveMode: StateArchiveMode}
): Promise<void> {
const expectedFork = config.getForkInfo(computeStartSlotAtEpoch(anchorState.fork.epoch));
const expectedForkVersion = toHex(expectedFork.version);
Expand Down Expand Up @@ -180,7 +185,7 @@ export async function checkAndPersistAnchorState(
}

if (isCheckpointState || anchorState.slot === GENESIS_SLOT) {
await persistAnchorState(config, db, anchorState, anchorStateBytes);
await persistAnchorState({config, db, anchorState, archiveMode});
}
}

Expand Down
26 changes: 18 additions & 8 deletions packages/cli/src/cmds/beacon/initBeaconState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
HierarchicalLayers,
getLastStoredState,
migrateStateArchive,
StateArchiveMode,
} from "@lodestar/beacon-node";
import {Checkpoint} from "@lodestar/types/phase0";

Expand All @@ -39,7 +40,7 @@ async function initAndVerifyWeakSubjectivityState(
dbStateBytes: StateWithBytes,
wsStateBytes: StateWithBytes,
wsCheckpoint: Checkpoint,
opts: {ignoreWeakSubjectivityCheck?: boolean} = {}
opts: {ignoreWeakSubjectivityCheck?: boolean; archiveMode: StateArchiveMode}
): Promise<{anchorState: BeaconStateAllForks; wsCheckpoint: Checkpoint}> {
const dbState = dbStateBytes.state;
const wsState = wsStateBytes.state;
Expand Down Expand Up @@ -74,9 +75,10 @@ async function initAndVerifyWeakSubjectivityState(
throw wssCheck.err;
}

await checkAndPersistAnchorState(config, db, logger, anchorState.state, anchorState.stateBytes, {
await checkAndPersistAnchorState(config, db, logger, anchorState.state, {
isWithinWeakSubjectivityPeriod,
isCheckpointState,
archiveMode: opts.archiveMode,
});

// Return the latest anchorState but still return original wsCheckpoint to validate in backfill
Expand Down Expand Up @@ -152,9 +154,10 @@ export async function initBeaconState(
// this never happens
throw Error(`There is no stateBytes for the lastDbState at slot ${lastDbState.slot}`);
}
await checkAndPersistAnchorState(config, db, logger, lastDbState, stateBytes, {
await checkAndPersistAnchorState(config, db, logger, lastDbState, {
isWithinWeakSubjectivityPeriod: wssCheck,
isCheckpointState: false,
archiveMode: options.chain.stateArchiveMode,
});
return {anchorState: lastDbState};
}
Expand All @@ -173,7 +176,8 @@ export async function initBeaconState(
},
chainForkConfig,
db,
logger
logger,
{archiveMode: options.chain.stateArchiveMode}
);
}

Expand All @@ -188,7 +192,8 @@ export async function initBeaconState(
},
chainForkConfig,
db,
logger
logger,
{archiveMode: options.chain.stateArchiveMode}
);
}

Expand All @@ -198,9 +203,10 @@ export async function initBeaconState(
const anchorState = getStateTypeFromBytes(chainForkConfig, stateBytes).deserializeToViewDU(stateBytes);
const config = createBeaconConfig(chainForkConfig, anchorState.genesisValidatorsRoot);
const wssCheck = isWithinWeakSubjectivityPeriod(config, anchorState, getCheckpointFromState(anchorState));
await checkAndPersistAnchorState(config, db, logger, anchorState, stateBytes, {
await checkAndPersistAnchorState(config, db, logger, anchorState, {
isWithinWeakSubjectivityPeriod: wssCheck,
isCheckpointState: true,
archiveMode: options.chain.stateArchiveMode,
});
return {anchorState};
}
Expand All @@ -222,7 +228,8 @@ async function readWSState(
wssOpts: {checkpointState: string; wssCheckpoint?: string; ignoreWeakSubjectivityCheck?: boolean},
chainForkConfig: ChainForkConfig,
db: IBeaconDb,
logger: Logger
logger: Logger,
opts: {archiveMode: StateArchiveMode}
): Promise<{anchorState: BeaconStateAllForks; wsCheckpoint?: Checkpoint}> {
// weak subjectivity sync from a provided state file:
// if a weak subjectivity checkpoint has been provided, it is used for additional verification
Expand All @@ -244,6 +251,7 @@ async function readWSState(
const checkpoint = wssCheckpoint ? getCheckpointFromArg(wssCheckpoint) : getCheckpointFromState(wsState);
return initAndVerifyWeakSubjectivityState(config, db, logger, store, wsStateBytes, checkpoint, {
ignoreWeakSubjectivityCheck,
archiveMode: opts.archiveMode,
});
}

Expand All @@ -253,7 +261,8 @@ async function fetchWSStateFromBeaconApi(
wssOpts: {checkpointSyncUrl: string; wssCheckpoint?: string; ignoreWeakSubjectivityCheck?: boolean},
chainForkConfig: ChainForkConfig,
db: IBeaconDb,
logger: Logger
logger: Logger,
opts: {archiveMode: StateArchiveMode}
): Promise<{anchorState: BeaconStateAllForks; wsCheckpoint?: Checkpoint}> {
// weak subjectivity sync from a state that needs to be fetched:
// if a weak subjectivity checkpoint has been provided, it is used to inform which state to download and used for additional verification
Expand All @@ -280,5 +289,6 @@ async function fetchWSStateFromBeaconApi(
const store = lastDbStateBytes ?? wsStateWithBytes;
return initAndVerifyWeakSubjectivityState(config, db, logger, store, wsStateWithBytes, wsCheckpoint, {
ignoreWeakSubjectivityCheck: wssOpts.ignoreWeakSubjectivityCheck,
archiveMode: opts.archiveMode,
});
}

0 comments on commit f091f6a

Please sign in to comment.