@aztec/archiver 0.0.1-commit.381b1a9 → 0.0.1-commit.3a4ae741b

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 (65) hide show
  1. package/dest/archiver.d.ts +1 -2
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +28 -15
  4. package/dest/config.d.ts +3 -3
  5. package/dest/config.d.ts.map +1 -1
  6. package/dest/config.js +2 -1
  7. package/dest/errors.d.ts +21 -9
  8. package/dest/errors.d.ts.map +1 -1
  9. package/dest/errors.js +27 -14
  10. package/dest/factory.d.ts +3 -3
  11. package/dest/factory.d.ts.map +1 -1
  12. package/dest/factory.js +16 -13
  13. package/dest/l1/data_retrieval.d.ts +6 -3
  14. package/dest/l1/data_retrieval.d.ts.map +1 -1
  15. package/dest/l1/data_retrieval.js +12 -6
  16. package/dest/modules/data_source_base.d.ts +3 -3
  17. package/dest/modules/data_source_base.d.ts.map +1 -1
  18. package/dest/modules/data_source_base.js +4 -4
  19. package/dest/modules/data_store_updater.d.ts +7 -10
  20. package/dest/modules/data_store_updater.d.ts.map +1 -1
  21. package/dest/modules/data_store_updater.js +44 -74
  22. package/dest/modules/instrumentation.d.ts +12 -1
  23. package/dest/modules/instrumentation.d.ts.map +1 -1
  24. package/dest/modules/instrumentation.js +10 -0
  25. package/dest/modules/l1_synchronizer.d.ts +1 -1
  26. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  27. package/dest/modules/l1_synchronizer.js +17 -9
  28. package/dest/store/block_store.d.ts +5 -5
  29. package/dest/store/block_store.d.ts.map +1 -1
  30. package/dest/store/block_store.js +30 -48
  31. package/dest/store/contract_class_store.d.ts +2 -3
  32. package/dest/store/contract_class_store.d.ts.map +1 -1
  33. package/dest/store/contract_class_store.js +7 -67
  34. package/dest/store/contract_instance_store.d.ts +1 -1
  35. package/dest/store/contract_instance_store.d.ts.map +1 -1
  36. package/dest/store/contract_instance_store.js +6 -2
  37. package/dest/store/kv_archiver_store.d.ts +16 -16
  38. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  39. package/dest/store/kv_archiver_store.js +18 -17
  40. package/dest/store/log_store.d.ts +6 -3
  41. package/dest/store/log_store.d.ts.map +1 -1
  42. package/dest/store/log_store.js +45 -6
  43. package/dest/store/message_store.d.ts +5 -1
  44. package/dest/store/message_store.d.ts.map +1 -1
  45. package/dest/store/message_store.js +13 -0
  46. package/dest/test/fake_l1_state.d.ts +1 -1
  47. package/dest/test/fake_l1_state.d.ts.map +1 -1
  48. package/dest/test/fake_l1_state.js +11 -3
  49. package/package.json +13 -13
  50. package/src/archiver.ts +31 -13
  51. package/src/config.ts +8 -1
  52. package/src/errors.ts +40 -24
  53. package/src/factory.ts +18 -11
  54. package/src/l1/data_retrieval.ts +17 -9
  55. package/src/modules/data_source_base.ts +8 -3
  56. package/src/modules/data_store_updater.ts +45 -104
  57. package/src/modules/instrumentation.ts +20 -0
  58. package/src/modules/l1_synchronizer.ts +27 -20
  59. package/src/store/block_store.ts +31 -56
  60. package/src/store/contract_class_store.ts +8 -106
  61. package/src/store/contract_instance_store.ts +8 -5
  62. package/src/store/kv_archiver_store.ts +21 -28
  63. package/src/store/log_store.ts +60 -15
  64. package/src/store/message_store.ts +19 -0
  65. package/src/test/fake_l1_state.ts +15 -5
@@ -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 { FunctionSelector } from '@aztec/stdlib/abi';
6
- import type {
7
- ContractClassPublic,
8
- ContractClassPublicWithBlockNumber,
9
- ExecutablePrivateFunctionWithMembershipProof,
10
- UtilityFunctionWithMembershipProof,
11
- } from '@aztec/stdlib/contract';
12
- import { Vector } from '@aztec/stdlib/types';
5
+ import type { ContractClassPublic, ContractClassPublicWithBlockNumber } from '@aztec/stdlib/contract';
13
6
 
