@aztec/archiver 0.0.1-commit.ee80a48 → 0.0.1-commit.f103f88

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 (110) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +12 -8
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +83 -114
  5. package/dest/config.d.ts +3 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +3 -2
  8. package/dest/errors.d.ts +34 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +45 -16
  11. package/dest/factory.d.ts +4 -5
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +31 -26
  14. package/dest/index.d.ts +2 -1
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +1 -0
  17. package/dest/l1/bin/retrieve-calldata.js +36 -33
  18. package/dest/l1/calldata_retriever.d.ts +73 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +191 -259
  21. package/dest/l1/data_retrieval.d.ts +11 -11
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +35 -34
  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/modules/data_source_base.d.ts +14 -7
  28. package/dest/modules/data_source_base.d.ts.map +1 -1
  29. package/dest/modules/data_source_base.js +39 -77
  30. package/dest/modules/data_store_updater.d.ts +25 -12
  31. package/dest/modules/data_store_updater.d.ts.map +1 -1
  32. package/dest/modules/data_store_updater.js +125 -94
  33. package/dest/modules/instrumentation.d.ts +18 -3
  34. package/dest/modules/instrumentation.d.ts.map +1 -1
  35. package/dest/modules/instrumentation.js +36 -8
  36. package/dest/modules/l1_synchronizer.d.ts +7 -9
  37. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  38. package/dest/modules/l1_synchronizer.js +179 -137
  39. package/dest/modules/validation.d.ts +1 -1
  40. package/dest/modules/validation.d.ts.map +1 -1
  41. package/dest/modules/validation.js +2 -2
  42. package/dest/store/block_store.d.ts +66 -28
  43. package/dest/store/block_store.d.ts.map +1 -1
  44. package/dest/store/block_store.js +357 -136
  45. package/dest/store/contract_class_store.d.ts +2 -3
  46. package/dest/store/contract_class_store.d.ts.map +1 -1
  47. package/dest/store/contract_class_store.js +7 -67
  48. package/dest/store/contract_instance_store.d.ts +1 -1
  49. package/dest/store/contract_instance_store.d.ts.map +1 -1
  50. package/dest/store/contract_instance_store.js +6 -2
  51. package/dest/store/kv_archiver_store.d.ts +61 -24
  52. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  53. package/dest/store/kv_archiver_store.js +75 -27
  54. package/dest/store/l2_tips_cache.d.ts +20 -0
  55. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  56. package/dest/store/l2_tips_cache.js +109 -0
  57. package/dest/store/log_store.d.ts +6 -3
  58. package/dest/store/log_store.d.ts.map +1 -1
  59. package/dest/store/log_store.js +150 -55
  60. package/dest/store/message_store.d.ts +5 -1
  61. package/dest/store/message_store.d.ts.map +1 -1
  62. package/dest/store/message_store.js +21 -9
  63. package/dest/test/fake_l1_state.d.ts +21 -1
  64. package/dest/test/fake_l1_state.d.ts.map +1 -1
  65. package/dest/test/fake_l1_state.js +133 -26
  66. package/dest/test/mock_archiver.d.ts +1 -1
  67. package/dest/test/mock_archiver.d.ts.map +1 -1
  68. package/dest/test/mock_archiver.js +3 -2
  69. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  70. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  71. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  72. package/dest/test/mock_l2_block_source.d.ts +26 -5
  73. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  74. package/dest/test/mock_l2_block_source.js +160 -89
  75. package/dest/test/mock_structs.d.ts +4 -1
  76. package/dest/test/mock_structs.d.ts.map +1 -1
  77. package/dest/test/mock_structs.js +13 -1
  78. package/dest/test/noop_l1_archiver.d.ts +4 -1
  79. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  80. package/dest/test/noop_l1_archiver.js +5 -2
  81. package/package.json +13 -13
  82. package/src/archiver.ts +106 -139
  83. package/src/config.ts +9 -2
  84. package/src/errors.ts +70 -26
  85. package/src/factory.ts +46 -24
  86. package/src/index.ts +1 -0
  87. package/src/l1/README.md +25 -68
  88. package/src/l1/bin/retrieve-calldata.ts +46 -39
  89. package/src/l1/calldata_retriever.ts +250 -379
  90. package/src/l1/data_retrieval.ts +31 -37
  91. package/src/l1/spire_proposer.ts +7 -15
  92. package/src/modules/data_source_base.ts +78 -98
  93. package/src/modules/data_store_updater.ts +138 -124
  94. package/src/modules/instrumentation.ts +48 -9
  95. package/src/modules/l1_synchronizer.ts +203 -172
  96. package/src/modules/validation.ts +2 -2
  97. package/src/store/block_store.ts +451 -172
  98. package/src/store/contract_class_store.ts +8 -106
  99. package/src/store/contract_instance_store.ts +8 -5
  100. package/src/store/kv_archiver_store.ts +115 -41
  101. package/src/store/l2_tips_cache.ts +134 -0
  102. package/src/store/log_store.ts +221 -63
  103. package/src/store/message_store.ts +27 -10
  104. package/src/structs/inbox_message.ts +1 -1
  105. package/src/test/fake_l1_state.ts +178 -30
  106. package/src/test/mock_archiver.ts +3 -2
  107. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  108. package/src/test/mock_l2_block_source.ts +209 -82
  109. package/src/test/mock_structs.ts +20 -6
  110. package/src/test/noop_l1_archiver.ts +7 -2
