@aztec/archiver 0.0.1-commit.e558bd1c → 0.0.1-commit.e5a3663dd

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 (126) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +26 -15
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +161 -153
  5. package/dest/config.d.ts +5 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +16 -4
  8. package/dest/errors.d.ts +61 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +88 -14
  11. package/dest/factory.d.ts +6 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +40 -32
  14. package/dest/index.d.ts +11 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +10 -2
  17. package/dest/l1/bin/retrieve-calldata.js +32 -28
  18. package/dest/l1/calldata_retriever.d.ts +74 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +197 -260
  21. package/dest/l1/data_retrieval.d.ts +26 -17
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +42 -47
  24. package/dest/l1/spire_proposer.d.ts +5 -5
  25. package/dest/l1/spire_proposer.d.ts.map +1 -1
  26. package/dest/l1/spire_proposer.js +9 -17
  27. package/dest/l1/validate_historical_logs.d.ts +23 -0
  28. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  29. package/dest/l1/validate_historical_logs.js +108 -0
  30. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  31. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  32. package/dest/modules/contract_data_source_adapter.js +42 -0
  33. package/dest/modules/data_source_base.d.ts +27 -14
  34. package/dest/modules/data_source_base.d.ts.map +1 -1
  35. package/dest/modules/data_source_base.js +98 -125
  36. package/dest/modules/data_store_updater.d.ts +37 -17
  37. package/dest/modules/data_store_updater.d.ts.map +1 -1
  38. package/dest/modules/data_store_updater.js +155 -112
  39. package/dest/modules/instrumentation.d.ts +21 -3
  40. package/dest/modules/instrumentation.d.ts.map +1 -1
  41. package/dest/modules/instrumentation.js +41 -8
  42. package/dest/modules/l1_synchronizer.d.ts +13 -11
  43. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  44. package/dest/modules/l1_synchronizer.js +355 -182
  45. package/dest/modules/validation.d.ts +4 -3
  46. package/dest/modules/validation.d.ts.map +1 -1
  47. package/dest/modules/validation.js +6 -6
  48. package/dest/store/block_store.d.ts +107 -31
  49. package/dest/store/block_store.d.ts.map +1 -1
  50. package/dest/store/block_store.js +477 -141
  51. package/dest/store/contract_class_store.d.ts +17 -4
  52. package/dest/store/contract_class_store.d.ts.map +1 -1
  53. package/dest/store/contract_class_store.js +24 -68
  54. package/dest/store/contract_instance_store.d.ts +28 -1
  55. package/dest/store/contract_instance_store.d.ts.map +1 -1
  56. package/dest/store/contract_instance_store.js +37 -2
  57. package/dest/store/data_stores.d.ts +68 -0
  58. package/dest/store/data_stores.d.ts.map +1 -0
  59. package/dest/store/data_stores.js +50 -0
  60. package/dest/store/function_names_cache.d.ts +17 -0
  61. package/dest/store/function_names_cache.d.ts.map +1 -0
  62. package/dest/store/function_names_cache.js +30 -0
  63. package/dest/store/l2_tips_cache.d.ts +20 -0
  64. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  65. package/dest/store/l2_tips_cache.js +109 -0
  66. package/dest/store/log_store.d.ts +6 -3
  67. package/dest/store/log_store.d.ts.map +1 -1
  68. package/dest/store/log_store.js +95 -20
  69. package/dest/store/message_store.d.ts +11 -1
  70. package/dest/store/message_store.d.ts.map +1 -1
  71. package/dest/store/message_store.js +51 -9
  72. package/dest/test/fake_l1_state.d.ts +25 -1
  73. package/dest/test/fake_l1_state.d.ts.map +1 -1
  74. package/dest/test/fake_l1_state.js +166 -32
  75. package/dest/test/mock_archiver.d.ts +1 -1
  76. package/dest/test/mock_archiver.d.ts.map +1 -1
  77. package/dest/test/mock_archiver.js +3 -2
  78. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  79. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  80. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  81. package/dest/test/mock_l2_block_source.d.ts +35 -5
  82. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  83. package/dest/test/mock_l2_block_source.js +182 -89
  84. package/dest/test/mock_structs.d.ts +4 -1
  85. package/dest/test/mock_structs.d.ts.map +1 -1
  86. package/dest/test/mock_structs.js +13 -1
  87. package/dest/test/noop_l1_archiver.d.ts +7 -4
  88. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  89. package/dest/test/noop_l1_archiver.js +14 -8
  90. package/package.json +13 -13
  91. package/src/archiver.ts +199 -174
  92. package/src/config.ts +23 -2
  93. package/src/errors.ts +133 -22
  94. package/src/factory.ts +53 -30
  95. package/src/index.ts +18 -2
  96. package/src/l1/README.md +25 -68
  97. package/src/l1/bin/retrieve-calldata.ts +40 -27
  98. package/src/l1/calldata_retriever.ts +261 -379
  99. package/src/l1/data_retrieval.ts +58 -69
  100. package/src/l1/spire_proposer.ts +7 -15
  101. package/src/l1/validate_historical_logs.ts +140 -0
  102. package/src/modules/contract_data_source_adapter.ts +59 -0
  103. package/src/modules/data_source_base.ts +142 -144
  104. package/src/modules/data_store_updater.ts +187 -141
  105. package/src/modules/instrumentation.ts +56 -9
  106. package/src/modules/l1_synchronizer.ts +463 -218
  107. package/src/modules/validation.ts +10 -9
  108. package/src/store/block_store.ts +587 -177
  109. package/src/store/contract_class_store.ts +31 -103
  110. package/src/store/contract_instance_store.ts +51 -5
  111. package/src/store/data_stores.ts +108 -0
  112. package/src/store/function_names_cache.ts +37 -0
  113. package/src/store/l2_tips_cache.ts +134 -0
  114. package/src/store/log_store.ts +128 -32
  115. package/src/store/message_store.ts +60 -10
  116. package/src/structs/inbox_message.ts +1 -1
  117. package/src/test/fake_l1_state.ts +213 -42
  118. package/src/test/mock_archiver.ts +3 -2
  119. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  120. package/src/test/mock_l2_block_source.ts +230 -82
  121. package/src/test/mock_structs.ts +20 -6
  122. package/src/test/noop_l1_archiver.ts +16 -8
  123. package/dest/store/kv_archiver_store.d.ts +0 -340
  124. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  125. package/dest/store/kv_archiver_store.js +0 -446
  126. package/src/store/kv_archiver_store.ts +0 -639