14
7
  /**
15
8
  * LMDB-based contract class storage for the archiver.
@@ -29,11 +22,15 @@ export class ContractClassStore {
29
22
  blockNumber: number,
30
23
  ): Promise<void> {
31
24
  await this.db.transactionAsync(async () => {
32
- await this.#contractClasses.setIfNotExists(
33
- contractClass.id.toString(),
25
+ const key = contractClass.id.toString();
26
+ if (await this.#contractClasses.hasAsync(key)) {
27
+ throw new Error(`Contract class ${key} already exists, cannot add again at block ${blockNumber}`);
28
+ }
29
+ await this.#contractClasses.set(
30
+ key,
34
31
  serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
35
32
  );
36
- await this.#bytecodeCommitments.setIfNotExists(contractClass.id.toString(), bytecodeCommitment.toBuffer());
33
+ await this.#bytecodeCommitments.set(key, bytecodeCommitment.toBuffer());
37
34
  });
38
35
  }
39
36
 
@@ -60,37 +57,6 @@ export class ContractClassStore {
60
57
  async getContractClassIds(): Promise<Fr[]> {
61
58
  return (await toArray(this.#contractClasses.keysAsync())).map(key => Fr.fromHexString(key));
62
59
  }
63
-
64
- async addFunctions(
65
- contractClassId: Fr,
66
- newPrivateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
67
- newUtilityFunctions: UtilityFunctionWithMembershipProof[],
68
- ): Promise<boolean> {
69
- await this.db.transactionAsync(async () => {
70
- const existingClassBuffer = await this.#contractClasses.getAsync(contractClassId.toString());
71
- if (!existingClassBuffer) {
72
- throw new Error(`Unknown contract class ${contractClassId} when adding private functions to store`);
73
- }
74
-
75
- const existingClass = deserializeContractClassPublic(existingClassBuffer);
76
- const { privateFunctions: existingPrivateFns, utilityFunctions: existingUtilityFns } = existingClass;
77
-
78
- const updatedClass: Omit<ContractClassPublicWithBlockNumber, 'id'> = {
79
- ...existingClass,
80
- privateFunctions: [
81
- ...existingPrivateFns,
82
- ...newPrivateFunctions.filter(newFn => !existingPrivateFns.some(f => f.selector.equals(newFn.selector))),
83
- ],
84
- utilityFunctions: [
85
- ...existingUtilityFns,
86
- ...newUtilityFunctions.filter(newFn => !existingUtilityFns.some(f => f.selector.equals(newFn.selector))),
87
- ],
88
- };
89
- await this.#contractClasses.set(contractClassId.toString(), serializeContractClassPublic(updatedClass));
90
- });
91
-
92
- return true;
93
- }
94
60
  }
95
61
 
96
62
  function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWithBlockNumber, 'id'>): Buffer {
@@ -98,83 +64,19 @@ function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWit
98
64
  contractClass.l2BlockNumber,
99
65
  numToUInt8(contractClass.version),
100
66
  contractClass.artifactHash,
101
- contractClass.privateFunctions.length,
102
- contractClass.privateFunctions.map(serializePrivateFunction),
103
- contractClass.utilityFunctions.length,
104
- contractClass.utilityFunctions.map(serializeUtilityFunction),
105
67
  contractClass.packedBytecode.length,
106
68
  contractClass.packedBytecode,
107
69
  contractClass.privateFunctionsRoot,
108
70
  );
109
71
  }
110
72
 
111
- function serializePrivateFunction(fn: ExecutablePrivateFunctionWithMembershipProof): Buffer {
112
- return serializeToBuffer(
113
- fn.selector,
114
- fn.vkHash,
115
- fn.bytecode.length,
116
- fn.bytecode,
117
- fn.functionMetadataHash,
118
- fn.artifactMetadataHash,
119
- fn.utilityFunctionsTreeRoot,
120
- new Vector(fn.privateFunctionTreeSiblingPath),
121
- fn.privateFunctionTreeLeafIndex,
122
- new Vector(fn.artifactTreeSiblingPath),
123
- fn.artifactTreeLeafIndex,
124
- );
125
- }
126
-
127
- function serializeUtilityFunction(fn: UtilityFunctionWithMembershipProof): Buffer {
128
- return serializeToBuffer(
129
- fn.selector,
130
- fn.bytecode.length,
131
- fn.bytecode,
132
- fn.functionMetadataHash,
133
- fn.artifactMetadataHash,
134
- fn.privateFunctionsArtifactTreeRoot,
135
- new Vector(fn.artifactTreeSiblingPath),
136
- fn.artifactTreeLeafIndex,
137
- );
138
- }
139
-
140
73
  function deserializeContractClassPublic(buffer: Buffer): Omit<ContractClassPublicWithBlockNumber, 'id'> {
141
74
  const reader = BufferReader.asReader(buffer);
142
75
  return {
143
76
  l2BlockNumber: reader.readNumber(),
144
77
  version: reader.readUInt8() as 1,
145
78
  artifactHash: reader.readObject(Fr),
146
- privateFunctions: reader.readVector({ fromBuffer: deserializePrivateFunction }),
147
- utilityFunctions: reader.readVector({ fromBuffer: deserializeUtilityFunction }),
148
79
  packedBytecode: reader.readBuffer(),
149
80
  privateFunctionsRoot: reader.readObject(Fr),
150
81
  };
151
82
  }
152
-
153
- function deserializePrivateFunction(buffer: Buffer | BufferReader): ExecutablePrivateFunctionWithMembershipProof {
154
- const reader = BufferReader.asReader(buffer);
155
- return {
156
- selector: reader.readObject(FunctionSelector),
157
- vkHash: reader.readObject(Fr),
158
- bytecode: reader.readBuffer(),
159
- functionMetadataHash: reader.readObject(Fr),
160
- artifactMetadataHash: reader.readObject(Fr),
161
- utilityFunctionsTreeRoot: reader.readObject(Fr),
162
- privateFunctionTreeSiblingPath: reader.readVector(Fr),
163
- privateFunctionTreeLeafIndex: reader.readNumber(),
164
- artifactTreeSiblingPath: reader.readVector(Fr),
165
- artifactTreeLeafIndex: reader.readNumber(),
166
- };
167
- }
168
-
169
- function deserializeUtilityFunction(buffer: Buffer | BufferReader): UtilityFunctionWithMembershipProof {
170
- const reader = BufferReader.asReader(buffer);
171
- return {
172
- selector: reader.readObject(FunctionSelector),
173
- bytecode: reader.readBuffer(),
174
- functionMetadataHash: reader.readObject(Fr),
175
- artifactMetadataHash: reader.readObject(Fr),
176
- privateFunctionsArtifactTreeRoot: reader.readObject(Fr),
177
- artifactTreeSiblingPath: reader.readVector(Fr),
178
- artifactTreeLeafIndex: reader.readNumber(),
179
- };
180
- }
@@ -27,11 +27,14 @@ export class ContractInstanceStore {
27
27
 
28
28
  addContractInstance(contractInstance: ContractInstanceWithAddress, blockNumber: number): Promise<void> {
29
29
  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);
30
+ const key = contractInstance.address.toString();
31
+ if (await this.#contractInstances.hasAsync(key)) {
32
+ throw new Error(
33
+ `Contract instance at ${key} already exists (deployed at block ${await this.#contractInstancePublishedAt.getAsync(key)}), cannot add again at block ${blockNumber}`,
34
+ );
35
+ }
36
+ await this.#contractInstances.set(key, new SerializableContractInstance(contractInstance).toBuffer());
37
+ await this.#contractInstancePublishedAt.set(key, blockNumber);
35
38
  });
36
39
  }
37
40
 
@@ -16,11 +16,10 @@ import {
16
16
  import type { CheckpointData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
17
17
  import type {
18
18
  ContractClassPublic,
19
+ ContractClassPublicWithCommitment,
19
20
  ContractDataSource,
20
21
  ContractInstanceUpdateWithAddress,
21
22
  ContractInstanceWithAddress,
22
- ExecutablePrivateFunctionWithMembershipProof,
23
- UtilityFunctionWithMembershipProof,
24
23
  } from '@aztec/stdlib/contract';
25
24
  import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
26
25
  import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
@@ -37,7 +36,7 @@ import { ContractInstanceStore } from './contract_instance_store.js';
37
36
  import { LogStore } from './log_store.js';
38
37
  import { MessageStore } from './message_store.js';
39
38
 
40
- export const ARCHIVER_DB_VERSION = 5;
39
+ export const ARCHIVER_DB_VERSION = 6;
41
40
  export const MAX_FUNCTION_SIGNATURES = 1000;
42
41
  export const MAX_FUNCTION_NAME_LEN = 256;
43
42
 
@@ -167,19 +166,14 @@ export class KVArchiverDataStore implements ContractDataSource {
167
166
 
168
167
  /**
169
168
  * Add new contract classes from an L2 block to the store's list.
170
- * @param data - List of contract classes to be added.
171
- * @param bytecodeCommitments - Bytecode commitments for the contract classes.
169
+ * @param data - List of contract classes (with bytecode commitments) to be added.
172
170
  * @param blockNumber - Number of the L2 block the contracts were registered in.
173
171
  * @returns True if the operation is successful.
174
172
  */