@@ -1,7 +1,6 @@
1
1
  import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
2
  import { BlockNumber } from '@aztec/foundation/branded-types';
3
- import { filterAsync } from '@aztec/foundation/collection';
4
- import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import { compactArray, filterAsync } from '@aztec/foundation/collection';
5
4
  import { createLogger } from '@aztec/foundation/log';
6
5
  import { BufferReader, numToUInt32BE } from '@aztec/foundation/serialize';
7
6
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
@@ -20,7 +19,9 @@ import {
20
19
  Tag,
21
20
  TxScopedL2Log,
22
21
  } from '@aztec/stdlib/logs';
22
+ import { TxHash } from '@aztec/stdlib/tx';
23
23
 
24
+ import { OutOfOrderLogInsertionError } from '../errors.js';
24
25
  import type { BlockStore } from './block_store.js';
25
26
 
26
27
  /**
@@ -164,10 +165,21 @@ export class LogStore {
164
165
 
165
166
  for (const taggedLogBuffer of currentPrivateTaggedLogs) {
166
167
  if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
167
- privateTaggedLogs.set(
168
- taggedLogBuffer.tag,
169
- taggedLogBuffer.logBuffers!.concat(privateTaggedLogs.get(taggedLogBuffer.tag)!),
170
- );
168
+ const newLogs = privateTaggedLogs.get(taggedLogBuffer.tag)!;
169
+ if (newLogs.length === 0) {
170
+ continue;
171
+ }
172
+ const lastExisting = TxScopedL2Log.fromBuffer(taggedLogBuffer.logBuffers.at(-1)!);
173
+ const firstNew = TxScopedL2Log.fromBuffer(newLogs[0]);
174
+ if (lastExisting.blockNumber > firstNew.blockNumber) {
175
+ throw new OutOfOrderLogInsertionError(
176
+ 'private',
177
+ taggedLogBuffer.tag,
178
+ lastExisting.blockNumber,
179
+ firstNew.blockNumber,
180
+ );
181
+ }
182
+ privateTaggedLogs.set(taggedLogBuffer.tag, taggedLogBuffer.logBuffers.concat(newLogs));
171
183
  }
172
184
  }
173
185
 
@@ -199,10 +211,21 @@ export class LogStore {
199
211
 
200
212
  for (const taggedLogBuffer of currentPublicTaggedLogs) {
201
213
  if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
202
- publicTaggedLogs.set(
203
- taggedLogBuffer.tag,
204
- taggedLogBuffer.logBuffers!.concat(publicTaggedLogs.get(taggedLogBuffer.tag)!),
205
- );
214
+ const newLogs = publicTaggedLogs.get(taggedLogBuffer.tag)!;
215
+ if (newLogs.length === 0) {
216
+ continue;
217
+ }
218
+ const lastExisting = TxScopedL2Log.fromBuffer(taggedLogBuffer.logBuffers.at(-1)!);
219
+ const firstNew = TxScopedL2Log.fromBuffer(newLogs[0]);
220
+ if (lastExisting.blockNumber > firstNew.blockNumber) {
221
+ throw new OutOfOrderLogInsertionError(
222
+ 'public',
223
+ taggedLogBuffer.tag,
224
+ lastExisting.blockNumber,
225
+ firstNew.blockNumber,
226
+ );
227
+ }
228
+ publicTaggedLogs.set(taggedLogBuffer.tag, taggedLogBuffer.logBuffers.concat(newLogs));
206
229
  }
207
230
  }
208
231
 
@@ -219,6 +242,7 @@ export class LogStore {
219
242
  .map((txEffect, txIndex) =>
220
243
  [
221
244
  numToUInt32BE(txIndex),
245
+ txEffect.txHash.toBuffer(),
222
246
  numToUInt32BE(txEffect.publicLogs.length),
223
247
  txEffect.publicLogs.map(log => log.toBuffer()),
224
248
  ].flat(),
@@ -242,6 +266,7 @@ export class LogStore {
242
266
  .map((txEffect, txIndex) =>
243
267
  [
244
268
  numToUInt32BE(txIndex),
269
+ txEffect.txHash.toBuffer(),
245
270
  numToUInt32BE(txEffect.contractClassLogs.length),
246
271
  txEffect.contractClassLogs.map(log => log.toBuffer()),
247
272
  ].flat(),
@@ -276,29 +301,58 @@ export class LogStore {
276
301
  }
277
302
 
278
303
  #unpackBlockHash(reader: BufferReader): BlockHash {
279
- const blockHash = reader.remainingBytes() > 0 ? reader.readObject(Fr) : undefined;
280
-
281
- if (!blockHash) {
304
+ if (reader.remainingBytes() === 0) {
282
305
  throw new Error('Failed to read block hash from log entry buffer');
283
306
  }
284
307
 
285
- return new BlockHash(blockHash);
308
+ return BlockHash.fromBuffer(reader);
286
309
  }
287
310
 
288
311
  deleteLogs(blocks: L2Block[]): Promise<boolean> {
289
312
  return this.db.transactionAsync(async () => {
290
- await Promise.all(
291
- blocks.map(async block => {
292
- // Delete private logs
293
- const privateKeys = (await this.#privateLogKeysByBlock.getAsync(block.number)) ?? [];
294
- await Promise.all(privateKeys.map(tag => this.#privateLogsByTag.delete(tag)));
295
-
296
- // Delete public logs
297
- const publicKeys = (await this.#publicLogKeysByBlock.getAsync(block.number)) ?? [];
298
- await Promise.all(publicKeys.map(key => this.#publicLogsByContractAndTag.delete(key)));
299
- }),
313
+ const blockNumbers = new Set(blocks.map(block => block.number));
314
+ const firstBlockToDelete = Math.min(...blockNumbers);
315
+
316
+ // Collect all unique private tags across all blocks being deleted
317
+ const allPrivateTags = new Set(
318
+ compactArray(await Promise.all(blocks.map(block => this.#privateLogKeysByBlock.getAsync(block.number)))).flat(),
319
+ );
320
+
321
+ // Trim private logs: for each tag, delete all instances including and after the first block being deleted.
322
+ // This hinges on the invariant that logs for a given tag are always inserted in order of block number, which is enforced in #addPrivateLogs.
323
+ for (const tag of allPrivateTags) {
324
+ const existing = await this.#privateLogsByTag.getAsync(tag);
325
+ if (existing === undefined || existing.length === 0) {
326
+ continue;
327
+ }
328
+ const lastIndexToKeep = existing.findLastIndex(
329
+ buf => TxScopedL2Log.getBlockNumberFromBuffer(buf) < firstBlockToDelete,
330
+ );
331
+ const remaining = existing.slice(0, lastIndexToKeep + 1);
332
+ await (remaining.length > 0 ? this.#privateLogsByTag.set(tag, remaining) : this.#privateLogsByTag.delete(tag));
333
+ }
334
+
335
+ // Collect all unique public keys across all blocks being deleted
336
+ const allPublicKeys = new Set(
337
+ compactArray(await Promise.all(blocks.map(block => this.#publicLogKeysByBlock.getAsync(block.number)))).flat(),
300
338
  );
301
339
 
340
+ // And do the same as we did with private logs
341
+ for (const key of allPublicKeys) {
342
+ const existing = await this.#publicLogsByContractAndTag.getAsync(key);
343
+ if (existing === undefined || existing.length === 0) {
344
+ continue;
345
+ }
346
+ const lastIndexToKeep = existing.findLastIndex(
347
+ buf => TxScopedL2Log.getBlockNumberFromBuffer(buf) < firstBlockToDelete,
348
+ );
349
+ const remaining = existing.slice(0, lastIndexToKeep + 1);
350
+ await (remaining.length > 0
351
+ ? this.#publicLogsByContractAndTag.set(key, remaining)
352
+ : this.#publicLogsByContractAndTag.delete(key));
353
+ }
354
+
355
+ // After trimming the tagged logs, we can delete the block-level keys that track which tags are in which blocks.
302
356
  await Promise.all(
303
357
  blocks.map(block =>
304
358
  Promise.all([
@@ -319,17 +373,30 @@ export class LogStore {
319
373
  * array implies no logs match that tag.
320
374
  * @param tags - The tags to search for.
321
375
  * @param page - The page number (0-indexed) for pagination.
376
+ * @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
322
377
  * @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
323
378
  * MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
324
379
  */
