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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (236) hide show
  1. package/README.md +164 -22
  2. package/dest/archiver.d.ts +194 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +949 -0
  5. package/dest/config.d.ts +34 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/config.js +95 -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 +16 -10
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +117 -20
  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 +143 -0
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/l1/calldata_retriever.js +413 -0
  26. package/dest/l1/data_retrieval.d.ts +102 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/{archiver → l1}/data_retrieval.js +85 -165
  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 +32 -0
  50. package/dest/modules/data_source_base.d.ts +112 -0
  51. package/dest/modules/data_source_base.d.ts.map +1 -0
  52. package/dest/modules/data_source_base.js +322 -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 +393 -0
  56. package/dest/modules/instrumentation.d.ts +63 -0
  57. package/dest/modules/instrumentation.d.ts.map +1 -0
  58. package/dest/modules/instrumentation.js +166 -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 +1381 -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 +40 -0
  66. package/dest/modules/validation.d.ts.map +1 -0
  67. package/dest/{archiver → modules}/validation.js +41 -17
  68. package/dest/store/block_store.d.ts +303 -0
  69. package/dest/store/block_store.d.ts.map +1 -0
  70. package/dest/store/block_store.js +1206 -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 +98 -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 +55 -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 +78 -0
  90. package/dest/store/log_store_codec.d.ts.map +1 -0
  91. package/dest/store/log_store_codec.js +110 -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/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  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 -6
  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 +5 -6
  109. package/dest/test/mock_archiver.d.ts.map +1 -1
  110. package/dest/test/mock_archiver.js +8 -12
  111. package/dest/test/mock_l1_to_l2_message_source.d.ts +6 -7
  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 +19 -14
  114. package/dest/test/mock_l2_block_source.d.ts +68 -40
  115. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  116. package/dest/test/mock_l2_block_source.js +347 -145
  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 -12
  120. package/dest/test/noop_l1_archiver.d.ts +34 -0
  121. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  122. package/dest/test/noop_l1_archiver.js +88 -0
  123. package/package.json +18 -18
  124. package/src/archiver.ts +785 -0
  125. package/src/config.ts +125 -0
  126. package/src/errors.ts +203 -0
  127. package/src/factory.ts +185 -22
  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 +529 -0
  133. package/src/{archiver → l1}/data_retrieval.ts +157 -243
  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 +46 -0
  141. package/src/modules/data_source_base.ts +466 -0
  142. package/src/modules/data_store_updater.ts +519 -0
  143. package/src/modules/instrumentation.ts +217 -0
  144. package/src/modules/l1_synchronizer.ts +1320 -0
  145. package/src/modules/outbox_trees_resolver.ts +199 -0
  146. package/src/modules/validation.ts +186 -0
  147. package/src/store/block_store.ts +1551 -0
  148. package/src/store/contract_class_store.ts +126 -0
  149. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +70 -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 +380 -0
  154. package/src/store/log_store_codec.ts +143 -0
  155. package/src/{archiver/kv_archiver_store → store}/message_store.ts +81 -28
  156. package/src/{archiver/structs → structs}/inbox_message.ts +8 -9
  157. package/src/{archiver/structs → structs}/published.ts +0 -1
  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 +10 -14
  168. package/src/test/mock_l1_to_l2_message_source.ts +16 -15
  169. package/src/test/mock_l2_block_source.ts +416 -161
  170. package/src/test/mock_structs.ts +292 -14
  171. package/src/test/noop_l1_archiver.ts +158 -0
  172. package/dest/archiver/archiver.d.ts +0 -290
  173. package/dest/archiver/archiver.d.ts.map +0 -1
  174. package/dest/archiver/archiver.js +0 -1434
  175. package/dest/archiver/archiver_store.d.ts +0 -256
  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 -1289
  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 -55
  184. package/dest/archiver/data_retrieval.d.ts +0 -80
  185. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  186. package/dest/archiver/errors.d.ts +0 -12
  187. package/dest/archiver/errors.d.ts.map +0 -1
  188. package/dest/archiver/errors.js +0 -17
  189. package/dest/archiver/index.d.ts +0 -7
  190. package/dest/archiver/index.d.ts.map +0 -1
  191. package/dest/archiver/index.js +0 -4
  192. package/dest/archiver/instrumentation.d.ts +0 -35
  193. package/dest/archiver/instrumentation.d.ts.map +0 -1
  194. package/dest/archiver/instrumentation.js +0 -140
  195. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -125
  196. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  197. package/dest/archiver/kv_archiver_store/block_store.js +0 -371
  198. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  199. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  200. package/dest/archiver/kv_archiver_store/contract_class_store.js +0 -120
  201. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  202. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  203. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -169
  204. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  205. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -296
  206. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
  207. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  208. package/dest/archiver/kv_archiver_store/log_store.js +0 -337
  209. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -39
  210. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  211. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  212. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  213. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  214. package/dest/archiver/structs/published.d.ts +0 -3
  215. package/dest/archiver/structs/published.d.ts.map +0 -1
  216. package/dest/archiver/validation.d.ts +0 -17
  217. package/dest/archiver/validation.d.ts.map +0 -1
  218. package/dest/rpc/index.d.ts +0 -9
  219. package/dest/rpc/index.d.ts.map +0 -1
  220. package/dest/rpc/index.js +0 -15
  221. package/src/archiver/archiver.ts +0 -1880
  222. package/src/archiver/archiver_store.ts +0 -310
  223. package/src/archiver/archiver_store_test_suite.ts +0 -1295
  224. package/src/archiver/config.ts +0 -79
  225. package/src/archiver/errors.ts +0 -26
  226. package/src/archiver/index.ts +0 -6
  227. package/src/archiver/instrumentation.ts +0 -187
  228. package/src/archiver/kv_archiver_store/block_store.ts +0 -482
  229. package/src/archiver/kv_archiver_store/contract_class_store.ts +0 -176
  230. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -423
  231. package/src/archiver/kv_archiver_store/log_store.ts +0 -407
  232. package/src/archiver/validation.ts +0 -124
  233. package/src/rpc/index.ts +0 -16
  234. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  235. /package/dest/{archiver/structs → structs}/published.js +0 -0
  236. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
