@chainsafe/lodestar 1.50.0-dev.a04826a24c → 1.50.0-dev.a4451e41c8

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 (49) 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 +71 -275
  8. package/lib/cmds/beacon/initBeaconState.js.map +1 -1
  9. package/lib/cmds/beacon/stateInitialization/checkpointState.d.ts +7 -0
  10. package/lib/cmds/beacon/stateInitialization/checkpointState.d.ts.map +1 -0
  11. package/lib/cmds/beacon/stateInitialization/checkpointState.js +168 -0
  12. package/lib/cmds/beacon/stateInitialization/checkpointState.js.map +1 -0
  13. package/lib/cmds/beacon/stateInitialization/errors.d.ts +17 -0
  14. package/lib/cmds/beacon/stateInitialization/errors.d.ts.map +1 -0
  15. package/lib/cmds/beacon/stateInitialization/errors.js +16 -0
  16. package/lib/cmds/beacon/stateInitialization/errors.js.map +1 -0
  17. package/lib/cmds/beacon/stateInitialization/genesisState.d.ts +3 -0
  18. package/lib/cmds/beacon/stateInitialization/genesisState.d.ts.map +1 -0
  19. package/lib/cmds/beacon/stateInitialization/genesisState.js +65 -0
  20. package/lib/cmds/beacon/stateInitialization/genesisState.js.map +1 -0
  21. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.d.ts +4 -0
  22. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.d.ts.map +1 -0
  23. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.js +55 -0
  24. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.js.map +1 -0
  25. package/lib/cmds/beacon/stateInitialization/types.d.ts +40 -0
  26. package/lib/cmds/beacon/stateInitialization/types.d.ts.map +1 -0
  27. package/lib/cmds/beacon/stateInitialization/types.js +2 -0
  28. package/lib/cmds/beacon/stateInitialization/types.js.map +1 -0
  29. package/lib/cmds/beacon/stateInitialization/validation.d.ts +4 -0
  30. package/lib/cmds/beacon/stateInitialization/validation.d.ts.map +1 -0
  31. package/lib/cmds/beacon/stateInitialization/validation.js +12 -0
  32. package/lib/cmds/beacon/stateInitialization/validation.js.map +1 -0
  33. package/lib/networks/index.d.ts +3 -9
  34. package/lib/networks/index.d.ts.map +1 -1
  35. package/lib/networks/index.js +15 -28
  36. package/lib/networks/index.js.map +1 -1
  37. package/lib/options/beaconNodeOptions/chain.js +1 -1
  38. package/lib/options/beaconNodeOptions/chain.js.map +1 -1
  39. package/package.json +14 -14
  40. package/src/cmds/beacon/handler.ts +4 -16
  41. package/src/cmds/beacon/initBeaconState.ts +112 -390
  42. package/src/cmds/beacon/stateInitialization/checkpointState.ts +256 -0
  43. package/src/cmds/beacon/stateInitialization/errors.ts +15 -0
  44. package/src/cmds/beacon/stateInitialization/genesisState.ts +85 -0
  45. package/src/cmds/beacon/stateInitialization/prepareStateInitialization.ts +66 -0
  46. package/src/cmds/beacon/stateInitialization/types.ts +59 -0
  47. package/src/cmds/beacon/stateInitialization/validation.ts +16 -0
  48. package/src/networks/index.ts +25 -40
  49. package/src/options/beaconNodeOptions/chain.ts +1 -1
@@ -1,100 +1,35 @@
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 {
18
+ prepareArchivedStateInitialization,
19
+ prepareCheckpointOrGenesisInitialization,
20
+ } from "./stateInitialization/prepareStateInitialization.js";
21
+ import {
22
+ ArchivedStateBytes,
23
+ StateInitialization,
24
+ StateInitializationOptions,
25
+ StatePreparationContext,
26
+ } from "./stateInitialization/types.js";
31
27
 
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
- }
28
+ type InitBeaconStateResult = {anchorState: IBeaconStateView; config: BeaconConfig; isFinalized: boolean};
86
29
 
87
30
  /**
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
31
+ * Select serialized anchor bytes before constructing the state used for validation, persistence, and return.
32
+ * Populates the global `pubkeyCache` with the anchor state's validators.
98
33
  */
