@chainsafe/lodestar 1.50.0-dev.3579e999b5 → 1.50.0-dev.3bd495295e

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 (66) hide show
  1. package/.git-data.json +1 -1
  2. package/lib/cmds/beacon/handler.d.ts +1 -0
  3. package/lib/cmds/beacon/handler.d.ts.map +1 -1
  4. package/lib/cmds/beacon/handler.js +9 -21
  5. package/lib/cmds/beacon/handler.js.map +1 -1
  6. package/lib/cmds/beacon/initBeaconState.d.ts +11 -17
  7. package/lib/cmds/beacon/initBeaconState.d.ts.map +1 -1
  8. package/lib/cmds/beacon/initBeaconState.js +83 -275
  9. package/lib/cmds/beacon/initBeaconState.js.map +1 -1
  10. package/lib/cmds/beacon/options.d.ts +1 -1
  11. package/lib/cmds/beacon/options.d.ts.map +1 -1
  12. package/lib/cmds/beacon/options.js +6 -5
  13. package/lib/cmds/beacon/options.js.map +1 -1
  14. package/lib/cmds/beacon/paths.d.ts +4 -0
  15. package/lib/cmds/beacon/paths.d.ts.map +1 -1
  16. package/lib/cmds/beacon/paths.js +3 -0
  17. package/lib/cmds/beacon/paths.js.map +1 -1
  18. package/lib/cmds/beacon/pubkeysFile.d.ts +10 -0
  19. package/lib/cmds/beacon/pubkeysFile.d.ts.map +1 -0
  20. package/lib/cmds/beacon/pubkeysFile.js +39 -0
  21. package/lib/cmds/beacon/pubkeysFile.js.map +1 -0
  22. package/lib/cmds/beacon/stateInitialization/checkpointState.d.ts +7 -0
  23. package/lib/cmds/beacon/stateInitialization/checkpointState.d.ts.map +1 -0
  24. package/lib/cmds/beacon/stateInitialization/checkpointState.js +168 -0
  25. package/lib/cmds/beacon/stateInitialization/checkpointState.js.map +1 -0
  26. package/lib/cmds/beacon/stateInitialization/errors.d.ts +17 -0
  27. package/lib/cmds/beacon/stateInitialization/errors.d.ts.map +1 -0
  28. package/lib/cmds/beacon/stateInitialization/errors.js +16 -0
  29. package/lib/cmds/beacon/stateInitialization/errors.js.map +1 -0
  30. package/lib/cmds/beacon/stateInitialization/genesisState.d.ts +3 -0
  31. package/lib/cmds/beacon/stateInitialization/genesisState.d.ts.map +1 -0
  32. package/lib/cmds/beacon/stateInitialization/genesisState.js +65 -0
  33. package/lib/cmds/beacon/stateInitialization/genesisState.js.map +1 -0
  34. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.d.ts +4 -0
  35. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.d.ts.map +1 -0
  36. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.js +52 -0
  37. package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.js.map +1 -0
  38. package/lib/cmds/beacon/stateInitialization/types.d.ts +40 -0
  39. package/lib/cmds/beacon/stateInitialization/types.d.ts.map +1 -0
  40. package/lib/cmds/beacon/stateInitialization/types.js +2 -0
  41. package/lib/cmds/beacon/stateInitialization/types.js.map +1 -0
  42. package/lib/cmds/beacon/stateInitialization/validation.d.ts +4 -0
  43. package/lib/cmds/beacon/stateInitialization/validation.d.ts.map +1 -0
  44. package/lib/cmds/beacon/stateInitialization/validation.js +12 -0
  45. package/lib/cmds/beacon/stateInitialization/validation.js.map +1 -0
  46. package/lib/cmds/dev/handler.d.ts.map +1 -1
  47. package/lib/cmds/dev/handler.js +3 -1
  48. package/lib/cmds/dev/handler.js.map +1 -1
  49. package/lib/networks/index.d.ts +3 -9
  50. package/lib/networks/index.d.ts.map +1 -1
  51. package/lib/networks/index.js +15 -28
  52. package/lib/networks/index.js.map +1 -1
  53. package/package.json +14 -14
  54. package/src/cmds/beacon/handler.ts +11 -22
  55. package/src/cmds/beacon/initBeaconState.ts +128 -386
  56. package/src/cmds/beacon/options.ts +8 -7
  57. package/src/cmds/beacon/paths.ts +5 -0
  58. package/src/cmds/beacon/pubkeysFile.ts +50 -0
  59. package/src/cmds/beacon/stateInitialization/checkpointState.ts +256 -0
  60. package/src/cmds/beacon/stateInitialization/errors.ts +15 -0
  61. package/src/cmds/beacon/stateInitialization/genesisState.ts +85 -0
  62. package/src/cmds/beacon/stateInitialization/prepareStateInitialization.ts +64 -0
  63. package/src/cmds/beacon/stateInitialization/types.ts +58 -0
  64. package/src/cmds/beacon/stateInitialization/validation.ts +16 -0
  65. package/src/cmds/dev/handler.ts +3 -1
  66. package/src/networks/index.ts +25 -40
