@chainsafe/lodestar 1.50.0-dev.ebecebb9ba → 1.50.0-dev.ece0239bd3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/.git-data.json +1 -1
  2. package/lib/cmds/beacon/handler.d.ts.map +1 -1
  3. package/lib/cmds/beacon/handler.js +3 -16
  4. package/lib/cmds/beacon/handler.js.map +1 -1
  5. package/lib/cmds/beacon/initBeaconState.d.ts +11 -17
  6. package/lib/cmds/beacon/initBeaconState.d.ts.map +1 -1
  7. package/lib/cmds/beacon/initBeaconState.js +78 -274
  8. package/lib/cmds/beacon/initBeaconState.js.map +1 -1
  9. package/lib/cmds/beacon/options.d.ts +0 -1
  10. package/lib/cmds/beacon/options.d.ts.map +1 -1
  11. package/lib/cmds/beacon/options.js +0 -5
  12. package/lib/cmds/beacon/options.js.map +1 -1
  13. package/lib/cmds/beacon/stateInitialization/checkpointState.d.ts +7 -0
  14. package/lib/cmds/beacon/stateInitialization/checkpointState.d.ts.map +1 -0
  15. package/lib/cmds/beacon/stateInitialization/checkpointState.js +168 -0
  16. package/lib/cmds/beacon/stateInitialization/checkpointState.js.map +1 -0
  17. package/lib/cmds/beacon/stateInitialization/errors.d.ts +17 -0
  18. package/lib/cmds/beacon/stateInitialization/errors.d.ts.map +1 -0
  19. package/lib/cmds/beacon/stateInitialization/errors.js +16 -0
  20. package/lib/cmds/beacon/stateInitialization/errors.js.map +1 -0
  21. package/lib/cmds/beacon/stateInitialization/genesisState.d.ts +3 -0
  22. package/lib/cmds/beacon/stateInitialization/genesisState.d.ts.map +1 -0
  23. package/lib/cmds/beacon/stateInitialization/genesisState.js +65 -0
  24. package/lib/cmds/beacon/stateInitialization/genesisState.js.map +1 -0
  25. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.d.ts +4 -0
  26. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.d.ts.map +1 -0
  27. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.js +52 -0
  28. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.js.map +1 -0
  29. package/lib/cmds/beacon/stateInitialization/types.d.ts +40 -0
  30. package/lib/cmds/beacon/stateInitialization/types.d.ts.map +1 -0
  31. package/lib/cmds/beacon/stateInitialization/types.js +2 -0
  32. package/lib/cmds/beacon/stateInitialization/types.js.map +1 -0
  33. package/lib/cmds/beacon/stateInitialization/validation.d.ts +4 -0
  34. package/lib/cmds/beacon/stateInitialization/validation.d.ts.map +1 -0
  35. package/lib/cmds/beacon/stateInitialization/validation.js +12 -0
  36. package/lib/cmds/beacon/stateInitialization/validation.js.map +1 -0
  37. package/lib/networks/index.d.ts +3 -9
  38. package/lib/networks/index.d.ts.map +1 -1
  39. package/lib/networks/index.js +15 -28
  40. package/lib/networks/index.js.map +1 -1
  41. package/lib/options/beaconNodeOptions/chain.js +1 -1
  42. package/lib/options/beaconNodeOptions/chain.js.map +1 -1
  43. package/package.json +14 -14
  44. package/src/cmds/beacon/handler.ts +4 -16
  45. package/src/cmds/beacon/initBeaconState.ts +119 -389
  46. package/src/cmds/beacon/options.ts +0 -7
  47. package/src/cmds/beacon/stateInitialization/checkpointState.ts +256 -0
  48. package/src/cmds/beacon/stateInitialization/errors.ts +15 -0
  49. package/src/cmds/beacon/stateInitialization/genesisState.ts +85 -0
  50. package/src/cmds/beacon/stateInitialization/prepareStateInitialization.ts +64 -0
  51. package/src/cmds/beacon/stateInitialization/types.ts +58 -0
  52. package/src/cmds/beacon/stateInitialization/validation.ts +16 -0
  53. package/src/networks/index.ts +25 -40
  54. package/src/options/beaconNodeOptions/chain.ts +1 -1