@@ -0,0 +1,126 @@
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 { isProtocolContractClass } from '@aztec/protocol-contracts';
6
+ import type {
7
+ ContractClassPublic,
8
+ ContractClassPublicWithBlockNumber,
9
+ ContractClassPublicWithCommitment,
10
+ } from '@aztec/stdlib/contract';
11
+
12
+ /**
13
+ * LMDB-based contract class storage for the archiver.
14
+ */
15
+ export class ContractClassStore {
16
+ #contractClasses: AztecAsyncMap<string, Buffer>;
17
+ #bytecodeCommitments: AztecAsyncMap<string, Buffer>;
18
+
19
+ constructor(private db: AztecAsyncKVStore) {
20
+ this.#contractClasses = db.openMap('archiver_contract_classes');
21
+ this.#bytecodeCommitments = db.openMap('archiver_bytecode_commitments');
22
+ }
23
+
24
+ /**
25
+ * Adds multiple contract classes to the store.
26
+ * @param data - Contract classes (with bytecode commitments) to add.
27
+ * @param blockNumber - L2 block number where the classes were registered.
28
+ * @returns True if every insert succeeded.
29
+ */
30
+ async addContractClasses(data: ContractClassPublicWithCommitment[], blockNumber: number): Promise<boolean> {
31
+ return (await Promise.all(data.map(c => this.addContractClass(c, c.publicBytecodeCommitment, blockNumber)))).every(
32
+ Boolean,
33
+ );
34
+ }
35
+
36
+ /**
37
+ * Removes multiple contract classes from the store, but only if they were registered at or after the given block.
38
+ * @param data - Contract classes to delete.
39
+ * @param blockNumber - Lower bound on the block number at which the classes were registered.
40
+ * @returns True if every delete succeeded.
41
+ */
42
+ async deleteContractClasses(data: ContractClassPublic[], blockNumber: number): Promise<boolean> {
43
+ return (await Promise.all(data.map(c => this.deleteContractClass(c, blockNumber)))).every(Boolean);
44
+ }
45
+
46
+ async addContractClass(
47
+ contractClass: ContractClassPublic,
48
+ bytecodeCommitment: Fr,
49
+ blockNumber: number,
50
+ ): Promise<void> {
51
+ await this.db.transactionAsync(async () => {
52
+ const key = contractClass.id.toString();
53
+ const existing = await this.#contractClasses.getAsync(key);
54
+ if (existing !== undefined) {
55
+ // Protocol contracts are preloaded at block 0, so a later on-chain (re-)publish of a bundled
56
+ // protocol class id is valid and must be a no-op. Keep the existing block-0 entry untouched
57
+ // (do not bump its block number) so it survives reorgs of the publishing block.
58
+ if (isProtocolContractClass(contractClass.id)) {
59
+ return;
60
+ }
61
+ // An L1 reorg can re-present an already-stored checkpoint, replaying this class at the same
62
+ // block; treat that as a no-op. A duplicate at a different block still signals double-processing.
63
+ if (deserializeContractClassPublic(existing).l2BlockNumber === blockNumber) {
64
+ return;
65
+ }
66
+ throw new Error(`Contract class ${key} already exists, cannot add again at block ${blockNumber}`);
67
+ }
68
+ await this.#contractClasses.set(
69
+ key,
70
+ serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
71
+ );
72
+ await this.#bytecodeCommitments.set(key, bytecodeCommitment.toBuffer());
73
+ });
74
+ }
75
+
76
+ async deleteContractClass(contractClass: ContractClassPublic, blockNumber: number): Promise<void> {
77
+ // Protocol contracts are preloaded at block 0 and must never be deleted, even when the block that
78
+ // (re-)published them on-chain is unwound by a reorg.
79
+ if (isProtocolContractClass(contractClass.id)) {
80
+ return;
81
+ }
82
+ const restoredContractClass = await this.#contractClasses.getAsync(contractClass.id.toString());
83
+ if (restoredContractClass && deserializeContractClassPublic(restoredContractClass).l2BlockNumber >= blockNumber) {
84
+ await this.db.transactionAsync(async () => {
85
+ await this.#contractClasses.delete(contractClass.id.toString());
86
+ await this.#bytecodeCommitments.delete(contractClass.id.toString());
87
+ });
88
+ }
89
+ }
90
+
91
+ async getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
92
+ const contractClass = await this.#contractClasses.getAsync(id.toString());
93
+ return contractClass && { ...deserializeContractClassPublic(contractClass), id };
94
+ }
95
+
96
+ async getBytecodeCommitment(id: Fr): Promise<Fr | undefined> {
97
+ const value = await this.#bytecodeCommitments.getAsync(id.toString());
98
+ return value === undefined ? undefined : Fr.fromBuffer(value);
99
+ }
100
+
101
+ async getContractClassIds(): Promise<Fr[]> {
102
+ return (await toArray(this.#contractClasses.keysAsync())).map(key => Fr.fromHexString(key));
103
+ }
104
+ }
105
+
106
+ function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWithBlockNumber, 'id'>): Buffer {
107
+ return serializeToBuffer(
108
+ contractClass.l2BlockNumber,
109
+ numToUInt8(contractClass.version),
110
+ contractClass.artifactHash,
111
+ contractClass.packedBytecode.length,
112
+ contractClass.packedBytecode,
113
+ contractClass.privateFunctionsRoot,
114
+ );
115
+ }
116
+
117
+ function deserializeContractClassPublic(buffer: Buffer): Omit<ContractClassPublicWithBlockNumber, 'id'> {
118
+ const reader = BufferReader.asReader(buffer);
119
+ return {
120
+ l2BlockNumber: reader.readNumber(),
121
+ version: reader.readUInt8() as 1,
122
+ artifactHash: reader.readObject(Fr),
123
+ packedBytecode: reader.readBuffer(),
124
+ privateFunctionsRoot: reader.readObject(Fr),
125
+ };
126
+ }
@@ -1,5 +1,6 @@
1
- import type { Fr } from '@aztec/foundation/fields';
1
+ import type { Fr } from '@aztec/foundation/curves/bn254';
2
2
  import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
