@aztec/archiver 0.0.1-commit.d1f2d6c → 0.0.1-commit.d20b825a7

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 (120) hide show
  1. package/README.md +12 -6
  2. package/dest/archiver.d.ts +16 -10
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +110 -122
  5. package/dest/config.d.ts +5 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +15 -3
  8. package/dest/errors.d.ts +55 -10
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +74 -15
  11. package/dest/factory.d.ts +5 -4
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +34 -29
  14. package/dest/index.d.ts +4 -2
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +3 -1
  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 +26 -17
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +43 -48
  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/l1/validate_trace.d.ts +6 -3
  31. package/dest/l1/validate_trace.d.ts.map +1 -1
  32. package/dest/l1/validate_trace.js +13 -9
  33. package/dest/modules/data_source_base.d.ts +17 -10
  34. package/dest/modules/data_source_base.d.ts.map +1 -1
  35. package/dest/modules/data_source_base.js +39 -77
  36. package/dest/modules/data_store_updater.d.ts +50 -26
  37. package/dest/modules/data_store_updater.d.ts.map +1 -1
  38. package/dest/modules/data_store_updater.js +169 -130
  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 +58 -18
  42. package/dest/modules/l1_synchronizer.d.ts +10 -9
  43. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  44. package/dest/modules/l1_synchronizer.js +285 -157
  45. package/dest/modules/validation.d.ts +1 -1
  46. package/dest/modules/validation.d.ts.map +1 -1
  47. package/dest/modules/validation.js +2 -2
  48. package/dest/store/block_store.d.ts +85 -36
  49. package/dest/store/block_store.d.ts.map +1 -1
  50. package/dest/store/block_store.js +433 -162
  51. package/dest/store/contract_class_store.d.ts +2 -3
  52. package/dest/store/contract_class_store.d.ts.map +1 -1
  53. package/dest/store/contract_class_store.js +16 -72
  54. package/dest/store/contract_instance_store.d.ts +1 -1
  55. package/dest/store/contract_instance_store.d.ts.map +1 -1
  56. package/dest/store/contract_instance_store.js +6 -2
  57. package/dest/store/kv_archiver_store.d.ts +76 -32
  58. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  59. package/dest/store/kv_archiver_store.js +92 -37
  60. package/dest/store/l2_tips_cache.d.ts +20 -0
  61. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  62. package/dest/store/l2_tips_cache.js +109 -0
  63. package/dest/store/log_store.d.ts +6 -3
  64. package/dest/store/log_store.d.ts.map +1 -1
  65. package/dest/store/log_store.js +151 -56
  66. package/dest/store/message_store.d.ts +5 -1
  67. package/dest/store/message_store.d.ts.map +1 -1
  68. package/dest/store/message_store.js +21 -9
  69. package/dest/test/fake_l1_state.d.ts +24 -1
  70. package/dest/test/fake_l1_state.d.ts.map +1 -1
  71. package/dest/test/fake_l1_state.js +145 -28
  72. package/dest/test/index.js +3 -1
  73. package/dest/test/mock_archiver.d.ts +1 -1
  74. package/dest/test/mock_archiver.d.ts.map +1 -1
  75. package/dest/test/mock_archiver.js +3 -2
  76. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  77. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  78. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  79. package/dest/test/mock_l2_block_source.d.ts +31 -10
  80. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  81. package/dest/test/mock_l2_block_source.js +163 -92
  82. package/dest/test/mock_structs.d.ts +6 -2
  83. package/dest/test/mock_structs.d.ts.map +1 -1
  84. package/dest/test/mock_structs.js +20 -6
  85. package/dest/test/noop_l1_archiver.d.ts +26 -0
  86. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  87. package/dest/test/noop_l1_archiver.js +74 -0
  88. package/package.json +14 -13
  89. package/src/archiver.ts +150 -146
  90. package/src/config.ts +22 -2
  91. package/src/errors.ts +116 -26
  92. package/src/factory.ts +49 -26
  93. package/src/index.ts +3 -1
  94. package/src/l1/README.md +25 -68
  95. package/src/l1/bin/retrieve-calldata.ts +46 -39
  96. package/src/l1/calldata_retriever.ts +250 -379
  97. package/src/l1/data_retrieval.ts +59 -70
  98. package/src/l1/spire_proposer.ts +7 -15
  99. package/src/l1/validate_historical_logs.ts +140 -0
  100. package/src/l1/validate_trace.ts +24 -6
  101. package/src/modules/data_source_base.ts +81 -101
  102. package/src/modules/data_store_updater.ts +202 -160
  103. package/src/modules/instrumentation.ts +71 -19
  104. package/src/modules/l1_synchronizer.ts +365 -197
  105. package/src/modules/validation.ts +2 -2
  106. package/src/store/block_store.ts +546 -206
  107. package/src/store/contract_class_store.ts +16 -110
  108. package/src/store/contract_instance_store.ts +8 -5
  109. package/src/store/kv_archiver_store.ts +143 -53
  110. package/src/store/l2_tips_cache.ts +134 -0
  111. package/src/store/log_store.ts +225 -67
  112. package/src/store/message_store.ts +27 -10
  113. package/src/structs/inbox_message.ts +1 -1
  114. package/src/test/fake_l1_state.ts +193 -32
  115. package/src/test/index.ts +3 -0
  116. package/src/test/mock_archiver.ts +3 -2
  117. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  118. package/src/test/mock_l2_block_source.ts +217 -90
  119. package/src/test/mock_structs.ts +42 -12
  120. package/src/test/noop_l1_archiver.ts +117 -0
