@aztec/archiver 0.0.1-commit.8c0b8ff → 0.0.1-commit.8cb2d04d8
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 -5
- package/dest/archiver.d.ts.map +1 -1
- package/dest/archiver.js +11 -5
- package/dest/config.d.ts +3 -3
- package/dest/config.d.ts.map +1 -1
- package/dest/config.js +3 -2
- package/dest/errors.d.ts +28 -2
- package/dest/errors.d.ts.map +1 -1
- package/dest/errors.js +36 -2
- package/dest/factory.d.ts +2 -2
- package/dest/factory.d.ts.map +1 -1
- package/dest/factory.js +1 -3
- 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 +8 -5
- package/dest/l1/data_retrieval.d.ts.map +1 -1
- package/dest/l1/data_retrieval.js +26 -21
- package/dest/modules/data_source_base.d.ts +6 -4
- package/dest/modules/data_source_base.d.ts.map +1 -1
- package/dest/modules/data_source_base.js +10 -4
- 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 +14 -56
- package/dest/modules/instrumentation.d.ts +15 -2
- package/dest/modules/instrumentation.d.ts.map +1 -1
- package/dest/modules/instrumentation.js +27 -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 +143 -131
- 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 +38 -4
- package/dest/store/block_store.d.ts.map +1 -1
- package/dest/store/block_store.js +187 -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 +1 -65
- package/dest/store/kv_archiver_store.d.ts +28 -13
- package/dest/store/kv_archiver_store.d.ts.map +1 -1
- package/dest/store/kv_archiver_store.js +33 -14
- 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 +6 -3
- package/dest/store/log_store.d.ts.map +1 -1
- package/dest/store/log_store.js +47 -10
- package/dest/store/message_store.d.ts +5 -1
- package/dest/store/message_store.d.ts.map +1 -1
- package/dest/store/message_store.js +20 -8
- package/dest/test/fake_l1_state.d.ts +2 -1
- package/dest/test/fake_l1_state.d.ts.map +1 -1
- package/dest/test/fake_l1_state.js +36 -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 +19 -10
- package/src/config.ts +9 -2
- package/src/errors.ts +60 -2
- package/src/factory.ts +1 -3
- package/src/l1/calldata_retriever.ts +2 -1
- package/src/l1/data_retrieval.ts +25 -21
- package/src/modules/data_source_base.ts +23 -4
- package/src/modules/data_store_updater.ts +17 -83
- package/src/modules/instrumentation.ts +39 -7
- package/src/modules/l1_synchronizer.ts +166 -168
- package/src/modules/validation.ts +2 -2
- package/src/store/block_store.ts +243 -73
- package/src/store/contract_class_store.ts +1 -103
- package/src/store/kv_archiver_store.ts +52 -24
- package/src/store/l2_tips_cache.ts +58 -13
- package/src/store/log_store.ts +62 -20
- package/src/store/message_store.ts +26 -9
- package/src/structs/inbox_message.ts +1 -1
- package/src/test/fake_l1_state.ts +53 -9
- 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
|
*/
|
|
@@ -247,29 +282,8 @@ export class BlockStore {
|
|
|
247
282
|
throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
|
|
248
283
|
}
|
|
249
284
|
|
|
250
|
-
//
|
|
251
|
-
|
|
252
|
-
let previousCheckpointData: CheckpointData | undefined = undefined;
|
|
253
|
-
if (currentFirstCheckpointNumber - 1 !== INITIAL_CHECKPOINT_NUMBER - 1) {
|
|
254
|
-
// There should be a previous checkpoint
|
|
255
|
-
previousCheckpointData = await this.getCheckpointData(CheckpointNumber(currentFirstCheckpointNumber - 1));
|
|
256
|
-
if (previousCheckpointData === undefined) {
|
|
257
|
-
throw new CheckpointNotFoundError(CheckpointNumber(currentFirstCheckpointNumber - 1));
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
let previousBlockNumber: BlockNumber | undefined = undefined;
|
|
262
|
-
let previousBlock: L2Block | undefined = undefined;
|
|
263
|
-
|
|
264
|
-
// If we have a previous checkpoint then we need to get the previous block number
|
|
265
|
-
if (previousCheckpointData !== undefined) {
|
|
266
|
-
previousBlockNumber = BlockNumber(previousCheckpointData.startBlock + previousCheckpointData.blockCount - 1);
|
|
267
|
-
previousBlock = await this.getBlock(previousBlockNumber);
|
|
268
|
-
if (previousBlock === undefined) {
|
|
269
|
-
// We should be able to get the required previous block
|
|
270
|
-
throw new BlockNotFoundError(previousBlockNumber);
|
|
271
|
-
}
|
|
272
|
-
}
|
|
285
|
+
// Get the last block of the previous checkpoint for archive chaining
|
|
286
|
+
let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
|
|
273
287
|
|
|
274
288
|
// Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
|
|
275
289
|
let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
|
|
@@ -286,42 +300,14 @@ export class BlockStore {
|
|
|
286
300
|
}
|
|
287
301
|
previousCheckpoint = checkpoint;
|
|
288
302
|
|
|
289
|
-
//
|
|
290
|
-
|
|
291
|
-
const block = checkpoint.checkpoint.blocks[i];
|
|
292
|
-
if (previousBlock) {
|
|
293
|
-
// The blocks should have a sequential block number
|
|
294
|
-
if (previousBlock.number !== block.number - 1) {
|
|
295
|
-
throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
|
|
296
|
-
}
|
|
297
|
-
// If the blocks are for the same checkpoint then they should have sequential indexes
|
|
298
|
-
if (
|
|
299
|
-
previousBlock.checkpointNumber === block.checkpointNumber &&
|
|
300
|
-
previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1
|
|
301
|
-
) {
|
|
302
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
|
|
303
|
-
}
|
|
304
|
-
if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
|
|
305
|
-
throw new BlockArchiveNotConsistentError(
|
|
306
|
-
block.number,
|
|
307
|
-
previousBlock.number,
|
|
308
|
-
block.header.lastArchive.root,
|
|
309
|
-
previousBlock.archive.root,
|
|
310
|
-
);
|
|
311
|
-
}
|
|
312
|
-
} else {
|
|
313
|
-
// No previous block, must be block 1 at checkpoint index 0
|
|
314
|
-
if (block.indexWithinCheckpoint !== 0) {
|
|
315
|
-
throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
|
|
316
|
-
}
|
|
317
|
-
if (block.number !== INITIAL_L2_BLOCK_NUM) {
|
|
318
|
-
throw new BlockNumberNotSequentialError(block.number, undefined);
|
|
319
|
-
}
|
|
320
|
-
}
|
|
303
|
+
// Validate block sequencing, indexes, and archive chaining
|
|
304
|
+
this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
|
|
321
305
|
|
|
322
|
-
|
|
323
|
-
|
|
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);
|
|
324
309
|
}
|
|
310
|
+
previousBlock = checkpoint.checkpoint.blocks.at(-1);
|
|
325
311
|
|
|
326
312
|
// Store the checkpoint in the database
|
|
327
313
|
await this.#checkpoints.set(checkpoint.checkpoint.number, {
|
|
@@ -339,6 +325,10 @@ export class BlockStore {
|
|
|
339
325
|
await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
|
|
340
326
|
}
|
|
341
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
|
+
|
|
342
332
|
await this.#lastSynchedL1Block.set(checkpoints[checkpoints.length - 1].l1.blockNumber);
|
|
343
333
|
return true;
|
|
344
334
|
});
|
|
@@ -388,6 +378,68 @@ export class BlockStore {
|
|
|
388
378
|
return checkpoints.slice(i);
|
|
389
379
|
}
|
|
390
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
|
+
|
|
391
443
|
private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
|
|
392
444
|
const blockHash = await block.hash();
|
|
393
445
|
|
|
@@ -480,6 +532,12 @@ export class BlockStore {
|
|
|
480
532
|
this.#log.debug(`Removed checkpoint ${c}`);
|
|
481
533
|
}
|
|
482
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
|
+
|
|
483
541
|
return { blocksRemoved };
|
|
484
542
|
});
|
|
485
543
|
}
|
|
@@ -585,7 +643,7 @@ export class BlockStore {
|
|
|
585
643
|
const removedBlocks: L2Block[] = [];
|
|
586
644
|
|
|
587
645
|
// Get the latest block number to determine the range
|
|
588
|
-
const latestBlockNumber = await this.
|
|
646
|
+
const latestBlockNumber = await this.getLatestL2BlockNumber();
|
|
589
647
|
|
|
590
648
|
// Iterate from blockNumber + 1 to latestBlockNumber
|
|
591
649
|
for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
|
|
@@ -618,13 +676,6 @@ export class BlockStore {
|
|
|
618
676
|
}
|
|
619
677
|
}
|
|
620
678
|
|
|
621
|
-
async getLatestBlockNumber(): Promise<BlockNumber> {
|
|
622
|
-
const [latestBlocknumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
|
|
623
|
-
return typeof latestBlocknumber === 'number'
|
|
624
|
-
? BlockNumber(latestBlocknumber)
|
|
625
|
-
: BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
|
|
626
|
-
}
|
|
627
|
-
|
|
628
679
|
async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
|
|
629
680
|
const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
|
|
630
681
|
if (latestCheckpointNumber === undefined) {
|
|
@@ -633,6 +684,84 @@ export class BlockStore {
|
|
|
633
684
|
return CheckpointNumber(latestCheckpointNumber);
|
|
634
685
|
}
|
|
635
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
|
+
|
|
636
765
|
async getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
|
|
637
766
|
const blockStorage = await this.#blocks.getAsync(number);
|
|
638
767
|
if (!blockStorage) {
|
|
@@ -847,7 +976,7 @@ export class BlockStore {
|
|
|
847
976
|
return {
|
|
848
977
|
header: BlockHeader.fromBuffer(blockStorage.header),
|
|
849
978
|
archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
|
|
850
|
-
blockHash:
|
|
979
|
+
blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
|
|
851
980
|
checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
|
|
852
981
|
indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
|
|
853
982
|
};
|
|
@@ -1007,6 +1136,47 @@ export class BlockStore {
|
|
|
1007
1136
|
return this.#lastSynchedL1Block.set(l1BlockNumber);
|
|
1008
1137
|
}
|
|
1009
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
|
+
|
|
1010
1180
|
async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
|
|
1011
1181
|
const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
|
|
1012
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.
|
|
@@ -64,37 +57,6 @@ export class ContractClassStore {
|
|
|
64
57
|
async getContractClassIds(): Promise<Fr[]> {
|
|
65
58
|
return (await toArray(this.#contractClasses.keysAsync())).map(key => Fr.fromHexString(key));
|
|
66
59
|
}
|
|
67
|
-
|
|
68
|
-
async addFunctions(
|
|
69
|
-
contractClassId: Fr,
|
|
70
|
-
newPrivateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
|
|
71
|
-
newUtilityFunctions: UtilityFunctionWithMembershipProof[],
|
|
72
|
-
): Promise<boolean> {
|
|
73
|
-
await this.db.transactionAsync(async () => {
|
|
74
|
-
const existingClassBuffer = await this.#contractClasses.getAsync(contractClassId.toString());
|
|
75
|
-
if (!existingClassBuffer) {
|
|
76
|
-
throw new Error(`Unknown contract class ${contractClassId} when adding private functions to store`);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
const existingClass = deserializeContractClassPublic(existingClassBuffer);
|
|
80
|
-
const { privateFunctions: existingPrivateFns, utilityFunctions: existingUtilityFns } = existingClass;
|
|
81
|
-
|
|
82
|
-
const updatedClass: Omit<ContractClassPublicWithBlockNumber, 'id'> = {
|
|
83
|
-
...existingClass,
|
|
84
|
-
privateFunctions: [
|
|
85
|
-
...existingPrivateFns,
|
|
86
|
-
...newPrivateFunctions.filter(newFn => !existingPrivateFns.some(f => f.selector.equals(newFn.selector))),
|
|
87
|
-
],
|
|
88
|
-
utilityFunctions: [
|
|
89
|
-
...existingUtilityFns,
|
|
90
|
-
...newUtilityFunctions.filter(newFn => !existingUtilityFns.some(f => f.selector.equals(newFn.selector))),
|
|
91
|
-
],
|
|
92
|
-
};
|
|
93
|
-
await this.#contractClasses.set(contractClassId.toString(), serializeContractClassPublic(updatedClass));
|
|
94
|
-
});
|
|
95
|
-
|
|
96
|
-
return true;
|
|
97
|
-
}
|
|
98
60
|
}
|
|
99
61
|
|
|
100
62
|
function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWithBlockNumber, 'id'>): Buffer {
|
|
@@ -102,83 +64,19 @@ function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWit
|
|
|
102
64
|
contractClass.l2BlockNumber,
|
|
103
65
|
numToUInt8(contractClass.version),
|
|
104
66
|
contractClass.artifactHash,
|
|
105
|
-
contractClass.privateFunctions.length,
|
|
106
|
-
contractClass.privateFunctions.map(serializePrivateFunction),
|
|
107
|
-
contractClass.utilityFunctions.length,
|
|
108
|
-
contractClass.utilityFunctions.map(serializeUtilityFunction),
|
|
109
67
|
contractClass.packedBytecode.length,
|
|
110
68
|
contractClass.packedBytecode,
|
|
111
69
|
contractClass.privateFunctionsRoot,
|
|
112
70
|
);
|
|
113
71
|
}
|
|
114
72
|
|
|
115
|
-
function serializePrivateFunction(fn: ExecutablePrivateFunctionWithMembershipProof): Buffer {
|
|
116
|
-
return serializeToBuffer(
|
|
117
|
-
fn.selector,
|
|
118
|
-
fn.vkHash,
|
|
119
|
-
fn.bytecode.length,
|
|
120
|
-
fn.bytecode,
|
|
121
|
-
fn.functionMetadataHash,
|
|
122
|
-
fn.artifactMetadataHash,
|
|
123
|
-
fn.utilityFunctionsTreeRoot,
|
|
124
|
-
new Vector(fn.privateFunctionTreeSiblingPath),
|
|
125
|
-
fn.privateFunctionTreeLeafIndex,
|
|
126
|
-
new Vector(fn.artifactTreeSiblingPath),
|
|
127
|
-
fn.artifactTreeLeafIndex,
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
function serializeUtilityFunction(fn: UtilityFunctionWithMembershipProof): Buffer {
|
|
132
|
-
return serializeToBuffer(
|
|
133
|
-
fn.selector,
|
|
134
|
-
fn.bytecode.length,
|
|
135
|
-
fn.bytecode,
|
|
136
|
-
fn.functionMetadataHash,
|
|
137
|
-
fn.artifactMetadataHash,
|
|
138
|
-
fn.privateFunctionsArtifactTreeRoot,
|
|
139
|
-
new Vector(fn.artifactTreeSiblingPath),
|
|
140
|
-
fn.artifactTreeLeafIndex,
|
|
141
|
-
);
|
|
142
|
-
}
|
|
143
|
-
|
|
144
73
|
function deserializeContractClassPublic(buffer: Buffer): Omit<ContractClassPublicWithBlockNumber, 'id'> {
|
|
145
74
|
const reader = BufferReader.asReader(buffer);
|
|
146
75
|
return {
|
|
147
76
|
l2BlockNumber: reader.readNumber(),
|
|
148
77
|
version: reader.readUInt8() as 1,
|
|
149
78
|
artifactHash: reader.readObject(Fr),
|
|
150
|
-
privateFunctions: reader.readVector({ fromBuffer: deserializePrivateFunction }),
|
|
151
|
-
utilityFunctions: reader.readVector({ fromBuffer: deserializeUtilityFunction }),
|
|
152
79
|
packedBytecode: reader.readBuffer(),
|
|
153
80
|
privateFunctionsRoot: reader.readObject(Fr),
|
|
154
81
|
};
|
|
155
82
|
}
|
|
156
|
-
|
|
157
|
-
function deserializePrivateFunction(buffer: Buffer | BufferReader): ExecutablePrivateFunctionWithMembershipProof {
|
|
158
|
-
const reader = BufferReader.asReader(buffer);
|
|
159
|
-
return {
|
|
160
|
-
selector: reader.readObject(FunctionSelector),
|
|
161
|
-
vkHash: reader.readObject(Fr),
|
|
162
|
-
bytecode: reader.readBuffer(),
|
|
163
|
-
functionMetadataHash: reader.readObject(Fr),
|
|
164
|
-
artifactMetadataHash: reader.readObject(Fr),
|
|
165
|
-
utilityFunctionsTreeRoot: reader.readObject(Fr),
|
|
166
|
-
privateFunctionTreeSiblingPath: reader.readVector(Fr),
|
|
167
|
-
privateFunctionTreeLeafIndex: reader.readNumber(),
|
|
168
|
-
artifactTreeSiblingPath: reader.readVector(Fr),
|
|
169
|
-
artifactTreeLeafIndex: reader.readNumber(),
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
function deserializeUtilityFunction(buffer: Buffer | BufferReader): UtilityFunctionWithMembershipProof {
|
|
174
|
-
const reader = BufferReader.asReader(buffer);
|
|
175
|
-
return {
|
|
176
|
-
selector: reader.readObject(FunctionSelector),
|
|
177
|
-
bytecode: reader.readBuffer(),
|
|
178
|
-
functionMetadataHash: reader.readObject(Fr),
|
|
179
|
-
artifactMetadataHash: reader.readObject(Fr),
|
|
180
|
-
privateFunctionsArtifactTreeRoot: reader.readObject(Fr),
|
|
181
|
-
artifactTreeSiblingPath: reader.readVector(Fr),
|
|
182
|
-
artifactTreeLeafIndex: reader.readNumber(),
|
|
183
|
-
};
|
|
184
|
-
}
|