@aztec/archiver 0.0.1-commit.86469d5 → 0.0.1-commit.8655d4a

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 (132) hide show
  1. package/README.md +19 -11
  2. package/dest/archiver.d.ts +39 -17
  3. package/dest/archiver.d.ts.map +1 -1
  4. package/dest/archiver.js +260 -160
  5. package/dest/config.d.ts +6 -3
  6. package/dest/config.d.ts.map +1 -1
  7. package/dest/config.js +23 -15
  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 +13 -9
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +51 -37
  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 +42 -47
  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 +71 -42
  36. package/dest/modules/data_source_base.d.ts.map +1 -1
  37. package/dest/modules/data_source_base.js +270 -179
  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/validation.d.ts +4 -3
  48. package/dest/modules/validation.d.ts.map +1 -1
  49. package/dest/modules/validation.js +6 -6
  50. package/dest/store/block_store.d.ts +174 -66
  51. package/dest/store/block_store.d.ts.map +1 -1
  52. package/dest/store/block_store.js +743 -245
  53. package/dest/store/contract_class_store.d.ts +17 -4
  54. package/dest/store/contract_class_store.d.ts.map +1 -1
  55. package/dest/store/contract_class_store.js +24 -68
  56. package/dest/store/contract_instance_store.d.ts +28 -1
  57. package/dest/store/contract_instance_store.d.ts.map +1 -1
  58. package/dest/store/contract_instance_store.js +37 -2
  59. package/dest/store/data_stores.d.ts +68 -0
  60. package/dest/store/data_stores.d.ts.map +1 -0
  61. package/dest/store/data_stores.js +54 -0
  62. package/dest/store/function_names_cache.d.ts +17 -0
  63. package/dest/store/function_names_cache.d.ts.map +1 -0
  64. package/dest/store/function_names_cache.js +30 -0
  65. package/dest/store/l2_tips_cache.d.ts +25 -0
  66. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  67. package/dest/store/l2_tips_cache.js +26 -0
  68. package/dest/store/log_store.d.ts +42 -37
  69. package/dest/store/log_store.d.ts.map +1 -1
  70. package/dest/store/log_store.js +262 -388
  71. package/dest/store/log_store_codec.d.ts +70 -0
  72. package/dest/store/log_store_codec.d.ts.map +1 -0
  73. package/dest/store/log_store_codec.js +101 -0
  74. package/dest/store/message_store.d.ts +11 -1
  75. package/dest/store/message_store.d.ts.map +1 -1
  76. package/dest/store/message_store.js +51 -9
  77. package/dest/test/fake_l1_state.d.ts +25 -1
  78. package/dest/test/fake_l1_state.d.ts.map +1 -1
  79. package/dest/test/fake_l1_state.js +166 -32
  80. package/dest/test/mock_archiver.d.ts +1 -1
  81. package/dest/test/mock_archiver.d.ts.map +1 -1
  82. package/dest/test/mock_archiver.js +3 -2
  83. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  84. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  85. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  86. package/dest/test/mock_l2_block_source.d.ts +62 -41
  87. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  88. package/dest/test/mock_l2_block_source.js +321 -202
  89. package/dest/test/mock_structs.d.ts +4 -1
  90. package/dest/test/mock_structs.d.ts.map +1 -1
  91. package/dest/test/mock_structs.js +13 -1
  92. package/dest/test/noop_l1_archiver.d.ts +12 -6
  93. package/dest/test/noop_l1_archiver.d.ts.map +1 -1
  94. package/dest/test/noop_l1_archiver.js +26 -9
  95. package/package.json +14 -14
  96. package/src/archiver.ts +319 -181
  97. package/src/config.ts +32 -12
  98. package/src/errors.ts +122 -21
  99. package/src/factory.ts +75 -36
  100. package/src/index.ts +19 -2
  101. package/src/l1/README.md +25 -68
  102. package/src/l1/bin/retrieve-calldata.ts +46 -39
  103. package/src/l1/calldata_retriever.ts +260 -379
  104. package/src/l1/data_retrieval.ts +58 -69
  105. package/src/l1/spire_proposer.ts +7 -15
  106. package/src/l1/validate_historical_logs.ts +140 -0
  107. package/src/modules/contract_data_source_adapter.ts +55 -0
  108. package/src/modules/data_source_base.ts +347 -221
  109. package/src/modules/data_store_updater.ts +248 -153
  110. package/src/modules/instrumentation.ts +56 -9
  111. package/src/modules/l1_synchronizer.ts +585 -258
  112. package/src/modules/validation.ts +10 -9
  113. package/src/store/block_store.ts +924 -300
  114. package/src/store/contract_class_store.ts +31 -103
  115. package/src/store/contract_instance_store.ts +51 -5
  116. package/src/store/data_stores.ts +104 -0
  117. package/src/store/function_names_cache.ts +37 -0
  118. package/src/store/l2_tips_cache.ts +35 -0
  119. package/src/store/log_store.ts +303 -499
  120. package/src/store/log_store_codec.ts +132 -0
  121. package/src/store/message_store.ts +60 -10
  122. package/src/structs/inbox_message.ts +1 -1
  123. package/src/test/fake_l1_state.ts +213 -42
  124. package/src/test/mock_archiver.ts +3 -2
  125. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  126. package/src/test/mock_l2_block_source.ts +394 -210
  127. package/src/test/mock_structs.ts +20 -6
  128. package/src/test/noop_l1_archiver.ts +39 -9
  129. package/dest/store/kv_archiver_store.d.ts +0 -340
  130. package/dest/store/kv_archiver_store.d.ts.map +0 -1
  131. package/dest/store/kv_archiver_store.js +0 -446
  132. package/src/store/kv_archiver_store.ts +0 -639