@@ -0,0 +1,134 @@
1
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
3
+ import {
4
+ type BlockData,
5
+ type CheckpointId,
6
+ GENESIS_BLOCK_HEADER_HASH,
7
+ GENESIS_CHECKPOINT_HEADER_HASH,
8
+ type L2Tips,
9
+ } from '@aztec/stdlib/block';
10
+
11
+ import type { BlockStore } from './block_store.js';
12
+
13
+ /**
14
+ * In-memory cache for L2 chain tips (proposed, checkpointed, proven, finalized).
15
+ * Populated from the BlockStore on first access, then kept up-to-date by the ArchiverDataStoreUpdater.
16
+ * Refresh calls should happen within the store transaction that mutates block data to ensure consistency.
17
+ */
18
+ export class L2TipsCache {
19
+ #tipsPromise: Promise<L2Tips> | undefined;
20
+
21
+ constructor(private blockStore: BlockStore) {}
22
+
23
+ /** Returns the cached L2 tips. Loads from the block store on first call. */
24
+ public getL2Tips(): Promise<L2Tips> {
25
+ return (this.#tipsPromise ??= this.loadFromStore());
26
+ }
27
+
28
+ /** Reloads the L2 tips from the block store. Should be called within the store transaction that mutates data. */
29
+ public async refresh(): Promise<void> {
30
+ this.#tipsPromise = this.loadFromStore();
31
+ await this.#tipsPromise;
32
+ }
33
+
34
+ private async loadFromStore(): Promise<L2Tips> {
35
+ const [
36
+ latestBlockNumber,
37
+ provenBlockNumber,
38
+ proposedCheckpointBlockNumber,
39
+ checkpointedBlockNumber,
40
+ finalizedBlockNumber,
41
+ ] = await Promise.all([
42
+ this.blockStore.getLatestL2BlockNumber(),
43
+ this.blockStore.getProvenBlockNumber(),
44
+ this.blockStore.getProposedCheckpointL2BlockNumber(),
45
+ this.blockStore.getCheckpointedL2BlockNumber(),
46
+ this.blockStore.getFinalizedL2BlockNumber(),
47
+ ]);
48
+
49
+ const genesisBlockHeader = {
50
+ blockHash: GENESIS_BLOCK_HEADER_HASH,
51
+ checkpointNumber: CheckpointNumber.ZERO,
52
+ } as const;
53
+ const beforeInitialBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
54
+
55
+ const getBlockData = (blockNumber: BlockNumber) =>
56
+ blockNumber > beforeInitialBlockNumber ? this.blockStore.getBlockData(blockNumber) : genesisBlockHeader;
57
+
58
+ const [latestBlockData, provenBlockData, proposedCheckpointBlockData, checkpointedBlockData, finalizedBlockData] =
59
+ await Promise.all(
60
+ [
61
+ latestBlockNumber,
62
+ provenBlockNumber,
63
+ proposedCheckpointBlockNumber,
64
+ checkpointedBlockNumber,
65
+ finalizedBlockNumber,
66
+ ].map(getBlockData),
67
+ );
68
+
69
+ if (
70
+ !latestBlockData ||
71
+ !provenBlockData ||
72
+ !finalizedBlockData ||
73
+ !checkpointedBlockData ||
74
+ !proposedCheckpointBlockData
75
+ ) {
76
+ throw new Error('Failed to load block data for L2 tips');
77
+ }
78
+
79
+ const [provenCheckpointId, finalizedCheckpointId, proposedCheckpointId, checkpointedCheckpointId] =
80
+ await Promise.all([
81
+ this.getCheckpointIdForBlock(provenBlockData),
82
+ this.getCheckpointIdForBlock(finalizedBlockData),
83
+ this.getCheckpointIdForProposedCheckpoint(checkpointedBlockData),
84
+ this.getCheckpointIdForBlock(checkpointedBlockData),
85
+ ]);
86
+
87
+ return {
88
+ proposed: { number: latestBlockNumber, hash: latestBlockData.blockHash.toString() },
89
+ proven: {
90
+ block: { number: provenBlockNumber, hash: provenBlockData.blockHash.toString() },
91
+ checkpoint: provenCheckpointId,
92
+ },
93
+ proposedCheckpoint: {
94
+ block: { number: proposedCheckpointBlockNumber, hash: proposedCheckpointBlockData.blockHash.toString() },
95
+ checkpoint: proposedCheckpointId,
96
+ },
97
+ finalized: {
98
+ block: { number: finalizedBlockNumber, hash: finalizedBlockData.blockHash.toString() },
99
+ checkpoint: finalizedCheckpointId,
100
+ },
101
+ checkpointed: {
102
+ block: { number: checkpointedBlockNumber, hash: checkpointedBlockData.blockHash.toString() },
103
+ checkpoint: checkpointedCheckpointId,
104
+ },
105
+ };
106
+ }
107
+
108
+ private async getCheckpointIdForProposedCheckpoint(
109
+ checkpointedBlockData: Pick<BlockData, 'checkpointNumber'>,
110
+ ): Promise<CheckpointId> {
111
+ const checkpointData = await this.blockStore.getProposedCheckpointOnly();
112
+ if (!checkpointData) {
113
+ return this.getCheckpointIdForBlock(checkpointedBlockData);
114
+ }
115
+ return {
116
+ number: checkpointData.checkpointNumber,
117
+ hash: checkpointData.header.hash().toString(),
118
+ };
119
+ }
120
+
121
+ private async getCheckpointIdForBlock(blockData: Pick<BlockData, 'checkpointNumber'>): Promise<CheckpointId> {
122
+ const checkpointData = await this.blockStore.getCheckpointData(blockData.checkpointNumber);
123
+ if (!checkpointData) {
124
+ return {
125
+ number: CheckpointNumber.ZERO,
126
+ hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
127
+ };
128
+ }
129
+ return {
130
+ number: checkpointData.checkpointNumber,
131
+ hash: checkpointData.header.hash().toString(),
132
+ };
133
+ }
134
+ }
@@ -1,12 +1,11 @@
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';
8
7
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
9
- import { L2Block, L2BlockHash } from '@aztec/stdlib/block';
8
+ import { BlockHash, L2Block } from '@aztec/stdlib/block';
10
9
  import { MAX_LOGS_PER_TAG } from '@aztec/stdlib/interfaces/api-limit';
11
10
  import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
12
11
  import {
@@ -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(),
@@ -271,34 +296,63 @@ export class LogStore {
271
296
  });
272
297
  }
273
298
 
274
- #packWithBlockHash(blockHash: Fr, data: Buffer<ArrayBufferLike>[]): Buffer<ArrayBufferLike> {
299
+ #packWithBlockHash(blockHash: BlockHash, data: Buffer<ArrayBufferLike>[]): Buffer<ArrayBufferLike> {
275
300
  return Buffer.concat([blockHash.toBuffer(), ...data]);
276
301
  }
277
302
 
278
- #unpackBlockHash(reader: BufferReader): L2BlockHash {
279
- const blockHash = reader.remainingBytes() > 0 ? reader.readObject(Fr) : undefined;
280
-
281
- if (!blockHash) {
303
+ #unpackBlockHash(reader: BufferReader): 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 L2BlockHash.fromField(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
- blockHash: L2BlockHash,
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;