@aztec/archiver 0.0.1-commit.b655e406 → 0.0.1-commit.b9865e97

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 (241) hide show
  1. package/README.md +164 -22
  2. package/dest/archiver.d.ts +176 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +905 -0
  5. package/dest/config.d.ts +33 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/config.js +78 -0
  8. package/dest/errors.d.ts +87 -0
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/errors.js +129 -0
  11. package/dest/factory.d.ts +17 -10
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +117 -22
  14. package/dest/index.d.ts +19 -4
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +17 -3
  17. package/dest/interfaces.d.ts +9 -0
  18. package/dest/interfaces.d.ts.map +1 -0
  19. package/dest/interfaces.js +3 -0
  20. package/dest/l1/bin/retrieve-calldata.d.ts +3 -0
  21. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  22. package/dest/l1/bin/retrieve-calldata.js +152 -0
  23. package/dest/l1/calldata_retriever.d.ts +136 -0
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/l1/calldata_retriever.js +412 -0
  26. package/dest/l1/data_retrieval.d.ts +97 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/l1/data_retrieval.js +307 -0
  29. package/dest/l1/debug_tx.d.ts +19 -0
  30. package/dest/l1/debug_tx.d.ts.map +1 -0
  31. package/dest/l1/debug_tx.js +73 -0
  32. package/dest/l1/spire_proposer.d.ts +70 -0
  33. package/dest/l1/spire_proposer.d.ts.map +1 -0
  34. package/dest/l1/spire_proposer.js +149 -0
  35. package/dest/l1/trace_tx.d.ts +43 -0
  36. package/dest/l1/trace_tx.d.ts.map +1 -0
  37. package/dest/l1/trace_tx.js +91 -0
  38. package/dest/l1/types.d.ts +12 -0
  39. package/dest/l1/types.d.ts.map +1 -0
  40. package/dest/l1/types.js +3 -0
  41. package/dest/l1/validate_historical_logs.d.ts +23 -0
  42. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  43. package/dest/l1/validate_historical_logs.js +108 -0
  44. package/dest/l1/validate_trace.d.ts +32 -0
  45. package/dest/l1/validate_trace.d.ts.map +1 -0
  46. package/dest/l1/validate_trace.js +154 -0
  47. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  48. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  49. package/dest/modules/contract_data_source_adapter.js +40 -0
  50. package/dest/modules/data_source_base.d.ts +113 -0
  51. package/dest/modules/data_source_base.d.ts.map +1 -0
  52. package/dest/modules/data_source_base.js +348 -0
  53. package/dest/modules/data_store_updater.d.ts +105 -0
  54. package/dest/modules/data_store_updater.d.ts.map +1 -0
  55. package/dest/modules/data_store_updater.js +392 -0
  56. package/dest/modules/instrumentation.d.ts +55 -0
  57. package/dest/modules/instrumentation.d.ts.map +1 -0
  58. package/dest/modules/instrumentation.js +145 -0
  59. package/dest/modules/l1_synchronizer.d.ts +77 -0
  60. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  61. package/dest/modules/l1_synchronizer.js +1344 -0
  62. package/dest/modules/outbox_trees_resolver.d.ts +62 -0
  63. package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
  64. package/dest/modules/outbox_trees_resolver.js +162 -0
  65. package/dest/modules/validation.d.ts +18 -0
  66. package/dest/modules/validation.d.ts.map +1 -0
  67. package/dest/modules/validation.js +104 -0
  68. package/dest/store/block_store.d.ts +294 -0
  69. package/dest/store/block_store.d.ts.map +1 -0
  70. package/dest/store/block_store.js +1188 -0
  71. package/dest/store/contract_class_store.d.ts +31 -0
  72. package/dest/store/contract_class_store.d.ts.map +1 -0
  73. package/dest/store/contract_class_store.js +80 -0
  74. package/dest/store/contract_instance_store.d.ts +51 -0
  75. package/dest/store/contract_instance_store.d.ts.map +1 -0
  76. package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +38 -3
  77. package/dest/store/data_stores.d.ts +68 -0
  78. package/dest/store/data_stores.d.ts.map +1 -0
  79. package/dest/store/data_stores.js +54 -0
  80. package/dest/store/function_names_cache.d.ts +17 -0
  81. package/dest/store/function_names_cache.d.ts.map +1 -0
  82. package/dest/store/function_names_cache.js +30 -0
  83. package/dest/store/l2_tips_cache.d.ts +25 -0
  84. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  85. package/dest/store/l2_tips_cache.js +26 -0
  86. package/dest/store/log_store.d.ts +59 -0
  87. package/dest/store/log_store.d.ts.map +1 -0
  88. package/dest/store/log_store.js +310 -0
  89. package/dest/store/log_store_codec.d.ts +70 -0
  90. package/dest/store/log_store_codec.d.ts.map +1 -0
  91. package/dest/store/log_store_codec.js +101 -0
  92. package/dest/store/message_store.d.ts +50 -0
  93. package/dest/store/message_store.d.ts.map +1 -0
  94. package/dest/{archiver/kv_archiver_store → store}/message_store.js +66 -23
  95. package/dest/structs/data_retrieval.d.ts +27 -0
  96. package/dest/structs/data_retrieval.d.ts.map +1 -0
  97. package/dest/structs/inbox_message.d.ts +15 -0
  98. package/dest/structs/inbox_message.d.ts.map +1 -0
  99. package/dest/{archiver/structs → structs}/inbox_message.js +6 -5
  100. package/dest/structs/published.d.ts +2 -0
  101. package/dest/structs/published.d.ts.map +1 -0
  102. package/dest/test/fake_l1_state.d.ts +214 -0
  103. package/dest/test/fake_l1_state.d.ts.map +1 -0
  104. package/dest/test/fake_l1_state.js +517 -0
  105. package/dest/test/index.d.ts +2 -1
  106. package/dest/test/index.d.ts.map +1 -1
  107. package/dest/test/index.js +4 -1
  108. package/dest/test/mock_archiver.d.ts +16 -8
  109. package/dest/test/mock_archiver.d.ts.map +1 -1
  110. package/dest/test/mock_archiver.js +19 -14
  111. package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
  112. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  113. package/dest/test/mock_l1_to_l2_message_source.js +23 -12
  114. package/dest/test/mock_l2_block_source.d.ts +76 -40
  115. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  116. package/dest/test/mock_l2_block_source.js +362 -133
  117. package/dest/test/mock_structs.d.ts +83 -4
  118. package/dest/test/mock_structs.d.ts.map +1 -1
  119. package/dest/test/mock_structs.js +157 -11
  120. package/dest/test/noop_l1_archiver.d.ts +30 -0
  121. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  122. package/dest/test/noop_l1_archiver.js +85 -0
  123. package/package.json +20 -20
  124. package/src/archiver.ts +719 -0
  125. package/src/config.ts +108 -0
  126. package/src/errors.ts +203 -0
  127. package/src/factory.ts +179 -23
  128. package/src/index.ts +27 -3
  129. package/src/interfaces.ts +9 -0
  130. package/src/l1/README.md +55 -0
  131. package/src/l1/bin/retrieve-calldata.ts +194 -0
  132. package/src/l1/calldata_retriever.ts +522 -0
  133. package/src/l1/data_retrieval.ts +484 -0
  134. package/src/l1/debug_tx.ts +99 -0
  135. package/src/l1/spire_proposer.ts +152 -0
  136. package/src/l1/trace_tx.ts +128 -0
  137. package/src/l1/types.ts +13 -0
  138. package/src/l1/validate_historical_logs.ts +140 -0
  139. package/src/l1/validate_trace.ts +229 -0
  140. package/src/modules/contract_data_source_adapter.ts +55 -0
  141. package/src/modules/data_source_base.ts +495 -0
  142. package/src/modules/data_store_updater.ts +518 -0
  143. package/src/modules/instrumentation.ts +204 -0
  144. package/src/modules/l1_synchronizer.ts +1257 -0
  145. package/src/modules/outbox_trees_resolver.ts +199 -0
  146. package/src/modules/validation.ts +130 -0
  147. package/src/store/block_store.ts +1540 -0
  148. package/src/store/contract_class_store.ts +108 -0
  149. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +53 -7
  150. package/src/store/data_stores.ts +104 -0
  151. package/src/store/function_names_cache.ts +37 -0
  152. package/src/store/l2_tips_cache.ts +35 -0
  153. package/src/store/log_store.ts +379 -0
  154. package/src/store/log_store_codec.ts +132 -0
  155. package/src/{archiver/kv_archiver_store → store}/message_store.ts +81 -28
  156. package/src/{archiver/structs → structs}/inbox_message.ts +7 -7
  157. package/src/structs/published.ts +1 -0
  158. package/src/test/fake_l1_state.ts +770 -0
  159. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  160. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  161. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  162. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  163. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  164. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  165. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  166. package/src/test/index.ts +4 -0
  167. package/src/test/mock_archiver.ts +23 -16
  168. package/src/test/mock_l1_to_l2_message_source.ts +19 -11
  169. package/src/test/mock_l2_block_source.ts +431 -145
  170. package/src/test/mock_structs.ts +289 -13
  171. package/src/test/noop_l1_archiver.ts +148 -0
  172. package/dest/archiver/archiver.d.ts +0 -277
  173. package/dest/archiver/archiver.d.ts.map +0 -1
  174. package/dest/archiver/archiver.js +0 -1322
  175. package/dest/archiver/archiver_store.d.ts +0 -255
  176. package/dest/archiver/archiver_store.d.ts.map +0 -1
  177. package/dest/archiver/archiver_store.js +0 -4
  178. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  179. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  180. package/dest/archiver/archiver_store_test_suite.js +0 -1288
  181. package/dest/archiver/config.d.ts +0 -21
  182. package/dest/archiver/config.d.ts.map +0 -1
  183. package/dest/archiver/config.js +0 -50
  184. package/dest/archiver/data_retrieval.d.ts +0 -79
  185. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  186. package/dest/archiver/data_retrieval.js +0 -362
  187. package/dest/archiver/errors.d.ts +0 -12
  188. package/dest/archiver/errors.d.ts.map +0 -1
  189. package/dest/archiver/errors.js +0 -17
  190. package/dest/archiver/index.d.ts +0 -7
  191. package/dest/archiver/index.d.ts.map +0 -1
  192. package/dest/archiver/index.js +0 -4
  193. package/dest/archiver/instrumentation.d.ts +0 -35
  194. package/dest/archiver/instrumentation.d.ts.map +0 -1
  195. package/dest/archiver/instrumentation.js +0 -140
  196. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -124
  197. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  198. package/dest/archiver/kv_archiver_store/block_store.js +0 -370
  199. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  200. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  201. package/dest/archiver/kv_archiver_store/contract_class_store.js +0 -120
  202. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  203. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  204. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -168
  205. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  206. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -296
  207. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
  208. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  209. package/dest/archiver/kv_archiver_store/log_store.js +0 -336
  210. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -39
  211. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  212. package/dest/archiver/structs/data_retrieval.d.ts +0 -27
  213. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  214. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  215. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  216. package/dest/archiver/structs/published.d.ts +0 -2
  217. package/dest/archiver/structs/published.d.ts.map +0 -1
  218. package/dest/archiver/validation.d.ts +0 -11
  219. package/dest/archiver/validation.d.ts.map +0 -1
  220. package/dest/archiver/validation.js +0 -90
  221. package/dest/rpc/index.d.ts +0 -9
  222. package/dest/rpc/index.d.ts.map +0 -1
  223. package/dest/rpc/index.js +0 -15
  224. package/src/archiver/archiver.ts +0 -1722
  225. package/src/archiver/archiver_store.ts +0 -305
  226. package/src/archiver/archiver_store_test_suite.ts +0 -1263
  227. package/src/archiver/config.ts +0 -74
  228. package/src/archiver/data_retrieval.ts +0 -545
  229. package/src/archiver/errors.ts +0 -26
  230. package/src/archiver/index.ts +0 -6
  231. package/src/archiver/instrumentation.ts +0 -187
  232. package/src/archiver/kv_archiver_store/block_store.ts +0 -481
  233. package/src/archiver/kv_archiver_store/contract_class_store.ts +0 -176
  234. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -422
  235. package/src/archiver/kv_archiver_store/log_store.ts +0 -406
  236. package/src/archiver/structs/published.ts +0 -1
  237. package/src/archiver/validation.ts +0 -99
  238. package/src/rpc/index.ts +0 -16
  239. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  240. /package/dest/{archiver/structs → structs}/published.js +0 -0
  241. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
