@aztec/archiver 0.0.1-commit.86469d5 → 0.0.1-commit.88c5703d4

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 (88) hide show
  1. package/dest/archiver.d.ts +7 -4
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +62 -110
  4. package/dest/errors.d.ts +21 -9
  5. package/dest/errors.d.ts.map +1 -1
  6. package/dest/errors.js +27 -14
  7. package/dest/factory.d.ts +3 -4
  8. package/dest/factory.d.ts.map +1 -1
  9. package/dest/factory.js +24 -20
  10. package/dest/index.d.ts +2 -1
  11. package/dest/index.d.ts.map +1 -1
  12. package/dest/index.js +1 -0
  13. package/dest/l1/bin/retrieve-calldata.js +36 -33
  14. package/dest/l1/calldata_retriever.d.ts +73 -50
  15. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  16. package/dest/l1/calldata_retriever.js +190 -259
  17. package/dest/l1/data_retrieval.d.ts +9 -9
  18. package/dest/l1/data_retrieval.d.ts.map +1 -1
  19. package/dest/l1/data_retrieval.js +21 -19
  20. package/dest/l1/spire_proposer.d.ts +5 -5
  21. package/dest/l1/spire_proposer.d.ts.map +1 -1
  22. package/dest/l1/spire_proposer.js +9 -17
  23. package/dest/modules/data_source_base.d.ts +12 -7
  24. package/dest/modules/data_source_base.d.ts.map +1 -1
  25. package/dest/modules/data_source_base.js +33 -77
  26. package/dest/modules/data_store_updater.d.ts +22 -7
  27. package/dest/modules/data_store_updater.d.ts.map +1 -1
  28. package/dest/modules/data_store_updater.js +69 -29
  29. package/dest/modules/instrumentation.d.ts +15 -2
  30. package/dest/modules/instrumentation.d.ts.map +1 -1
  31. package/dest/modules/instrumentation.js +19 -2
  32. package/dest/modules/l1_synchronizer.d.ts +5 -8
  33. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  34. package/dest/modules/l1_synchronizer.js +42 -10
  35. package/dest/store/block_store.d.ts +29 -26
  36. package/dest/store/block_store.d.ts.map +1 -1
  37. package/dest/store/block_store.js +130 -78
  38. package/dest/store/kv_archiver_store.d.ts +40 -13
  39. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  40. package/dest/store/kv_archiver_store.js +48 -13
  41. package/dest/store/l2_tips_cache.d.ts +19 -0
  42. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  43. package/dest/store/l2_tips_cache.js +89 -0
  44. package/dest/store/log_store.d.ts +6 -3
  45. package/dest/store/log_store.d.ts.map +1 -1
  46. package/dest/store/log_store.js +100 -41
  47. package/dest/store/message_store.d.ts +5 -1
  48. package/dest/store/message_store.d.ts.map +1 -1
  49. package/dest/store/message_store.js +14 -1
  50. package/dest/test/fake_l1_state.d.ts +13 -1
  51. package/dest/test/fake_l1_state.d.ts.map +1 -1
  52. package/dest/test/fake_l1_state.js +95 -23
  53. package/dest/test/mock_archiver.d.ts +1 -1
  54. package/dest/test/mock_archiver.d.ts.map +1 -1
  55. package/dest/test/mock_archiver.js +3 -2
  56. package/dest/test/mock_l2_block_source.d.ts +21 -5
  57. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  58. package/dest/test/mock_l2_block_source.js +132 -86
  59. package/dest/test/mock_structs.d.ts +4 -1
  60. package/dest/test/mock_structs.d.ts.map +1 -1
  61. package/dest/test/mock_structs.js +13 -1
  62. package/dest/test/noop_l1_archiver.d.ts +4 -1
  63. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  64. package/dest/test/noop_l1_archiver.js +5 -1
  65. package/package.json +13 -13
  66. package/src/archiver.ts +74 -130
  67. package/src/errors.ts +40 -24
  68. package/src/factory.ts +34 -17
  69. package/src/index.ts +1 -0
  70. package/src/l1/README.md +25 -68
  71. package/src/l1/bin/retrieve-calldata.ts +46 -39
  72. package/src/l1/calldata_retriever.ts +249 -379
  73. package/src/l1/data_retrieval.ts +23 -25
  74. package/src/l1/spire_proposer.ts +7 -15
  75. package/src/modules/data_source_base.ts +64 -98
  76. package/src/modules/data_store_updater.ts +71 -30
  77. package/src/modules/instrumentation.ts +29 -2
  78. package/src/modules/l1_synchronizer.ts +47 -14
  79. package/src/store/block_store.ts +157 -105
  80. package/src/store/kv_archiver_store.ts +73 -15
  81. package/src/store/l2_tips_cache.ts +89 -0
  82. package/src/store/log_store.ts +153 -46
  83. package/src/store/message_store.ts +20 -1
  84. package/src/test/fake_l1_state.ts +125 -26
  85. package/src/test/mock_archiver.ts +3 -2
  86. package/src/test/mock_l2_block_source.ts +173 -81
  87. package/src/test/mock_structs.ts +20 -6
  88. package/src/test/noop_l1_archiver.ts +7 -1