@@ -1,31 +1,88 @@
1
1
  import { GENESIS_ARCHIVE_ROOT } from '@aztec/constants';
2
2
  import { DefaultL1ContractsConfig } from '@aztec/ethereum/config';
3
- import { BlockNumber, CheckpointNumber, SlotNumber } from '@aztec/foundation/branded-types';
3
+ import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
4
4
  import { Buffer32 } from '@aztec/foundation/buffer';
5
5
  import { Fr } from '@aztec/foundation/curves/bn254';
6
6
  import { EthAddress } from '@aztec/foundation/eth-address';
7
7
  import { createLogger } from '@aztec/foundation/log';
8
- import { CheckpointedL2Block, L2Block } from '@aztec/stdlib/block';
8
+ import { Body, GENESIS_BLOCK_HEADER_HASH, GENESIS_CHECKPOINT_HEADER_HASH, L2Block } from '@aztec/stdlib/block';
9
9
  import { Checkpoint, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
10
- import { EmptyL1RollupConstants, getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
11
- import { TxExecutionResult, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
10
+ import { EmptyL1RollupConstants, getEpochAtSlot, getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
11
+ import { computeCheckpointOutHash } from '@aztec/stdlib/messaging';
12
+ import { CheckpointHeader } from '@aztec/stdlib/rollup';
13
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
14
+ import { BlockHeader, TxExecutionResult, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
12
15
  /**
13
16
  * A mocked implementation of L2BlockSource to be used in tests.
14
17
  */ export class MockL2BlockSource {
15
18
  l2Blocks = [];
19
+ checkpointList = [];
16
20
  provenBlockNumber = 0;
17
21
  finalizedBlockNumber = 0;
18
22
  checkpointedBlockNumber = 0;
23
+ proposedCheckpointBlockNumber = 0;
24
+ initialHeader = BlockHeader.empty();
25
+ initialHeaderHash = GENESIS_BLOCK_HEADER_HASH;
26
+ genesisArchiveRoot;
27
+ genesisBlock;
19
28
  log = createLogger('archiver:mock_l2_block_source');
20
- async createBlocks(numBlocks) {
21
- for(let i = 0; i < numBlocks; i++){
22
- const blockNum = this.l2Blocks.length + 1;
23
- const block = await L2Block.random(BlockNumber(blockNum), {
24
- slotNumber: SlotNumber(blockNum)
29
+ /** Returns the initial header used to synthesize block 0. */ getInitialHeader() {
30
+ return this.initialHeader;
31
+ }
32
+ /**
33
+ * Sets the initial header used to synthesize block 0. Tests that wire up a real
34
+ * world-state should call this with `worldState.getInitialHeader()` so the L2BlockStream
35
+ * agrees on the genesis hash on both sides. Precomputes and caches the header hash so
36
+ * `getGenesisBlockHash()` can return synchronously.
37
+ */ async setInitialHeader(header) {
38
+ this.initialHeader = header;
39
+ this.initialHeaderHash = await header.hash();
40
+ this.genesisBlock = undefined;
41
+ }
42
+ /**
43
+ * Returns the precomputed hash of the genesis block header. Defaults to the static
44
+ * {@link GENESIS_BLOCK_HEADER_HASH} unless {@link setInitialHeader} has been called with a
45
+ * custom header.
46
+ */ getGenesisBlockHash() {
47
+ return this.initialHeaderHash;
48
+ }
49
+ /**
50
+ * Sets the post-genesis archive root used to synthesize block 0. Mirrors the real archiver,
51
+ * whose synthetic block 0 carries `new AppendOnlyTreeSnapshot(genesisArchiveRoot, 1)` rather
52
+ * than `AppendOnlyTreeSnapshot.empty()`. Tests wiring up a real world-state should set this so
53
+ * archive-based block lookups against the mock match production semantics.
54
+ */ setGenesisArchiveRoot(root) {
55
+ this.genesisArchiveRoot = root;
56
+ this.genesisBlock = undefined;
57
+ }
58
+ getGenesisBlock() {
59
+ if (this.genesisBlock) {
60
+ return this.genesisBlock;
61
+ }
62
+ const archive = this.genesisArchiveRoot ? new AppendOnlyTreeSnapshot(this.genesisArchiveRoot, 1) : AppendOnlyTreeSnapshot.empty();
63
+ return this.genesisBlock = new L2Block(archive, this.initialHeader, Body.empty(), CheckpointNumber.ZERO, IndexWithinCheckpoint(0));
64
+ }
65
+ /** Creates blocks grouped into single-block checkpoints. */ async createBlocks(numBlocks) {
66
+ await this.createCheckpoints(numBlocks, 1);
67
+ }
68
+ getCheckpointNumber() {
69
+ return Promise.resolve(this.checkpointList.length === 0 ? CheckpointNumber.ZERO : CheckpointNumber(this.checkpointList.length));
70
+ }
71
+ /** Creates checkpoints, each containing `blocksPerCheckpoint` blocks. */ async createCheckpoints(numCheckpoints, blocksPerCheckpoint = 1) {
72
+ for(let c = 0; c < numCheckpoints; c++){
73
+ const checkpointNum = CheckpointNumber(this.checkpointList.length + 1);
74
+ const startBlockNum = this.l2Blocks.length + 1;
75
+ const slotNumber = SlotNumber(Number(checkpointNum));
76
+ const checkpoint = await Checkpoint.random(checkpointNum, {
77
+ numBlocks: blocksPerCheckpoint,
78
+ startBlockNumber: startBlockNum,
79
+ slotNumber,
80
+ checkpointNumber: checkpointNum
25
81
  });
26
- this.l2Blocks.push(block);
82
+ this.checkpointList.push(checkpoint);
83
+ this.l2Blocks.push(...checkpoint.blocks);
27
84
  }
28
- this.log.verbose(`Created ${numBlocks} blocks in the mock L2 block source`);
85
+ this.log.verbose(`Created ${numCheckpoints} checkpoints with ${blocksPerCheckpoint} blocks each in the mock L2 block source`);
29
86
  }
30
87
  addProposedBlocks(blocks) {
31
88
  this.l2Blocks.push(...blocks);
@@ -33,6 +90,17 @@ import { TxExecutionResult, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
33
90
  }
34
91
  removeBlocks(numBlocks) {
35
92
  this.l2Blocks = this.l2Blocks.slice(0, -numBlocks);
93
+ const maxBlockNum = this.l2Blocks.length;
94
+ // Remove any checkpoint whose last block is beyond the remaining blocks.
95
+ this.checkpointList = this.checkpointList.filter((c)=>{
96
+ const lastBlockNum = c.blocks[0].number + c.blocks.length - 1;
97
+ return lastBlockNum <= maxBlockNum;
98
+ });
99
+ // Keep tip numbers consistent with remaining blocks.
100
+ this.checkpointedBlockNumber = Math.min(this.checkpointedBlockNumber, maxBlockNum);
101
+ this.proposedCheckpointBlockNumber = Math.min(this.proposedCheckpointBlockNumber, maxBlockNum);
102
+ this.provenBlockNumber = Math.min(this.provenBlockNumber, maxBlockNum);
103
+ this.finalizedBlockNumber = Math.min(this.finalizedBlockNumber, maxBlockNum);
36
104
  this.log.verbose(`Removed ${numBlocks} blocks from the mock L2 block source`);
37
105
  }
38
106
  setProvenBlockNumber(provenBlockNumber) {
@@ -44,8 +112,40 @@ import { TxExecutionResult, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
44
112
  }
45
113
  this.finalizedBlockNumber = finalizedBlockNumber;
46
114
  }
115
+ setProposedCheckpointBlockNumber(blockNumber) {
116
+ this.proposedCheckpointBlockNumber = blockNumber;
117
+ }
47
118
  setCheckpointedBlockNumber(checkpointedBlockNumber) {
119
+ const prevCheckpointed = this.checkpointedBlockNumber;
48
120
  this.checkpointedBlockNumber = checkpointedBlockNumber;
121
+ // Proposed checkpoint is always at least as advanced as checkpointed
122
+ if (this.proposedCheckpointBlockNumber < checkpointedBlockNumber) {
123
+ this.proposedCheckpointBlockNumber = checkpointedBlockNumber;
124
+ }
125
+ // Auto-create single-block checkpoints for newly checkpointed blocks that don't have one yet.
126
+ // This handles blocks added via addProposedBlocks that are now being marked as checkpointed.
127
+ const newCheckpoints = [];
128
+ for(let blockNum = prevCheckpointed + 1; blockNum <= checkpointedBlockNumber; blockNum++){
129
+ const block = this.l2Blocks[blockNum - 1];
130
+ if (!block) {
131
+ continue;
132
+ }
133
+ if (this.checkpointList.some((c)=>c.blocks.some((b)=>b.number === block.number))) {
134
+ continue;
135
+ }
136
+ const checkpointNum = CheckpointNumber(this.checkpointList.length + newCheckpoints.length + 1);
137
+ const checkpoint = new Checkpoint(block.archive, CheckpointHeader.random({
138
+ slotNumber: block.header.globalVariables.slotNumber
139
+ }), [
140
+ block
141
+ ], checkpointNum);
142
+ newCheckpoints.push(checkpoint);
143
+ }
144
+ // Insert new checkpoints in order by number.
145
+ if (newCheckpoints.length > 0) {
146
+ this.checkpointList.push(...newCheckpoints);
147
+ this.checkpointList.sort((a, b)=>a.number - b.number);
148
+ }
49
149
  }
50
150
  /**
51
151
  * Method to fetch the rollup contract address at the base-layer.
@@ -59,199 +159,93 @@ import { TxExecutionResult, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
59
159
  */ getRegistryAddress() {
60
160
  return Promise.resolve(EthAddress.random());
61
161
  }
62
- /**
63
- * Gets the number of the latest L2 block processed by the block source implementation.
64
- * @returns In this mock instance, returns the number of L2 blocks that we've mocked.
65
- */ getBlockNumber() {
66
- return Promise.resolve(BlockNumber(this.l2Blocks.length));
67
- }
68
- getProvenBlockNumber() {
69
- return Promise.resolve(BlockNumber(this.provenBlockNumber));
70
- }
71
- getCheckpointedL2BlockNumber() {
72
- return Promise.resolve(BlockNumber(this.checkpointedBlockNumber));
73
- }
74
- getFinalizedL2BlockNumber() {
75
- return Promise.resolve(BlockNumber(this.finalizedBlockNumber));
76
- }
77
- getCheckpointedBlock(number) {
78
- if (number > this.checkpointedBlockNumber) {
79
- return Promise.resolve(undefined);
162
+ async getBlockNumber(query) {
163
+ if (!query) {
164
+ return BlockNumber(this.l2Blocks.length);
80
165
  }
81
- const block = this.l2Blocks[number - 1];
82
- if (!block) {
83
- return Promise.resolve(undefined);
166
+ if ('number' in query) {
167
+ return query.number;
84
168
  }
85
- const checkpointedBlock = new CheckpointedL2Block(CheckpointNumber.fromBlockNumber(number), block, new L1PublishedData(BigInt(number), BigInt(number), `0x${number.toString(16).padStart(64, '0')}`), []);
86
- return Promise.resolve(checkpointedBlock);
87
- }
88
- async getCheckpointedBlocks(from, limit) {
89
- const result = [];
90
- for(let i = 0; i < limit; i++){
91
- const blockNum = from + i;
92
- if (blockNum > this.checkpointedBlockNumber) {
93
- break;
94
- }
95
- const block = await this.getCheckpointedBlock(BlockNumber(blockNum));
96
- if (block) {
97
- result.push(block);
98
- }
169
+ if ('tag' in query) {
170
+ return BlockNumber(this.resolveBlockTag(query.tag));
99
171
  }
100
- return result;
172
+ const block = await this.getBlock(query);
173
+ return block ? block.header.globalVariables.blockNumber : undefined;
101
174
  }
102
- /**
103
- * Gets an l2 block.
104
- * @param number - The block number to return (inclusive).
105
- * @returns The requested L2 block.
106
- */ getBlock(number) {
107
- const block = this.l2Blocks[number - 1];
108
- return Promise.resolve(block);
175
+ getProposedCheckpointL2BlockNumber() {
176
+ return Promise.resolve(BlockNumber(this.proposedCheckpointBlockNumber));
109
177
  }
110
- /**
111
- * Gets an L2 block (new format).
112
- * @param number - The block number to return.
113
- * @returns The requested L2 block.
114
- */ getL2Block(number) {
115
- const block = this.l2Blocks[number - 1];
116
- return Promise.resolve(block);
117
- }
118
- /**
119
- * Gets up to `limit` amount of L2 blocks starting from `from`.
120
- * @param from - Number of the first block to return (inclusive).
121
- * @param limit - The maximum number of blocks to return.
122
- * @returns The requested mocked L2 blocks.
123
- */ getBlocks(from, limit) {
124
- return Promise.resolve(this.l2Blocks.slice(from - 1, from - 1 + limit));
125
- }
126
- getCheckpoints(from, limit) {
127
- // TODO(mbps): Implement this properly. This only works when we have one block per checkpoint.
128
- const blocks = this.l2Blocks.slice(from - 1, from - 1 + limit);
129
- return Promise.all(blocks.map(async (block)=>{
130
- // Create a checkpoint from the block - manually construct since L2Block doesn't have toCheckpoint()
131
- const checkpoint = await Checkpoint.random(block.checkpointNumber, {
132
- numBlocks: 1
133
- });
134
- checkpoint.blocks = [
135
- block
136
- ];
137
- return new PublishedCheckpoint(checkpoint, new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()), []);
138
- }));
139
- }
140
- async getCheckpointByArchive(archive) {
141
- // TODO(mbps): Implement this properly. This only works when we have one block per checkpoint.
142
- const block = this.l2Blocks.find((b)=>b.archive.root.equals(archive));
143
- if (!block) {
144
- return undefined;
145
- }
146
- // Create a checkpoint from the block - manually construct since L2Block doesn't have toCheckpoint()
147
- const checkpoint = await Checkpoint.random(block.checkpointNumber, {
148
- numBlocks: 1
149
- });
150
- checkpoint.blocks = [
151
- block
152
- ];
153
- return checkpoint;
154
- }
155
- async getCheckpointedBlockByHash(blockHash) {
156
- for (const block of this.l2Blocks){
157
- const hash = await block.hash();
158
- if (hash.equals(blockHash)) {
159
- return CheckpointedL2Block.fromFields({
160
- checkpointNumber: CheckpointNumber.fromBlockNumber(block.number),
161
- block,
162
- l1: new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
163
- attestations: []
164
- });
165
- }
166
- }
167
- return undefined;
168
- }
169
- getCheckpointedBlockByArchive(archive) {
170
- const block = this.l2Blocks.find((b)=>b.archive.root.equals(archive));
171
- if (!block) {
178
+ getCheckpoint(query) {
179
+ const checkpoint = this.resolveCheckpointQuery(query);
180
+ if (!checkpoint) {
172
181
  return Promise.resolve(undefined);
173
182
  }
174
- return Promise.resolve(CheckpointedL2Block.fromFields({
175
- checkpointNumber: CheckpointNumber.fromBlockNumber(block.number),
176
- block,
177
- l1: new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
178
- attestations: []
179
- }));
180
- }
181
- async getL2BlockByHash(blockHash) {
182
- for (const block of this.l2Blocks){
183
- const hash = await block.hash();
184
- if (hash.equals(blockHash)) {
185
- return block;
186
- }
187
- }
188
- return undefined;
183
+ return Promise.resolve(new PublishedCheckpoint(checkpoint, this.mockL1DataForCheckpoint(checkpoint), []));
189
184
  }
190
- getL2BlockByArchive(archive) {
191
- const block = this.l2Blocks.find((b)=>b.archive.root.equals(archive));
192
- return Promise.resolve(block);
185
+ getCheckpoints(query) {
186
+ const checkpoints = this.resolveCheckpointsQuery(query);
187
+ return Promise.resolve(checkpoints.map((checkpoint)=>new PublishedCheckpoint(checkpoint, this.mockL1DataForCheckpoint(checkpoint), [])));
193
188
  }
194
- async getBlockHeaderByHash(blockHash) {
195
- for (const block of this.l2Blocks){
196
- const hash = await block.hash();
197
- if (hash.equals(blockHash)) {
198
- return block.header;
199
- }
189
+ getCheckpointByArchive(archive) {
190
+ const checkpoint = this.checkpointList.find((c)=>c.archive.root.equals(archive));
191
+ return Promise.resolve(checkpoint);
192
+ }
193
+ getCheckpointData(query) {
194
+ const checkpoint = this.resolveCheckpointQuery(query);
195
+ if (!checkpoint) {
196
+ return Promise.resolve(undefined);
200
197
  }
201
- return undefined;
198
+ return Promise.resolve(this.checkpointToData(checkpoint));
202
199
  }
203
- getBlockHeaderByArchive(archive) {
204
- const block = this.l2Blocks.find((b)=>b.archive.root.equals(archive));
205
- return Promise.resolve(block?.header);
200
+ getCheckpointsData(query) {
201
+ const checkpoints = this.resolveCheckpointsQuery(query);
202
+ return Promise.resolve(checkpoints.map((c)=>this.checkpointToData(c)));
206
203
  }
207
- getBlockHeader(number) {
208
- return Promise.resolve(this.l2Blocks.at(typeof number === 'number' ? number - 1 : -1)?.header);
204
+ checkpointToData(checkpoint) {
205
+ return {
206
+ checkpointNumber: checkpoint.number,
207
+ header: checkpoint.header,
208
+ archive: checkpoint.archive,
209
+ checkpointOutHash: computeCheckpointOutHash(checkpoint.blocks.map((b)=>b.body.txEffects.map((tx)=>tx.l2ToL1Msgs))),
210
+ startBlock: checkpoint.blocks[0].number,
211
+ blockCount: checkpoint.blocks.length,
212
+ feeAssetPriceModifier: checkpoint.feeAssetPriceModifier,
213
+ attestations: [],
214
+ l1: this.mockL1DataForCheckpoint(checkpoint)
215
+ };
209
216
  }
210
- getCheckpointsForEpoch(epochNumber) {
211
- // TODO(mbps): Implement this properly. This only works when we have one block per checkpoint.
212
- const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
213
- const [start, end] = getSlotRangeForEpoch(epochNumber, {
214
- epochDuration
215
- });
216
- const blocks = this.l2Blocks.filter((b)=>{
217
- const slot = b.header.globalVariables.slotNumber;
218
- return slot >= start && slot <= end;
219
- });
220
- // Create checkpoints from blocks - manually construct since L2Block doesn't have toCheckpoint()
221
- return Promise.all(blocks.map(async (block)=>{
222
- const checkpoint = await Checkpoint.random(block.checkpointNumber, {
223
- numBlocks: 1
224
- });
225
- checkpoint.blocks = [
226
- block
227
- ];
228
- return checkpoint;
229
- }));
217
+ resolveCheckpointQuery(query) {
218
+ if ('number' in query) {
219
+ return this.checkpointList[query.number - 1];
220
+ }
221
+ if ('slot' in query) {
222
+ return this.checkpointList.find((c)=>c.header.slotNumber === query.slot);
223
+ }
224
+ switch(query.tag){
225
+ case 'checkpointed':
226
+ return this.checkpointList[this.checkpointList.length - 1];
227
+ case 'proven':
228
+ {
229
+ const provenCheckpoint = this.checkpointList.filter((c)=>c.blocks.some((b)=>b.number <= this.provenBlockNumber));
230
+ return provenCheckpoint.at(-1);
231
+ }
232
+ case 'finalized':
233
+ {
234
+ const finalizedCheckpoint = this.checkpointList.filter((c)=>c.blocks.some((b)=>b.number <= this.finalizedBlockNumber));
235
+ return finalizedCheckpoint.at(-1);
236
+ }
237
+ }
230
238
  }
231
- getCheckpointedBlocksForEpoch(epochNumber) {
232
- const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
233
- const [start, end] = getSlotRangeForEpoch(epochNumber, {
234
- epochDuration
235
- });
236
- const blocks = this.l2Blocks.filter((b)=>{
237
- const slot = b.header.globalVariables.slotNumber;
238
- return slot >= start && slot <= end;
239
- });
240
- return Promise.resolve(blocks.map((block)=>CheckpointedL2Block.fromFields({
241
- checkpointNumber: CheckpointNumber.fromBlockNumber(block.number),
242
- block,
243
- l1: new L1PublishedData(BigInt(block.number), BigInt(block.number), Buffer32.random().toString()),
244
- attestations: []
245
- })));
239
+ resolveCheckpointsQuery(query) {
240
+ if ('from' in query) {
241
+ return this.checkpointList.slice(query.from - 1, query.from - 1 + query.limit);
242
+ }
243
+ return this.getCheckpointsInEpoch(query.epoch);
246
244
  }
247
245
  getBlocksForSlot(slotNumber) {
248
246
  const blocks = this.l2Blocks.filter((b)=>b.header.globalVariables.slotNumber === slotNumber);
249
247
  return Promise.resolve(blocks);
250
248
  }
251
- async getCheckpointedBlockHeadersForEpoch(epochNumber) {
252
- const checkpointedBlocks = await this.getCheckpointedBlocksForEpoch(epochNumber);
253
- return checkpointedBlocks.map((b)=>b.block.header);
254
- }
255
249
  /**
256
250
  * Gets a tx effect.
257
251
  * @param txHash - The hash of the tx corresponding to the tx effect.
@@ -269,7 +263,7 @@ import { TxExecutionResult, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
269
263
  data: txEffect,
270
264
  l2BlockNumber: block.number,
271
265
  l2BlockHash: await block.hash(),
272
- txIndexInBlock: block.body.txEffects.indexOf(txEffect)
266
+ txIndexInBlock: block.body.txEffects.findIndex((t)=>t.txHash.equals(txHash))
273
267
  };
274
268
  }
275
269
  /**
@@ -281,57 +275,80 @@ import { TxExecutionResult, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
281
275
  for (const txEffect of block.body.txEffects){
282
276
  if (txEffect.txHash.equals(txHash)) {
283
277
  // In mock, assume all txs are checkpointed with successful execution
284
- return new TxReceipt(txHash, TxStatus.CHECKPOINTED, TxExecutionResult.SUCCESS, undefined, txEffect.transactionFee.toBigInt(), await block.hash(), block.number);
278
+ return new TxReceipt(txHash, TxStatus.CHECKPOINTED, TxExecutionResult.SUCCESS, undefined, txEffect.transactionFee.toBigInt(), await block.hash(), block.number, getEpochAtSlot(block.slot, EmptyL1RollupConstants));
285
279
  }
286
280
  }
287
281
  }
288
282
  return undefined;
289
283
  }
290
284
  async getL2Tips() {
291
- const [latest, proven, finalized, checkpointed] = [
285
+ const [latest, proven, finalized, checkpointed, proposedCheckpoint] = [
292
286
  await this.getBlockNumber(),
293
- await this.getProvenBlockNumber(),
287
+ this.provenBlockNumber,
294
288
  this.finalizedBlockNumber,
295
- this.checkpointedBlockNumber
289
+ this.checkpointedBlockNumber,
290
+ await this.getProposedCheckpointL2BlockNumber()
296
291
  ];
297
292
  const latestBlock = this.l2Blocks[latest - 1];
298
293
  const provenBlock = this.l2Blocks[proven - 1];
299
294
  const finalizedBlock = this.l2Blocks[finalized - 1];
300
295
  const checkpointedBlock = this.l2Blocks[checkpointed - 1];
296
+ const proposedCheckpointBlock = this.l2Blocks[proposedCheckpoint - 1];
297
+ // For genesis tips (block number 0) report the dynamic initial header hash so consumers
298
+ // running L2BlockStream against this mock agree at block 0 with their local tip store.
299
+ const genesisHash = (await this.initialHeader.hash()).toString();
300
+ const tipHash = async (block, number)=>{
301
+ if (block) {
302
+ return (await block.hash()).toString();
303
+ }
304
+ return number === 0 ? genesisHash : '';
305
+ };
301
306
  const latestBlockId = {
302
307
  number: BlockNumber(latest),
303
- hash: (await latestBlock?.hash())?.toString()
308
+ hash: await tipHash(latestBlock, latest)
304
309
  };
305
310
  const provenBlockId = {
306
311
  number: BlockNumber(proven),
307
- hash: (await provenBlock?.hash())?.toString()
312
+ hash: await tipHash(provenBlock, proven)
308
313
  };
309
314
  const finalizedBlockId = {
310
315
  number: BlockNumber(finalized),
311
- hash: (await finalizedBlock?.hash())?.toString()
316
+ hash: await tipHash(finalizedBlock, finalized)
312
317
  };
313
318
  const checkpointedBlockId = {
314
319
  number: BlockNumber(checkpointed),
315
- hash: (await checkpointedBlock?.hash())?.toString()
320
+ hash: await tipHash(checkpointedBlock, checkpointed)
321
+ };
322
+ const proposedCheckpointBlockId = {
323
+ number: BlockNumber(proposedCheckpoint),
324
+ hash: await tipHash(proposedCheckpointBlock, proposedCheckpoint)
316
325
  };
317
- const makeTipId = (blockId)=>({
326
+ const makeTipId = (blockId)=>{
327
+ const checkpointNumber = this.findCheckpointNumberForBlock(blockId.number) ?? CheckpointNumber(0);
328
+ // Match production semantics: checkpoint 0 is fully synthetic (no real checkpoint header
329
+ // exists at 0), so its hash stays at the protocol constant `GENESIS_CHECKPOINT_HEADER_HASH`
330
+ // even though the block-0 hash is dynamic. See L2TipsCache for the production path.
331
+ const hash = checkpointNumber === 0 ? GENESIS_CHECKPOINT_HEADER_HASH.toString() : blockId.hash;
332
+ return {
318
333
  block: blockId,
319
334
  checkpoint: {
320
- number: CheckpointNumber.fromBlockNumber(blockId.number),
321
- hash: blockId.hash
335
+ number: checkpointNumber,
336
+ hash
322
337
  }
323
- });
338
+ };
339
+ };
324
340
  return {
325
341
  proposed: latestBlockId,
326
342
  checkpointed: makeTipId(checkpointedBlockId),
327
343
  proven: makeTipId(provenBlockId),
328
- finalized: makeTipId(finalizedBlockId)
344
+ finalized: makeTipId(finalizedBlockId),
345
+ proposedCheckpoint: makeTipId(proposedCheckpointBlockId)
329
346
  };
330
347
  }
331
- getL2EpochNumber() {
348
+ getSyncedL2EpochNumber() {
332
349
  throw new Error('Method not implemented.');
333
350
  }
334
- getL2SlotNumber() {
351
+ getSyncedL2SlotNumber() {
335
352
  throw new Error('Method not implemented.');
336
353
  }
337
354
  isEpochComplete(_epochNumber) {
@@ -340,9 +357,12 @@ import { TxExecutionResult, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
340
357
  getL1Constants() {
341
358
  return Promise.resolve(EmptyL1RollupConstants);
342
359
  }
360
+ isPruneDueAtSlot(_slot) {
361
+ return Promise.resolve(false);
362
+ }
343
363
  getGenesisValues() {
344
364
  return Promise.resolve({
345
- genesisArchiveRoot: new Fr(GENESIS_ARCHIVE_ROOT)
365
+ genesisArchiveRoot: this.genesisArchiveRoot ?? new Fr(GENESIS_ARCHIVE_ROOT)
346
366
  });
347
367
  }
348
368
  getL1Timestamp() {
@@ -383,6 +403,88 @@ import { TxExecutionResult, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
383
403
  syncImmediate() {
384
404
  return Promise.resolve();
385
405
  }
406
+ async getBlock(query) {
407
+ if ('number' in query) {
408
+ if (query.number === 0) {
409
+ return this.getGenesisBlock();
410
+ }
411
+ return this.l2Blocks[query.number - 1];
412
+ }
413
+ if ('hash' in query) {
414
+ const genesis = this.getGenesisBlock();
415
+ if ((await genesis.hash()).equals(query.hash)) {
416
+ return genesis;
417
+ }
418
+ for (const b of this.l2Blocks){
419
+ const hash = await b.hash();
420
+ if (hash.equals(query.hash)) {
421
+ return b;
422
+ }
423
+ }
424
+ return undefined;
425
+ }
426
+ if ('archive' in query) {
427
+ const genesis = this.getGenesisBlock();
428
+ if (genesis.archive.root.equals(query.archive)) {
429
+ return genesis;
430
+ }
431
+ return this.l2Blocks.find((b)=>b.archive.root.equals(query.archive));
432
+ }
433
+ const number = this.resolveBlockTag(query.tag);
434
+ if (number === 0) {
435
+ return this.getGenesisBlock();
436
+ }
437
+ return this.l2Blocks[number - 1];
438
+ }
439
+ resolveBlockTag(tag) {
440
+ switch(tag){
441
+ case 'latest':
442
+ case 'proposed':
443
+ return this.l2Blocks.length;
444
+ case 'checkpointed':
445
+ return this.checkpointedBlockNumber;
446
+ case 'proven':
447
+ return this.provenBlockNumber;
448
+ case 'finalized':
449
+ return this.finalizedBlockNumber;
450
+ }
451
+ }
452
+ getBlocks(query) {
453
+ let blocks;
454
+ if ('from' in query) {
455
+ blocks = this.l2Blocks.slice(query.from - 1, query.from - 1 + query.limit);
456
+ } else {
457
+ const epochCheckpoints = this.getCheckpointsInEpoch(query.epoch);
458
+ blocks = epochCheckpoints.flatMap((c)=>c.blocks);
459
+ }
460
+ if (query.onlyCheckpointed) {
461
+ blocks = blocks.filter((b)=>b.header.globalVariables.blockNumber <= this.checkpointedBlockNumber);
462
+ }
463
+ return Promise.resolve(blocks);
464
+ }
465
+ async getBlockData(query) {
466
+ const block = await this.getBlock(query);
467
+ if (!block) {
468
+ return undefined;
469
+ }
470
+ return {
471
+ header: block.header,
472
+ archive: block.archive,
473
+ blockHash: await block.hash(),
474
+ checkpointNumber: block.checkpointNumber,
475
+ indexWithinCheckpoint: block.indexWithinCheckpoint
476
+ };
477
+ }
478
+ async getBlocksData(query) {
479
+ const blocks = await this.getBlocks(query);
480
+ return Promise.all(blocks.map(async (block)=>({
481
+ header: block.header,
482
+ archive: block.archive,
483
+ blockHash: await block.hash(),
484
+ checkpointNumber: block.checkpointNumber,
485
+ indexWithinCheckpoint: block.indexWithinCheckpoint
486
+ })));
487
+ }
386
488
  isPendingChainInvalid() {
387
489
  return Promise.resolve(false);
388
490
  }
@@ -391,4 +493,21 @@ import { TxExecutionResult, TxReceipt, TxStatus } from '@aztec/stdlib/tx';
391
493
  valid: true
392
494
  });
393
495
  }
496
+ getProposedCheckpointData(_query) {
497
+ return Promise.resolve(undefined);
498
+ }
499
+ /** Returns checkpoints whose slot falls within the given epoch. */ getCheckpointsInEpoch(epochNumber) {
500
+ const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
501
+ const [start, end] = getSlotRangeForEpoch(epochNumber, {
502
+ epochDuration
503
+ });
504
+ return this.checkpointList.filter((c)=>c.header.slotNumber >= start && c.header.slotNumber <= end);
505
+ }
506
+ /** Creates a mock L1PublishedData for a checkpoint. */ mockL1DataForCheckpoint(checkpoint) {
507
+ return new L1PublishedData(BigInt(checkpoint.number), BigInt(checkpoint.number), Buffer32.random().toString());
508
+ }
509
+ /** Finds the checkpoint number for a block, or undefined if the block is not in any checkpoint. */ findCheckpointNumberForBlock(blockNumber) {
510
+ const checkpoint = this.checkpointList.find((c)=>c.blocks.some((b)=>b.number === blockNumber));
511
+ return checkpoint?.number;
512
+ }
394
513
  }