@aztec/archiver 0.0.1-commit.e3c1de76 → 0.0.1-commit.e57c76e

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