3
+ import { isProtocolContract } from '@aztec/protocol-contracts';
3
4
  import type { AztecAddress } from '@aztec/stdlib/aztec-address';
4
5
  import {
5
6
  type ContractInstanceUpdateWithAddress,
@@ -12,7 +13,7 @@ import type { UInt64 } from '@aztec/stdlib/types';
12
13
  type ContractInstanceUpdateKey = [string, string] | [string, string, number];
13
14
 
14
15
  /**
15
- * LMDB implementation of the ArchiverDataStore interface.
16
+ * LMDB-based contract instance storage for the archiver.
16
17
  */
17
18
  export class ContractInstanceStore {
18
19
  #contractInstances: AztecAsyncMap<string, Buffer>;
@@ -25,17 +26,79 @@ export class ContractInstanceStore {
25
26
  this.#contractInstanceUpdates = db.openMap('archiver_contract_instance_updates');
26
27
  }
27
28
 
29
+ /**
30
+ * Adds multiple contract instances to the store.
31
+ * @param data - Contract instances to add.
32
+ * @param blockNumber - L2 block number where the instances were deployed.
33
+ * @returns True if every insert succeeded.
34
+ */
35
+ async addContractInstances(data: ContractInstanceWithAddress[], blockNumber: number): Promise<boolean> {
36
+ return (await Promise.all(data.map(c => this.addContractInstance(c, blockNumber)))).every(Boolean);
37
+ }
38
+
39
+ /**
40
+ * Removes multiple contract instances from the store.
41
+ * @param data - Contract instances to delete.
42
+ * @returns True if every delete succeeded.
43
+ */
44
+ async deleteContractInstances(data: ContractInstanceWithAddress[]): Promise<boolean> {
45
+ return (await Promise.all(data.map(c => this.deleteContractInstance(c)))).every(Boolean);
46
+ }
47
+
48
+ /**
49
+ * Adds multiple contract instance updates to the store.
50
+ * @param data - Contract instance updates to add.
51
+ * @param timestamp - Timestamp at which the updates were scheduled.
52
+ * @returns True if every insert succeeded.
53
+ */
54
+ async addContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], timestamp: UInt64): Promise<boolean> {
55
+ return (
56
+ await Promise.all(data.map((update, logIndex) => this.addContractInstanceUpdate(update, timestamp, logIndex)))
57
+ ).every(Boolean);
58
+ }
59
+
60
+ /**
61
+ * Removes multiple contract instance updates from the store.
62
+ * @param data - Contract instance updates to delete.
63
+ * @param timestamp - Timestamp at which the updates were scheduled.
64
+ * @returns True if every delete succeeded.
65
+ */
66
+ async deleteContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], timestamp: UInt64): Promise<boolean> {
67
+ return (
68
+ await Promise.all(data.map((update, logIndex) => this.deleteContractInstanceUpdate(update, timestamp, logIndex)))
69
+ ).every(Boolean);
70
+ }
71
+
28
72
  addContractInstance(contractInstance: ContractInstanceWithAddress, blockNumber: number): Promise<void> {
29
73
  return this.db.transactionAsync(async () => {
30
- await this.#contractInstances.set(
31
- contractInstance.address.toString(),
32
- new SerializableContractInstance(contractInstance).toBuffer(),
33
- );
34
- await this.#contractInstancePublishedAt.set(contractInstance.address.toString(), blockNumber);
74
+ const key = contractInstance.address.toString();
75
+ if (await this.#contractInstances.hasAsync(key)) {
76
+ // Protocol contracts are preloaded at block 0, so a later on-chain (re-)publish of a bundled
77
+ // protocol instance is valid and must be a no-op. Keep the existing block-0 entry untouched.
78
+ if (isProtocolContract(contractInstance.address)) {
79
+ return;
80
+ }
81
+ const existingBlockNumber = await this.#contractInstancePublishedAt.getAsync(key);
82
+ // An L1 reorg can re-present an already-stored checkpoint, replaying this instance at the same
83
+ // block; treat that as a no-op. A duplicate at a different block still signals double-processing.
84
+ if (existingBlockNumber === blockNumber) {
85
+ return;
86
+ }
87
+ throw new Error(
88
+ `Contract instance at ${key} already exists (deployed at block ${existingBlockNumber}), cannot add again at block ${blockNumber}`,
89
+ );
90
+ }
91
+ await this.#contractInstances.set(key, new SerializableContractInstance(contractInstance).toBuffer());
92
+ await this.#contractInstancePublishedAt.set(key, blockNumber);
35
93
  });
36
94
  }
37
95
 
38
96
  deleteContractInstance(contractInstance: ContractInstanceWithAddress): Promise<void> {
97
+ // Protocol contracts are preloaded at block 0 and must never be deleted, even when the block that
98
+ // (re-)published them on-chain is unwound by a reorg.
99
+ if (isProtocolContract(contractInstance.address)) {
100
+ return Promise.resolve();
101
+ }
39
102
  return this.db.transactionAsync(async () => {
40
103
  await this.#contractInstances.delete(contractInstance.address.toString());
41
104
  await this.#contractInstancePublishedAt.delete(contractInstance.address.toString());
@@ -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
+ }