@aztec/archiver 0.0.0-test.1 → 0.0.1-commit.017a351

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 (252) hide show
  1. package/README.md +172 -9
  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 +19 -17
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +125 -61
  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/store/contract_instance_store.js +112 -0
  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/store/message_store.js +230 -0
  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/structs/inbox_message.js +39 -0
  100. package/dest/structs/published.d.ts +2 -0
  101. package/dest/structs/published.d.ts.map +1 -0
  102. package/dest/structs/published.js +1 -0
  103. package/dest/test/fake_l1_state.d.ts +214 -0
  104. package/dest/test/fake_l1_state.d.ts.map +1 -0
  105. package/dest/test/fake_l1_state.js +517 -0
  106. package/dest/test/index.d.ts +2 -1
  107. package/dest/test/index.d.ts.map +1 -1
  108. package/dest/test/index.js +4 -1
  109. package/dest/test/mock_archiver.d.ts +16 -8
  110. package/dest/test/mock_archiver.d.ts.map +1 -1
  111. package/dest/test/mock_archiver.js +19 -14
  112. package/dest/test/mock_l1_to_l2_message_source.d.ts +9 -6
  113. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  114. package/dest/test/mock_l1_to_l2_message_source.js +31 -7
  115. package/dest/test/mock_l2_block_source.d.ts +98 -35
  116. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  117. package/dest/test/mock_l2_block_source.js +416 -76
  118. package/dest/test/mock_structs.d.ts +88 -0
  119. package/dest/test/mock_structs.d.ts.map +1 -0
  120. package/dest/test/mock_structs.js +183 -0
  121. package/dest/test/noop_l1_archiver.d.ts +30 -0
  122. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  123. package/dest/test/noop_l1_archiver.js +85 -0
  124. package/package.json +31 -33
  125. package/src/archiver.ts +719 -0
  126. package/src/config.ts +108 -0
  127. package/src/errors.ts +203 -0
  128. package/src/factory.ts +190 -76
  129. package/src/index.ts +27 -3
  130. package/src/interfaces.ts +9 -0
  131. package/src/l1/README.md +55 -0
  132. package/src/l1/bin/retrieve-calldata.ts +194 -0
  133. package/src/l1/calldata_retriever.ts +522 -0
  134. package/src/l1/data_retrieval.ts +484 -0
  135. package/src/l1/debug_tx.ts +99 -0
  136. package/src/l1/spire_proposer.ts +152 -0
  137. package/src/l1/trace_tx.ts +128 -0
  138. package/src/l1/types.ts +13 -0
  139. package/src/l1/validate_historical_logs.ts +140 -0
  140. package/src/l1/validate_trace.ts +229 -0
  141. package/src/modules/contract_data_source_adapter.ts +55 -0
  142. package/src/modules/data_source_base.ts +495 -0
  143. package/src/modules/data_store_updater.ts +518 -0
  144. package/src/modules/instrumentation.ts +204 -0
  145. package/src/modules/l1_synchronizer.ts +1257 -0
  146. package/src/modules/outbox_trees_resolver.ts +199 -0
  147. package/src/modules/validation.ts +130 -0
  148. package/src/store/block_store.ts +1540 -0
  149. package/src/store/contract_class_store.ts +108 -0
  150. package/src/store/contract_instance_store.ts +161 -0
  151. package/src/store/data_stores.ts +104 -0
  152. package/src/store/function_names_cache.ts +37 -0
  153. package/src/store/l2_tips_cache.ts +35 -0
  154. package/src/store/log_store.ts +379 -0
  155. package/src/store/log_store_codec.ts +132 -0
  156. package/src/store/message_store.ts +311 -0
  157. package/src/structs/inbox_message.ts +41 -0
  158. package/src/structs/published.ts +1 -0
  159. package/src/test/fake_l1_state.ts +770 -0
  160. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  161. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  162. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  163. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  164. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  165. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  166. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  167. package/src/test/index.ts +4 -0
  168. package/src/test/mock_archiver.ts +23 -16
  169. package/src/test/mock_l1_to_l2_message_source.ts +27 -8
  170. package/src/test/mock_l2_block_source.ts +514 -84
  171. package/src/test/mock_structs.ts +325 -0
  172. package/src/test/noop_l1_archiver.ts +148 -0
  173. package/dest/archiver/archiver.d.ts +0 -197
  174. package/dest/archiver/archiver.d.ts.map +0 -1
  175. package/dest/archiver/archiver.js +0 -900
  176. package/dest/archiver/archiver_store.d.ts +0 -220
  177. package/dest/archiver/archiver_store.d.ts.map +0 -1
  178. package/dest/archiver/archiver_store.js +0 -4
  179. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  180. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  181. package/dest/archiver/archiver_store_test_suite.js +0 -794
  182. package/dest/archiver/config.d.ts +0 -37
  183. package/dest/archiver/config.d.ts.map +0 -1
  184. package/dest/archiver/config.js +0 -46
  185. package/dest/archiver/data_retrieval.d.ts +0 -74
  186. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  187. package/dest/archiver/data_retrieval.js +0 -283
  188. package/dest/archiver/errors.d.ts +0 -4
  189. package/dest/archiver/errors.d.ts.map +0 -1
  190. package/dest/archiver/errors.js +0 -5
  191. package/dest/archiver/index.d.ts +0 -8
  192. package/dest/archiver/index.d.ts.map +0 -1
  193. package/dest/archiver/index.js +0 -5
  194. package/dest/archiver/instrumentation.d.ts +0 -29
  195. package/dest/archiver/instrumentation.d.ts.map +0 -1
  196. package/dest/archiver/instrumentation.js +0 -99
  197. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -87
  198. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  199. package/dest/archiver/kv_archiver_store/block_store.js +0 -217
  200. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  201. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  202. package/dest/archiver/kv_archiver_store/contract_class_store.js +0 -126
  203. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -21
  204. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  205. package/dest/archiver/kv_archiver_store/contract_instance_store.js +0 -63
  206. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -153
  207. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  208. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -254
  209. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
  210. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  211. package/dest/archiver/kv_archiver_store/log_store.js +0 -364
  212. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -33
  213. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  214. package/dest/archiver/kv_archiver_store/message_store.js +0 -85
  215. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +0 -12
  216. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +0 -1
  217. package/dest/archiver/kv_archiver_store/nullifier_store.js +0 -73
  218. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
  219. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
  220. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
  221. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +0 -175
  222. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +0 -1
  223. package/dest/archiver/memory_archiver_store/memory_archiver_store.js +0 -636
  224. package/dest/archiver/structs/data_retrieval.d.ts +0 -27
  225. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  226. package/dest/archiver/structs/published.d.ts +0 -11
  227. package/dest/archiver/structs/published.d.ts.map +0 -1
  228. package/dest/archiver/structs/published.js +0 -1
  229. package/dest/rpc/index.d.ts +0 -10
  230. package/dest/rpc/index.d.ts.map +0 -1
  231. package/dest/rpc/index.js +0 -18
  232. package/src/archiver/archiver.ts +0 -1181
  233. package/src/archiver/archiver_store.ts +0 -263
  234. package/src/archiver/archiver_store_test_suite.ts +0 -810
  235. package/src/archiver/config.ts +0 -92
  236. package/src/archiver/data_retrieval.ts +0 -422
  237. package/src/archiver/errors.ts +0 -5
  238. package/src/archiver/index.ts +0 -7
  239. package/src/archiver/instrumentation.ts +0 -132
  240. package/src/archiver/kv_archiver_store/block_store.ts +0 -283
  241. package/src/archiver/kv_archiver_store/contract_class_store.ts +0 -186
  242. package/src/archiver/kv_archiver_store/contract_instance_store.ts +0 -107
  243. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -358
  244. package/src/archiver/kv_archiver_store/log_store.ts +0 -444
  245. package/src/archiver/kv_archiver_store/message_store.ts +0 -102
  246. package/src/archiver/kv_archiver_store/nullifier_store.ts +0 -97
  247. package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
  248. package/src/archiver/memory_archiver_store/memory_archiver_store.ts +0 -801
  249. package/src/archiver/structs/published.ts +0 -11
  250. package/src/rpc/index.ts +0 -20
  251. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  252. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
