@aztec/archiver 0.0.1-commit.fffb133c → 0.0.1-dev

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.
Files changed (100) hide show
  1. package/dest/archiver.d.ts +9 -5
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +87 -111
  4. package/dest/errors.d.ts +7 -9
  5. package/dest/errors.d.ts.map +1 -1
  6. package/dest/errors.js +9 -14
  7. package/dest/factory.d.ts +4 -3
  8. package/dest/factory.d.ts.map +1 -1
  9. package/dest/factory.js +32 -26
  10. package/dest/index.d.ts +2 -1
  11. package/dest/index.d.ts.map +1 -1
  12. package/dest/index.js +1 -0
  13. package/dest/l1/bin/retrieve-calldata.js +36 -33
  14. package/dest/l1/calldata_retriever.d.ts +73 -50
  15. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  16. package/dest/l1/calldata_retriever.js +190 -259
  17. package/dest/l1/data_retrieval.d.ts +4 -7
  18. package/dest/l1/data_retrieval.d.ts.map +1 -1
  19. package/dest/l1/data_retrieval.js +10 -14
  20. package/dest/l1/spire_proposer.d.ts +5 -5
  21. package/dest/l1/spire_proposer.d.ts.map +1 -1
  22. package/dest/l1/spire_proposer.js +9 -17
  23. package/dest/l1/validate_trace.d.ts +6 -3
  24. package/dest/l1/validate_trace.d.ts.map +1 -1
  25. package/dest/l1/validate_trace.js +13 -9
  26. package/dest/modules/data_source_base.d.ts +13 -8
  27. package/dest/modules/data_source_base.d.ts.map +1 -1
  28. package/dest/modules/data_source_base.js +29 -73
  29. package/dest/modules/data_store_updater.d.ts +22 -7
  30. package/dest/modules/data_store_updater.d.ts.map +1 -1
  31. package/dest/modules/data_store_updater.js +114 -40
  32. package/dest/modules/instrumentation.d.ts +4 -2
  33. package/dest/modules/instrumentation.d.ts.map +1 -1
  34. package/dest/modules/instrumentation.js +26 -12
  35. package/dest/modules/l1_synchronizer.d.ts +5 -8
  36. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  37. package/dest/modules/l1_synchronizer.js +42 -13
  38. package/dest/store/block_store.d.ts +38 -29
  39. package/dest/store/block_store.d.ts.map +1 -1
  40. package/dest/store/block_store.js +294 -85
  41. package/dest/store/contract_class_store.d.ts +1 -1
  42. package/dest/store/contract_class_store.d.ts.map +1 -1
  43. package/dest/store/contract_class_store.js +27 -7
  44. package/dest/store/contract_instance_store.d.ts +1 -1
  45. package/dest/store/contract_instance_store.d.ts.map +1 -1
  46. package/dest/store/contract_instance_store.js +17 -2
  47. package/dest/store/kv_archiver_store.d.ts +40 -18
  48. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  49. package/dest/store/kv_archiver_store.js +44 -16
  50. package/dest/store/l2_tips_cache.d.ts +18 -0
  51. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  52. package/dest/store/l2_tips_cache.js +20 -0
  53. package/dest/store/log_store.d.ts +1 -1
  54. package/dest/store/log_store.d.ts.map +1 -1
  55. package/dest/store/log_store.js +125 -56
  56. package/dest/store/message_store.js +1 -1
  57. package/dest/test/fake_l1_state.d.ts +20 -1
  58. package/dest/test/fake_l1_state.d.ts.map +1 -1
  59. package/dest/test/fake_l1_state.js +97 -20
  60. package/dest/test/index.js +3 -1
  61. package/dest/test/mock_archiver.d.ts +1 -1
  62. package/dest/test/mock_archiver.d.ts.map +1 -1
  63. package/dest/test/mock_archiver.js +3 -2
  64. package/dest/test/mock_l2_block_source.d.ts +25 -9
  65. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  66. package/dest/test/mock_l2_block_source.js +133 -87
  67. package/dest/test/mock_structs.d.ts +6 -2
  68. package/dest/test/mock_structs.d.ts.map +1 -1
  69. package/dest/test/mock_structs.js +20 -6
  70. package/dest/test/noop_l1_archiver.d.ts +26 -0
  71. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  72. package/dest/test/noop_l1_archiver.js +72 -0
  73. package/package.json +14 -13
  74. package/src/archiver.ts +110 -132
  75. package/src/errors.ts +10 -24
  76. package/src/factory.ts +47 -23
  77. package/src/index.ts +1 -0
  78. package/src/l1/README.md +25 -68
  79. package/src/l1/bin/retrieve-calldata.ts +46 -39
  80. package/src/l1/calldata_retriever.ts +249 -379
  81. package/src/l1/data_retrieval.ts +7 -17
  82. package/src/l1/spire_proposer.ts +7 -15
  83. package/src/l1/validate_trace.ts +24 -6
  84. package/src/modules/data_source_base.ts +59 -98
  85. package/src/modules/data_store_updater.ts +124 -43
  86. package/src/modules/instrumentation.ts +24 -12
  87. package/src/modules/l1_synchronizer.ts +49 -17
  88. package/src/store/block_store.ts +359 -115
  89. package/src/store/contract_class_store.ts +27 -7
  90. package/src/store/contract_instance_store.ts +19 -5
  91. package/src/store/kv_archiver_store.ts +71 -25
  92. package/src/store/l2_tips_cache.ts +27 -0
  93. package/src/store/log_store.ts +186 -57
  94. package/src/store/message_store.ts +1 -1
  95. package/src/test/fake_l1_state.ts +125 -21
  96. package/src/test/index.ts +3 -0
  97. package/src/test/mock_archiver.ts +3 -2
  98. package/src/test/mock_l2_block_source.ts +179 -87
  99. package/src/test/mock_structs.ts +42 -12
  100. package/src/test/noop_l1_archiver.ts +115 -0