325
- async getPrivateLogsByTags(tags: SiloedTag[], page: number = 0): Promise<TxScopedL2Log[][]> {
380
+ async getPrivateLogsByTags(
381
+ tags: SiloedTag[],
382
+ page: number = 0,
383
+ upToBlockNumber?: BlockNumber,
384
+ ): Promise<TxScopedL2Log[][]> {
326
385
  const logs = await Promise.all(tags.map(tag => this.#privateLogsByTag.getAsync(tag.toString())));
386
+
327
387
  const start = page * MAX_LOGS_PER_TAG;
328
388
  const end = start + MAX_LOGS_PER_TAG;
329
389
 
330
- return logs.map(
331
- logBuffers => logBuffers?.slice(start, end).map(logBuffer => TxScopedL2Log.fromBuffer(logBuffer)) ?? [],
332
- );
390
+ return logs.map(logBuffers => {
391
+ const deserialized = logBuffers?.slice(start, end).map(buf => TxScopedL2Log.fromBuffer(buf)) ?? [];
392
+ if (upToBlockNumber !== undefined) {
393
+ const cutoff = deserialized.findIndex(log => log.blockNumber > upToBlockNumber);
394
+ if (cutoff !== -1) {
395
+ return deserialized.slice(0, cutoff);
396
+ }
397
+ }
398
+ return deserialized;
399
+ });
333
400
  }
334
401
 
335
402
  /**
@@ -338,6 +405,7 @@ export class LogStore {
338
405
  * @param contractAddress - The contract address to search logs for.
339
406
  * @param tags - The tags to search for.
340
407
  * @param page - The page number (0-indexed) for pagination.
408
+ * @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
341
409
  * @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
342
410
  * MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
343
411
  */
@@ -345,6 +413,7 @@ export class LogStore {
345
413
  contractAddress: AztecAddress,
346
414
  tags: Tag[],
347
415
  page: number = 0,
416
+ upToBlockNumber?: BlockNumber,
348
417
  ): Promise<TxScopedL2Log[][]> {
349
418
  const logs = await Promise.all(
350
419
  tags.map(tag => {
@@ -355,9 +424,16 @@ export class LogStore {
355
424
  const start = page * MAX_LOGS_PER_TAG;
356
425
  const end = start + MAX_LOGS_PER_TAG;
357
426
 
358
- return logs.map(
359
- logBuffers => logBuffers?.slice(start, end).map(logBuffer => TxScopedL2Log.fromBuffer(logBuffer)) ?? [],
360
- );
427
+ return logs.map(logBuffers => {
428
+ const deserialized = logBuffers?.slice(start, end).map(buf => TxScopedL2Log.fromBuffer(buf)) ?? [];
429
+ if (upToBlockNumber !== undefined) {
430
+ const cutoff = deserialized.findIndex(log => log.blockNumber > upToBlockNumber);
431
+ if (cutoff !== -1) {
432
+ return deserialized.slice(0, cutoff);
433
+ }
434
+ }
435
+ return deserialized;
436
+ });
361
437
  }
362
438
 
363
439
  /**
@@ -386,24 +462,33 @@ export class LogStore {
386
462
  }
387
463
 
388
464
  const buffer = (await this.#publicLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
389
- const publicLogsInBlock: [PublicLog[]] = [[]];
465
+ const publicLogsInBlock: { txHash: TxHash; logs: PublicLog[] }[] = [];
390
466
  const reader = new BufferReader(buffer);
391
467
 
392
468
  const blockHash = this.#unpackBlockHash(reader);
393
469
 
394
470
  while (reader.remainingBytes() > 0) {
395
471
  const indexOfTx = reader.readNumber();
472
+ const txHash = reader.readObject(TxHash);
396
473
  const numLogsInTx = reader.readNumber();
397
- publicLogsInBlock[indexOfTx] = [];
474
+ publicLogsInBlock[indexOfTx] = { txHash, logs: [] };
398
475
  for (let i = 0; i < numLogsInTx; i++) {
399
- publicLogsInBlock[indexOfTx].push(reader.readObject(PublicLog));
476
+ publicLogsInBlock[indexOfTx].logs.push(reader.readObject(PublicLog));
400
477
  }
401
478
  }
402
479
 
403
- const txLogs = publicLogsInBlock[txIndex];
480
+ const txData = publicLogsInBlock[txIndex];
404
481
 
405
482
  const logs: ExtendedPublicLog[] = [];
406
- const maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
483
+ const maxLogsHit = this.#accumulatePublicLogs(
484
+ logs,
485
+ blockNumber,
486
+ blockHash,
487
+ txIndex,
488
+ txData.txHash,
489
+ txData.logs,
490
+ filter,
491
+ );
407
492
 
408
493
  return { logs, maxLogsHit };
409
494
  }
@@ -424,22 +509,31 @@ export class LogStore {
424
509
 
425
510
  let maxLogsHit = false;
426
511
  loopOverBlocks: for await (const [blockNumber, logBuffer] of this.#publicLogsByBlock.entriesAsync({ start, end })) {
427
- const publicLogsInBlock: [PublicLog[]] = [[]];
512
+ const publicLogsInBlock: { txHash: TxHash; logs: PublicLog[] }[] = [];
428
513
  const reader = new BufferReader(logBuffer);
429
514
 
430
515
  const blockHash = this.#unpackBlockHash(reader);
431
516
 
432
517
  while (reader.remainingBytes() > 0) {
433
518
  const indexOfTx = reader.readNumber();
519
+ const txHash = reader.readObject(TxHash);
434
520
  const numLogsInTx = reader.readNumber();
435
- publicLogsInBlock[indexOfTx] = [];
521
+ publicLogsInBlock[indexOfTx] = { txHash, logs: [] };
436
522
  for (let i = 0; i < numLogsInTx; i++) {
437
- publicLogsInBlock[indexOfTx].push(reader.readObject(PublicLog));
523
+ publicLogsInBlock[indexOfTx].logs.push(reader.readObject(PublicLog));
438
524
  }
439
525
  }
440
526
  for (let txIndex = filter.afterLog?.txIndex ?? 0; txIndex < publicLogsInBlock.length; txIndex++) {
441
- const txLogs = publicLogsInBlock[txIndex];
442
- maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
527
+ const txData = publicLogsInBlock[txIndex];
528
+ maxLogsHit = this.#accumulatePublicLogs(
529
+ logs,
530
+ blockNumber,
531
+ blockHash,
532
+ txIndex,
533
+ txData.txHash,
534
+ txData.logs,
535
+ filter,
536
+ );
443
537
  if (maxLogsHit) {
444
538
  this.#log.debug(`Max logs hit at block ${blockNumber}`);
445
539
  break loopOverBlocks;
@@ -475,24 +569,33 @@ export class LogStore {
475
569
  return { logs: [], maxLogsHit: false };
476
570
  }
477
571
  const contractClassLogsBuffer = (await this.#contractClassLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
478
- const contractClassLogsInBlock: [ContractClassLog[]] = [[]];
572
+ const contractClassLogsInBlock: { txHash: TxHash; logs: ContractClassLog[] }[] = [];
479
573
 
480
574
  const reader = new BufferReader(contractClassLogsBuffer);
481
575
  const blockHash = this.#unpackBlockHash(reader);
482
576
 
483
577
  while (reader.remainingBytes() > 0) {
484
578
  const indexOfTx = reader.readNumber();
579
+ const txHash = reader.readObject(TxHash);
485
580
  const numLogsInTx = reader.readNumber();
486
- contractClassLogsInBlock[indexOfTx] = [];
581
+ contractClassLogsInBlock[indexOfTx] = { txHash, logs: [] };
487
582
  for (let i = 0; i < numLogsInTx; i++) {
488
- contractClassLogsInBlock[indexOfTx].push(reader.readObject(ContractClassLog));
583
+ contractClassLogsInBlock[indexOfTx].logs.push(reader.readObject(ContractClassLog));
489
584
  }
490
585
  }
491
586
 
492
- const txLogs = contractClassLogsInBlock[txIndex];
587
+ const txData = contractClassLogsInBlock[txIndex];
493
588
 
494
589
  const logs: ExtendedContractClassLog[] = [];
495
- const maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
590
+ const maxLogsHit = this.#accumulateContractClassLogs(
591
+ logs,
592
+ blockNumber,
593
+ blockHash,
594
+ txIndex,
595
+ txData.txHash,
596
+ txData.logs,
597
+ filter,
598
+ );
496
599
 
497
600
  return { logs, maxLogsHit };
498
601
  }
@@ -516,20 +619,29 @@ export class LogStore {
516
619
  start,
517
620
  end,
518
621
  })) {
519
- const contractClassLogsInBlock: [ContractClassLog[]] = [[]];
622
+ const contractClassLogsInBlock: { txHash: TxHash; logs: ContractClassLog[] }[] = [];
520
623
  const reader = new BufferReader(logBuffer);
521
624
  const blockHash = this.#unpackBlockHash(reader);
522
625
  while (reader.remainingBytes() > 0) {
523
626
  const indexOfTx = reader.readNumber();
627
+ const txHash = reader.readObject(TxHash);
524
628
  const numLogsInTx = reader.readNumber();
525
- contractClassLogsInBlock[indexOfTx] = [];
629
+ contractClassLogsInBlock[indexOfTx] = { txHash, logs: [] };
526
630
  for (let i = 0; i < numLogsInTx; i++) {
527
- contractClassLogsInBlock[indexOfTx].push(reader.readObject(ContractClassLog));
631
+ contractClassLogsInBlock[indexOfTx].logs.push(reader.readObject(ContractClassLog));
528
632
  }
529
633
  }
530
634
  for (let txIndex = filter.afterLog?.txIndex ?? 0; txIndex < contractClassLogsInBlock.length; txIndex++) {
531
- const txLogs = contractClassLogsInBlock[txIndex];
532
- maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
635
+ const txData = contractClassLogsInBlock[txIndex];
636
+ maxLogsHit = this.#accumulateContractClassLogs(
637
+ logs,
638
+ blockNumber,
639
+ blockHash,
640
+ txIndex,
641
+ txData.txHash,
642
+ txData.logs,
643
+ filter,
644
+ );
533
645
  if (maxLogsHit) {
534
646
  this.#log.debug(`Max logs hit at block ${blockNumber}`);
535
647
  break loopOverBlocks;
@@ -540,28 +652,74 @@ export class LogStore {
540
652
  return { logs, maxLogsHit };
541
653
  }
542
654
 
543
- #accumulateLogs(
544
- results: (ExtendedContractClassLog | ExtendedPublicLog)[],
655
+ #accumulatePublicLogs(
656
+ results: ExtendedPublicLog[],
545
657
  blockNumber: number,
546
658
  blockHash: BlockHash,
547
659
  txIndex: number,
548
- txLogs: (ContractClassLog | PublicLog)[],
660
+ txHash: TxHash,
661
+ txLogs: PublicLog[],
549
662
  filter: LogFilter = {},
550
663
  ): boolean {
664
+ if (filter.fromBlock && blockNumber < filter.fromBlock) {
665
+ return false;
666
+ }
667
+ if (filter.toBlock && blockNumber >= filter.toBlock) {
668
+ return false;
669
+ }
670
+ if (filter.txHash && !txHash.equals(filter.txHash)) {
671
+ return false;
672
+ }
673
+
551
674
  let maxLogsHit = false;
552
675
  let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
553
676
  for (; logIndex < txLogs.length; logIndex++) {
554
677
  const log = txLogs[logIndex];
555
- if (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) {
556
- if (log instanceof ContractClassLog) {
557
- results.push(
558
- new ExtendedContractClassLog(new LogId(BlockNumber(blockNumber), blockHash, txIndex, logIndex), log),
559
- );
560
- } else if (log instanceof PublicLog) {
561
- results.push(new ExtendedPublicLog(new LogId(BlockNumber(blockNumber), blockHash, txIndex, logIndex), log));
562
- } else {
563
- throw new Error('Unknown log type');
678
+ if (
679
+ (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) &&
680
+ (!filter.tag || log.fields[0]?.equals(filter.tag))
681
+ ) {
682
+ results.push(
683
+ new ExtendedPublicLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
684
+ );
685
+
686
+ if (results.length >= this.#logsMaxPageSize) {
687
+ maxLogsHit = true;
688
+ break;
564
689
  }
690
+ }
691
+ }
692
+
693
+ return maxLogsHit;
694
+ }
695
+
696
+ #accumulateContractClassLogs(
697
+ results: ExtendedContractClassLog[],
698
+ blockNumber: number,
699
+ blockHash: BlockHash,
700
+ txIndex: number,
701
+ txHash: TxHash,
702
+ txLogs: ContractClassLog[],
703
+ filter: LogFilter = {},
704
+ ): boolean {
705
+ if (filter.fromBlock && blockNumber < filter.fromBlock) {
706
+ return false;
707
+ }
708
+ if (filter.toBlock && blockNumber >= filter.toBlock) {
709
+ return false;
710
+ }
711
+ if (filter.txHash && !txHash.equals(filter.txHash)) {
712
+ return false;
713
+ }
714
+
715
+ let maxLogsHit = false;
716
+ let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
717
+ for (; logIndex < txLogs.length; logIndex++) {
718
+ const log = txLogs[logIndex];
719
+ if (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) {
720
+ results.push(
721
+ new ExtendedContractClassLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
722
+ );
565
723
 
566
724
  if (results.length >= this.#logsMaxPageSize) {
567
725
  maxLogsHit = true;
@@ -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> {
@@ -137,7 +141,7 @@ export class MessageStore {
137
141
  );
138
142
  }
139
143
 
140
- // Check the first message in a block has the correct index.
144
+ // Check the first message in a checkpoint has the correct index.
141
145
  if (
142
146
  (!lastMessage || message.checkpointNumber > lastMessage.checkpointNumber) &&
143
147
  message.index !== expectedStart
@@ -157,15 +161,6 @@ export class MessageStore {
157
161
  lastMessage = message;
158
162
  }
159
163
 
160
- // Update the L1 sync point to that of the last message added.
161
- const currentSyncPoint = await this.getSynchedL1Block();
162
- if (!currentSyncPoint || currentSyncPoint.l1BlockNumber < lastMessage!.l1BlockNumber) {
163
- await this.setSynchedL1Block({
164
- l1BlockNumber: lastMessage!.l1BlockNumber,
165
- l1BlockHash: lastMessage!.l1BlockHash,
166
- });
167
- }
168
-
169
164
  // Update total message count with the number of inserted messages.
170
165
  await this.increaseTotalMessageCount(messageCount);
171
166
  });
@@ -185,7 +180,29 @@ export class MessageStore {
185
180
  return msg ? deserializeInboxMessage(msg) : undefined;
186
181
  }
187
182
 
183
+ /** Returns the inbox tree-in-progress checkpoint number from L1, or undefined if not yet set. */
184
+ public getInboxTreeInProgress(): Promise<bigint | undefined> {
185
+ return this.#inboxTreeInProgress.getAsync();
186
+ }
187
+
188
+ /** Atomically updates the message sync state: the L1 sync point and the inbox tree-in-progress marker. */
189
+ public setMessageSyncState(l1Block: L1BlockId, treeInProgress: bigint | undefined): Promise<void> {
190
+ return this.db.transactionAsync(async () => {
191
+ await this.setSynchedL1Block(l1Block);
192
+ if (treeInProgress !== undefined) {
193
+ await this.#inboxTreeInProgress.set(treeInProgress);
194
+ } else {
195
+ await this.#inboxTreeInProgress.delete();
196
+ }
197
+ });
198
+ }
199
+
188
200
  public async getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
201
+ const treeInProgress = await this.#inboxTreeInProgress.getAsync();
202
+ if (treeInProgress !== undefined && BigInt(checkpointNumber) >= treeInProgress) {
203
+ throw new L1ToL2MessagesNotReadyError(checkpointNumber, treeInProgress);
204
+ }
205
+
189
206
  const messages: Fr[] = [];
190
207
 
191
208
  const [startIndex, endIndex] = InboxLeaf.indexRangeForCheckpoint(checkpointNumber);
@@ -8,7 +8,7 @@ export type InboxMessage = {
8
8
  index: bigint;
9
9
  leaf: Fr;
10
10
  checkpointNumber: CheckpointNumber;
11
- l1BlockNumber: bigint; // L1 block number - NOT Aztec L2
11
+ l1BlockNumber: bigint;
12
12
  l1BlockHash: Buffer32;
13
13
  rollingHash: Buffer16;
14
14
  };