@aztec/archiver 0.0.1-commit.4d3c002 → 0.0.1-commit.4d9804df
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/README.md +7 -5
- package/dest/archiver.d.ts +70 -15
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +274 -71
- package/dest/config.d.ts +7 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +39 -16
- package/dest/errors.d.ts +35 -13
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +54 -16
- package/dest/factory.d.ts +11 -6
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +35 -21
- package/dest/index.d.ts +11 -3
- package/dest/index.d.ts.map +1 -1
- package/dest/index.js +10 -2
- package/dest/l1/calldata_retriever.d.ts +9 -1
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +20 -10
- package/dest/l1/data_retrieval.d.ts +24 -10
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +13 -19
- package/dest/l1/trace_tx.d.ts +12 -66
- package/dest/l1/trace_tx.d.ts.map +1 -1
- package/dest/l1/validate_historical_logs.d.ts +23 -0
- package/dest/l1/validate_historical_logs.d.ts.map +1 -0
- package/dest/l1/validate_historical_logs.js +108 -0
- package/dest/modules/contract_data_source_adapter.d.ts +25 -0
- package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
- package/dest/modules/contract_data_source_adapter.js +32 -0
- package/dest/modules/data_source_base.d.ts +71 -50
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +250 -150
- package/dest/modules/data_store_updater.d.ts +28 -9
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +121 -61
- package/dest/modules/instrumentation.d.ts +15 -2
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +48 -9
- package/dest/modules/l1_synchronizer.d.ts +11 -7
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +347 -118
- package/dest/modules/outbox_trees_resolver.d.ts +62 -0
- package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
- package/dest/modules/outbox_trees_resolver.js +162 -0
- package/dest/modules/validation.d.ts +30 -7
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +33 -15
- package/dest/store/block_store.d.ts +167 -94
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +524 -260
- package/dest/store/contract_class_store.d.ts +17 -3
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +36 -2
- package/dest/store/contract_instance_store.d.ts +28 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +49 -1
- package/dest/store/data_stores.d.ts +68 -0
- package/dest/store/data_stores.d.ts.map +1 -0
- package/dest/store/data_stores.js +54 -0
- package/dest/store/function_names_cache.d.ts +17 -0
- package/dest/store/function_names_cache.d.ts.map +1 -0
- package/dest/store/function_names_cache.js +30 -0
- package/dest/store/l2_tips_cache.d.ts +13 -8
- package/dest/store/l2_tips_cache.d.ts.map +1 -1
- package/dest/store/l2_tips_cache.js +13 -96
- package/dest/store/log_store.d.ts +42 -40
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +257 -480
- package/dest/store/log_store_codec.d.ts +78 -0
- package/dest/store/log_store_codec.d.ts.map +1 -0
- package/dest/store/log_store_codec.js +110 -0
- package/dest/store/message_store.d.ts +9 -3
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +31 -1
- package/dest/test/fake_l1_state.d.ts +7 -3
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +45 -18
- package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
- package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
- package/dest/test/mock_l1_to_l2_message_source.js +1 -2
- package/dest/test/mock_l2_block_source.d.ts +47 -54
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +229 -205
- package/dest/test/mock_structs.d.ts +1 -1
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +2 -2
- package/dest/test/noop_l1_archiver.d.ts +14 -6
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +26 -10
- package/package.json +14 -14
- package/src/archiver.ts +355 -77
- package/src/config.ts +43 -20
- package/src/errors.ts +80 -23
- package/src/factory.ts +46 -19
- package/src/index.ts +18 -2
- package/src/l1/calldata_retriever.ts +27 -10
- package/src/l1/data_retrieval.ts +38 -36
- package/src/l1/validate_historical_logs.ts +140 -0
- package/src/modules/contract_data_source_adapter.ts +46 -0
- package/src/modules/data_source_base.ts +319 -200
- package/src/modules/data_store_updater.ts +146 -64
- package/src/modules/instrumentation.ts +43 -10
- package/src/modules/l1_synchronizer.ts +499 -136
- package/src/modules/outbox_trees_resolver.ts +199 -0
- package/src/modules/validation.ts +78 -21
- package/src/store/block_store.ts +627 -321
- package/src/store/contract_class_store.ts +47 -3
- package/src/store/contract_instance_store.ts +61 -1
- package/src/store/data_stores.ts +104 -0
- package/src/store/function_names_cache.ts +37 -0
- package/src/store/l2_tips_cache.ts +16 -109
- package/src/store/log_store.ts +296 -652
- package/src/store/log_store_codec.ts +143 -0
- package/src/store/message_store.ts +35 -2
- package/src/test/fake_l1_state.ts +50 -27
- package/src/test/mock_l1_to_l2_message_source.ts +0 -1
- package/src/test/mock_l2_block_source.ts +276 -258
- package/src/test/mock_structs.ts +5 -2
- package/src/test/noop_l1_archiver.ts +54 -11
- package/dest/store/kv_archiver_store.d.ts +0 -377
- package/dest/store/kv_archiver_store.d.ts.map +0 -1
- package/dest/store/kv_archiver_store.js +0 -494
- package/src/store/kv_archiver_store.ts +0 -713
package/src/store/block_store.ts
CHANGED
|
@@ -12,9 +12,11 @@ import {
|
|
|
12
12
|
type BlockData,
|
|
13
13
|
BlockHash,
|
|
14
14
|
Body,
|
|
15
|
-
CheckpointedL2Block,
|
|
16
15
|
CommitteeAttestation,
|
|
16
|
+
GENESIS_CHECKPOINT_HEADER_HASH,
|
|
17
17
|
L2Block,
|
|
18
|
+
type L2TipId,
|
|
19
|
+
type L2Tips,
|
|
18
20
|
type ValidateCheckpointResult,
|
|
19
21
|
deserializeValidateCheckpointResult,
|
|
20
22
|
serializeValidateCheckpointResult,
|
|
@@ -28,7 +30,6 @@ import {
|
|
|
28
30
|
type ProposedCheckpointInput,
|
|
29
31
|
PublishedCheckpoint,
|
|
30
32
|
} from '@aztec/stdlib/checkpoint';
|
|
31
|
-
import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
32
33
|
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
33
34
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
34
35
|
import {
|
|
@@ -36,8 +37,6 @@ import {
|
|
|
36
37
|
type IndexedTxEffect,
|
|
37
38
|
TxEffect,
|
|
38
39
|
TxHash,
|
|
39
|
-
TxReceipt,
|
|
40
|
-
TxStatus,
|
|
41
40
|
deserializeIndexedTxEffect,
|
|
42
41
|
serializeIndexedTxEffect,
|
|
43
42
|
} from '@aztec/stdlib/tx';
|
|
@@ -45,6 +44,7 @@ import {
|
|
|
45
44
|
import {
|
|
46
45
|
BlockAlreadyCheckpointedError,
|
|
47
46
|
BlockArchiveNotConsistentError,
|
|
47
|
+
BlockCheckpointNumberNotSequentialError,
|
|
48
48
|
BlockIndexNotSequentialError,
|
|
49
49
|
BlockNotFoundError,
|
|
50
50
|
BlockNumberNotSequentialError,
|
|
@@ -52,11 +52,13 @@ import {
|
|
|
52
52
|
CheckpointNotFoundError,
|
|
53
53
|
CheckpointNumberNotSequentialError,
|
|
54
54
|
InitialCheckpointNumberNotSequentialError,
|
|
55
|
+
NoProposedCheckpointToPromoteError,
|
|
56
|
+
ProposedCheckpointArchiveRootMismatchError,
|
|
55
57
|
ProposedCheckpointNotSequentialError,
|
|
56
|
-
|
|
58
|
+
ProposedCheckpointPromotionNotSequentialError,
|
|
57
59
|
} from '../errors.js';
|
|
58
60
|
|
|
59
|
-
export {
|
|
61
|
+
export type { TxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
|
|
60
62
|
|
|
61
63
|
type BlockIndexValue = [blockNumber: number, index: number];
|
|
62
64
|
|
|
@@ -68,6 +70,41 @@ type BlockStorage = {
|
|
|
68
70
|
indexWithinCheckpoint: number;
|
|
69
71
|
};
|
|
70
72
|
|
|
73
|
+
/** Reason a checkpoint was rejected during sync. */
|
|
74
|
+
export type RejectedCheckpointReason = 'invalid-attestations' | 'descends-from-invalid-attestations';
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A checkpoint observed on L1 that the archiver decided not to ingest, recorded so that
|
|
78
|
+
* any descendant that builds on top of it can also be skipped (rather than throwing
|
|
79
|
+
* `InitialCheckpointNumberNotSequentialError` and looping). An entry is dropped via
|
|
80
|
+
* {@link BlockStore.removeRejectedCheckpointByArchiveRoot} once a checkpoint with the same
|
|
81
|
+
* archive root is later ingested as valid (e.g. it gathered enough attestations), which
|
|
82
|
+
* re-enables its descendants.
|
|
83
|
+
*/
|
|
84
|
+
export type RejectedCheckpoint = {
|
|
85
|
+
/** Checkpoint number this entry represents. */
|
|
86
|
+
checkpointNumber: CheckpointNumber;
|
|
87
|
+
/** Archive root produced by this rejected checkpoint (matched against descendants' `lastArchiveRoot`). */
|
|
88
|
+
archiveRoot: Fr;
|
|
89
|
+
/** `lastArchiveRoot` from this checkpoint's header (the ancestor it built on). */
|
|
90
|
+
parentArchiveRoot: Fr;
|
|
91
|
+
/** Slot number of the rejected checkpoint. */
|
|
92
|
+
slotNumber: SlotNumber;
|
|
93
|
+
/** L1 publication data for the rejected checkpoint (block number, hash, timestamp). */
|
|
94
|
+
l1: L1PublishedData;
|
|
95
|
+
/** Why the entry was recorded. */
|
|
96
|
+
reason: RejectedCheckpointReason;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
type RejectedCheckpointStorage = {
|
|
100
|
+
checkpointNumber: number;
|
|
101
|
+
archiveRoot: Buffer;
|
|
102
|
+
parentArchiveRoot: Buffer;
|
|
103
|
+
slotNumber: number;
|
|
104
|
+
l1: Buffer;
|
|
105
|
+
reason: RejectedCheckpointReason;
|
|
106
|
+
};
|
|
107
|
+
|
|
71
108
|
/** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
|
|
72
109
|
type CommonCheckpointStorage = {
|
|
73
110
|
header: Buffer;
|
|
@@ -81,6 +118,7 @@ type CommonCheckpointStorage = {
|
|
|
81
118
|
type CheckpointStorage = CommonCheckpointStorage & {
|
|
82
119
|
l1: Buffer;
|
|
83
120
|
attestations: Buffer[];
|
|
121
|
+
feeAssetPriceModifier: string;
|
|
84
122
|
};
|
|
85
123
|
|
|
86
124
|
/** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
|
|
@@ -91,6 +129,19 @@ type ProposedCheckpointStorage = CommonCheckpointStorage & {
|
|
|
91
129
|
|
|
92
130
|
export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
|
|
93
131
|
|
|
132
|
+
/**
|
|
133
|
+
* Single-block lookup with the chain-tip `tag` variant of {@link BlockQuery} already resolved
|
|
134
|
+
* to a concrete block number. The `tag` branch is unrepresentable here so storage code does
|
|
135
|
+
* not need to handle it at runtime.
|
|
136
|
+
*/
|
|
137
|
+
export type ResolvedBlockQuery = { number: BlockNumber } | { hash: BlockHash } | { archive: Fr };
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Range lookup with the `epoch` variant of {@link BlocksQuery} already resolved to a
|
|
141
|
+
* `{ from, limit }` pair. Storage code never needs to map epoch numbers to block ranges.
|
|
142
|
+
*/
|
|
143
|
+
export type ResolvedBlocksQuery = { from: BlockNumber; limit: number; onlyCheckpointed?: boolean };
|
|
144
|
+
|
|
94
145
|
/**
|
|
95
146
|
* LMDB-based block storage for the archiver.
|
|
96
147
|
*/
|
|
@@ -98,7 +149,10 @@ export class BlockStore {
|
|
|
98
149
|
/** Map block number to block data */
|
|
99
150
|
#blocks: AztecAsyncMap<number, BlockStorage>;
|
|
100
151
|
|
|
101
|
-
/** Map checkpoint number
|
|
152
|
+
/** Map keyed by checkpoint number holding proposed (locally-validated, not yet L1-confirmed) checkpoints. */
|
|
153
|
+
#proposedCheckpoints: AztecAsyncMap<number, ProposedCheckpointStorage>;
|
|
154
|
+
|
|
155
|
+
/** Map checkpoint number to checkpoint data for mined checkpoints only */
|
|
102
156
|
#checkpoints: AztecAsyncMap<number, CheckpointStorage>;
|
|
103
157
|
|
|
104
158
|
/** Map slot number to checkpoint number, for looking up checkpoints by slot range. */
|
|
@@ -131,8 +185,11 @@ export class BlockStore {
|
|
|
131
185
|
/** Index mapping block archive to block number */
|
|
132
186
|
#blockArchiveIndex: AztecAsyncMap<string, number>;
|
|
133
187
|
|
|
134
|
-
/**
|
|
135
|
-
#
|
|
188
|
+
/** Map rejected checkpoints (due to invalid attestations) by archive root */
|
|
189
|
+
#rejectedCheckpoints: AztecAsyncMap<string, RejectedCheckpointStorage>;
|
|
190
|
+
|
|
191
|
+
/** Index mapping a rejected checkpoint's number to its archive root, so the latest can be read in reverse order */
|
|
192
|
+
#rejectedCheckpointsByNumber: AztecAsyncMap<number, string>;
|
|
136
193
|
|
|
137
194
|
#log = createLogger('archiver:block_store');
|
|
138
195
|
|
|
@@ -149,7 +206,9 @@ export class BlockStore {
|
|
|
149
206
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
150
207
|
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
151
208
|
this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
|
|
152
|
-
this.#
|
|
209
|
+
this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
|
|
210
|
+
this.#rejectedCheckpoints = db.openMap('archiver_rejected_checkpoints');
|
|
211
|
+
this.#rejectedCheckpointsByNumber = db.openMap('archiver_rejected_checkpoints_by_number');
|
|
153
212
|
}
|
|
154
213
|
|
|
155
214
|
/**
|
|
@@ -185,14 +244,13 @@ export class BlockStore {
|
|
|
185
244
|
|
|
186
245
|
// Extract the latest block and checkpoint numbers
|
|
187
246
|
const previousBlockNumber = await this.getLatestL2BlockNumber();
|
|
188
|
-
const
|
|
189
|
-
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
247
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
190
248
|
|
|
191
249
|
// Verify we're not overwriting checkpointed blocks
|
|
192
250
|
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
193
251
|
if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
|
|
194
252
|
// Check if the proposed block matches the already-checkpointed one
|
|
195
|
-
const existingBlock = await this.
|
|
253
|
+
const existingBlock = await this.getBlockData({ number: BlockNumber(blockNumber) });
|
|
196
254
|
if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
|
|
197
255
|
throw new BlockAlreadyCheckpointedError(blockNumber);
|
|
198
256
|
}
|
|
@@ -204,23 +262,18 @@ export class BlockStore {
|
|
|
204
262
|
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
205
263
|
}
|
|
206
264
|
|
|
207
|
-
//
|
|
208
|
-
//
|
|
265
|
+
// Accept the block if either the confirmed checkpoint or a pending checkpoint matches
|
|
266
|
+
// the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
|
|
209
267
|
const expectedCheckpointNumber = blockCheckpointNumber - 1;
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
const [reported, source]: [CheckpointNumber, 'confirmed' | 'proposed'] =
|
|
216
|
-
proposedCheckpointNumber > previousCheckpointNumber
|
|
217
|
-
? [proposedCheckpointNumber, 'proposed']
|
|
218
|
-
: [previousCheckpointNumber, 'confirmed'];
|
|
219
|
-
throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, reported, source);
|
|
268
|
+
const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
|
|
269
|
+
if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
|
|
270
|
+
const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
271
|
+
const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
|
|
272
|
+
throw new BlockCheckpointNumberNotSequentialError(BlockNumber(blockNumber), blockCheckpointNumber, previous);
|
|
220
273
|
}
|
|
221
274
|
|
|
222
275
|
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
223
|
-
const previousBlockResult = await this.
|
|
276
|
+
const previousBlockResult = await this.getBlockData({ number: previousBlockNumber });
|
|
224
277
|
|
|
225
278
|
let expectedBlockIndex = 0;
|
|
226
279
|
let previousBlockIndex: number | undefined = undefined;
|
|
@@ -233,7 +286,7 @@ export class BlockStore {
|
|
|
233
286
|
if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
|
|
234
287
|
throw new BlockArchiveNotConsistentError(
|
|
235
288
|
blockNumber,
|
|
236
|
-
previousBlockResult.
|
|
289
|
+
previousBlockResult.header.globalVariables.blockNumber,
|
|
237
290
|
blockLastArchive,
|
|
238
291
|
previousBlockResult.archive.root,
|
|
239
292
|
);
|
|
@@ -253,12 +306,18 @@ export class BlockStore {
|
|
|
253
306
|
|
|
254
307
|
/**
|
|
255
308
|
* Append new checkpoints to the store's list.
|
|
309
|
+
* Checkpoints at the start of the batch that are already stored (e.g. re-included by an L1 reorg)
|
|
310
|
+
* are accepted if their archive root matches: their L1 metadata is updated but they are not
|
|
311
|
+
* re-inserted, and they are excluded from the returned array.
|
|
256
312
|
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
257
|
-
* @returns
|
|
313
|
+
* @returns The checkpoints that were actually inserted (excluding already-stored ones).
|
|
258
314
|
*/
|
|
259
|
-
async addCheckpoints(
|
|
315
|
+
async addCheckpoints(
|
|
316
|
+
checkpoints: PublishedCheckpoint[],
|
|
317
|
+
opts: { force?: boolean } = {},
|
|
318
|
+
): Promise<PublishedCheckpoint[]> {
|
|
260
319
|
if (checkpoints.length === 0) {
|
|
261
|
-
return
|
|
320
|
+
return [];
|
|
262
321
|
}
|
|
263
322
|
|
|
264
323
|
return await this.db.transactionAsync(async () => {
|
|
@@ -271,7 +330,7 @@ export class BlockStore {
|
|
|
271
330
|
if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
|
|
272
331
|
checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
|
|
273
332
|
if (checkpoints.length === 0) {
|
|
274
|
-
return
|
|
333
|
+
return [];
|
|
275
334
|
}
|
|
276
335
|
// Re-check sequentiality after skipping
|
|
277
336
|
const newFirstNumber = checkpoints[0].checkpoint.number;
|
|
@@ -319,18 +378,22 @@ export class BlockStore {
|
|
|
319
378
|
checkpointNumber: checkpoint.checkpoint.number,
|
|
320
379
|
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
321
380
|
blockCount: checkpoint.checkpoint.blocks.length,
|
|
381
|
+
feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString(),
|
|
322
382
|
});
|
|
323
383
|
|
|
324
384
|
// Update slot-to-checkpoint index
|
|
325
385
|
await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
|
|
326
|
-
}
|
|
327
386
|
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
await this.clearProposedCheckpointIfSuperseded(lastConfirmedCheckpointNumber);
|
|
387
|
+
// Remove proposed checkpoint if it exists, since L1 is authoritative
|
|
388
|
+
await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
|
|
331
389
|
|
|
332
|
-
|
|
333
|
-
|
|
390
|
+
// Drop any rejected entry for this archive root: a checkpoint that was previously rejected
|
|
391
|
+
// (e.g. invalid attestations) is now being ingested as valid, so its descendants are allowed.
|
|
392
|
+
await this.removeRejectedCheckpointByArchiveRoot(checkpoint.checkpoint.archive.root);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
await this.advanceSynchedL1BlockNumber(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
396
|
+
return checkpoints;
|
|
334
397
|
});
|
|
335
398
|
}
|
|
336
399
|
|
|
@@ -371,9 +434,10 @@ export class BlockStore {
|
|
|
371
434
|
checkpointNumber: incoming.checkpoint.number,
|
|
372
435
|
startBlock: incoming.checkpoint.blocks[0].number,
|
|
373
436
|
blockCount: incoming.checkpoint.blocks.length,
|
|
437
|
+
feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString(),
|
|
374
438
|
});
|
|
375
439
|
// Update the sync point to reflect the new L1 block
|
|
376
|
-
await this
|
|
440
|
+
await this.advanceSynchedL1BlockNumber(incoming.l1.blockNumber);
|
|
377
441
|
}
|
|
378
442
|
return checkpoints.slice(i);
|
|
379
443
|
}
|
|
@@ -388,24 +452,27 @@ export class BlockStore {
|
|
|
388
452
|
return undefined;
|
|
389
453
|
}
|
|
390
454
|
|
|
391
|
-
|
|
392
|
-
|
|
455
|
+
// Check across both proposed and mined checkpoints
|
|
456
|
+
const predecessor =
|
|
457
|
+
(await this.getProposedCheckpointByNumber(previousCheckpointNumber)) ??
|
|
458
|
+
(await this.getCheckpointData(previousCheckpointNumber));
|
|
459
|
+
|
|
460
|
+
if (!predecessor) {
|
|
393
461
|
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
394
462
|
}
|
|
395
463
|
|
|
396
|
-
const previousBlockNumber = BlockNumber(
|
|
397
|
-
const previousBlock = await this.getBlock(previousBlockNumber);
|
|
464
|
+
const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
|
|
465
|
+
const previousBlock = await this.getBlock({ number: previousBlockNumber });
|
|
398
466
|
if (previousBlock === undefined) {
|
|
399
467
|
throw new BlockNotFoundError(previousBlockNumber);
|
|
400
468
|
}
|
|
401
|
-
|
|
402
469
|
return previousBlock;
|
|
403
470
|
}
|
|
404
471
|
|
|
405
472
|
/**
|
|
406
473
|
* Validates that blocks are sequential, have correct indexes, and chain via archive roots.
|
|
407
474
|
* This is the same validation used for both confirmed checkpoints (addCheckpoints) and
|
|
408
|
-
* proposed checkpoints (
|
|
475
|
+
* proposed checkpoints (addProposedCheckpoint).
|
|
409
476
|
*/
|
|
410
477
|
private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
|
|
411
478
|
for (const block of blocks) {
|
|
@@ -457,6 +524,7 @@ export class BlockStore {
|
|
|
457
524
|
l2BlockNumber: block.number,
|
|
458
525
|
l2BlockHash: blockHash,
|
|
459
526
|
txIndexInBlock: i,
|
|
527
|
+
slotNumber: block.header.globalVariables.slotNumber,
|
|
460
528
|
};
|
|
461
529
|
await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
|
|
462
530
|
}
|
|
@@ -532,11 +600,8 @@ export class BlockStore {
|
|
|
532
600
|
this.#log.debug(`Removed checkpoint ${c}`);
|
|
533
601
|
}
|
|
534
602
|
|
|
535
|
-
//
|
|
536
|
-
|
|
537
|
-
if (proposedCheckpointNumber > checkpointNumber) {
|
|
538
|
-
await this.#proposedCheckpoint.delete();
|
|
539
|
-
}
|
|
603
|
+
// Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
|
|
604
|
+
await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
|
|
540
605
|
|
|
541
606
|
return { blocksRemoved };
|
|
542
607
|
});
|
|
@@ -562,6 +627,26 @@ export class BlockStore {
|
|
|
562
627
|
return checkpoints;
|
|
563
628
|
}
|
|
564
629
|
|
|
630
|
+
/**
|
|
631
|
+
* Returns up to `limit` checkpoints anchored at `fromSlot`, ordered nearest-first, walking the slot index.
|
|
632
|
+
* With `reverse`, takes the checkpoints at or before `fromSlot` (descending by slot); otherwise the
|
|
633
|
+
* checkpoints at or after it (ascending). `limit: 1, reverse: true` yields the latest checkpoint at or
|
|
634
|
+
* before the slot in a single range scan.
|
|
635
|
+
*/
|
|
636
|
+
async getCheckpointsBySlot(fromSlot: SlotNumber, limit: number, reverse: boolean): Promise<CheckpointData[]> {
|
|
637
|
+
// The KV range bounds are direction-dependent: forward is [start, end), reverse is (start, end], so a
|
|
638
|
+
// reverse scan uses `end: fromSlot` (inclusive) with no +1 to include the checkpoint at fromSlot itself.
|
|
639
|
+
const range = reverse ? { end: fromSlot, reverse: true, limit } : { start: fromSlot, limit };
|
|
640
|
+
const result: CheckpointData[] = [];
|
|
641
|
+
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync(range)) {
|
|
642
|
+
const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
|
|
643
|
+
if (checkpointStorage) {
|
|
644
|
+
result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
return result;
|
|
648
|
+
}
|
|
649
|
+
|
|
565
650
|
/** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */
|
|
566
651
|
async getCheckpointDataForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointData[]> {
|
|
567
652
|
const result: CheckpointData[] = [];
|
|
@@ -577,6 +662,22 @@ export class BlockStore {
|
|
|
577
662
|
return result;
|
|
578
663
|
}
|
|
579
664
|
|
|
665
|
+
/**
|
|
666
|
+
* Returns the checkpoint numbers for all checkpoints whose slot falls within the given range (inclusive).
|
|
667
|
+
* Lighter than {@link getCheckpointDataForSlotRange} when callers only need to identify which
|
|
668
|
+
* checkpoints fall in the range and will fetch full data for at most a few of them.
|
|
669
|
+
*/
|
|
670
|
+
async getCheckpointNumbersForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointNumber[]> {
|
|
671
|
+
const result: CheckpointNumber[] = [];
|
|
672
|
+
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
|
|
673
|
+
start: startSlot,
|
|
674
|
+
end: endSlot + 1,
|
|
675
|
+
})) {
|
|
676
|
+
result.push(CheckpointNumber(checkpointNumber));
|
|
677
|
+
}
|
|
678
|
+
return result;
|
|
679
|
+
}
|
|
680
|
+
|
|
580
681
|
private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage): CheckpointData {
|
|
581
682
|
return {
|
|
582
683
|
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
@@ -585,6 +686,7 @@ export class BlockStore {
|
|
|
585
686
|
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
586
687
|
startBlock: BlockNumber(checkpointStorage.startBlock),
|
|
587
688
|
blockCount: checkpointStorage.blockCount,
|
|
689
|
+
feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
|
|
588
690
|
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
589
691
|
attestations: checkpointStorage.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
590
692
|
};
|
|
@@ -647,7 +749,7 @@ export class BlockStore {
|
|
|
647
749
|
|
|
648
750
|
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
649
751
|
for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
|
|
650
|
-
const block = await this.getBlock(BlockNumber(bn));
|
|
752
|
+
const block = await this.getBlock({ number: BlockNumber(bn) });
|
|
651
753
|
|
|
652
754
|
if (block === undefined) {
|
|
653
755
|
this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
|
|
@@ -685,44 +787,137 @@ export class BlockStore {
|
|
|
685
787
|
}
|
|
686
788
|
|
|
687
789
|
async hasProposedCheckpoint(): Promise<boolean> {
|
|
688
|
-
const
|
|
689
|
-
return
|
|
790
|
+
const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ limit: 1 }));
|
|
791
|
+
return key !== undefined;
|
|
690
792
|
}
|
|
691
793
|
|
|
692
|
-
/** Deletes
|
|
693
|
-
async
|
|
694
|
-
await this.#
|
|
794
|
+
/** Deletes all pending proposed checkpoints from storage. */
|
|
795
|
+
async deleteProposedCheckpoints(): Promise<void> {
|
|
796
|
+
for await (const key of this.#proposedCheckpoints.keysAsync()) {
|
|
797
|
+
await this.#proposedCheckpoints.delete(key);
|
|
798
|
+
}
|
|
695
799
|
}
|
|
696
800
|
|
|
697
|
-
/**
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
801
|
+
/**
|
|
802
|
+
* Promotes a specific pending checkpoint to a confirmed checkpoint entry.
|
|
803
|
+
* This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
|
|
804
|
+
* Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
|
|
805
|
+
* @param checkpointNumber - The checkpoint number to promote.
|
|
806
|
+
* @param l1 - L1 published data for the checkpoint.
|
|
807
|
+
* @param attestations - Committee attestations.
|
|
808
|
+
* @param expectedArchiveRoot - Archive root guard against races.
|
|
809
|
+
*/
|
|
810
|
+
async promoteProposedToCheckpointed(
|
|
811
|
+
checkpointNumber: CheckpointNumber,
|
|
812
|
+
l1: L1PublishedData,
|
|
813
|
+
attestations: CommitteeAttestation[],
|
|
814
|
+
expectedArchiveRoot: Fr,
|
|
815
|
+
): Promise<void> {
|
|
816
|
+
return await this.db.transactionAsync(async () => {
|
|
817
|
+
const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
|
|
818
|
+
if (!proposed) {
|
|
819
|
+
throw new NoProposedCheckpointToPromoteError();
|
|
820
|
+
}
|
|
821
|
+
if (!proposed.archive.root.equals(expectedArchiveRoot)) {
|
|
822
|
+
throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
// Verify sequentiality: promoted checkpoint must follow the latest confirmed one
|
|
826
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
827
|
+
if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
|
|
828
|
+
throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
// Write the checkpoint entry
|
|
832
|
+
await this.#checkpoints.set(proposed.checkpointNumber, {
|
|
833
|
+
header: proposed.header.toBuffer(),
|
|
834
|
+
archive: proposed.archive.toBuffer(),
|
|
835
|
+
checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
|
|
836
|
+
l1: l1.toBuffer(),
|
|
837
|
+
attestations: attestations.map(attestation => attestation.toBuffer()),
|
|
838
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
839
|
+
startBlock: proposed.startBlock,
|
|
840
|
+
blockCount: proposed.blockCount,
|
|
841
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
|
|
842
|
+
});
|
|
843
|
+
|
|
844
|
+
// Update the slot-to-checkpoint index
|
|
845
|
+
await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
|
|
846
|
+
|
|
847
|
+
// Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
|
|
848
|
+
await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
|
|
849
|
+
|
|
850
|
+
// Drop any rejected entry for this archive root: a checkpoint that was previously rejected
|
|
851
|
+
// (e.g. invalid attestations) is now being promoted as valid, so its descendants are allowed.
|
|
852
|
+
await this.removeRejectedCheckpointByArchiveRoot(proposed.archive.root);
|
|
853
|
+
|
|
854
|
+
// Update the last synced L1 block
|
|
855
|
+
await this.advanceSynchedL1BlockNumber(l1.blockNumber);
|
|
856
|
+
});
|
|
703
857
|
}
|
|
704
858
|
|
|
705
|
-
/**
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
859
|
+
/**
|
|
860
|
+
* Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
|
|
861
|
+
* No fallback to confirmed.
|
|
862
|
+
*/
|
|
863
|
+
async getLastProposedCheckpoint(): Promise<ProposedCheckpointData | undefined> {
|
|
864
|
+
const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
865
|
+
if (key === undefined) {
|
|
709
866
|
return undefined;
|
|
710
867
|
}
|
|
711
|
-
|
|
868
|
+
const stored = await this.#proposedCheckpoints.getAsync(key);
|
|
869
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
870
|
+
}
|
|
871
|
+
|
|
872
|
+
/** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */
|
|
873
|
+
async getProposedCheckpointByNumber(n: CheckpointNumber): Promise<ProposedCheckpointData | undefined> {
|
|
874
|
+
const stored = await this.#proposedCheckpoints.getAsync(n);
|
|
875
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
/**
|
|
879
|
+
* Returns the pending checkpoint whose header slot matches the given slot, or undefined if not found.
|
|
880
|
+
* Iterates `#proposedCheckpoints` rather than reading an index because the map carries 0–1 entries
|
|
881
|
+
* in normal operation (bounded by the proposer pipelining window). Returns the first match.
|
|
882
|
+
*/
|
|
883
|
+
async getProposedCheckpointBySlot(slot: SlotNumber): Promise<ProposedCheckpointData | undefined> {
|
|
884
|
+
for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()) {
|
|
885
|
+
const header = CheckpointHeader.fromBuffer(stored.header);
|
|
886
|
+
if (header.slotNumber === slot) {
|
|
887
|
+
return this.convertToProposedCheckpointData(stored);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
return undefined;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
/**
|
|
894
|
+
* Evicts all pending checkpoints with checkpoint number >= fromNumber.
|
|
895
|
+
* Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
|
|
896
|
+
* all pending >= N must be evicted since they chain off the now-invalid pending N.
|
|
897
|
+
*/
|
|
898
|
+
async evictProposedCheckpointsFrom(fromNumber: CheckpointNumber): Promise<void> {
|
|
899
|
+
const keysToDelete: number[] = [];
|
|
900
|
+
for await (const key of this.#proposedCheckpoints.keysAsync()) {
|
|
901
|
+
if (key >= fromNumber) {
|
|
902
|
+
keysToDelete.push(key);
|
|
903
|
+
}
|
|
904
|
+
}
|
|
905
|
+
for (const key of keysToDelete) {
|
|
906
|
+
await this.#proposedCheckpoints.delete(key);
|
|
907
|
+
}
|
|
712
908
|
}
|
|
713
909
|
|
|
714
910
|
/**
|
|
715
|
-
* Gets the checkpoint at the proposed tip
|
|
716
|
-
* - pending checkpoint if
|
|
911
|
+
* Gets the checkpoint at the proposed tip:
|
|
912
|
+
* - latest pending checkpoint if any exist
|
|
717
913
|
* - fallsback to latest confirmed checkpoint otherwise
|
|
718
|
-
* @returns CommonCheckpointData
|
|
719
914
|
*/
|
|
720
|
-
async
|
|
721
|
-
const
|
|
722
|
-
if (!
|
|
915
|
+
async getLastCheckpoint(): Promise<CommonCheckpointData | undefined> {
|
|
916
|
+
const latest = await this.getLastProposedCheckpoint();
|
|
917
|
+
if (!latest) {
|
|
723
918
|
return this.getCheckpointData(await this.getLatestCheckpointNumber());
|
|
724
919
|
}
|
|
725
|
-
return
|
|
920
|
+
return latest;
|
|
726
921
|
}
|
|
727
922
|
|
|
728
923
|
private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
|
|
@@ -743,7 +938,7 @@ export class BlockStore {
|
|
|
743
938
|
* @returns CheckpointNumber
|
|
744
939
|
*/
|
|
745
940
|
async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
746
|
-
const proposed = await this.
|
|
941
|
+
const proposed = await this.getLastCheckpoint();
|
|
747
942
|
if (!proposed) {
|
|
748
943
|
return await this.getLatestCheckpointNumber();
|
|
749
944
|
}
|
|
@@ -755,165 +950,40 @@ export class BlockStore {
|
|
|
755
950
|
* @returns BlockNumber
|
|
756
951
|
*/
|
|
757
952
|
async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
|
|
758
|
-
const proposed = await this.
|
|
953
|
+
const proposed = await this.getLastCheckpoint();
|
|
759
954
|
if (!proposed) {
|
|
760
955
|
return await this.getCheckpointedL2BlockNumber();
|
|
761
956
|
}
|
|
762
957
|
return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
|
|
763
958
|
}
|
|
764
959
|
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
}
|
|
770
|
-
const checkpoint = await this.#checkpoints.getAsync(blockStorage.checkpointNumber);
|
|
771
|
-
if (!checkpoint) {
|
|
772
|
-
return undefined;
|
|
773
|
-
}
|
|
774
|
-
const block = await this.getBlockFromBlockStorage(number, blockStorage);
|
|
775
|
-
if (!block) {
|
|
776
|
-
return undefined;
|
|
777
|
-
}
|
|
778
|
-
return new CheckpointedL2Block(
|
|
779
|
-
CheckpointNumber(checkpoint.checkpointNumber),
|
|
780
|
-
block,
|
|
781
|
-
L1PublishedData.fromBuffer(checkpoint.l1),
|
|
782
|
-
checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
783
|
-
);
|
|
960
|
+
/** Returns the checkpoint number that contains the given slot (or undefined if not found). */
|
|
961
|
+
async getCheckpointNumberBySlot(slot: SlotNumber): Promise<CheckpointNumber | undefined> {
|
|
962
|
+
const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
|
|
963
|
+
return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
|
|
784
964
|
}
|
|
785
965
|
|
|
786
|
-
/**
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
* @param limit - The number of blocks to return.
|
|
790
|
-
* @returns The requested L2 blocks
|
|
791
|
-
*/
|
|
792
|
-
async *getCheckpointedBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<CheckpointedL2Block> {
|
|
793
|
-
const checkpointCache = new Map<CheckpointNumber, CheckpointStorage>();
|
|
794
|
-
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
795
|
-
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
796
|
-
if (block) {
|
|
797
|
-
const checkpoint =
|
|
798
|
-
checkpointCache.get(CheckpointNumber(blockStorage.checkpointNumber)) ??
|
|
799
|
-
(await this.#checkpoints.getAsync(blockStorage.checkpointNumber));
|
|
800
|
-
if (checkpoint) {
|
|
801
|
-
checkpointCache.set(CheckpointNumber(blockStorage.checkpointNumber), checkpoint);
|
|
802
|
-
const checkpointedBlock = new CheckpointedL2Block(
|
|
803
|
-
CheckpointNumber(checkpoint.checkpointNumber),
|
|
804
|
-
block,
|
|
805
|
-
L1PublishedData.fromBuffer(checkpoint.l1),
|
|
806
|
-
checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
807
|
-
);
|
|
808
|
-
yield checkpointedBlock;
|
|
809
|
-
}
|
|
810
|
-
}
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
async getCheckpointedBlockByHash(blockHash: BlockHash): Promise<CheckpointedL2Block | undefined> {
|
|
815
|
-
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
816
|
-
if (blockNumber === undefined) {
|
|
817
|
-
return undefined;
|
|
818
|
-
}
|
|
819
|
-
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
820
|
-
}
|
|
821
|
-
|
|
822
|
-
async getCheckpointedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
|
|
823
|
-
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
966
|
+
/** Gets a single L2 block matching the given resolved query. */
|
|
967
|
+
async getBlock(query: ResolvedBlockQuery): Promise<L2Block | undefined> {
|
|
968
|
+
const blockNumber = await this.getBlockNumber(query);
|
|
824
969
|
if (blockNumber === undefined) {
|
|
825
970
|
return undefined;
|
|
826
971
|
}
|
|
827
|
-
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
/**
|
|
831
|
-
* Gets up to `limit` amount of L2 blocks starting from `from`.
|
|
832
|
-
* @param start - Number of the first block to return (inclusive).
|
|
833
|
-
* @param limit - The number of blocks to return.
|
|
834
|
-
* @returns The requested L2 blocks
|
|
835
|
-
*/
|
|
836
|
-
async *getBlocks(start: BlockNumber, limit: number): AsyncIterableIterator<L2Block> {
|
|
837
|
-
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
838
|
-
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
839
|
-
if (block) {
|
|
840
|
-
yield block;
|
|
841
|
-
}
|
|
842
|
-
}
|
|
843
|
-
}
|
|
844
|
-
|
|
845
|
-
/**
|
|
846
|
-
* Gets block metadata (without tx data) by block number.
|
|
847
|
-
* @param blockNumber - The number of the block to return.
|
|
848
|
-
* @returns The requested block data.
|
|
849
|
-
*/
|
|
850
|
-
async getBlockData(blockNumber: BlockNumber): Promise<BlockData | undefined> {
|
|
851
972
|
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
852
|
-
if (!blockStorage
|
|
853
|
-
return undefined;
|
|
854
|
-
}
|
|
855
|
-
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
856
|
-
}
|
|
857
|
-
|
|
858
|
-
/**
|
|
859
|
-
* Gets block metadata (without tx data) by archive root.
|
|
860
|
-
* @param archive - The archive root of the block to return.
|
|
861
|
-
* @returns The requested block data.
|
|
862
|
-
*/
|
|
863
|
-
async getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
|
|
864
|
-
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
865
|
-
if (blockNumber === undefined) {
|
|
973
|
+
if (!blockStorage) {
|
|
866
974
|
return undefined;
|
|
867
975
|
}
|
|
868
|
-
return this.getBlockData(BlockNumber(blockNumber));
|
|
869
|
-
}
|
|
870
|
-
|
|
871
|
-
/**
|
|
872
|
-
* Gets an L2 block.
|
|
873
|
-
* @param blockNumber - The number of the block to return.
|
|
874
|
-
* @returns The requested L2 block.
|
|
875
|
-
*/
|
|
876
|
-
async getBlock(blockNumber: BlockNumber): Promise<L2Block | undefined> {
|
|
877
|
-
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
878
|
-
if (!blockStorage || !blockStorage.header) {
|
|
879
|
-
return Promise.resolve(undefined);
|
|
880
|
-
}
|
|
881
976
|
return this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
882
977
|
}
|
|
883
978
|
|
|
884
|
-
/**
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
* @returns The requested L2 block.
|
|
888
|
-
*/
|
|
889
|
-
async getBlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
|
|
890
|
-
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
891
|
-
if (blockNumber === undefined) {
|
|
892
|
-
return undefined;
|
|
893
|
-
}
|
|
894
|
-
return this.getBlock(BlockNumber(blockNumber));
|
|
895
|
-
}
|
|
896
|
-
|
|
897
|
-
/**
|
|
898
|
-
* Gets an L2 block by its archive root.
|
|
899
|
-
* @param archive - The archive root of the block to return.
|
|
900
|
-
* @returns The requested L2 block.
|
|
901
|
-
*/
|
|
902
|
-
async getBlockByArchive(archive: Fr): Promise<L2Block | undefined> {
|
|
903
|
-
const blockNumber = await this.#blockArchiveIndex.getAsync(archive.toString());
|
|
904
|
-
if (blockNumber === undefined) {
|
|
905
|
-
return undefined;
|
|
906
|
-
}
|
|
907
|
-
return this.getBlock(BlockNumber(blockNumber));
|
|
979
|
+
/** Gets a collection of L2 blocks for a resolved range. */
|
|
980
|
+
getBlocks(query: ResolvedBlocksQuery): Promise<L2Block[]> {
|
|
981
|
+
return toArray(this.iterateBlocks(query));
|
|
908
982
|
}
|
|
909
983
|
|
|
910
|
-
/**
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
* @returns The requested block header.
|
|
914
|
-
*/
|
|
915
|
-
async getBlockHeaderByHash(blockHash: BlockHash): Promise<BlockHeader | undefined> {
|
|
916
|
-
const blockNumber = await this.#blockHashIndex.getAsync(blockHash.toString());
|
|
984
|
+
/** Gets single block metadata matching the given resolved query. */
|
|
985
|
+
async getBlockData(query: ResolvedBlockQuery): Promise<BlockData | undefined> {
|
|
986
|
+
const blockNumber = await this.getBlockNumber(query);
|
|
917
987
|
if (blockNumber === undefined) {
|
|
918
988
|
return undefined;
|
|
919
989
|
}
|
|
@@ -921,41 +991,36 @@ export class BlockStore {
|
|
|
921
991
|
if (!blockStorage || !blockStorage.header) {
|
|
922
992
|
return undefined;
|
|
923
993
|
}
|
|
924
|
-
return
|
|
994
|
+
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
925
995
|
}
|
|
926
996
|
|
|
927
|
-
/**
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
997
|
+
/** Gets a collection of block metadata entries for a resolved range. */
|
|
998
|
+
getBlocksData(query: ResolvedBlocksQuery): Promise<BlockData[]> {
|
|
999
|
+
return toArray(this.iterateBlocksData(query));
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/** Async iterator over L2 blocks for a resolved range. */
|
|
1003
|
+
private async *iterateBlocks(query: ResolvedBlocksQuery): AsyncIterableIterator<L2Block> {
|
|
1004
|
+
const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
|
|
1005
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)) {
|
|
1006
|
+
if (cap !== undefined && blockNumber > cap) {
|
|
1007
|
+
break;
|
|
1008
|
+
}
|
|
1009
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
1010
|
+
if (block) {
|
|
1011
|
+
yield block;
|
|
1012
|
+
}
|
|
940
1013
|
}
|
|
941
|
-
return BlockHeader.fromBuffer(blockStorage.header);
|
|
942
1014
|
}
|
|
943
1015
|
|
|
944
|
-
/**
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
async *getBlockHeaders(start: BlockNumber, limit: number): AsyncIterableIterator<BlockHeader> {
|
|
951
|
-
for await (const [blockNumber, blockStorage] of this.getBlockStorages(start, limit)) {
|
|
952
|
-
const header = BlockHeader.fromBuffer(blockStorage.header);
|
|
953
|
-
if (header.getBlockNumber() !== blockNumber) {
|
|
954
|
-
throw new Error(
|
|
955
|
-
`Block number mismatch when retrieving block header from archive (expected ${blockNumber} but got ${header.getBlockNumber()})`,
|
|
956
|
-
);
|
|
1016
|
+
/** Async iterator over block metadata for a resolved range. */
|
|
1017
|
+
private async *iterateBlocksData(query: ResolvedBlocksQuery): AsyncIterableIterator<BlockData> {
|
|
1018
|
+
const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
|
|
1019
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)) {
|
|
1020
|
+
if (cap !== undefined && blockNumber > cap) {
|
|
1021
|
+
break;
|
|
957
1022
|
}
|
|
958
|
-
yield
|
|
1023
|
+
yield this.getBlockDataFromBlockStorage(blockStorage);
|
|
959
1024
|
}
|
|
960
1025
|
}
|
|
961
1026
|
|
|
@@ -972,11 +1037,29 @@ export class BlockStore {
|
|
|
972
1037
|
}
|
|
973
1038
|
}
|
|
974
1039
|
|
|
1040
|
+
/** Resolves a ResolvedBlockQuery discriminant to a block number, or undefined if not found. */
|
|
1041
|
+
async getBlockNumber(query: ResolvedBlockQuery): Promise<BlockNumber | undefined> {
|
|
1042
|
+
let blockNumber: BlockNumber | undefined;
|
|
1043
|
+
if ('number' in query) {
|
|
1044
|
+
blockNumber = query.number;
|
|
1045
|
+
} else if ('hash' in query) {
|
|
1046
|
+
const n = await this.#blockHashIndex.getAsync(query.hash.toString());
|
|
1047
|
+
blockNumber = n !== undefined ? BlockNumber(n) : undefined;
|
|
1048
|
+
} else {
|
|
1049
|
+
const n = await this.#blockArchiveIndex.getAsync(query.archive.toString());
|
|
1050
|
+
blockNumber = n !== undefined ? BlockNumber(n) : undefined;
|
|
1051
|
+
}
|
|
1052
|
+
if (blockNumber === undefined) {
|
|
1053
|
+
return undefined;
|
|
1054
|
+
}
|
|
1055
|
+
return blockNumber;
|
|
1056
|
+
}
|
|
1057
|
+
|
|
975
1058
|
private getBlockDataFromBlockStorage(blockStorage: BlockStorage): BlockData {
|
|
976
1059
|
return {
|
|
977
1060
|
header: BlockHeader.fromBuffer(blockStorage.header),
|
|
978
1061
|
archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
|
|
979
|
-
blockHash:
|
|
1062
|
+
blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
|
|
980
1063
|
checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
|
|
981
1064
|
indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
|
|
982
1065
|
};
|
|
@@ -1033,56 +1116,6 @@ export class BlockStore {
|
|
|
1033
1116
|
return deserializeIndexedTxEffect(buffer);
|
|
1034
1117
|
}
|
|
1035
1118
|
|
|
1036
|
-
/**
|
|
1037
|
-
* Gets a receipt of a settled tx.
|
|
1038
|
-
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
1039
|
-
* @returns The requested tx receipt (or undefined if not found).
|
|
1040
|
-
*/
|
|
1041
|
-
async getSettledTxReceipt(
|
|
1042
|
-
txHash: TxHash,
|
|
1043
|
-
l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
|
|
1044
|
-
): Promise<TxReceipt | undefined> {
|
|
1045
|
-
const txEffect = await this.getTxEffect(txHash);
|
|
1046
|
-
if (!txEffect) {
|
|
1047
|
-
return undefined;
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
1051
|
-
|
|
1052
|
-
// Use existing archiver methods to determine finalization level
|
|
1053
|
-
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
|
|
1054
|
-
this.getProvenBlockNumber(),
|
|
1055
|
-
this.getCheckpointedL2BlockNumber(),
|
|
1056
|
-
this.getFinalizedL2BlockNumber(),
|
|
1057
|
-
this.getBlockData(blockNumber),
|
|
1058
|
-
]);
|
|
1059
|
-
|
|
1060
|
-
let status: TxStatus;
|
|
1061
|
-
if (blockNumber <= finalizedBlockNumber) {
|
|
1062
|
-
status = TxStatus.FINALIZED;
|
|
1063
|
-
} else if (blockNumber <= provenBlockNumber) {
|
|
1064
|
-
status = TxStatus.PROVEN;
|
|
1065
|
-
} else if (blockNumber <= checkpointedBlockNumber) {
|
|
1066
|
-
status = TxStatus.CHECKPOINTED;
|
|
1067
|
-
} else {
|
|
1068
|
-
status = TxStatus.PROPOSED;
|
|
1069
|
-
}
|
|
1070
|
-
|
|
1071
|
-
const epochNumber =
|
|
1072
|
-
blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
|
|
1073
|
-
|
|
1074
|
-
return new TxReceipt(
|
|
1075
|
-
txHash,
|
|
1076
|
-
status,
|
|
1077
|
-
TxReceipt.executionResultFromRevertCode(txEffect.data.revertCode),
|
|
1078
|
-
undefined,
|
|
1079
|
-
txEffect.data.transactionFee.toBigInt(),
|
|
1080
|
-
txEffect.l2BlockHash,
|
|
1081
|
-
blockNumber,
|
|
1082
|
-
epochNumber,
|
|
1083
|
-
);
|
|
1084
|
-
}
|
|
1085
|
-
|
|
1086
1119
|
/**
|
|
1087
1120
|
* Looks up which block included the requested tx effect.
|
|
1088
1121
|
* @param txHash - The txHash of the tx.
|
|
@@ -1093,10 +1126,45 @@ export class BlockStore {
|
|
|
1093
1126
|
if (!txEffect) {
|
|
1094
1127
|
return undefined;
|
|
1095
1128
|
}
|
|
1096
|
-
|
|
1129
|
+
// Read only the IndexedTxEffect header (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4)`); the
|
|
1130
|
+
// large tail (the full TxEffect with logs etc.) is irrelevant here.
|
|
1131
|
+
const view = Buffer.from(txEffect.buffer, txEffect.byteOffset, txEffect.byteLength);
|
|
1132
|
+
const l2BlockNumber = view.readUInt32BE(32);
|
|
1133
|
+
const txIndexInBlock = view.readUInt32BE(36);
|
|
1097
1134
|
return [l2BlockNumber, txIndexInBlock];
|
|
1098
1135
|
}
|
|
1099
1136
|
|
|
1137
|
+
/**
|
|
1138
|
+
* Batched, partial deserializer that fetches `noteHashes` and `nullifiers` (all of them) for the given
|
|
1139
|
+
* txs. For each input txHash, returns a `[noteHashes, nullifiers]` tuple. Returns `[[], []]` for any
|
|
1140
|
+
* unknown txHash. Preserves input order. Used by the log read path when `includeEffects` is set to
|
|
1141
|
+
* attach effect data on demand without paying for a full {@link TxEffect} deserialization.
|
|
1142
|
+
*
|
|
1143
|
+
* The on-disk `IndexedTxEffect` layout starts with a fixed-length header
|
|
1144
|
+
* (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4) + slotNumber(4) + revertCode(1) + txHash(32) +
|
|
1145
|
+
* transactionFee(32)` = 109 bytes), followed by `noteHashes` and `nullifiers` (both u8-length-prefixed `Fr`
|
|
1146
|
+
* vectors). We
|
|
1147
|
+
* skip the header, then read the two vectors, and stop — the large tail (`l2ToL1Msgs`,
|
|
1148
|
+
* `publicDataWrites`, `privateLogs`, `publicLogs`, `contractClassLogs`) is never touched.
|
|
1149
|
+
*/
|
|
1150
|
+
public getNoteHashesAndNullifiers(txHashes: TxHash[]): Promise<[Fr[], Fr[]][]> {
|
|
1151
|
+
return Promise.all(
|
|
1152
|
+
txHashes.map(async (txHash): Promise<[Fr[], Fr[]]> => {
|
|
1153
|
+
const buffer = await this.#txEffects.getAsync(txHash.toString());
|
|
1154
|
+
if (!buffer) {
|
|
1155
|
+
return [[], []];
|
|
1156
|
+
}
|
|
1157
|
+
const reader = BufferReader.asReader(buffer);
|
|
1158
|
+
// Skip the fixed-length header: blockHash + l2BlockNumber + txIndexInBlock + slotNumber + revertCode +
|
|
1159
|
+
// txHash + transactionFee.
|
|
1160
|
+
reader.readBytes(32 + 4 + 4 + 4 + 1 + 32 + 32);
|
|
1161
|
+
const noteHashes = reader.readVectorUint8Prefix(Fr);
|
|
1162
|
+
const nullifiers = reader.readVectorUint8Prefix(Fr);
|
|
1163
|
+
return [noteHashes, nullifiers];
|
|
1164
|
+
}),
|
|
1165
|
+
);
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1100
1168
|
/**
|
|
1101
1169
|
* Looks up which block deployed a particular contract.
|
|
1102
1170
|
* @param contractAddress - The address of the contract to look up.
|
|
@@ -1124,6 +1192,159 @@ export class BlockStore {
|
|
|
1124
1192
|
return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
1125
1193
|
}
|
|
1126
1194
|
|
|
1195
|
+
/**
|
|
1196
|
+
* Resolves all four L2 chain tips (proposed, checkpointed, proven, finalized) in a single
|
|
1197
|
+
* read-only transaction so the snapshot is internally consistent. Each underlying record is
|
|
1198
|
+
* read at most once: latest block and latest confirmed checkpoint are loaded directly (no
|
|
1199
|
+
* separate "find the number, then look up data" hop), the proven/finalized checkpoint
|
|
1200
|
+
* singletons are read once and their storage entries are reused if they coincide with the
|
|
1201
|
+
* latest checkpoint, and per-tip block hashes are deduped when two tips land on the same block
|
|
1202
|
+
* (e.g. finalized == proven).
|
|
1203
|
+
*
|
|
1204
|
+
* The result is guaranteed to satisfy `finalized <= proven <= checkpointed <= proposed` (by
|
|
1205
|
+
* block number). Genesis is represented by `(INITIAL_L2_BLOCK_NUM - 1)` and the supplied
|
|
1206
|
+
* `genesisBlockHash`, paired with the synthetic genesis checkpoint id.
|
|
1207
|
+
*
|
|
1208
|
+
* @param genesisBlockHash - Block hash to report for the synthetic pre-initial block (used when
|
|
1209
|
+
* a tip is still at genesis).
|
|
1210
|
+
*/
|
|
1211
|
+
async getL2TipsData(genesisBlockHash: BlockHash): Promise<L2Tips> {
|
|
1212
|
+
return await this.db.transactionAsync(async () => {
|
|
1213
|
+
// Define genesis tips
|
|
1214
|
+
const genesisBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
1215
|
+
const genesisCheckpointNumber = CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
|
|
1216
|
+
const genesisBlockId = { number: genesisBlockNumber, hash: genesisBlockHash.toString() };
|
|
1217
|
+
const genesisCheckpointId = {
|
|
1218
|
+
number: genesisCheckpointNumber,
|
|
1219
|
+
hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
|
|
1220
|
+
};
|
|
1221
|
+
const genesisTip: L2TipId = { block: genesisBlockId, checkpoint: genesisCheckpointId };
|
|
1222
|
+
|
|
1223
|
+
// Load latest block and checkpoint entries
|
|
1224
|
+
const [latestBlockEntry] = await toArray(this.#blocks.entriesAsync({ reverse: true, limit: 1 }));
|
|
1225
|
+
const [latestCheckpointEntry] = await toArray(this.#checkpoints.entriesAsync({ reverse: true, limit: 1 }));
|
|
1226
|
+
const latestCheckpointNumber = latestCheckpointEntry
|
|
1227
|
+
? CheckpointNumber(latestCheckpointEntry[0])
|
|
1228
|
+
: genesisCheckpointNumber;
|
|
1229
|
+
|
|
1230
|
+
// Load proven and finalized checkpoint number pointers
|
|
1231
|
+
const [provenRaw, finalizedRaw] = await Promise.all([
|
|
1232
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
1233
|
+
this.#lastFinalizedCheckpoint.getAsync(),
|
|
1234
|
+
]);
|
|
1235
|
+
|
|
1236
|
+
// Clamp to enforce finalized <= proven <= checkpointed.
|
|
1237
|
+
const provenCheckpointNumber = CheckpointNumber(Math.min(provenRaw ?? 0, latestCheckpointNumber));
|
|
1238
|
+
const finalizedCheckpointNumber = CheckpointNumber(Math.min(finalizedRaw ?? 0, provenCheckpointNumber));
|
|
1239
|
+
|
|
1240
|
+
// Avoid loading the same checkpoint more than once
|
|
1241
|
+
const checkpointStorageCache = new Map<CheckpointNumber, CheckpointStorage>();
|
|
1242
|
+
if (latestCheckpointEntry) {
|
|
1243
|
+
checkpointStorageCache.set(CheckpointNumber(latestCheckpointEntry[0]), latestCheckpointEntry[1]);
|
|
1244
|
+
}
|
|
1245
|
+
const loadCheckpointStorage = async (n: CheckpointNumber): Promise<CheckpointStorage | undefined> => {
|
|
1246
|
+
if (n === 0) {
|
|
1247
|
+
return undefined;
|
|
1248
|
+
}
|
|
1249
|
+
if (!checkpointStorageCache.has(n)) {
|
|
1250
|
+
const checkpointStorage = await this.#checkpoints.getAsync(n);
|
|
1251
|
+
if (!checkpointStorage) {
|
|
1252
|
+
throw new CheckpointNotFoundError(n);
|
|
1253
|
+
}
|
|
1254
|
+
checkpointStorageCache.set(n, checkpointStorage);
|
|
1255
|
+
}
|
|
1256
|
+
return checkpointStorageCache.get(n)!;
|
|
1257
|
+
};
|
|
1258
|
+
|
|
1259
|
+
// Load proven and finalized checkpoint storage entries
|
|
1260
|
+
const provenCheckpoint = await loadCheckpointStorage(provenCheckpointNumber);
|
|
1261
|
+
const finalizedCheckpoint = await loadCheckpointStorage(finalizedCheckpointNumber);
|
|
1262
|
+
|
|
1263
|
+
// Avoid loading the same block hash multiple times when tips land on the same block
|
|
1264
|
+
const blockHashCache = new Map<number, string>();
|
|
1265
|
+
blockHashCache.set(genesisBlockNumber, genesisBlockHash.toString());
|
|
1266
|
+
if (latestBlockEntry) {
|
|
1267
|
+
blockHashCache.set(latestBlockEntry[0], BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString());
|
|
1268
|
+
}
|
|
1269
|
+
const loadBlockHash = async (n: BlockNumber): Promise<string> => {
|
|
1270
|
+
if (!blockHashCache.has(n)) {
|
|
1271
|
+
const blockStorage = await this.#blocks.getAsync(n);
|
|
1272
|
+
if (!blockStorage) {
|
|
1273
|
+
throw new BlockNotFoundError(n);
|
|
1274
|
+
}
|
|
1275
|
+
const blockHash = BlockHash.fromBuffer(blockStorage.blockHash).toString();
|
|
1276
|
+
blockHashCache.set(n, blockHash);
|
|
1277
|
+
}
|
|
1278
|
+
return blockHashCache.get(n)!;
|
|
1279
|
+
};
|
|
1280
|
+
|
|
1281
|
+
// Build proposed chain tip (this one has block only, no checkpoint)
|
|
1282
|
+
const proposedBlockId =
|
|
1283
|
+
latestBlockEntry === undefined
|
|
1284
|
+
? genesisBlockId
|
|
1285
|
+
: {
|
|
1286
|
+
number: BlockNumber(latestBlockEntry[0]),
|
|
1287
|
+
hash: BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString(),
|
|
1288
|
+
};
|
|
1289
|
+
|
|
1290
|
+
// Build other tips from checkpoint data, reading corresponding block data from the cache
|
|
1291
|
+
const buildTipFromCheckpoint = async (
|
|
1292
|
+
stored: ProposedCheckpointStorage | CheckpointStorage | undefined,
|
|
1293
|
+
): Promise<L2TipId> => {
|
|
1294
|
+
if (!stored) {
|
|
1295
|
+
return genesisTip;
|
|
1296
|
+
}
|
|
1297
|
+
const blockNumber = BlockNumber(stored.startBlock + stored.blockCount - 1);
|
|
1298
|
+
const blockHash = await loadBlockHash(blockNumber);
|
|
1299
|
+
const header = CheckpointHeader.fromBuffer(stored.header);
|
|
1300
|
+
return {
|
|
1301
|
+
block: { number: blockNumber, hash: blockHash },
|
|
1302
|
+
checkpoint: { number: CheckpointNumber(stored.checkpointNumber), hash: header.hash().toString() },
|
|
1303
|
+
};
|
|
1304
|
+
};
|
|
1305
|
+
|
|
1306
|
+
const checkpointedTip = await buildTipFromCheckpoint(latestCheckpointEntry?.[1]);
|
|
1307
|
+
const provenTip = await buildTipFromCheckpoint(provenCheckpoint);
|
|
1308
|
+
const finalizedTip = await buildTipFromCheckpoint(finalizedCheckpoint);
|
|
1309
|
+
|
|
1310
|
+
// A checkpointed block past the latest stored block would mean a checkpoint
|
|
1311
|
+
// references blocks that aren't in blocks.
|
|
1312
|
+
if (proposedBlockId.number < checkpointedTip.block.number) {
|
|
1313
|
+
throw new Error(
|
|
1314
|
+
`Inconsistent block store: latest block ${proposedBlockId.number} is behind checkpointed block ${checkpointedTip.block.number}`,
|
|
1315
|
+
);
|
|
1316
|
+
}
|
|
1317
|
+
|
|
1318
|
+
// Assert that checkpoint numbers are increasing
|
|
1319
|
+
if (
|
|
1320
|
+
finalizedTip.checkpoint.number > provenTip.checkpoint.number ||
|
|
1321
|
+
provenTip.checkpoint.number > checkpointedTip.checkpoint.number
|
|
1322
|
+
) {
|
|
1323
|
+
throw new Error(
|
|
1324
|
+
`Inconsistent checkpoint numbers in chain tips: finalized=${finalizedTip.checkpoint.number} proven=${provenTip.checkpoint.number} checkpointed=${checkpointedTip.checkpoint.number}`,
|
|
1325
|
+
);
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
// Assert block numbers are increasing
|
|
1329
|
+
if (
|
|
1330
|
+
finalizedTip.block.number > provenTip.block.number ||
|
|
1331
|
+
provenTip.block.number > checkpointedTip.block.number ||
|
|
1332
|
+
checkpointedTip.block.number > proposedBlockId.number
|
|
1333
|
+
) {
|
|
1334
|
+
throw new Error(
|
|
1335
|
+
`Inconsistent block numbers in chain tips: finalized=${finalizedTip.block.number} proven=${provenTip.block.number} checkpointed=${checkpointedTip.block.number} proposed=${proposedBlockId.number}`,
|
|
1336
|
+
);
|
|
1337
|
+
}
|
|
1338
|
+
|
|
1339
|
+
return {
|
|
1340
|
+
proposed: proposedBlockId,
|
|
1341
|
+
checkpointed: checkpointedTip,
|
|
1342
|
+
proven: provenTip,
|
|
1343
|
+
finalized: finalizedTip,
|
|
1344
|
+
};
|
|
1345
|
+
});
|
|
1346
|
+
}
|
|
1347
|
+
|
|
1127
1348
|
/**
|
|
1128
1349
|
* Gets the most recent L1 block processed.
|
|
1129
1350
|
* @returns The L1 block that published the latest L2 block
|
|
@@ -1136,24 +1357,29 @@ export class BlockStore {
|
|
|
1136
1357
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
1137
1358
|
}
|
|
1138
1359
|
|
|
1139
|
-
/**
|
|
1140
|
-
*
|
|
1141
|
-
|
|
1360
|
+
/**
|
|
1361
|
+
* Adds a proposed checkpoint to the pending queue.
|
|
1362
|
+
* Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
|
|
1363
|
+
* confirmed and the highest pending checkpoint number.
|
|
1364
|
+
* Computes archive and checkpointOutHash from the stored blocks.
|
|
1365
|
+
*/
|
|
1366
|
+
async addProposedCheckpoint(proposed: ProposedCheckpointInput) {
|
|
1142
1367
|
return await this.db.transactionAsync(async () => {
|
|
1143
|
-
const current = await this.getProposedCheckpointNumber();
|
|
1144
|
-
if (proposed.checkpointNumber <= current) {
|
|
1145
|
-
throw new ProposedCheckpointStaleError(proposed.checkpointNumber, current);
|
|
1146
|
-
}
|
|
1147
1368
|
const confirmed = await this.getLatestCheckpointNumber();
|
|
1148
|
-
|
|
1149
|
-
|
|
1369
|
+
const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
1370
|
+
const latestTip = CheckpointNumber(
|
|
1371
|
+
latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed,
|
|
1372
|
+
);
|
|
1373
|
+
|
|
1374
|
+
if (proposed.checkpointNumber !== latestTip + 1) {
|
|
1375
|
+
throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
|
|
1150
1376
|
}
|
|
1151
1377
|
|
|
1152
|
-
// Ensure the
|
|
1378
|
+
// Ensure the predecessor block (from pending or confirmed chain) exists
|
|
1153
1379
|
const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
|
|
1154
1380
|
const blocks: L2Block[] = [];
|
|
1155
1381
|
for (let i = 0; i < proposed.blockCount; i++) {
|
|
1156
|
-
const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
|
|
1382
|
+
const block = await this.getBlock({ number: BlockNumber(proposed.startBlock + i) });
|
|
1157
1383
|
if (!block) {
|
|
1158
1384
|
throw new BlockNotFoundError(proposed.startBlock + i);
|
|
1159
1385
|
}
|
|
@@ -1164,7 +1390,7 @@ export class BlockStore {
|
|
|
1164
1390
|
const archive = blocks[blocks.length - 1].archive;
|
|
1165
1391
|
const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
|
|
1166
1392
|
|
|
1167
|
-
await this.#
|
|
1393
|
+
await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
|
|
1168
1394
|
header: proposed.header.toBuffer(),
|
|
1169
1395
|
archive: archive.toBuffer(),
|
|
1170
1396
|
checkpointOutHash: checkpointOutHash.toBuffer(),
|
|
@@ -1178,13 +1404,15 @@ export class BlockStore {
|
|
|
1178
1404
|
}
|
|
1179
1405
|
|
|
1180
1406
|
async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1407
|
+
return await this.db.transactionAsync(async () => {
|
|
1408
|
+
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
1409
|
+
this.getLatestCheckpointNumber(),
|
|
1410
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
1411
|
+
]);
|
|
1412
|
+
return (provenCheckpointNumber ?? 0) > latestCheckpointNumber
|
|
1413
|
+
? latestCheckpointNumber
|
|
1414
|
+
: CheckpointNumber(provenCheckpointNumber ?? 0);
|
|
1415
|
+
});
|
|
1188
1416
|
}
|
|
1189
1417
|
|
|
1190
1418
|
async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
@@ -1193,13 +1421,15 @@ export class BlockStore {
|
|
|
1193
1421
|
}
|
|
1194
1422
|
|
|
1195
1423
|
async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1424
|
+
return await this.db.transactionAsync(async () => {
|
|
1425
|
+
const [provenCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
|
|
1426
|
+
this.getProvenCheckpointNumber(),
|
|
1427
|
+
this.#lastFinalizedCheckpoint.getAsync(),
|
|
1428
|
+
]);
|
|
1429
|
+
return (finalizedCheckpointNumber ?? 0) > provenCheckpointNumber
|
|
1430
|
+
? provenCheckpointNumber
|
|
1431
|
+
: CheckpointNumber(finalizedCheckpointNumber ?? 0);
|
|
1432
|
+
});
|
|
1203
1433
|
}
|
|
1204
1434
|
|
|
1205
1435
|
setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
@@ -1242,4 +1472,80 @@ export class BlockStore {
|
|
|
1242
1472
|
await this.#pendingChainValidationStatus.delete();
|
|
1243
1473
|
}
|
|
1244
1474
|
}
|
|
1475
|
+
|
|
1476
|
+
/** Records a rejected-checkpoint entry, keyed by its own archive root. */
|
|
1477
|
+
async addRejectedCheckpoint(entry: RejectedCheckpoint): Promise<void> {
|
|
1478
|
+
const archiveRootHex = entry.archiveRoot.toString();
|
|
1479
|
+
await this.#rejectedCheckpoints.set(archiveRootHex, {
|
|
1480
|
+
checkpointNumber: entry.checkpointNumber,
|
|
1481
|
+
archiveRoot: entry.archiveRoot.toBuffer(),
|
|
1482
|
+
parentArchiveRoot: entry.parentArchiveRoot.toBuffer(),
|
|
1483
|
+
slotNumber: entry.slotNumber,
|
|
1484
|
+
l1: entry.l1.toBuffer(),
|
|
1485
|
+
reason: entry.reason,
|
|
1486
|
+
});
|
|
1487
|
+
await this.#rejectedCheckpointsByNumber.set(entry.checkpointNumber, archiveRootHex);
|
|
1488
|
+
await this.advanceSynchedL1BlockNumber(entry.l1.blockNumber);
|
|
1489
|
+
}
|
|
1490
|
+
|
|
1491
|
+
/** Returns the rejected-checkpoint entry with the given archive root, or undefined if not present. */
|
|
1492
|
+
async getRejectedCheckpointByArchiveRoot(archiveRoot: Fr): Promise<RejectedCheckpoint | undefined> {
|
|
1493
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRoot.toString());
|
|
1494
|
+
return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
/** Returns the rejected-checkpoint entry recorded for the given checkpoint number, or undefined if none. */
|
|
1498
|
+
async getRejectedCheckpointByNumber(checkpointNumber: CheckpointNumber): Promise<RejectedCheckpoint | undefined> {
|
|
1499
|
+
const archiveRootHex = await this.#rejectedCheckpointsByNumber.getAsync(checkpointNumber);
|
|
1500
|
+
if (archiveRootHex === undefined) {
|
|
1501
|
+
return undefined;
|
|
1502
|
+
}
|
|
1503
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
|
|
1504
|
+
return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
|
|
1505
|
+
}
|
|
1506
|
+
|
|
1507
|
+
/** Returns the highest checkpoint number recorded across all rejected entries, or `INITIAL_CHECKPOINT_NUMBER - 1` if none. */
|
|
1508
|
+
async getLatestRejectedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1509
|
+
const [latest] = await toArray(this.#rejectedCheckpointsByNumber.keysAsync({ reverse: true, limit: 1 }));
|
|
1510
|
+
return CheckpointNumber(latest ?? INITIAL_CHECKPOINT_NUMBER - 1);
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
/** Removes a rejected-checkpoint entry by its archive root (used when an entry no longer matches L1). */
|
|
1514
|
+
async removeRejectedCheckpointByArchiveRoot(archiveRoot: Fr): Promise<void> {
|
|
1515
|
+
const archiveRootHex = archiveRoot.toString();
|
|
1516
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
|
|
1517
|
+
await this.#rejectedCheckpoints.delete(archiveRootHex);
|
|
1518
|
+
if (stored) {
|
|
1519
|
+
// Only clear the by-number index if it still points at this archive root, so a distinct
|
|
1520
|
+
// entry that shares the checkpoint number (e.g. an L1 reorg replacement) is not dropped.
|
|
1521
|
+
const indexed = await this.#rejectedCheckpointsByNumber.getAsync(stored.checkpointNumber);
|
|
1522
|
+
if (indexed === archiveRootHex) {
|
|
1523
|
+
await this.#rejectedCheckpointsByNumber.delete(stored.checkpointNumber);
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
/**
|
|
1529
|
+
* Advances the stored last-synched L1 block number to `l1BlockNumber` only if it is strictly
|
|
1530
|
+
* greater than the current value. Use this whenever ingesting checkpoint-shaped data so the
|
|
1531
|
+
* sync pointer never walks backwards on out-of-order writes (e.g. an invalid checkpoint
|
|
1532
|
+
* advance followed by a valid-checkpoint commit landing at an earlier L1 block).
|
|
1533
|
+
*/
|
|
1534
|
+
private async advanceSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
|
|
1535
|
+
const current = await this.#lastSynchedL1Block.getAsync();
|
|
1536
|
+
if (current === undefined || l1BlockNumber > current) {
|
|
1537
|
+
await this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1540
|
+
|
|
1541
|
+
private rejectedCheckpointFromStorage(stored: RejectedCheckpointStorage): RejectedCheckpoint {
|
|
1542
|
+
return {
|
|
1543
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
1544
|
+
archiveRoot: Fr.fromBuffer(stored.archiveRoot),
|
|
1545
|
+
parentArchiveRoot: Fr.fromBuffer(stored.parentArchiveRoot),
|
|
1546
|
+
slotNumber: SlotNumber(stored.slotNumber),
|
|
1547
|
+
l1: L1PublishedData.fromBuffer(stored.l1),
|
|
1548
|
+
reason: stored.reason,
|
|
1549
|
+
};
|
|
1550
|
+
}
|
|
1245
1551
|
}
|