99
34
  export async function initBeaconState(
100
35
  args: BeaconArgs & GlobalArgs,
@@ -102,315 +37,102 @@ export async function initBeaconState(
102
37
  chainForkConfig: ChainForkConfig,
103
38
  db: IBeaconDb,
104
39
  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
40
+ ): Promise<InitBeaconStateResult> {
41
+ const options: StateInitializationOptions = args;
42
+ const useNative = options["chain.nativeStateView"] ?? false;
43
+ if (
44
+ options.forceCheckpointSync &&
45
+ !(options.checkpointState || options.checkpointSyncUrl || options.unsafeCheckpointState)
46
+ ) {
47
+ throw new StateInitializationError(
48
+ {code: StateInitializationErrorCode.INVALID_CHECKPOINT_SOURCE},
49
+ "Forced checkpoint sync without specifying a checkpointState, checkpointSyncUrl or unsafeCheckpointState"
229
50
  );
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
51
  }
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),
52
+ const context: StatePreparationContext = {chainForkConfig, db, dataDir, logger};
53
+ const archived = await readLatestArchivedStateBytes(context);
54
+ if (archived !== null) {
55
+ if (options.forceCheckpointSync && archived.isWithinWeakSubjectivityPeriod) {
56
+ logger.warn("Forced syncing from checkpoint even though db state is within weak subjectivity period", {
57
+ slot: archived.metadata.slot,
267
58
  });
59
+ logger.warn("Please consider removing --forceCheckpointSync flag unless absolutely necessary");
268
60
  }
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};
301
- }
302
- }
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}`);
61
+ const hasCheckpointSource = Boolean(
62
+ options.checkpointState ||
63
+ options.checkpointSyncUrl ||
64
+ options.unsafeCheckpointState ||
65
+ options.lastPersistedCheckpointState
66
+ );
67
+ if (!options.forceCheckpointSync && (!hasCheckpointSource || archived.isWithinWeakSubjectivityPeriod)) {
68
+ return executeStateInitialization(prepareArchivedStateInitialization(archived, context), useNative);
319
69
  }
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};
332
70
  }
333
-
334
- throw Error("Failed to initialize beacon state, please provide a genesis state file or use checkpoint sync");
71
+ const stateInit = await prepareCheckpointOrGenesisInitialization(options, archived, context);
72
+ return executeStateInitialization(stateInit, useNative);
335
73
  }
336
74
 
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);
75
+ /**
76
+ * Read the archived DB anchor without constructing a state. Retain its metadata, weak-subjectivity summary,
77
+ * and period verdict so selection and initialization use the same result without rescanning or checking time again.
78
+ */
79
+ async function readLatestArchivedStateBytes({
80
+ chainForkConfig,
81
+ db,
82
+ logger,
83
+ }: StatePreparationContext): Promise<ArchivedStateBytes | null> {
84
+ const slot = await db.stateArchive.lastKey();
85
+ const bytes = slot === null ? null : await db.stateArchive.getBinary(slot);
86
+ if (bytes === null) return null;
87
+ logger.verbose("Found the last archived state", {slot, size: formatBytes(bytes.length)});
88
+ const stateBytes = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
89
+ const metadata = readBeaconStateBytesMetadata(stateBytes);
90
+ if (metadata === null) {
91
+ throw new StateInitializationError(
92
+ {code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
93
+ "Cannot read metadata from archived state bytes"
94
+ );
95
+ }
96
+ const weakSubjectivity = computeWeakSubjectivitySummaryFromStateBytes(chainForkConfig, stateBytes, metadata);
97
+ if (weakSubjectivity === null) {
98
+ throw new StateInitializationError(
99
+ {code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
100
+ `Cannot extract weak subjectivity summary from archived state bytes, expected ${chainForkConfig.getForkName(metadata.slot)} state at slot ${metadata.slot}, possible fork or network mismatch`
101
+ );
363
102
  }
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
- });
103
+ return {
104
+ stateBytes,
105
+ metadata,
106
+ weakSubjectivity,
107
+ isWithinWeakSubjectivityPeriod: isWithinWeakSubjectivityPeriodFromSummary(chainForkConfig, weakSubjectivity),
108
+ };
372
109
  }
373
110
 
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;
111
+ /**
112
+ * Shared completion template for all sources: construct only the selected state, exactly once.
113
+ * Validate before persistence, and log success only after any required writes succeed.
114
+ */
115
+ async function executeStateInitialization(
116
+ stateInit: StateInitialization,
117
+ useNative: boolean
118
+ ): Promise<InitBeaconStateResult> {
119
+ const {config, stateBytes} = stateInit;
120
+ const validatorCount = getValidatorCountFromStateBytes(config, stateBytes);
121
+ if (validatorCount === null) {
122
+ throw new StateInitializationError(
123
+ {code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
124
+ "Cannot read validator count from selected state bytes"
125
+ );
400
126
  }
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
- });
127
+ // Reserve 3 months of worst-case registry growth (MAX_PENDING_DEPOSITS_PER_EPOCH per epoch),
128
+ // over a year at organic rates, to avoid routine cache reallocations. Cache growth is protected
129
+ // by its native lock; if this headroom is exceeded, it grows by the same fixed step.
130
+ // The view syncs pubkeys during construction, so capacity must be reserved first.
131
+ const headroomEpochs = (90 * 24 * 60 * 60) / (config.SECONDS_PER_SLOT * SLOTS_PER_EPOCH);
132
+ pubkeyCache.ensureCapacity(validatorCount + MAX_PENDING_DEPOSITS_PER_EPOCH * Math.ceil(headroomEpochs));
133
+ const anchorState = createBeaconStateView({useNative, config, stateBytes});
134
+ stateInit.validate(anchorState);
135
+ await stateInit.persist?.(anchorState, stateBytes);
136
+ stateInit.log(anchorState);
137
+ return {anchorState, config, isFinalized: stateInit.isFinalized};
416
138
  }