@@ -1,33 +1,106 @@
1
1
  import { GENESIS_ARCHIVE_ROOT } from '@aztec/constants';
2
- import { DefaultL1ContractsConfig } from '@aztec/ethereum';
2
+ import { DefaultL1ContractsConfig } from '@aztec/ethereum/config';
3
+ import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
3
4
  import { Buffer32 } from '@aztec/foundation/buffer';
5
+ import { Fr } from '@aztec/foundation/curves/bn254';
4
6
  import { EthAddress } from '@aztec/foundation/eth-address';
5
- import { Fr } from '@aztec/foundation/fields';
6
7
  import { createLogger } from '@aztec/foundation/log';
7
- import { L2Block, L2BlockHash, PublishedL2Block } from '@aztec/stdlib/block';
8
+ import { Body, GENESIS_BLOCK_HEADER_HASH, GENESIS_CHECKPOINT_HEADER_HASH, L2Block } from '@aztec/stdlib/block';
9
+ import { Checkpoint, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
8
10
  import { EmptyL1RollupConstants, getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
9
- import { TxReceipt, TxStatus } from '@aztec/stdlib/tx';
11
+ import { computeCheckpointOutHash } from '@aztec/stdlib/messaging';
12
+ import { CheckpointHeader } from '@aztec/stdlib/rollup';
13
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
14
+ import { BlockHeader } from '@aztec/stdlib/tx';
10
15
  /**
11
16
  * A mocked implementation of L2BlockSource to be used in tests.
12
17
  */ export class MockL2BlockSource {
13
18
  l2Blocks = [];
19
+ checkpointList = [];
14
20
  provenBlockNumber = 0;
15
21
  finalizedBlockNumber = 0;
22
+ checkpointedBlockNumber = 0;
23
+ proposedCheckpointBlockNumber = 0;
24
+ initialHeader = BlockHeader.empty();
25
+ initialHeaderHash = GENESIS_BLOCK_HEADER_HASH;
26
+ genesisArchiveRoot;
27
+ genesisBlock;
16
28
  log = createLogger('archiver:mock_l2_block_source');
17
- async createBlocks(numBlocks) {
18
- for(let i = 0; i < numBlocks; i++){
19
- const blockNum = this.l2Blocks.length + 1;
20
- const block = await L2Block.random(blockNum);
21
- this.l2Blocks.push(block);
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;
22
61
  }
23
- this.log.verbose(`Created ${numBlocks} blocks in the mock L2 block source`);
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
81
+ });
82
+ this.checkpointList.push(checkpoint);
83
+ this.l2Blocks.push(...checkpoint.blocks);
84
+ }
85
+ this.log.verbose(`Created ${numCheckpoints} checkpoints with ${blocksPerCheckpoint} blocks each in the mock L2 block source`);
24
86
  }
