@aztec/archiver 0.0.1-commit.96dac018d → 0.0.1-commit.993d240
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 +19 -11
- package/dest/archiver.d.ts +36 -17
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +257 -75
- package/dest/config.d.ts +6 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +23 -15
- package/dest/errors.d.ts +55 -9
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +81 -14
- package/dest/factory.d.ts +13 -9
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +47 -35
- 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 +2 -1
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +15 -5
- package/dest/l1/data_retrieval.d.ts +24 -12
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +36 -37
- 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 +40 -0
- package/dest/modules/data_source_base.d.ts +70 -46
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +270 -135
- package/dest/modules/data_store_updater.d.ts +42 -17
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +191 -122
- package/dest/modules/instrumentation.d.ts +7 -2
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +25 -7
- package/dest/modules/l1_synchronizer.d.ts +12 -6
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +432 -205
- package/dest/modules/validation.d.ts +4 -3
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +6 -6
- package/dest/store/block_store.d.ts +174 -70
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +696 -250
- package/dest/store/contract_class_store.d.ts +17 -4
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +24 -68
- 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 +37 -2
- 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 -7
- package/dest/store/l2_tips_cache.d.ts.map +1 -1
- package/dest/store/l2_tips_cache.js +13 -76
- package/dest/store/log_store.d.ts +42 -37
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +262 -408
- package/dest/store/log_store_codec.d.ts +70 -0
- package/dest/store/log_store_codec.d.ts.map +1 -0
- package/dest/store/log_store_codec.js +101 -0
- package/dest/store/message_store.d.ts +11 -1
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +51 -9
- package/dest/test/fake_l1_state.d.ts +20 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +114 -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 +2 -1
- package/dest/test/mock_l2_block_source.d.ts +52 -46
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +246 -170
- package/dest/test/mock_structs.d.ts +4 -1
- package/dest/test/mock_structs.d.ts.map +1 -1
- package/dest/test/mock_structs.js +13 -1
- package/dest/test/noop_l1_archiver.d.ts +12 -6
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +26 -9
- package/package.json +14 -14
- package/src/archiver.ts +313 -75
- package/src/config.ts +32 -12
- package/src/errors.ts +122 -21
- package/src/factory.ts +54 -29
- package/src/index.ts +18 -2
- package/src/l1/calldata_retriever.ts +16 -5
- package/src/l1/data_retrieval.ts +52 -53
- package/src/l1/validate_historical_logs.ts +140 -0
- package/src/modules/contract_data_source_adapter.ts +55 -0
- package/src/modules/data_source_base.ts +336 -171
- package/src/modules/data_store_updater.ts +224 -154
- package/src/modules/instrumentation.ts +28 -8
- package/src/modules/l1_synchronizer.ts +572 -248
- package/src/modules/validation.ts +10 -9
- package/src/store/block_store.ts +865 -290
- package/src/store/contract_class_store.ts +31 -103
- package/src/store/contract_instance_store.ts +51 -5
- 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 -70
- package/src/store/log_store.ts +301 -559
- package/src/store/log_store_codec.ts +132 -0
- package/src/store/message_store.ts +60 -10
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +142 -29
- package/src/test/mock_l1_to_l2_message_source.ts +1 -0
- package/src/test/mock_l2_block_source.ts +309 -205
- package/src/test/mock_structs.ts +20 -6
- package/src/test/noop_l1_archiver.ts +39 -9
- package/dest/store/kv_archiver_store.d.ts +0 -354
- package/dest/store/kv_archiver_store.d.ts.map +0 -1
- package/dest/store/kv_archiver_store.js +0 -464
- package/src/store/kv_archiver_store.ts +0 -671
package/src/store/block_store.ts
CHANGED
|
@@ -12,15 +12,25 @@ 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,
|
|
21
23
|
} from '@aztec/stdlib/block';
|
|
22
|
-
import {
|
|
23
|
-
|
|
24
|
+
import {
|
|
25
|
+
Checkpoint,
|
|
26
|
+
type CheckpointData,
|
|
27
|
+
type CommonCheckpointData,
|
|
28
|
+
L1PublishedData,
|
|
29
|
+
type ProposedCheckpointData,
|
|
30
|
+
type ProposedCheckpointInput,
|
|
31
|
+
PublishedCheckpoint,
|
|
32
|
+
} from '@aztec/stdlib/checkpoint';
|
|
33
|
+
import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
24
34
|
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
25
35
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
26
36
|
import {
|
|
@@ -35,16 +45,20 @@ import {
|
|
|
35
45
|
} from '@aztec/stdlib/tx';
|
|
36
46
|
|
|
37
47
|
import {
|
|
48
|
+
BlockAlreadyCheckpointedError,
|
|
38
49
|
BlockArchiveNotConsistentError,
|
|
50
|
+
BlockCheckpointNumberNotSequentialError,
|
|
39
51
|
BlockIndexNotSequentialError,
|
|
40
52
|
BlockNotFoundError,
|
|
41
53
|
BlockNumberNotSequentialError,
|
|
42
54
|
CannotOverwriteCheckpointedBlockError,
|
|
43
55
|
CheckpointNotFoundError,
|
|
44
|
-
CheckpointNumberNotConsistentError,
|
|
45
56
|
CheckpointNumberNotSequentialError,
|
|
46
|
-
InitialBlockNumberNotSequentialError,
|
|
47
57
|
InitialCheckpointNumberNotSequentialError,
|
|
58
|
+
NoProposedCheckpointToPromoteError,
|
|
59
|
+
ProposedCheckpointArchiveRootMismatchError,
|
|
60
|
+
ProposedCheckpointNotSequentialError,
|
|
61
|
+
ProposedCheckpointPromotionNotSequentialError,
|
|
48
62
|
} from '../errors.js';
|
|
49
63
|
|
|
50
64
|
export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
|
|
@@ -59,19 +73,78 @@ type BlockStorage = {
|
|
|
59
73
|
indexWithinCheckpoint: number;
|
|
60
74
|
};
|
|
61
75
|
|
|
62
|
-
|
|
76
|
+
/** Reason a checkpoint was rejected during sync. */
|
|
77
|
+
export type RejectedCheckpointReason = 'invalid-attestations' | 'descends-from-invalid-attestations';
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* A checkpoint observed on L1 that the archiver decided not to ingest, recorded so that
|
|
81
|
+
* any descendant that builds on top of it can also be skipped (rather than throwing
|
|
82
|
+
* `InitialCheckpointNumberNotSequentialError` and looping). An entry is dropped via
|
|
83
|
+
* {@link BlockStore.removeRejectedCheckpointByArchiveRoot} once a checkpoint with the same
|
|
84
|
+
* archive root is later ingested as valid (e.g. it gathered enough attestations), which
|
|
85
|
+
* re-enables its descendants.
|
|
86
|
+
*/
|
|
87
|
+
export type RejectedCheckpoint = {
|
|
88
|
+
/** Checkpoint number this entry represents. */
|
|
89
|
+
checkpointNumber: CheckpointNumber;
|
|
90
|
+
/** Archive root produced by this rejected checkpoint (matched against descendants' `lastArchiveRoot`). */
|
|
91
|
+
archiveRoot: Fr;
|
|
92
|
+
/** `lastArchiveRoot` from this checkpoint's header (the ancestor it built on). */
|
|
93
|
+
parentArchiveRoot: Fr;
|
|
94
|
+
/** Slot number of the rejected checkpoint. */
|
|
95
|
+
slotNumber: SlotNumber;
|
|
96
|
+
/** L1 publication data for the rejected checkpoint (block number, hash, timestamp). */
|
|
97
|
+
l1: L1PublishedData;
|
|
98
|
+
/** Why the entry was recorded. */
|
|
99
|
+
reason: RejectedCheckpointReason;
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
type RejectedCheckpointStorage = {
|
|
103
|
+
checkpointNumber: number;
|
|
104
|
+
archiveRoot: Buffer;
|
|
105
|
+
parentArchiveRoot: Buffer;
|
|
106
|
+
slotNumber: number;
|
|
107
|
+
l1: Buffer;
|
|
108
|
+
reason: RejectedCheckpointReason;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
/** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
|
|
112
|
+
type CommonCheckpointStorage = {
|
|
63
113
|
header: Buffer;
|
|
64
114
|
archive: Buffer;
|
|
65
115
|
checkpointOutHash: Buffer;
|
|
66
116
|
checkpointNumber: number;
|
|
67
117
|
startBlock: number;
|
|
68
118
|
blockCount: number;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
type CheckpointStorage = CommonCheckpointStorage & {
|
|
69
122
|
l1: Buffer;
|
|
70
123
|
attestations: Buffer[];
|
|
124
|
+
feeAssetPriceModifier: string;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
/** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
|
|
128
|
+
type ProposedCheckpointStorage = CommonCheckpointStorage & {
|
|
129
|
+
totalManaUsed: string;
|
|
130
|
+
feeAssetPriceModifier: string;
|
|
71
131
|
};
|
|
72
132
|
|
|
73
133
|
export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
|
|
74
134
|
|
|
135
|
+
/**
|
|
136
|
+
* Single-block lookup with the chain-tip `tag` variant of {@link BlockQuery} already resolved
|
|
137
|
+
* to a concrete block number. The `tag` branch is unrepresentable here so storage code does
|
|
138
|
+
* not need to handle it at runtime.
|
|
139
|
+
*/
|
|
140
|
+
export type ResolvedBlockQuery = { number: BlockNumber } | { hash: BlockHash } | { archive: Fr };
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Range lookup with the `epoch` variant of {@link BlocksQuery} already resolved to a
|
|
144
|
+
* `{ from, limit }` pair. Storage code never needs to map epoch numbers to block ranges.
|
|
145
|
+
*/
|
|
146
|
+
export type ResolvedBlocksQuery = { from: BlockNumber; limit: number; onlyCheckpointed?: boolean };
|
|
147
|
+
|
|
75
148
|
/**
|
|
76
149
|
* LMDB-based block storage for the archiver.
|
|
77
150
|
*/
|
|
@@ -79,7 +152,10 @@ export class BlockStore {
|
|
|
79
152
|
/** Map block number to block data */
|
|
80
153
|
#blocks: AztecAsyncMap<number, BlockStorage>;
|
|
81
154
|
|
|
82
|
-
/** Map checkpoint number
|
|
155
|
+
/** Map keyed by checkpoint number holding proposed (locally-validated, not yet L1-confirmed) checkpoints. */
|
|
156
|
+
#proposedCheckpoints: AztecAsyncMap<number, ProposedCheckpointStorage>;
|
|
157
|
+
|
|
158
|
+
/** Map checkpoint number to checkpoint data for mined checkpoints only */
|
|
83
159
|
#checkpoints: AztecAsyncMap<number, CheckpointStorage>;
|
|
84
160
|
|
|
85
161
|
/** Map slot number to checkpoint number, for looking up checkpoints by slot range. */
|
|
@@ -97,6 +173,9 @@ export class BlockStore {
|
|
|
97
173
|
/** Stores last proven checkpoint */
|
|
98
174
|
#lastProvenCheckpoint: AztecAsyncSingleton<number>;
|
|
99
175
|
|
|
176
|
+
/** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
|
|
177
|
+
#lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
|
|
178
|
+
|
|
100
179
|
/** Stores the pending chain validation status */
|
|
101
180
|
#pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
|
|
102
181
|
|
|
@@ -109,12 +188,15 @@ export class BlockStore {
|
|
|
109
188
|
/** Index mapping block archive to block number */
|
|
110
189
|
#blockArchiveIndex: AztecAsyncMap<string, number>;
|
|
111
190
|
|
|
191
|
+
/** Map rejected checkpoints (due to invalid attestations) by archive root */
|
|
192
|
+
#rejectedCheckpoints: AztecAsyncMap<string, RejectedCheckpointStorage>;
|
|
193
|
+
|
|
194
|
+
/** Index mapping a rejected checkpoint's number to its archive root, so the latest can be read in reverse order */
|
|
195
|
+
#rejectedCheckpointsByNumber: AztecAsyncMap<number, string>;
|
|
196
|
+
|
|
112
197
|
#log = createLogger('archiver:block_store');
|
|
113
198
|
|
|
114
|
-
constructor(
|
|
115
|
-
private db: AztecAsyncKVStore,
|
|
116
|
-
private l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
|
|
117
|
-
) {
|
|
199
|
+
constructor(private db: AztecAsyncKVStore) {
|
|
118
200
|
this.#blocks = db.openMap('archiver_blocks');
|
|
119
201
|
this.#blockTxs = db.openMap('archiver_block_txs');
|
|
120
202
|
this.#txEffects = db.openMap('archiver_tx_effects');
|
|
@@ -123,120 +205,110 @@ export class BlockStore {
|
|
|
123
205
|
this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
|
|
124
206
|
this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
|
|
125
207
|
this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
|
|
208
|
+
this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
|
|
126
209
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
127
210
|
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
128
211
|
this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
|
|
212
|
+
this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
|
|
213
|
+
this.#rejectedCheckpoints = db.openMap('archiver_rejected_checkpoints');
|
|
214
|
+
this.#rejectedCheckpointsByNumber = db.openMap('archiver_rejected_checkpoints_by_number');
|
|
129
215
|
}
|
|
130
216
|
|
|
131
217
|
/**
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
* TODO(#13569): Compute proper finalized block number based on L1 finalized block.
|
|
135
|
-
* TODO(palla/mbps): Even the provisional computation is wrong, since it should subtract checkpoints, not blocks
|
|
218
|
+
* Returns the finalized L2 block number. An L2 block is finalized when it was proven
|
|
219
|
+
* in an L1 block that has itself been finalized on Ethereum.
|
|
136
220
|
* @returns The finalized block number.
|
|
137
221
|
*/
|
|
138
222
|
async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
|
|
139
|
-
const
|
|
140
|
-
|
|
223
|
+
const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
|
|
224
|
+
if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
225
|
+
return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
226
|
+
}
|
|
227
|
+
const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
|
|
228
|
+
if (!checkpointStorage) {
|
|
229
|
+
throw new CheckpointNotFoundError(finalizedCheckpointNumber);
|
|
230
|
+
}
|
|
231
|
+
return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
|
|
141
232
|
}
|
|
142
233
|
|
|
143
234
|
/**
|
|
144
|
-
* Append new proposed
|
|
145
|
-
*
|
|
235
|
+
* Append a new proposed block to the store.
|
|
236
|
+
* This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
|
|
146
237
|
* For checkpointed blocks (already published to L1), use addCheckpoints() instead.
|
|
147
|
-
* @param
|
|
238
|
+
* @param block - The proposed L2 block to be added to the store.
|
|
148
239
|
* @returns True if the operation is successful.
|
|
149
240
|
*/
|
|
150
|
-
async
|
|
151
|
-
if (blocks.length === 0) {
|
|
152
|
-
return true;
|
|
153
|
-
}
|
|
154
|
-
|
|
241
|
+
async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
|
|
155
242
|
return await this.db.transactionAsync(async () => {
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
const
|
|
159
|
-
const
|
|
160
|
-
const firstBlockLastArchive = blocks[0].header.lastArchive.root;
|
|
243
|
+
const blockNumber = block.number;
|
|
244
|
+
const blockCheckpointNumber = block.checkpointNumber;
|
|
245
|
+
const blockIndex = block.indexWithinCheckpoint;
|
|
246
|
+
const blockLastArchive = block.header.lastArchive.root;
|
|
161
247
|
|
|
162
248
|
// Extract the latest block and checkpoint numbers
|
|
163
|
-
const previousBlockNumber = await this.
|
|
164
|
-
const
|
|
249
|
+
const previousBlockNumber = await this.getLatestL2BlockNumber();
|
|
250
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
165
251
|
|
|
166
252
|
// Verify we're not overwriting checkpointed blocks
|
|
167
253
|
const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
|
|
168
|
-
if (!opts.force &&
|
|
169
|
-
|
|
254
|
+
if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
|
|
255
|
+
// Check if the proposed block matches the already-checkpointed one
|
|
256
|
+
const existingBlock = await this.getBlockData({ number: BlockNumber(blockNumber) });
|
|
257
|
+
if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
|
|
258
|
+
throw new BlockAlreadyCheckpointedError(blockNumber);
|
|
259
|
+
}
|
|
260
|
+
throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
|
|
170
261
|
}
|
|
171
262
|
|
|
172
|
-
// Check that the
|
|
173
|
-
if (!opts.force && previousBlockNumber !==
|
|
174
|
-
throw new
|
|
263
|
+
// Check that the block number is the expected one
|
|
264
|
+
if (!opts.force && previousBlockNumber !== blockNumber - 1) {
|
|
265
|
+
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
175
266
|
}
|
|
176
267
|
|
|
177
|
-
//
|
|
178
|
-
|
|
179
|
-
|
|
268
|
+
// Accept the block if either the confirmed checkpoint or a pending checkpoint matches
|
|
269
|
+
// the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
|
|
270
|
+
const expectedCheckpointNumber = blockCheckpointNumber - 1;
|
|
271
|
+
const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
|
|
272
|
+
if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
|
|
273
|
+
const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
274
|
+
const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
|
|
275
|
+
throw new BlockCheckpointNumberNotSequentialError(BlockNumber(blockNumber), blockCheckpointNumber, previous);
|
|
180
276
|
}
|
|
181
277
|
|
|
182
278
|
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
183
|
-
const previousBlockResult = await this.
|
|
279
|
+
const previousBlockResult = await this.getBlockData({ number: previousBlockNumber });
|
|
184
280
|
|
|
185
|
-
let
|
|
281
|
+
let expectedBlockIndex = 0;
|
|
186
282
|
let previousBlockIndex: number | undefined = undefined;
|
|
187
283
|
if (previousBlockResult !== undefined) {
|
|
188
|
-
if (previousBlockResult.checkpointNumber ===
|
|
284
|
+
if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
|
|
189
285
|
// The previous block is for the same checkpoint, therefore our index should follow it
|
|
190
286
|
previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
|
|
191
|
-
|
|
287
|
+
expectedBlockIndex = previousBlockIndex + 1;
|
|
192
288
|
}
|
|
193
|
-
if (!previousBlockResult.archive.root.equals(
|
|
289
|
+
if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
|
|
194
290
|
throw new BlockArchiveNotConsistentError(
|
|
195
|
-
|
|
196
|
-
previousBlockResult.
|
|
197
|
-
|
|
291
|
+
blockNumber,
|
|
292
|
+
previousBlockResult.header.globalVariables.blockNumber,
|
|
293
|
+
blockLastArchive,
|
|
198
294
|
previousBlockResult.archive.root,
|
|
199
295
|
);
|
|
200
296
|
}
|
|
201
297
|
}
|
|
202
298
|
|
|
203
|
-
// Now check that the
|
|
204
|
-
if (!opts.force &&
|
|
205
|
-
throw new BlockIndexNotSequentialError(
|
|
299
|
+
// Now check that the block has the expected index value
|
|
300
|
+
if (!opts.force && expectedBlockIndex !== blockIndex) {
|
|
301
|
+
throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
|
|
206
302
|
}
|
|
207
303
|
|
|
208
|
-
|
|
209
|
-
let previousBlock: L2Block | undefined = undefined;
|
|
210
|
-
for (const block of blocks) {
|
|
211
|
-
if (!opts.force && previousBlock) {
|
|
212
|
-
if (previousBlock.number + 1 !== block.number) {
|
|
213
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
214
|
-
}
|
|
215
|
-
if (previousBlock.indexWithinCheckpoint + 1 !== block.indexWithinCheckpoint) {
|
|
216
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
217
|
-
}
|
|
218
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
219
|
-
throw new BlockArchiveNotConsistentError(
|
|
220
|
-
block.number,
|
|
221
|
-
previousBlock.number,
|
|
222
|
-
block.header.lastArchive.root,
|
|
223
|
-
previousBlock.archive.root,
|
|
224
|
-
);
|
|
225
|
-
}
|
|
226
|
-
}
|
|
227
|
-
if (!opts.force && firstBlockCheckpointNumber !== block.checkpointNumber) {
|
|
228
|
-
throw new CheckpointNumberNotConsistentError(block.checkpointNumber, firstBlockCheckpointNumber);
|
|
229
|
-
}
|
|
230
|
-
previousBlock = block;
|
|
231
|
-
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
232
|
-
}
|
|
304
|
+
await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
|
|
233
305
|
|
|
234
306
|
return true;
|
|
235
307
|
});
|
|
236
308
|
}
|
|
237
309
|
|
|
238
310
|
/**
|
|
239
|
-
* Append new
|
|
311
|
+
* Append new checkpoints to the store's list.
|
|
240
312
|
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
241
313
|
* @returns True if the operation is successful.
|
|
242
314
|
*/
|
|
@@ -246,37 +318,29 @@ export class BlockStore {
|
|
|
246
318
|
}
|
|
247
319
|
|
|
248
320
|
return await this.db.transactionAsync(async () => {
|
|
249
|
-
// Check that the checkpoint immediately before the first block to be added is present in the store.
|
|
250
321
|
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
251
322
|
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
252
323
|
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
// There should be a previous checkpoint
|
|
261
|
-
previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
262
|
-
if (previousCheckpointData === undefined) {
|
|
263
|
-
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
324
|
+
// Handle already-stored checkpoints at the start of the batch.
|
|
325
|
+
// This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
|
|
326
|
+
// We accept them if archives match (same content) and update their L1 metadata.
|
|
327
|
+
if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
|
|
328
|
+
checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
|
|
329
|
+
if (checkpoints.length === 0) {
|
|
330
|
+
return true;
|
|
264
331
|
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
// If we have a previous checkpoint then we need to get the previous block number
|
|
271
|
-
if (previousCheckpointData !== undefined) {
|
|
272
|
-
previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
|
|
273
|
-
previousBlock = await this.getBlock(previousBlockNumber);
|
|
274
|
-
if (previousBlock === undefined) {
|
|
275
|
-
// We should be able to get the required previous block
|
|
276
|
-
throw new BlockNotFoundError(previousBlockNumber);
|
|
332
|
+
// Re-check sequentiality after skipping
|
|
333
|
+
const newFirstNumber = checkpoints[0].checkpoint.number;
|
|
334
|
+
if (previousCheckpointNumber !== newFirstNumber - 1) {
|
|
335
|
+
throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
|
|
277
336
|
}
|
|
337
|
+
} else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
338
|
+
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
278
339
|
}
|
|
279
340
|
|
|
341
|
+
// Get the last block of the previous checkpoint for archive chaining
|
|
342
|
+
let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
|
|
343
|
+
|
|
280
344
|
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
281
345
|
let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
|
|
282
346
|
for (const checkpoint of checkpoints) {
|
|
@@ -292,42 +356,14 @@ export class BlockStore {
|
|
|
292
356
|
}
|
|
293
357
|
previousCheckpoint = checkpoint;
|
|
294
358
|
|
|
295
|
-
//
|
|
296
|
-
|
|
297
|
-
const block = checkpoint.checkpoint.blocks[i];
|
|
298
|
-
if (previousBlock) {
|
|
299
|
-
// The blocks should have a sequential block number
|
|
300
|
-
if (previousBlock.number !== block.number - 1) {
|
|
301
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
302
|
-
}
|
|
303
|
-
// If the blocks are for the same checkpoint then they should have sequential indexes
|
|
304
|
-
if (
|
|
305
|
-
previousBlock.checkpointNumber === block.checkpointNumber &&
|
|
306
|
-
previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1
|
|
307
|
-
) {
|
|
308
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
309
|
-
}
|
|
310
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
311
|
-
throw new BlockArchiveNotConsistentError(
|
|
312
|
-
block.number,
|
|
313
|
-
previousBlock.number,
|
|
314
|
-
block.header.lastArchive.root,
|
|
315
|
-
previousBlock.archive.root,
|
|
316
|
-
);
|
|
317
|
-
}
|
|
318
|
-
} else {
|
|
319
|
-
// No previous block, must be block 1 at checkpoint index 0
|
|
320
|
-
if (block.indexWithinCheckpoint !== 0) {
|
|
321
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
322
|
-
}
|
|
323
|
-
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
324
|
-
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
325
|
-
}
|
|
326
|
-
}
|
|
359
|
+
// Validate block sequencing, indexes, and archive chaining
|
|
360
|
+
this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
|
|
327
361
|
|
|
328
|
-
|
|
329
|
-
|
|
362
|
+
// Store every block in the database (may already exist, but L1 data is authoritative)
|
|
363
|
+
for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
|
|
364
|
+
await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
|
|
330
365
|
}
|
|
366
|
+
previousBlock = checkpoint.checkpoint.blocks.at(-1);
|
|
331
367
|
|
|
332
368
|
// Store the checkpoint in the database
|
|
333
369
|
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
@@ -339,17 +375,135 @@ export class BlockStore {
|
|
|
339
375
|
checkpointNumber: checkpoint.checkpoint.number,
|
|
340
376
|
startBlock: checkpoint.checkpoint.blocks[0].number,
|
|
341
377
|
blockCount: checkpoint.checkpoint.blocks.length,
|
|
378
|
+
feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString(),
|
|
342
379
|
});
|
|
343
380
|
|
|
344
381
|
// Update slot-to-checkpoint index
|
|
345
382
|
await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
|
|
383
|
+
|
|
384
|
+
// Remove proposed checkpoint if it exists, since L1 is authoritative
|
|
385
|
+
await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
|
|
386
|
+
|
|
387
|
+
// Drop any rejected entry for this archive root: a checkpoint that was previously rejected
|
|
388
|
+
// (e.g. invalid attestations) is now being ingested as valid, so its descendants are allowed.
|
|
389
|
+
await this.removeRejectedCheckpointByArchiveRoot(checkpoint.checkpoint.archive.root);
|
|
346
390
|
}
|
|
347
391
|
|
|
348
|
-
await this
|
|
392
|
+
await this.advanceSynchedL1BlockNumber(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
349
393
|
return true;
|
|
350
394
|
});
|
|
351
395
|
}
|
|
352
396
|
|
|
397
|
+
/**
|
|
398
|
+
* Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
|
|
399
|
+
* Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
|
|
400
|
+
*/
|
|
401
|
+
private async skipOrUpdateAlreadyStoredCheckpoints(
|
|
402
|
+
checkpoints: PublishedCheckpoint[],
|
|
403
|
+
latestStored: CheckpointNumber,
|
|
404
|
+
): Promise<PublishedCheckpoint[]> {
|
|
405
|
+
let i = 0;
|
|
406
|
+
for (; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++) {
|
|
407
|
+
const incoming = checkpoints[i];
|
|
408
|
+
const stored = await this.getCheckpointData(incoming.checkpoint.number);
|
|
409
|
+
if (!stored) {
|
|
410
|
+
// Should not happen if latestStored is correct, but be safe
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
// Verify the checkpoint content matches (archive root)
|
|
414
|
+
if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
|
|
415
|
+
throw new Error(
|
|
416
|
+
`Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` +
|
|
417
|
+
`Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`,
|
|
418
|
+
);
|
|
419
|
+
}
|
|
420
|
+
// Update L1 metadata and attestations for the already-stored checkpoint
|
|
421
|
+
this.#log.warn(
|
|
422
|
+
`Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` +
|
|
423
|
+
`(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`,
|
|
424
|
+
);
|
|
425
|
+
await this.#checkpoints.set(incoming.checkpoint.number, {
|
|
426
|
+
header: incoming.checkpoint.header.toBuffer(),
|
|
427
|
+
archive: incoming.checkpoint.archive.toBuffer(),
|
|
428
|
+
checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
429
|
+
l1: incoming.l1.toBuffer(),
|
|
430
|
+
attestations: incoming.attestations.map(a => a.toBuffer()),
|
|
431
|
+
checkpointNumber: incoming.checkpoint.number,
|
|
432
|
+
startBlock: incoming.checkpoint.blocks[0].number,
|
|
433
|
+
blockCount: incoming.checkpoint.blocks.length,
|
|
434
|
+
feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString(),
|
|
435
|
+
});
|
|
436
|
+
// Update the sync point to reflect the new L1 block
|
|
437
|
+
await this.advanceSynchedL1BlockNumber(incoming.l1.blockNumber);
|
|
438
|
+
}
|
|
439
|
+
return checkpoints.slice(i);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
/**
|
|
443
|
+
* Gets the last block of the checkpoint before the given one.
|
|
444
|
+
* Returns undefined if there is no previous checkpoint (i.e. genesis).
|
|
445
|
+
*/
|
|
446
|
+
private async getPreviousCheckpointBlock(checkpointNumber: CheckpointNumber): Promise<L2Block | undefined> {
|
|
447
|
+
const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
|
|
448
|
+
if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
449
|
+
return undefined;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
// Check across both proposed and mined checkpoints
|
|
453
|
+
const predecessor =
|
|
454
|
+
(await this.getProposedCheckpointByNumber(previousCheckpointNumber)) ??
|
|
455
|
+
(await this.getCheckpointData(previousCheckpointNumber));
|
|
456
|
+
|
|
457
|
+
if (!predecessor) {
|
|
458
|
+
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
|
|
462
|
+
const previousBlock = await this.getBlock({ number: previousBlockNumber });
|
|
463
|
+
if (previousBlock === undefined) {
|
|
464
|
+
throw new BlockNotFoundError(previousBlockNumber);
|
|
465
|
+
}
|
|
466
|
+
return previousBlock;
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Validates that blocks are sequential, have correct indexes, and chain via archive roots.
|
|
471
|
+
* This is the same validation used for both confirmed checkpoints (addCheckpoints) and
|
|
472
|
+
* proposed checkpoints (addProposedCheckpoint).
|
|
473
|
+
*/
|
|
474
|
+
private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
|
|
475
|
+
for (const block of blocks) {
|
|
476
|
+
if (previousBlock) {
|
|
477
|
+
if (previousBlock.number !== block.number - 1) {
|
|
478
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
479
|
+
}
|
|
480
|
+
if (previousBlock.checkpointNumber === block.checkpointNumber) {
|
|
481
|
+
if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
482
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
483
|
+
}
|
|
484
|
+
} else if (block.indexWithinCheckpoint !== 0) {
|
|
485
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
486
|
+
}
|
|
487
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
488
|
+
throw new BlockArchiveNotConsistentError(
|
|
489
|
+
block.number,
|
|
490
|
+
previousBlock.number,
|
|
491
|
+
block.header.lastArchive.root,
|
|
492
|
+
previousBlock.archive.root,
|
|
493
|
+
);
|
|
494
|
+
}
|
|
495
|
+
} else {
|
|
496
|
+
if (block.indexWithinCheckpoint !== 0) {
|
|
497
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
498
|
+
}
|
|
499
|
+
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
500
|
+
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
previousBlock = block;
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
|
|
353
507
|
private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
|
|
354
508
|
const blockHash = await block.hash();
|
|
355
509
|
|
|
@@ -442,6 +596,9 @@ export class BlockStore {
|
|
|
442
596
|
this.#log.debug(`Removed checkpoint ${c}`);
|
|
443
597
|
}
|
|
444
598
|
|
|
599
|
+
// Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
|
|
600
|
+
await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
|
|
601
|
+
|
|
445
602
|
return { blocksRemoved };
|
|
446
603
|
});
|
|
447
604
|
}
|
|
@@ -481,6 +638,22 @@ export class BlockStore {
|
|
|
481
638
|
return result;
|
|
482
639
|
}
|
|
483
640
|
|
|
641
|
+
/**
|
|
642
|
+
* Returns the checkpoint numbers for all checkpoints whose slot falls within the given range (inclusive).
|
|
643
|
+
* Lighter than {@link getCheckpointDataForSlotRange} when callers only need to identify which
|
|
644
|
+
* checkpoints fall in the range and will fetch full data for at most a few of them.
|
|
645
|
+
*/
|
|
646
|
+
async getCheckpointNumbersForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointNumber[]> {
|
|
647
|
+
const result: CheckpointNumber[] = [];
|
|
648
|
+
for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
|
|
649
|
+
start: startSlot,
|
|
650
|
+
end: endSlot + 1,
|
|
651
|
+
})) {
|
|
652
|
+
result.push(CheckpointNumber(checkpointNumber));
|
|
653
|
+
}
|
|
654
|
+
return result;
|
|
655
|
+
}
|
|
656
|
+
|
|
484
657
|
private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage): CheckpointData {
|
|
485
658
|
return {
|
|
486
659
|
header: CheckpointHeader.fromBuffer(checkpointStorage.header),
|
|
@@ -489,6 +662,7 @@ export class BlockStore {
|
|
|
489
662
|
checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
|
|
490
663
|
startBlock: BlockNumber(checkpointStorage.startBlock),
|
|
491
664
|
blockCount: checkpointStorage.blockCount,
|
|
665
|
+
feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
|
|
492
666
|
l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
|
|
493
667
|
attestations: checkpointStorage.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
494
668
|
};
|
|
@@ -547,11 +721,11 @@ export class BlockStore {
|
|
|
547
721
|
const removedBlocks: L2Block[] = [];
|
|
548
722
|
|
|
549
723
|
// Get the latest block number to determine the range
|
|
550
|
-
const latestBlockNumber = await this.
|
|
724
|
+
const latestBlockNumber = await this.getLatestL2BlockNumber();
|
|
551
725
|
|
|
552
726
|
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
553
727
|
for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
|
|
554
|
-
const block = await this.getBlock(BlockNumber(bn));
|
|
728
|
+
const block = await this.getBlock({ number: BlockNumber(bn) });
|
|
555
729
|
|
|
556
730
|
if (block === undefined) {
|
|
557
731
|
this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
|
|
@@ -580,13 +754,6 @@ export class BlockStore {
|
|
|
580
754
|
}
|
|
581
755
|
}
|
|
582
756
|
|
|
583
|
-
async getLatestBlockNumber(): Promise<BlockNumber> {
|
|
584
|
-
const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
585
|
-
return typeof latestBlocknumber === 'number'
|
|
586
|
-
? BlockNumber(latestBlocknumber)
|
|
587
|
-
: BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
588
|
-
}
|
|
589
|
-
|
|
590
757
|
async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
|
|
591
758
|
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
592
759
|
if (latestCheckpointNumber === undefined) {
|
|
@@ -595,175 +762,204 @@ export class BlockStore {
|
|
|
595
762
|
return CheckpointNumber(latestCheckpointNumber);
|
|
596
763
|
}
|
|
597
764
|
|
|
598
|
-
async
|
|
599
|
-
const
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
const block = await this.getBlockFromBlockStorage(number, blockStorage);
|
|
608
|
-
if (!block) {
|
|
609
|
-
return undefined;
|
|
765
|
+
async hasProposedCheckpoint(): Promise<boolean> {
|
|
766
|
+
const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ limit: 1 }));
|
|
767
|
+
return key !== undefined;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/** Deletes all pending proposed checkpoints from storage. */
|
|
771
|
+
async deleteProposedCheckpoints(): Promise<void> {
|
|
772
|
+
for await (const key of this.#proposedCheckpoints.keysAsync()) {
|
|
773
|
+
await this.#proposedCheckpoints.delete(key);
|
|
610
774
|
}
|
|
611
|
-
return new CheckpointedL2Block(
|
|
612
|
-
CheckpointNumber(checkpoint.checkpointNumber),
|
|
613
|
-
block,
|
|
614
|
-
L1PublishedData.fromBuffer(checkpoint.l1),
|
|
615
|
-
checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
616
|
-
);
|
|
617
775
|
}
|
|
618
776
|
|
|
619
777
|
/**
|
|
620
|
-
*
|
|
621
|
-
*
|
|
622
|
-
*
|
|
623
|
-
* @
|
|
778
|
+
* Promotes a specific pending checkpoint to a confirmed checkpoint entry.
|
|
779
|
+
* This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
|
|
780
|
+
* Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
|
|
781
|
+
* @param checkpointNumber - The checkpoint number to promote.
|
|
782
|
+
* @param l1 - L1 published data for the checkpoint.
|
|
783
|
+
* @param attestations - Committee attestations.
|
|
784
|
+
* @param expectedArchiveRoot - Archive root guard against races.
|
|
624
785
|
*/
|
|
625
|
-
async
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
const checkpointedBlock = new CheckpointedL2Block(
|
|
636
|
-
CheckpointNumber(checkpoint.checkpointNumber),
|
|
637
|
-
block,
|
|
638
|
-
L1PublishedData.fromBuffer(checkpoint.l1),
|
|
639
|
-
checkpoint.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
|
|
640
|
-
);
|
|
641
|
-
yield checkpointedBlock;
|
|
642
|
-
}
|
|
786
|
+
async promoteProposedToCheckpointed(
|
|
787
|
+
checkpointNumber: CheckpointNumber,
|
|
788
|
+
l1: L1PublishedData,
|
|
789
|
+
attestations: CommitteeAttestation[],
|
|
790
|
+
expectedArchiveRoot: Fr,
|
|
791
|
+
): Promise<void> {
|
|
792
|
+
return await this.db.transactionAsync(async () => {
|
|
793
|
+
const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
|
|
794
|
+
if (!proposed) {
|
|
795
|
+
throw new NoProposedCheckpointToPromoteError();
|
|
643
796
|
}
|
|
644
|
-
|
|
797
|
+
if (!proposed.archive.root.equals(expectedArchiveRoot)) {
|
|
798
|
+
throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
|
|
799
|
+
}
|
|
800
|
+
|
|
801
|
+
// Verify sequentiality: promoted checkpoint must follow the latest confirmed one
|
|
802
|
+
const latestCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
803
|
+
if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
|
|
804
|
+
throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
|
|
805
|
+
}
|
|
806
|
+
|
|
807
|
+
// Write the checkpoint entry
|
|
808
|
+
await this.#checkpoints.set(proposed.checkpointNumber, {
|
|
809
|
+
header: proposed.header.toBuffer(),
|
|
810
|
+
archive: proposed.archive.toBuffer(),
|
|
811
|
+
checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
|
|
812
|
+
l1: l1.toBuffer(),
|
|
813
|
+
attestations: attestations.map(attestation => attestation.toBuffer()),
|
|
814
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
815
|
+
startBlock: proposed.startBlock,
|
|
816
|
+
blockCount: proposed.blockCount,
|
|
817
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
// Update the slot-to-checkpoint index
|
|
821
|
+
await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
|
|
822
|
+
|
|
823
|
+
// Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
|
|
824
|
+
await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
|
|
825
|
+
|
|
826
|
+
// Drop any rejected entry for this archive root: a checkpoint that was previously rejected
|
|
827
|
+
// (e.g. invalid attestations) is now being promoted as valid, so its descendants are allowed.
|
|
828
|
+
await this.removeRejectedCheckpointByArchiveRoot(proposed.archive.root);
|
|
829
|
+
|
|
830
|
+
// Update the last synced L1 block
|
|
831
|
+
await this.advanceSynchedL1BlockNumber(l1.blockNumber);
|
|
832
|
+
});
|
|
645
833
|
}
|
|
646
834
|
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
835
|
+
/**
|
|
836
|
+
* Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
|
|
837
|
+
* No fallback to confirmed.
|
|
838
|
+
*/
|
|
839
|
+
async getLastProposedCheckpoint(): Promise<ProposedCheckpointData | undefined> {
|
|
840
|
+
const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
841
|
+
if (key === undefined) {
|
|
650
842
|
return undefined;
|
|
651
843
|
}
|
|
652
|
-
|
|
844
|
+
const stored = await this.#proposedCheckpoints.getAsync(key);
|
|
845
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
653
846
|
}
|
|
654
847
|
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
}
|
|
660
|
-
return this.getCheckpointedBlock(BlockNumber(blockNumber));
|
|
848
|
+
/** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */
|
|
849
|
+
async getProposedCheckpointByNumber(n: CheckpointNumber): Promise<ProposedCheckpointData | undefined> {
|
|
850
|
+
const stored = await this.#proposedCheckpoints.getAsync(n);
|
|
851
|
+
return stored ? this.convertToProposedCheckpointData(stored) : undefined;
|
|
661
852
|
}
|
|
662
853
|
|
|
663
854
|
/**
|
|
664
|
-
*
|
|
665
|
-
*
|
|
666
|
-
*
|
|
667
|
-
* @returns The requested L2 blocks
|
|
855
|
+
* Returns the pending checkpoint whose header slot matches the given slot, or undefined if not found.
|
|
856
|
+
* Iterates `#proposedCheckpoints` rather than reading an index because the map carries 0–1 entries
|
|
857
|
+
* in normal operation (bounded by the proposer pipelining window). Returns the first match.
|
|
668
858
|
*/
|
|
669
|
-
async
|
|
670
|
-
for await (const [
|
|
671
|
-
const
|
|
672
|
-
if (
|
|
673
|
-
|
|
859
|
+
async getProposedCheckpointBySlot(slot: SlotNumber): Promise<ProposedCheckpointData | undefined> {
|
|
860
|
+
for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()) {
|
|
861
|
+
const header = CheckpointHeader.fromBuffer(stored.header);
|
|
862
|
+
if (header.slotNumber === slot) {
|
|
863
|
+
return this.convertToProposedCheckpointData(stored);
|
|
674
864
|
}
|
|
675
865
|
}
|
|
866
|
+
return undefined;
|
|
676
867
|
}
|
|
677
868
|
|
|
678
869
|
/**
|
|
679
|
-
*
|
|
680
|
-
*
|
|
681
|
-
*
|
|
870
|
+
* Evicts all pending checkpoints with checkpoint number >= fromNumber.
|
|
871
|
+
* Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
|
|
872
|
+
* all pending >= N must be evicted since they chain off the now-invalid pending N.
|
|
682
873
|
*/
|
|
683
|
-
async
|
|
684
|
-
const
|
|
685
|
-
|
|
686
|
-
|
|
874
|
+
async evictProposedCheckpointsFrom(fromNumber: CheckpointNumber): Promise<void> {
|
|
875
|
+
const keysToDelete: number[] = [];
|
|
876
|
+
for await (const key of this.#proposedCheckpoints.keysAsync()) {
|
|
877
|
+
if (key >= fromNumber) {
|
|
878
|
+
keysToDelete.push(key);
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
for (const key of keysToDelete) {
|
|
882
|
+
await this.#proposedCheckpoints.delete(key);
|
|
687
883
|
}
|
|
688
|
-
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
689
884
|
}
|
|
690
885
|
|
|
691
886
|
/**
|
|
692
|
-
* Gets
|
|
693
|
-
*
|
|
694
|
-
*
|
|
887
|
+
* Gets the checkpoint at the proposed tip:
|
|
888
|
+
* - latest pending checkpoint if any exist
|
|
889
|
+
* - fallsback to latest confirmed checkpoint otherwise
|
|
695
890
|
*/
|
|
696
|
-
async
|
|
697
|
-
const
|
|
698
|
-
if (
|
|
699
|
-
return
|
|
891
|
+
async getLastCheckpoint(): Promise<CommonCheckpointData | undefined> {
|
|
892
|
+
const latest = await this.getLastProposedCheckpoint();
|
|
893
|
+
if (!latest) {
|
|
894
|
+
return this.getCheckpointData(await this.getLatestCheckpointNumber());
|
|
700
895
|
}
|
|
701
|
-
return
|
|
896
|
+
return latest;
|
|
702
897
|
}
|
|
703
898
|
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
899
|
+
private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
|
|
900
|
+
return {
|
|
901
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
902
|
+
header: CheckpointHeader.fromBuffer(stored.header),
|
|
903
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
|
|
904
|
+
checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
|
|
905
|
+
startBlock: BlockNumber(stored.startBlock),
|
|
906
|
+
blockCount: stored.blockCount,
|
|
907
|
+
totalManaUsed: BigInt(stored.totalManaUsed),
|
|
908
|
+
feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
|
|
909
|
+
};
|
|
715
910
|
}
|
|
716
911
|
|
|
717
912
|
/**
|
|
718
|
-
*
|
|
719
|
-
* @
|
|
720
|
-
* @returns The requested L2 block.
|
|
913
|
+
* Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
|
|
914
|
+
* @returns CheckpointNumber
|
|
721
915
|
*/
|
|
722
|
-
async
|
|
723
|
-
const
|
|
724
|
-
if (
|
|
725
|
-
return
|
|
916
|
+
async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
917
|
+
const proposed = await this.getLastCheckpoint();
|
|
918
|
+
if (!proposed) {
|
|
919
|
+
return await this.getLatestCheckpointNumber();
|
|
726
920
|
}
|
|
727
|
-
return
|
|
921
|
+
return CheckpointNumber(proposed.checkpointNumber);
|
|
728
922
|
}
|
|
729
923
|
|
|
730
924
|
/**
|
|
731
|
-
*
|
|
732
|
-
* @
|
|
733
|
-
* @returns The requested L2 block.
|
|
925
|
+
* Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
|
|
926
|
+
* @returns BlockNumber
|
|
734
927
|
*/
|
|
735
|
-
async
|
|
736
|
-
const
|
|
737
|
-
if (
|
|
738
|
-
return
|
|
928
|
+
async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
|
|
929
|
+
const proposed = await this.getLastCheckpoint();
|
|
930
|
+
if (!proposed) {
|
|
931
|
+
return await this.getCheckpointedL2BlockNumber();
|
|
739
932
|
}
|
|
740
|
-
return
|
|
933
|
+
return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
|
|
741
934
|
}
|
|
742
935
|
|
|
743
|
-
/**
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
936
|
+
/** Returns the checkpoint number that contains the given slot (or undefined if not found). */
|
|
937
|
+
async getCheckpointNumberBySlot(slot: SlotNumber): Promise<CheckpointNumber | undefined> {
|
|
938
|
+
const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
|
|
939
|
+
return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
/** Gets a single L2 block matching the given resolved query. */
|
|
943
|
+
async getBlock(query: ResolvedBlockQuery): Promise<L2Block | undefined> {
|
|
944
|
+
const blockNumber = await this.getBlockNumber(query);
|
|
750
945
|
if (blockNumber === undefined) {
|
|
751
946
|
return undefined;
|
|
752
947
|
}
|
|
753
948
|
const blockStorage = await this.#blocks.getAsync(blockNumber);
|
|
754
|
-
if (!blockStorage
|
|
949
|
+
if (!blockStorage) {
|
|
755
950
|
return undefined;
|
|
756
951
|
}
|
|
757
|
-
return
|
|
952
|
+
return this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
758
953
|
}
|
|
759
954
|
|
|
760
|
-
/**
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
955
|
+
/** Gets a collection of L2 blocks for a resolved range. */
|
|
956
|
+
getBlocks(query: ResolvedBlocksQuery): Promise<L2Block[]> {
|
|
957
|
+
return toArray(this.iterateBlocks(query));
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
/** Gets single block metadata matching the given resolved query. */
|
|
961
|
+
async getBlockData(query: ResolvedBlockQuery): Promise<BlockData | undefined> {
|
|
962
|
+
const blockNumber = await this.getBlockNumber(query);
|
|
767
963
|
if (blockNumber === undefined) {
|
|
768
964
|
return undefined;
|
|
769
965
|
}
|
|
@@ -771,24 +967,36 @@ export class BlockStore {
|
|
|
771
967
|
if (!blockStorage || !blockStorage.header) {
|
|
772
968
|
return undefined;
|
|
773
969
|
}
|
|
774
|
-
return
|
|
970
|
+
return this.getBlockDataFromBlockStorage(blockStorage);
|
|
775
971
|
}
|
|
776
972
|
|
|
777
|
-
/**
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
async *
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
if (
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
973
|
+
/** Gets a collection of block metadata entries for a resolved range. */
|
|
974
|
+
getBlocksData(query: ResolvedBlocksQuery): Promise<BlockData[]> {
|
|
975
|
+
return toArray(this.iterateBlocksData(query));
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
/** Async iterator over L2 blocks for a resolved range. */
|
|
979
|
+
private async *iterateBlocks(query: ResolvedBlocksQuery): AsyncIterableIterator<L2Block> {
|
|
980
|
+
const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
|
|
981
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)) {
|
|
982
|
+
if (cap !== undefined && blockNumber > cap) {
|
|
983
|
+
break;
|
|
984
|
+
}
|
|
985
|
+
const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
|
|
986
|
+
if (block) {
|
|
987
|
+
yield block;
|
|
790
988
|
}
|
|
791
|
-
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/** Async iterator over block metadata for a resolved range. */
|
|
993
|
+
private async *iterateBlocksData(query: ResolvedBlocksQuery): AsyncIterableIterator<BlockData> {
|
|
994
|
+
const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
|
|
995
|
+
for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)) {
|
|
996
|
+
if (cap !== undefined && blockNumber > cap) {
|
|
997
|
+
break;
|
|
998
|
+
}
|
|
999
|
+
yield this.getBlockDataFromBlockStorage(blockStorage);
|
|
792
1000
|
}
|
|
793
1001
|
}
|
|
794
1002
|
|
|
@@ -805,11 +1013,29 @@ export class BlockStore {
|
|
|
805
1013
|
}
|
|
806
1014
|
}
|
|
807
1015
|
|
|
1016
|
+
/** Resolves a ResolvedBlockQuery discriminant to a block number, or undefined if not found. */
|
|
1017
|
+
async getBlockNumber(query: ResolvedBlockQuery): Promise<BlockNumber | undefined> {
|
|
1018
|
+
let blockNumber: BlockNumber | undefined;
|
|
1019
|
+
if ('number' in query) {
|
|
1020
|
+
blockNumber = query.number;
|
|
1021
|
+
} else if ('hash' in query) {
|
|
1022
|
+
const n = await this.#blockHashIndex.getAsync(query.hash.toString());
|
|
1023
|
+
blockNumber = n !== undefined ? BlockNumber(n) : undefined;
|
|
1024
|
+
} else {
|
|
1025
|
+
const n = await this.#blockArchiveIndex.getAsync(query.archive.toString());
|
|
1026
|
+
blockNumber = n !== undefined ? BlockNumber(n) : undefined;
|
|
1027
|
+
}
|
|
1028
|
+
if (blockNumber === undefined) {
|
|
1029
|
+
return undefined;
|
|
1030
|
+
}
|
|
1031
|
+
return blockNumber;
|
|
1032
|
+
}
|
|
1033
|
+
|
|
808
1034
|
private getBlockDataFromBlockStorage(blockStorage: BlockStorage): BlockData {
|
|
809
1035
|
return {
|
|
810
1036
|
header: BlockHeader.fromBuffer(blockStorage.header),
|
|
811
1037
|
archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
|
|
812
|
-
blockHash:
|
|
1038
|
+
blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
|
|
813
1039
|
checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
|
|
814
1040
|
indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
|
|
815
1041
|
};
|
|
@@ -871,7 +1097,10 @@ export class BlockStore {
|
|
|
871
1097
|
* @param txHash - The hash of a tx we try to get the receipt for.
|
|
872
1098
|
* @returns The requested tx receipt (or undefined if not found).
|
|
873
1099
|
*/
|
|
874
|
-
async getSettledTxReceipt(
|
|
1100
|
+
async getSettledTxReceipt(
|
|
1101
|
+
txHash: TxHash,
|
|
1102
|
+
l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
|
|
1103
|
+
): Promise<TxReceipt | undefined> {
|
|
875
1104
|
const txEffect = await this.getTxEffect(txHash);
|
|
876
1105
|
if (!txEffect) {
|
|
877
1106
|
return undefined;
|
|
@@ -880,10 +1109,11 @@ export class BlockStore {
|
|
|
880
1109
|
const blockNumber = BlockNumber(txEffect.l2BlockNumber);
|
|
881
1110
|
|
|
882
1111
|
// Use existing archiver methods to determine finalization level
|
|
883
|
-
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
|
|
1112
|
+
const [provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber, blockData] = await Promise.all([
|
|
884
1113
|
this.getProvenBlockNumber(),
|
|
885
1114
|
this.getCheckpointedL2BlockNumber(),
|
|
886
1115
|
this.getFinalizedL2BlockNumber(),
|
|
1116
|
+
this.getBlockData({ number: blockNumber }),
|
|
887
1117
|
]);
|
|
888
1118
|
|
|
889
1119
|
let status: TxStatus;
|
|
@@ -897,6 +1127,9 @@ export class BlockStore {
|
|
|
897
1127
|
status = TxStatus.PROPOSED;
|
|
898
1128
|
}
|
|
899
1129
|
|
|
1130
|
+
const epochNumber =
|
|
1131
|
+
blockData && l1Constants ? getEpochAtSlot(blockData.header.globalVariables.slotNumber, l1Constants) : undefined;
|
|
1132
|
+
|
|
900
1133
|
return new TxReceipt(
|
|
901
1134
|
txHash,
|
|
902
1135
|
status,
|
|
@@ -905,6 +1138,7 @@ export class BlockStore {
|
|
|
905
1138
|
txEffect.data.transactionFee.toBigInt(),
|
|
906
1139
|
txEffect.l2BlockHash,
|
|
907
1140
|
blockNumber,
|
|
1141
|
+
epochNumber,
|
|
908
1142
|
);
|
|
909
1143
|
}
|
|
910
1144
|
|
|
@@ -918,10 +1152,43 @@ export class BlockStore {
|
|
|
918
1152
|
if (!txEffect) {
|
|
919
1153
|
return undefined;
|
|
920
1154
|
}
|
|
921
|
-
|
|
1155
|
+
// Read only the IndexedTxEffect header (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4)`); the
|
|
1156
|
+
// large tail (the full TxEffect with logs etc.) is irrelevant here.
|
|
1157
|
+
const view = Buffer.from(txEffect.buffer, txEffect.byteOffset, txEffect.byteLength);
|
|
1158
|
+
const l2BlockNumber = view.readUInt32BE(32);
|
|
1159
|
+
const txIndexInBlock = view.readUInt32BE(36);
|
|
922
1160
|
return [l2BlockNumber, txIndexInBlock];
|
|
923
1161
|
}
|
|
924
1162
|
|
|
1163
|
+
/**
|
|
1164
|
+
* Batched, partial deserializer that fetches `noteHashes` and `nullifiers` (all of them) for the given
|
|
1165
|
+
* txs. For each input txHash, returns a `[noteHashes, nullifiers]` tuple. Returns `[[], []]` for any
|
|
1166
|
+
* unknown txHash. Preserves input order. Used by the log read path when `includeEffects` is set to
|
|
1167
|
+
* attach effect data on demand without paying for a full {@link TxEffect} deserialization.
|
|
1168
|
+
*
|
|
1169
|
+
* The on-disk `IndexedTxEffect` layout starts with a fixed-length header
|
|
1170
|
+
* (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4) + revertCode(1) + txHash(32) + transactionFee(32)` =
|
|
1171
|
+
* 105 bytes), followed by `noteHashes` and `nullifiers` (both u8-length-prefixed `Fr` vectors). We
|
|
1172
|
+
* skip the header, then read the two vectors, and stop — the large tail (`l2ToL1Msgs`,
|
|
1173
|
+
* `publicDataWrites`, `privateLogs`, `publicLogs`, `contractClassLogs`) is never touched.
|
|
1174
|
+
*/
|
|
1175
|
+
public getNoteHashesAndNullifiers(txHashes: TxHash[]): Promise<[Fr[], Fr[]][]> {
|
|
1176
|
+
return Promise.all(
|
|
1177
|
+
txHashes.map(async (txHash): Promise<[Fr[], Fr[]]> => {
|
|
1178
|
+
const buffer = await this.#txEffects.getAsync(txHash.toString());
|
|
1179
|
+
if (!buffer) {
|
|
1180
|
+
return [[], []];
|
|
1181
|
+
}
|
|
1182
|
+
const reader = BufferReader.asReader(buffer);
|
|
1183
|
+
// Skip the fixed-length header: blockHash + l2BlockNumber + txIndexInBlock + revertCode + txHash + transactionFee.
|
|
1184
|
+
reader.readBytes(32 + 4 + 4 + 1 + 32 + 32);
|
|
1185
|
+
const noteHashes = reader.readVectorUint8Prefix(Fr);
|
|
1186
|
+
const nullifiers = reader.readVectorUint8Prefix(Fr);
|
|
1187
|
+
return [noteHashes, nullifiers];
|
|
1188
|
+
}),
|
|
1189
|
+
);
|
|
1190
|
+
}
|
|
1191
|
+
|
|
925
1192
|
/**
|
|
926
1193
|
* Looks up which block deployed a particular contract.
|
|
927
1194
|
* @param contractAddress - The address of the contract to look up.
|
|
@@ -949,6 +1216,174 @@ export class BlockStore {
|
|
|
949
1216
|
return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
950
1217
|
}
|
|
951
1218
|
|
|
1219
|
+
/**
|
|
1220
|
+
* Resolves all five L2 chain tips (proposed, proposedCheckpoint, checkpointed, proven, finalized)
|
|
1221
|
+
* in a single read-only transaction so the snapshot is internally consistent. Each underlying
|
|
1222
|
+
* record is read at most once: latest block, latest confirmed checkpoint, and latest pending
|
|
1223
|
+
* checkpoint are each loaded directly (no separate "find the number, then look up data" hop),
|
|
1224
|
+
* the proven/finalized checkpoint singletons are read once and their storage entries are
|
|
1225
|
+
* reused if they coincide with the latest checkpoint, and per-tip block hashes are deduped
|
|
1226
|
+
* when two tips land on the same block (e.g. finalized == proven, or proposedCheckpoint falls
|
|
1227
|
+
* back to checkpointed when no pending checkpoint exists).
|
|
1228
|
+
*
|
|
1229
|
+
* The result is guaranteed to satisfy `finalized <= proven <= checkpointed <= proposed` (by
|
|
1230
|
+
* block number). Genesis is represented by `(INITIAL_L2_BLOCK_NUM - 1)` and the supplied
|
|
1231
|
+
* `genesisBlockHash`, paired with the synthetic genesis checkpoint id.
|
|
1232
|
+
*
|
|
1233
|
+
* @param genesisBlockHash - Block hash to report for the synthetic pre-initial block (used when
|
|
1234
|
+
* a tip is still at genesis).
|
|
1235
|
+
*/
|
|
1236
|
+
async getL2TipsData(genesisBlockHash: BlockHash): Promise<L2Tips> {
|
|
1237
|
+
return await this.db.transactionAsync(async () => {
|
|
1238
|
+
// Define genesis tips
|
|
1239
|
+
const genesisBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
1240
|
+
const genesisCheckpointNumber = CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
|
|
1241
|
+
const genesisBlockId = { number: genesisBlockNumber, hash: genesisBlockHash.toString() };
|
|
1242
|
+
const genesisCheckpointId = {
|
|
1243
|
+
number: genesisCheckpointNumber,
|
|
1244
|
+
hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
|
|
1245
|
+
};
|
|
1246
|
+
const genesisTip: L2TipId = { block: genesisBlockId, checkpoint: genesisCheckpointId };
|
|
1247
|
+
|
|
1248
|
+
// Load latest block and checkpoint entries
|
|
1249
|
+
const [latestBlockEntry] = await toArray(this.#blocks.entriesAsync({ reverse: true, limit: 1 }));
|
|
1250
|
+
const [proposedCheckpointEntry] = await toArray(
|
|
1251
|
+
this.#proposedCheckpoints.entriesAsync({ reverse: true, limit: 1 }),
|
|
1252
|
+
);
|
|
1253
|
+
const [latestCheckpointEntry] = await toArray(this.#checkpoints.entriesAsync({ reverse: true, limit: 1 }));
|
|
1254
|
+
const latestCheckpointNumber = latestCheckpointEntry
|
|
1255
|
+
? CheckpointNumber(latestCheckpointEntry[0])
|
|
1256
|
+
: genesisCheckpointNumber;
|
|
1257
|
+
|
|
1258
|
+
// Load proven and finalized checkpoint number pointers
|
|
1259
|
+
const [provenRaw, finalizedRaw] = await Promise.all([
|
|
1260
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
1261
|
+
this.#lastFinalizedCheckpoint.getAsync(),
|
|
1262
|
+
]);
|
|
1263
|
+
|
|
1264
|
+
// Clamp to enforce finalized <= proven <= checkpointed.
|
|
1265
|
+
const provenCheckpointNumber = CheckpointNumber(Math.min(provenRaw ?? 0, latestCheckpointNumber));
|
|
1266
|
+
const finalizedCheckpointNumber = CheckpointNumber(Math.min(finalizedRaw ?? 0, provenCheckpointNumber));
|
|
1267
|
+
|
|
1268
|
+
// Avoid loading the same checkpoint more than once
|
|
1269
|
+
const checkpointStorageCache = new Map<CheckpointNumber, CheckpointStorage>();
|
|
1270
|
+
if (latestCheckpointEntry) {
|
|
1271
|
+
checkpointStorageCache.set(CheckpointNumber(latestCheckpointEntry[0]), latestCheckpointEntry[1]);
|
|
1272
|
+
}
|
|
1273
|
+
const loadCheckpointStorage = async (n: CheckpointNumber): Promise<CheckpointStorage | undefined> => {
|
|
1274
|
+
if (n === 0) {
|
|
1275
|
+
return undefined;
|
|
1276
|
+
}
|
|
1277
|
+
if (!checkpointStorageCache.has(n)) {
|
|
1278
|
+
const checkpointStorage = await this.#checkpoints.getAsync(n);
|
|
1279
|
+
if (!checkpointStorage) {
|
|
1280
|
+
throw new CheckpointNotFoundError(n);
|
|
1281
|
+
}
|
|
1282
|
+
checkpointStorageCache.set(n, checkpointStorage);
|
|
1283
|
+
}
|
|
1284
|
+
return checkpointStorageCache.get(n)!;
|
|
1285
|
+
};
|
|
1286
|
+
|
|
1287
|
+
// Load proven and finalized checkpoint storage entries
|
|
1288
|
+
const provenCheckpoint = await loadCheckpointStorage(provenCheckpointNumber);
|
|
1289
|
+
const finalizedCheckpoint = await loadCheckpointStorage(finalizedCheckpointNumber);
|
|
1290
|
+
|
|
1291
|
+
// Avoid loading the same block hash multiple times when tips land on the same block
|
|
1292
|
+
const blockHashCache = new Map<number, string>();
|
|
1293
|
+
blockHashCache.set(genesisBlockNumber, genesisBlockHash.toString());
|
|
1294
|
+
if (latestBlockEntry) {
|
|
1295
|
+
blockHashCache.set(latestBlockEntry[0], BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString());
|
|
1296
|
+
}
|
|
1297
|
+
const loadBlockHash = async (n: BlockNumber): Promise<string> => {
|
|
1298
|
+
if (!blockHashCache.has(n)) {
|
|
1299
|
+
const blockStorage = await this.#blocks.getAsync(n);
|
|
1300
|
+
if (!blockStorage) {
|
|
1301
|
+
throw new BlockNotFoundError(n);
|
|
1302
|
+
}
|
|
1303
|
+
const blockHash = BlockHash.fromBuffer(blockStorage.blockHash).toString();
|
|
1304
|
+
blockHashCache.set(n, blockHash);
|
|
1305
|
+
}
|
|
1306
|
+
return blockHashCache.get(n)!;
|
|
1307
|
+
};
|
|
1308
|
+
|
|
1309
|
+
// Build proposed chain tip (this one has block only, no checkpoint)
|
|
1310
|
+
const proposedBlockId =
|
|
1311
|
+
latestBlockEntry === undefined
|
|
1312
|
+
? genesisBlockId
|
|
1313
|
+
: {
|
|
1314
|
+
number: BlockNumber(latestBlockEntry[0]),
|
|
1315
|
+
hash: BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString(),
|
|
1316
|
+
};
|
|
1317
|
+
|
|
1318
|
+
// Build other tips from checkpoint data, reading corresponding block data from the cache
|
|
1319
|
+
const buildTipFromCheckpoint = async (
|
|
1320
|
+
stored: ProposedCheckpointStorage | CheckpointStorage | undefined,
|
|
1321
|
+
): Promise<L2TipId> => {
|
|
1322
|
+
if (!stored) {
|
|
1323
|
+
return genesisTip;
|
|
1324
|
+
}
|
|
1325
|
+
const blockNumber = BlockNumber(stored.startBlock + stored.blockCount - 1);
|
|
1326
|
+
const blockHash = await loadBlockHash(blockNumber);
|
|
1327
|
+
const header = CheckpointHeader.fromBuffer(stored.header);
|
|
1328
|
+
return {
|
|
1329
|
+
block: { number: blockNumber, hash: blockHash },
|
|
1330
|
+
checkpoint: { number: CheckpointNumber(stored.checkpointNumber), hash: header.hash().toString() },
|
|
1331
|
+
};
|
|
1332
|
+
};
|
|
1333
|
+
|
|
1334
|
+
const checkpointedTip = await buildTipFromCheckpoint(latestCheckpointEntry?.[1]);
|
|
1335
|
+
const provenTip = await buildTipFromCheckpoint(provenCheckpoint);
|
|
1336
|
+
const finalizedTip = await buildTipFromCheckpoint(finalizedCheckpoint);
|
|
1337
|
+
|
|
1338
|
+
// Proposed checkpoint falls back to the checkpoint tip if it's not set. And if local storage is
|
|
1339
|
+
// inconsistent and the proposed checkpoint is behind the checkpointed tip, we patch that and
|
|
1340
|
+
// report the checkpointed tip as the proposed checkpoint to maintain the invariant.
|
|
1341
|
+
const proposedCheckpointTip =
|
|
1342
|
+
proposedCheckpointEntry === undefined || proposedCheckpointEntry[0] <= latestCheckpointNumber
|
|
1343
|
+
? checkpointedTip
|
|
1344
|
+
: await buildTipFromCheckpoint(proposedCheckpointEntry[1]);
|
|
1345
|
+
|
|
1346
|
+
// A checkpointed block past the latest stored block would mean a checkpoint
|
|
1347
|
+
// references blocks that aren't in blocks.
|
|
1348
|
+
if (proposedBlockId.number < checkpointedTip.block.number) {
|
|
1349
|
+
throw new Error(
|
|
1350
|
+
`Inconsistent block store: latest block ${proposedBlockId.number} is behind checkpointed block ${checkpointedTip.block.number}`,
|
|
1351
|
+
);
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
// Assert that checkpoint numbers are increasing
|
|
1355
|
+
if (
|
|
1356
|
+
finalizedTip.checkpoint.number > provenTip.checkpoint.number ||
|
|
1357
|
+
provenTip.checkpoint.number > checkpointedTip.checkpoint.number ||
|
|
1358
|
+
checkpointedTip.checkpoint.number > proposedCheckpointTip.checkpoint.number
|
|
1359
|
+
) {
|
|
1360
|
+
throw new Error(
|
|
1361
|
+
`Inconsistent checkpoint numbers in chain tips: finalized=${finalizedTip.checkpoint.number} proven=${provenTip.checkpoint.number} checkpointed=${checkpointedTip.checkpoint.number} proposed=${proposedCheckpointTip.checkpoint.number}`,
|
|
1362
|
+
);
|
|
1363
|
+
}
|
|
1364
|
+
|
|
1365
|
+
// Assert block numbers are increasing
|
|
1366
|
+
if (
|
|
1367
|
+
finalizedTip.block.number > provenTip.block.number ||
|
|
1368
|
+
provenTip.block.number > checkpointedTip.block.number ||
|
|
1369
|
+
checkpointedTip.block.number > proposedCheckpointTip.block.number ||
|
|
1370
|
+
proposedCheckpointTip.block.number > proposedBlockId.number
|
|
1371
|
+
) {
|
|
1372
|
+
throw new Error(
|
|
1373
|
+
`Inconsistent block numbers in chain tips: finalized=${finalizedTip.block.number} proven=${provenTip.block.number} checkpointed=${checkpointedTip.block.number} proposedCheckpoint=${proposedCheckpointTip.block.number} proposed=${proposedBlockId.number}`,
|
|
1374
|
+
);
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
return {
|
|
1378
|
+
proposed: proposedBlockId,
|
|
1379
|
+
proposedCheckpoint: proposedCheckpointTip,
|
|
1380
|
+
checkpointed: checkpointedTip,
|
|
1381
|
+
proven: provenTip,
|
|
1382
|
+
finalized: finalizedTip,
|
|
1383
|
+
};
|
|
1384
|
+
});
|
|
1385
|
+
}
|
|
1386
|
+
|
|
952
1387
|
/**
|
|
953
1388
|
* Gets the most recent L1 block processed.
|
|
954
1389
|
* @returns The L1 block that published the latest L2 block
|
|
@@ -961,14 +1396,62 @@ export class BlockStore {
|
|
|
961
1396
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
962
1397
|
}
|
|
963
1398
|
|
|
1399
|
+
/**
|
|
1400
|
+
* Adds a proposed checkpoint to the pending queue.
|
|
1401
|
+
* Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
|
|
1402
|
+
* confirmed and the highest pending checkpoint number.
|
|
1403
|
+
* Computes archive and checkpointOutHash from the stored blocks.
|
|
1404
|
+
*/
|
|
1405
|
+
async addProposedCheckpoint(proposed: ProposedCheckpointInput) {
|
|
1406
|
+
return await this.db.transactionAsync(async () => {
|
|
1407
|
+
const confirmed = await this.getLatestCheckpointNumber();
|
|
1408
|
+
const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
1409
|
+
const latestTip = CheckpointNumber(
|
|
1410
|
+
latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed,
|
|
1411
|
+
);
|
|
1412
|
+
|
|
1413
|
+
if (proposed.checkpointNumber !== latestTip + 1) {
|
|
1414
|
+
throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
|
|
1415
|
+
}
|
|
1416
|
+
|
|
1417
|
+
// Ensure the predecessor block (from pending or confirmed chain) exists
|
|
1418
|
+
const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
|
|
1419
|
+
const blocks: L2Block[] = [];
|
|
1420
|
+
for (let i = 0; i < proposed.blockCount; i++) {
|
|
1421
|
+
const block = await this.getBlock({ number: BlockNumber(proposed.startBlock + i) });
|
|
1422
|
+
if (!block) {
|
|
1423
|
+
throw new BlockNotFoundError(proposed.startBlock + i);
|
|
1424
|
+
}
|
|
1425
|
+
blocks.push(block);
|
|
1426
|
+
}
|
|
1427
|
+
this.validateCheckpointBlocks(blocks, previousBlock);
|
|
1428
|
+
|
|
1429
|
+
const archive = blocks[blocks.length - 1].archive;
|
|
1430
|
+
const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
|
|
1431
|
+
|
|
1432
|
+
await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
|
|
1433
|
+
header: proposed.header.toBuffer(),
|
|
1434
|
+
archive: archive.toBuffer(),
|
|
1435
|
+
checkpointOutHash: checkpointOutHash.toBuffer(),
|
|
1436
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
1437
|
+
startBlock: proposed.startBlock,
|
|
1438
|
+
blockCount: proposed.blockCount,
|
|
1439
|
+
totalManaUsed: proposed.totalManaUsed.toString(),
|
|
1440
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
|
|
1441
|
+
});
|
|
1442
|
+
});
|
|
1443
|
+
}
|
|
1444
|
+
|
|
964
1445
|
async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
1446
|
+
return await this.db.transactionAsync(async () => {
|
|
1447
|
+
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
1448
|
+
this.getLatestCheckpointNumber(),
|
|
1449
|
+
this.#lastProvenCheckpoint.getAsync(),
|
|
1450
|
+
]);
|
|
1451
|
+
return (provenCheckpointNumber ?? 0) > latestCheckpointNumber
|
|
1452
|
+
? latestCheckpointNumber
|
|
1453
|
+
: CheckpointNumber(provenCheckpointNumber ?? 0);
|
|
1454
|
+
});
|
|
972
1455
|
}
|
|
973
1456
|
|
|
974
1457
|
async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
@@ -976,6 +1459,22 @@ export class BlockStore {
|
|
|
976
1459
|
return result;
|
|
977
1460
|
}
|
|
978
1461
|
|
|
1462
|
+
async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1463
|
+
return await this.db.transactionAsync(async () => {
|
|
1464
|
+
const [provenCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
|
|
1465
|
+
this.getProvenCheckpointNumber(),
|
|
1466
|
+
this.#lastFinalizedCheckpoint.getAsync(),
|
|
1467
|
+
]);
|
|
1468
|
+
return (finalizedCheckpointNumber ?? 0) > provenCheckpointNumber
|
|
1469
|
+
? provenCheckpointNumber
|
|
1470
|
+
: CheckpointNumber(finalizedCheckpointNumber ?? 0);
|
|
1471
|
+
});
|
|
1472
|
+
}
|
|
1473
|
+
|
|
1474
|
+
setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
|
|
1475
|
+
return this.#lastFinalizedCheckpoint.set(checkpointNumber);
|
|
1476
|
+
}
|
|
1477
|
+
|
|
979
1478
|
#computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
|
|
980
1479
|
if (limit < 1) {
|
|
981
1480
|
throw new Error(`Invalid limit: ${limit}`);
|
|
@@ -1012,4 +1511,80 @@ export class BlockStore {
|
|
|
1012
1511
|
await this.#pendingChainValidationStatus.delete();
|
|
1013
1512
|
}
|
|
1014
1513
|
}
|
|
1514
|
+
|
|
1515
|
+
/** Records a rejected-checkpoint entry, keyed by its own archive root. */
|
|
1516
|
+
async addRejectedCheckpoint(entry: RejectedCheckpoint): Promise<void> {
|
|
1517
|
+
const archiveRootHex = entry.archiveRoot.toString();
|
|
1518
|
+
await this.#rejectedCheckpoints.set(archiveRootHex, {
|
|
1519
|
+
checkpointNumber: entry.checkpointNumber,
|
|
1520
|
+
archiveRoot: entry.archiveRoot.toBuffer(),
|
|
1521
|
+
parentArchiveRoot: entry.parentArchiveRoot.toBuffer(),
|
|
1522
|
+
slotNumber: entry.slotNumber,
|
|
1523
|
+
l1: entry.l1.toBuffer(),
|
|
1524
|
+
reason: entry.reason,
|
|
1525
|
+
});
|
|
1526
|
+
await this.#rejectedCheckpointsByNumber.set(entry.checkpointNumber, archiveRootHex);
|
|
1527
|
+
await this.advanceSynchedL1BlockNumber(entry.l1.blockNumber);
|
|
1528
|
+
}
|
|
1529
|
+
|
|
1530
|
+
/** Returns the rejected-checkpoint entry with the given archive root, or undefined if not present. */
|
|
1531
|
+
async getRejectedCheckpointByArchiveRoot(archiveRoot: Fr): Promise<RejectedCheckpoint | undefined> {
|
|
1532
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRoot.toString());
|
|
1533
|
+
return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
/** Returns the rejected-checkpoint entry recorded for the given checkpoint number, or undefined if none. */
|
|
1537
|
+
async getRejectedCheckpointByNumber(checkpointNumber: CheckpointNumber): Promise<RejectedCheckpoint | undefined> {
|
|
1538
|
+
const archiveRootHex = await this.#rejectedCheckpointsByNumber.getAsync(checkpointNumber);
|
|
1539
|
+
if (archiveRootHex === undefined) {
|
|
1540
|
+
return undefined;
|
|
1541
|
+
}
|
|
1542
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
|
|
1543
|
+
return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
/** Returns the highest checkpoint number recorded across all rejected entries, or `INITIAL_CHECKPOINT_NUMBER - 1` if none. */
|
|
1547
|
+
async getLatestRejectedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1548
|
+
const [latest] = await toArray(this.#rejectedCheckpointsByNumber.keysAsync({ reverse: true, limit: 1 }));
|
|
1549
|
+
return CheckpointNumber(latest ?? INITIAL_CHECKPOINT_NUMBER - 1);
|
|
1550
|
+
}
|
|
1551
|
+
|
|
1552
|
+
/** Removes a rejected-checkpoint entry by its archive root (used when an entry no longer matches L1). */
|
|
1553
|
+
async removeRejectedCheckpointByArchiveRoot(archiveRoot: Fr): Promise<void> {
|
|
1554
|
+
const archiveRootHex = archiveRoot.toString();
|
|
1555
|
+
const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
|
|
1556
|
+
await this.#rejectedCheckpoints.delete(archiveRootHex);
|
|
1557
|
+
if (stored) {
|
|
1558
|
+
// Only clear the by-number index if it still points at this archive root, so a distinct
|
|
1559
|
+
// entry that shares the checkpoint number (e.g. an L1 reorg replacement) is not dropped.
|
|
1560
|
+
const indexed = await this.#rejectedCheckpointsByNumber.getAsync(stored.checkpointNumber);
|
|
1561
|
+
if (indexed === archiveRootHex) {
|
|
1562
|
+
await this.#rejectedCheckpointsByNumber.delete(stored.checkpointNumber);
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
/**
|
|
1568
|
+
* Advances the stored last-synched L1 block number to `l1BlockNumber` only if it is strictly
|
|
1569
|
+
* greater than the current value. Use this whenever ingesting checkpoint-shaped data so the
|
|
1570
|
+
* sync pointer never walks backwards on out-of-order writes (e.g. an invalid checkpoint
|
|
1571
|
+
* advance followed by a valid-checkpoint commit landing at an earlier L1 block).
|
|
1572
|
+
*/
|
|
1573
|
+
private async advanceSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
|
|
1574
|
+
const current = await this.#lastSynchedL1Block.getAsync();
|
|
1575
|
+
if (current === undefined || l1BlockNumber > current) {
|
|
1576
|
+
await this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
|
|
1580
|
+
private rejectedCheckpointFromStorage(stored: RejectedCheckpointStorage): RejectedCheckpoint {
|
|
1581
|
+
return {
|
|
1582
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
1583
|
+
archiveRoot: Fr.fromBuffer(stored.archiveRoot),
|
|
1584
|
+
parentArchiveRoot: Fr.fromBuffer(stored.parentArchiveRoot),
|
|
1585
|
+
slotNumber: SlotNumber(stored.slotNumber),
|
|
1586
|
+
l1: L1PublishedData.fromBuffer(stored.l1),
|
|
1587
|
+
reason: stored.reason,
|
|
1588
|
+
};
|
|
1589
|
+
}
|
|
1015
1590
|
}
|