@@ -1,100 +1,36 @@
1
+ import {pubkeyCache} from "@chainsafe/lodestar-z/pubkeys";
2
+ import {IBeaconDb} from "@lodestar/beacon-node";
3
+ import {BeaconConfig, ChainForkConfig} from "@lodestar/config";
4
+ import {MAX_PENDING_DEPOSITS_PER_EPOCH, SLOTS_PER_EPOCH} from "@lodestar/params";
1
5
  import {
2
- DbCPStateDatastore,
3
- FileCPStateDatastore,
4
- IBeaconDb,
5
- checkAndPersistAnchorState,
6
- getStateTypeFromBytes,
7
- } from "@lodestar/beacon-node";
8
- import {BeaconConfig, ChainForkConfig, createBeaconConfig} from "@lodestar/config";
9
- import {
10
- BeaconStateAllForks,
11
- computeAnchorCheckpoint,
12
- computeEpochAtSlot,
13
- ensureWithinWeakSubjectivityPeriod,
14
- isWithinWeakSubjectivityPeriod,
15
- loadState,
16
- loadStateAndValidators,
6
+ IBeaconStateView,
7
+ computeWeakSubjectivitySummaryFromStateBytes,
8
+ createBeaconStateView,
9
+ getValidatorCountFromStateBytes,
10
+ isWithinWeakSubjectivityPeriodFromSummary,
11
+ readBeaconStateBytesMetadata,
17
12
  } from "@lodestar/state-transition";
18
- import {ssz} from "@lodestar/types";
19
- import {Checkpoint} from "@lodestar/types/phase0";
20
- import {Logger, formatBytes, toRootHex} from "@lodestar/utils";
21
- import {
22
- fetchWeakSubjectivityState,
23
- getCheckpointFromArg,
24
- getCheckpointFromState,
25
- getGenesisFileUrl,
26
- getGenesisStateRoot,
27
- } from "../../networks/index.js";
28
- import {GlobalArgs, defaultNetwork} from "../../options/globalOptions.js";
29
- import {downloadOrLoadFile, wrapFnError} from "../../util/index.js";
13
+ import {Logger, formatBytes} from "@lodestar/utils";
14
+ import {GlobalArgs} from "../../options/globalOptions.js";
30
15
  import {BeaconArgs} from "./options.js";
16
+ import {StateInitializationError, StateInitializationErrorCode} from "./stateInitialization/errors.js";
17
+ import {prepareGenesisInitialization} from "./stateInitialization/genesisState.js";
18
+ import {
19
+ prepareArchivedStateInitialization,
20
+ prepareCheckpointSourceInitialization,
21
+ } from "./stateInitialization/prepareStateInitialization.js";
22
+ import {
23
+ ArchivedStateBytes,
24
+ StateInitialization,
25
+ StateInitializationOptions,
26
+ StatePreparationContext,
27
+ } from "./stateInitialization/types.js";
31
28
 