25
- addBlocks(blocks) {
87
+ addProposedBlocks(blocks) {
26
88
  this.l2Blocks.push(...blocks);
27
- this.log.verbose(`Added ${blocks.length} blocks to the mock L2 block source`);
89
+ this.log.verbose(`Added ${blocks.length} proposed blocks to the mock L2 block source`);
28
90
  }
29
91
  removeBlocks(numBlocks) {
30
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);
31
104
  this.log.verbose(`Removed ${numBlocks} blocks from the mock L2 block source`);
32
105
  }
33
106
  setProvenBlockNumber(provenBlockNumber) {
@@ -39,6 +112,41 @@ import { TxReceipt, TxStatus } from '@aztec/stdlib/tx';
39
112
  }
40
113
  this.finalizedBlockNumber = finalizedBlockNumber;
41
114
  }
115
+ setProposedCheckpointBlockNumber(blockNumber) {
116
+ this.proposedCheckpointBlockNumber = blockNumber;
117
+ }
118
+ setCheckpointedBlockNumber(checkpointedBlockNumber) {
119
+ const prevCheckpointed = this.checkpointedBlockNumber;
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
+ }
149
+ }
42
150
  /**
43
151
  * Method to fetch the rollup contract address at the base-layer.
44
152
  * @returns The rollup address.
@@ -51,104 +159,92 @@ import { TxReceipt, TxStatus } from '@aztec/stdlib/tx';
51
159
  */ getRegistryAddress() {
52
160
  return Promise.resolve(EthAddress.random());
53
161
  }
