@aztec/archiver 0.0.1-commit.7035c9bd6 → 0.0.1-commit.71324e566
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 +12 -6
- package/dest/archiver.d.ts +6 -4
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +32 -14
- package/dest/config.js +1 -1
- package/dest/errors.d.ts +14 -2
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +18 -2
- package/dest/factory.d.ts +2 -2
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +7 -6
- package/dest/l1/calldata_retriever.d.ts +1 -1
- package/dest/l1/calldata_retriever.d.ts.map +1 -1
- package/dest/l1/calldata_retriever.js +2 -1
- package/dest/l1/data_retrieval.d.ts +3 -3
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +14 -15
- package/dest/modules/data_source_base.d.ts +4 -2
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +6 -0
- package/dest/modules/data_store_updater.d.ts +5 -7
- package/dest/modules/data_store_updater.d.ts.map +1 -1
- package/dest/modules/data_store_updater.js +56 -65
- package/dest/modules/instrumentation.d.ts +4 -2
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +17 -6
- package/dest/modules/l1_synchronizer.d.ts +3 -2
- package/dest/modules/l1_synchronizer.d.ts.map +1 -1
- package/dest/modules/l1_synchronizer.js +131 -124
- package/dest/modules/validation.d.ts +1 -1
- package/dest/modules/validation.d.ts.map +1 -1
- package/dest/modules/validation.js +2 -2
- package/dest/store/block_store.d.ts +39 -4
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +232 -63
- package/dest/store/contract_class_store.d.ts +2 -3
- package/dest/store/contract_class_store.d.ts.map +1 -1
- package/dest/store/contract_class_store.js +7 -67
- package/dest/store/contract_instance_store.d.ts +1 -1
- package/dest/store/contract_instance_store.d.ts.map +1 -1
- package/dest/store/contract_instance_store.js +6 -2
- package/dest/store/kv_archiver_store.d.ts +26 -16
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +29 -16
- package/dest/store/l2_tips_cache.d.ts +2 -1
- package/dest/store/l2_tips_cache.d.ts.map +1 -1
- package/dest/store/l2_tips_cache.js +27 -7
- package/dest/store/log_store.d.ts +1 -1
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +2 -4
- package/dest/store/message_store.d.ts +3 -3
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +9 -10
- package/dest/test/fake_l1_state.d.ts +9 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +41 -6
- 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 +7 -2
- package/dest/test/mock_l2_block_source.d.ts.map +1 -1
- package/dest/test/mock_l2_block_source.js +28 -3
- package/dest/test/noop_l1_archiver.d.ts +1 -1
- package/dest/test/noop_l1_archiver.d.ts.map +1 -1
- package/dest/test/noop_l1_archiver.js +0 -1
- package/package.json +13 -13
- package/src/archiver.ts +43 -19
- package/src/config.ts +1 -1
- package/src/errors.ts +30 -2
- package/src/factory.ts +12 -7
- package/src/l1/calldata_retriever.ts +2 -1
- package/src/l1/data_retrieval.ts +8 -12
- package/src/modules/data_source_base.ts +15 -1
- package/src/modules/data_store_updater.ts +68 -95
- package/src/modules/instrumentation.ts +19 -7
- package/src/modules/l1_synchronizer.ts +145 -151
- package/src/modules/validation.ts +2 -2
- package/src/store/block_store.ts +301 -74
- package/src/store/contract_class_store.ts +8 -106
- package/src/store/contract_instance_store.ts +8 -5
- package/src/store/kv_archiver_store.ts +48 -32
- package/src/store/l2_tips_cache.ts +58 -13
- package/src/store/log_store.ts +2 -5
- package/src/store/message_store.ts +10 -12
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +56 -7
- package/src/test/mock_l1_to_l2_message_source.ts +1 -0
- package/src/test/mock_l2_block_source.ts +37 -2
- package/src/test/noop_l1_archiver.ts +0 -1
package/src/store/block_store.ts
CHANGED
|
@@ -19,7 +19,15 @@ import {
|
|
|
19
19
|
deserializeValidateCheckpointResult,
|
|
20
20
|
serializeValidateCheckpointResult,
|
|
21
21
|
} from '@aztec/stdlib/block';
|
|
22
|
-
import {
|
|
22
|
+
import {
|
|
23
|
+
Checkpoint,
|
|
24
|
+
type CheckpointData,
|
|
25
|
+
type CommonCheckpointData,
|
|
26
|
+
L1PublishedData,
|
|
27
|
+
type ProposedCheckpointData,
|
|
28
|
+
type ProposedCheckpointInput,
|
|
29
|
+
PublishedCheckpoint,
|
|
30
|
+
} from '@aztec/stdlib/checkpoint';
|
|
23
31
|
import { type L1RollupConstants, getEpochAtSlot } from '@aztec/stdlib/epoch-helpers';
|
|
24
32
|
import { CheckpointHeader } from '@aztec/stdlib/rollup';
|
|
25
33
|
import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
|
|
@@ -44,6 +52,8 @@ import {
|
|
|
44
52
|
CheckpointNotFoundError,
|
|
45
53
|
CheckpointNumberNotSequentialError,
|
|
46
54
|
InitialCheckpointNumberNotSequentialError,
|
|
55
|
+
ProposedCheckpointNotSequentialError,
|
|
56
|
+
ProposedCheckpointStaleError,
|
|
47
57
|
} from '../errors.js';
|
|
48
58
|
|
|
49
59
|
export { TxReceipt, type TxEffect, type TxHash } from '@aztec/stdlib/tx';
|
|
@@ -58,17 +68,27 @@ type BlockStorage = {
|
|
|
58
68
|
indexWithinCheckpoint: number;
|
|
59
69
|
};
|
|
60
70
|
|
|
61
|
-
|
|
71
|
+
/** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
|
|
72
|
+
type CommonCheckpointStorage = {
|
|
62
73
|
header: Buffer;
|
|
63
74
|
archive: Buffer;
|
|
64
75
|
checkpointOutHash: Buffer;
|
|
65
76
|
checkpointNumber: number;
|
|
66
77
|
startBlock: number;
|
|
67
78
|
blockCount: number;
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
type CheckpointStorage = CommonCheckpointStorage & {
|
|
68
82
|
l1: Buffer;
|
|
69
83
|
attestations: Buffer[];
|
|
70
84
|
};
|
|
71
85
|
|
|
86
|
+
/** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
|
|
87
|
+
type ProposedCheckpointStorage = CommonCheckpointStorage & {
|
|
88
|
+
totalManaUsed: string;
|
|
89
|
+
feeAssetPriceModifier: string;
|
|
90
|
+
};
|
|
91
|
+
|
|
72
92
|
export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
|
|
73
93
|
|
|
74
94
|
/**
|
|
@@ -111,6 +131,9 @@ export class BlockStore {
|
|
|
111
131
|
/** Index mapping block archive to block number */
|
|
112
132
|
#blockArchiveIndex: AztecAsyncMap<string, number>;
|
|
113
133
|
|
|
134
|
+
/** Singleton: assumes max 1-deep pipeline. For deeper pipelining, replace with a map keyed by checkpoint number. */
|
|
135
|
+
#proposedCheckpoint: AztecAsyncSingleton<ProposedCheckpointStorage>;
|
|
136
|
+
|
|
114
137
|
#log = createLogger('archiver:block_store');
|
|
115
138
|
|
|
116
139
|
constructor(private db: AztecAsyncKVStore) {
|
|
@@ -126,6 +149,7 @@ export class BlockStore {
|
|
|
126
149
|
this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
|
|
127
150
|
this.#checkpoints = db.openMap('archiver_checkpoints');
|
|
128
151
|
this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
|
|
152
|
+
this.#proposedCheckpoint = db.openSingleton('proposed_checkpoint_data');
|
|
129
153
|
}
|
|
130
154
|
|
|
131
155
|
/**
|
|
@@ -160,7 +184,8 @@ export class BlockStore {
|
|
|
160
184
|
const blockLastArchive = block.header.lastArchive.root;
|
|
161
185
|
|
|
162
186
|
// Extract the latest block and checkpoint numbers
|
|
163
|
-
const previousBlockNumber = await this.
|
|
187
|
+
const previousBlockNumber = await this.getLatestL2BlockNumber();
|
|
188
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
164
189
|
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
165
190
|
|
|
166
191
|
// Verify we're not overwriting checkpointed blocks
|
|
@@ -179,9 +204,19 @@ export class BlockStore {
|
|
|
179
204
|
throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
|
|
180
205
|
}
|
|
181
206
|
|
|
182
|
-
// The same check as above but for checkpoints
|
|
183
|
-
|
|
184
|
-
|
|
207
|
+
// The same check as above but for checkpoints. Accept the block if either the confirmed
|
|
208
|
+
// checkpoint or the pending (locally validated but not yet confirmed) checkpoint matches.
|
|
209
|
+
const expectedCheckpointNumber = blockCheckpointNumber - 1;
|
|
210
|
+
if (
|
|
211
|
+
!opts.force &&
|
|
212
|
+
previousCheckpointNumber !== expectedCheckpointNumber &&
|
|
213
|
+
proposedCheckpointNumber !== expectedCheckpointNumber
|
|
214
|
+
) {
|
|
215
|
+
const [reported, source]: [CheckpointNumber, 'confirmed' | 'proposed'] =
|
|
216
|
+
proposedCheckpointNumber > previousCheckpointNumber
|
|
217
|
+
? [proposedCheckpointNumber, 'proposed']
|
|
218
|
+
: [previousCheckpointNumber, 'confirmed'];
|
|
219
|
+
throw new CheckpointNumberNotSequentialError(blockCheckpointNumber, reported, source);
|
|
185
220
|
}
|
|
186
221
|
|
|
187
222
|
// Extract the previous block if there is one and see if it is for the same checkpoint or not
|
|
@@ -217,7 +252,7 @@ export class BlockStore {
|
|
|
217
252
|
}
|
|
218
253
|
|
|
219
254
|
/**
|
|
220
|
-
* Append new
|
|
255
|
+
* Append new checkpoints to the store's list.
|
|
221
256
|
* @param checkpoints - The L2 checkpoints to be added to the store.
|
|
222
257
|
* @returns True if the operation is successful.
|
|
223
258
|
*/
|
|
@@ -227,37 +262,29 @@ export class BlockStore {
|
|
|
227
262
|
}
|
|
228
263
|
|
|
229
264
|
return await this.db.transactionAsync(async () => {
|
|
230
|
-
// Check that the checkpoint immediately before the first block to be added is present in the store.
|
|
231
265
|
const firstCheckpointNumber = checkpoints[0].checkpoint.number;
|
|
232
266
|
const previousCheckpointNumber = await this.getLatestCheckpointNumber();
|
|
233
267
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
// There should be a previous checkpoint
|
|
242
|
-
previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
243
|
-
if (previousCheckpointData === undefined) {
|
|
244
|
-
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
268
|
+
// Handle already-stored checkpoints at the start of the batch.
|
|
269
|
+
// This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
|
|
270
|
+
// We accept them if archives match (same content) and update their L1 metadata.
|
|
271
|
+
if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
|
|
272
|
+
checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
|
|
273
|
+
if (checkpoints.length === 0) {
|
|
274
|
+
return true;
|
|
245
275
|
}
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
// If we have a previous checkpoint then we need to get the previous block number
|
|
252
|
-
if (previousCheckpointData !== undefined) {
|
|
253
|
-
previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
|
|
254
|
-
previousBlock = await this.getBlock(previousBlockNumber);
|
|
255
|
-
if (previousBlock === undefined) {
|
|
256
|
-
// We should be able to get the required previous block
|
|
257
|
-
throw new BlockNotFoundError(previousBlockNumber);
|
|
276
|
+
// Re-check sequentiality after skipping
|
|
277
|
+
const newFirstNumber = checkpoints[0].checkpoint.number;
|
|
278
|
+
if (previousCheckpointNumber !== newFirstNumber - 1) {
|
|
279
|
+
throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
|
|
258
280
|
}
|
|
281
|
+
} else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
|
|
282
|
+
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
259
283
|
}
|
|
260
284
|
|
|
285
|
+
// Get the last block of the previous checkpoint for archive chaining
|
|
286
|
+
let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
|
|
287
|
+
|
|
261
288
|
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
262
289
|
let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
|
|
263
290
|
for (const checkpoint of checkpoints) {
|
|
@@ -273,42 +300,14 @@ export class BlockStore {
|
|
|
273
300
|
}
|
|
274
301
|
previousCheckpoint = checkpoint;
|
|
275
302
|
|
|
276
|
-
//
|
|
277
|
-
|
|
278
|
-
const block = checkpoint.checkpoint.blocks[i];
|
|
279
|
-
if (previousBlock) {
|
|
280
|
-
// The blocks should have a sequential block number
|
|
281
|
-
if (previousBlock.number !== block.number - 1) {
|
|
282
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
283
|
-
}
|
|
284
|
-
// If the blocks are for the same checkpoint then they should have sequential indexes
|
|
285
|
-
if (
|
|
286
|
-
previousBlock.checkpointNumber === block.checkpointNumber &&
|
|
287
|
-
previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1
|
|
288
|
-
) {
|
|
289
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
290
|
-
}
|
|
291
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
292
|
-
throw new BlockArchiveNotConsistentError(
|
|
293
|
-
block.number,
|
|
294
|
-
previousBlock.number,
|
|
295
|
-
block.header.lastArchive.root,
|
|
296
|
-
previousBlock.archive.root,
|
|
297
|
-
);
|
|
298
|
-
}
|
|
299
|
-
} else {
|
|
300
|
-
// No previous block, must be block 1 at checkpoint index 0
|
|
301
|
-
if (block.indexWithinCheckpoint !== 0) {
|
|
302
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
303
|
-
}
|
|
304
|
-
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
305
|
-
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
306
|
-
}
|
|
307
|
-
}
|
|
303
|
+
// Validate block sequencing, indexes, and archive chaining
|
|
304
|
+
this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
|
|
308
305
|
|
|
309
|
-
|
|
310
|
-
|
|
306
|
+
// Store every block in the database (may already exist, but L1 data is authoritative)
|
|
307
|
+
for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
|
|
308
|
+
await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
|
|
311
309
|
}
|
|
310
|
+
previousBlock = checkpoint.checkpoint.blocks.at(-1);
|
|
312
311
|
|
|
313
312
|
// Store the checkpoint in the database
|
|
314
313
|
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
@@ -326,11 +325,121 @@ export class BlockStore {
|
|
|
326
325
|
await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
|
|
327
326
|
}
|
|
328
327
|
|
|
328
|
+
// Clear the proposed checkpoint if any of the confirmed checkpoints match or supersede it
|
|
329
|
+
const lastConfirmedCheckpointNumber = checkpoints[checkpoints.length - 1].checkpoint.number;
|
|
330
|
+
await this.clearProposedCheckpointIfSuperseded(lastConfirmedCheckpointNumber);
|
|
331
|
+
|
|
329
332
|
await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
330
333
|
return true;
|
|
331
334
|
});
|
|
332
335
|
}
|
|
333
336
|
|
|
337
|
+
/**
|
|
338
|
+
* Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
|
|
339
|
+
* Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
|
|
340
|
+
*/
|
|
341
|
+
private async skipOrUpdateAlreadyStoredCheckpoints(
|
|
342
|
+
checkpoints: PublishedCheckpoint[],
|
|
343
|
+
latestStored: CheckpointNumber,
|
|
344
|
+
): Promise<PublishedCheckpoint[]> {
|
|
345
|
+
let i = 0;
|
|
346
|
+
for (; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++) {
|
|
347
|
+
const incoming = checkpoints[i];
|
|
348
|
+
const stored = await this.getCheckpointData(incoming.checkpoint.number);
|
|
349
|
+
if (!stored) {
|
|
350
|
+
// Should not happen if latestStored is correct, but be safe
|
|
351
|
+
break;
|
|
352
|
+
}
|
|
353
|
+
// Verify the checkpoint content matches (archive root)
|
|
354
|
+
if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
|
|
355
|
+
throw new Error(
|
|
356
|
+
`Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` +
|
|
357
|
+
`Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`,
|
|
358
|
+
);
|
|
359
|
+
}
|
|
360
|
+
// Update L1 metadata and attestations for the already-stored checkpoint
|
|
361
|
+
this.#log.warn(
|
|
362
|
+
`Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` +
|
|
363
|
+
`(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`,
|
|
364
|
+
);
|
|
365
|
+
await this.#checkpoints.set(incoming.checkpoint.number, {
|
|
366
|
+
header: incoming.checkpoint.header.toBuffer(),
|
|
367
|
+
archive: incoming.checkpoint.archive.toBuffer(),
|
|
368
|
+
checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
|
|
369
|
+
l1: incoming.l1.toBuffer(),
|
|
370
|
+
attestations: incoming.attestations.map(a => a.toBuffer()),
|
|
371
|
+
checkpointNumber: incoming.checkpoint.number,
|
|
372
|
+
startBlock: incoming.checkpoint.blocks[0].number,
|
|
373
|
+
blockCount: incoming.checkpoint.blocks.length,
|
|
374
|
+
});
|
|
375
|
+
// Update the sync point to reflect the new L1 block
|
|
376
|
+
await this.#lastSynchedL1Block.set(incoming.l1.blockNumber);
|
|
377
|
+
}
|
|
378
|
+
return checkpoints.slice(i);
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Gets the last block of the checkpoint before the given one.
|
|
383
|
+
* Returns undefined if there is no previous checkpoint (i.e. genesis).
|
|
384
|
+
*/
|
|
385
|
+
private async getPreviousCheckpointBlock(checkpointNumber: CheckpointNumber): Promise<L2Block | undefined> {
|
|
386
|
+
const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
|
|
387
|
+
if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
388
|
+
return undefined;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
const previousCheckpointData = await this.getCheckpointData(previousCheckpointNumber);
|
|
392
|
+
if (previousCheckpointData === undefined) {
|
|
393
|
+
throw new CheckpointNotFoundError(previousCheckpointNumber);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
const previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
|
|
397
|
+
const previousBlock = await this.getBlock(previousBlockNumber);
|
|
398
|
+
if (previousBlock === undefined) {
|
|
399
|
+
throw new BlockNotFoundError(previousBlockNumber);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
return previousBlock;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Validates that blocks are sequential, have correct indexes, and chain via archive roots.
|
|
407
|
+
* This is the same validation used for both confirmed checkpoints (addCheckpoints) and
|
|
408
|
+
* proposed checkpoints (setProposedCheckpoint).
|
|
409
|
+
*/
|
|
410
|
+
private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
|
|
411
|
+
for (const block of blocks) {
|
|
412
|
+
if (previousBlock) {
|
|
413
|
+
if (previousBlock.number !== block.number - 1) {
|
|
414
|
+
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
415
|
+
}
|
|
416
|
+
if (previousBlock.checkpointNumber === block.checkpointNumber) {
|
|
417
|
+
if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
|
|
418
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
419
|
+
}
|
|
420
|
+
} else if (block.indexWithinCheckpoint !== 0) {
|
|
421
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
422
|
+
}
|
|
423
|
+
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
424
|
+
throw new BlockArchiveNotConsistentError(
|
|
425
|
+
block.number,
|
|
426
|
+
previousBlock.number,
|
|
427
|
+
block.header.lastArchive.root,
|
|
428
|
+
previousBlock.archive.root,
|
|
429
|
+
);
|
|
430
|
+
}
|
|
431
|
+
} else {
|
|
432
|
+
if (block.indexWithinCheckpoint !== 0) {
|
|
433
|
+
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
434
|
+
}
|
|
435
|
+
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
436
|
+
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
previousBlock = block;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
334
443
|
private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
|
|
335
444
|
const blockHash = await block.hash();
|
|
336
445
|
|
|
@@ -423,6 +532,12 @@ export class BlockStore {
|
|
|
423
532
|
this.#log.debug(`Removed checkpoint ${c}`);
|
|
424
533
|
}
|
|
425
534
|
|
|
535
|
+
// Clear any proposed checkpoint that was orphaned by the removal (its base chain no longer exists)
|
|
536
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
537
|
+
if (proposedCheckpointNumber > checkpointNumber) {
|
|
538
|
+
await this.#proposedCheckpoint.delete();
|
|
539
|
+
}
|
|
540
|
+
|
|
426
541
|
return { blocksRemoved };
|
|
427
542
|
});
|
|
428
543
|
}
|
|
@@ -528,7 +643,7 @@ export class BlockStore {
|
|
|
528
643
|
const removedBlocks: L2Block[] = [];
|
|
529
644
|
|
|
530
645
|
// Get the latest block number to determine the range
|
|
531
|
-
const latestBlockNumber = await this.
|
|
646
|
+
const latestBlockNumber = await this.getLatestL2BlockNumber();
|
|
532
647
|
|
|
533
648
|
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
534
649
|
for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
|
|
@@ -561,13 +676,6 @@ export class BlockStore {
|
|
|
561
676
|
}
|
|
562
677
|
}
|
|
563
678
|
|
|
564
|
-
async getLatestBlockNumber(): Promise<BlockNumber> {
|
|
565
|
-
const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
566
|
-
return typeof latestBlocknumber === 'number'
|
|
567
|
-
? BlockNumber(latestBlocknumber)
|
|
568
|
-
: BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
569
|
-
}
|
|
570
|
-
|
|
571
679
|
async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
|
|
572
680
|
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
573
681
|
if (latestCheckpointNumber === undefined) {
|
|
@@ -576,6 +684,84 @@ export class BlockStore {
|
|
|
576
684
|
return CheckpointNumber(latestCheckpointNumber);
|
|
577
685
|
}
|
|
578
686
|
|
|
687
|
+
async hasProposedCheckpoint(): Promise<boolean> {
|
|
688
|
+
const proposed = await this.#proposedCheckpoint.getAsync();
|
|
689
|
+
return proposed !== undefined;
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
/** Deletes the proposed checkpoint from storage. */
|
|
693
|
+
async deleteProposedCheckpoint(): Promise<void> {
|
|
694
|
+
await this.#proposedCheckpoint.delete();
|
|
695
|
+
}
|
|
696
|
+
|
|
697
|
+
/** Clears the proposed checkpoint if the given confirmed checkpoint number supersedes it. */
|
|
698
|
+
async clearProposedCheckpointIfSuperseded(confirmedCheckpointNumber: CheckpointNumber): Promise<void> {
|
|
699
|
+
const proposedCheckpointNumber = await this.getProposedCheckpointNumber();
|
|
700
|
+
if (proposedCheckpointNumber <= confirmedCheckpointNumber) {
|
|
701
|
+
await this.#proposedCheckpoint.delete();
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/** Returns the proposed checkpoint data, or undefined if no proposed checkpoint exists. No fallback to confirmed. */
|
|
706
|
+
async getProposedCheckpointOnly(): Promise<ProposedCheckpointData | undefined> {
|
|
707
|
+
const stored = await this.#proposedCheckpoint.getAsync();
|
|
708
|
+
if (!stored) {
|
|
709
|
+
return undefined;
|
|
710
|
+
}
|
|
711
|
+
return this.convertToProposedCheckpointData(stored);
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
/**
|
|
715
|
+
* Gets the checkpoint at the proposed tip
|
|
716
|
+
* - pending checkpoint if it exists
|
|
717
|
+
* - fallsback to latest confirmed checkpoint otherwise
|
|
718
|
+
* @returns CommonCheckpointData
|
|
719
|
+
*/
|
|
720
|
+
async getProposedCheckpoint(): Promise<CommonCheckpointData | undefined> {
|
|
721
|
+
const stored = await this.#proposedCheckpoint.getAsync();
|
|
722
|
+
if (!stored) {
|
|
723
|
+
return this.getCheckpointData(await this.getLatestCheckpointNumber());
|
|
724
|
+
}
|
|
725
|
+
return this.convertToProposedCheckpointData(stored);
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
|
|
729
|
+
return {
|
|
730
|
+
checkpointNumber: CheckpointNumber(stored.checkpointNumber),
|
|
731
|
+
header: CheckpointHeader.fromBuffer(stored.header),
|
|
732
|
+
archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
|
|
733
|
+
checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
|
|
734
|
+
startBlock: BlockNumber(stored.startBlock),
|
|
735
|
+
blockCount: stored.blockCount,
|
|
736
|
+
totalManaUsed: BigInt(stored.totalManaUsed),
|
|
737
|
+
feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
/**
|
|
742
|
+
* Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
|
|
743
|
+
* @returns CheckpointNumber
|
|
744
|
+
*/
|
|
745
|
+
async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
|
|
746
|
+
const proposed = await this.getProposedCheckpoint();
|
|
747
|
+
if (!proposed) {
|
|
748
|
+
return await this.getLatestCheckpointNumber();
|
|
749
|
+
}
|
|
750
|
+
return CheckpointNumber(proposed.checkpointNumber);
|
|
751
|
+
}
|
|
752
|
+
|
|
753
|
+
/**
|
|
754
|
+
* Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
|
|
755
|
+
* @returns BlockNumber
|
|
756
|
+
*/
|
|
757
|
+
async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
|
|
758
|
+
const proposed = await this.getProposedCheckpoint();
|
|
759
|
+
if (!proposed) {
|
|
760
|
+
return await this.getCheckpointedL2BlockNumber();
|
|
761
|
+
}
|
|
762
|
+
return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
|
|
763
|
+
}
|
|
764
|
+
|
|
579
765
|
async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
|
|
580
766
|
const blockStorage = await this.#blocks.getAsync(number);
|
|
581
767
|
if (!blockStorage) {
|
|
@@ -790,7 +976,7 @@ export class BlockStore {
|
|
|
790
976
|
return {
|
|
791
977
|
header: BlockHeader.fromBuffer(blockStorage.header),
|
|
792
978
|
archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
|
|
793
|
-
blockHash:
|
|
979
|
+
blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
|
|
794
980
|
checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
|
|
795
981
|
indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
|
|
796
982
|
};
|
|
@@ -950,6 +1136,47 @@ export class BlockStore {
|
|
|
950
1136
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
951
1137
|
}
|
|
952
1138
|
|
|
1139
|
+
/** Sets the proposed checkpoint (not yet L1-confirmed). Only accepts confirmed + 1.
|
|
1140
|
+
* Computes archive and checkpointOutHash from the stored blocks. */
|
|
1141
|
+
async setProposedCheckpoint(proposed: ProposedCheckpointInput) {
|
|
1142
|
+
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
|
+
const confirmed = await this.getLatestCheckpointNumber();
|
|
1148
|
+
if (proposed.checkpointNumber !== confirmed + 1) {
|
|
1149
|
+
throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, confirmed);
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
// Ensure the previous checkpoint + blocks exist
|
|
1153
|
+
const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
|
|
1154
|
+
const blocks: L2Block[] = [];
|
|
1155
|
+
for (let i = 0; i < proposed.blockCount; i++) {
|
|
1156
|
+
const block = await this.getBlock(BlockNumber(proposed.startBlock + i));
|
|
1157
|
+
if (!block) {
|
|
1158
|
+
throw new BlockNotFoundError(proposed.startBlock + i);
|
|
1159
|
+
}
|
|
1160
|
+
blocks.push(block);
|
|
1161
|
+
}
|
|
1162
|
+
this.validateCheckpointBlocks(blocks, previousBlock);
|
|
1163
|
+
|
|
1164
|
+
const archive = blocks[blocks.length - 1].archive;
|
|
1165
|
+
const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
|
|
1166
|
+
|
|
1167
|
+
await this.#proposedCheckpoint.set({
|
|
1168
|
+
header: proposed.header.toBuffer(),
|
|
1169
|
+
archive: archive.toBuffer(),
|
|
1170
|
+
checkpointOutHash: checkpointOutHash.toBuffer(),
|
|
1171
|
+
checkpointNumber: proposed.checkpointNumber,
|
|
1172
|
+
startBlock: proposed.startBlock,
|
|
1173
|
+
blockCount: proposed.blockCount,
|
|
1174
|
+
totalManaUsed: proposed.totalManaUsed.toString(),
|
|
1175
|
+
feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
|
|
1176
|
+
});
|
|
1177
|
+
});
|
|
1178
|
+
}
|
|
1179
|
+
|
|
953
1180
|
async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
|
|
954
1181
|
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
955
1182
|
this.getLatestCheckpointNumber(),
|
|
@@ -2,14 +2,7 @@ import { Fr } from '@aztec/foundation/curves/bn254';
|
|
|
2
2
|
import { toArray } from '@aztec/foundation/iterable';
|
|
3
3
|
import { BufferReader, numToUInt8, serializeToBuffer } from '@aztec/foundation/serialize';
|
|
4
4
|
import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
|
|
5
|
-
import {
|
|
6
|
-
import type {
|
|
7
|
-
ContractClassPublic,
|
|
8
|
-
ContractClassPublicWithBlockNumber,
|
|
9
|
-
ExecutablePrivateFunctionWithMembershipProof,
|
|
10
|
-
UtilityFunctionWithMembershipProof,
|
|
11
|
-
} from '@aztec/stdlib/contract';
|
|
12
|
-
import { Vector } from '@aztec/stdlib/types';
|
|
5
|
+
import type { ContractClassPublic, ContractClassPublicWithBlockNumber } from '@aztec/stdlib/contract';
|
|
13
6
|
|
|
14
7
|
/**
|
|
15
8
|
* LMDB-based contract class storage for the archiver.
|
|
@@ -29,11 +22,15 @@ export class ContractClassStore {
|
|
|
29
22
|
blockNumber: number,
|
|
30
23
|
): Promise<void> {
|
|
31
24
|
await this.db.transactionAsync(async () => {
|
|
32
|
-
|
|
33
|
-
|
|
25
|
+
const key = contractClass.id.toString();
|
|
26
|
+
if (await this.#contractClasses.hasAsync(key)) {
|
|
27
|
+
throw new Error(`Contract class ${key} already exists, cannot add again at block ${blockNumber}`);
|
|
28
|
+
}
|
|
29
|
+
await this.#contractClasses.set(
|
|
30
|
+
key,
|
|
34
31
|
serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
|
|
35
32
|
);
|
|
36
|
-
await this.#bytecodeCommitments.
|
|
33
|
+
await this.#bytecodeCommitments.set(key, bytecodeCommitment.toBuffer());
|
|
37
34
|
});
|
|
38
35
|
}
|
|
39
36
|
|
|
@@ -60,37 +57,6 @@ export class ContractClassStore {
|
|
|
60
57
|
async getContractClassIds(): Promise<Fr[]> {
|
|
61
58
|
return (await toArray(this.#contractClasses.keysAsync())).map(key => Fr.fromHexString(key));
|
|
62
59
|
}
|
|
63
|
-
|
|
64
|
-
async addFunctions(
|
|
65
|
-
contractClassId: Fr,
|
|
66
|
-
newPrivateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
|
|
67
|
-
newUtilityFunctions: UtilityFunctionWithMembershipProof[],
|
|
68
|
-
): Promise<boolean> {
|
|
69
|
-
await this.db.transactionAsync(async () => {
|
|
70
|
-
const existingClassBuffer = await this.#contractClasses.getAsync(contractClassId.toString());
|
|
71
|
-
if (!existingClassBuffer) {
|
|
72
|
-
throw new Error(`Unknown contract class ${contractClassId} when adding private functions to store`);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const existingClass = deserializeContractClassPublic(existingClassBuffer);
|
|
76
|
-
const { privateFunctions: existingPrivateFns, utilityFunctions: existingUtilityFns } = existingClass;
|
|
77
|
-
|
|
78
|
-
const updatedClass: Omit<ContractClassPublicWithBlockNumber, 'id'> = {
|
|
79
|
-
...existingClass,
|
|
80
|
-
privateFunctions: [
|
|
81
|
-
...existingPrivateFns,
|
|
82
|
-
...newPrivateFunctions.filter(newFn => !existingPrivateFns.some(f => f.selector.equals(newFn.selector))),
|
|
83
|
-
],
|
|
84
|
-
utilityFunctions: [
|
|
85
|
-
...existingUtilityFns,
|
|
86
|
-
...newUtilityFunctions.filter(newFn => !existingUtilityFns.some(f => f.selector.equals(newFn.selector))),
|
|
87
|
-
],
|
|
88
|
-
};
|
|
89
|
-
await this.#contractClasses.set(contractClassId.toString(), serializeContractClassPublic(updatedClass));
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
return true;
|
|
93
|
-
}
|
|
94
60
|
}
|
|
95
61
|
|
|
96
62
|
function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWithBlockNumber, 'id'>): Buffer {
|
|
@@ -98,83 +64,19 @@ function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWit
|
|
|
98
64
|
contractClass.l2BlockNumber,
|
|
99
65
|
numToUInt8(contractClass.version),
|
|
100
66
|
contractClass.artifactHash,
|
|
101
|
-
contractClass.privateFunctions.length,
|
|
102
|
-
contractClass.privateFunctions.map(serializePrivateFunction),
|
|
103
|
-
contractClass.utilityFunctions.length,
|
|
104
|
-
contractClass.utilityFunctions.map(serializeUtilityFunction),
|
|
105
67
|
contractClass.packedBytecode.length,
|
|
106
68
|
contractClass.packedBytecode,
|
|
107
69
|
contractClass.privateFunctionsRoot,
|
|
108
70
|
);
|
|
109
71
|
}
|
|
110
72
|
|
|
111
|
-
function serializePrivateFunction(fn: ExecutablePrivateFunctionWithMembershipProof): Buffer {
|
|
112
|
-
return serializeToBuffer(
|
|
113
|
-
fn.selector,
|
|
114
|
-
fn.vkHash,
|
|
115
|
-
fn.bytecode.length,
|
|
116
|
-
fn.bytecode,
|
|
117
|
-
fn.functionMetadataHash,
|
|
118
|
-
fn.artifactMetadataHash,
|
|
119
|
-
fn.utilityFunctionsTreeRoot,
|
|
120
|
-
new Vector(fn.privateFunctionTreeSiblingPath),
|
|
121
|
-
fn.privateFunctionTreeLeafIndex,
|
|
122
|
-
new Vector(fn.artifactTreeSiblingPath),
|
|
123
|
-
fn.artifactTreeLeafIndex,
|
|
124
|
-
);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
function serializeUtilityFunction(fn: UtilityFunctionWithMembershipProof): Buffer {
|
|
128
|
-
return serializeToBuffer(
|
|
129
|
-
fn.selector,
|
|
130
|
-
fn.bytecode.length,
|
|
131
|
-
fn.bytecode,
|
|
132
|
-
fn.functionMetadataHash,
|
|
133
|
-
fn.artifactMetadataHash,
|
|
134
|
-
fn.privateFunctionsArtifactTreeRoot,
|
|
135
|
-
new Vector(fn.artifactTreeSiblingPath),
|
|
136
|
-
fn.artifactTreeLeafIndex,
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
73
|
function deserializeContractClassPublic(buffer: Buffer): Omit<ContractClassPublicWithBlockNumber, 'id'> {
|
|
141
74
|
const reader = BufferReader.asReader(buffer);
|
|
142
75
|
return {
|
|
143
76
|
l2BlockNumber: reader.readNumber(),
|
|
144
77
|
version: reader.readUInt8() as 1,
|
|
145
78
|
artifactHash: reader.readObject(Fr),
|
|
146
|
-
privateFunctions: reader.readVector({ fromBuffer: deserializePrivateFunction }),
|
|
147
|
-
utilityFunctions: reader.readVector({ fromBuffer: deserializeUtilityFunction }),
|
|
148
79
|
packedBytecode: reader.readBuffer(),
|
|
149
80
|
privateFunctionsRoot: reader.readObject(Fr),
|
|
150
81
|
};
|
|
151
82
|
}
|
|
152
|
-
|
|
153
|
-
function deserializePrivateFunction(buffer: Buffer | BufferReader): ExecutablePrivateFunctionWithMembershipProof {
|
|
154
|
-
const reader = BufferReader.asReader(buffer);
|
|
155
|
-
return {
|
|
156
|
-
selector: reader.readObject(FunctionSelector),
|
|
157
|
-
vkHash: reader.readObject(Fr),
|
|
158
|
-
bytecode: reader.readBuffer(),
|
|
159
|
-
functionMetadataHash: reader.readObject(Fr),
|
|
160
|
-
artifactMetadataHash: reader.readObject(Fr),
|
|
161
|
-
utilityFunctionsTreeRoot: reader.readObject(Fr),
|
|
162
|
-
privateFunctionTreeSiblingPath: reader.readVector(Fr),
|
|
163
|
-
privateFunctionTreeLeafIndex: reader.readNumber(),
|
|
164
|
-
artifactTreeSiblingPath: reader.readVector(Fr),
|
|
165
|
-
artifactTreeLeafIndex: reader.readNumber(),
|
|
166
|
-
};
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
function deserializeUtilityFunction(buffer: Buffer | BufferReader): UtilityFunctionWithMembershipProof {
|
|
170
|
-
const reader = BufferReader.asReader(buffer);
|
|
171
|
-
return {
|
|
172
|
-
selector: reader.readObject(FunctionSelector),
|
|
173
|
-
bytecode: reader.readBuffer(),
|
|
174
|
-
functionMetadataHash: reader.readObject(Fr),
|
|
175
|
-
artifactMetadataHash: reader.readObject(Fr),
|
|
176
|
-
privateFunctionsArtifactTreeRoot: reader.readObject(Fr),
|
|
177
|
-
artifactTreeSiblingPath: reader.readVector(Fr),
|
|
178
|
-
artifactTreeLeafIndex: reader.readNumber(),
|
|
179
|
-
};
|
|
180
|
-
}
|