@@ -2,6 +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 { isProtocolContractClass } from '@aztec/protocol-contracts';
5
6
  import { FunctionSelector } from '@aztec/stdlib/abi';
6
7
  import type {
7
8
  ContractClassPublic,
@@ -28,18 +29,37 @@ export class ContractClassStore {
28
29
  bytecodeCommitment: Fr,
29
30
  blockNumber: number,
30
31
  ): Promise<void> {
31
- await this.#contractClasses.setIfNotExists(
32
- contractClass.id.toString(),
33
- serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
34
- );
35
- await this.#bytecodeCommitments.setIfNotExists(contractClass.id.toString(), bytecodeCommitment.toBuffer());
32
+ await this.db.transactionAsync(async () => {
33
+ const key = contractClass.id.toString();
34
+ if (await this.#contractClasses.hasAsync(key)) {
35
+ // Protocol contracts are preloaded at block 0, so a later on-chain (re-)publish of a bundled
36
+ // protocol class id is valid and must be a no-op. Keep the existing block-0 entry untouched
37
+ // (do not bump its block number) so it survives reorgs of the publishing block.
38
+ if (isProtocolContractClass(contractClass.id)) {
39
+ return;
40
+ }
41
+ throw new Error(`Contract class ${key} already exists, cannot add again at block ${blockNumber}`);
42
+ }
43
+ await this.#contractClasses.set(
44
+ key,
45
+ serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
46
+ );
47
+ await this.#bytecodeCommitments.set(key, bytecodeCommitment.toBuffer());
48
+ });
36
49
  }
37
50
 
38
51
  async deleteContractClasses(contractClass: ContractClassPublic, blockNumber: number): Promise<void> {
52
+ // Protocol contracts are preloaded at block 0 and must never be deleted, even when the block that
53
+ // (re-)published them on-chain is unwound by a reorg.
54
+ if (isProtocolContractClass(contractClass.id)) {
55
+ return;
56
+ }
39
57
  const restoredContractClass = await this.#contractClasses.getAsync(contractClass.id.toString());
40
58
  if (restoredContractClass && deserializeContractClassPublic(restoredContractClass).l2BlockNumber >= blockNumber) {
41
- await this.#contractClasses.delete(contractClass.id.toString());
42
- await this.#bytecodeCommitments.delete(contractClass.id.toString());
59
+ await this.db.transactionAsync(async () => {
60
+ await this.#contractClasses.delete(contractClass.id.toString());
61
+ await this.#bytecodeCommitments.delete(contractClass.id.toString());
62
+ });
43
63
  }
44
64
  }
45
65
 