54
- /**
55
- * Gets the number of the latest L2 block processed by the block source implementation.
56
- * @returns In this mock instance, returns the number of L2 blocks that we've mocked.
57
- */ getBlockNumber() {
58
- return Promise.resolve(this.l2Blocks.length);
59
- }
60
- getProvenBlockNumber() {
61
- return Promise.resolve(this.provenBlockNumber);
162
+ async getBlockNumber(query) {
163
+ if (!query) {
164
+ return BlockNumber(this.l2Blocks.length);
165
+ }
166
+ if ('number' in query) {
167
+ return query.number;
168
+ }
169
+ if ('tag' in query) {
170
+ return BlockNumber(this.resolveBlockTag(query.tag));
171
+ }
172
+ const block = await this.getBlock(query);
173
+ return block ? block.header.globalVariables.blockNumber : undefined;
62
174
  }
63
- /**
64
- * Gets an l2 block.
65
- * @param number - The block number to return (inclusive).
66
- * @returns The requested L2 block.
67
- */ getBlock(number) {
68
- return Promise.resolve(this.l2Blocks[number - 1]);
175
+ getProposedCheckpointL2BlockNumber() {
176
+ return Promise.resolve(BlockNumber(this.proposedCheckpointBlockNumber));
69
177
  }
70
- /**
71
- * Gets up to `limit` amount of L2 blocks starting from `from`.
72
- * @param from - Number of the first block to return (inclusive).
73
- * @param limit - The maximum number of blocks to return.
74
- * @returns The requested mocked L2 blocks.
75
- */ getBlocks(from, limit, proven) {
76
- return Promise.resolve(this.l2Blocks.slice(from - 1, from - 1 + limit).filter((b)=>!proven || this.provenBlockNumber === undefined || b.number <= this.provenBlockNumber));
77
- }
78
- async getPublishedBlocks(from, limit, proven) {
79
- const blocks = await this.getBlocks(from, limit, proven);
80
- return blocks.map((block)=>PublishedL2Block.fromFields({
81
- block,
82
- l1: {
83
- blockNumber: BigInt(block.number),
84
- blockHash: Buffer32.random().toString(),
85
- timestamp: BigInt(block.number)
86
- },
87
- attestations: []
88
- }));
89
- }
90
- async getPublishedBlockByHash(blockHash) {
91
- for (const block of this.l2Blocks){
92
- const hash = await block.hash();
93
- if (hash.equals(blockHash)) {
94
- return PublishedL2Block.fromFields({
95
- block,
96
- l1: {
97
- blockNumber: BigInt(block.number),
98
- blockHash: Buffer32.random().toString(),
99
- timestamp: BigInt(block.number)
100
- },
101
- attestations: []
102
- });
103
- }
178
+ getCheckpoint(query) {
179
+ const checkpoint = this.resolveCheckpointQuery(query);
180
+ if (!checkpoint) {
181
+ return Promise.resolve(undefined);
104
182
  }
105
- return undefined;
183
+ return Promise.resolve(new PublishedCheckpoint(checkpoint, this.mockL1DataForCheckpoint(checkpoint), []));
106
184
  }
107
- getPublishedBlockByArchive(archive) {
108
- const block = this.l2Blocks.find((b)=>b.archive.root.equals(archive));
109
- if (!block) {
185
+ getCheckpoints(query) {
186
+ const checkpoints = this.resolveCheckpointsQuery(query);
187
+ return Promise.resolve(checkpoints.map((checkpoint)=>new PublishedCheckpoint(checkpoint, this.mockL1DataForCheckpoint(checkpoint), [])));
188
+ }
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) {
110
196
  return Promise.resolve(undefined);
111
197
  }
112
- return Promise.resolve(PublishedL2Block.fromFields({
113
- block,
114
- l1: {
115
- blockNumber: BigInt(block.number),
116
- blockHash: Buffer32.random().toString(),
117
- timestamp: BigInt(block.number)
118
- },
119
- attestations: []
120
- }));
121
- }
122
- async getBlockHeaderByHash(blockHash) {
123
- for (const block of this.l2Blocks){
124
- const hash = await block.hash();
125
- if (hash.equals(blockHash)) {
126
- return block.getBlockHeader();
127
- }
128
- }
129
- return undefined;
198
+ return Promise.resolve(this.checkpointToData(checkpoint));
130
199
  }
131
- getBlockHeaderByArchive(archive) {
132
- const block = this.l2Blocks.find((b)=>b.archive.root.equals(archive));
133
- return Promise.resolve(block?.getBlockHeader());
200
+ getCheckpointsData(query) {
201
+ const checkpoints = this.resolveCheckpointsQuery(query);
202
+ return Promise.resolve(checkpoints.map((c)=>this.checkpointToData(c)));
134
203
  }
135
- getBlockHeader(number) {
136
- return Promise.resolve(this.l2Blocks.at(typeof number === 'number' ? number - 1 : -1)?.getBlockHeader());
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
+ };
137
216
  }
138
- getBlocksForEpoch(epochNumber) {
139
- const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
140
- const [start, end] = getSlotRangeForEpoch(epochNumber, {
141
- epochDuration
142
- });
143
- const blocks = this.l2Blocks.filter((b)=>{
144
- const slot = b.header.globalVariables.slotNumber.toBigInt();
145
- return slot >= start && slot <= end;
146
- });
147
- return Promise.resolve(blocks);
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
+ }
148
238
  }
149
- async getBlockHeadersForEpoch(epochNumber) {
150
- const blocks = await this.getBlocksForEpoch(epochNumber);
151
- return blocks.map((b)=>b.getBlockHeader());
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);
244
+ }
245
+ getBlocksForSlot(slotNumber) {
246
+ const blocks = this.l2Blocks.filter((b)=>b.header.globalVariables.slotNumber === slotNumber);
247
+ return Promise.resolve(blocks);
152
248
  }
153
249
  /**
154
250
  * Gets a tx effect.
@@ -166,52 +262,83 @@ import { TxReceipt, TxStatus } from '@aztec/stdlib/tx';
166
262
  return {
167
263
  data: txEffect,
168
264
  l2BlockNumber: block.number,
169
- l2BlockHash: L2BlockHash.fromField(await block.hash()),
170
- txIndexInBlock: block.body.txEffects.indexOf(txEffect)
265
+ l2BlockHash: await block.hash(),
266
+ txIndexInBlock: block.body.txEffects.findIndex((t)=>t.txHash.equals(txHash)),
267
+ slotNumber: block.header.globalVariables.slotNumber
171
268
  };
172
269
  }
173
- /**
174
- * Gets a receipt of a settled tx.
175
- * @param txHash - The hash of a tx we try to get the receipt for.
176
- * @returns The requested tx receipt (or undefined if not found).
177
- */ async getSettledTxReceipt(txHash) {
178
- for (const block of this.l2Blocks){
179
- for (const txEffect of block.body.txEffects){
180
- if (txEffect.txHash.equals(txHash)) {
181
- return new TxReceipt(txHash, TxStatus.SUCCESS, '', txEffect.transactionFee.toBigInt(), L2BlockHash.fromField(await block.hash()), block.number);
182
- }
183
- }
184
- }
185
- return undefined;
270
+ getL2ToL1MembershipWitness() {
271
+ // Mock does not back the L2-to-L1 message witness flow.
272
+ return Promise.resolve(undefined);
186
273
  }
187
274
  async getL2Tips() {
188
- const [latest, proven, finalized] = [
275
+ const [latest, proven, finalized, checkpointed, proposedCheckpoint] = [
189
276
  await this.getBlockNumber(),
190
- await this.getProvenBlockNumber(),
191
- this.finalizedBlockNumber
277
+ this.provenBlockNumber,
278
+ this.finalizedBlockNumber,
279
+ this.checkpointedBlockNumber,
280
+ await this.getProposedCheckpointL2BlockNumber()
192
281
  ];
193
282
  const latestBlock = this.l2Blocks[latest - 1];
194
283
  const provenBlock = this.l2Blocks[proven - 1];
195
284
  const finalizedBlock = this.l2Blocks[finalized - 1];
196
- return {
197
- latest: {
198
- number: latest,
199
- hash: (await latestBlock?.hash())?.toString()
200
- },
201
- proven: {
202
- number: proven,
203
- hash: (await provenBlock?.hash())?.toString()
204
- },
205
- finalized: {
206
- number: finalized,
207
- hash: (await finalizedBlock?.hash())?.toString()
285
+ const checkpointedBlock = this.l2Blocks[checkpointed - 1];
286
+ const proposedCheckpointBlock = this.l2Blocks[proposedCheckpoint - 1];
287
+ // For genesis tips (block number 0) report the dynamic initial header hash so consumers
288
+ // running L2BlockStream against this mock agree at block 0 with their local tip store.
289
+ const genesisHash = (await this.initialHeader.hash()).toString();
290
+ const tipHash = async (block, number)=>{
291
+ if (block) {
292
+ return (await block.hash()).toString();
208
293
  }
294
+ return number === 0 ? genesisHash : '';
295
+ };
296
+ const latestBlockId = {
297
+ number: BlockNumber(latest),
298
+ hash: await tipHash(latestBlock, latest)
299
+ };
300
+ const provenBlockId = {
301
+ number: BlockNumber(proven),
302
+ hash: await tipHash(provenBlock, proven)
303
+ };
304
+ const finalizedBlockId = {
305
+ number: BlockNumber(finalized),
306
+ hash: await tipHash(finalizedBlock, finalized)
307
+ };
308
+ const checkpointedBlockId = {
309
+ number: BlockNumber(checkpointed),
310
+ hash: await tipHash(checkpointedBlock, checkpointed)
311
+ };
312
+ const proposedCheckpointBlockId = {
313
+ number: BlockNumber(proposedCheckpoint),
314
+ hash: await tipHash(proposedCheckpointBlock, proposedCheckpoint)
315
+ };
316
+ const makeTipId = (blockId)=>{
317
+ const checkpointNumber = this.findCheckpointNumberForBlock(blockId.number) ?? CheckpointNumber(0);
318
+ // Match production semantics: checkpoint 0 is fully synthetic (no real checkpoint header
319
+ // exists at 0), so its hash stays at the protocol constant `GENESIS_CHECKPOINT_HEADER_HASH`
320
+ // even though the block-0 hash is dynamic. See L2TipsCache for the production path.
321
+ const hash = checkpointNumber === 0 ? GENESIS_CHECKPOINT_HEADER_HASH.toString() : blockId.hash;
322
+ return {
323
+ block: blockId,
324
+ checkpoint: {
325
+ number: checkpointNumber,
326
+ hash
327
+ }
328
+ };
329
+ };
330
+ return {
331
+ proposed: latestBlockId,
332
+ checkpointed: makeTipId(checkpointedBlockId),
333
+ proven: makeTipId(provenBlockId),
334
+ finalized: makeTipId(finalizedBlockId),
335
+ proposedCheckpoint: makeTipId(proposedCheckpointBlockId)
209
336
  };
210
337
  }
211
- getL2EpochNumber() {
338
+ getSyncedL2EpochNumber() {
212
339
  throw new Error('Method not implemented.');
213
340
  }
214
- getL2SlotNumber() {
341
+ getSyncedL2SlotNumber() {
215
342
  throw new Error('Method not implemented.');
216
343
  }
217
344
  isEpochComplete(_epochNumber) {
@@ -220,9 +347,12 @@ import { TxReceipt, TxStatus } from '@aztec/stdlib/tx';
220
347
  getL1Constants() {
221
348
  return Promise.resolve(EmptyL1RollupConstants);
222
349
  }
350
+ isPruneDueAtSlot(_slot) {
351
+ return Promise.resolve(false);
352
+ }
223
353
  getGenesisValues() {
224
354
  return Promise.resolve({
225
- genesisArchiveRoot: new Fr(GENESIS_ARCHIVE_ROOT)
355
+ genesisArchiveRoot: this.genesisArchiveRoot ?? new Fr(GENESIS_ARCHIVE_ROOT)
226
356
  });
227
357
  }
228
358
  getL1Timestamp() {
@@ -263,6 +393,88 @@ import { TxReceipt, TxStatus } from '@aztec/stdlib/tx';
263
393
  syncImmediate() {
264
394
  return Promise.resolve();
265
395
  }
396
+ async getBlock(query) {
397
+ if ('number' in query) {
398
+ if (query.number === 0) {
399
+ return this.getGenesisBlock();
400
+ }
401
+ return this.l2Blocks[query.number - 1];
402
+ }
403
+ if ('hash' in query) {
404
+ const genesis = this.getGenesisBlock();
405
+ if ((await genesis.hash()).equals(query.hash)) {
406
+ return genesis;
407
+ }
408
+ for (const b of this.l2Blocks){
409
+ const hash = await b.hash();
410
+ if (hash.equals(query.hash)) {
411
+ return b;
412
+ }
413
+ }
414
+ return undefined;
415
+ }
416
+ if ('archive' in query) {
417
+ const genesis = this.getGenesisBlock();
418
+ if (genesis.archive.root.equals(query.archive)) {
419
+ return genesis;
420
+ }
421
+ return this.l2Blocks.find((b)=>b.archive.root.equals(query.archive));
422
+ }
423
+ const number = this.resolveBlockTag(query.tag);
424
+ if (number === 0) {
425
+ return this.getGenesisBlock();
426
+ }
427
+ return this.l2Blocks[number - 1];
428
+ }
429
+ resolveBlockTag(tag) {
430
+ switch(tag){
431
+ case 'latest':
432
+ case 'proposed':
433
+ return this.l2Blocks.length;
434
+ case 'checkpointed':
435
+ return this.checkpointedBlockNumber;
436
+ case 'proven':
437
+ return this.provenBlockNumber;
438
+ case 'finalized':
439
+ return this.finalizedBlockNumber;
440
+ }
441
+ }
442
+ getBlocks(query) {
443
+ let blocks;
444
+ if ('from' in query) {
445
+ blocks = this.l2Blocks.slice(query.from - 1, query.from - 1 + query.limit);
446
+ } else {
447
+ const epochCheckpoints = this.getCheckpointsInEpoch(query.epoch);
448
+ blocks = epochCheckpoints.flatMap((c)=>c.blocks);
449
+ }
450
+ if (query.onlyCheckpointed) {
451
+ blocks = blocks.filter((b)=>b.header.globalVariables.blockNumber <= this.checkpointedBlockNumber);
452
+ }
453
+ return Promise.resolve(blocks);
454
+ }
455
+ async getBlockData(query) {
456
+ const block = await this.getBlock(query);
457
+ if (!block) {
458
+ return undefined;
459
+ }
460
+ return {
461
+ header: block.header,
462
+ archive: block.archive,
463
+ blockHash: await block.hash(),
464
+ checkpointNumber: block.checkpointNumber,
465
+ indexWithinCheckpoint: block.indexWithinCheckpoint
466
+ };
467
+ }
468
+ async getBlocksData(query) {
469
+ const blocks = await this.getBlocks(query);
470
+ return Promise.all(blocks.map(async (block)=>({
471
+ header: block.header,
472
+ archive: block.archive,
473
+ blockHash: await block.hash(),
474
+ checkpointNumber: block.checkpointNumber,
475
+ indexWithinCheckpoint: block.indexWithinCheckpoint
476
+ })));
477
+ }
266
478
  isPendingChainInvalid() {
267
479
  return Promise.resolve(false);
268
480
  }
@@ -271,4 +483,21 @@ import { TxReceipt, TxStatus } from '@aztec/stdlib/tx';
271
483
  valid: true
272
484
  });
273
485
  }
486
+ getProposedCheckpointData(_query) {
487
+ return Promise.resolve(undefined);
488
+ }
489
+ /** Returns checkpoints whose slot falls within the given epoch. */ getCheckpointsInEpoch(epochNumber) {
490
+ const epochDuration = DefaultL1ContractsConfig.aztecEpochDuration;
491
+ const [start, end] = getSlotRangeForEpoch(epochNumber, {
492
+ epochDuration
493
+ });
494
+ return this.checkpointList.filter((c)=>c.header.slotNumber >= start && c.header.slotNumber <= end);
495
+ }
496
+ /** Creates a mock L1PublishedData for a checkpoint. */ mockL1DataForCheckpoint(checkpoint) {
497
+ return new L1PublishedData(BigInt(checkpoint.number), BigInt(checkpoint.number), Buffer32.random().toString());
498
+ }
499
+ /** Finds the checkpoint number for a block, or undefined if the block is not in any checkpoint. */ findCheckpointNumberForBlock(blockNumber) {
500
+ const checkpoint = this.checkpointList.find((c)=>c.blocks.some((b)=>b.number === blockNumber));
501
+ return checkpoint?.number;
502
+ }
274
503
  }