@lodestar/beacon-node 1.36.0-dev.8ac1ed5b17 → 1.36.0-dev.9dbc67b579
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/lib/api/impl/lodestar/index.d.ts +5 -0
- package/lib/api/impl/lodestar/index.d.ts.map +1 -1
- package/lib/api/impl/lodestar/index.js +29 -1
- package/lib/api/impl/lodestar/index.js.map +1 -1
- package/lib/api/impl/node/utils.js +1 -1
- package/lib/api/impl/node/utils.js.map +1 -1
- package/lib/chain/chain.d.ts +5 -2
- package/lib/chain/chain.d.ts.map +1 -1
- package/lib/chain/chain.js +27 -12
- package/lib/chain/chain.js.map +1 -1
- package/lib/chain/forkChoice/index.d.ts +9 -1
- package/lib/chain/forkChoice/index.d.ts.map +1 -1
- package/lib/chain/forkChoice/index.js +108 -3
- package/lib/chain/forkChoice/index.js.map +1 -1
- package/lib/chain/interface.d.ts +2 -0
- package/lib/chain/interface.d.ts.map +1 -1
- package/lib/chain/stateCache/datastore/db.d.ts +12 -0
- package/lib/chain/stateCache/datastore/db.d.ts.map +1 -1
- package/lib/chain/stateCache/datastore/db.js +70 -0
- package/lib/chain/stateCache/datastore/db.js.map +1 -1
- package/lib/chain/stateCache/datastore/file.d.ts +1 -0
- package/lib/chain/stateCache/datastore/file.d.ts.map +1 -1
- package/lib/chain/stateCache/datastore/file.js +7 -0
- package/lib/chain/stateCache/datastore/file.js.map +1 -1
- package/lib/chain/stateCache/datastore/types.d.ts +1 -0
- package/lib/chain/stateCache/datastore/types.d.ts.map +1 -1
- package/lib/index.d.ts +2 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +2 -0
- package/lib/index.js.map +1 -1
- package/lib/network/reqresp/handlers/beaconBlocksByRange.d.ts.map +1 -1
- package/lib/network/reqresp/handlers/beaconBlocksByRange.js +3 -1
- package/lib/network/reqresp/handlers/beaconBlocksByRange.js.map +1 -1
- package/lib/node/nodejs.d.ts +2 -1
- package/lib/node/nodejs.d.ts.map +1 -1
- package/lib/node/nodejs.js +2 -1
- package/lib/node/nodejs.js.map +1 -1
- package/lib/sync/range/range.d.ts.map +1 -1
- package/lib/sync/range/range.js +2 -1
- package/lib/sync/range/range.js.map +1 -1
- package/lib/sync/utils/remoteSyncType.d.ts +2 -1
- package/lib/sync/utils/remoteSyncType.d.ts.map +1 -1
- package/lib/sync/utils/remoteSyncType.js +19 -4
- package/lib/sync/utils/remoteSyncType.js.map +1 -1
- package/lib/util/sszBytes.d.ts +2 -0
- package/lib/util/sszBytes.d.ts.map +1 -1
- package/lib/util/sszBytes.js +25 -0
- package/lib/util/sszBytes.js.map +1 -1
- package/package.json +14 -14
- package/src/api/impl/lodestar/index.ts +36 -1
- package/src/api/impl/node/utils.ts +1 -1
- package/src/chain/chain.ts +43 -18
- package/src/chain/forkChoice/index.ts +177 -2
- package/src/chain/interface.ts +2 -0
- package/src/chain/stateCache/datastore/db.ts +89 -1
- package/src/chain/stateCache/datastore/file.ts +8 -0
- package/src/chain/stateCache/datastore/types.ts +1 -0
- package/src/index.ts +2 -0
- package/src/network/reqresp/handlers/beaconBlocksByRange.ts +3 -1
- package/src/node/nodejs.ts +3 -0
- package/src/sync/range/range.ts +2 -1
- package/src/sync/utils/remoteSyncType.ts +23 -4
- package/src/util/sszBytes.ts +30 -0
|
@@ -5,17 +5,22 @@ import {
|
|
|
5
5
|
ForkChoiceStore,
|
|
6
6
|
JustifiedBalancesGetter,
|
|
7
7
|
ProtoArray,
|
|
8
|
+
ProtoBlock,
|
|
8
9
|
ForkChoiceOpts as RawForkChoiceOpts,
|
|
9
10
|
} from "@lodestar/fork-choice";
|
|
11
|
+
import {ZERO_HASH_HEX} from "@lodestar/params";
|
|
10
12
|
import {
|
|
11
13
|
CachedBeaconStateAllForks,
|
|
12
14
|
DataAvailabilityStatus,
|
|
13
15
|
computeAnchorCheckpoint,
|
|
16
|
+
computeEpochAtSlot,
|
|
17
|
+
computeStartSlotAtEpoch,
|
|
18
|
+
getBlockRootAtSlot,
|
|
14
19
|
getEffectiveBalanceIncrementsZeroInactive,
|
|
15
20
|
isExecutionStateType,
|
|
16
21
|
isMergeTransitionComplete,
|
|
17
22
|
} from "@lodestar/state-transition";
|
|
18
|
-
import {Slot} from "@lodestar/types";
|
|
23
|
+
import {Slot, ssz} from "@lodestar/types";
|
|
19
24
|
import {Logger, toRootHex} from "@lodestar/utils";
|
|
20
25
|
import {GENESIS_SLOT} from "../../constants/index.js";
|
|
21
26
|
import {Metrics} from "../../metrics/index.js";
|
|
@@ -35,6 +40,43 @@ export enum ForkchoiceCaller {
|
|
|
35
40
|
* Fork Choice extended with a ChainEventEmitter
|
|
36
41
|
*/
|
|
37
42
|
export function initializeForkChoice(
|
|
43
|
+
config: ChainForkConfig,
|
|
44
|
+
emitter: ChainEventEmitter,
|
|
45
|
+
currentSlot: Slot,
|
|
46
|
+
state: CachedBeaconStateAllForks,
|
|
47
|
+
isFinalizedState: boolean,
|
|
48
|
+
opts: ForkChoiceOpts,
|
|
49
|
+
justifiedBalancesGetter: JustifiedBalancesGetter,
|
|
50
|
+
metrics: Metrics | null,
|
|
51
|
+
logger?: Logger
|
|
52
|
+
): ForkChoice {
|
|
53
|
+
return isFinalizedState
|
|
54
|
+
? initializeForkChoiceFromFinalizedState(
|
|
55
|
+
config,
|
|
56
|
+
emitter,
|
|
57
|
+
currentSlot,
|
|
58
|
+
state,
|
|
59
|
+
opts,
|
|
60
|
+
justifiedBalancesGetter,
|
|
61
|
+
metrics,
|
|
62
|
+
logger
|
|
63
|
+
)
|
|
64
|
+
: initializeForkChoiceFromUnfinalizedState(
|
|
65
|
+
config,
|
|
66
|
+
emitter,
|
|
67
|
+
currentSlot,
|
|
68
|
+
state,
|
|
69
|
+
opts,
|
|
70
|
+
justifiedBalancesGetter,
|
|
71
|
+
metrics,
|
|
72
|
+
logger
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Initialize forkchoice from a finalized state.
|
|
78
|
+
*/
|
|
79
|
+
export function initializeForkChoiceFromFinalizedState(
|
|
38
80
|
config: ChainForkConfig,
|
|
39
81
|
emitter: ChainEventEmitter,
|
|
40
82
|
currentSlot: Slot,
|
|
@@ -82,7 +124,7 @@ export function initializeForkChoice(
|
|
|
82
124
|
parentRoot: toRootHex(blockHeader.parentRoot),
|
|
83
125
|
stateRoot: toRootHex(blockHeader.stateRoot),
|
|
84
126
|
blockRoot: toRootHex(checkpoint.root),
|
|
85
|
-
timeliness: true, //
|
|
127
|
+
timeliness: true, // Optimistically assume is timely
|
|
86
128
|
|
|
87
129
|
justifiedEpoch: justifiedCheckpoint.epoch,
|
|
88
130
|
justifiedRoot: toRootHex(justifiedCheckpoint.root),
|
|
@@ -111,3 +153,136 @@ export function initializeForkChoice(
|
|
|
111
153
|
logger
|
|
112
154
|
);
|
|
113
155
|
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Initialize forkchoice from an unfinalized state.
|
|
159
|
+
*/
|
|
160
|
+
export function initializeForkChoiceFromUnfinalizedState(
|
|
161
|
+
config: ChainForkConfig,
|
|
162
|
+
emitter: ChainEventEmitter,
|
|
163
|
+
currentSlot: Slot,
|
|
164
|
+
unfinalizedState: CachedBeaconStateAllForks,
|
|
165
|
+
opts: ForkChoiceOpts,
|
|
166
|
+
justifiedBalancesGetter: JustifiedBalancesGetter,
|
|
167
|
+
metrics: Metrics | null,
|
|
168
|
+
logger?: Logger
|
|
169
|
+
): ForkChoice {
|
|
170
|
+
const {blockHeader} = computeAnchorCheckpoint(config, unfinalizedState);
|
|
171
|
+
const finalizedCheckpoint = unfinalizedState.finalizedCheckpoint.toValue();
|
|
172
|
+
const justifiedCheckpoint = unfinalizedState.currentJustifiedCheckpoint.toValue();
|
|
173
|
+
const headRoot = toRootHex(ssz.phase0.BeaconBlockHeader.hashTreeRoot(blockHeader));
|
|
174
|
+
|
|
175
|
+
const logCtx = {
|
|
176
|
+
currentSlot: currentSlot,
|
|
177
|
+
stateSlot: unfinalizedState.slot,
|
|
178
|
+
headSlot: blockHeader.slot,
|
|
179
|
+
headRoot: headRoot,
|
|
180
|
+
finalizedEpoch: finalizedCheckpoint.epoch,
|
|
181
|
+
finalizedRoot: toRootHex(finalizedCheckpoint.root),
|
|
182
|
+
justifiedEpoch: justifiedCheckpoint.epoch,
|
|
183
|
+
justifiedRoot: toRootHex(justifiedCheckpoint.root),
|
|
184
|
+
};
|
|
185
|
+
logger?.warn("Initializing fork choice from unfinalized state", logCtx);
|
|
186
|
+
|
|
187
|
+
// this is not the justified state, but there is no other ways to get justified balances
|
|
188
|
+
const justifiedBalances = getEffectiveBalanceIncrementsZeroInactive(unfinalizedState);
|
|
189
|
+
const store = new ForkChoiceStore(
|
|
190
|
+
currentSlot,
|
|
191
|
+
justifiedCheckpoint,
|
|
192
|
+
finalizedCheckpoint,
|
|
193
|
+
justifiedBalances,
|
|
194
|
+
justifiedBalancesGetter,
|
|
195
|
+
{
|
|
196
|
+
onJustified: (cp) => emitter.emit(ChainEvent.forkChoiceJustified, cp),
|
|
197
|
+
onFinalized: (cp) => emitter.emit(ChainEvent.forkChoiceFinalized, cp),
|
|
198
|
+
}
|
|
199
|
+
);
|
|
200
|
+
|
|
201
|
+
// this is the same to the finalized state
|
|
202
|
+
const headBlock: ProtoBlock = {
|
|
203
|
+
slot: blockHeader.slot,
|
|
204
|
+
parentRoot: toRootHex(blockHeader.parentRoot),
|
|
205
|
+
stateRoot: toRootHex(blockHeader.stateRoot),
|
|
206
|
+
blockRoot: headRoot,
|
|
207
|
+
targetRoot: headRoot,
|
|
208
|
+
timeliness: true, // Optimistically assume is timely
|
|
209
|
+
|
|
210
|
+
justifiedEpoch: justifiedCheckpoint.epoch,
|
|
211
|
+
justifiedRoot: toRootHex(justifiedCheckpoint.root),
|
|
212
|
+
finalizedEpoch: finalizedCheckpoint.epoch,
|
|
213
|
+
finalizedRoot: toRootHex(finalizedCheckpoint.root),
|
|
214
|
+
unrealizedJustifiedEpoch: justifiedCheckpoint.epoch,
|
|
215
|
+
unrealizedJustifiedRoot: toRootHex(justifiedCheckpoint.root),
|
|
216
|
+
unrealizedFinalizedEpoch: finalizedCheckpoint.epoch,
|
|
217
|
+
unrealizedFinalizedRoot: toRootHex(finalizedCheckpoint.root),
|
|
218
|
+
|
|
219
|
+
...(isExecutionStateType(unfinalizedState) && isMergeTransitionComplete(unfinalizedState)
|
|
220
|
+
? {
|
|
221
|
+
executionPayloadBlockHash: toRootHex(unfinalizedState.latestExecutionPayloadHeader.blockHash),
|
|
222
|
+
executionPayloadNumber: unfinalizedState.latestExecutionPayloadHeader.blockNumber,
|
|
223
|
+
executionStatus: blockHeader.slot === GENESIS_SLOT ? ExecutionStatus.Valid : ExecutionStatus.Syncing,
|
|
224
|
+
}
|
|
225
|
+
: {executionPayloadBlockHash: null, executionStatus: ExecutionStatus.PreMerge}),
|
|
226
|
+
|
|
227
|
+
dataAvailabilityStatus: DataAvailabilityStatus.PreData,
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const parentSlot = blockHeader.slot - 1;
|
|
231
|
+
const parentEpoch = computeEpochAtSlot(parentSlot);
|
|
232
|
+
// parent of head block
|
|
233
|
+
const parentBlock: ProtoBlock = {
|
|
234
|
+
...headBlock,
|
|
235
|
+
slot: parentSlot,
|
|
236
|
+
// link this to the dummy justified block
|
|
237
|
+
parentRoot: toRootHex(justifiedCheckpoint.root),
|
|
238
|
+
// dummy data, we're not able to regen state before headBlock
|
|
239
|
+
stateRoot: ZERO_HASH_HEX,
|
|
240
|
+
blockRoot: headBlock.parentRoot,
|
|
241
|
+
targetRoot: toRootHex(getBlockRootAtSlot(unfinalizedState, computeStartSlotAtEpoch(parentEpoch))),
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const justifiedBlock: ProtoBlock = {
|
|
245
|
+
...headBlock,
|
|
246
|
+
slot: computeStartSlotAtEpoch(justifiedCheckpoint.epoch),
|
|
247
|
+
// link this to the finalized root so that getAncestors can find the finalized block
|
|
248
|
+
parentRoot: toRootHex(finalizedCheckpoint.root),
|
|
249
|
+
// dummy data, we're not able to regen state before headBlock
|
|
250
|
+
stateRoot: ZERO_HASH_HEX,
|
|
251
|
+
blockRoot: toRootHex(justifiedCheckpoint.root),
|
|
252
|
+
// same to blockRoot
|
|
253
|
+
targetRoot: toRootHex(justifiedCheckpoint.root),
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
const finalizedBlock: ProtoBlock = {
|
|
257
|
+
...headBlock,
|
|
258
|
+
slot: computeStartSlotAtEpoch(finalizedCheckpoint.epoch),
|
|
259
|
+
// we don't care parent of finalized block
|
|
260
|
+
parentRoot: ZERO_HASH_HEX,
|
|
261
|
+
// dummy data, we're not able to regen state before headBlock
|
|
262
|
+
stateRoot: ZERO_HASH_HEX,
|
|
263
|
+
blockRoot: toRootHex(finalizedCheckpoint.root),
|
|
264
|
+
// same to blockRoot
|
|
265
|
+
targetRoot: toRootHex(finalizedCheckpoint.root),
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
const protoArray = ProtoArray.initialize(finalizedBlock, currentSlot);
|
|
269
|
+
protoArray.onBlock(justifiedBlock, currentSlot);
|
|
270
|
+
protoArray.onBlock(parentBlock, currentSlot);
|
|
271
|
+
protoArray.onBlock(headBlock, currentSlot);
|
|
272
|
+
|
|
273
|
+
logger?.verbose("Initialized protoArray successfully", {...logCtx, length: protoArray.length()});
|
|
274
|
+
|
|
275
|
+
// forkchoiceConstructor is only used for some test cases
|
|
276
|
+
// production code use ForkChoice constructor directly
|
|
277
|
+
const forkchoiceConstructor = opts.forkchoiceConstructor ?? ForkChoice;
|
|
278
|
+
|
|
279
|
+
return new forkchoiceConstructor(
|
|
280
|
+
config,
|
|
281
|
+
store,
|
|
282
|
+
protoArray,
|
|
283
|
+
unfinalizedState.validators.length,
|
|
284
|
+
metrics,
|
|
285
|
+
opts,
|
|
286
|
+
logger
|
|
287
|
+
);
|
|
288
|
+
}
|
package/src/chain/interface.ts
CHANGED
|
@@ -178,6 +178,8 @@ export interface IBeaconChain {
|
|
|
178
178
|
stateRoot: RootHex,
|
|
179
179
|
opts?: StateGetOpts
|
|
180
180
|
): Promise<{state: BeaconStateAllForks; executionOptimistic: boolean; finalized: boolean} | null>;
|
|
181
|
+
/** Return serialized bytes of a persisted checkpoint state */
|
|
182
|
+
getPersistedCheckpointState(checkpoint?: phase0.Checkpoint): Promise<Uint8Array | null>;
|
|
181
183
|
/** Returns a cached state by checkpoint */
|
|
182
184
|
getStateByCheckpoint(
|
|
183
185
|
checkpoint: CheckpointWithHex
|
|
@@ -1,5 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {SLOTS_PER_EPOCH} from "@lodestar/params";
|
|
2
|
+
import {Epoch, phase0, ssz} from "@lodestar/types";
|
|
3
|
+
import {MapDef} from "@lodestar/utils";
|
|
2
4
|
import {IBeaconDb} from "../../../db/interface.js";
|
|
5
|
+
import {
|
|
6
|
+
getLastProcessedSlotFromBeaconStateSerialized,
|
|
7
|
+
getSlotFromBeaconStateSerialized,
|
|
8
|
+
} from "../../../util/sszBytes.js";
|
|
3
9
|
import {CPStateDatastore, DatastoreKey} from "./types.js";
|
|
4
10
|
|
|
5
11
|
/**
|
|
@@ -22,6 +28,13 @@ export class DbCPStateDatastore implements CPStateDatastore {
|
|
|
22
28
|
return this.db.checkpointState.getBinary(serializedCheckpoint);
|
|
23
29
|
}
|
|
24
30
|
|
|
31
|
+
async readLatestSafe(): Promise<Uint8Array | null> {
|
|
32
|
+
const allKeys = await this.readKeys();
|
|
33
|
+
if (allKeys.length === 0) return null;
|
|
34
|
+
|
|
35
|
+
return getLatestSafeDatastoreKey(allKeys, this.read.bind(this));
|
|
36
|
+
}
|
|
37
|
+
|
|
25
38
|
async readKeys(): Promise<DatastoreKey[]> {
|
|
26
39
|
return this.db.checkpointState.keys();
|
|
27
40
|
}
|
|
@@ -34,3 +47,78 @@ export function datastoreKeyToCheckpoint(key: DatastoreKey): phase0.Checkpoint {
|
|
|
34
47
|
export function checkpointToDatastoreKey(cp: phase0.Checkpoint): DatastoreKey {
|
|
35
48
|
return ssz.phase0.Checkpoint.serialize(cp);
|
|
36
49
|
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Get the latest safe checkpoint state the node can use to boot from
|
|
53
|
+
* - it should be the checkpoint state that's unique in its epoch
|
|
54
|
+
* - its last processed block slot should be at epoch boundary or last slot of previous epoch
|
|
55
|
+
* - state slot should be at epoch boundary
|
|
56
|
+
* - state slot should be equal to epoch * SLOTS_PER_EPOCH
|
|
57
|
+
*
|
|
58
|
+
* return the serialized data of Current Root Checkpoint State (CRCS) or Previous Root Checkpoint State (PRCS)
|
|
59
|
+
*
|
|
60
|
+
*/
|
|
61
|
+
export async function getLatestSafeDatastoreKey(
|
|
62
|
+
allKeys: DatastoreKey[],
|
|
63
|
+
readFn: (key: DatastoreKey) => Promise<Uint8Array | null>
|
|
64
|
+
): Promise<Uint8Array | null> {
|
|
65
|
+
const checkpointsByEpoch = new MapDef<Epoch, DatastoreKey[]>(() => []);
|
|
66
|
+
for (const key of allKeys) {
|
|
67
|
+
const cp = datastoreKeyToCheckpoint(key);
|
|
68
|
+
checkpointsByEpoch.getOrDefault(cp.epoch).push(key);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const dataStoreKeyByEpoch: Map<Epoch, DatastoreKey> = new Map();
|
|
72
|
+
for (const [epoch, keys] of checkpointsByEpoch.entries()) {
|
|
73
|
+
// only consider epochs with a single checkpoint to avoid ambiguity from forks
|
|
74
|
+
if (keys.length === 1) {
|
|
75
|
+
dataStoreKeyByEpoch.set(epoch, keys[0]);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const epochsDesc = Array.from(dataStoreKeyByEpoch.keys()).sort((a, b) => b - a);
|
|
80
|
+
for (const epoch of epochsDesc) {
|
|
81
|
+
const datastoreKey = dataStoreKeyByEpoch.get(epoch);
|
|
82
|
+
if (datastoreKey == null) {
|
|
83
|
+
// should not happen
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const stateBytes = await readFn(datastoreKey);
|
|
88
|
+
if (stateBytes == null) {
|
|
89
|
+
// should not happen
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const lastProcessedSlot = getLastProcessedSlotFromBeaconStateSerialized(stateBytes);
|
|
94
|
+
if (lastProcessedSlot == null) {
|
|
95
|
+
// cannot extract last processed slot from serialized state, skip
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const stateSlot = getSlotFromBeaconStateSerialized(stateBytes);
|
|
100
|
+
if (stateSlot == null) {
|
|
101
|
+
// cannot extract slot from serialized state, skip
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if (lastProcessedSlot !== stateSlot && lastProcessedSlot !== stateSlot - 1) {
|
|
106
|
+
// not CRCS or PRCS, skip
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (stateSlot % SLOTS_PER_EPOCH !== 0) {
|
|
111
|
+
// not at epoch boundary, skip
|
|
112
|
+
continue;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (stateSlot !== SLOTS_PER_EPOCH * epoch) {
|
|
116
|
+
// should not happen after above checks, but just to be safe
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return stateBytes;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
@@ -2,6 +2,7 @@ import path from "node:path";
|
|
|
2
2
|
import {phase0, ssz} from "@lodestar/types";
|
|
3
3
|
import {fromHex, toHex} from "@lodestar/utils";
|
|
4
4
|
import {ensureDir, readFile, readFileNames, removeFile, writeIfNotExist} from "../../../util/file.js";
|
|
5
|
+
import {getLatestSafeDatastoreKey} from "./db.js";
|
|
5
6
|
import {CPStateDatastore, DatastoreKey} from "./types.js";
|
|
6
7
|
|
|
7
8
|
const CHECKPOINT_STATES_FOLDER = "checkpoint_states";
|
|
@@ -44,6 +45,13 @@ export class FileCPStateDatastore implements CPStateDatastore {
|
|
|
44
45
|
return readFile(filePath);
|
|
45
46
|
}
|
|
46
47
|
|
|
48
|
+
async readLatestSafe(): Promise<Uint8Array | null> {
|
|
49
|
+
const allKeys = await this.readKeys();
|
|
50
|
+
if (allKeys.length === 0) return null;
|
|
51
|
+
|
|
52
|
+
return getLatestSafeDatastoreKey(allKeys, this.read.bind(this));
|
|
53
|
+
}
|
|
54
|
+
|
|
47
55
|
async readKeys(): Promise<DatastoreKey[]> {
|
|
48
56
|
const fileNames = await readFileNames(this.folderPath);
|
|
49
57
|
return fileNames
|
|
@@ -8,6 +8,7 @@ export interface CPStateDatastore {
|
|
|
8
8
|
write: (cpKey: phase0.Checkpoint, stateBytes: Uint8Array) => Promise<DatastoreKey>;
|
|
9
9
|
remove: (key: DatastoreKey) => Promise<void>;
|
|
10
10
|
read: (key: DatastoreKey) => Promise<Uint8Array | null>;
|
|
11
|
+
readLatestSafe: () => Promise<Uint8Array | null>;
|
|
11
12
|
readKeys: () => Promise<DatastoreKey[]>;
|
|
12
13
|
init?: () => Promise<void>;
|
|
13
14
|
}
|
package/src/index.ts
CHANGED
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
export type {RestApiServerMetrics, RestApiServerModules, RestApiServerOpts} from "./api/rest/base.js";
|
|
4
4
|
export {RestApiServer} from "./api/rest/base.js";
|
|
5
5
|
export {checkAndPersistAnchorState, initStateFromDb, initStateFromEth1} from "./chain/index.js";
|
|
6
|
+
export {DbCPStateDatastore} from "./chain/stateCache/datastore/db.js";
|
|
7
|
+
export {FileCPStateDatastore} from "./chain/stateCache/datastore/file.js";
|
|
6
8
|
export {BeaconDb, type IBeaconDb} from "./db/index.js";
|
|
7
9
|
export {Eth1Provider, type IEth1Provider} from "./eth1/index.js";
|
|
8
10
|
// Export metrics utilities to de-duplicate validator metrics
|
|
@@ -19,7 +19,9 @@ export async function* onBeaconBlocksByRange(
|
|
|
19
19
|
|
|
20
20
|
const finalized = db.blockArchive;
|
|
21
21
|
const unfinalized = db.block;
|
|
22
|
-
|
|
22
|
+
// in the case of initializing from a non-finalized state, we don't have the finalized block so this api does not work
|
|
23
|
+
// chain.forkChoice.getFinalizeBlock().slot
|
|
24
|
+
const finalizedSlot = chain.forkChoice.getFinalizedCheckpointSlot();
|
|
23
25
|
|
|
24
26
|
// Finalized range of blocks
|
|
25
27
|
if (startSlot <= finalizedSlot) {
|
package/src/node/nodejs.ts
CHANGED
|
@@ -53,6 +53,7 @@ export type BeaconNodeInitModules = {
|
|
|
53
53
|
dataDir: string;
|
|
54
54
|
peerStoreDir?: string;
|
|
55
55
|
anchorState: BeaconStateAllForks;
|
|
56
|
+
isAnchorStateFinalized: boolean;
|
|
56
57
|
wsCheckpoint?: phase0.Checkpoint;
|
|
57
58
|
metricsRegistries?: Registry[];
|
|
58
59
|
};
|
|
@@ -154,6 +155,7 @@ export class BeaconNode {
|
|
|
154
155
|
dataDir,
|
|
155
156
|
peerStoreDir,
|
|
156
157
|
anchorState,
|
|
158
|
+
isAnchorStateFinalized,
|
|
157
159
|
wsCheckpoint,
|
|
158
160
|
metricsRegistries = [],
|
|
159
161
|
}: BeaconNodeInitModules): Promise<T> {
|
|
@@ -217,6 +219,7 @@ export class BeaconNode {
|
|
|
217
219
|
metrics,
|
|
218
220
|
validatorMonitor,
|
|
219
221
|
anchorState,
|
|
222
|
+
isAnchorStateFinalized,
|
|
220
223
|
eth1: initializeEth1ForBlockProduction(opts.eth1, {
|
|
221
224
|
config,
|
|
222
225
|
db,
|
package/src/sync/range/range.ts
CHANGED
|
@@ -114,13 +114,14 @@ export class RangeSync extends (EventEmitter as {new (): RangeSyncEmitter}) {
|
|
|
114
114
|
*/
|
|
115
115
|
addPeer(peerId: PeerIdStr, localStatus: Status, peerStatus: Status): void {
|
|
116
116
|
// Compute if we should do a Finalized or Head sync with this peer
|
|
117
|
-
const {syncType, startEpoch, target} = getRangeSyncTarget(localStatus, peerStatus, this.chain
|
|
117
|
+
const {syncType, startEpoch, target} = getRangeSyncTarget(localStatus, peerStatus, this.chain);
|
|
118
118
|
this.logger.debug("Sync peer joined", {
|
|
119
119
|
peer: peerId,
|
|
120
120
|
syncType,
|
|
121
121
|
startEpoch,
|
|
122
122
|
targetSlot: target.slot,
|
|
123
123
|
targetRoot: toRootHex(target.root),
|
|
124
|
+
localHeadSlot: localStatus.headSlot,
|
|
124
125
|
earliestAvailableSlot: (peerStatus as fulu.Status).earliestAvailableSlot ?? Infinity,
|
|
125
126
|
});
|
|
126
127
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {IForkChoice} from "@lodestar/fork-choice";
|
|
2
2
|
import {computeEpochAtSlot, computeStartSlotAtEpoch} from "@lodestar/state-transition";
|
|
3
3
|
import {Slot, Status} from "@lodestar/types";
|
|
4
|
+
import {IBeaconChain} from "../../chain/interface.ts";
|
|
4
5
|
import {ChainTarget} from "../range/utils/index.js";
|
|
5
6
|
|
|
6
7
|
/** The type of peer relative to our current state */
|
|
@@ -103,8 +104,11 @@ export function getRangeSyncType(local: Status, remote: Status, forkChoice: IFor
|
|
|
103
104
|
export function getRangeSyncTarget(
|
|
104
105
|
local: Status,
|
|
105
106
|
remote: Status,
|
|
106
|
-
|
|
107
|
+
chain: IBeaconChain
|
|
107
108
|
): {syncType: RangeSyncType; startEpoch: Slot; target: ChainTarget} {
|
|
109
|
+
const forkChoice = chain.forkChoice;
|
|
110
|
+
|
|
111
|
+
// finalized sync
|
|
108
112
|
if (remote.finalizedEpoch > local.finalizedEpoch && !forkChoice.hasBlock(remote.finalizedRoot)) {
|
|
109
113
|
return {
|
|
110
114
|
// If RangeSyncType.Finalized, the range of blocks fetchable from startEpoch and target must allow to switch
|
|
@@ -131,11 +135,26 @@ export function getRangeSyncTarget(
|
|
|
131
135
|
},
|
|
132
136
|
};
|
|
133
137
|
}
|
|
138
|
+
|
|
139
|
+
// we don't want to sync from epoch < minEpoch
|
|
140
|
+
// if we boot from an unfinalized checkpoint state, we don't want to sync before anchorStateLatestBlockSlot
|
|
141
|
+
// if we boot from a finalized checkpoint state, anchorStateLatestBlockSlot is trusted and we also don't want to sync before it
|
|
142
|
+
const minEpoch = Math.max(remote.finalizedEpoch, computeEpochAtSlot(chain.anchorStateLatestBlockSlot));
|
|
143
|
+
|
|
144
|
+
// head sync
|
|
134
145
|
return {
|
|
135
146
|
syncType: RangeSyncType.Head,
|
|
136
|
-
// The new peer has the same finalized
|
|
137
|
-
//
|
|
138
|
-
|
|
147
|
+
// The new peer has the same finalized `remote.finalizedEpoch == local.finalizedEpoch` since
|
|
148
|
+
// previous filters should prevent a peer with an earlier finalized chain from reaching here.
|
|
149
|
+
//
|
|
150
|
+
// By default and during stable network conditions, the head sync always starts from
|
|
151
|
+
// the finalized epoch (even though it's the head sync) because finalized epoch is < local head.
|
|
152
|
+
// This is to prevent the issue noted here https://github.com/ChainSafe/lodestar/pull/7509#discussion_r1984353063.
|
|
153
|
+
//
|
|
154
|
+
// During non-finality of the network, when starting from an unfinalized checkpoint state, we don't want
|
|
155
|
+
// to sync before anchorStateLatestBlockSlot as finalized epoch is too far away. Local head will also be
|
|
156
|
+
// the same to that value at startup, the head sync always starts from anchorStateLatestBlockSlot in this case.
|
|
157
|
+
startEpoch: Math.min(computeEpochAtSlot(local.headSlot), minEpoch),
|
|
139
158
|
target: {
|
|
140
159
|
slot: remote.headSlot,
|
|
141
160
|
root: remote.headRoot,
|
package/src/util/sszBytes.ts
CHANGED
|
@@ -417,6 +417,36 @@ export function getSlotFromDataColumnSidecarSerialized(data: Uint8Array): Slot |
|
|
|
417
417
|
return getSlotFromOffset(data, SLOT_BYTES_POSITION_IN_SIGNED_DATA_COLUMN_SIDECAR);
|
|
418
418
|
}
|
|
419
419
|
|
|
420
|
+
/**
|
|
421
|
+
* BeaconState of all forks (up until Electra, check with new forks)
|
|
422
|
+
* class BeaconState(Container):
|
|
423
|
+
* genesis_time: uint64 - 8 bytes
|
|
424
|
+
* genesis_validators_root: Root - 32 bytes
|
|
425
|
+
* slot: Slot - 8 bytes
|
|
426
|
+
* fork: Fork - 16 bytes
|
|
427
|
+
* latest_block_header: BeaconBlockHeader - fixed size
|
|
428
|
+
* slot: Slot - 8 bytes
|
|
429
|
+
*
|
|
430
|
+
*/
|
|
431
|
+
|
|
432
|
+
const BLOCK_HEADER_SLOT_BYTES_POSITION_IN_BEACON_STATE = 8 + 32 + 8 + 16;
|
|
433
|
+
export function getLastProcessedSlotFromBeaconStateSerialized(data: Uint8Array): Slot | null {
|
|
434
|
+
if (data.length < BLOCK_HEADER_SLOT_BYTES_POSITION_IN_BEACON_STATE + SLOT_SIZE) {
|
|
435
|
+
return null;
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return getSlotFromOffset(data, BLOCK_HEADER_SLOT_BYTES_POSITION_IN_BEACON_STATE);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const SLOT_BYTES_POSITION_IN_BEACON_STATE = 8 + 32;
|
|
442
|
+
export function getSlotFromBeaconStateSerialized(data: Uint8Array): Slot | null {
|
|
443
|
+
if (data.length < SLOT_BYTES_POSITION_IN_BEACON_STATE) {
|
|
444
|
+
return null;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
return getSlotFromOffset(data, SLOT_BYTES_POSITION_IN_BEACON_STATE);
|
|
448
|
+
}
|
|
449
|
+
|
|
420
450
|
/**
|
|
421
451
|
* Read only the first 4 bytes of Slot, max value is 4,294,967,295 will be reached 1634 years after genesis
|
|
422
452
|
*
|