@aztec/archiver 0.0.1-commit.d431d1c → 0.0.1-commit.d58ff9d0

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 (138) hide show
  1. package/README.md +28 -11
  2. package/dest/archiver.d.ts +80 -21
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +347 -167
  5. package/dest/config.d.ts +7 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +38 -14
  8. package/dest/errors.d.ts +60 -9
  9. package/dest/errors.d.ts.map +1 -1
  10. package/dest/errors.js +88 -13
  11. package/dest/factory.d.ts +13 -6
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +59 -39
  14. package/dest/index.d.ts +12 -3
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +11 -2
  17. package/dest/l1/bin/retrieve-calldata.js +35 -32
  18. package/dest/l1/calldata_retriever.d.ts +81 -50
  19. package/dest/l1/calldata_retriever.d.ts.map +1 -1
  20. package/dest/l1/calldata_retriever.js +202 -260
  21. package/dest/l1/data_retrieval.d.ts +32 -18
  22. package/dest/l1/data_retrieval.d.ts.map +1 -1
  23. package/dest/l1/data_retrieval.js +45 -50
  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/trace_tx.d.ts +12 -66
  28. package/dest/l1/trace_tx.d.ts.map +1 -1
  29. package/dest/l1/validate_historical_logs.d.ts +23 -0
  30. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  31. package/dest/l1/validate_historical_logs.js +108 -0
  32. package/dest/l1/validate_trace.d.ts +6 -3
  33. package/dest/l1/validate_trace.d.ts.map +1 -1
  34. package/dest/l1/validate_trace.js +13 -9
  35. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  36. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  37. package/dest/modules/contract_data_source_adapter.js +32 -0
  38. package/dest/modules/data_source_base.d.ts +75 -48
  39. package/dest/modules/data_source_base.d.ts.map +1 -1
  40. package/dest/modules/data_source_base.js +251 -220
  41. package/dest/modules/data_store_updater.d.ts +65 -29
  42. package/dest/modules/data_store_updater.d.ts.map +1 -1
  43. package/dest/modules/data_store_updater.js +242 -153
  44. package/dest/modules/instrumentation.d.ts +30 -4
  45. package/dest/modules/instrumentation.d.ts.map +1 -1
  46. package/dest/modules/instrumentation.js +83 -20
  47. package/dest/modules/l1_synchronizer.d.ts +16 -14
  48. package/dest/modules/l1_synchronizer.d.ts.map +1 -1
  49. package/dest/modules/l1_synchronizer.js +493 -225
  50. package/dest/modules/outbox_trees_resolver.d.ts +62 -0
  51. package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
  52. package/dest/modules/outbox_trees_resolver.js +162 -0
  53. package/dest/modules/validation.d.ts +30 -7
  54. package/dest/modules/validation.d.ts.map +1 -1
  55. package/dest/modules/validation.js +35 -17
  56. package/dest/store/block_store.d.ts +200 -75
  57. package/dest/store/block_store.d.ts.map +1 -1
  58. package/dest/store/block_store.js +802 -276
  59. package/dest/store/contract_class_store.d.ts +17 -4
  60. package/dest/store/contract_class_store.d.ts.map +1 -1
  61. package/dest/store/contract_class_store.js +51 -73
  62. package/dest/store/contract_instance_store.d.ts +28 -1
  63. package/dest/store/contract_instance_store.d.ts.map +1 -1
  64. package/dest/store/contract_instance_store.js +54 -2
  65. package/dest/store/data_stores.d.ts +68 -0
  66. package/dest/store/data_stores.d.ts.map +1 -0
  67. package/dest/store/data_stores.js +54 -0
  68. package/dest/store/function_names_cache.d.ts +17 -0
  69. package/dest/store/function_names_cache.d.ts.map +1 -0
  70. package/dest/store/function_names_cache.js +30 -0
  71. package/dest/store/l2_tips_cache.d.ts +25 -0
  72. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  73. package/dest/store/l2_tips_cache.js +26 -0
  74. package/dest/store/log_store.d.ts +42 -37
  75. package/dest/store/log_store.d.ts.map +1 -1
  76. package/dest/store/log_store.js +262 -388
  77. package/dest/store/log_store_codec.d.ts +78 -0
  78. package/dest/store/log_store_codec.d.ts.map +1 -0
  79. package/dest/store/log_store_codec.js +110 -0
  80. package/dest/store/message_store.d.ts +11 -1
  81. package/dest/store/message_store.d.ts.map +1 -1
  82. package/dest/store/message_store.js +51 -9
  83. package/dest/test/fake_l1_state.d.ts +28 -4
  84. package/dest/test/fake_l1_state.d.ts.map +1 -1
  85. package/dest/test/fake_l1_state.js +166 -32
  86. package/dest/test/index.js +3 -1
  87. package/dest/test/mock_archiver.d.ts +1 -1
  88. package/dest/test/mock_archiver.d.ts.map +1 -1
  89. package/dest/test/mock_archiver.js +3 -2
  90. package/dest/test/mock_l2_block_source.d.ts +65 -51
  91. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  92. package/dest/test/mock_l2_block_source.js +309 -215
  93. package/dest/test/mock_structs.d.ts +6 -2
  94. package/dest/test/mock_structs.d.ts.map +1 -1
  95. package/dest/test/mock_structs.js +26 -12
  96. package/dest/test/noop_l1_archiver.d.ts +34 -0
  97. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  98. package/dest/test/noop_l1_archiver.js +88 -0
  99. package/package.json +15 -14
  100. package/src/archiver.ts +458 -198
  101. package/src/config.ts +43 -13
  102. package/src/errors.ts +134 -21
  103. package/src/factory.ts +87 -36
  104. package/src/index.ts +19 -2
  105. package/src/l1/README.md +25 -68
  106. package/src/l1/bin/retrieve-calldata.ts +45 -33
  107. package/src/l1/calldata_retriever.ts +267 -379
  108. package/src/l1/data_retrieval.ts +70 -74
  109. package/src/l1/spire_proposer.ts +7 -15
  110. package/src/l1/validate_historical_logs.ts +140 -0
  111. package/src/l1/validate_trace.ts +24 -6
  112. package/src/modules/contract_data_source_adapter.ts +46 -0
  113. package/src/modules/data_source_base.ts +336 -288
  114. package/src/modules/data_store_updater.ts +287 -187
  115. package/src/modules/instrumentation.ts +86 -21
  116. package/src/modules/l1_synchronizer.ts +667 -278
  117. package/src/modules/outbox_trees_resolver.ts +199 -0
  118. package/src/modules/validation.ts +80 -23
  119. package/src/store/block_store.ts +984 -352
  120. package/src/store/contract_class_store.ts +57 -107
  121. package/src/store/contract_instance_store.ts +68 -5
  122. package/src/store/data_stores.ts +104 -0
  123. package/src/store/function_names_cache.ts +37 -0
  124. package/src/store/l2_tips_cache.ts +35 -0
  125. package/src/store/log_store.ts +305 -500
  126. package/src/store/log_store_codec.ts +143 -0
  127. package/src/store/message_store.ts +60 -10
  128. package/src/structs/inbox_message.ts +1 -1
  129. package/src/test/fake_l1_state.ts +215 -44
  130. package/src/test/index.ts +3 -0
  131. package/src/test/mock_archiver.ts +3 -2
  132. package/src/test/mock_l2_block_source.ts +381 -246
  133. package/src/test/mock_structs.ts +50 -17
  134. package/src/test/noop_l1_archiver.ts +158 -0
  135. package/dest/store/kv_archiver_store.d.ts +0 -336
  136. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  137. package/dest/store/kv_archiver_store.js +0 -443
  138. package/src/store/kv_archiver_store.ts +0 -631