@@ -0,0 +1,89 @@
1
+ import { GENESIS_BLOCK_HEADER_HASH, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
3
+ import { type BlockData, type CheckpointId, GENESIS_CHECKPOINT_HEADER_HASH, type L2Tips } from '@aztec/stdlib/block';
4
+
5
+ import type { BlockStore } from './block_store.js';
6
+
7
+ /**
8
+ * In-memory cache for L2 chain tips (proposed, checkpointed, proven, finalized).
9
+ * Populated from the BlockStore on first access, then kept up-to-date by the ArchiverDataStoreUpdater.
10
+ * Refresh calls should happen within the store transaction that mutates block data to ensure consistency.
11
+ */
12
+ export class L2TipsCache {
13
+ #tipsPromise: Promise<L2Tips> | undefined;
14
+
15
+ constructor(private blockStore: BlockStore) {}
16
+
17
+ /** Returns the cached L2 tips. Loads from the block store on first call. */
18
+ public getL2Tips(): Promise<L2Tips> {
19
+ return (this.#tipsPromise ??= this.loadFromStore());
20
+ }
21
+
22
+ /** Reloads the L2 tips from the block store. Should be called within the store transaction that mutates data. */
23
+ public async refresh(): Promise<void> {
24
+ this.#tipsPromise = this.loadFromStore();
25
+ await this.#tipsPromise;
26
+ }
27
+
28
+ private async loadFromStore(): Promise<L2Tips> {
29
+ const [latestBlockNumber, provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber] = await Promise.all([
30
+ this.blockStore.getLatestBlockNumber(),
31
+ this.blockStore.getProvenBlockNumber(),
32
+ this.blockStore.getCheckpointedL2BlockNumber(),
33
+ this.blockStore.getFinalizedL2BlockNumber(),
34
+ ]);
35
+
36
+ const genesisBlockHeader = {
37
+ blockHash: GENESIS_BLOCK_HEADER_HASH,
38
+ checkpointNumber: CheckpointNumber.ZERO,
39
+ } as const;
40
+ const beforeInitialBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
41
+
42
+ const getBlockData = (blockNumber: BlockNumber) =>
43
+ blockNumber > beforeInitialBlockNumber ? this.blockStore.getBlockData(blockNumber) : genesisBlockHeader;
44
+
45
+ const [latestBlockData, provenBlockData, checkpointedBlockData, finalizedBlockData] = await Promise.all(
46
+ [latestBlockNumber, provenBlockNumber, checkpointedBlockNumber, finalizedBlockNumber].map(getBlockData),
47
+ );
48
+
49
+ if (!latestBlockData || !provenBlockData || !finalizedBlockData || !checkpointedBlockData) {
50
+ throw new Error('Failed to load block data for L2 tips');
51
+ }
52
+
53
+ const [provenCheckpointId, finalizedCheckpointId, checkpointedCheckpointId] = await Promise.all([
54
+ this.getCheckpointIdForBlock(provenBlockData),
55
+ this.getCheckpointIdForBlock(finalizedBlockData),
56
+ this.getCheckpointIdForBlock(checkpointedBlockData),
57
+ ]);
58
+
59
+ return {
60
+ proposed: { number: latestBlockNumber, hash: latestBlockData.blockHash.toString() },
61
+ proven: {
62
+ block: { number: provenBlockNumber, hash: provenBlockData.blockHash.toString() },
63
+ checkpoint: provenCheckpointId,
64
+ },
65
+ finalized: {
66
+ block: { number: finalizedBlockNumber, hash: finalizedBlockData.blockHash.toString() },
67
+ checkpoint: finalizedCheckpointId,
68
+ },
69
+ checkpointed: {
70
+ block: { number: checkpointedBlockNumber, hash: checkpointedBlockData.blockHash.toString() },
71
+ checkpoint: checkpointedCheckpointId,
72
+ },
73
+ };
74
+ }
75
+
76
+ private async getCheckpointIdForBlock(blockData: Pick<BlockData, 'checkpointNumber'>): Promise<CheckpointId> {
77
+ const checkpointData = await this.blockStore.getCheckpointData(blockData.checkpointNumber);
78
+ if (!checkpointData) {
79
+ return {
80
+ number: CheckpointNumber.ZERO,
81
+ hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
82
+ };
83
+ }
84
+ return {
85
+ number: checkpointData.checkpointNumber,
86
+ hash: checkpointData.header.hash().toString(),
87
+ };
88
+ }
89
+ }
@@ -20,7 +20,9 @@ import {
20
20
  Tag,
21
21
  TxScopedL2Log,
22
22
  } from '@aztec/stdlib/logs';
23
+ import { TxHash } from '@aztec/stdlib/tx';
23
24
 
25
+ import { OutOfOrderLogInsertionError } from '../errors.js';
24
26
  import type { BlockStore } from './block_store.js';
25
27
 
26
28
  /**
@@ -164,10 +166,21 @@ export class LogStore {
164
166
 
165
167
  for (const taggedLogBuffer of currentPrivateTaggedLogs) {
166
168
  if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
167
- privateTaggedLogs.set(
168
- taggedLogBuffer.tag,
169
- taggedLogBuffer.logBuffers!.concat(privateTaggedLogs.get(taggedLogBuffer.tag)!),
170
- );
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));
171
184
  }
172
185
  }
173
186
 
@@ -199,10 +212,21 @@ export class LogStore {
199
212
 
200
213
  for (const taggedLogBuffer of currentPublicTaggedLogs) {
201
214
  if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
202
- publicTaggedLogs.set(
203
- taggedLogBuffer.tag,
204
- taggedLogBuffer.logBuffers!.concat(publicTaggedLogs.get(taggedLogBuffer.tag)!),
205
- );
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));
206
230
  }
207
231
  }
208
232
 
@@ -219,6 +243,7 @@ export class LogStore {
219
243
  .map((txEffect, txIndex) =>
220
244
  [
221
245
  numToUInt32BE(txIndex),
246
+ txEffect.txHash.toBuffer(),
222
247
  numToUInt32BE(txEffect.publicLogs.length),
223
248
  txEffect.publicLogs.map(log => log.toBuffer()),
224
249
  ].flat(),
@@ -242,6 +267,7 @@ export class LogStore {
242
267
  .map((txEffect, txIndex) =>
243
268
  [
244
269
  numToUInt32BE(txIndex),
270
+ txEffect.txHash.toBuffer(),
245
271
  numToUInt32BE(txEffect.contractClassLogs.length),
246
272
  txEffect.contractClassLogs.map(log => log.toBuffer()),
247
273
  ].flat(),
@@ -319,17 +345,30 @@ export class LogStore {
319
345
  * array implies no logs match that tag.
320
346
  * @param tags - The tags to search for.
321
347
  * @param page - The page number (0-indexed) for pagination.
348
+ * @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
322
349
  * @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
323
350
  * MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
324
351
  */
325
- async getPrivateLogsByTags(tags: SiloedTag[], page: number = 0): Promise<TxScopedL2Log[][]> {
352
+ async getPrivateLogsByTags(
353
+ tags: SiloedTag[],
354
+ page: number = 0,
355
+ upToBlockNumber?: BlockNumber,
356
+ ): Promise<TxScopedL2Log[][]> {
326
357
  const logs = await Promise.all(tags.map(tag => this.#privateLogsByTag.getAsync(tag.toString())));
358
+
327
359
  const start = page * MAX_LOGS_PER_TAG;
328
360
  const end = start + MAX_LOGS_PER_TAG;
329
361
 
330
- return logs.map(
331
- logBuffers => logBuffers?.slice(start, end).map(logBuffer => TxScopedL2Log.fromBuffer(logBuffer)) ?? [],
332
- );
362
+ return logs.map(logBuffers => {
363
+ const deserialized = logBuffers?.slice(start, end).map(buf => TxScopedL2Log.fromBuffer(buf)) ?? [];
364
+ if (upToBlockNumber !== undefined) {
365
+ const cutoff = deserialized.findIndex(log => log.blockNumber > upToBlockNumber);
366
+ if (cutoff !== -1) {
367
+ return deserialized.slice(0, cutoff);
368
+ }
369
+ }
370
+ return deserialized;
371
+ });
333
372
  }
334
373
 
335
374
  /**
@@ -338,6 +377,7 @@ export class LogStore {
338
377
  * @param contractAddress - The contract address to search logs for.
339
378
  * @param tags - The tags to search for.
340
379
  * @param page - The page number (0-indexed) for pagination.
380
+ * @param upToBlockNumber - If set, only return logs from blocks up to and including this block number.
341
381
  * @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
342
382
  * MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
343
383
  */
@@ -345,6 +385,7 @@ export class LogStore {
345
385
  contractAddress: AztecAddress,
346
386
  tags: Tag[],
347
387
  page: number = 0,
388
+ upToBlockNumber?: BlockNumber,
348
389
  ): Promise<TxScopedL2Log[][]> {
349
390
  const logs = await Promise.all(
350
391
  tags.map(tag => {
@@ -355,9 +396,16 @@ export class LogStore {
355
396
  const start = page * MAX_LOGS_PER_TAG;
356
397
  const end = start + MAX_LOGS_PER_TAG;
357
398
 
358
- return logs.map(
359
- logBuffers => logBuffers?.slice(start, end).map(logBuffer => TxScopedL2Log.fromBuffer(logBuffer)) ?? [],
360
- );
399
+ return logs.map(logBuffers => {
400
+ const deserialized = logBuffers?.slice(start, end).map(buf => TxScopedL2Log.fromBuffer(buf)) ?? [];
401
+ if (upToBlockNumber !== undefined) {
402
+ const cutoff = deserialized.findIndex(log => log.blockNumber > upToBlockNumber);
403
+ if (cutoff !== -1) {
404
+ return deserialized.slice(0, cutoff);
405
+ }
406
+ }
407
+ return deserialized;
408
+ });
361
409
  }
362
410
 
363
411
  /**
@@ -386,24 +434,33 @@ export class LogStore {
386
434
  }
387
435
 
388
436
  const buffer = (await this.#publicLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
389
- const publicLogsInBlock: [PublicLog[]] = [[]];
437
+ const publicLogsInBlock: { txHash: TxHash; logs: PublicLog[] }[] = [];
390
438
  const reader = new BufferReader(buffer);
391
439
 
392
440
  const blockHash = this.#unpackBlockHash(reader);
393
441
 
394
442
  while (reader.remainingBytes() > 0) {
395
443
  const indexOfTx = reader.readNumber();
444
+ const txHash = reader.readObject(TxHash);
396
445
  const numLogsInTx = reader.readNumber();
397
- publicLogsInBlock[indexOfTx] = [];
446
+ publicLogsInBlock[indexOfTx] = { txHash, logs: [] };
398
447
  for (let i = 0; i < numLogsInTx; i++) {
399
- publicLogsInBlock[indexOfTx].push(reader.readObject(PublicLog));
448
+ publicLogsInBlock[indexOfTx].logs.push(reader.readObject(PublicLog));
400
449
  }
401
450
  }
402
451
 
403
- const txLogs = publicLogsInBlock[txIndex];
452
+ const txData = publicLogsInBlock[txIndex];
404
453
 
405
454
  const logs: ExtendedPublicLog[] = [];
406
- const maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
455
+ const maxLogsHit = this.#accumulatePublicLogs(
456
+ logs,
457
+ blockNumber,
458
+ blockHash,
459
+ txIndex,
460
+ txData.txHash,
461
+ txData.logs,
462
+ filter,
463
+ );
407
464
 
408
465
  return { logs, maxLogsHit };
409
466
  }
@@ -424,22 +481,31 @@ export class LogStore {
424
481
 
425
482
  let maxLogsHit = false;
426
483
  loopOverBlocks: for await (const [blockNumber, logBuffer] of this.#publicLogsByBlock.entriesAsync({ start, end })) {
427
- const publicLogsInBlock: [PublicLog[]] = [[]];
484
+ const publicLogsInBlock: { txHash: TxHash; logs: PublicLog[] }[] = [];
428
485
  const reader = new BufferReader(logBuffer);
429
486
 
430
487
  const blockHash = this.#unpackBlockHash(reader);
431
488
 
432
489
  while (reader.remainingBytes() > 0) {
433
490
  const indexOfTx = reader.readNumber();
491
+ const txHash = reader.readObject(TxHash);
434
492
  const numLogsInTx = reader.readNumber();
435
- publicLogsInBlock[indexOfTx] = [];
493
+ publicLogsInBlock[indexOfTx] = { txHash, logs: [] };
436
494
  for (let i = 0; i < numLogsInTx; i++) {
437
- publicLogsInBlock[indexOfTx].push(reader.readObject(PublicLog));
495
+ publicLogsInBlock[indexOfTx].logs.push(reader.readObject(PublicLog));
438
496
  }
439
497
  }
440
498
  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);
499
+ const txData = publicLogsInBlock[txIndex];
500
+ maxLogsHit = this.#accumulatePublicLogs(
501
+ logs,
502
+ blockNumber,
503
+ blockHash,
504
+ txIndex,
505
+ txData.txHash,
506
+ txData.logs,
507
+ filter,
508
+ );
443
509
  if (maxLogsHit) {
444
510
  this.#log.debug(`Max logs hit at block ${blockNumber}`);
445
511
  break loopOverBlocks;
@@ -475,24 +541,33 @@ export class LogStore {
475
541
  return { logs: [], maxLogsHit: false };
476
542
  }
477
543
  const contractClassLogsBuffer = (await this.#contractClassLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
478
- const contractClassLogsInBlock: [ContractClassLog[]] = [[]];
544
+ const contractClassLogsInBlock: { txHash: TxHash; logs: ContractClassLog[] }[] = [];
479
545
 
480
546
  const reader = new BufferReader(contractClassLogsBuffer);
481
547
  const blockHash = this.#unpackBlockHash(reader);
482
548
 
483
549
  while (reader.remainingBytes() > 0) {
484
550
  const indexOfTx = reader.readNumber();
551
+ const txHash = reader.readObject(TxHash);
485
552
  const numLogsInTx = reader.readNumber();
486
- contractClassLogsInBlock[indexOfTx] = [];
553
+ contractClassLogsInBlock[indexOfTx] = { txHash, logs: [] };
487
554
  for (let i = 0; i < numLogsInTx; i++) {
488
- contractClassLogsInBlock[indexOfTx].push(reader.readObject(ContractClassLog));
555
+ contractClassLogsInBlock[indexOfTx].logs.push(reader.readObject(ContractClassLog));
489
556
  }
490
557
  }
491
558
 
492
- const txLogs = contractClassLogsInBlock[txIndex];
559
+ const txData = contractClassLogsInBlock[txIndex];
493
560
 
494
561
  const logs: ExtendedContractClassLog[] = [];
495
- const maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
562
+ const maxLogsHit = this.#accumulateContractClassLogs(
563
+ logs,
564
+ blockNumber,
565
+ blockHash,
566
+ txIndex,
567
+ txData.txHash,
568
+ txData.logs,
569
+ filter,
570
+ );
496
571
 
497
572
  return { logs, maxLogsHit };
498
573
  }
@@ -516,20 +591,29 @@ export class LogStore {
516
591
  start,
517
592
  end,
518
593
  })) {
519
- const contractClassLogsInBlock: [ContractClassLog[]] = [[]];
594
+ const contractClassLogsInBlock: { txHash: TxHash; logs: ContractClassLog[] }[] = [];
520
595
  const reader = new BufferReader(logBuffer);
521
596
  const blockHash = this.#unpackBlockHash(reader);
522
597
  while (reader.remainingBytes() > 0) {
523
598
  const indexOfTx = reader.readNumber();
599
+ const txHash = reader.readObject(TxHash);
524
600
  const numLogsInTx = reader.readNumber();
525
- contractClassLogsInBlock[indexOfTx] = [];
601
+ contractClassLogsInBlock[indexOfTx] = { txHash, logs: [] };
526
602
  for (let i = 0; i < numLogsInTx; i++) {
527
- contractClassLogsInBlock[indexOfTx].push(reader.readObject(ContractClassLog));
603
+ contractClassLogsInBlock[indexOfTx].logs.push(reader.readObject(ContractClassLog));
528
604
  }
529
605
  }
530
606
  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);
607
+ const txData = contractClassLogsInBlock[txIndex];
608
+ maxLogsHit = this.#accumulateContractClassLogs(
609
+ logs,
610
+ blockNumber,
611
+ blockHash,
612
+ txIndex,
613
+ txData.txHash,
614
+ txData.logs,
615
+ filter,
616
+ );
533
617
  if (maxLogsHit) {
534
618
  this.#log.debug(`Max logs hit at block ${blockNumber}`);
535
619
  break loopOverBlocks;
@@ -540,12 +624,13 @@ export class LogStore {
540
624
  return { logs, maxLogsHit };
541
625
  }
542
626
 
543
- #accumulateLogs(
544
- results: (ExtendedContractClassLog | ExtendedPublicLog)[],
627
+ #accumulatePublicLogs(
628
+ results: ExtendedPublicLog[],
545
629
  blockNumber: number,
546
630
  blockHash: BlockHash,
547
631
  txIndex: number,
548
- txLogs: (ContractClassLog | PublicLog)[],
632
+ txHash: TxHash,
633
+ txLogs: PublicLog[],
549
634
  filter: LogFilter = {},
550
635
  ): boolean {
551
636
  let maxLogsHit = false;
@@ -553,15 +638,37 @@ export class LogStore {
553
638
  for (; logIndex < txLogs.length; logIndex++) {
554
639
  const log = txLogs[logIndex];
555
640
  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');
641
+ results.push(
642
+ new ExtendedPublicLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
643
+ );
644
+
645
+ if (results.length >= this.#logsMaxPageSize) {
646
+ maxLogsHit = true;
647
+ break;
564
648
  }
649
+ }
650
+ }
651
+
652
+ return maxLogsHit;
653
+ }
654
+
655
+ #accumulateContractClassLogs(
656
+ results: ExtendedContractClassLog[],
657
+ blockNumber: number,
658
+ blockHash: BlockHash,
659
+ txIndex: number,
660
+ txHash: TxHash,
661
+ txLogs: ContractClassLog[],
662
+ filter: LogFilter = {},
663
+ ): boolean {
664
+ let maxLogsHit = false;
665
+ let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
666
+ for (; logIndex < txLogs.length; logIndex++) {
667
+ const log = txLogs[logIndex];
668
+ if (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) {
669
+ results.push(
670
+ new ExtendedContractClassLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
671
+ );
565
672
 
566
673
  if (results.length >= this.#logsMaxPageSize) {
567
674
  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
@@ -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);