32
- type StateWithBytes = {state: BeaconStateAllForks; stateBytes: Uint8Array};
33
-
34
- async function initAndVerifyWeakSubjectivityState(
35
- config: BeaconConfig,
36
- db: IBeaconDb,
37
- logger: Logger,
38
- dbStateBytes: StateWithBytes,
39
- wsStateBytes: StateWithBytes,
40
- isWsStateFinalized: boolean,
41
- wsCheckpoint: Checkpoint,
42
- opts: {forceCheckpointSync?: boolean; ignoreWeakSubjectivityCheck?: boolean} = {}
43
- ): Promise<{anchorState: BeaconStateAllForks; stateBytes: Uint8Array}> {
44
- const dbState = dbStateBytes.state;
45
- const wsState = wsStateBytes.state;
46
- // Check if the store's state and wsState are compatible
47
- if (
48
- dbState.genesisTime !== wsState.genesisTime ||
49
- !ssz.Root.equals(dbState.genesisValidatorsRoot, wsState.genesisValidatorsRoot)
50
- ) {
51
- throw new Error(
52
- "Db state and checkpoint state are not compatible, either clear the db or verify your checkpoint source"
53
- );
54
- }
55
-
56
- // Pick the state which is ahead as an anchor to initialize the beacon chain
57
- let anchorState = wsStateBytes;
58
- let anchorCheckpoint = wsCheckpoint;
59
- let isCheckpointState = true;
60
- if (dbState.slot > wsState.slot && !opts.forceCheckpointSync) {
61
- anchorState = dbStateBytes;
62
- anchorCheckpoint = getCheckpointFromState(dbState);
63
- isCheckpointState = false;
64
- logger.verbose(
65
- "Db state is ahead of the provided checkpoint state, using the db state to initialize the beacon chain"
66
- );
67
- }
68
-
69
- // Throw error unless user explicitly asked not to, in testnets can happen that wss period is too small
70
- // that even some epochs of non finalization can cause finalized checkpoint to be out of valid range
71
- const wssCheck = wrapFnError(() => ensureWithinWeakSubjectivityPeriod(config, anchorState.state, anchorCheckpoint));
72
- const isWithinWeakSubjectivityPeriod = wssCheck.err === null;
73
- if (!isWithinWeakSubjectivityPeriod && !opts.ignoreWeakSubjectivityCheck) {
74
- throw wssCheck.err;
75
- }
76
-
77
- if (isWsStateFinalized) {
78
- await checkAndPersistAnchorState(config, db, logger, anchorState.state, anchorState.stateBytes, {
79
- isWithinWeakSubjectivityPeriod,
80
- isCheckpointState,
81
- });
82
- }
83
-
84
- return {anchorState: anchorState.state, stateBytes: anchorState.stateBytes};
85
- }
29
+ type InitBeaconStateResult = {anchorState: IBeaconStateView; config: BeaconConfig; isFinalized: boolean};
86
30
 
87
31
  /**
88
- * Initialize a beacon state, picking the strategy based on the `IBeaconArgs`
89
- *
90
- * State is initialized in one of three ways:
91
- * 1. restore from weak subjectivity state (possibly downloaded from a remote beacon node)
92
- * 2. restore from db
93
- * 3. restore from genesis state (possibly downloaded via URL)
94
- *
95
- * The returned anchorState could be finalized or not.
96
- * - if we load from checkpointState, checkpointSyncUrl, genesisStateFile or archived db, it is finalized
97
- * - it's not finalized if we load from unsafeCheckpointState or lastPersistedCheckpointState
32
+ * Select serialized anchor bytes before constructing the state used for validation, persistence, and return.
33
+ * Populates the global `pubkeyCache` with the anchor state's validators.
98
34
  */