@@ -1,6 +1,12 @@
1
1
  import { GENESIS_ARCHIVE_ROOT } from '@aztec/constants';
2
2
  import { DefaultL1ContractsConfig } from '@aztec/ethereum/config';
3
- import { BlockNumber, CheckpointNumber, EpochNumber, SlotNumber } from '@aztec/foundation/branded-types';
3
+ import {
4
+ BlockNumber,
5
+ CheckpointNumber,
6
+ EpochNumber,
7
+ IndexWithinCheckpoint,
8
+ SlotNumber,
9
+ } from '@aztec/foundation/branded-types';
4
10
  import { Buffer32 } from '@aztec/foundation/buffer';
5
11
  import { Fr } from '@aztec/foundation/curves/bn254';
6
12
  import { EthAddress } from '@aztec/foundation/eth-address';
@@ -8,48 +14,157 @@ import { createLogger } from '@aztec/foundation/log';
8
14
  import type { FunctionSelector } from '@aztec/stdlib/abi';
9
15
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
10
16
  import {
11
- CheckpointedL2Block,
12
- L2BlockHash,
13
- L2BlockNew,
17
+ type BlockData,
18
+ type BlockHash,
19
+ type BlockQuery,
20
+ type BlockTag,
21
+ type BlocksQuery,
22
+ Body,
23
+ type CheckpointQuery,
24
+ type CheckpointsQuery,
25
+ GENESIS_BLOCK_HEADER_HASH,
26
+ GENESIS_CHECKPOINT_HEADER_HASH,
27
+ L2Block,
14
28
  type L2BlockSource,
15
29
  type L2Tips,
30
+ type ProposedCheckpointQuery,
16
31
  type ValidateCheckpointResult,
17
32
  } from '@aztec/stdlib/block';
