@chainsafe/lodestar 1.50.0-dev.abf1d462fa → 1.50.0-dev.be50566b7e
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.
- package/.git-data.json +1 -1
- package/lib/cmds/beacon/handler.d.ts.map +1 -1
- package/lib/cmds/beacon/handler.js +3 -16
- package/lib/cmds/beacon/handler.js.map +1 -1
- package/lib/cmds/beacon/initBeaconState.d.ts +11 -17
- package/lib/cmds/beacon/initBeaconState.d.ts.map +1 -1
- package/lib/cmds/beacon/initBeaconState.js +78 -274
- package/lib/cmds/beacon/initBeaconState.js.map +1 -1
- package/lib/cmds/beacon/options.d.ts +0 -1
- package/lib/cmds/beacon/options.d.ts.map +1 -1
- package/lib/cmds/beacon/options.js +0 -5
- package/lib/cmds/beacon/options.js.map +1 -1
- package/lib/cmds/beacon/stateInitialization/checkpointState.d.ts +7 -0
- package/lib/cmds/beacon/stateInitialization/checkpointState.d.ts.map +1 -0
- package/lib/cmds/beacon/stateInitialization/checkpointState.js +168 -0
- package/lib/cmds/beacon/stateInitialization/checkpointState.js.map +1 -0
- package/lib/cmds/beacon/stateInitialization/errors.d.ts +17 -0
- package/lib/cmds/beacon/stateInitialization/errors.d.ts.map +1 -0
- package/lib/cmds/beacon/stateInitialization/errors.js +16 -0
- package/lib/cmds/beacon/stateInitialization/errors.js.map +1 -0
- package/lib/cmds/beacon/stateInitialization/genesisState.d.ts +3 -0
- package/lib/cmds/beacon/stateInitialization/genesisState.d.ts.map +1 -0
- package/lib/cmds/beacon/stateInitialization/genesisState.js +65 -0
- package/lib/cmds/beacon/stateInitialization/genesisState.js.map +1 -0
- package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.d.ts +4 -0
- package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.d.ts.map +1 -0
- package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.js +52 -0
- package/lib/cmds/beacon/stateInitialization/prepareStateInitialization.js.map +1 -0
- package/lib/cmds/beacon/stateInitialization/types.d.ts +40 -0
- package/lib/cmds/beacon/stateInitialization/types.d.ts.map +1 -0
- package/lib/cmds/beacon/stateInitialization/types.js +2 -0
- package/lib/cmds/beacon/stateInitialization/types.js.map +1 -0
- package/lib/cmds/beacon/stateInitialization/validation.d.ts +4 -0
- package/lib/cmds/beacon/stateInitialization/validation.d.ts.map +1 -0
- package/lib/cmds/beacon/stateInitialization/validation.js +12 -0
- package/lib/cmds/beacon/stateInitialization/validation.js.map +1 -0
- package/lib/networks/index.d.ts +3 -9
- package/lib/networks/index.d.ts.map +1 -1
- package/lib/networks/index.js +15 -28
- package/lib/networks/index.js.map +1 -1
- package/lib/options/beaconNodeOptions/chain.js +1 -1
- package/lib/options/beaconNodeOptions/chain.js.map +1 -1
- package/package.json +14 -14
- package/src/cmds/beacon/handler.ts +4 -16
- package/src/cmds/beacon/initBeaconState.ts +119 -389
- package/src/cmds/beacon/options.ts +0 -7
- package/src/cmds/beacon/stateInitialization/checkpointState.ts +256 -0
- package/src/cmds/beacon/stateInitialization/errors.ts +15 -0
- package/src/cmds/beacon/stateInitialization/genesisState.ts +85 -0
- package/src/cmds/beacon/stateInitialization/prepareStateInitialization.ts +64 -0
- package/src/cmds/beacon/stateInitialization/types.ts +58 -0
- package/src/cmds/beacon/stateInitialization/validation.ts +16 -0
- package/src/networks/index.ts +25 -40
- 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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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 {
|
|
19
|
-
import {
|
|
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
|
|
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
|
-
*
|
|
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
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
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
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
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
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
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
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
logger:
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
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
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
ignoreWeakSubjectivityCheck,
|
|
371
|
-
});
|
|
111
|
+
return {
|
|
112
|
+
stateBytes,
|
|
113
|
+
metadata,
|
|
114
|
+
weakSubjectivity,
|
|
115
|
+
isWithinWeakSubjectivityPeriod: isWithinWeakSubjectivityPeriodFromSummary(chainForkConfig, weakSubjectivity),
|
|
116
|
+
};
|
|
372
117
|
}
|
|
373
118
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
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
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
const
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
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",
|