@@ -1,5 +1,6 @@
1
1
  import type { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
3
+ import { isProtocolContract } from '@aztec/protocol-contracts';
3
4
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
5
  import {
5
6
  type ContractInstanceUpdateWithAddress,
@@ -27,15 +28,28 @@ export class ContractInstanceStore {
27
28
 
28
29
  addContractInstance(contractInstance: ContractInstanceWithAddress, blockNumber: number): Promise<void> {
29
30
  return this.db.transactionAsync(async () => {
30
- await this.#contractInstances.set(
31
- contractInstance.address.toString(),
32
- new SerializableContractInstance(contractInstance).toBuffer(),
33
- );
34
- await this.#contractInstancePublishedAt.set(contractInstance.address.toString(), blockNumber);
31
+ const key = contractInstance.address.toString();
32
+ if (await this.#contractInstances.hasAsync(key)) {
33
+ // Protocol contracts are preloaded at block 0, so a later on-chain (re-)publish of a bundled
34
+ // protocol instance is valid and must be a no-op. Keep the existing block-0 entry untouched.
35
+ if (isProtocolContract(contractInstance.address)) {
36
+ return;
37
+ }
38
+ throw new Error(
39
+ `Contract instance at ${key} already exists (deployed at block ${await this.#contractInstancePublishedAt.getAsync(key)}), cannot add again at block ${blockNumber}`,
40
+ );
41
+ }
42
+ await this.#contractInstances.set(key, new SerializableContractInstance(contractInstance).toBuffer());
43
+ await this.#contractInstancePublishedAt.set(key, blockNumber);
35
44
  });
36
45
  }
37
46
 
38
47
  deleteContractInstance(contractInstance: ContractInstanceWithAddress): Promise<void> {
48
+ // Protocol contracts are preloaded at block 0 and must never be deleted, even when the block that
49
+ // (re-)published them on-chain is unwound by a reorg.
50
+ if (isProtocolContract(contractInstance.address)) {
51
+ return Promise.resolve();
52
+ }
39
53
  return this.db.transactionAsync(async () => {
40
54
  await this.#contractInstances.delete(contractInstance.address.toString());
41
55
  await this.#contractInstancePublishedAt.delete(contractInstance.address.toString());
@@ -6,10 +6,17 @@ import { createLogger } from '@aztec/foundation/log';
6
6
  import type { AztecAsyncKVStore, CustomRange, StoreSize } from '@aztec/kv-store';
7
7
  import { FunctionSelector } from '@aztec/stdlib/abi';
8
8
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
9
- import { CheckpointedL2Block, L2Block, L2BlockHash, type ValidateCheckpointResult } from '@aztec/stdlib/block';
10
- import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
9
+ import {
10
+ type BlockData,
11
+ BlockHash,
12
+ CheckpointedL2Block,
13
+ L2Block,
14
+ type ValidateCheckpointResult,
15
+ } from '@aztec/stdlib/block';
16
+ import type { CheckpointData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
11
17
  import type {
12
18
  ContractClassPublic,
19
+ ContractClassPublicWithCommitment,
13
20
  ContractDataSource,
14
21
  ContractInstanceUpdateWithAddress,
15
22
  ContractInstanceWithAddress,
@@ -25,7 +32,7 @@ import type { UInt64 } from '@aztec/stdlib/types';
25
32
  import { join } from 'path';
26
33
 
27
34
  import type { InboxMessage } from '../structs/inbox_message.js';
28
- import { BlockStore, type CheckpointData, type RemoveCheckpointsResult } from './block_store.js';
35
+ import { BlockStore, type RemoveCheckpointsResult } from './block_store.js';
29
36
  import { ContractClassStore } from './contract_class_store.js';
30
37
  import { ContractInstanceStore } from './contract_instance_store.js';
31
38
  import { LogStore } from './log_store.js';
@@ -65,15 +72,19 @@ export class KVArchiverDataStore implements ContractDataSource {
65
72
  constructor(
66
73
  private db: AztecAsyncKVStore,
67
74
  logsMaxPageSize: number = 1000,
68
- l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
69
75
  ) {
70
- this.#blockStore = new BlockStore(db, l1Constants);
76
+ this.#blockStore = new BlockStore(db);
71
77
  this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize);
72
78
  this.#messageStore = new MessageStore(db);
73
79
  this.#contractClassStore = new ContractClassStore(db);
74
80
  this.#contractInstanceStore = new ContractInstanceStore(db);
75
81
  }
76
82
 
83
+ /** Returns the underlying block store. Used by L2TipsCache. */
84
+ get blockStore(): BlockStore {
85
+ return this.#blockStore;
86
+ }
87
+
77
88
  /** Opens a new transaction to the underlying store and runs all operations within it. */
78
89
  public transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
79
90
  return this.db.transactionAsync(callback);
@@ -157,19 +168,14 @@ export class KVArchiverDataStore implements ContractDataSource {
157
168
 
158
169
  /**
159
170
  * Add new contract classes from an L2 block to the store's list.
160
- * @param data - List of contract classes to be added.
161
- * @param bytecodeCommitments - Bytecode commitments for the contract classes.
171
+ * @param data - List of contract classes (with bytecode commitments) to be added.
162
172
  * @param blockNumber - Number of the L2 block the contracts were registered in.
163
173
  * @returns True if the operation is successful.
164
174
  */
165
- async addContractClasses(
166
- data: ContractClassPublic[],
167
- bytecodeCommitments: Fr[],
168
- blockNumber: BlockNumber,
169
- ): Promise<boolean> {
175
+ async addContractClasses(data: ContractClassPublicWithCommitment[], blockNumber: BlockNumber): Promise<boolean> {
170
176
  return (
171
177
  await Promise.all(
172
- data.map((c, i) => this.#contractClassStore.addContractClass(c, bytecodeCommitments[i], blockNumber)),
178
+ data.map(c => this.#contractClassStore.addContractClass(c, c.publicBytecodeCommitment, blockNumber)),
173
179
  )
174
180
  ).every(Boolean);
175
181
  }
@@ -235,14 +241,14 @@ export class KVArchiverDataStore implements ContractDataSource {
235
241
  }
236
242
 
237
243
  /**
238
- * Append new proposed blocks to the store's list.
239
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
244
+ * Append a new proposed block to the store.
245
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
240
246
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
241
- * @param blocks - The proposed L2 blocks to be added to the store.
247
+ * @param block - The proposed L2 block to be added to the store.
242
248
  * @returns True if the operation is successful.
243
249
  */
244
- addProposedBlocks(blocks: L2Block[], opts: { force?: boolean; checkpointNumber?: number } = {}): Promise<boolean> {
245
- return this.#blockStore.addProposedBlocks(blocks, opts);
250
+ addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
251
+ return this.#blockStore.addProposedBlock(block, opts);
246
252
  }
247
253
 
248
254
  /**
@@ -291,7 +297,7 @@ export class KVArchiverDataStore implements ContractDataSource {
291
297
  * Returns the block for the given hash, or undefined if not exists.
292
298
  * @param blockHash - The block hash to return.
293
299
  */
294
- getCheckpointedBlockByHash(blockHash: Fr): Promise<CheckpointedL2Block | undefined> {
300
+ getCheckpointedBlockByHash(blockHash: BlockHash): Promise<CheckpointedL2Block | undefined> {
295
301
  return this.#blockStore.getCheckpointedBlockByHash(blockHash);
296
302
  }
297
303
  /**
@@ -312,8 +318,8 @@ export class KVArchiverDataStore implements ContractDataSource {
312
318
  * Returns the block for the given hash, or undefined if not exists.
313
319
  * @param blockHash - The block hash to return.
314
320
  */
315
- getBlockByHash(blockHash: Fr): Promise<L2Block | undefined> {
316
- return this.#blockStore.getBlockByHash(L2BlockHash.fromField(blockHash));
321
+ getBlockByHash(blockHash: BlockHash): Promise<L2Block | undefined> {
322
+ return this.#blockStore.getBlockByHash(blockHash);
317
323
  }
318
324
  /**
319
325
  * Returns the block for the given archive root, or undefined if not exists.
@@ -357,8 +363,8 @@ export class KVArchiverDataStore implements ContractDataSource {
357
363
  * Returns the block header for the given hash, or undefined if not exists.
358
364
  * @param blockHash - The block hash to return.
359
365
  */
360
- getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
361
- return this.#blockStore.getBlockHeaderByHash(L2BlockHash.fromField(blockHash));
366
+ getBlockHeaderByHash(blockHash: BlockHash): Promise<BlockHeader | undefined> {
367
+ return this.#blockStore.getBlockHeaderByHash(blockHash);
362
368
  }
363
369
 
364
370
  /**
@@ -369,6 +375,22 @@ export class KVArchiverDataStore implements ContractDataSource {
369
375
  return this.#blockStore.getBlockHeaderByArchive(archive);
370
376
  }
371
377
 
378
+ /**
379
+ * Gets block metadata (without tx data) by block number.
380
+ * @param blockNumber - The block number to return.
381
+ */
382
+ getBlockData(blockNumber: BlockNumber): Promise<BlockData | undefined> {
383
+ return this.#blockStore.getBlockData(blockNumber);
384
+ }
385
+
386
+ /**
387
+ * Gets block metadata (without tx data) by archive root.
388
+ * @param archive - The archive root to return.
389
+ */
390
+ getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
391
+ return this.#blockStore.getBlockDataByArchive(archive);
392
+ }
393
+
372
394
  /**
373
395
  * Gets a tx effect.
374
396
  * @param txHash - The hash of the tx corresponding to the tx effect.
@@ -383,8 +405,11 @@ export class KVArchiverDataStore implements ContractDataSource {
383
405
  * @param txHash - The hash of a tx we try to get the receipt for.
384
406
  * @returns The requested tx receipt (or undefined if not found).
385
407
  */
386
- getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
387
- return this.#blockStore.getSettledTxReceipt(txHash);
408
+ getSettledTxReceipt(
409
+ txHash: TxHash,
410
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
411
+ ): Promise<TxReceipt | undefined> {
412
+ return this.#blockStore.getSettledTxReceipt(txHash, l1Constants);
388
413
  }
389
414
 
390
415
  /**
@@ -515,6 +540,22 @@ export class KVArchiverDataStore implements ContractDataSource {
515
540
  await this.#blockStore.setProvenCheckpointNumber(checkpointNumber);
516
541
  }
517
542
 
543
+ /**
544
+ * Gets the number of the latest finalized checkpoint processed.
545
+ * @returns The number of the latest finalized checkpoint processed.
546
+ */
547
+ getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
548
+ return this.#blockStore.getFinalizedCheckpointNumber();
549
+ }
550
+
551
+ /**
552
+ * Stores the number of the latest finalized checkpoint processed.
553
+ * @param checkpointNumber - The number of the latest finalized checkpoint processed.
554
+ */
555
+ async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
556
+ await this.#blockStore.setFinalizedCheckpointNumber(checkpointNumber);
557
+ }
558
+
518
559
  async setBlockSynchedL1BlockNumber(l1BlockNumber: bigint) {
519
560
  await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
520
561
  }
@@ -618,6 +659,11 @@ export class KVArchiverDataStore implements ContractDataSource {
618
659
  return this.#blockStore.getCheckpointData(checkpointNumber);
619
660
  }
620
661
 
662
+ /** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */
663
+ getCheckpointDataForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointData[]> {
664
+ return this.#blockStore.getCheckpointDataForSlotRange(startSlot, endSlot);
665
+ }
666
+
621
667
  /**
622
668
  * Gets all blocks that have the given slot number.
623
669
  * @param slotNumber - The slot number to search for.
@@ -0,0 +1,27 @@
1
+ import { GENESIS_BLOCK_HEADER_HASH } from '@aztec/constants';
2
+ import type { L2Tips } from '@aztec/stdlib/block';
3
+
4
+ import type { BlockStore } from './block_store.js';
5
+
6
+ /**
7
+ * In-memory cache for L2 chain tips (proposed, checkpointed, proven, finalized).
8
+ * Populated from the BlockStore on first access, then kept up-to-date by the ArchiverDataStoreUpdater.
9
+ * Refresh calls should happen after the store transaction that mutates block data has committed,
10
+ * so the cache loads from committed state and is never replaced if the writer aborts.
11
+ */
12
+ export class L2TipsCache {
13
+ #tipsPromise: Promise<L2Tips> | undefined;
14
+
15
+ constructor(private blockStore: BlockStore) {}
16
+
17
+ /** Returns the cached L2 tips. Loads from the block store on first call. */
18
+ public getL2Tips(): Promise<L2Tips> {
19
+ return (this.#tipsPromise ??= this.blockStore.getL2TipsData(GENESIS_BLOCK_HEADER_HASH));
20
+ }
21
+
22
+ /** Reloads the L2 tips from the block store. Should be called after the writer transaction has committed. */
23
+ public async refresh(): Promise<void> {
24
+ this.#tipsPromise = this.blockStore.getL2TipsData(GENESIS_BLOCK_HEADER_HASH);
25
+ await this.#tipsPromise;
26
+ }
27
+ }