18
- import { Checkpoint, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
33
+ import {
34
+ Checkpoint,
35
+ type CheckpointData,
36
+ L1PublishedData,
37
+ type ProposedCheckpointData,
38
+ PublishedCheckpoint,
39
+ } from '@aztec/stdlib/checkpoint';
19
40
  import type { ContractClassPublic, ContractDataSource, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
20
41
  import { EmptyL1RollupConstants, type L1RollupConstants, getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
21
- import { type BlockHeader, TxHash, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
42
+ import { computeCheckpointOutHash } from '@aztec/stdlib/messaging';
43
+ import { CheckpointHeader } from '@aztec/stdlib/rollup';
44
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
45
+ import { BlockHeader, TxHash } from '@aztec/stdlib/tx';
22
46
  import type { UInt64 } from '@aztec/stdlib/types';
23
47
 
24
48
  /**
25
49
  * A mocked implementation of L2BlockSource to be used in tests.
26
50
  */
27
51
  export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
28
- protected l2Blocks: L2BlockNew[] = [];
52
+ protected l2Blocks: L2Block[] = [];
53
+ protected checkpointList: Checkpoint[] = [];
29
54
 
30
55
  private provenBlockNumber: number = 0;
31
56
  private finalizedBlockNumber: number = 0;
32
57
  private checkpointedBlockNumber: number = 0;
33
58
 
59
+ private initialHeader: BlockHeader = BlockHeader.empty();
60
+ private initialHeaderHash: BlockHash = GENESIS_BLOCK_HEADER_HASH;
61
+ private genesisArchiveRoot?: Fr;
62
+ private genesisBlock?: L2Block;
63
+
34
64
  private log = createLogger('archiver:mock_l2_block_source');
35
65
 
66
+ /** Returns the initial header used to synthesize block 0. */
67
+ public getInitialHeader(): BlockHeader {
68
+ return this.initialHeader;
69
+ }
70
+
71
+ /**
72
+ * Sets the initial header used to synthesize block 0. Tests that wire up a real
73
+ * world-state should call this with `worldState.getInitialHeader()` so the L2BlockStream
74
+ * agrees on the genesis hash on both sides. Precomputes and caches the header hash so
75
+ * `getGenesisBlockHash()` can return synchronously.
76
+ */
77
+ public async setInitialHeader(header: BlockHeader): Promise<void> {
78
+ this.initialHeader = header;
79
+ this.initialHeaderHash = await header.hash();
80
+ this.genesisBlock = undefined;
81
+ }
82
+
83
+ /**
84
+ * Returns the precomputed hash of the genesis block header. Defaults to the static
85
+ * {@link GENESIS_BLOCK_HEADER_HASH} unless {@link setInitialHeader} has been called with a
86
+ * custom header.
87
+ */
88
+ public getGenesisBlockHash(): BlockHash {
89
+ return this.initialHeaderHash;
90
+ }
91
+
92
+ /**
93
+ * Sets the post-genesis archive root used to synthesize block 0. Mirrors the real archiver,
94
+ * whose synthetic block 0 carries `new AppendOnlyTreeSnapshot(genesisArchiveRoot, 1)` rather
95
+ * than `AppendOnlyTreeSnapshot.empty()`. Tests wiring up a real world-state should set this so
96
+ * archive-based block lookups against the mock match production semantics.
97
+ */
98
+ public setGenesisArchiveRoot(root: Fr): void {
99
+ this.genesisArchiveRoot = root;
100
+ this.genesisBlock = undefined;
101
+ }
102
+
103
+ private getGenesisBlock(): L2Block {
104
+ if (this.genesisBlock) {
105
+ return this.genesisBlock;
106
+ }
107
+ const archive = this.genesisArchiveRoot
108
+ ? new AppendOnlyTreeSnapshot(this.genesisArchiveRoot, 1)
109
+ : AppendOnlyTreeSnapshot.empty();
110
+ return (this.genesisBlock = new L2Block(
111
+ archive,
112
+ this.initialHeader,
113
+ Body.empty(),
114
+ CheckpointNumber.ZERO,
115
+ IndexWithinCheckpoint(0),
116
+ ));
117
+ }
118
+
119
+ /** Creates blocks grouped into single-block checkpoints. */
36
120
  public async createBlocks(numBlocks: number) {
37
- for (let i = 0; i < numBlocks; i++) {
38
- const blockNum = this.l2Blocks.length + 1;
39
- const block = await L2BlockNew.random(BlockNumber(blockNum), { slotNumber: SlotNumber(blockNum) });
40
- this.l2Blocks.push(block);
121
+ await this.createCheckpoints(numBlocks, 1);
122
+ }
123
+
124
+ public getCheckpointNumber(): Promise<CheckpointNumber> {
125
+ return Promise.resolve(
126
+ this.checkpointList.length === 0 ? CheckpointNumber.ZERO : CheckpointNumber(this.checkpointList.length),
127
+ );
128
+ }
129
+
130
+ /** Creates checkpoints, each containing `blocksPerCheckpoint` blocks. */
131
+ public async createCheckpoints(numCheckpoints: number, blocksPerCheckpoint: number = 1) {
132
+ for (let c = 0; c < numCheckpoints; c++) {
133
+ const checkpointNum = CheckpointNumber(this.checkpointList.length + 1);
134
+ const startBlockNum = this.l2Blocks.length + 1;
135
+ const slotNumber = SlotNumber(Number(checkpointNum));
136
+ const checkpoint = await Checkpoint.random(checkpointNum, {
137
+ numBlocks: blocksPerCheckpoint,
138
+ startBlockNumber: startBlockNum,
139
+ slotNumber,
140
+ checkpointNumber: checkpointNum,
141
+ });
142
+ this.checkpointList.push(checkpoint);
143
+ this.l2Blocks.push(...checkpoint.blocks);
41
144
  }
42
145
 
43
- this.log.verbose(`Created ${numBlocks} blocks in the mock L2 block source`);
146
+ this.log.verbose(
147
+ `Created ${numCheckpoints} checkpoints with ${blocksPerCheckpoint} blocks each in the mock L2 block source`,
148
+ );
44
149
  }
45
150
 
46
- public addBlocks(blocks: L2BlockNew[]) {
151
+ public addProposedBlocks(blocks: L2Block[]) {
47
152
  this.l2Blocks.push(...blocks);
48
- this.log.verbose(`Added ${blocks.length} blocks to the mock L2 block source`);
153
+ this.log.verbose(`Added ${blocks.length} proposed blocks to the mock L2 block source`);
49
154
  }
50
155
 
51
156
  public removeBlocks(numBlocks: number) {
52
157
  this.l2Blocks = this.l2Blocks.slice(0, -numBlocks);
158
+ const maxBlockNum = this.l2Blocks.length;
159
+ // Remove any checkpoint whose last block is beyond the remaining blocks.
160
+ this.checkpointList = this.checkpointList.filter(c => {
161
+ const lastBlockNum = c.blocks[0].number + c.blocks.length - 1;
162
+ return lastBlockNum <= maxBlockNum;
163
+ });
164
+ // Keep tip numbers consistent with remaining blocks.
165
+ this.checkpointedBlockNumber = Math.min(this.checkpointedBlockNumber, maxBlockNum);
166
+ this.provenBlockNumber = Math.min(this.provenBlockNumber, maxBlockNum);
167
+ this.finalizedBlockNumber = Math.min(this.finalizedBlockNumber, maxBlockNum);
53
168
  this.log.verbose(`Removed ${numBlocks} blocks from the mock L2 block source`);
54
169
  }
55
170
 
@@ -65,7 +180,33 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
65
180
  }
66
181
 
67
182
  public setCheckpointedBlockNumber(checkpointedBlockNumber: number) {
183
+ const prevCheckpointed = this.checkpointedBlockNumber;
68
184
  this.checkpointedBlockNumber = checkpointedBlockNumber;
185
+ // Auto-create single-block checkpoints for newly checkpointed blocks that don't have one yet.
186
+ // This handles blocks added via addProposedBlocks that are now being marked as checkpointed.
187
+ const newCheckpoints: Checkpoint[] = [];
188
+ for (let blockNum = prevCheckpointed + 1; blockNum <= checkpointedBlockNumber; blockNum++) {
189
+ const block = this.l2Blocks[blockNum - 1];
190
+ if (!block) {
191
+ continue;
192
+ }
193
+ if (this.checkpointList.some(c => c.blocks.some(b => b.number === block.number))) {
194
+ continue;
195
+ }
196
+ const checkpointNum = CheckpointNumber(this.checkpointList.length + newCheckpoints.length + 1);
197
+ const checkpoint = new Checkpoint(
198
+ block.archive,
199
+ CheckpointHeader.random({ slotNumber: block.header.globalVariables.slotNumber }),
200
+ [block],
201
+ checkpointNum,
202
+ );
203
+ newCheckpoints.push(checkpoint);
204
+ }
205
+ // Insert new checkpoints in order by number.
206
+ if (newCheckpoints.length > 0) {
207
+ this.checkpointList.push(...newCheckpoints);
208
+ this.checkpointList.sort((a, b) => a.number - b.number);
209
+ }
69
210
  }
70
211
 
71
212
  /**
@@ -88,236 +229,115 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
88
229
  * Gets the number of the latest L2 block processed by the block source implementation.
89
230
  * @returns In this mock instance, returns the number of L2 blocks that we've mocked.
90
231
  */
91
- public getBlockNumber() {
92
- return Promise.resolve(BlockNumber(this.l2Blocks.length));
93
- }
94
-
95
- public getProvenBlockNumber() {
96
- return Promise.resolve(BlockNumber(this.provenBlockNumber));
97
- }
98
-
99
- public getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
100
- if (number > this.checkpointedBlockNumber) {
101
- return Promise.resolve(undefined);
232
+ public getBlockNumber(): Promise<BlockNumber>;
233
+ public getBlockNumber(query: BlockQuery): Promise<BlockNumber | undefined>;
234
+ public async getBlockNumber(query?: BlockQuery): Promise<BlockNumber | undefined> {
235
+ if (!query) {
236
+ return BlockNumber(this.l2Blocks.length);
102
237
  }
103
- const block = this.l2Blocks[number - 1];
104
- if (!block) {
105
- return Promise.resolve(undefined);
238
+ if ('number' in query) {
239
+ return query.number;
106
240
  }
107
- const checkpointedBlock = new CheckpointedL2Block(
108
- CheckpointNumber(number),
109
- block,
110
- new L1PublishedData(BigInt(number), BigInt(number), `0x${number.toString(16).padStart(64, '0')}`),
111
- [],
112
- );
113
- return Promise.resolve(checkpointedBlock);
114
- }
115
-
116
- public async getCheckpointedBlocks(
117
- from: BlockNumber,
118
- limit: number,
119
- _proven?: boolean,
120
- ): Promise<CheckpointedL2Block[]> {
121
- const result: CheckpointedL2Block[] = [];
122
- for (let i = 0; i < limit; i++) {
123
- const blockNum = from + i;
124
- if (blockNum > this.checkpointedBlockNumber) {
125
- break;
126
- }
127
- const block = await this.getCheckpointedBlock(BlockNumber(blockNum));
128
- if (block) {
129
- result.push(block);
130
- }
241
+ if ('tag' in query) {
242
+ return BlockNumber(this.resolveBlockTag(query.tag));
131
243
  }
132
- return result;
133
- }
134
-
135
- /**
136
- * Gets an l2 block.
137
- * @param number - The block number to return (inclusive).
138
- * @returns The requested L2 block.
139
- */
140
- public getBlock(number: number): Promise<L2BlockNew | undefined> {
141
- const block = this.l2Blocks[number - 1];
142
- return Promise.resolve(block);
244
+ const block = await this.getBlock(query);
245
+ return block ? block.header.globalVariables.blockNumber : undefined;
143
246
  }
144
247
 
145
- /**
146
- * Gets an L2 block (new format).
147
- * @param number - The block number to return.
148
- * @returns The requested L2 block.
149
- */
150
- public getL2BlockNew(number: BlockNumber): Promise<L2BlockNew | undefined> {
151
- const block = this.l2Blocks[number - 1];
152
- return Promise.resolve(block);
248
+ public getCheckpoint(query: CheckpointQuery): Promise<PublishedCheckpoint | undefined> {
249
+ const checkpoint = this.resolveCheckpointQuery(query);
250
+ if (!checkpoint) {
251
+ return Promise.resolve(undefined);
252
+ }
253
+ return Promise.resolve(new PublishedCheckpoint(checkpoint, this.mockL1DataForCheckpoint(checkpoint), []));
153
254
  }
154
255
 
155
- /**
156
- * Gets up to `limit` amount of L2 blocks starting from `from`.
157
- * @param from - Number of the first block to return (inclusive).
158
- * @param limit - The maximum number of blocks to return.
159
- * @returns The requested mocked L2 blocks.
160
- */
161
- public getBlocks(from: number, limit: number, proven?: boolean): Promise<L2BlockNew[]> {
256
+ public getCheckpoints(query: CheckpointsQuery): Promise<PublishedCheckpoint[]> {
257
+ const checkpoints = this.resolveCheckpointsQuery(query);
162
258
  return Promise.resolve(
163
- this.l2Blocks
164
- .slice(from - 1, from - 1 + limit)
165
- .filter(b => !proven || this.provenBlockNumber === undefined || b.number <= this.provenBlockNumber),
259
+ checkpoints.map(checkpoint => new PublishedCheckpoint(checkpoint, this.mockL1DataForCheckpoint(checkpoint), [])),
166
260
  );
167
261
  }
168
262
 
169
- public getPublishedCheckpoints(from: CheckpointNumber, limit: number) {
170
- // TODO(mbps): Implement this properly. This only works when we have one block per checkpoint.
171
- const blocks = this.l2Blocks.slice(from - 1, from - 1 + limit);
172
- return Promise.all(
173
- blocks.map(async block => {
174
- // Create a checkpoint from the block - manually construct since L2BlockNew doesn't have toCheckpoint()
175
- const checkpoint = await Checkpoint.random(block.checkpointNumber, { numBlocks: 1 });
176
- checkpoint.blocks = [block];
177
- return new PublishedCheckpoint(
178
- checkpoint,
179
- new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
180
- [],
181
- );
182
- }),
183
- );
263
+ public getCheckpointByArchive(archive: Fr): Promise<Checkpoint | undefined> {
264
+ const checkpoint = this.checkpointList.find(c => c.archive.root.equals(archive));
265
+ return Promise.resolve(checkpoint);
184
266
  }
185
267
 
186
- public async getCheckpointByArchive(archive: Fr): Promise<Checkpoint | undefined> {
187
- // TODO(mbps): Implement this properly. This only works when we have one block per checkpoint.
188
- const block = this.l2Blocks.find(b => b.archive.root.equals(archive));
189
- if (!block) {
190
- return undefined;
268
+ public getCheckpointData(query: CheckpointQuery): Promise<CheckpointData | undefined> {
269
+ const checkpoint = this.resolveCheckpointQuery(query);
270
+ if (!checkpoint) {
271
+ return Promise.resolve(undefined);
191
272
  }
192
- // Create a checkpoint from the block - manually construct since L2BlockNew doesn't have toCheckpoint()
193
- const checkpoint = await Checkpoint.random(block.checkpointNumber, { numBlocks: 1 });
194
- checkpoint.blocks = [block];
195
- return checkpoint;
273
+ return Promise.resolve(this.checkpointToData(checkpoint));
196
274
  }
197
275
 
198
- public getPublishedBlocks(from: number, limit: number, proven?: boolean): Promise<CheckpointedL2Block[]> {
199
- const blocks = this.l2Blocks
200
- .slice(from - 1, from - 1 + limit)
201
- .filter(b => !proven || this.provenBlockNumber === undefined || b.number <= this.provenBlockNumber);
202
- return Promise.resolve(
203
- blocks.map(block =>
204
- CheckpointedL2Block.fromFields({
205
- checkpointNumber: CheckpointNumber(block.number),
206
- block,
207
- l1: new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
208
- attestations: [],
209
- }),
210
- ),
211
- );
276
+ public getCheckpointsData(query: CheckpointsQuery): Promise<CheckpointData[]> {
277
+ const checkpoints = this.resolveCheckpointsQuery(query);
278
+ return Promise.resolve(checkpoints.map(c => this.checkpointToData(c)));
212
279
  }
213
280
 
214
- getL2BlocksNew(from: BlockNumber, limit: number, proven?: boolean): Promise<L2BlockNew[]> {
215
- // getBlocks already returns L2BlockNew[], so just return directly
216
- return this.getBlocks(from, limit, proven);
281
+ private checkpointToData(checkpoint: Checkpoint): CheckpointData {
282
+ return {
283
+ checkpointNumber: checkpoint.number,
284
+ header: checkpoint.header,
285
+ archive: checkpoint.archive,
286
+ checkpointOutHash: computeCheckpointOutHash(
287
+ checkpoint.blocks.map(b => b.body.txEffects.map(tx => tx.l2ToL1Msgs)),
288
+ ),
289
+ startBlock: checkpoint.blocks[0].number,
290
+ blockCount: checkpoint.blocks.length,
291
+ feeAssetPriceModifier: checkpoint.feeAssetPriceModifier,
292
+ attestations: [],
293
+ l1: this.mockL1DataForCheckpoint(checkpoint),
294
+ };
217
295
  }
218
296
 
219
- public async getPublishedBlockByHash(blockHash: Fr): Promise<CheckpointedL2Block | undefined> {
220
- for (const block of this.l2Blocks) {
221
- const hash = await block.hash();
222
- if (hash.equals(blockHash)) {
223
- return CheckpointedL2Block.fromFields({
224
- checkpointNumber: CheckpointNumber(block.number),
225
- block,
226
- l1: new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
227
- attestations: [],
228
- });
229
- }
297
+ private resolveCheckpointQuery(query: CheckpointQuery): Checkpoint | undefined {
298
+ if ('number' in query) {
299
+ return this.checkpointList[query.number - 1];
230
300
  }
231
- return undefined;
232
- }
233
-
234
- public getPublishedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
235
- const block = this.l2Blocks.find(b => b.archive.root.equals(archive));
236
- if (!block) {
237
- return Promise.resolve(undefined);
301
+ if ('slot' in query) {
302
+ return this.checkpointList.find(c => c.header.slotNumber === query.slot);
238
303
  }
239
- return Promise.resolve(
240
- CheckpointedL2Block.fromFields({
241
- checkpointNumber: CheckpointNumber(block.number),
242
- block,
243
- l1: new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
244
- attestations: [],
245
- }),
246
- );
247
- }
248
-
249
- public async getL2BlockNewByHash(blockHash: Fr): Promise<L2BlockNew | undefined> {
250
- for (const block of this.l2Blocks) {
251
- const hash = await block.hash();
252
- if (hash.equals(blockHash)) {
253
- return block;
304
+ switch (query.tag) {
305
+ case 'checkpointed':
306
+ return this.checkpointList[this.checkpointList.length - 1];
307
+ case 'proven': {
308
+ const provenCheckpoint = this.checkpointList.filter(c =>
309
+ c.blocks.some(b => b.number <= this.provenBlockNumber),
310
+ );
311
+ return provenCheckpoint.at(-1);
254
312
  }
255
- }
256
- return undefined;
257
- }
258
-
259
- public getL2BlockNewByArchive(archive: Fr): Promise<L2BlockNew | undefined> {
260
- const block = this.l2Blocks.find(b => b.archive.root.equals(archive));
261
- return Promise.resolve(block);
262
- }
263
-
264
- public async getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
265
- for (const block of this.l2Blocks) {
266
- const hash = await block.hash();
267
- if (hash.equals(blockHash)) {
268
- return block.header;
313
+ case 'finalized': {
314
+ const finalizedCheckpoint = this.checkpointList.filter(c =>
315
+ c.blocks.some(b => b.number <= this.finalizedBlockNumber),
316
+ );
317
+ return finalizedCheckpoint.at(-1);
269
318
  }
270
319
  }
271
- return undefined;
272
- }
273
-
274
- public getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
275
- const block = this.l2Blocks.find(b => b.archive.root.equals(archive));
276
- return Promise.resolve(block?.header);
277
- }
278
-
279
- getBlockHeader(number: number | 'latest'): Promise<BlockHeader | undefined> {
280
- return Promise.resolve(this.l2Blocks.at(typeof number === 'number' ? number - 1 : -1)?.header);
281
320
  }
282
321
 
283
- getCheckpointsForEpoch(epochNumber: EpochNumber): Promise<Checkpoint[]> {
284
- // TODO(mbps): Implement this properly. This only works when we have one block per checkpoint.
285
- const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
286
- const [start, end] = getSlotRangeForEpoch(epochNumber, { epochDuration });
287
- const blocks = this.l2Blocks.filter(b => {
288
- const slot = b.header.globalVariables.slotNumber;
289
- return slot >= start && slot <= end;
290
- });
291
- // Create checkpoints from blocks - manually construct since L2BlockNew doesn't have toCheckpoint()
292
- return Promise.all(
293
- blocks.map(async block => {
294
- const checkpoint = await Checkpoint.random(block.checkpointNumber, { numBlocks: 1 });
295
- checkpoint.blocks = [block];
296
- return checkpoint;
297
- }),
298
- );
299
- }
300
-
301
- getBlocksForEpoch(epochNumber: EpochNumber): Promise<L2BlockNew[]> {
302
- const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
303
- const [start, end] = getSlotRangeForEpoch(epochNumber, { epochDuration });
304
- const blocks = this.l2Blocks.filter(b => {
305
- const slot = b.header.globalVariables.slotNumber;
306
- return slot >= start && slot <= end;
307
- });
308
- return Promise.resolve(blocks);
322
+ private resolveCheckpointsQuery(query: CheckpointsQuery): Checkpoint[] {
323
+ if ('from' in query) {
324
+ return this.checkpointList.slice(query.from - 1, query.from - 1 + query.limit);
325
+ }
326
+ if ('fromSlot' in query) {
327
+ const matching = this.checkpointList.filter(c =>
328
+ query.reverse ? c.header.slotNumber <= query.fromSlot : c.header.slotNumber >= query.fromSlot,
329
+ );
330
+ const nearestFirst = query.reverse ? matching.reverse() : matching;
331
+ return nearestFirst.slice(0, query.limit);
332
+ }
333
+ return this.getCheckpointsInEpoch(query.epoch);
309
334
  }
310
335
 
311
- getBlocksForSlot(slotNumber: SlotNumber): Promise<L2BlockNew[]> {
336
+ getBlocksForSlot(slotNumber: SlotNumber): Promise<L2Block[]> {
312
337
  const blocks = this.l2Blocks.filter(b => b.header.globalVariables.slotNumber === slotNumber);
313
338
  return Promise.resolve(blocks);
314
339
  }
315
340
 
316
- async getBlockHeadersForEpoch(epochNumber: EpochNumber): Promise<BlockHeader[]> {
317
- const blocks = await this.getBlocksForEpoch(epochNumber);
318
- return blocks.map(b => b.header);
319
- }
320
-
321
341
  /**
322
342
  * Gets a tx effect.
323
343
  * @param txHash - The hash of the tx corresponding to the tx effect.
@@ -334,38 +354,21 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
334
354
  return {
335
355
  data: txEffect,
336
356
  l2BlockNumber: block.number,
337
- l2BlockHash: L2BlockHash.fromField(await block.hash()),
338
- txIndexInBlock: block.body.txEffects.indexOf(txEffect),
357
+ l2BlockHash: await block.hash(),
358
+ txIndexInBlock: block.body.txEffects.findIndex(t => t.txHash.equals(txHash)),
359
+ slotNumber: block.header.globalVariables.slotNumber,
339
360
  };
340
361
  }
341
362
 
342
- /**
343
- * Gets a receipt of a settled tx.
344
- * @param txHash - The hash of a tx we try to get the receipt for.
345
- * @returns The requested tx receipt (or undefined if not found).
346
- */
347
- public async getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
348
- for (const block of this.l2Blocks) {
349
- for (const txEffect of block.body.txEffects) {
350
- if (txEffect.txHash.equals(txHash)) {
351
- return new TxReceipt(
352
- txHash,
353
- TxStatus.SUCCESS,
354
- '',
355
- txEffect.transactionFee.toBigInt(),
356
- L2BlockHash.fromField(await block.hash()),
357
- block.number,
358
- );
359
- }
360
- }
361
- }
362
- return undefined;
363
+ public getL2ToL1MembershipWitness(): Promise<undefined> {
364
+ // Mock does not back the L2-to-L1 message witness flow.
365
+ return Promise.resolve(undefined);
363
366
  }
364
367
 
365
368
  async getL2Tips(): Promise<L2Tips> {
366
369
  const [latest, proven, finalized, checkpointed] = [
367
370
  await this.getBlockNumber(),
368
- await this.getProvenBlockNumber(),
371
+ this.provenBlockNumber,
369
372
  this.finalizedBlockNumber,
370
373
  this.checkpointedBlockNumber,
371
374
  ] as const;
@@ -375,27 +378,44 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
375
378
  const finalizedBlock = this.l2Blocks[finalized - 1];
376
379
  const checkpointedBlock = this.l2Blocks[checkpointed - 1];
377
380
 
381
+ // For genesis tips (block number 0) report the dynamic initial header hash so consumers
382
+ // running L2BlockStream against this mock agree at block 0 with their local tip store.
383
+ const genesisHash = (await this.initialHeader.hash()).toString();
384
+ const tipHash = async (block: L2Block | undefined, number: number): Promise<string> => {
385
+ if (block) {
386
+ return (await block.hash()).toString();
387
+ }
388
+ return number === 0 ? genesisHash : '';
389
+ };
390
+
378
391
  const latestBlockId = {
379
392
  number: BlockNumber(latest),
380
- hash: (await latestBlock?.hash())?.toString(),
393
+ hash: await tipHash(latestBlock, latest),
381
394
  };
382
395
  const provenBlockId = {
383
396
  number: BlockNumber(proven),
384
- hash: (await provenBlock?.hash())?.toString(),
397
+ hash: await tipHash(provenBlock, proven),
385
398
  };
386
399
  const finalizedBlockId = {
387
400
  number: BlockNumber(finalized),
388
- hash: (await finalizedBlock?.hash())?.toString(),
401
+ hash: await tipHash(finalizedBlock, finalized),
389
402
  };
390
403
  const checkpointedBlockId = {
391
404
  number: BlockNumber(checkpointed),
392
- hash: (await checkpointedBlock?.hash())?.toString(),
405
+ hash: await tipHash(checkpointedBlock, checkpointed),
393
406
  };
394
407
 
395
- const makeTipId = (blockId: typeof latestBlockId) => ({
396
- block: blockId,
397
- checkpoint: { number: CheckpointNumber(blockId.number), hash: blockId.hash },
398
- });
408
+ const makeTipId = (blockId: typeof latestBlockId) => {
409
+ const checkpointNumber = this.findCheckpointNumberForBlock(blockId.number) ?? CheckpointNumber(0);
410
+ // Match production semantics: checkpoint 0 is fully synthetic (no real checkpoint header
411
+ // exists at 0), so its hash stays at the protocol constant `GENESIS_CHECKPOINT_HEADER_HASH`
412
+ // even though the block-0 hash is dynamic. See L2TipsCache for the production path.
413
+ const hash = checkpointNumber === 0 ? GENESIS_CHECKPOINT_HEADER_HASH.toString() : blockId.hash;
414
+ return {
415
+ block: blockId,
416
+ checkpoint: { number: checkpointNumber, hash },
417
+ };
418
+ };
399
419
 
400
420
  return {
401
421
  proposed: latestBlockId,
@@ -405,11 +425,11 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
405
425
  };
406
426
  }
407
427
 
408
- getL2EpochNumber(): Promise<EpochNumber> {
428
+ getSyncedL2EpochNumber(): Promise<EpochNumber> {
409
429
  throw new Error('Method not implemented.');
410
430
  }
411
431
 
412
- getL2SlotNumber(): Promise<SlotNumber> {
432
+ getSyncedL2SlotNumber(): Promise<SlotNumber> {
413
433
  throw new Error('Method not implemented.');
414
434
  }
415
435
 
@@ -421,8 +441,12 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
421
441
  return Promise.resolve(EmptyL1RollupConstants);
422
442
  }
423
443
 
444
+ isPruneDueAtSlot(_slot: SlotNumber): Promise<boolean> {
445
+ return Promise.resolve(false);
446
+ }
447
+
424
448
  getGenesisValues(): Promise<{ genesisArchiveRoot: Fr }> {
425
- return Promise.resolve({ genesisArchiveRoot: new Fr(GENESIS_ARCHIVE_ROOT) });
449
+ return Promise.resolve({ genesisArchiveRoot: this.genesisArchiveRoot ?? new Fr(GENESIS_ARCHIVE_ROOT) });
426
450
  }
427
451
 
428
452
  getL1Timestamp(): Promise<bigint> {
@@ -475,6 +499,95 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
475
499
  return Promise.resolve();
476
500
  }
477
501
 
502
+ async getBlock(query: BlockQuery): Promise<L2Block | undefined> {
503
+ if ('number' in query) {
504
+ if (query.number === 0) {
505
+ return this.getGenesisBlock();
506
+ }
507
+ return this.l2Blocks[query.number - 1];
508
+ }
509
+ if ('hash' in query) {
510
+ const genesis = this.getGenesisBlock();
511
+ if ((await genesis.hash()).equals(query.hash)) {
512
+ return genesis;
513
+ }
514
+ for (const b of this.l2Blocks) {
515
+ const hash = await b.hash();
516
+ if (hash.equals(query.hash)) {
517
+ return b;
518
+ }
519
+ }
520
+ return undefined;
521
+ }
522
+ if ('archive' in query) {
523
+ const genesis = this.getGenesisBlock();
524
+ if (genesis.archive.root.equals(query.archive)) {
525
+ return genesis;
526
+ }
527
+ return this.l2Blocks.find(b => b.archive.root.equals(query.archive));
528
+ }
529
+ const number = this.resolveBlockTag(query.tag);
530
+ if (number === 0) {
531
+ return this.getGenesisBlock();
532
+ }
533
+ return this.l2Blocks[number - 1];
534
+ }
535
+
536
+ private resolveBlockTag(tag: BlockTag): number {
537
+ switch (tag) {
538
+ case 'latest':
539
+ case 'proposed':
540
+ return this.l2Blocks.length;
541
+ case 'checkpointed':
542
+ return this.checkpointedBlockNumber;
543
+ case 'proven':
544
+ return this.provenBlockNumber;
545
+ case 'finalized':
546
+ return this.finalizedBlockNumber;
547
+ }
548
+ }
549
+
550
+ getBlocks(query: BlocksQuery): Promise<L2Block[]> {
551
+ let blocks: L2Block[];
552
+ if ('from' in query) {
553
+ blocks = this.l2Blocks.slice(query.from - 1, query.from - 1 + query.limit);
554
+ } else {
555
+ const epochCheckpoints = this.getCheckpointsInEpoch(query.epoch);
556
+ blocks = epochCheckpoints.flatMap(c => c.blocks);
557
+ }
558
+ if (query.onlyCheckpointed) {
559
+ blocks = blocks.filter(b => b.header.globalVariables.blockNumber <= this.checkpointedBlockNumber);
560
+ }
561
+ return Promise.resolve(blocks);
562
+ }
563
+
564
+ async getBlockData(query: BlockQuery): Promise<BlockData | undefined> {
565
+ const block = await this.getBlock(query);
566
+ if (!block) {
567
+ return undefined;
568
+ }
569
+ return {
570
+ header: block.header,
571
+ archive: block.archive,
572
+ blockHash: await block.hash(),
573
+ checkpointNumber: block.checkpointNumber,
574
+ indexWithinCheckpoint: block.indexWithinCheckpoint,
575
+ };
576
+ }
577
+
578
+ async getBlocksData(query: BlocksQuery): Promise<BlockData[]> {
579
+ const blocks = await this.getBlocks(query);
580
+ return Promise.all(
581
+ blocks.map(async block => ({
582
+ header: block.header,
583
+ archive: block.archive,
584
+ blockHash: await block.hash(),
585
+ checkpointNumber: block.checkpointNumber,
586
+ indexWithinCheckpoint: block.indexWithinCheckpoint,
587
+ })),
588
+ );
589
+ }
590
+
478
591
  isPendingChainInvalid(): Promise<boolean> {
479
592
  return Promise.resolve(false);
480
593
  }
@@ -482,4 +595,26 @@ export class MockL2BlockSource implements L2BlockSource, ContractDataSource {
482
595
  getPendingChainValidationStatus(): Promise<ValidateCheckpointResult> {
483
596
  return Promise.resolve({ valid: true });
484
597
  }
598
+
599
+ getProposedCheckpointData(_query?: ProposedCheckpointQuery): Promise<ProposedCheckpointData | undefined> {
600
+ return Promise.resolve(undefined);
601
+ }
602
+
603
+ /** Returns checkpoints whose slot falls within the given epoch. */
604
+ private getCheckpointsInEpoch(epochNumber: EpochNumber): Checkpoint[] {
605
+ const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
606
+ const [start, end] = getSlotRangeForEpoch(epochNumber, { epochDuration });
607
+ return this.checkpointList.filter(c => c.header.slotNumber >= start && c.header.slotNumber <= end);
608
+ }
609
+
610
+ /** Creates a mock L1PublishedData for a checkpoint. */
611
+ private mockL1DataForCheckpoint(checkpoint: Checkpoint): L1PublishedData {
612
+ return new L1PublishedData(BigInt(checkpoint.number), BigInt(checkpoint.number), Buffer32.random().toString());
613
+ }
614
+
615
+ /** Finds the checkpoint number for a block, or undefined if the block is not in any checkpoint. */
616
+ private findCheckpointNumberForBlock(blockNumber: BlockNumber): CheckpointNumber | undefined {
617
+ const checkpoint = this.checkpointList.find(c => c.blocks.some(b => b.number === blockNumber));
618
+ return checkpoint?.number;
619
+ }
485
620
  }