@aztec/archiver 0.0.1-commit.a072138 → 0.0.1-commit.a89ec08

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 (94) hide show
  1. package/dest/archiver.d.ts +7 -4
  2. package/dest/archiver.d.ts.map +1 -1
  3. package/dest/archiver.js +73 -110
  4. package/dest/errors.d.ts +7 -9
  5. package/dest/errors.d.ts.map +1 -1
  6. package/dest/errors.js +9 -14
  7. package/dest/factory.d.ts +3 -4
  8. package/dest/factory.d.ts.map +1 -1
  9. package/dest/factory.js +31 -24
  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 +4 -7
  18. package/dest/l1/data_retrieval.d.ts.map +1 -1
  19. package/dest/l1/data_retrieval.js +9 -13
  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 +10 -5
  24. package/dest/modules/data_source_base.d.ts.map +1 -1
  25. package/dest/modules/data_source_base.js +29 -73
  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 +113 -40
  29. package/dest/modules/instrumentation.d.ts +4 -2
  30. package/dest/modules/instrumentation.d.ts.map +1 -1
  31. package/dest/modules/instrumentation.js +9 -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 +40 -10
  35. package/dest/store/block_store.d.ts +30 -26
  36. package/dest/store/block_store.d.ts.map +1 -1
  37. package/dest/store/block_store.js +180 -83
  38. package/dest/store/contract_class_store.d.ts +1 -1
  39. package/dest/store/contract_class_store.d.ts.map +1 -1
  40. package/dest/store/contract_class_store.js +6 -2
  41. package/dest/store/contract_instance_store.d.ts +1 -1
  42. package/dest/store/contract_instance_store.d.ts.map +1 -1
  43. package/dest/store/contract_instance_store.js +6 -2
  44. package/dest/store/kv_archiver_store.d.ts +37 -15
  45. package/dest/store/kv_archiver_store.d.ts.map +1 -1
  46. package/dest/store/kv_archiver_store.js +42 -13
  47. package/dest/store/l2_tips_cache.d.ts +19 -0
  48. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  49. package/dest/store/l2_tips_cache.js +89 -0
  50. package/dest/store/log_store.d.ts +1 -1
  51. package/dest/store/log_store.d.ts.map +1 -1
  52. package/dest/store/log_store.js +103 -45
  53. package/dest/store/message_store.js +1 -1
  54. package/dest/test/fake_l1_state.d.ts +20 -1
  55. package/dest/test/fake_l1_state.d.ts.map +1 -1
  56. package/dest/test/fake_l1_state.js +97 -20
  57. package/dest/test/mock_archiver.d.ts +1 -1
  58. package/dest/test/mock_archiver.d.ts.map +1 -1
  59. package/dest/test/mock_archiver.js +3 -2
  60. package/dest/test/mock_l2_block_source.d.ts +21 -5
  61. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  62. package/dest/test/mock_l2_block_source.js +132 -86
  63. package/dest/test/mock_structs.d.ts +4 -1
  64. package/dest/test/mock_structs.d.ts.map +1 -1
  65. package/dest/test/mock_structs.js +13 -1
  66. package/dest/test/noop_l1_archiver.d.ts +4 -1
  67. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  68. package/dest/test/noop_l1_archiver.js +5 -1
  69. package/package.json +13 -13
  70. package/src/archiver.ts +88 -130
  71. package/src/errors.ts +10 -24
  72. package/src/factory.ts +45 -21
  73. package/src/index.ts +1 -0
  74. package/src/l1/README.md +25 -68
  75. package/src/l1/bin/retrieve-calldata.ts +46 -39
  76. package/src/l1/calldata_retriever.ts +249 -379
  77. package/src/l1/data_retrieval.ts +6 -16
  78. package/src/l1/spire_proposer.ts +7 -15
  79. package/src/modules/data_source_base.ts +56 -95
  80. package/src/modules/data_store_updater.ts +123 -43
  81. package/src/modules/instrumentation.ts +9 -2
  82. package/src/modules/l1_synchronizer.ts +47 -14
  83. package/src/store/block_store.ts +219 -110
  84. package/src/store/contract_class_store.ts +7 -3
  85. package/src/store/contract_instance_store.ts +8 -5
  86. package/src/store/kv_archiver_store.ts +66 -20
  87. package/src/store/l2_tips_cache.ts +89 -0
  88. package/src/store/log_store.ts +159 -43
  89. package/src/store/message_store.ts +1 -1
  90. package/src/test/fake_l1_state.ts +125 -21
  91. package/src/test/mock_archiver.ts +3 -2
  92. package/src/test/mock_l2_block_source.ts +173 -81
  93. package/src/test/mock_structs.ts +20 -6
  94. package/src/test/noop_l1_archiver.ts +7 -1