@@ -0,0 +1,108 @@
1
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
+ import { toArray } from '@aztec/foundation/iterable';
3
+ import { BufferReader, numToUInt8, serializeToBuffer } from '@aztec/foundation/serialize';
4
+ import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
5
+ import type {
6
+ ContractClassPublic,
7
+ ContractClassPublicWithBlockNumber,
8
+ ContractClassPublicWithCommitment,
9
+ } from '@aztec/stdlib/contract';
10
+
11
+ /**
12
+ * LMDB-based contract class storage for the archiver.
13
+ */
14
+ export class ContractClassStore {
15
+ #contractClasses: AztecAsyncMap<string, Buffer>;
16
+ #bytecodeCommitments: AztecAsyncMap<string, Buffer>;
17
+
18
+ constructor(private db: AztecAsyncKVStore) {
19
+ this.#contractClasses = db.openMap('archiver_contract_classes');
20
+ this.#bytecodeCommitments = db.openMap('archiver_bytecode_commitments');
21
+ }
22
+
23
+ /**
24
+ * Adds multiple contract classes to the store.
25
+ * @param data - Contract classes (with bytecode commitments) to add.
26
+ * @param blockNumber - L2 block number where the classes were registered.
27
+ * @returns True if every insert succeeded.
28
+ */
29
+ async addContractClasses(data: ContractClassPublicWithCommitment[], blockNumber: number): Promise<boolean> {
30
+ return (await Promise.all(data.map(c => this.addContractClass(c, c.publicBytecodeCommitment, blockNumber)))).every(
31
+ Boolean,
32
+ );
33
+ }
34
+
35
+ /**
36
+ * Removes multiple contract classes from the store, but only if they were registered at or after the given block.
37
+ * @param data - Contract classes to delete.
38
+ * @param blockNumber - Lower bound on the block number at which the classes were registered.
39
+ * @returns True if every delete succeeded.
40
+ */
41
+ async deleteContractClasses(data: ContractClassPublic[], blockNumber: number): Promise<boolean> {
42
+ return (await Promise.all(data.map(c => this.deleteContractClass(c, blockNumber)))).every(Boolean);
43
+ }
44
+
45
+ async addContractClass(
46
+ contractClass: ContractClassPublic,
47
+ bytecodeCommitment: Fr,
48
+ blockNumber: number,
49
+ ): Promise<void> {
50
+ await this.db.transactionAsync(async () => {
51
+ const key = contractClass.id.toString();
52
+ if (await this.#contractClasses.hasAsync(key)) {
53
+ throw new Error(`Contract class ${key} already exists, cannot add again at block ${blockNumber}`);
54
+ }
55
+ await this.#contractClasses.set(
56
+ key,
57
+ serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
58
+ );
59
+ await this.#bytecodeCommitments.set(key, bytecodeCommitment.toBuffer());
60
+ });
61
+ }
62
+
63
+ async deleteContractClass(contractClass: ContractClassPublic, blockNumber: number): Promise<void> {
64
+ const restoredContractClass = await this.#contractClasses.getAsync(contractClass.id.toString());
65
+ if (restoredContractClass && deserializeContractClassPublic(restoredContractClass).l2BlockNumber >= blockNumber) {
66
+ await this.db.transactionAsync(async () => {
67
+ await this.#contractClasses.delete(contractClass.id.toString());
68
+ await this.#bytecodeCommitments.delete(contractClass.id.toString());
69
+ });
70
+ }
71
+ }
72
+
73
+ async getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
74
+ const contractClass = await this.#contractClasses.getAsync(id.toString());
75
+ return contractClass && { ...deserializeContractClassPublic(contractClass), id };
76
+ }
77
+
78
+ async getBytecodeCommitment(id: Fr): Promise<Fr | undefined> {
79
+ const value = await this.#bytecodeCommitments.getAsync(id.toString());
80
+ return value === undefined ? undefined : Fr.fromBuffer(value);
81
+ }
82
+
83
+ async getContractClassIds(): Promise<Fr[]> {
84
+ return (await toArray(this.#contractClasses.keysAsync())).map(key => Fr.fromHexString(key));
85
+ }
86
+ }
87
+
88
+ function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWithBlockNumber, 'id'>): Buffer {
89
+ return serializeToBuffer(
90
+ contractClass.l2BlockNumber,
91
+ numToUInt8(contractClass.version),
92
+ contractClass.artifactHash,
93
+ contractClass.packedBytecode.length,
94
+ contractClass.packedBytecode,
95
+ contractClass.privateFunctionsRoot,
96
+ );
97
+ }
98
+
99
+ function deserializeContractClassPublic(buffer: Buffer): Omit<ContractClassPublicWithBlockNumber, 'id'> {
100
+ const reader = BufferReader.asReader(buffer);
101
+ return {
102
+ l2BlockNumber: reader.readNumber(),
103
+ version: reader.readUInt8() as 1,
104
+ artifactHash: reader.readObject(Fr),
105
+ packedBytecode: reader.readBuffer(),
106
+ privateFunctionsRoot: reader.readObject(Fr),
107
+ };
108
+ }
@@ -0,0 +1,161 @@
1
+ import type { Fr } from '@aztec/foundation/curves/bn254';
2
+ import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
3
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
+ import {
5
+ type ContractInstanceUpdateWithAddress,
6
+ type ContractInstanceWithAddress,
7
+ SerializableContractInstance,
8
+ SerializableContractInstanceUpdate,
9
+ } from '@aztec/stdlib/contract';
10
+ import type { UInt64 } from '@aztec/stdlib/types';
11
+
12
+ type ContractInstanceUpdateKey = [string, string] | [string, string, number];
13
+
14
+ /**
15
+ * LMDB-based contract instance storage for the archiver.
16
+ */
17
+ export class ContractInstanceStore {
18
+ #contractInstances: AztecAsyncMap<string, Buffer>;
19
+ #contractInstancePublishedAt: AztecAsyncMap<string, number>;
20
+ #contractInstanceUpdates: AztecAsyncMap<ContractInstanceUpdateKey, Buffer>;
21
+
22
+ constructor(private db: AztecAsyncKVStore) {
23
+ this.#contractInstances = db.openMap('archiver_contract_instances');
24
+ this.#contractInstancePublishedAt = db.openMap('archiver_contract_instances_publication_block_number');
25
+ this.#contractInstanceUpdates = db.openMap('archiver_contract_instance_updates');
26
+ }
27
+
28
+ /**
29
+ * Adds multiple contract instances to the store.
30
+ * @param data - Contract instances to add.
31
+ * @param blockNumber - L2 block number where the instances were deployed.
32
+ * @returns True if every insert succeeded.
33
+ */
34
+ async addContractInstances(data: ContractInstanceWithAddress[], blockNumber: number): Promise<boolean> {
35
+ return (await Promise.all(data.map(c => this.addContractInstance(c, blockNumber)))).every(Boolean);
36
+ }
37
+
38
+ /**
39
+ * Removes multiple contract instances from the store.
40
+ * @param data - Contract instances to delete.
41
+ * @returns True if every delete succeeded.
42
+ */
43
+ async deleteContractInstances(data: ContractInstanceWithAddress[]): Promise<boolean> {
44
+ return (await Promise.all(data.map(c => this.deleteContractInstance(c)))).every(Boolean);
45
+ }
46
+
47
+ /**
48
+ * Adds multiple contract instance updates to the store.
49
+ * @param data - Contract instance updates to add.
50
+ * @param timestamp - Timestamp at which the updates were scheduled.
51
+ * @returns True if every insert succeeded.
52
+ */
53
+ async addContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], timestamp: UInt64): Promise<boolean> {
54
+ return (
55
+ await Promise.all(data.map((update, logIndex) => this.addContractInstanceUpdate(update, timestamp, logIndex)))
56
+ ).every(Boolean);
57
+ }
58
+
59
+ /**
60
+ * Removes multiple contract instance updates from the store.
61
+ * @param data - Contract instance updates to delete.
62
+ * @param timestamp - Timestamp at which the updates were scheduled.
63
+ * @returns True if every delete succeeded.
64
+ */
65
+ async deleteContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], timestamp: UInt64): Promise<boolean> {
66
+ return (
67
+ await Promise.all(data.map((update, logIndex) => this.deleteContractInstanceUpdate(update, timestamp, logIndex)))
68
+ ).every(Boolean);
69
+ }
70
+
71
+ addContractInstance(contractInstance: ContractInstanceWithAddress, blockNumber: number): Promise<void> {
72
+ return this.db.transactionAsync(async () => {
73
+ const key = contractInstance.address.toString();
74
+ if (await this.#contractInstances.hasAsync(key)) {
75
+ throw new Error(
76
+ `Contract instance at ${key} already exists (deployed at block ${await this.#contractInstancePublishedAt.getAsync(key)}), cannot add again at block ${blockNumber}`,
77
+ );
78
+ }
79
+ await this.#contractInstances.set(key, new SerializableContractInstance(contractInstance).toBuffer());
80
+ await this.#contractInstancePublishedAt.set(key, blockNumber);
81
+ });
82
+ }
83
+
84
+ deleteContractInstance(contractInstance: ContractInstanceWithAddress): Promise<void> {
85
+ return this.db.transactionAsync(async () => {
86
+ await this.#contractInstances.delete(contractInstance.address.toString());
87
+ await this.#contractInstancePublishedAt.delete(contractInstance.address.toString());
88
+ });
89
+ }
90
+
91
+ getUpdateKey(contractAddress: AztecAddress, timestamp: UInt64, logIndex?: number): ContractInstanceUpdateKey {
92
+ if (logIndex === undefined) {
93
+ return [contractAddress.toString(), timestamp.toString()];
94
+ } else {
95
+ return [contractAddress.toString(), timestamp.toString(), logIndex];
96
+ }
97
+ }
98
+
99
+ addContractInstanceUpdate(
100
+ contractInstanceUpdate: ContractInstanceUpdateWithAddress,
101
+ timestamp: UInt64,
102
+ logIndex: number,
103
+ ): Promise<void> {
104
+ return this.#contractInstanceUpdates.set(
105
+ this.getUpdateKey(contractInstanceUpdate.address, timestamp, logIndex),
106
+ new SerializableContractInstanceUpdate(contractInstanceUpdate).toBuffer(),
107
+ );
108
+ }
109
+
110
+ deleteContractInstanceUpdate(
111
+ contractInstanceUpdate: ContractInstanceUpdateWithAddress,
112
+ timestamp: UInt64,
113
+ logIndex: number,
114
+ ): Promise<void> {
115
+ return this.#contractInstanceUpdates.delete(this.getUpdateKey(contractInstanceUpdate.address, timestamp, logIndex));
116
+ }
117
+
118
+ async getCurrentContractInstanceClassId(address: AztecAddress, timestamp: UInt64, originalClassId: Fr): Promise<Fr> {
119
+ // We need to find the last update before the given timestamp
120
+ const queryResult = await this.#contractInstanceUpdates
121
+ .valuesAsync({
122
+ reverse: true,
123
+ start: this.getUpdateKey(address, 0n), // Make sure we only look at updates for this contract
124
+ end: this.getUpdateKey(address, timestamp + 1n), // No update can match this key since it doesn't have a log index. We want the highest key <= timestamp
125
+ limit: 1,
126
+ })
127
+ .next();
128
+ if (queryResult.done) {
129
+ return originalClassId;
130
+ }
131
+
132
+ const serializedUpdate = queryResult.value;
133
+ const update = SerializableContractInstanceUpdate.fromBuffer(serializedUpdate);
134
+ if (timestamp < update.timestampOfChange) {
135
+ return update.prevContractClassId.isZero() ? originalClassId : update.prevContractClassId;
136
+ }
137
+ return update.newContractClassId;
138
+ }
139
+
140
+ async getContractInstance(
141
+ address: AztecAddress,
142
+ timestamp: UInt64,
143
+ ): Promise<ContractInstanceWithAddress | undefined> {
144
+ const contractInstance = await this.#contractInstances.getAsync(address.toString());
145
+ if (!contractInstance) {
146
+ return undefined;
147
+ }
148
+
149
+ const instance = SerializableContractInstance.fromBuffer(contractInstance).withAddress(address);
150
+ instance.currentContractClassId = await this.getCurrentContractInstanceClassId(
151
+ address,
152
+ timestamp,
153
+ instance.originalContractClassId,
154
+ );
155
+ return instance;
156
+ }
157
+
158
+ getContractInstanceDeploymentBlockNumber(address: AztecAddress): Promise<number | undefined> {
159
+ return this.#contractInstancePublishedAt.getAsync(address.toString());
160
+ }
161
+ }
@@ -0,0 +1,104 @@
1
+ import type { L1BlockId } from '@aztec/ethereum/l1-types';
2
+ import type { AztecAsyncKVStore } from '@aztec/kv-store';
3
+ import type { BlockHash } from '@aztec/stdlib/block';
4
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
5
+
6
+ import { join } from 'path';
7
+
8
+ import { ArchiverContractDataSourceAdapter } from '../modules/contract_data_source_adapter.js';
9
+ import { BlockStore } from './block_store.js';
10
+ import { ContractClassStore } from './contract_class_store.js';
11
+ import { ContractInstanceStore } from './contract_instance_store.js';
12
+ import { FunctionNamesCache } from './function_names_cache.js';
13
+ import { LogStore } from './log_store.js';
14
+ import { MessageStore } from './message_store.js';
15
+
16
+ export const ARCHIVER_DB_VERSION = 7;
17
+
18
+ /**
19
+ * Represents the latest L1 block processed by the archiver for various objects in L2.
20
+ */
21
+ export type ArchiverL1SynchPoint = {
22
+ /** Number of the last L1 block that added a new L2 checkpoint metadata. */
23
+ blocksSynchedTo?: bigint;
24
+ /** Last L1 block checked for L1 to L2 messages. */
25
+ messagesSynchedTo?: L1BlockId;
26
+ };
27
+
28
+ /**
29
+ * Bundle of archiver-owned LMDB substores plus the in-memory caches that span them.
30
+ *
31
+ * Replaces the former `KVArchiverDataStore` pass-through wrapper. Callers reach into
32
+ * the relevant substore directly (e.g. `stores.blocks.getBlock`) and use
33
+ * {@link createArchiverDataStores} to wire them up against a shared KV store.
34
+ */
35
+ export type ArchiverDataStores = {
36
+ /** The underlying key-value store. Use {@link AztecAsyncKVStore.transactionAsync} to compose updates atomically. */
37
+ db: AztecAsyncKVStore;
38
+ /** Blocks, checkpoints, tx effects, proven/finalized state. */
39
+ blocks: BlockStore;
40
+ /** Public, private and contract class logs. */
41
+ logs: LogStore;
42
+ /** L1 to L2 messages and message sync state. */
43
+ messages: MessageStore;
44
+ /** Contract classes (with bytecode commitments). */
45
+ contractClasses: ContractClassStore;
46
+ /** Contract instances and contract instance updates. */
47
+ contractInstances: ContractInstanceStore;
48
+ /** In-memory cache of public function selectors -> names. */
49
+ functionNames: FunctionNamesCache;
50
+ };
51
+
52
+ /**
53
+ * Wires up the archiver substores against a shared KV store and returns the
54
+ * {@link ArchiverDataStores} bundle.
55
+ *
56
+ * @param genesisBlockHash - Hash of the synthetic genesis block, forwarded to the {@link LogStore} so it
57
+ * can resolve a genesis `referenceBlock` (used by the PXE during early sync) instead of treating it as a
58
+ * reorg.
59
+ */
60
+ export function createArchiverDataStores(db: AztecAsyncKVStore, genesisBlockHash: BlockHash): ArchiverDataStores {
61
+ const blocks = new BlockStore(db);
62
+ return {
63
+ db,
64
+ blocks,
65
+ logs: new LogStore(db, blocks, genesisBlockHash),
66
+ messages: new MessageStore(db),
67
+ contractClasses: new ContractClassStore(db),
68
+ contractInstances: new ContractInstanceStore(db),
69
+ functionNames: new FunctionNamesCache(),
70
+ };
71
+ }
72
+
73
+ /**
74
+ * Returns the L1 sync point of the archiver, combining the block sync point from {@link BlockStore}
75
+ * and the message sync point from {@link MessageStore}.
76
+ */
77
+ export async function getArchiverSynchPoint(stores: ArchiverDataStores): Promise<ArchiverL1SynchPoint> {
78
+ const [blocksSynchedTo, messagesSynchedTo] = await Promise.all([
79
+ stores.blocks.getSynchedL1BlockNumber(),
80
+ stores.messages.getSynchedL1Block(),
81
+ ]);
82
+ return { blocksSynchedTo, messagesSynchedTo };
83
+ }
84
+
85
+ /**
86
+ * Backs up the underlying KV store to the given folder. Returns the path to the resulting db file.
87
+ */
88
+ export async function backupArchiverDataStores(
89
+ stores: ArchiverDataStores,
90
+ path: string,
91
+ compress = true,
92
+ ): Promise<string> {
93
+ await stores.db.backupTo(path, compress);
94
+ return join(path, 'data.mdb');
95
+ }
96
+
97
+ /**
98
+ * Returns a {@link ContractDataSource} adapter over {@link ArchiverDataStores}.
99
+ * Used by contexts (e.g. offline epoch re-prover tools) that need a ContractDataSource
100
+ * but do not need a full archiver instance.
101
+ */
102
+ export function createContractDataSource(stores: ArchiverDataStores): ContractDataSource {
103
+ return new ArchiverContractDataSourceAdapter(stores);
104
+ }
@@ -0,0 +1,37 @@
1
+ import { createLogger } from '@aztec/foundation/log';
2
+ import { FunctionSelector } from '@aztec/stdlib/abi';
3
+
4
+ const MAX_FUNCTION_SIGNATURES = 1000;
5
+ const MAX_FUNCTION_NAME_LEN = 256;
6
+
7
+ /**
8
+ * In-memory cache mapping public function selectors to function names.
9
+ *
10
+ * Populated opportunistically (e.g. by PXE registering signatures from artifacts) so the
11
+ * archiver can attach human-readable names to logs and traces. Bounded by
12
+ * {@link MAX_FUNCTION_SIGNATURES} to avoid unbounded growth from untrusted callers.
13
+ */
14
+ export class FunctionNamesCache {
15
+ private readonly log = createLogger('archiver:data-stores');
16
+ private readonly names: Map<string, string> = new Map();
17
+
18
+ /** Adds the given public function signatures to the cache. */
19
+ public async register(signatures: string[]): Promise<void> {
20
+ for (const sig of signatures) {
21
+ if (this.names.size > MAX_FUNCTION_SIGNATURES) {
22
+ return;
23
+ }
24
+ try {
25
+ const selector = await FunctionSelector.fromSignature(sig);
26
+ this.names.set(selector.toString(), sig.slice(0, sig.indexOf('(')).slice(0, MAX_FUNCTION_NAME_LEN));
27
+ } catch {
28
+ this.log.warn(`Failed to parse signature: ${sig}. Ignoring`);
29
+ }
30
+ }
31
+ }
32
+
33
+ /** Looks up a function name for the given selector, or returns undefined if not registered. */
34
+ public get(selector: FunctionSelector): string | undefined {
35
+ return this.names.get(selector.toString());
36
+ }
37
+ }
@@ -0,0 +1,35 @@
1
+ import type { BlockHash, L2Tips } from '@aztec/stdlib/block';
2
+
3
+ import type { BlockStore } from './block_store.js';
4
+
5
+ /**
6
+ * In-memory cache for L2 chain tips (proposed, checkpointed, proven, finalized).
7
+ * Populated from the BlockStore on first access, then kept up-to-date by the ArchiverDataStoreUpdater.
8
+ * Refresh calls should happen *after* the store transaction that mutates block data has committed,
9
+ * so the cache loads from committed state and is never replaced if the writer aborts.
10
+ */
11
+ export class L2TipsCache {
12
+ #tipsPromise: Promise<L2Tips> | undefined;
13
+
14
+ /**
15
+ * The genesis block hash is dynamic — derived from the injected initial header, which depends on
16
+ * `genesisTimestamp` and any prefilled state — so it is supplied here rather than read from store.
17
+ * The genesis checkpoint hash, by contrast, is the static protocol constant and is resolved
18
+ * inside the block store.
19
+ */
20
+ constructor(
21
+ private blockStore: BlockStore,
22
+ private readonly initialBlockHash: BlockHash,
23
+ ) {}
24
+
25
+ /** Returns the cached L2 tips. Loads from the block store on first call. */
26
+ public getL2Tips(): Promise<L2Tips> {
27
+ return (this.#tipsPromise ??= this.blockStore.getL2TipsData(this.initialBlockHash));
28
+ }
29
+
30
+ /** Reloads the L2 tips from the block store. Should be called after the writer transaction has committed. */
31
+ public async refresh(): Promise<void> {
32
+ this.#tipsPromise = this.blockStore.getL2TipsData(this.initialBlockHash);
33
+ await this.#tipsPromise;
34
+ }
35
+ }