175
- async addContractClasses(
176
- data: ContractClassPublic[],
177
- bytecodeCommitments: Fr[],
178
- blockNumber: BlockNumber,
179
- ): Promise<boolean> {
173
+ async addContractClasses(data: ContractClassPublicWithCommitment[], blockNumber: BlockNumber): Promise<boolean> {
180
174
  return (
181
175
  await Promise.all(
182
- data.map((c, i) => this.#contractClassStore.addContractClass(c, bytecodeCommitments[i], blockNumber)),
176
+ data.map(c => this.#contractClassStore.addContractClass(c, c.publicBytecodeCommitment, blockNumber)),
183
177
  )
184
178
  ).every(Boolean);
185
179
  }
@@ -194,15 +188,6 @@ export class KVArchiverDataStore implements ContractDataSource {
194
188
  return this.#contractClassStore.getBytecodeCommitment(contractClassId);
195
189
  }
196
190
 
197
- /** Adds private functions to a contract class. */
198
- addFunctions(
199
- contractClassId: Fr,
200
- privateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
201
- utilityFunctions: UtilityFunctionWithMembershipProof[],
202
- ): Promise<boolean> {
203
- return this.#contractClassStore.addFunctions(contractClassId, privateFunctions, utilityFunctions);
204
- }
205
-
206
191
  /**
207
192
  * Add new contract instances from an L2 block to the store's list.
208
193
  * @param data - List of contract instances to be added.
@@ -245,14 +230,14 @@ export class KVArchiverDataStore implements ContractDataSource {
245
230
  }
246
231
 
247
232
  /**
248
- * Append new proposed blocks to the store's list.
249
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
233
+ * Append a new proposed block to the store.
234
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
250
235
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
251
- * @param blocks - The proposed L2 blocks to be added to the store.
236
+ * @param block - The proposed L2 block to be added to the store.
252
237
  * @returns True if the operation is successful.
253
238
  */
254
- addProposedBlocks(blocks: L2Block[], opts: { force?: boolean; checkpointNumber?: number } = {}): Promise<boolean> {
255
- return this.#blockStore.addProposedBlocks(blocks, opts);
239
+ addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
240
+ return this.#blockStore.addProposedBlock(block, opts);
256
241
  }
257
242
 
258
243
  /**
@@ -474,10 +459,11 @@ export class KVArchiverDataStore implements ContractDataSource {
474
459
  * array implies no logs match that tag.
475
460
  * @param tags - The tags to search for.
476
461
  * @param page - The page number (0-indexed) for pagination. Returns at most 10 logs per tag per page.
462
+ * @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
477
463
  */
478
- getPrivateLogsByTags(tags: SiloedTag[], page?: number): Promise<TxScopedL2Log[][]> {
464
+ getPrivateLogsByTags(tags: SiloedTag[], page?: number, upToBlockNumber?: BlockNumber): Promise<TxScopedL2Log[][]> {
479
465
  try {
480
- return this.#logStore.getPrivateLogsByTags(tags, page);
466
+ return this.#logStore.getPrivateLogsByTags(tags, page, upToBlockNumber);
481
467
  } catch (err) {
482
468
  return Promise.reject(err);
483
469
  }
@@ -489,14 +475,16 @@ export class KVArchiverDataStore implements ContractDataSource {
489
475
  * @param contractAddress - The contract address to search logs for.
490
476
  * @param tags - The tags to search for.
491
477
  * @param page - The page number (0-indexed) for pagination. Returns at most 10 logs per tag per page.
478
+ * @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
492
479
  */
493
480
  getPublicLogsByTagsFromContract(
494
481
  contractAddress: AztecAddress,
495
482
  tags: Tag[],
496
483
  page?: number,
484
+ upToBlockNumber?: BlockNumber,
497
485
  ): Promise<TxScopedL2Log[][]> {
498
486
  try {
499
- return this.#logStore.getPublicLogsByTagsFromContract(contractAddress, tags, page);
487
+ return this.#logStore.getPublicLogsByTagsFromContract(contractAddress, tags, page, upToBlockNumber);
500
488
  } catch (err) {
501
489
  return Promise.reject(err);
502
490
  }
@@ -603,6 +591,11 @@ export class KVArchiverDataStore implements ContractDataSource {
603
591
  return this.#messageStore.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
604
592
  }
605
593
 
594
+ /** Persists the inbox tree-in-progress checkpoint number from L1 state. */
595
+ public setInboxTreeInProgress(value: bigint): Promise<void> {
596
+ return this.#messageStore.setInboxTreeInProgress(value);
597
+ }
598
+
606
599
  /** Returns an async iterator to all L1 to L2 messages on the range. */
607
600
  public iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
608
601
  return this.#messageStore.iterateL1ToL2Messages(range);
@@ -22,6 +22,7 @@ import {
22
22
  } from '@aztec/stdlib/logs';
23
23
  import { TxHash } from '@aztec/stdlib/tx';
24
24
 
25
+ import { OutOfOrderLogInsertionError } from '../errors.js';
25
26
  import type { BlockStore } from './block_store.js';
26
27
 
27
28
  /**
@@ -165,10 +166,21 @@ export class LogStore {
165
166
 
166
167
  for (const taggedLogBuffer of currentPrivateTaggedLogs) {
167
168
  if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
168
- privateTaggedLogs.set(
169
- taggedLogBuffer.tag,
170
- taggedLogBuffer.logBuffers!.concat(privateTaggedLogs.get(taggedLogBuffer.tag)!),
171
- );
169
+ const newLogs = privateTaggedLogs.get(taggedLogBuffer.tag)!;
170
+ if (newLogs.length === 0) {
171
+ continue;
172
+ }
173
+ const lastExisting = TxScopedL2Log.fromBuffer(taggedLogBuffer.logBuffers.at(-1)!);
174
+ const firstNew = TxScopedL2Log.fromBuffer(newLogs[0]);
175
+ if (lastExisting.blockNumber > firstNew.blockNumber) {
176
+ throw new OutOfOrderLogInsertionError(
177
+ 'private',
178
+ taggedLogBuffer.tag,
179
+ lastExisting.blockNumber,
180
+ firstNew.blockNumber,
181
+ );
182
+ }
183
+ privateTaggedLogs.set(taggedLogBuffer.tag, taggedLogBuffer.logBuffers.concat(newLogs));
172
184
  }
173
185
  }
174
186
 
@@ -200,10 +212,21 @@ export class LogStore {
200
212
 
201
213
  for (const taggedLogBuffer of currentPublicTaggedLogs) {
202
214
  if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
203
- publicTaggedLogs.set(
204
- taggedLogBuffer.tag,
205
- taggedLogBuffer.logBuffers!.concat(publicTaggedLogs.get(taggedLogBuffer.tag)!),
206
- );
215
+ const newLogs = publicTaggedLogs.get(taggedLogBuffer.tag)!;
216
+ if (newLogs.length === 0) {
217
+ continue;
218
+ }
219
+ const lastExisting = TxScopedL2Log.fromBuffer(taggedLogBuffer.logBuffers.at(-1)!);
220
+ const firstNew = TxScopedL2Log.fromBuffer(newLogs[0]);
221
+ if (lastExisting.blockNumber > firstNew.blockNumber) {
222
+ throw new OutOfOrderLogInsertionError(
223
+ 'public',
224
+ taggedLogBuffer.tag,
225
+ lastExisting.blockNumber,
226
+ firstNew.blockNumber,
227
+ );
228
+ }
229
+ publicTaggedLogs.set(taggedLogBuffer.tag, taggedLogBuffer.logBuffers.concat(newLogs));
207
230
  }
208
231
  }
209
232
 
@@ -353,17 +376,30 @@ export class LogStore {
353
376
  * array implies no logs match that tag.
354
377
  * @param tags - The tags to search for.
355
378
  * @param page - The page number (0-indexed) for pagination.
379
+ * @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
356
380
  * @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
357
381
  * MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
358
382
  */
359
- async getPrivateLogsByTags(tags: SiloedTag[], page: number = 0): Promise<TxScopedL2Log[][]> {
383
+ async getPrivateLogsByTags(
384
+ tags: SiloedTag[],
385
+ page: number = 0,
386
+ upToBlockNumber?: BlockNumber,
387
+ ): Promise<TxScopedL2Log[][]> {
360
388
  const logs = await Promise.all(tags.map(tag => this.#privateLogsByTag.getAsync(tag.toString())));
389
+
361
390
  const start = page * MAX_LOGS_PER_TAG;
362
391
  const end = start + MAX_LOGS_PER_TAG;
363
392
 
364
- return logs.map(
365
- logBuffers => logBuffers?.slice(start, end).map(logBuffer => TxScopedL2Log.fromBuffer(logBuffer)) ?? [],
366
- );
393
+ return logs.map(logBuffers => {
394
+ const deserialized = logBuffers?.slice(start, end).map(buf => TxScopedL2Log.fromBuffer(buf)) ?? [];
395
+ if (upToBlockNumber !== undefined) {
396
+ const cutoff = deserialized.findIndex(log => log.blockNumber > upToBlockNumber);
397
+ if (cutoff !== -1) {
398
+ return deserialized.slice(0, cutoff);
399
+ }
400
+ }
401
+ return deserialized;
402
+ });
367
403
  }
368
404
 
369
405
  /**
@@ -372,6 +408,7 @@ export class LogStore {
372
408
  * @param contractAddress - The contract address to search logs for.
373
409
  * @param tags - The tags to search for.
374
410
  * @param page - The page number (0-indexed) for pagination.
411
+ * @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
375
412
  * @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
376
413
  * MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
377
414
  */
@@ -379,6 +416,7 @@ export class LogStore {
379
416
  contractAddress: AztecAddress,
380
417
  tags: Tag[],
381
418
  page: number = 0,
419
+ upToBlockNumber?: BlockNumber,
382
420
  ): Promise<TxScopedL2Log[][]> {
383
421
  const logs = await Promise.all(
384
422
  tags.map(tag => {
@@ -389,9 +427,16 @@ export class LogStore {
389
427
  const start = page * MAX_LOGS_PER_TAG;
390
428
  const end = start + MAX_LOGS_PER_TAG;
391
429
 
392
- return logs.map(
393
- logBuffers => logBuffers?.slice(start, end).map(logBuffer => TxScopedL2Log.fromBuffer(logBuffer)) ?? [],
394
- );
430
+ return logs.map(logBuffers => {
431
+ const deserialized = logBuffers?.slice(start, end).map(buf => TxScopedL2Log.fromBuffer(buf)) ?? [];
432
+ if (upToBlockNumber !== undefined) {
433
+ const cutoff = deserialized.findIndex(log => log.blockNumber > upToBlockNumber);
434
+ if (cutoff !== -1) {
435
+ return deserialized.slice(0, cutoff);
436
+ }
437
+ }
438
+ return deserialized;
439
+ });
395
440
  }
396
441
 
397
442
  /**
@@ -14,6 +14,7 @@ import {
14
14
  } from '@aztec/kv-store';
15
15
  import { InboxLeaf } from '@aztec/stdlib/messaging';
16
16
 
17
+ import { L1ToL2MessagesNotReadyError } from '../errors.js';
17
18
  import {
18
19
  type InboxMessage,
19
20
  deserializeInboxMessage,
@@ -40,6 +41,8 @@ export class MessageStore {
40
41
  #lastSynchedL1Block: AztecAsyncSingleton<Buffer>;
41
42
  /** Stores total messages stored */
42
43
  #totalMessageCount: AztecAsyncSingleton<bigint>;
44
+ /** Stores the checkpoint number whose message tree is currently being filled on L1. */
45
+ #inboxTreeInProgress: AztecAsyncSingleton<bigint>;
43
46
 
44
47
  #log = createLogger('archiver:message_store');
45
48
 
@@ -48,6 +51,7 @@ export class MessageStore {
48
51
  this.#l1ToL2MessageIndices = db.openMap('archiver_l1_to_l2_message_indices');
49
52
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_id');
50
53
  this.#totalMessageCount = db.openSingleton('archiver_l1_to_l2_message_count');
54
+ this.#inboxTreeInProgress = db.openSingleton('archiver_inbox_tree_in_progress');
51
55
  }
52
56
 
53
57
  public async getTotalL1ToL2MessageCount(): Promise<bigint> {
@@ -185,7 +189,22 @@ export class MessageStore {
185
189
  return msg ? deserializeInboxMessage(msg) : undefined;
186
190
  }
187
191
 
192
+ /** Returns the inbox tree-in-progress checkpoint number from L1, or undefined if not yet set. */
193
+ public getInboxTreeInProgress(): Promise<bigint | undefined> {
194
+ return this.#inboxTreeInProgress.getAsync();
195
+ }
196
+
197
+ /** Persists the inbox tree-in-progress checkpoint number from L1 state. */
198
+ public async setInboxTreeInProgress(value: bigint): Promise<void> {
199
+ await this.#inboxTreeInProgress.set(value);
200
+ }
201
+
188
202
  public async getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
203
+ const treeInProgress = await this.#inboxTreeInProgress.getAsync();
204
+ if (treeInProgress !== undefined && BigInt(checkpointNumber) >= treeInProgress) {
205
+ throw new L1ToL2MessagesNotReadyError(checkpointNumber, treeInProgress);
206
+ }
207
+
189
208
  const messages: Fr[] = [];
190
209
 
191
210
  const [startIndex, endIndex] = InboxLeaf.indexRangeForCheckpoint(checkpointNumber);
@@ -1,5 +1,6 @@
1
1
  import type { BlobClientInterface } from '@aztec/blob-client/client';
2
2
  import { type Blob, getBlobsPerL1Block, getPrefixedEthBlobCommitments } from '@aztec/blob-lib';
3
+ import { INITIAL_CHECKPOINT_NUMBER } from '@aztec/constants';
3
4
  import type { CheckpointProposedLog, InboxContract, MessageSentLog, RollupContract } from '@aztec/ethereum/contracts';
4
5
  import { MULTI_CALL_3_ADDRESS } from '@aztec/ethereum/contracts';
5
6
  import type { ViemPublicClient } from '@aztec/ethereum/types';
@@ -450,13 +451,22 @@ export class FakeL1State {
450
451
  createMockInboxContract(_publicClient: MockProxy<ViemPublicClient>): MockProxy<InboxContract> {
451
452
  const mockInbox = mock<InboxContract>();
452
453
 
453
- mockInbox.getState.mockImplementation(() =>
454
- Promise.resolve({
454
+ mockInbox.getState.mockImplementation(() => {
455
+ // treeInProgress must be > any sealed checkpoint. On L1, a checkpoint can only be proposed
456
+ // after its messages are sealed, so treeInProgress > checkpointNumber for all published checkpoints.
457
+ const maxFromMessages =
458
+ this.messages.length > 0 ? Math.max(...this.messages.map(m => Number(m.checkpointNumber))) + 1 : 0;
459
+ const maxFromCheckpoints =
460
+ this.checkpoints.length > 0
461
+ ? Math.max(...this.checkpoints.filter(cp => !cp.pruned).map(cp => Number(cp.checkpointNumber))) + 1
462
+ : 0;
463
+ const treeInProgress = Math.max(maxFromMessages, maxFromCheckpoints, INITIAL_CHECKPOINT_NUMBER);
464
+ return Promise.resolve({
455
465
  messagesRollingHash: this.messagesRollingHash,
456
466
  totalMessagesInserted: BigInt(this.messages.length),
457
- treeInProgress: 0n,
458
- }),
459
- );
467
+ treeInProgress: BigInt(treeInProgress),
468
+ });
469
+ });
460
470
 
461
471
  // Mock the wrapper methods for fetching message events
462
472
  mockInbox.getMessageSentEvents.mockImplementation((fromBlock: bigint, toBlock: bigint) =>