@chainsafe/lodestar 1.50.0-dev.033e6b7138 → 1.50.0-dev.2e668c516f
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/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
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import {DbCPStateDatastore, FileCPStateDatastore, persistAnchorState} from "@lodestar/beacon-node";
|
|
2
|
+
import {createBeaconConfig} from "@lodestar/config";
|
|
3
|
+
import {
|
|
4
|
+
IBeaconStateView,
|
|
5
|
+
computeEpochAtSlot,
|
|
6
|
+
computeWeakSubjectivitySummaryFromStateBytes,
|
|
7
|
+
getCurrentSlot,
|
|
8
|
+
isWithinWeakSubjectivityPeriodFromSummary,
|
|
9
|
+
readBeaconStateBytesMetadata,
|
|
10
|
+
} from "@lodestar/state-transition";
|
|
11
|
+
import {ssz} from "@lodestar/types";
|
|
12
|
+
import {Checkpoint} from "@lodestar/types/phase0";
|
|
13
|
+
import {formatBytes, toRootHex} from "@lodestar/utils";
|
|
14
|
+
import {fetchWeakSubjectivityStateBytes, getCheckpointFromArg} from "../../../networks/index.js";
|
|
15
|
+
import {downloadOrLoadFile} from "../../../util/index.js";
|
|
16
|
+
import {StateInitializationError, StateInitializationErrorCode} from "./errors.js";
|
|
17
|
+
import {
|
|
18
|
+
ArchivedStateBytes,
|
|
19
|
+
CheckpointStartupPolicy,
|
|
20
|
+
CheckpointStateBytesCandidate,
|
|
21
|
+
StateInitialization,
|
|
22
|
+
StateInitializationOptions,
|
|
23
|
+
StatePreparationContext,
|
|
24
|
+
} from "./types.js";
|
|
25
|
+
import {assertAnchorStateForkMatchesConfig} from "./validation.js";
|
|
26
|
+
|
|
27
|
+
type CheckpointOptions = Pick<
|
|
28
|
+
StateInitializationOptions,
|
|
29
|
+
"wssCheckpoint" | "forceCheckpointSync" | "ignoreWeakSubjectivityCheck"
|
|
30
|
+
>;
|
|
31
|
+
|
|
32
|
+
export async function prepareCheckpointFileInitialization(
|
|
33
|
+
checkpointState: string,
|
|
34
|
+
options: CheckpointOptions,
|
|
35
|
+
archived: ArchivedStateBytes | null,
|
|
36
|
+
context: StatePreparationContext
|
|
37
|
+
): Promise<StateInitialization> {
|
|
38
|
+
context.logger.info("Loading checkpoint state", {checkpointState});
|
|
39
|
+
const bytes = await downloadOrLoadFile(checkpointState);
|
|
40
|
+
const stateBytes = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
41
|
+
context.logger.info("Loaded checkpoint state", {checkpointState, size: formatBytes(stateBytes.length)});
|
|
42
|
+
return prepareCheckpointInitialization(
|
|
43
|
+
{
|
|
44
|
+
stateBytes,
|
|
45
|
+
expectedCheckpoint: options.wssCheckpoint ? getCheckpointFromArg(options.wssCheckpoint) : null,
|
|
46
|
+
source: "checkpoint file",
|
|
47
|
+
},
|
|
48
|
+
archived,
|
|
49
|
+
{
|
|
50
|
+
isFinalized: true,
|
|
51
|
+
forceCheckpointSync: Boolean(options.forceCheckpointSync),
|
|
52
|
+
ignoreWeakSubjectivityCheck: Boolean(options.ignoreWeakSubjectivityCheck),
|
|
53
|
+
},
|
|
54
|
+
context
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function prepareCheckpointApiInitialization(
|
|
59
|
+
checkpointSyncUrl: string,
|
|
60
|
+
options: CheckpointOptions,
|
|
61
|
+
archived: ArchivedStateBytes | null,
|
|
62
|
+
context: StatePreparationContext
|
|
63
|
+
): Promise<StateInitialization> {
|
|
64
|
+
const {chainForkConfig, logger} = context;
|
|
65
|
+
try {
|
|
66
|
+
const url = new URL(checkpointSyncUrl);
|
|
67
|
+
logger.info("Fetching checkpoint state", {checkpointSyncUrl: url.origin});
|
|
68
|
+
} catch (error) {
|
|
69
|
+
logger.error("Invalid checkpoint sync URL", {checkpointSyncUrl}, error as Error);
|
|
70
|
+
throw error;
|
|
71
|
+
}
|
|
72
|
+
const {stateBytes, expectedCheckpoint} = await fetchWeakSubjectivityStateBytes(chainForkConfig, logger, {
|
|
73
|
+
checkpointSyncUrl,
|
|
74
|
+
wssCheckpoint: options.wssCheckpoint,
|
|
75
|
+
});
|
|
76
|
+
return prepareCheckpointInitialization(
|
|
77
|
+
{stateBytes, expectedCheckpoint, source: "checkpointSyncUrl"},
|
|
78
|
+
archived,
|
|
79
|
+
{
|
|
80
|
+
isFinalized: true,
|
|
81
|
+
forceCheckpointSync: Boolean(options.forceCheckpointSync),
|
|
82
|
+
ignoreWeakSubjectivityCheck: Boolean(options.ignoreWeakSubjectivityCheck),
|
|
83
|
+
},
|
|
84
|
+
context
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export async function prepareUnfinalizedCheckpointInitialization(
|
|
89
|
+
options: CheckpointOptions &
|
|
90
|
+
Pick<
|
|
91
|
+
StateInitializationOptions,
|
|
92
|
+
"lastPersistedCheckpointState" | "unsafeCheckpointState" | "chain.nHistoricalStatesFileDataStore"
|
|
93
|
+
>,
|
|
94
|
+
archived: ArchivedStateBytes | null,
|
|
95
|
+
context: StatePreparationContext
|
|
96
|
+
): Promise<StateInitialization | null> {
|
|
97
|
+
const {db, dataDir, logger} = context;
|
|
98
|
+
let bytes: Uint8Array | null = null;
|
|
99
|
+
if (options.lastPersistedCheckpointState && !options.forceCheckpointSync) {
|
|
100
|
+
const store = options["chain.nHistoricalStatesFileDataStore"]
|
|
101
|
+
? new FileCPStateDatastore(dataDir)
|
|
102
|
+
: new DbCPStateDatastore(db);
|
|
103
|
+
logger.verbose(`Finding last persisted checkpoint state from ${store.constructor.name}`);
|
|
104
|
+
bytes = await store.readLatestSafe();
|
|
105
|
+
if (bytes === null) {
|
|
106
|
+
logger.warn("Last persisted checkpoint state not found");
|
|
107
|
+
} else {
|
|
108
|
+
logger.info("Found last persisted checkpoint state", {size: formatBytes(bytes.length)});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (bytes === null && options.unsafeCheckpointState) {
|
|
112
|
+
logger.info("Loading checkpoint state", {unsafeCheckpointState: options.unsafeCheckpointState});
|
|
113
|
+
bytes = await downloadOrLoadFile(options.unsafeCheckpointState);
|
|
114
|
+
logger.info("Loaded checkpoint state", {size: formatBytes(bytes.length)});
|
|
115
|
+
}
|
|
116
|
+
if (bytes === null) return null;
|
|
117
|
+
logger.warn(
|
|
118
|
+
"Initializing from unfinalized checkpoint state is unsafe and may cause the node to follow a minority chain"
|
|
119
|
+
);
|
|
120
|
+
const stateBytes = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
121
|
+
return prepareCheckpointInitialization(
|
|
122
|
+
{
|
|
123
|
+
stateBytes,
|
|
124
|
+
expectedCheckpoint: options.wssCheckpoint ? getCheckpointFromArg(options.wssCheckpoint) : null,
|
|
125
|
+
source: "unfinalized source",
|
|
126
|
+
},
|
|
127
|
+
archived,
|
|
128
|
+
{
|
|
129
|
+
isFinalized: false,
|
|
130
|
+
forceCheckpointSync: Boolean(options.forceCheckpointSync),
|
|
131
|
+
ignoreWeakSubjectivityCheck: Boolean(options.ignoreWeakSubjectivityCheck),
|
|
132
|
+
},
|
|
133
|
+
context
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function prepareCheckpointInitialization(
|
|
138
|
+
candidate: CheckpointStateBytesCandidate,
|
|
139
|
+
archived: ArchivedStateBytes | null,
|
|
140
|
+
policy: CheckpointStartupPolicy,
|
|
141
|
+
{chainForkConfig, db, logger}: StatePreparationContext
|
|
142
|
+
): StateInitialization {
|
|
143
|
+
const candidateMetadata = readBeaconStateBytesMetadata(candidate.stateBytes);
|
|
144
|
+
if (candidateMetadata === null) {
|
|
145
|
+
throw new StateInitializationError(
|
|
146
|
+
{code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
|
|
147
|
+
`Cannot read state metadata from ${candidate.source}`
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
if (
|
|
151
|
+
archived !== null &&
|
|
152
|
+
(archived.metadata.genesisTime !== candidateMetadata.genesisTime ||
|
|
153
|
+
!ssz.Root.equals(archived.metadata.genesisValidatorsRoot, candidateMetadata.genesisValidatorsRoot))
|
|
154
|
+
) {
|
|
155
|
+
throw new StateInitializationError(
|
|
156
|
+
{code: StateInitializationErrorCode.INCOMPATIBLE_GENESIS},
|
|
157
|
+
"Db state and checkpoint state are not compatible, either clear the db or verify your checkpoint source"
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
const useArchived =
|
|
161
|
+
archived !== null && archived.metadata.slot > candidateMetadata.slot && !policy.forceCheckpointSync;
|
|
162
|
+
if (useArchived) {
|
|
163
|
+
logger.verbose(
|
|
164
|
+
"Db state is ahead of the provided checkpoint state, using the db state to initialize the beacon chain"
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
const {stateBytes, metadata, weakSubjectivity} = useArchived
|
|
168
|
+
? archived
|
|
169
|
+
: {
|
|
170
|
+
stateBytes: candidate.stateBytes,
|
|
171
|
+
metadata: candidateMetadata,
|
|
172
|
+
weakSubjectivity: computeWeakSubjectivitySummaryFromStateBytes(
|
|
173
|
+
chainForkConfig,
|
|
174
|
+
candidate.stateBytes,
|
|
175
|
+
candidateMetadata
|
|
176
|
+
),
|
|
177
|
+
};
|
|
178
|
+
if (weakSubjectivity === null) {
|
|
179
|
+
throw new StateInitializationError(
|
|
180
|
+
{code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
|
|
181
|
+
`Cannot extract weak subjectivity summary from ${candidate.source}, expected ${chainForkConfig.getForkName(metadata.slot)} state at slot ${metadata.slot}, possible fork or network mismatch`
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
const expectedCheckpoint = useArchived ? null : candidate.expectedCheckpoint;
|
|
185
|
+
const source = useArchived ? "db" : candidate.source;
|
|
186
|
+
const {isFinalized, ignoreWeakSubjectivityCheck} = policy;
|
|
187
|
+
const config = createBeaconConfig(chainForkConfig, metadata.genesisValidatorsRoot);
|
|
188
|
+
const archivedWithinWeakSubjectivityPeriod = useArchived ? archived.isWithinWeakSubjectivityPeriod : null;
|
|
189
|
+
// DB-selected anchors need no persistence, including at slot zero.
|
|
190
|
+
const shouldPersist = isFinalized && !useArchived;
|
|
191
|
+
return {
|
|
192
|
+
stateBytes,
|
|
193
|
+
config,
|
|
194
|
+
isFinalized,
|
|
195
|
+
validate(state) {
|
|
196
|
+
// A supplied checkpoint must match even when the period check is ignored.
|
|
197
|
+
if (expectedCheckpoint !== null) assertStateMatchesCheckpoint(state, expectedCheckpoint);
|
|
198
|
+
const passedValidation =
|
|
199
|
+
archivedWithinWeakSubjectivityPeriod ?? isWithinWeakSubjectivityPeriodFromSummary(config, weakSubjectivity);
|
|
200
|
+
if (!passedValidation && !ignoreWeakSubjectivityCheck) {
|
|
201
|
+
const clockEpoch = computeEpochAtSlot(getCurrentSlot(config, weakSubjectivity.genesisTime));
|
|
202
|
+
throw new StateInitializationError(
|
|
203
|
+
{code: StateInitializationErrorCode.STALE_CHECKPOINT},
|
|
204
|
+
`The selected state with epoch ${weakSubjectivity.checkpointEpoch} is not within weak subjectivity period of ${weakSubjectivity.period} epochs from the current epoch ${clockEpoch}. Please verify your checkpoint source`
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
assertAnchorStateForkMatchesConfig(config, state);
|
|
208
|
+
if (isFinalized) {
|
|
209
|
+
const source = useArchived ? "db" : "checkpoint";
|
|
210
|
+
const logData = {
|
|
211
|
+
slot: state.slot,
|
|
212
|
+
epoch: computeEpochAtSlot(state.slot),
|
|
213
|
+
stateSize: formatBytes(stateBytes.length),
|
|
214
|
+
stateRoot: toRootHex(state.hashTreeRoot()),
|
|
215
|
+
isWithinWeakSubjectivityPeriod: passedValidation,
|
|
216
|
+
};
|
|
217
|
+
if (passedValidation) {
|
|
218
|
+
logger.info(`Initializing beacon from a valid ${source} state`, logData);
|
|
219
|
+
} else {
|
|
220
|
+
logger.warn(`Initializing from a stale ${source} state vulnerable to long range attacks`, logData);
|
|
221
|
+
logger.warn("Checkpoint sync recommended, please use --help to see checkpoint sync options");
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
},
|
|
225
|
+
persist: shouldPersist ? (state, bytes) => persistAnchorState(config, db, state, bytes) : null,
|
|
226
|
+
log(state) {
|
|
227
|
+
const {checkpoint} = state.computeAnchorCheckpoint();
|
|
228
|
+
logger.info("Initialized checkpoint state", {
|
|
229
|
+
source,
|
|
230
|
+
slot: state.slot,
|
|
231
|
+
epoch: checkpoint.epoch,
|
|
232
|
+
stateSize: formatBytes(stateBytes.length),
|
|
233
|
+
stateRoot: toRootHex(state.hashTreeRoot()),
|
|
234
|
+
checkpointRoot: toRootHex(checkpoint.root),
|
|
235
|
+
isFinalized,
|
|
236
|
+
...(isFinalized ? {} : {lastProcessedSlot: state.latestBlockHeader.slot}),
|
|
237
|
+
});
|
|
238
|
+
},
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
function assertStateMatchesCheckpoint(state: IBeaconStateView, checkpoint: Checkpoint): void {
|
|
243
|
+
const {root: blockRoot, epoch: stateEpoch} = state.computeAnchorCheckpoint().checkpoint;
|
|
244
|
+
if (!ssz.Root.equals(blockRoot, checkpoint.root)) {
|
|
245
|
+
throw new StateInitializationError(
|
|
246
|
+
{code: StateInitializationErrorCode.CHECKPOINT_ROOT_MISMATCH},
|
|
247
|
+
`Roots do not match. expected=${toRootHex(checkpoint.root)}, actual=${toRootHex(blockRoot)}`
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
if (!ssz.Epoch.equals(stateEpoch, checkpoint.epoch)) {
|
|
251
|
+
throw new StateInitializationError(
|
|
252
|
+
{code: StateInitializationErrorCode.CHECKPOINT_EPOCH_MISMATCH},
|
|
253
|
+
`Epochs do not match. expected=${checkpoint.epoch}, actual=${stateEpoch}`
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import {LodestarError} from "@lodestar/utils";
|
|
2
|
+
|
|
3
|
+
export enum StateInitializationErrorCode {
|
|
4
|
+
INVALID_CHECKPOINT_SOURCE = "INVALID_CHECKPOINT_SOURCE",
|
|
5
|
+
MALFORMED_STATE_BYTES = "MALFORMED_STATE_BYTES",
|
|
6
|
+
INCOMPATIBLE_GENESIS = "INCOMPATIBLE_GENESIS",
|
|
7
|
+
CHECKPOINT_ROOT_MISMATCH = "CHECKPOINT_ROOT_MISMATCH",
|
|
8
|
+
CHECKPOINT_EPOCH_MISMATCH = "CHECKPOINT_EPOCH_MISMATCH",
|
|
9
|
+
STALE_CHECKPOINT = "STALE_CHECKPOINT",
|
|
10
|
+
ANCHOR_STATE_FORK_MISMATCH = "ANCHOR_STATE_FORK_MISMATCH",
|
|
11
|
+
GENESIS_ROOT_MISMATCH = "GENESIS_ROOT_MISMATCH",
|
|
12
|
+
MISSING_INITIALIZATION_SOURCE = "MISSING_INITIALIZATION_SOURCE",
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export class StateInitializationError extends LodestarError<{code: StateInitializationErrorCode}> {}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import {persistAnchorState} from "@lodestar/beacon-node";
|
|
2
|
+
import {createBeaconConfig} from "@lodestar/config";
|
|
3
|
+
import {
|
|
4
|
+
computeEpochAtSlot,
|
|
5
|
+
computeWeakSubjectivitySummaryFromStateBytes,
|
|
6
|
+
isWithinWeakSubjectivityPeriodFromSummary,
|
|
7
|
+
readBeaconStateBytesMetadata,
|
|
8
|
+
} from "@lodestar/state-transition";
|
|
9
|
+
import {formatBytes, toRootHex} from "@lodestar/utils";
|
|
10
|
+
import {getGenesisFileUrl, getGenesisStateRoot} from "../../../networks/index.js";
|
|
11
|
+
import {defaultNetwork} from "../../../options/globalOptions.js";
|
|
12
|
+
import {downloadOrLoadFile} from "../../../util/index.js";
|
|
13
|
+
import {StateInitializationError, StateInitializationErrorCode} from "./errors.js";
|
|
14
|
+
import {StateInitialization, StateInitializationOptions, StatePreparationContext} from "./types.js";
|
|
15
|
+
import {assertAnchorStateForkMatchesConfig} from "./validation.js";
|
|
16
|
+
|
|
17
|
+
export async function prepareGenesisInitialization(
|
|
18
|
+
options: Pick<StateInitializationOptions, "genesisStateFile" | "network">,
|
|
19
|
+
{chainForkConfig, db, logger}: StatePreparationContext
|
|
20
|
+
): Promise<StateInitialization> {
|
|
21
|
+
const source = options.genesisStateFile || getGenesisFileUrl(options.network || defaultNetwork);
|
|
22
|
+
if (!source) {
|
|
23
|
+
throw new StateInitializationError(
|
|
24
|
+
{code: StateInitializationErrorCode.MISSING_INITIALIZATION_SOURCE},
|
|
25
|
+
"Failed to initialize beacon state, please provide a genesis state file or use checkpoint sync"
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
logger.info("Loading genesis state", {genesisStateFile: source});
|
|
29
|
+
const bytes = await downloadOrLoadFile(source);
|
|
30
|
+
const stateBytes = new Uint8Array(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
31
|
+
logger.info("Loaded genesis state", {size: formatBytes(stateBytes.length)});
|
|
32
|
+
const metadata = readBeaconStateBytesMetadata(stateBytes);
|
|
33
|
+
if (metadata === null) {
|
|
34
|
+
throw new StateInitializationError(
|
|
35
|
+
{code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
|
|
36
|
+
"Cannot read metadata from genesis state bytes"
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
const weakSubjectivity = computeWeakSubjectivitySummaryFromStateBytes(chainForkConfig, stateBytes, metadata);
|
|
40
|
+
if (weakSubjectivity === null) {
|
|
41
|
+
throw new StateInitializationError(
|
|
42
|
+
{code: StateInitializationErrorCode.MALFORMED_STATE_BYTES},
|
|
43
|
+
`Cannot extract weak subjectivity summary from genesis state bytes, expected ${chainForkConfig.getForkName(metadata.slot)} state at slot ${metadata.slot}, possible fork or network mismatch`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
const config = createBeaconConfig(chainForkConfig, metadata.genesisValidatorsRoot);
|
|
47
|
+
const expectedRoot = getGenesisStateRoot(options.network);
|
|
48
|
+
return {
|
|
49
|
+
stateBytes,
|
|
50
|
+
config,
|
|
51
|
+
isFinalized: true,
|
|
52
|
+
validate(state) {
|
|
53
|
+
const stateRoot = toRootHex(state.hashTreeRoot());
|
|
54
|
+
if (expectedRoot !== null && stateRoot !== expectedRoot) {
|
|
55
|
+
throw new StateInitializationError(
|
|
56
|
+
{code: StateInitializationErrorCode.GENESIS_ROOT_MISMATCH},
|
|
57
|
+
`Genesis state root mismatch expected=${expectedRoot} received=${stateRoot}`
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
assertAnchorStateForkMatchesConfig(config, state);
|
|
61
|
+
const passedValidation = isWithinWeakSubjectivityPeriodFromSummary(config, weakSubjectivity);
|
|
62
|
+
const logData = {
|
|
63
|
+
slot: state.slot,
|
|
64
|
+
epoch: computeEpochAtSlot(state.slot),
|
|
65
|
+
stateSize: formatBytes(stateBytes.length),
|
|
66
|
+
stateRoot,
|
|
67
|
+
isWithinWeakSubjectivityPeriod: passedValidation,
|
|
68
|
+
};
|
|
69
|
+
if (passedValidation) {
|
|
70
|
+
logger.info("Initializing beacon from a valid checkpoint state", logData);
|
|
71
|
+
} else {
|
|
72
|
+
logger.warn("Initializing from a stale checkpoint state vulnerable to long range attacks", logData);
|
|
73
|
+
logger.warn("Checkpoint sync recommended, please use --help to see checkpoint sync options");
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
persist: (state, bytes) => persistAnchorState(config, db, state, bytes),
|
|
77
|
+
log(state) {
|
|
78
|
+
logger.info("Initialized genesis state", {
|
|
79
|
+
slot: state.slot,
|
|
80
|
+
stateRoot: toRootHex(state.hashTreeRoot()),
|
|
81
|
+
isFinalized: true,
|
|
82
|
+
});
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {createBeaconConfig} from "@lodestar/config";
|
|
2
|
+
import {computeEpochAtSlot} from "@lodestar/state-transition";
|
|
3
|
+
import {formatBytes, toRootHex} from "@lodestar/utils";
|
|
4
|
+
import {
|
|
5
|
+
prepareCheckpointApiInitialization,
|
|
6
|
+
prepareCheckpointFileInitialization,
|
|
7
|
+
prepareUnfinalizedCheckpointInitialization,
|
|
8
|
+
} from "./checkpointState.js";
|
|
9
|
+
import {ArchivedStateBytes, StateInitialization, StateInitializationOptions, StatePreparationContext} from "./types.js";
|
|
10
|
+
import {assertAnchorStateForkMatchesConfig} from "./validation.js";
|
|
11
|
+
|
|
12
|
+
export function prepareArchivedStateInitialization(
|
|
13
|
+
{stateBytes, metadata, isWithinWeakSubjectivityPeriod}: ArchivedStateBytes,
|
|
14
|
+
{chainForkConfig, logger}: StatePreparationContext
|
|
15
|
+
): StateInitialization {
|
|
16
|
+
const config = createBeaconConfig(chainForkConfig, metadata.genesisValidatorsRoot);
|
|
17
|
+
return {
|
|
18
|
+
stateBytes,
|
|
19
|
+
config,
|
|
20
|
+
isFinalized: true,
|
|
21
|
+
validate(state) {
|
|
22
|
+
assertAnchorStateForkMatchesConfig(config, state);
|
|
23
|
+
const logData = {
|
|
24
|
+
slot: state.slot,
|
|
25
|
+
epoch: computeEpochAtSlot(state.slot),
|
|
26
|
+
stateSize: formatBytes(stateBytes.length),
|
|
27
|
+
stateRoot: toRootHex(state.hashTreeRoot()),
|
|
28
|
+
isWithinWeakSubjectivityPeriod,
|
|
29
|
+
};
|
|
30
|
+
if (isWithinWeakSubjectivityPeriod) {
|
|
31
|
+
logger.info("Initializing beacon from a valid db state", logData);
|
|
32
|
+
} else {
|
|
33
|
+
logger.warn("Initializing from a stale db state vulnerable to long range attacks", logData);
|
|
34
|
+
logger.warn("Checkpoint sync recommended, please use --help to see checkpoint sync options");
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
persist: null,
|
|
38
|
+
log(state) {
|
|
39
|
+
logger.info("Initialized state from db", {
|
|
40
|
+
slot: state.slot,
|
|
41
|
+
epoch: computeEpochAtSlot(state.slot),
|
|
42
|
+
stateRoot: toRootHex(state.hashTreeRoot()),
|
|
43
|
+
isFinalized: true,
|
|
44
|
+
});
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function prepareCheckpointSourceInitialization(
|
|
50
|
+
options: StateInitializationOptions,
|
|
51
|
+
archived: ArchivedStateBytes | null,
|
|
52
|
+
context: StatePreparationContext
|
|
53
|
+
): Promise<StateInitialization | null> {
|
|
54
|
+
if (options.checkpointState) {
|
|
55
|
+
return prepareCheckpointFileInitialization(options.checkpointState, options, archived, context);
|
|
56
|
+
}
|
|
57
|
+
if (options.checkpointSyncUrl) {
|
|
58
|
+
return prepareCheckpointApiInitialization(options.checkpointSyncUrl, options, archived, context);
|
|
59
|
+
}
|
|
60
|
+
if (options.lastPersistedCheckpointState || options.unsafeCheckpointState) {
|
|
61
|
+
return prepareUnfinalizedCheckpointInitialization(options, archived, context);
|
|
62
|
+
}
|
|
63
|
+
return null;
|
|
64
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import {IBeaconDb} from "@lodestar/beacon-node";
|
|
2
|
+
import {BeaconConfig, ChainForkConfig} from "@lodestar/config";
|
|
3
|
+
import {IBeaconStateView, StateBytesMetadata, WeakSubjectivitySummary} from "@lodestar/state-transition";
|
|
4
|
+
import {Checkpoint} from "@lodestar/types/phase0";
|
|
5
|
+
import {Logger} from "@lodestar/utils";
|
|
6
|
+
import {GlobalArgs} from "../../../options/globalOptions.js";
|
|
7
|
+
import {BeaconArgs} from "../options.js";
|
|
8
|
+
|
|
9
|
+
export type StateInitializationOptions = Pick<
|
|
10
|
+
BeaconArgs & GlobalArgs,
|
|
11
|
+
| "checkpointState"
|
|
12
|
+
| "checkpointSyncUrl"
|
|
13
|
+
| "unsafeCheckpointState"
|
|
14
|
+
| "lastPersistedCheckpointState"
|
|
15
|
+
| "genesisStateFile"
|
|
16
|
+
| "wssCheckpoint"
|
|
17
|
+
| "forceCheckpointSync"
|
|
18
|
+
| "ignoreWeakSubjectivityCheck"
|
|
19
|
+
| "network"
|
|
20
|
+
| "chain.nHistoricalStatesFileDataStore"
|
|
21
|
+
| "chain.nativeStateView"
|
|
22
|
+
>;
|
|
23
|
+
|
|
24
|
+
export type StatePreparationContext = {
|
|
25
|
+
chainForkConfig: ChainForkConfig;
|
|
26
|
+
db: IBeaconDb;
|
|
27
|
+
dataDir: string;
|
|
28
|
+
logger: Logger;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export type ArchivedStateBytes = {
|
|
32
|
+
stateBytes: Uint8Array;
|
|
33
|
+
metadata: StateBytesMetadata;
|
|
34
|
+
weakSubjectivity: WeakSubjectivitySummary;
|
|
35
|
+
isWithinWeakSubjectivityPeriod: boolean;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type CheckpointStateBytesCandidate = {
|
|
39
|
+
stateBytes: Uint8Array;
|
|
40
|
+
expectedCheckpoint: Checkpoint | null;
|
|
41
|
+
source: "checkpoint file" | "checkpointSyncUrl" | "unfinalized source";
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type CheckpointStartupPolicy = {
|
|
45
|
+
isFinalized: boolean;
|
|
46
|
+
forceCheckpointSync: boolean;
|
|
47
|
+
ignoreWeakSubjectivityCheck: boolean;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/** Selected state bytes and source-specific initialization steps. */
|
|
51
|
+
export type StateInitialization = {
|
|
52
|
+
stateBytes: Uint8Array;
|
|
53
|
+
config: BeaconConfig;
|
|
54
|
+
isFinalized: boolean;
|
|
55
|
+
validate: (state: IBeaconStateView) => void;
|
|
56
|
+
persist: ((state: IBeaconStateView, stateBytes: Uint8Array) => Promise<void>) | null;
|
|
57
|
+
log: (state: IBeaconStateView) => void;
|
|
58
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import {ChainForkConfig} from "@lodestar/config";
|
|
2
|
+
import {IBeaconStateView, computeStartSlotAtEpoch} from "@lodestar/state-transition";
|
|
3
|
+
import {toHex} from "@lodestar/utils";
|
|
4
|
+
import {StateInitializationError, StateInitializationErrorCode} from "./errors.js";
|
|
5
|
+
|
|
6
|
+
export function assertAnchorStateForkMatchesConfig(config: ChainForkConfig, anchorState: IBeaconStateView): void {
|
|
7
|
+
const expectedFork = config.getForkInfo(computeStartSlotAtEpoch(anchorState.fork.epoch));
|
|
8
|
+
const expectedForkVersion = toHex(expectedFork.version);
|
|
9
|
+
const stateFork = toHex(anchorState.fork.currentVersion);
|
|
10
|
+
if (stateFork !== expectedForkVersion) {
|
|
11
|
+
throw new StateInitializationError(
|
|
12
|
+
{code: StateInitializationErrorCode.ANCHOR_STATE_FORK_MISMATCH},
|
|
13
|
+
`State current fork version ${stateFork} not equal to current config ${expectedForkVersion}. Maybe caused by importing a state from a different network`
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
}
|
package/src/networks/index.ts
CHANGED
|
@@ -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
|
|
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
|
-
//
|
|
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
|
|
179
|
+
const response = await callFnWhenAwait(
|
|
190
180
|
getStatePromise,
|
|
191
181
|
() => logger.info("Download in progress, please wait..."),
|
|
192
182
|
GET_STATE_LOG_INTERVAL
|
|
193
|
-
)
|
|
194
|
-
|
|
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
|
-
|
|
202
|
-
if (
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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
|
-
|
|
211
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
}
|