@@ -6,10 +6,17 @@ import { createLogger } from '@aztec/foundation/log';
6
6
  import type { AztecAsyncKVStore, CustomRange, StoreSize } from '@aztec/kv-store';
7
7
  import { FunctionSelector } from '@aztec/stdlib/abi';
8
8
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
9
- import { BlockHash, CheckpointedL2Block, L2Block, type ValidateCheckpointResult } from '@aztec/stdlib/block';
10
- import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
9
+ import {
10
+ type BlockData,
11
+ BlockHash,
12
+ CheckpointedL2Block,
13
+ L2Block,
14
+ type ValidateCheckpointResult,
15
+ } from '@aztec/stdlib/block';
16
+ import type { CheckpointData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
11
17
  import type {
12
18
  ContractClassPublic,
19
+ ContractClassPublicWithCommitment,
13
20
  ContractDataSource,
14
21
  ContractInstanceUpdateWithAddress,
15
22
  ContractInstanceWithAddress,
@@ -25,7 +32,7 @@ import type { UInt64 } from '@aztec/stdlib/types';
25
32
  import { join } from 'path';
26
33
 
27
34
  import type { InboxMessage } from '../structs/inbox_message.js';
28
- import { BlockStore, type CheckpointData, type RemoveCheckpointsResult } from './block_store.js';
35
+ import { BlockStore, type RemoveCheckpointsResult } from './block_store.js';
29
36
  import { ContractClassStore } from './contract_class_store.js';
30
37
  import { ContractInstanceStore } from './contract_instance_store.js';
31
38
  import { LogStore } from './log_store.js';
@@ -65,15 +72,19 @@ export class KVArchiverDataStore implements ContractDataSource {
65
72
  constructor(
66
73
  private db: AztecAsyncKVStore,
67
74
  logsMaxPageSize: number = 1000,
68
- l1Constants: Pick<L1RollupConstants, 'epochDuration'>,
69
75
  ) {
70
- this.#blockStore = new BlockStore(db, l1Constants);
76
+ this.#blockStore = new BlockStore(db);
71
77
  this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize);
72
78
  this.#messageStore = new MessageStore(db);
73
79
  this.#contractClassStore = new ContractClassStore(db);
74
80
  this.#contractInstanceStore = new ContractInstanceStore(db);
75
81
  }
76
82
 
83
+ /** Returns the underlying block store. Used by L2TipsCache. */
84
+ get blockStore(): BlockStore {
85
+ return this.#blockStore;
86
+ }
87
+
77
88
  /** Opens a new transaction to the underlying store and runs all operations within it. */
78
89
  public transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
79
90
  return this.db.transactionAsync(callback);
@@ -157,19 +168,14 @@ export class KVArchiverDataStore implements ContractDataSource {
157
168
 
158
169
  /**
159
170
  * Add new contract classes from an L2 block to the store's list.
160
- * @param data - List of contract classes to be added.
161
- * @param bytecodeCommitments - Bytecode commitments for the contract classes.
171
+ * @param data - List of contract classes (with bytecode commitments) to be added.
162
172
  * @param blockNumber - Number of the L2 block the contracts were registered in.
163
173
  * @returns True if the operation is successful.
164
174
  */
165
- async addContractClasses(
166
- data: ContractClassPublic[],
167
- bytecodeCommitments: Fr[],
168
- blockNumber: BlockNumber,
169
- ): Promise<boolean> {
175
+ async addContractClasses(data: ContractClassPublicWithCommitment[], blockNumber: BlockNumber): Promise<boolean> {
170
176
  return (
171
177
  await Promise.all(
172
- data.map((c, i) => this.#contractClassStore.addContractClass(c, bytecodeCommitments[i], blockNumber)),
178
+ data.map(c => this.#contractClassStore.addContractClass(c, c.publicBytecodeCommitment, blockNumber)),
173
179
  )
174
180
  ).every(Boolean);
175
181
  }
@@ -235,14 +241,14 @@ export class KVArchiverDataStore implements ContractDataSource {
235
241
  }
236
242
 
237
243
  /**
238
- * Append new proposed blocks to the store's list.
239
- * These are uncheckpointed blocks that have been proposed by the sequencer but not yet included in a checkpoint on L1.
244
+ * Append a new proposed block to the store.
245
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
240
246
  * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
241
- * @param blocks - The proposed L2 blocks to be added to the store.
247
+ * @param block - The proposed L2 block to be added to the store.
242
248
  * @returns True if the operation is successful.
243
249
  */
244
- addProposedBlocks(blocks: L2Block[], opts: { force?: boolean; checkpointNumber?: number } = {}): Promise<boolean> {
245
- return this.#blockStore.addProposedBlocks(blocks, opts);
250
+ addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
251
+ return this.#blockStore.addProposedBlock(block, opts);
246
252
  }
247
253
 
248
254
  /**
@@ -369,6 +375,22 @@ export class KVArchiverDataStore implements ContractDataSource {
369
375
  return this.#blockStore.getBlockHeaderByArchive(archive);
370
376
  }
371
377
 
378
+ /**
379
+ * Gets block metadata (without tx data) by block number.
380
+ * @param blockNumber - The block number to return.
381
+ */
382
+ getBlockData(blockNumber: BlockNumber): Promise<BlockData | undefined> {
383
+ return this.#blockStore.getBlockData(blockNumber);
384
+ }
385
+
386
+ /**
387
+ * Gets block metadata (without tx data) by archive root.
388
+ * @param archive - The archive root to return.
389
+ */
390
+ getBlockDataByArchive(archive: Fr): Promise<BlockData | undefined> {
391
+ return this.#blockStore.getBlockDataByArchive(archive);
392
+ }
393
+
372
394
  /**
373
395
  * Gets a tx effect.
374
396
  * @param txHash - The hash of the tx corresponding to the tx effect.
@@ -383,8 +405,11 @@ export class KVArchiverDataStore implements ContractDataSource {
383
405
  * @param txHash - The hash of a tx we try to get the receipt for.
384
406
  * @returns The requested tx receipt (or undefined if not found).
385
407
  */
386
- getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
387
- return this.#blockStore.getSettledTxReceipt(txHash);
408
+ getSettledTxReceipt(
409
+ txHash: TxHash,
410
+ l1Constants?: Pick<L1RollupConstants, 'epochDuration'>,
411
+ ): Promise<TxReceipt | undefined> {
412
+ return this.#blockStore.getSettledTxReceipt(txHash, l1Constants);
388
413
  }
389
414
 
390
415
  /**
@@ -515,6 +540,22 @@ export class KVArchiverDataStore implements ContractDataSource {
515
540
  await this.#blockStore.setProvenCheckpointNumber(checkpointNumber);
516
541
  }
517
542
 
543
+ /**
544
+ * Gets the number of the latest finalized checkpoint processed.
545
+ * @returns The number of the latest finalized checkpoint processed.
546
+ */
547
+ getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
548
+ return this.#blockStore.getFinalizedCheckpointNumber();
549
+ }
550
+
551
+ /**
552
+ * Stores the number of the latest finalized checkpoint processed.
553
+ * @param checkpointNumber - The number of the latest finalized checkpoint processed.
554
+ */
555
+ async setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
556
+ await this.#blockStore.setFinalizedCheckpointNumber(checkpointNumber);
557
+ }
558
+
518
559
  async setBlockSynchedL1BlockNumber(l1BlockNumber: bigint) {
519
560
  await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
520
561
  }
@@ -618,6 +659,11 @@ export class KVArchiverDataStore implements ContractDataSource {
618
659
  return this.#blockStore.getCheckpointData(checkpointNumber);
619
660
  }
620
661
 
662
+ /** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */
663
+ getCheckpointDataForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointData[]> {
664
+ return this.#blockStore.getCheckpointDataForSlotRange(startSlot, endSlot);
665
+ }
666
+
621
667
  /**
622
668
  * Gets all blocks that have the given slot number.
623
669
  * @param slotNumber - The slot number to search for.
@@ -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
+ }
@@ -1,6 +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';
3
+ import { compactArray, filterAsync } from '@aztec/foundation/collection';
4
4
  import { Fr } from '@aztec/foundation/curves/bn254';
5
5
  import { createLogger } from '@aztec/foundation/log';
6
6
  import { BufferReader, numToUInt32BE } from '@aztec/foundation/serialize';
@@ -20,6 +20,7 @@ import {
20
20
  Tag,
21
21
  TxScopedL2Log,
22
22
  } from '@aztec/stdlib/logs';
23
+ import { TxHash } from '@aztec/stdlib/tx';
23
24
 
24
25
  import type { BlockStore } from './block_store.js';
25
26
 
@@ -219,6 +220,7 @@ export class LogStore {
219
220
  .map((txEffect, txIndex) =>
220
221
  [
221
222
  numToUInt32BE(txIndex),
223
+ txEffect.txHash.toBuffer(),
222
224
  numToUInt32BE(txEffect.publicLogs.length),
223
225
  txEffect.publicLogs.map(log => log.toBuffer()),
224
226
  ].flat(),
@@ -242,6 +244,7 @@ export class LogStore {
242
244
  .map((txEffect, txIndex) =>
243
245
  [
244
246
  numToUInt32BE(txIndex),
247
+ txEffect.txHash.toBuffer(),
245
248
  numToUInt32BE(txEffect.contractClassLogs.length),
246
249
  txEffect.contractClassLogs.map(log => log.toBuffer()),
247
250
  ].flat(),
@@ -287,18 +290,49 @@ export class LogStore {
287
290
 
288
291
  deleteLogs(blocks: L2Block[]): Promise<boolean> {
289
292
  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
- }),
293
+ const blockNumbers = new Set(blocks.map(block => block.number));
294
+ const firstBlockToDelete = Math.min(...blockNumbers);
295
+
296
+ // Collect all unique private tags across all blocks being deleted
297
+ const allPrivateTags = new Set(
298
+ compactArray(await Promise.all(blocks.map(block => this.#privateLogKeysByBlock.getAsync(block.number)))).flat(),
299
+ );
300
+
301
+ // Trim private logs: for each tag, delete all instances including and after the first block being deleted.
302
+ // This hinges on the invariant that logs for a given tag are always inserted in order of block number, which is enforced in #addPrivateLogs.
303
+ for (const tag of allPrivateTags) {
304
+ const existing = await this.#privateLogsByTag.getAsync(tag);
305
+ if (existing === undefined || existing.length === 0) {
306
+ continue;
307
+ }
308
+ const lastIndexToKeep = existing.findLastIndex(
309
+ buf => TxScopedL2Log.getBlockNumberFromBuffer(buf) < firstBlockToDelete,
310
+ );
311
+ const remaining = existing.slice(0, lastIndexToKeep + 1);
312
+ await (remaining.length > 0 ? this.#privateLogsByTag.set(tag, remaining) : this.#privateLogsByTag.delete(tag));
313
+ }
314
+
315
+ // Collect all unique public keys across all blocks being deleted
316
+ const allPublicKeys = new Set(
317
+ compactArray(await Promise.all(blocks.map(block => this.#publicLogKeysByBlock.getAsync(block.number)))).flat(),
300
318
  );
301
319
 
320
+ // And do the same as we did with private logs
321
+ for (const key of allPublicKeys) {
322
+ const existing = await this.#publicLogsByContractAndTag.getAsync(key);
323
+ if (existing === undefined || existing.length === 0) {
324
+ continue;
325
+ }
326
+ const lastIndexToKeep = existing.findLastIndex(
327
+ buf => TxScopedL2Log.getBlockNumberFromBuffer(buf) < firstBlockToDelete,
328
+ );
329
+ const remaining = existing.slice(0, lastIndexToKeep + 1);
330
+ await (remaining.length > 0
331
+ ? this.#publicLogsByContractAndTag.set(key, remaining)
332
+ : this.#publicLogsByContractAndTag.delete(key));
333
+ }
334
+
335
+ // After trimming the tagged logs, we can delete the block-level keys that track which tags are in which blocks.
302
336
  await Promise.all(
303
337
  blocks.map(block =>
304
338
  Promise.all([
@@ -386,24 +420,33 @@ export class LogStore {
386
420
  }
387
421
 
388
422
  const buffer = (await this.#publicLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
389
- const publicLogsInBlock: [PublicLog[]] = [[]];
423
+ const publicLogsInBlock: { txHash: TxHash; logs: PublicLog[] }[] = [];
390
424
  const reader = new BufferReader(buffer);
391
425
 
392
426
  const blockHash = this.#unpackBlockHash(reader);
393
427
 
394
428
  while (reader.remainingBytes() > 0) {
395
429
  const indexOfTx = reader.readNumber();
430
+ const txHash = reader.readObject(TxHash);
396
431
  const numLogsInTx = reader.readNumber();
397
- publicLogsInBlock[indexOfTx] = [];
432
+ publicLogsInBlock[indexOfTx] = { txHash, logs: [] };
398
433
  for (let i = 0; i < numLogsInTx; i++) {
399
- publicLogsInBlock[indexOfTx].push(reader.readObject(PublicLog));
434
+ publicLogsInBlock[indexOfTx].logs.push(reader.readObject(PublicLog));
400
435
  }
401
436
  }
402
437
 
403
- const txLogs = publicLogsInBlock[txIndex];
438
+ const txData = publicLogsInBlock[txIndex];
404
439
 
405
440
  const logs: ExtendedPublicLog[] = [];
406
- const maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
441
+ const maxLogsHit = this.#accumulatePublicLogs(
442
+ logs,
443
+ blockNumber,
444
+ blockHash,
445
+ txIndex,
446
+ txData.txHash,
447
+ txData.logs,
448
+ filter,
449
+ );
407
450
 
408
451
  return { logs, maxLogsHit };
409
452
  }
@@ -424,22 +467,31 @@ export class LogStore {
424
467
 
425
468
  let maxLogsHit = false;
426
469
  loopOverBlocks: for await (const [blockNumber, logBuffer] of this.#publicLogsByBlock.entriesAsync({ start, end })) {
427
- const publicLogsInBlock: [PublicLog[]] = [[]];
470
+ const publicLogsInBlock: { txHash: TxHash; logs: PublicLog[] }[] = [];
428
471
  const reader = new BufferReader(logBuffer);
429
472
 
430
473
  const blockHash = this.#unpackBlockHash(reader);
431
474
 
432
475
  while (reader.remainingBytes() > 0) {
433
476
  const indexOfTx = reader.readNumber();
477
+ const txHash = reader.readObject(TxHash);
434
478
  const numLogsInTx = reader.readNumber();
435
- publicLogsInBlock[indexOfTx] = [];
479
+ publicLogsInBlock[indexOfTx] = { txHash, logs: [] };
436
480
  for (let i = 0; i < numLogsInTx; i++) {
437
- publicLogsInBlock[indexOfTx].push(reader.readObject(PublicLog));
481
+ publicLogsInBlock[indexOfTx].logs.push(reader.readObject(PublicLog));
438
482
  }
439
483
  }
440
484
  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);
485
+ const txData = publicLogsInBlock[txIndex];
486
+ maxLogsHit = this.#accumulatePublicLogs(
487
+ logs,
488
+ blockNumber,
489
+ blockHash,
490
+ txIndex,
491
+ txData.txHash,
492
+ txData.logs,
493
+ filter,
494
+ );
443
495
  if (maxLogsHit) {
444
496
  this.#log.debug(`Max logs hit at block ${blockNumber}`);
445
497
  break loopOverBlocks;
@@ -475,24 +527,33 @@ export class LogStore {
475
527
  return { logs: [], maxLogsHit: false };
476
528
  }
477
529
  const contractClassLogsBuffer = (await this.#contractClassLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
478
- const contractClassLogsInBlock: [ContractClassLog[]] = [[]];
530
+ const contractClassLogsInBlock: { txHash: TxHash; logs: ContractClassLog[] }[] = [];
479
531
 
480
532
  const reader = new BufferReader(contractClassLogsBuffer);
481
533
  const blockHash = this.#unpackBlockHash(reader);
482
534
 
483
535
  while (reader.remainingBytes() > 0) {
484
536
  const indexOfTx = reader.readNumber();
537
+ const txHash = reader.readObject(TxHash);
485
538
  const numLogsInTx = reader.readNumber();
486
- contractClassLogsInBlock[indexOfTx] = [];
539
+ contractClassLogsInBlock[indexOfTx] = { txHash, logs: [] };
487
540
  for (let i = 0; i < numLogsInTx; i++) {
488
- contractClassLogsInBlock[indexOfTx].push(reader.readObject(ContractClassLog));
541
+ contractClassLogsInBlock[indexOfTx].logs.push(reader.readObject(ContractClassLog));
489
542
  }
490
543
  }
491
544
 
492
- const txLogs = contractClassLogsInBlock[txIndex];
545
+ const txData = contractClassLogsInBlock[txIndex];
493
546
 
494
547
  const logs: ExtendedContractClassLog[] = [];
495
- const maxLogsHit = this.#accumulateLogs(logs, blockNumber, blockHash, txIndex, txLogs, filter);
548
+ const maxLogsHit = this.#accumulateContractClassLogs(
549
+ logs,
550
+ blockNumber,
551
+ blockHash,
552
+ txIndex,
553
+ txData.txHash,
554
+ txData.logs,
555
+ filter,
556
+ );
496
557
 
497
558
  return { logs, maxLogsHit };
498
559
  }
@@ -516,20 +577,29 @@ export class LogStore {
516
577
  start,
517
578
  end,
518
579
  })) {
519
- const contractClassLogsInBlock: [ContractClassLog[]] = [[]];
580
+ const contractClassLogsInBlock: { txHash: TxHash; logs: ContractClassLog[] }[] = [];
520
581
  const reader = new BufferReader(logBuffer);
521
582
  const blockHash = this.#unpackBlockHash(reader);
522
583
  while (reader.remainingBytes() > 0) {
523
584
  const indexOfTx = reader.readNumber();
585
+ const txHash = reader.readObject(TxHash);
524
586
  const numLogsInTx = reader.readNumber();
525
- contractClassLogsInBlock[indexOfTx] = [];
587
+ contractClassLogsInBlock[indexOfTx] = { txHash, logs: [] };
526
588
  for (let i = 0; i < numLogsInTx; i++) {
527
- contractClassLogsInBlock[indexOfTx].push(reader.readObject(ContractClassLog));
589
+ contractClassLogsInBlock[indexOfTx].logs.push(reader.readObject(ContractClassLog));
528
590
  }
529
591
  }
530
592
  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);
593
+ const txData = contractClassLogsInBlock[txIndex];
594
+ maxLogsHit = this.#accumulateContractClassLogs(
595
+ logs,
596
+ blockNumber,
597
+ blockHash,
598
+ txIndex,
599
+ txData.txHash,
600
+ txData.logs,
601
+ filter,
602
+ );
533
603
  if (maxLogsHit) {
534
604
  this.#log.debug(`Max logs hit at block ${blockNumber}`);
535
605
  break loopOverBlocks;
@@ -540,28 +610,74 @@ export class LogStore {
540
610
  return { logs, maxLogsHit };
541
611
  }
542
612
 
543
- #accumulateLogs(
544
- results: (ExtendedContractClassLog | ExtendedPublicLog)[],
613
+ #accumulatePublicLogs(
614
+ results: ExtendedPublicLog[],
545
615
  blockNumber: number,
546
616
  blockHash: BlockHash,
547
617
  txIndex: number,
548
- txLogs: (ContractClassLog | PublicLog)[],
618
+ txHash: TxHash,
619
+ txLogs: PublicLog[],
549
620
  filter: LogFilter = {},
550
621
  ): boolean {
622
+ if (filter.fromBlock && blockNumber < filter.fromBlock) {
623
+ return false;
624
+ }
625
+ if (filter.toBlock && blockNumber >= filter.toBlock) {
626
+ return false;
627
+ }
628
+ if (filter.txHash && !txHash.equals(filter.txHash)) {
629
+ return false;
630
+ }
631
+
551
632
  let maxLogsHit = false;
552
633
  let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
553
634
  for (; logIndex < txLogs.length; logIndex++) {
554
635
  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');
636
+ if (
637
+ (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) &&
638
+ (!filter.tag || log.fields[0]?.equals(filter.tag))
639
+ ) {
640
+ results.push(
641
+ new ExtendedPublicLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
642
+ );
643
+
644
+ if (results.length >= this.#logsMaxPageSize) {
645
+ maxLogsHit = true;
646
+ break;
564
647
  }
648
+ }
649
+ }
650
+
651
+ return maxLogsHit;
652
+ }
653
+
654
+ #accumulateContractClassLogs(
655
+ results: ExtendedContractClassLog[],
656
+ blockNumber: number,
657
+ blockHash: BlockHash,
658
+ txIndex: number,
659
+ txHash: TxHash,
660
+ txLogs: ContractClassLog[],
661
+ filter: LogFilter = {},
662
+ ): boolean {
663
+ if (filter.fromBlock && blockNumber < filter.fromBlock) {
664
+ return false;
665
+ }
666
+ if (filter.toBlock && blockNumber >= filter.toBlock) {
667
+ return false;
668
+ }
669
+ if (filter.txHash && !txHash.equals(filter.txHash)) {
670
+ return false;
671
+ }
672
+
673
+ let maxLogsHit = false;
674
+ let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
675
+ for (; logIndex < txLogs.length; logIndex++) {
676
+ const log = txLogs[logIndex];
677
+ if (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) {
678
+ results.push(
679
+ new ExtendedContractClassLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
680
+ );
565
681
 
566
682
  if (results.length >= this.#logsMaxPageSize) {
567
683
  maxLogsHit = true;
@@ -137,7 +137,7 @@ export class MessageStore {
137
137
  );
138
138
  }
139
139
 
140
- // Check the first message in a block has the correct index.
140
+ // Check the first message in a checkpoint has the correct index.
141
141
  if (
142
142
  (!lastMessage || message.checkpointNumber > lastMessage.checkpointNumber) &&
143
143
  message.index !== expectedStart