@chainsafe/lodestar 1.50.0-dev.7f24e06bb6 → 1.50.0-dev.9c3f7bebe9
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/initBeaconState.d.ts +1 -12
- package/lib/cmds/beacon/initBeaconState.d.ts.map +1 -1
- package/lib/cmds/beacon/initBeaconState.js +59 -278
- package/lib/cmds/beacon/initBeaconState.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 +169 -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 +55 -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 +11 -29
- 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/initBeaconState.ts +91 -393
- package/src/cmds/beacon/stateInitialization/checkpointState.ts +260 -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 +66 -0
- package/src/cmds/beacon/stateInitialization/types.ts +58 -0
- package/src/cmds/beacon/stateInitialization/validation.ts +16 -0
- package/src/networks/index.ts +18 -42
- package/src/options/beaconNodeOptions/chain.ts +1 -1
|
@@ -1,416 +1,114 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
FileCPStateDatastore,
|
|
4
|
-
IBeaconDb,
|
|
5
|
-
checkAndPersistAnchorState,
|
|
6
|
-
getStateTypeFromBytes,
|
|
7
|
-
} from "@lodestar/beacon-node";
|
|
8
|
-
import {BeaconConfig, ChainForkConfig, createBeaconConfig} from "@lodestar/config";
|
|
1
|
+
import {IBeaconDb, getStateTypeFromBytes} from "@lodestar/beacon-node";
|
|
2
|
+
import {ChainForkConfig} from "@lodestar/config";
|
|
9
3
|
import {
|
|
10
4
|
BeaconStateAllForks,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
isWithinWeakSubjectivityPeriod,
|
|
15
|
-
loadState,
|
|
16
|
-
loadStateAndValidators,
|
|
5
|
+
computeWeakSubjectivitySummaryFromStateBytes,
|
|
6
|
+
isWithinWeakSubjectivityPeriodFromSummary,
|
|
7
|
+
readBeaconStateBytesMetadata,
|
|
17
8
|
} 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";
|
|
9
|
+
import {Logger, formatBytes} from "@lodestar/utils";
|
|
10
|
+
import {GlobalArgs} from "../../options/globalOptions.js";
|
|
30
11
|
import {BeaconArgs} from "./options.js";
|
|
12
|
+
import {StateInitializationError, StateInitializationErrorCode} from "./stateInitialization/errors.js";
|
|
13
|
+
import {
|
|
14
|
+
prepareArchivedStateInitialization,
|
|
15
|
+
prepareCheckpointOrGenesisInitialization,
|
|
16
|
+
} from "./stateInitialization/prepareStateInitialization.js";
|
|
17
|
+
import {
|
|
18
|
+
ArchivedStateBytes,
|
|
19
|
+
StateInitialization,
|
|
20
|
+
StateInitializationOptions,
|
|
21
|
+
StatePreparationContext,
|
|
22
|
+
} from "./stateInitialization/types.js";
|
|
31
23
|
|
|
32
|
-
|
|
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
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
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
|
|
98
|
-
*/
|
|
24
|
+
/** Select serialized anchor bytes before constructing the state used for validation, persistence, and return. */
|
|
99
25
|
export async function initBeaconState(
|
|
100
26
|
args: BeaconArgs & GlobalArgs,
|
|
101
27
|
dataDir: string,
|
|
102
28
|
chainForkConfig: ChainForkConfig,
|
|
103
29
|
db: IBeaconDb,
|
|
104
30
|
logger: Logger
|
|
105
|
-
): Promise<{
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
throw new
|
|
112
|
-
|
|
113
|
-
|
|
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
|
|
31
|
+
): Promise<{anchorState: BeaconStateAllForks; stateBytes: Uint8Array; isFinalized: boolean}> {
|
|
32
|
+
const options: StateInitializationOptions = args;
|
|
33
|
+
if (
|
|
34
|
+
options.forceCheckpointSync &&
|
|
35
|
+
!(options.checkpointState || options.checkpointSyncUrl || options.unsafeCheckpointState)
|
|
36
|
+
) {
|
|
37
|
+
throw new StateInitializationError(
|
|
38
|
+
{code: StateInitializationErrorCode.INVALID_CHECKPOINT_SOURCE},
|
|
39
|
+
"Forced checkpoint sync without specifying a checkpointState, checkpointSyncUrl or unsafeCheckpointState"
|
|
229
40
|
);
|
|
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
41
|
}
|
|
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),
|
|
267
|
-
});
|
|
268
|
-
}
|
|
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,
|
|
42
|
+
const context: StatePreparationContext = {chainForkConfig, db, dataDir, logger};
|
|
43
|
+
const archived = await readLatestArchivedStateBytes(context);
|
|
44
|
+
if (archived !== null) {
|
|
45
|
+
if (options.forceCheckpointSync && archived.isWithinWeakSubjectivityPeriod) {
|
|
46
|
+
logger.warn("Forced syncing from checkpoint even though db state is within weak subjectivity period", {
|
|
47
|
+
slot: archived.metadata.slot,
|
|
298
48
|
});
|
|
299
|
-
|
|
300
|
-
return {...stateAndCp, isFinalized};
|
|
49
|
+
logger.warn("Please consider removing --forceCheckpointSync flag unless absolutely necessary");
|
|
301
50
|
}
|
|
302
|
-
|
|
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}`);
|
|
51
|
+
const hasCheckpointSource = Boolean(
|
|
52
|
+
options.checkpointState ||
|
|
53
|
+
options.checkpointSyncUrl ||
|
|
54
|
+
options.unsafeCheckpointState ||
|
|
55
|
+
options.lastPersistedCheckpointState
|
|
56
|
+
);
|
|
57
|
+
if (!options.forceCheckpointSync && (!hasCheckpointSource || archived.isWithinWeakSubjectivityPeriod)) {
|
|
58
|
+
return executeStateInitialization(prepareArchivedStateInitialization(archived, context));
|
|
319
59
|
}
|
|
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
60
|
}
|
|
333
|
-
|
|
334
|
-
|
|
61
|
+
const stateInit = await prepareCheckpointOrGenesisInitialization(options, archived, context);
|
|
62
|
+
return executeStateInitialization(stateInit);
|
|
335
63
|
}
|
|
336
64
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
logger:
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
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);
|
|
65
|
+
/**
|
|
66
|
+
* Read the archived DB anchor without constructing a state. Retain its metadata, weak-subjectivity summary,
|
|
67
|
+
* and period verdict so selection and initialization use the same result without rescanning or checking time again.
|
|
68
|
+
*/
|
|
69
|
+
async function readLatestArchivedStateBytes({
|
|
70
|
+
chainForkConfig,
|
|
71
|
+
db,
|
|
72
|
+
logger,
|
|
73
|
+
}: StatePreparationContext): Promise<ArchivedStateBytes | null> {
|
|
74
|
+
const slot = await db.stateArchive.lastKey();
|
|
75
|
+
const bytes = slot === null ? null : await db.stateArchive.getBinary(slot);
|
|
76
|
+
if (bytes === null) return null;
|
|
77
|
+
logger.verbose("Found the last archived state", {slot, size: formatBytes(bytes.length)});
|
|
78
|
+
const stateBytes = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
79
|
+
const metadata = readBeaconStateBytesMetadata(stateBytes);
|
|
80
|
+
if (metadata === null) {
|
|
81
|
+
throw new StateInitializationError(
|
|
82
|
+
{code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
|
|
83
|
+
"Cannot read metadata from archived state bytes"
|
|
84
|
+
);
|
|
363
85
|
}
|
|
364
|
-
const
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
ignoreWeakSubjectivityCheck,
|
|
371
|
-
});
|
|
372
|
-
}
|
|
373
|
-
|
|
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;
|
|
86
|
+
const weakSubjectivity = computeWeakSubjectivitySummaryFromStateBytes(chainForkConfig, stateBytes, metadata);
|
|
87
|
+
if (weakSubjectivity === null) {
|
|
88
|
+
throw new StateInitializationError(
|
|
89
|
+
{code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
|
|
90
|
+
"Cannot extract weak subjectivity summary from archived state bytes"
|
|
91
|
+
);
|
|
400
92
|
}
|
|
93
|
+
return {
|
|
94
|
+
stateBytes,
|
|
95
|
+
metadata,
|
|
96
|
+
weakSubjectivity,
|
|
97
|
+
isWithinWeakSubjectivityPeriod: isWithinWeakSubjectivityPeriodFromSummary(chainForkConfig, weakSubjectivity),
|
|
98
|
+
};
|
|
99
|
+
}
|
|
401
100
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
const
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
});
|
|
101
|
+
/**
|
|
102
|
+
* Shared completion template for all sources: construct only the selected state, exactly once.
|
|
103
|
+
* Validate before persistence, and log success only after any required writes succeed.
|
|
104
|
+
*/
|
|
105
|
+
async function executeStateInitialization(
|
|
106
|
+
stateInit: StateInitialization
|
|
107
|
+
): Promise<{anchorState: BeaconStateAllForks; stateBytes: Uint8Array; isFinalized: boolean}> {
|
|
108
|
+
const stateType = getStateTypeFromBytes(stateInit.config, stateInit.stateBytes);
|
|
109
|
+
const anchorState = stateType.deserializeToViewDU(stateInit.stateBytes);
|
|
110
|
+
stateInit.validate(anchorState);
|
|
111
|
+
await stateInit.persist?.(anchorState, stateInit.stateBytes);
|
|
112
|
+
stateInit.log(anchorState);
|
|
113
|
+
return {anchorState, stateBytes: stateInit.stateBytes, isFinalized: stateInit.isFinalized};
|
|
416
114
|
}
|