@@ -4,15 +4,9 @@ import {WireFormat, getClient} from "@lodestar/api";
4
4
  import {getStateSlotFromBytes} from "@lodestar/beacon-node";
5
5
  import {ChainConfig, ChainForkConfig} from "@lodestar/config";
6
6
  import {SLOTS_PER_EPOCH} from "@lodestar/params";
7
- import {
8
- BeaconStateAllForks,
9
- computeCheckpointEpochAtStateSlot,
10
- getLatestBlockRoot,
11
- loadState,
12
- } from "@lodestar/state-transition";
13
- import {Slot, sszTypesFor} from "@lodestar/types";
7
+ import {Slot} from "@lodestar/types";
14
8
  import {Checkpoint} from "@lodestar/types/phase0";
15
- import {Logger, callFnWhenAwait, fetch, formatBytes, fromHex} from "@lodestar/utils";
9
+ import {LodestarError, Logger, callFnWhenAwait, fetch, formatBytes, fromHex} from "@lodestar/utils";
16
10
  import {parseBootnodesFile} from "../util/format.js";
17
11
  import * as chiado from "./chiado.js";
18
12
  import * as dev from "./dev.js";
@@ -160,15 +154,11 @@ export function readBootnodes(bootnodesFilePath: string): string[] {
160
154
  /**
161
155
  * Fetch weak subjectivity state from a remote beacon node
162
156
  */
163
- export async function fetchWeakSubjectivityState(
157
+ export async function fetchWeakSubjectivityStateBytes(
164
158
  config: ChainForkConfig,
165
159
  logger: Logger,
166
- {checkpointSyncUrl, wssCheckpoint}: {checkpointSyncUrl: string; wssCheckpoint?: string},
167
- {
168
- lastDbState,
169
- lastDbValidatorsBytes,
170
- }: {lastDbState: BeaconStateAllForks | null; lastDbValidatorsBytes: Uint8Array | null}
171
- ): Promise<{wsState: BeaconStateAllForks; wsStateBytes: Uint8Array; wsCheckpoint: Checkpoint}> {
160
+ {checkpointSyncUrl, wssCheckpoint}: {checkpointSyncUrl: string; wssCheckpoint?: string}
161
+ ): Promise<{stateBytes: Uint8Array; expectedCheckpoint: Checkpoint | null}> {
172
162
  try {
173
163
  let wsCheckpoint: Checkpoint | null;
174
164
  let stateId: Slot | "finalized";
@@ -178,7 +168,7 @@ export async function fetchWeakSubjectivityState(
178
168
  wsCheckpoint = getCheckpointFromArg(wssCheckpoint);
179
169
  stateId = wsCheckpoint.epoch * SLOTS_PER_EPOCH;
180
170
  } else {
181
- // Fetch current finalized state and extract checkpoint from it
171
+ // Checkpoint identity can be derived after the selected state is constructed.
182
172
  stateId = "finalized";
183
173
  wsCheckpoint = null;
184
174
  }
@@ -186,33 +176,37 @@ export async function fetchWeakSubjectivityState(
186
176
  // getStateV2 should be available for all forks including phase0
187
177
  const getStatePromise = api.debug.getStateV2({stateId}, {responseWireFormat: WireFormat.ssz});
188
178
 
189
- const {wsStateBytes, fork} = await callFnWhenAwait(
179
+ const response = await callFnWhenAwait(
190
180
  getStatePromise,
191
181
  () => logger.info("Download in progress, please wait..."),
192
182
  GET_STATE_LOG_INTERVAL
193
- ).then((res) => {
194
- return {wsStateBytes: res.ssz(), fork: res.meta().version};
195
- });
183
+ );
184
+ const wsStateBytes = response.ssz();
185
+ const {version} = response.meta();
196
186
 
197
187
  const wsSlot = getStateSlotFromBytes(wsStateBytes);
198
188
  const logData = {stateId, size: formatBytes(wsStateBytes.length)};
199
189
  logger.info("Download completed", typeof stateId === "number" ? logData : {...logData, slot: wsSlot});
200
190
 
201
- let wsState: BeaconStateAllForks;
202
- if (lastDbState && lastDbValidatorsBytes) {
203
- // use lastDbState to load wsState if possible to share the same state tree
204
- wsState = loadState(config, lastDbState, wsStateBytes, lastDbValidatorsBytes).state;
205
- } else {
206
- wsState = sszTypesFor(fork).BeaconState.deserializeToViewDU(wsStateBytes);
191
+ const expectedFork = config.getForkName(wsSlot);
192
+ if (version !== expectedFork) {
193
+ throw new LodestarError(
194
+ {code: "CHECKPOINT_FORK_MISMATCH"},
195
+ `Checkpoint sync server returned ${version} state at slot ${wsSlot} but local config expects ${expectedFork}, verify --network or the chain config`
196
+ );
207
197
  }
208
198
 
209
199
  return {
210
- wsState,
211
- wsStateBytes,
212
- wsCheckpoint: wsCheckpoint ?? getCheckpointFromState(wsState),
200
+ stateBytes: new Uint8Array(wsStateBytes.buffer, wsStateBytes.byteOffset, wsStateBytes.byteLength),
201
+ expectedCheckpoint: wsCheckpoint,
213
202
  };
214
203
  } catch (e) {
215
- throw new Error("Unable to fetch weak subjectivity state: " + (e as Error).message);
204
+ const error = new LodestarError(
205
+ {code: "CHECKPOINT_DOWNLOAD_FAILED"},
206
+ "Unable to fetch weak subjectivity state: " + (e as Error).message
207
+ );
208
+ error.cause = e;
209
+ throw error;
216
210
  }
217
211
  }
218
212
 
@@ -220,16 +214,7 @@ export function getCheckpointFromArg(checkpointStr: string): Checkpoint {
220
214
  const checkpointRegex = /^(?:0x)?([0-9a-f]{64}):([0-9]+)$/;
221
215
  const match = checkpointRegex.exec(checkpointStr.toLowerCase());
222
216
  if (!match) {
223
- throw new Error(`Could not parse checkpoint string: ${checkpointStr}`);
217
+ throw new LodestarError({code: "INVALID_CHECKPOINT"}, `Could not parse checkpoint string: ${checkpointStr}`);
224
218
  }
225
219
  return {root: fromHex(match[1]), epoch: parseInt(match[2])};
226
220
  }
227
-
228
- export function getCheckpointFromState(state: BeaconStateAllForks): Checkpoint {
229
- return {
230
- // the correct checkpoint is based on state's slot, its latestBlockHeader's slot's epoch can be
231
- // behind the state
232
- epoch: computeCheckpointEpochAtStateSlot(state.slot),
233
- root: getLatestBlockRoot(state),
234
- };
235
- }