@@ -1,30 +1,28 @@
1
1
  import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
2
- import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import { filterAsync } from '@aztec/foundation/collection';
3
3
  import { createLogger } from '@aztec/foundation/log';
4
- import {
5
- ContractClassPublishedEvent,
6
- PrivateFunctionBroadcastedEvent,
7
- UtilityFunctionBroadcastedEvent,
8
- } from '@aztec/protocol-contracts/class-registry';
4
+ import { ContractClassPublishedEvent } from '@aztec/protocol-contracts/class-registry';
9
5
  import {
10
6
  ContractInstancePublishedEvent,
11
7
  ContractInstanceUpdatedEvent,
12
8
  } from '@aztec/protocol-contracts/instance-registry';
13
- import type { L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
14
- import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
9
+ import type { CommitteeAttestation, L2Block, ValidateCheckpointResult } from '@aztec/stdlib/block';
10
+ import {
11
+ type L1PublishedData,
12
+ type ProposedCheckpointInput,
13
+ type PublishedCheckpoint,
14
+ validateCheckpoint,
15
+ } from '@aztec/stdlib/checkpoint';
15
16
  import {
16
- type ExecutablePrivateFunctionWithMembershipProof,
17
- type UtilityFunctionWithMembershipProof,
18
- computePublicBytecodeCommitment,
19
- isValidPrivateFunctionMembershipProof,
20
- isValidUtilityFunctionMembershipProof,
17
+ type ContractClassPublicWithCommitment,
18
+ computeContractAddressFromInstance,
19
+ computeContractClassId,
21
20
  } from '@aztec/stdlib/contract';
22
21
  import type { ContractClassLog, PrivateLog, PublicLog } from '@aztec/stdlib/logs';
23
22
  import type { UInt64 } from '@aztec/stdlib/types';
24
23
 
25
- import groupBy from 'lodash.groupby';
26
-
27
- import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
24
+ import type { ArchiverDataStores } from '../store/data_stores.js';
25
+ import type { L2TipsCache } from '../store/l2_tips_cache.js';
28
26
 
29
27
  /** Operation type for contract data updates. */
30
28
  enum Operation {
@@ -44,75 +42,121 @@ type ReconcileCheckpointsResult = {
44
42
  export class ArchiverDataStoreUpdater {
45
43
  private readonly log = createLogger('archiver:store_updater');
46
44
 
47
- constructor(private store: KVArchiverDataStore) {}
45
+ constructor(
46
+ private stores: ArchiverDataStores,
47
+ private l2TipsCache?: L2TipsCache,
48
+ private opts: { rollupManaLimit?: number } = {},
49
+ ) {}
48
50
 
49
51
  /**
50
- * Adds proposed blocks to the store with contract class/instance extraction from logs.
51
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
52
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
53
- * and individually broadcasted functions from the block logs.
52
+ * Adds a proposed block to the store with contract class/instance extraction from logs.
53
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
54
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the block logs.
54
55
  *
55
- * @param blocks - The proposed L2 blocks to add.
56
+ * @param block - The proposed L2 block to add.
56
57
  * @param pendingChainValidationStatus - Optional validation status to set.
57
58
  * @returns True if the operation is successful.
58
59
  */
59
- public addProposedBlocks(
60
- blocks: L2Block[],
60
+ public async addProposedBlock(
61
+ block: L2Block,
61
62
  pendingChainValidationStatus?: ValidateCheckpointResult,
62
63
  ): Promise<boolean> {
63
- return this.store.transactionAsync(async () => {
64
- await this.store.addProposedBlocks(blocks);
64
+ const result = await this.stores.db.transactionAsync(async () => {
65
+ await this.stores.blocks.addProposedBlock(block);
65
66
 
66
67
  const opResults = await Promise.all([
67
68
  // Update the pending chain validation status if provided
68
- pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
69
- // Add any logs emitted during the retrieved blocks
70
- this.store.addLogs(blocks),
71
- // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
72
- ...blocks.map(block => this.addContractDataToDb(block)),
69
+ pendingChainValidationStatus &&
70
+ this.stores.blocks.setPendingChainValidationStatus(pendingChainValidationStatus),
71
+ // Add any logs emitted during the retrieved block
72
+ this.stores.logs.addLogs([block]),
73
+ // Unroll all logs emitted during the retrieved block and extract any contract classes and instances from it
74
+ this.addContractDataToDb(block),
73
75
  ]);
74
76
 
77
+ await this.l2TipsCache?.refresh();
75
78
  return opResults.every(Boolean);
76
79
  });
80
+ return result;
77
81
  }
78
82
 
79
83
  /**
80
84
  * Reconciles local blocks with incoming checkpoints from L1.
81
85
  * Adds new checkpoints to the store with contract class/instance extraction from logs.
82
86
  * Prunes any local blocks that conflict with checkpoint data (by comparing archive roots).
83
- * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events,
84
- * and individually broadcasted functions from the checkpoint block logs.
87
+ * Extracts ContractClassPublished, ContractInstancePublished, ContractInstanceUpdated events from the checkpoint block logs.
88
+ * If `promoteProposed` is supplied, the proposed-checkpoint promotion runs inside the same transaction
89
+ * as the added checkpoints so both updates are applied atomically.
85
90
  *
86
- * @param checkpoints - The published checkpoints to add.
91
+ * @param checkpoints - The published checkpoints to add (excluding any being promoted from proposed).
87
92
  * @param pendingChainValidationStatus - Optional validation status to set.
93
+ * @param promoteProposed - Optional promotion of the current proposed checkpoint (fast path when blocks are already local).
88
94
  * @returns Result with information about any pruned blocks.
89
95
  */
90
- public addCheckpoints(
96
+ public async addCheckpoints(
91
97
  checkpoints: PublishedCheckpoint[],
92
98
  pendingChainValidationStatus?: ValidateCheckpointResult,
99
+ promoteProposed?: {
100
+ l1: L1PublishedData;
101
+ attestations: CommitteeAttestation[];
102
+ checkpoint: PublishedCheckpoint;
103
+ },
104
+ evictProposedFrom?: CheckpointNumber,
93
105
  ): Promise<ReconcileCheckpointsResult> {
94
- return this.store.transactionAsync(async () => {
106
+ for (const checkpoint of checkpoints) {
107
+ validateCheckpoint(checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
108
+ }
109
+ if (promoteProposed) {
110
+ validateCheckpoint(promoteProposed.checkpoint.checkpoint, { rollupManaLimit: this.opts?.rollupManaLimit });
111
+ }
112
+
113
+ const result = await this.stores.db.transactionAsync(async () => {
95
114
  // Before adding checkpoints, check for conflicts with local blocks if any
96
115
  const { prunedBlocks, lastAlreadyInsertedBlockNumber } = await this.pruneMismatchingLocalBlocks(checkpoints);
97
116
 
98
- await this.store.addCheckpoints(checkpoints);
117
+ await this.stores.blocks.addCheckpoints(checkpoints);
99
118
 
100
- // Filter out blocks that were already inserted via addProposedBlocks() to avoid duplicating logs/contract data
119
+ // Filter out blocks that were already inserted via addProposedBlock() to avoid duplicating logs/contract data
101
120
  const newBlocks = checkpoints
102
121
  .flatMap((ch: PublishedCheckpoint) => ch.checkpoint.blocks)
103
122
  .filter(b => lastAlreadyInsertedBlockNumber === undefined || b.number > lastAlreadyInsertedBlockNumber);
104
123
 
105
124
  await Promise.all([
106
125
  // Update the pending chain validation status if provided
107
- pendingChainValidationStatus && this.store.setPendingChainValidationStatus(pendingChainValidationStatus),
126
+ pendingChainValidationStatus &&
127
+ this.stores.blocks.setPendingChainValidationStatus(pendingChainValidationStatus),
108
128
  // Add any logs emitted during the retrieved blocks
109
- this.store.addLogs(newBlocks),
129
+ this.stores.logs.addLogs(newBlocks),
110
130
  // Unroll all logs emitted during the retrieved blocks and extract any contract classes and instances from them
111
131
  ...newBlocks.map(block => this.addContractDataToDb(block)),
132
+ // Promote the proposed checkpoint if requested (uses explicit checkpoint number)
133
+ promoteProposed
134
+ ? this.stores.blocks.promoteProposedToCheckpointed(
135
+ promoteProposed.checkpoint.checkpoint.number,
136
+ promoteProposed.l1,
137
+ promoteProposed.attestations,
138
+ promoteProposed.checkpoint.checkpoint.archive.root,
139
+ )
140
+ : undefined,
141
+ // Evict pending checkpoints that diverged from what L1 mined
142
+ evictProposedFrom !== undefined
143
+ ? this.stores.blocks.evictProposedCheckpointsFrom(evictProposedFrom)
144
+ : undefined,
112
145
  ]);
113
146
 
147
+ await this.l2TipsCache?.refresh();
114
148
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
115
149
  });
150
+ return result;
151
+ }
152
+
153
+ public async addProposedCheckpoint(proposedCheckpoint: ProposedCheckpointInput) {
154
+ const result = await this.stores.db.transactionAsync(async () => {
155
+ await this.stores.blocks.addProposedCheckpoint(proposedCheckpoint);
156
+ await this.l2TipsCache?.refresh();
157
+ });
158
+
159
+ return result;
116
160
  }
117
161
 
118
162
  /**
@@ -123,8 +167,8 @@ export class ArchiverDataStoreUpdater {
123
167
  */
124
168
  private async pruneMismatchingLocalBlocks(checkpoints: PublishedCheckpoint[]): Promise<ReconcileCheckpointsResult> {
125
169
  const [lastCheckpointedBlockNumber, lastBlockNumber] = await Promise.all([
126
- this.store.getCheckpointedL2BlockNumber(),
127
- this.store.getLatestBlockNumber(),
170
+ this.stores.blocks.getCheckpointedL2BlockNumber(),
171
+ this.stores.blocks.getLatestL2BlockNumber(),
128
172
  ]);
129
173
 
130
174
  // Exit early if there are no local uncheckpointed blocks
@@ -133,7 +177,7 @@ export class ArchiverDataStoreUpdater {
133
177
  }
134
178
 
135
179
  // Get all uncheckpointed local blocks
136
- const uncheckpointedLocalBlocks = await this.store.getBlocks(
180
+ const uncheckpointedLocalBlocks = await this.stores.blocks.getBlocks(
137
181
  BlockNumber.add(lastCheckpointedBlockNumber, 1),
138
182
  lastBlockNumber - lastCheckpointedBlockNumber,
139
183
  );
@@ -165,7 +209,7 @@ export class ArchiverDataStoreUpdater {
165
209
  this.log.verbose(`Block number ${blockNumber} already inserted and matches checkpoint`, blockInfos);
166
210
  lastAlreadyInsertedBlockNumber = blockNumber;
167
211
  } else {
168
- this.log.warn(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
212
+ this.log.info(`Conflict detected at block ${blockNumber} between checkpointed and local block`, blockInfos);
169
213
  const prunedBlocks = await this.removeBlocksAfter(BlockNumber(blockNumber - 1));
170
214
  return { prunedBlocks, lastAlreadyInsertedBlockNumber };
171
215
  }
@@ -197,18 +241,25 @@ export class ArchiverDataStoreUpdater {
197
241
  * @returns The removed blocks.
198
242
  * @throws Error if any block to be removed is checkpointed.
199
243
  */
200
- public removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
201
- return this.store.transactionAsync(async () => {
244
+ public async removeUncheckpointedBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
245
+ const result = await this.stores.db.transactionAsync(async () => {
202
246
  // Verify we're only removing uncheckpointed blocks
203
- const lastCheckpointedBlockNumber = await this.store.getCheckpointedL2BlockNumber();
247
+ const lastCheckpointedBlockNumber = await this.stores.blocks.getCheckpointedL2BlockNumber();
204
248
  if (blockNumber < lastCheckpointedBlockNumber) {
205
249
  throw new Error(
206
250
  `Cannot remove blocks after ${blockNumber} because checkpointed blocks exist up to ${lastCheckpointedBlockNumber}`,
207
251
  );
208
252
  }
209
253
 
210
- return await this.removeBlocksAfter(blockNumber);
254
+ const result = await this.removeBlocksAfter(blockNumber);
255
+
256
+ // Clear all pending proposed checkpoints since their blocks have been pruned
257
+ await this.stores.blocks.deleteProposedCheckpoints();
258
+
259
+ await this.l2TipsCache?.refresh();
260
+ return result;
211
261
  });
262
+ return result;
212
263
  }
213
264
 
214
265
  /**
@@ -217,11 +268,11 @@ export class ArchiverDataStoreUpdater {
217
268
  */
218
269
  private async removeBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
219
270
  // First get the blocks to be removed so we can clean up contract data
220
- const removedBlocks = await this.store.removeBlocksAfter(blockNumber);
271
+ const removedBlocks = await this.stores.blocks.removeBlocksAfter(blockNumber);
221
272
 
222
273
  // Clean up contract data and logs for the removed blocks
223
274
  await Promise.all([
224
- this.store.deleteLogs(removedBlocks),
275
+ this.stores.logs.deleteLogs(removedBlocks),
225
276
  ...removedBlocks.map(block => this.removeContractDataFromDb(block)),
226
277
  ]);
227
278
 
@@ -238,17 +289,42 @@ export class ArchiverDataStoreUpdater {
238
289
  * @returns True if the operation is successful.
239
290
  */
240
291
  public async removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<boolean> {
241
- const { blocksRemoved = [] } = await this.store.removeCheckpointsAfter(checkpointNumber);
242
-
243
- const opResults = await Promise.all([
244
- // Prune rolls back to the last proven block, which is by definition valid
245
- this.store.setPendingChainValidationStatus({ valid: true }),
246
- // Remove contract data for all blocks being removed
247
- ...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
248
- this.store.deleteLogs(blocksRemoved),
249
- ]);
292
+ return await this.stores.db.transactionAsync(async () => {
293
+ const { blocksRemoved = [] } = await this.stores.blocks.removeCheckpointsAfter(checkpointNumber);
250
294
 
251
- return opResults.every(Boolean);
295
+ const opResults = await Promise.all([
296
+ // Prune rolls back to the last proven block, which is by definition valid
297
+ this.stores.blocks.setPendingChainValidationStatus({ valid: true }),
298
+ // Remove contract data for all blocks being removed
299
+ ...blocksRemoved.map(block => this.removeContractDataFromDb(block)),
300
+ this.stores.logs.deleteLogs(blocksRemoved),
301
+ ]);
302
+
303
+ await this.l2TipsCache?.refresh();
304
+ return opResults.every(Boolean);
305
+ });
306
+ }
307
+
308
+ /**
309
+ * Updates the proven checkpoint number and refreshes the L2 tips cache.
310
+ * @param checkpointNumber - The checkpoint number to set as proven.
311
+ */
312
+ public async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
313
+ await this.stores.db.transactionAsync(async () => {
314
+ await this.stores.blocks.setProvenCheckpointNumber(checkpointNumber);
315
+ await this.l2TipsCache?.refresh();
316
+ });
317
+ }
318
+
319
+ /**
320
+ * Updates the finalized checkpoint number and refreshes the L2 tips cache.
321
+ * @param checkpointNumber - The checkpoint number to set as finalized.
322
+ */
323
+ public async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber): Promise<void> {
324
+ await this.stores.db.transactionAsync(async () => {
325
+ await this.stores.blocks.setFinalizedCheckpointNumber(checkpointNumber);
326
+ await this.l2TipsCache?.refresh();
327
+ });
252
328
  }
253
329
 
254
330
  /** Extracts and stores contract data from a single block. */
@@ -272,9 +348,6 @@ export class ArchiverDataStoreUpdater {
272
348
  this.updatePublishedContractClasses(contractClassLogs, block.number, operation),
273
349
  this.updateDeployedContractInstances(privateLogs, block.number, operation),
274
350
  this.updateUpdatedContractInstances(publicLogs, block.header.globalVariables.timestamp, operation),
275
- operation === Operation.Store
276
- ? this.storeBroadcastedIndividualFunctions(contractClassLogs, block.number)
277
- : Promise.resolve(true),
278
351
  ])
279
352
  ).every(Boolean);
280
353
  }
@@ -291,18 +364,37 @@ export class ArchiverDataStoreUpdater {
291
364
  .filter(log => ContractClassPublishedEvent.isContractClassPublishedEvent(log))
292
365
  .map(log => ContractClassPublishedEvent.fromLog(log));
293
366
 
294
- const contractClasses = await Promise.all(contractClassPublishedEvents.map(e => e.toContractClassPublic()));
295
- if (contractClasses.length > 0) {
296
- contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
297
- if (operation == Operation.Store) {
298
- // TODO: Will probably want to create some worker threads to compute these bytecode commitments as they are expensive
299
- const commitments = await Promise.all(
300
- contractClasses.map(c => computePublicBytecodeCommitment(c.packedBytecode)),
367
+ if (operation == Operation.Delete) {
368
+ const contractClasses = contractClassPublishedEvents.map(e => e.toContractClassPublic());
369
+ if (contractClasses.length > 0) {
370
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
371
+ return await this.stores.contractClasses.deleteContractClasses(contractClasses, blockNum);
372
+ }
373
+ return true;
374
+ }
375
+
376
+ // Compute bytecode commitments and validate class IDs in a single pass.
377
+ const contractClasses: ContractClassPublicWithCommitment[] = [];
378
+ for (const event of contractClassPublishedEvents) {
379
+ const contractClass = await event.toContractClassPublicWithBytecodeCommitment();
380
+ const computedClassId = await computeContractClassId({
381
+ artifactHash: contractClass.artifactHash,
382
+ privateFunctionsRoot: contractClass.privateFunctionsRoot,
383
+ publicBytecodeCommitment: contractClass.publicBytecodeCommitment,
384
+ });
385
+ if (!computedClassId.equals(contractClass.id)) {
386
+ this.log.warn(
387
+ `Skipping contract class with mismatched id at block ${blockNum}. Claimed ${contractClass.id}, computed ${computedClassId}`,
388
+ { blockNum, contractClassId: event.contractClassId.toString() },
301
389
  );
302
- return await this.store.addContractClasses(contractClasses, commitments, blockNum);
303
- } else if (operation == Operation.Delete) {
304
- return await this.store.deleteContractClasses(contractClasses, blockNum);
390
+ continue;
305
391
  }
392
+ contractClasses.push(contractClass);
393
+ }
394
+
395
+ if (contractClasses.length > 0) {
396
+ contractClasses.forEach(c => this.log.verbose(`${Operation[operation]} contract class ${c.id.toString()}`));
397
+ return await this.stores.contractClasses.addContractClasses(contractClasses, blockNum);
306
398
  }
307
399
  return true;
308
400
  }
@@ -315,18 +407,35 @@ export class ArchiverDataStoreUpdater {
315
407
  blockNum: BlockNumber,
316
408
  operation: Operation,
317
409
  ): Promise<boolean> {
318
- const contractInstances = allLogs
410
+ const allInstances = allLogs
319
411
  .filter(log => ContractInstancePublishedEvent.isContractInstancePublishedEvent(log))
320
412
  .map(log => ContractInstancePublishedEvent.fromLog(log))
321
413
  .map(e => e.toContractInstance());
414
+
415
+ // Verify that each instance's address matches the one derived from its fields if we're adding
416
+ const contractInstances =
417
+ operation === Operation.Delete
418
+ ? allInstances
419
+ : await filterAsync(allInstances, async instance => {
420
+ const computedAddress = await computeContractAddressFromInstance(instance);
421
+ if (!computedAddress.equals(instance.address)) {
422
+ this.log.warn(
423
+ `Found contract instance with mismatched address at block ${blockNum}. Claimed ${instance.address} but computed ${computedAddress}.`,
424
+ { instanceAddress: instance.address.toString(), computedAddress: computedAddress.toString(), blockNum },
425
+ );
426
+ return false;
427
+ }
428
+ return true;
429
+ });
430
+
322
431
  if (contractInstances.length > 0) {
323
432
  contractInstances.forEach(c =>
324
433
  this.log.verbose(`${Operation[operation]} contract instance at ${c.address.toString()}`),
325
434
  );
326
435
  if (operation == Operation.Store) {
327
- return await this.store.addContractInstances(contractInstances, blockNum);
436
+ return await this.stores.contractInstances.addContractInstances(contractInstances, blockNum);
328
437
  } else if (operation == Operation.Delete) {
329
- return await this.store.deleteContractInstances(contractInstances, blockNum);
438
+ return await this.stores.contractInstances.deleteContractInstances(contractInstances);
330
439
  }
331
440
  }
332
441
  return true;
@@ -350,73 +459,10 @@ export class ArchiverDataStoreUpdater {
350
459
  this.log.verbose(`${Operation[operation]} contract instance update at ${c.address.toString()}`),
351
460
  );
352
461
  if (operation == Operation.Store) {
353
- return await this.store.addContractInstanceUpdates(contractUpdates, timestamp);
462
+ return await this.stores.contractInstances.addContractInstanceUpdates(contractUpdates, timestamp);
354
463
  } else if (operation == Operation.Delete) {
355
- return await this.store.deleteContractInstanceUpdates(contractUpdates, timestamp);
356
- }
357
- }
358
- return true;
359
- }
360
-
361
- /**
362
- * Stores the functions that were broadcasted individually.
363
- *
364
- * @dev Beware that there is not a delete variant of this, since they are added to contract classes
365
- * and will be deleted as part of the class if needed.
366
- */
367
- private async storeBroadcastedIndividualFunctions(
368
- allLogs: ContractClassLog[],
369
- _blockNum: BlockNumber,
370
- ): Promise<boolean> {
371
- // Filter out private and utility function broadcast events
372
- const privateFnEvents = allLogs
373
- .filter(log => PrivateFunctionBroadcastedEvent.isPrivateFunctionBroadcastedEvent(log))
374
- .map(log => PrivateFunctionBroadcastedEvent.fromLog(log));
375
- const utilityFnEvents = allLogs
376
- .filter(log => UtilityFunctionBroadcastedEvent.isUtilityFunctionBroadcastedEvent(log))
377
- .map(log => UtilityFunctionBroadcastedEvent.fromLog(log));
378
-
379
- // Group all events by contract class id
380
- for (const [classIdString, classEvents] of Object.entries(
381
- groupBy([...privateFnEvents, ...utilityFnEvents], e => e.contractClassId.toString()),
382
- )) {
383
- const contractClassId = Fr.fromHexString(classIdString);
384
- const contractClass = await this.store.getContractClass(contractClassId);
385
- if (!contractClass) {
386
- this.log.warn(`Skipping broadcasted functions as contract class ${contractClassId.toString()} was not found`);
387
- continue;
388
- }
389
-
390
- // Split private and utility functions, and filter out invalid ones
391
- const allFns = classEvents.map(e => e.toFunctionWithMembershipProof());
392
- const privateFns = allFns.filter(
393
- (fn): fn is ExecutablePrivateFunctionWithMembershipProof => 'utilityFunctionsTreeRoot' in fn,
394
- );
395
- const utilityFns = allFns.filter(
396
- (fn): fn is UtilityFunctionWithMembershipProof => 'privateFunctionsArtifactTreeRoot' in fn,
397
- );
398
-
399
- const privateFunctionsWithValidity = await Promise.all(
400
- privateFns.map(async fn => ({ fn, valid: await isValidPrivateFunctionMembershipProof(fn, contractClass) })),
401
- );
402
- const validPrivateFns = privateFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
403
- const utilityFunctionsWithValidity = await Promise.all(
404
- utilityFns.map(async fn => ({
405
- fn,
406
- valid: await isValidUtilityFunctionMembershipProof(fn, contractClass),
407
- })),
408
- );
409
- const validUtilityFns = utilityFunctionsWithValidity.filter(({ valid }) => valid).map(({ fn }) => fn);
410
- const validFnCount = validPrivateFns.length + validUtilityFns.length;
411
- if (validFnCount !== allFns.length) {
412
- this.log.warn(`Skipping ${allFns.length - validFnCount} invalid functions`);
413
- }
414
-
415
- // Store the functions in the contract class in a single operation
416
- if (validFnCount > 0) {
417
- this.log.verbose(`Storing ${validFnCount} functions for contract class ${contractClassId.toString()}`);
464
+ return await this.stores.contractInstances.deleteContractInstanceUpdates(contractUpdates, timestamp);
418
465
  }
419
- return await this.store.addFunctions(contractClassId, validPrivateFns, validUtilityFns);
420
466
  }
421
467
  return true;
422
468
  }
@@ -1,5 +1,9 @@
1
+ import type { SlotNumber } from '@aztec/foundation/branded-types';
1
2
  import { createLogger } from '@aztec/foundation/log';
2
3
  import type { L2Block } from '@aztec/stdlib/block';
4
+ import type { CheckpointData } from '@aztec/stdlib/checkpoint';
5
+ import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
6
+ import { getTimestampForSlot } from '@aztec/stdlib/epoch-helpers';
3
7
  import {
4
8
  Attributes,
5
9
  type Gauge,
@@ -17,6 +21,7 @@ export class ArchiverInstrumentation {
17
21
  public readonly tracer: Tracer;
18
22
 
19
23
  private blockHeight: Gauge;
24
+ private checkpointHeight: Gauge;
20
25
  private txCount: UpDownCounter;
21
26
  private l1BlockHeight: Gauge;
22
27
  private proofsSubmittedDelay: Histogram;
@@ -27,6 +32,7 @@ export class ArchiverInstrumentation {
27
32
  private pruneCount: UpDownCounter;
28
33
 
29
34
  private syncDurationPerBlock: Histogram;
35
+ private syncDurationPerCheckpoint: Histogram;
30
36
  private syncBlockCount: UpDownCounter;
31
37
  private manaPerBlock: Histogram;
32
38
  private txsPerBlock: Histogram;
@@ -36,6 +42,9 @@ export class ArchiverInstrumentation {
36
42
 
37
43
  private blockProposalTxTargetCount: UpDownCounter;
38
44
 
45
+ private checkpointL1InclusionDelay: Histogram;
46
+ private checkpointPromotedCount: UpDownCounter;
47
+
39
48
  private log = createLogger('archiver:instrumentation');
40
49
 
41
50
  private constructor(
@@ -47,6 +56,8 @@ export class ArchiverInstrumentation {
47
56
 
48
57
  this.blockHeight = meter.createGauge(Metrics.ARCHIVER_BLOCK_HEIGHT);
49
58
 
59
+ this.checkpointHeight = meter.createGauge(Metrics.ARCHIVER_CHECKPOINT_HEIGHT);
60
+
50
61
  this.l1BlockHeight = meter.createGauge(Metrics.ARCHIVER_L1_BLOCK_HEIGHT);
51
62
 
52
63
  this.txCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_TOTAL_TXS);
@@ -59,6 +70,8 @@ export class ArchiverInstrumentation {
59
70
 
60
71
  this.syncDurationPerBlock = meter.createHistogram(Metrics.ARCHIVER_SYNC_PER_BLOCK);
61
72
 
73
+ this.syncDurationPerCheckpoint = meter.createHistogram(Metrics.ARCHIVER_SYNC_PER_CHECKPOINT);
74
+
62
75
  this.syncBlockCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_SYNC_BLOCK_COUNT);
63
76
 
64
77
  this.manaPerBlock = meter.createHistogram(Metrics.ARCHIVER_MANA_PER_BLOCK);
@@ -81,6 +94,10 @@ export class ArchiverInstrumentation {
81
94
  },
82
95
  );
83
96
 
97
+ this.checkpointL1InclusionDelay = meter.createHistogram(Metrics.ARCHIVER_CHECKPOINT_L1_INCLUSION_DELAY);
98
+
99
+ this.checkpointPromotedCount = createUpDownCounterWithDefault(meter, Metrics.ARCHIVER_CHECKPOINT_PROMOTED_COUNT);
100
+
84
101
  this.dbMetrics = new LmdbMetrics(
85
102
  meter,
86
103
  {
@@ -102,16 +119,26 @@ export class ArchiverInstrumentation {
102
119
  return this.telemetry.isEnabled();
103
120
  }
104
121
 
105
- public processNewBlocks(syncTimePerBlock: number, blocks: L2Block[]) {
122
+ public processNewProposedBlock(syncTimePerBlock: number, block: L2Block) {
123
+ const attrs = { [Attributes.STATUS]: 'proposed' };
124
+ this.blockHeight.record(block.number, attrs);
106
125
  this.syncDurationPerBlock.record(Math.ceil(syncTimePerBlock));
107
- this.blockHeight.record(Math.max(...blocks.map(b => b.number)));
108
- this.syncBlockCount.add(blocks.length);
109
126
 
110
- for (const block of blocks) {
111
- this.txCount.add(block.body.txEffects.length);
112
- this.txsPerBlock.record(block.body.txEffects.length);
113
- this.manaPerBlock.record(block.header.totalManaUsed.toNumber() / 1e6);
127
+ // Per block metrics
128
+ this.txCount.add(block.body.txEffects.length);
129
+ this.txsPerBlock.record(block.body.txEffects.length);
130
+ this.manaPerBlock.record(block.header.totalManaUsed.toNumber() / 1e6);
131
+ }
132
+
133
+ public processNewCheckpointedBlocks(syncTimePerCheckpoint: number, blocks: L2Block[]) {
134
+ if (blocks.length === 0) {
135
+ return;
114
136
  }
137
+
138
+ this.syncDurationPerCheckpoint.record(Math.ceil(syncTimePerCheckpoint));
139
+ this.blockHeight.record(Math.max(...blocks.map(b => b.number)));
140
+ this.checkpointHeight.record(Math.max(...blocks.map(b => b.checkpointNumber)));
141
+ this.syncBlockCount.add(blocks.length);
115
142
  }
116
143
 
117
144
  public processNewMessages(count: number, syncPerMessageMs: number) {
@@ -127,8 +154,10 @@ export class ArchiverInstrumentation {
127
154
  this.pruneDuration.record(Math.ceil(duration));
128
155
  }
129
156
 
130
- public updateLastProvenBlock(blockNumber: number) {
131
- this.blockHeight.record(blockNumber, { [Attributes.STATUS]: 'proven' });
157
+ public updateLastProvenCheckpoint(checkpoint: CheckpointData) {
158
+ const lastBlockNumberInCheckpoint = checkpoint.startBlock + checkpoint.blockCount - 1;
159
+ this.blockHeight.record(lastBlockNumberInCheckpoint, { [Attributes.STATUS]: 'proven' });
160
+ this.checkpointHeight.record(checkpoint.checkpointNumber, { [Attributes.STATUS]: 'proven' });
132
161
  }
133
162
 
134
163
  public processProofsVerified(logs: { proverId: string; l2BlockNumber: bigint; delay: bigint }[]) {
@@ -154,4 +183,22 @@ export class ArchiverInstrumentation {
154
183
  [Attributes.L1_BLOCK_PROPOSAL_USED_TRACE]: usedTrace,
155
184
  });
156
185
  }
186
+
187
+ /** Records a checkpoint promoted from proposed (blob fetch skipped). */
188
+ public processCheckpointPromoted() {
189
+ this.checkpointPromotedCount.add(1);
190
+ }
191
+
192
+ /**
193
+ * Records L1 inclusion timing for a checkpoint observed on L1 (seconds into the L2 slot).
194
+ */
195
+ public processCheckpointL1Timing(data: {
196
+ slotNumber: SlotNumber;
197
+ l1Timestamp: bigint;
198
+ l1Constants: Pick<L1RollupConstants, 'l1GenesisTime' | 'slotDuration'>;
199
+ }): void {
200
+ const slotStartTs = getTimestampForSlot(data.slotNumber, data.l1Constants);
201
+ const inclusionDelaySeconds = Number(data.l1Timestamp - slotStartTs);
202
+ this.checkpointL1InclusionDelay.record(inclusionDelaySeconds);
203
+ }
157
204
  }