99
35
  export async function initBeaconState(
100
36
  args: BeaconArgs & GlobalArgs,
@@ -102,315 +38,109 @@ export async function initBeaconState(
102
38
  chainForkConfig: ChainForkConfig,
103
39
  db: IBeaconDb,
104
40
  logger: Logger
105
- ): Promise<{
106
- anchorState: BeaconStateAllForks;
107
- stateBytes: Uint8Array;
108
- isFinalized: boolean;
109
- }> {
110
- if (args.forceCheckpointSync && !(args.checkpointState || args.checkpointSyncUrl || args.unsafeCheckpointState)) {
111
- throw new Error("Forced checkpoint sync without specifying a checkpointState or checkpointSyncUrl");
112
- }
113
- // fetch the latest state stored in the db which will be used in all cases, if it exists, either
114
- // i) used directly as the anchor state
115
- // ii) used to load and verify a weak subjectivity state,
116
- const lastDbSlot = await db.stateArchive.lastKey();
117
- let stateBytes = lastDbSlot !== null ? await db.stateArchive.getBinary(lastDbSlot) : null;
118
- // Convert to `Uint8Array` to avoid unexpected behavior such as `Buffer.prototype.slice` not copying memory
119
- stateBytes = stateBytes ? new Uint8Array(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength) : null;
120
- let lastDbState: BeaconStateAllForks | null = null;
121
- let lastDbValidatorsBytes: Uint8Array | null = null;
122
- let lastDbStateWithBytes: StateWithBytes | null = null;
123
- if (stateBytes) {
124
- logger.verbose("Found the last archived state", {slot: lastDbSlot, size: formatBytes(stateBytes.length)});
125
- const {state, validatorsBytes} = loadStateAndValidators(chainForkConfig, stateBytes);
126
- lastDbState = state;
127
- lastDbValidatorsBytes = validatorsBytes;
128
- lastDbStateWithBytes = {state, stateBytes: stateBytes};
129
- }
130
-
131
- if (lastDbState) {
132
- // state from archive db is finalized
133
- const isFinalized = true;
134
- const config = createBeaconConfig(chainForkConfig, lastDbState.genesisValidatorsRoot);
135
- const wssCheck = isWithinWeakSubjectivityPeriod(config, lastDbState, getCheckpointFromState(lastDbState));
136
-
137
- // Explicitly force syncing from checkpoint state
138
- if (args.forceCheckpointSync) {
139
- // Forcing to sync from checkpoint is only recommended if node is taking too long to sync from last db state.
140
- // It is important to remind the user to remove this flag again unless it is absolutely necessary.
141
- if (wssCheck) {
142
- logger.warn(
143
- `Forced syncing from checkpoint even though db state at slot ${lastDbState.slot} is within weak subjectivity period`
144
- );
145
- logger.warn("Please consider removing --forceCheckpointSync flag unless absolutely necessary");
146
- }
147
- } else {
148
- // All cases when we want to directly use lastDbState as the anchor state:
149
- // - if no checkpoint sync args provided, or
150
- // - the lastDbState is within weak subjectivity period:
151
- if (
152
- (!args.checkpointState &&
153
- !args.checkpointSyncUrl &&
154
- !args.unsafeCheckpointState &&
155
- !args.lastPersistedCheckpointState) ||
156
- wssCheck
157
- ) {
158
- if (stateBytes === null) {
159
- // this never happens
160
- throw Error(`There is no stateBytes for the lastDbState at slot ${lastDbState.slot}`);
161
- }
162
- await checkAndPersistAnchorState(config, db, logger, lastDbState, stateBytes, {
163
- isWithinWeakSubjectivityPeriod: wssCheck,
164
- isCheckpointState: false,
165
- });
166
- logger.info("Initialized state from db", {
167
- slot: lastDbState.slot,
168
- epoch: computeEpochAtSlot(lastDbState.slot),
169
- stateRoot: toRootHex(lastDbState.hashTreeRoot()),
170
- isFinalized,
171
- });
172
- return {anchorState: lastDbState, stateBytes, isFinalized};
173
- }
174
- }
175
- }
176
-
177
- // See if we can sync state using checkpoint sync args or else start from genesis
178
- if (args.checkpointState) {
179
- // state is trusted to be finalized
180
- const isFinalized = true;
181
- logger.info("Loading checkpoint state", {checkpointState: args.checkpointState});
182
- const stateBytes = await downloadOrLoadFile(args.checkpointState);
183
- logger.info("Loaded checkpoint state", {
184
- size: formatBytes(stateBytes.length),
185
- });
186
-
187
- const stateAndCp = await readWSState(
188
- lastDbStateWithBytes,
189
- lastDbValidatorsBytes,
190
- {
191
- stateBytes,
192
- isFinalized,
193
- wssCheckpoint: args.wssCheckpoint,
194
- forceCheckpointSync: args.forceCheckpointSync,
195
- ignoreWeakSubjectivityCheck: args.ignoreWeakSubjectivityCheck,
196
- },
197
- chainForkConfig,
198
- db,
199
- logger
200
- );
201
-
202
- const {checkpoint} = computeAnchorCheckpoint(chainForkConfig, stateAndCp.anchorState);
203
-
204
- logger.info("Initialized checkpoint state", {
205
- slot: stateAndCp.anchorState.slot,
206
- epoch: checkpoint.epoch,
207
- checkpointRoot: toRootHex(checkpoint.root),
208
- isFinalized,
209
- });
210
-
211
- return {...stateAndCp, isFinalized};
212
- }
213
-
214
- if (args.checkpointSyncUrl) {
215
- // state is trusted to be finalized
216
- const isFinalized = true;
217
- const stateAndCp = await fetchWSStateFromBeaconApi(
218
- lastDbStateWithBytes,
219
- lastDbValidatorsBytes,
220
- {
221
- checkpointSyncUrl: args.checkpointSyncUrl,
222
- wssCheckpoint: args.wssCheckpoint,
223
- forceCheckpointSync: args.forceCheckpointSync,
224
- ignoreWeakSubjectivityCheck: args.ignoreWeakSubjectivityCheck,
225
- },
226
- chainForkConfig,
227
- db,
228
- logger
41
+ ): Promise<InitBeaconStateResult> {
42
+ const options: StateInitializationOptions = args;
43
+ const useNative = options["chain.nativeStateView"] ?? false;
44
+ if (
45
+ options.forceCheckpointSync &&
46
+ !(options.checkpointState || options.checkpointSyncUrl || options.unsafeCheckpointState)
47
+ ) {
48
+ throw new StateInitializationError(
49
+ {code: StateInitializationErrorCode.INVALID_CHECKPOINT_SOURCE},
50
+ "Forced checkpoint sync without specifying a checkpointState, checkpointSyncUrl or unsafeCheckpointState"
229
51
  );
230
-
231
- const {checkpoint} = computeAnchorCheckpoint(chainForkConfig, stateAndCp.anchorState);
232
-
233
- logger.info("Initialized checkpoint state", {
234
- slot: stateAndCp.anchorState.slot,
235
- epoch: checkpoint.epoch,
236
- checkpointRoot: toRootHex(checkpoint.root),
237
- isFinalized,
238
- });
239
-
240
- return {...stateAndCp, isFinalized};
241
52
  }
242
-
243
- if (args.unsafeCheckpointState || args.lastPersistedCheckpointState) {
244
- // state is supposed to be not yet finalized
245
- const isFinalized = false;
246
- let stateBytes: Uint8Array | null = null;
247
- // prioritize lastPersistedCheckpointState over unsafeCheckpointState, unless forceCheckpointSync is set
248
- if (args.lastPersistedCheckpointState && !args.forceCheckpointSync) {
249
- // find the last persisted checkpoint state to load
250
- const cpDataStore = args["chain.nHistoricalStatesFileDataStore"]
251
- ? new FileCPStateDatastore(dataDir)
252
- : new DbCPStateDatastore(db);
253
- logger.verbose(`Finding last persisted checkpoint state from ${cpDataStore.constructor.name}`);
254
- stateBytes = await cpDataStore.readLatestSafe();
255
- if (stateBytes === null) {
256
- logger.warn("Last persisted checkpoint state not found");
257
- } else {
258
- logger.info("Found last persisted checkpoint state", {size: formatBytes(stateBytes.length)});
259
- }
260
- }
261
-
262
- if (stateBytes === null && args.unsafeCheckpointState) {
263
- logger.info("Loading checkpoint state", {unsafeCheckpointState: args.unsafeCheckpointState});
264
- stateBytes = await downloadOrLoadFile(args.unsafeCheckpointState);
265
- logger.info("Loaded checkpoint state", {
266
- size: formatBytes(stateBytes.length),
53
+ const context: StatePreparationContext = {chainForkConfig, db, dataDir, logger};
54
+ const archived = await readLatestArchivedStateBytes(context);
55
+ if (archived !== null) {
56
+ if (options.forceCheckpointSync && archived.isWithinWeakSubjectivityPeriod) {
57
+ logger.warn("Forced syncing from checkpoint even though db state is within weak subjectivity period", {
58
+ slot: archived.metadata.slot,
267
59
  });
60
+ logger.warn("Please consider removing --forceCheckpointSync flag unless absolutely necessary");
268
61
  }
269
-
270
- if (stateBytes !== null) {
271
- logger.warn(
272
- "Initializing from unfinalized checkpoint state is unsafe and may cause the node to follow a minority chain"
273
- );
274
- const stateAndCp = await readWSState(
275
- lastDbStateWithBytes,
276
- lastDbValidatorsBytes,
277
- {
278
- stateBytes,
279
- isFinalized,
280
- wssCheckpoint: args.wssCheckpoint,
281
- forceCheckpointSync: args.forceCheckpointSync,
282
- ignoreWeakSubjectivityCheck: args.ignoreWeakSubjectivityCheck,
283
- },
284
- chainForkConfig,
285
- db,
286
- logger
287
- );
288
-
289
- const lastProcessedSlot = stateAndCp.anchorState.latestBlockHeader.slot;
290
- const {checkpoint} = computeAnchorCheckpoint(chainForkConfig, stateAndCp.anchorState);
291
-
292
- logger.info("Initialized checkpoint state", {
293
- slot: stateAndCp.anchorState.slot,
294
- epoch: checkpoint.epoch,
295
- checkpointRoot: toRootHex(checkpoint.root),
296
- lastProcessedSlot,
297
- isFinalized,
298
- });
299
-
300
- return {...stateAndCp, isFinalized};
62
+ const hasCheckpointSource = Boolean(
63
+ options.checkpointState ||
64
+ options.checkpointSyncUrl ||
65
+ options.unsafeCheckpointState ||
66
+ options.lastPersistedCheckpointState
67
+ );
68
+ if (!options.forceCheckpointSync && (!hasCheckpointSource || archived.isWithinWeakSubjectivityPeriod)) {
69
+ return executeStateInitialization(prepareArchivedStateInitialization(archived, context), useNative);
301
70
  }
302
71
  }
303
-
304
- const genesisStateFile = args.genesisStateFile || getGenesisFileUrl(args.network || defaultNetwork);
305
- if (genesisStateFile && !args.forceGenesis) {
306
- // genesis state is considered finalized
307
- const isFinalized = true;
308
- logger.info("Loading genesis state", {genesisStateFile});
309
- let stateBytes = await downloadOrLoadFile(genesisStateFile);
310
- logger.info("Loaded genesis state", {size: formatBytes(stateBytes.length)});
311
- // Convert to `Uint8Array` to avoid unexpected behavior such as `Buffer.prototype.slice` not copying memory
312
- stateBytes = new Uint8Array(stateBytes.buffer, stateBytes.byteOffset, stateBytes.byteLength);
313
- const anchorState = getStateTypeFromBytes(chainForkConfig, stateBytes).deserializeToViewDU(stateBytes);
314
- // Validate genesis state root
315
- const stateRoot = toRootHex(anchorState.hashTreeRoot());
316
- const expectedRoot = getGenesisStateRoot(args.network);
317
- if (expectedRoot !== null && stateRoot !== expectedRoot) {
318
- throw Error(`Genesis state root mismatch expected=${expectedRoot} received=${stateRoot}`);
319
- }
320
- const config = createBeaconConfig(chainForkConfig, anchorState.genesisValidatorsRoot);
321
- const wssCheck = isWithinWeakSubjectivityPeriod(config, anchorState, getCheckpointFromState(anchorState));
322
- await checkAndPersistAnchorState(config, db, logger, anchorState, stateBytes, {
323
- isWithinWeakSubjectivityPeriod: wssCheck,
324
- isCheckpointState: true,
325
- });
326
- logger.info("Initialized genesis state", {
327
- slot: anchorState.slot,
328
- stateRoot,
329
- isFinalized,
330
- });
331
- return {anchorState, stateBytes, isFinalized};
72
+ let stateInit = await prepareCheckpointSourceInitialization(options, archived, context);
73
+ if (stateInit === null) {
74
+ // Without a usable checkpoint state, resume from the db as if no checkpoint source was set instead of genesis
75
+ stateInit =
76
+ archived !== null
77
+ ? prepareArchivedStateInitialization(archived, context)
78
+ : await prepareGenesisInitialization(options, context);
332
79
  }
333
-
334
- throw Error("Failed to initialize beacon state, please provide a genesis state file or use checkpoint sync");
80
+ return executeStateInitialization(stateInit, useNative);
335
81
  }
336
82
 
337
- async function readWSState(
338
- lastDbStateBytes: StateWithBytes | null,
339
- lastDbValidatorsBytes: Uint8Array | null,
340
- wssOpts: {
341
- stateBytes: Uint8Array;
342
- isFinalized: boolean;
343
- wssCheckpoint?: string;
344
- forceCheckpointSync?: boolean;
345
- ignoreWeakSubjectivityCheck?: boolean;
346
- },
347
- chainForkConfig: ChainForkConfig,
348
- db: IBeaconDb,
349
- logger: Logger
350
- ): Promise<{anchorState: BeaconStateAllForks; stateBytes: Uint8Array}> {
351
- // weak subjectivity sync from a provided state file:
352
- // if a weak subjectivity checkpoint has been provided, it is used for additional verification
353
- // otherwise, the state itself is used for verification (not bad, because the trusted state has been explicitly provided)
354
- const {stateBytes, isFinalized, wssCheckpoint, forceCheckpointSync, ignoreWeakSubjectivityCheck} = wssOpts;
355
- const lastDbState = lastDbStateBytes?.state ?? null;
356
-
357
- let wsState: BeaconStateAllForks;
358
- if (lastDbState && lastDbValidatorsBytes) {
359
- // use lastDbState to load wsState if possible to share the same state tree
360
- wsState = loadState(chainForkConfig, lastDbState, stateBytes, lastDbValidatorsBytes).state;
361
- } else {
362
- wsState = getStateTypeFromBytes(chainForkConfig, stateBytes).deserializeToViewDU(stateBytes);
83
+ /**
84
+ * Read the archived DB anchor without constructing a state. Retain its metadata, weak-subjectivity summary,
85
+ * and period verdict so selection and initialization use the same result without rescanning or checking time again.
86
+ */
87
+ async function readLatestArchivedStateBytes({
88
+ chainForkConfig,
89
+ db,
90
+ logger,
91
+ }: StatePreparationContext): Promise<ArchivedStateBytes | null> {
92
+ const slot = await db.stateArchive.lastKey();
93
+ const bytes = slot === null ? null : await db.stateArchive.getBinary(slot);
94
+ if (bytes === null) return null;
95
+ logger.verbose("Found the last archived state", {slot, size: formatBytes(bytes.length)});
96
+ const stateBytes = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
97
+ const metadata = readBeaconStateBytesMetadata(stateBytes);
98
+ if (metadata === null) {
99
+ throw new StateInitializationError(
100
+ {code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
101
+ "Cannot read metadata from archived state bytes"
102
+ );
103
+ }
104
+ const weakSubjectivity = computeWeakSubjectivitySummaryFromStateBytes(chainForkConfig, stateBytes, metadata);
105
+ if (weakSubjectivity === null) {
106
+ throw new StateInitializationError(
107
+ {code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
108
+ `Cannot extract weak subjectivity summary from archived state bytes, expected ${chainForkConfig.getForkName(metadata.slot)} state at slot ${metadata.slot}, possible fork or network mismatch`
109
+ );
363
110
  }
364
- const config = createBeaconConfig(chainForkConfig, wsState.genesisValidatorsRoot);
365
- const wsStateBytes = {state: wsState, stateBytes};
366
- const store = lastDbStateBytes ?? wsStateBytes;
367
- const checkpoint = wssCheckpoint ? getCheckpointFromArg(wssCheckpoint) : getCheckpointFromState(wsState);
368
- return initAndVerifyWeakSubjectivityState(config, db, logger, store, wsStateBytes, isFinalized, checkpoint, {
369
- forceCheckpointSync,
370
- ignoreWeakSubjectivityCheck,
371
- });
111
+ return {
112
+ stateBytes,
113
+ metadata,
114
+ weakSubjectivity,
115
+ isWithinWeakSubjectivityPeriod: isWithinWeakSubjectivityPeriodFromSummary(chainForkConfig, weakSubjectivity),
116
+ };
372
117
  }
373
118
 
374
- async function fetchWSStateFromBeaconApi(
375
- lastDbStateBytes: StateWithBytes | null,
376
- lastDbValidatorsBytes: Uint8Array | null,
377
- wssOpts: {
378
- checkpointSyncUrl: string;
379
- wssCheckpoint?: string;
380
- forceCheckpointSync?: boolean;
381
- ignoreWeakSubjectivityCheck?: boolean;
382
- },
383
- chainForkConfig: ChainForkConfig,
384
- db: IBeaconDb,
385
- logger: Logger
386
- ): Promise<{anchorState: BeaconStateAllForks; stateBytes: Uint8Array}> {
387
- // weak subjectivity sync from a state that needs to be fetched:
388
- // if a weak subjectivity checkpoint has been provided, it is used to inform which state to download and used for additional verification
389
- // otherwise, the 'finalized' state is downloaded and the state itself is used for verification (all trust delegated to the remote beacon node)
390
- try {
391
- // Validate the weakSubjectivityServerUrl and only log the origin to mask the
392
- // username password credentials
393
- const checkpointSyncUrl = new URL(wssOpts.checkpointSyncUrl);
394
- logger.info("Fetching checkpoint state", {
395
- checkpointSyncUrl: checkpointSyncUrl.origin,
396
- });
397
- } catch (e) {
398
- logger.error("Invalid", {checkpointSyncUrl: wssOpts.checkpointSyncUrl}, e as Error);
399
- throw e;
119
+ /**
120
+ * Shared completion template for all sources: construct only the selected state, exactly once.
121
+ * Validate before persistence, and log success only after any required writes succeed.
122
+ */
123
+ async function executeStateInitialization(
124
+ stateInit: StateInitialization,
125
+ useNative: boolean
126
+ ): Promise<InitBeaconStateResult> {
127
+ const {config, stateBytes} = stateInit;
128
+ const validatorCount = getValidatorCountFromStateBytes(config, stateBytes);
129
+ if (validatorCount === null) {
130
+ throw new StateInitializationError(
131
+ {code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
132
+ "Cannot read validator count from selected state bytes"
133
+ );
400
134
  }
401
-
402
- const {wsState, wsStateBytes, wsCheckpoint} = await fetchWeakSubjectivityState(chainForkConfig, logger, wssOpts, {
403
- lastDbState: lastDbStateBytes?.state ?? null,
404
- lastDbValidatorsBytes,
405
- });
406
-
407
- const config = createBeaconConfig(chainForkConfig, wsState.genesisValidatorsRoot);
408
- const wsStateWithBytes = {state: wsState, stateBytes: wsStateBytes};
409
- const store = lastDbStateBytes ?? wsStateWithBytes;
410
- // a fetched ws state is trusted to be finalized
411
- const isFinalized = true;
412
- return initAndVerifyWeakSubjectivityState(config, db, logger, store, wsStateWithBytes, isFinalized, wsCheckpoint, {
413
- forceCheckpointSync: wssOpts.forceCheckpointSync,
414
- ignoreWeakSubjectivityCheck: wssOpts.ignoreWeakSubjectivityCheck,
415
- });
135
+ // Reserve 3 months of worst-case registry growth (MAX_PENDING_DEPOSITS_PER_EPOCH per epoch),
136
+ // over a year at organic rates, to avoid routine cache reallocations. Cache growth is protected
137
+ // by its native lock; if this headroom is exceeded, it grows by the same fixed step.
138
+ // The view syncs pubkeys during construction, so capacity must be reserved first.
139
+ const headroomEpochs = (90 * 24 * 60 * 60) / (config.SECONDS_PER_SLOT * SLOTS_PER_EPOCH);
140
+ pubkeyCache.ensureCapacity(validatorCount + MAX_PENDING_DEPOSITS_PER_EPOCH * Math.ceil(headroomEpochs));
141
+ const anchorState = createBeaconStateView({useNative, config, stateBytes});
142
+ stateInit.validate(anchorState);
143
+ await stateInit.persist?.(anchorState, stateBytes);
144
+ stateInit.log(anchorState);
145
+ return {anchorState, config, isFinalized: stateInit.isFinalized};
416
146
  }
@@ -4,7 +4,6 @@ import {LogArgs, logOptions} from "../../options/logOptions.js";
4
4
  import {BeaconPaths, defaultBeaconPaths} from "./paths.js";
5
5
 
6
6
  type BeaconExtraArgs = {
7
- forceGenesis?: boolean;
8
7
  genesisStateFile?: string;
9
8
  configFile?: string;
10
9
  bootnodesFile?: string;
@@ -30,12 +29,6 @@ type BeaconExtraArgs = {
30
29
  };
31
30
 
32
31
  export const beaconExtraOptions: CliCommandOptions<BeaconExtraArgs> = {
33
- forceGenesis: {
34
- description: "Force beacon to create genesis without file",
35
- type: "boolean",
36
- hidden: true,
37
- },
38
-
39
32
  genesisStateFile: {
40
33
  description: "Path or URL to download a genesis state file in ssz-encoded format",
41
34
  type: "string",