@aztec/archiver 0.0.1-commit.03f7ef2 → 0.0.1-commit.04d373f

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 (235) hide show
  1. package/README.md +164 -22
  2. package/dest/archiver.d.ts +158 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +881 -0
  5. package/dest/config.d.ts +33 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/config.js +79 -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 +112 -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/{archiver/l1 → l1}/bin/retrieve-calldata.d.ts +1 -1
  21. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  22. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.js +35 -32
  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/{archiver/l1 → l1}/data_retrieval.js +65 -89
  29. package/dest/{archiver/l1 → l1}/debug_tx.d.ts +1 -1
  30. package/dest/l1/debug_tx.d.ts.map +1 -0
  31. package/dest/{archiver/l1 → l1}/spire_proposer.d.ts +5 -5
  32. package/dest/l1/spire_proposer.d.ts.map +1 -0
  33. package/dest/{archiver/l1 → l1}/spire_proposer.js +9 -17
  34. package/dest/l1/trace_tx.d.ts +43 -0
  35. package/dest/l1/trace_tx.d.ts.map +1 -0
  36. package/dest/l1/types.d.ts +12 -0
  37. package/dest/l1/types.d.ts.map +1 -0
  38. package/dest/l1/validate_historical_logs.d.ts +23 -0
  39. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  40. package/dest/l1/validate_historical_logs.js +108 -0
  41. package/dest/{archiver/l1 → l1}/validate_trace.d.ts +6 -3
  42. package/dest/l1/validate_trace.d.ts.map +1 -0
  43. package/dest/{archiver/l1 → l1}/validate_trace.js +14 -10
  44. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  45. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  46. package/dest/modules/contract_data_source_adapter.js +40 -0
  47. package/dest/modules/data_source_base.d.ts +113 -0
  48. package/dest/modules/data_source_base.d.ts.map +1 -0
  49. package/dest/modules/data_source_base.js +351 -0
  50. package/dest/modules/data_store_updater.d.ts +105 -0
  51. package/dest/modules/data_store_updater.d.ts.map +1 -0
  52. package/dest/modules/data_store_updater.js +392 -0
  53. package/dest/modules/instrumentation.d.ts +55 -0
  54. package/dest/modules/instrumentation.d.ts.map +1 -0
  55. package/dest/modules/instrumentation.js +145 -0
  56. package/dest/modules/l1_synchronizer.d.ts +77 -0
  57. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  58. package/dest/modules/l1_synchronizer.js +1344 -0
  59. package/dest/modules/validation.d.ts +18 -0
  60. package/dest/modules/validation.d.ts.map +1 -0
  61. package/dest/{archiver → modules}/validation.js +13 -7
  62. package/dest/store/block_store.d.ts +300 -0
  63. package/dest/store/block_store.d.ts.map +1 -0
  64. package/dest/store/block_store.js +1219 -0
  65. package/dest/store/contract_class_store.d.ts +31 -0
  66. package/dest/store/contract_class_store.d.ts.map +1 -0
  67. package/dest/store/contract_class_store.js +80 -0
  68. package/dest/store/contract_instance_store.d.ts +51 -0
  69. package/dest/store/contract_instance_store.d.ts.map +1 -0
  70. package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +38 -3
  71. package/dest/store/data_stores.d.ts +68 -0
  72. package/dest/store/data_stores.d.ts.map +1 -0
  73. package/dest/store/data_stores.js +54 -0
  74. package/dest/store/function_names_cache.d.ts +17 -0
  75. package/dest/store/function_names_cache.d.ts.map +1 -0
  76. package/dest/store/function_names_cache.js +30 -0
  77. package/dest/store/l2_tips_cache.d.ts +25 -0
  78. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  79. package/dest/store/l2_tips_cache.js +26 -0
  80. package/dest/store/log_store.d.ts +59 -0
  81. package/dest/store/log_store.d.ts.map +1 -0
  82. package/dest/store/log_store.js +310 -0
  83. package/dest/store/log_store_codec.d.ts +70 -0
  84. package/dest/store/log_store_codec.d.ts.map +1 -0
  85. package/dest/store/log_store_codec.js +101 -0
  86. package/dest/store/message_store.d.ts +50 -0
  87. package/dest/store/message_store.d.ts.map +1 -0
  88. package/dest/{archiver/kv_archiver_store → store}/message_store.js +51 -9
  89. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  90. package/dest/structs/data_retrieval.d.ts.map +1 -0
  91. package/dest/structs/inbox_message.d.ts +15 -0
  92. package/dest/structs/inbox_message.d.ts.map +1 -0
  93. package/dest/{archiver/structs → structs}/published.d.ts +1 -1
  94. package/dest/structs/published.d.ts.map +1 -0
  95. package/dest/test/fake_l1_state.d.ts +214 -0
  96. package/dest/test/fake_l1_state.d.ts.map +1 -0
  97. package/dest/test/fake_l1_state.js +517 -0
  98. package/dest/test/index.d.ts +2 -1
  99. package/dest/test/index.d.ts.map +1 -1
  100. package/dest/test/index.js +4 -1
  101. package/dest/test/mock_archiver.d.ts +2 -2
  102. package/dest/test/mock_archiver.d.ts.map +1 -1
  103. package/dest/test/mock_archiver.js +3 -3
  104. package/dest/test/mock_l1_to_l2_message_source.d.ts +2 -2
  105. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  106. package/dest/test/mock_l1_to_l2_message_source.js +14 -4
  107. package/dest/test/mock_l2_block_source.d.ts +68 -40
  108. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  109. package/dest/test/mock_l2_block_source.js +356 -131
  110. package/dest/test/mock_structs.d.ts +81 -3
  111. package/dest/test/mock_structs.d.ts.map +1 -1
  112. package/dest/test/mock_structs.js +152 -7
  113. package/dest/test/noop_l1_archiver.d.ts +29 -0
  114. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  115. package/dest/test/noop_l1_archiver.js +85 -0
  116. package/package.json +17 -18
  117. package/src/archiver.ts +681 -0
  118. package/src/{archiver/config.ts → config.ts} +45 -14
  119. package/src/errors.ts +203 -0
  120. package/src/factory.ts +175 -22
  121. package/src/index.ts +27 -3
  122. package/src/interfaces.ts +9 -0
  123. package/src/l1/README.md +55 -0
  124. package/src/{archiver/l1 → l1}/bin/retrieve-calldata.ts +45 -33
  125. package/src/l1/calldata_retriever.ts +522 -0
  126. package/src/{archiver/l1 → l1}/data_retrieval.ts +106 -134
  127. package/src/{archiver/l1 → l1}/spire_proposer.ts +7 -15
  128. package/src/l1/validate_historical_logs.ts +140 -0
  129. package/src/{archiver/l1 → l1}/validate_trace.ts +25 -7
  130. package/src/modules/contract_data_source_adapter.ts +55 -0
  131. package/src/modules/data_source_base.ts +493 -0
  132. package/src/modules/data_store_updater.ts +518 -0
  133. package/src/modules/instrumentation.ts +204 -0
  134. package/src/modules/l1_synchronizer.ts +1257 -0
  135. package/src/{archiver → modules}/validation.ts +21 -15
  136. package/src/store/block_store.ts +1590 -0
  137. package/src/store/contract_class_store.ts +108 -0
  138. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +52 -6
  139. package/src/store/data_stores.ts +104 -0
  140. package/src/store/function_names_cache.ts +37 -0
  141. package/src/store/l2_tips_cache.ts +35 -0
  142. package/src/store/log_store.ts +379 -0
  143. package/src/store/log_store_codec.ts +132 -0
  144. package/src/{archiver/kv_archiver_store → store}/message_store.ts +60 -10
  145. package/src/{archiver/structs → structs}/inbox_message.ts +1 -1
  146. package/src/test/fake_l1_state.ts +770 -0
  147. package/src/test/index.ts +4 -0
  148. package/src/test/mock_archiver.ts +4 -3
  149. package/src/test/mock_l1_to_l2_message_source.ts +11 -4
  150. package/src/test/mock_l2_block_source.ts +434 -144
  151. package/src/test/mock_structs.ts +283 -8
  152. package/src/test/noop_l1_archiver.ts +139 -0
  153. package/dest/archiver/archiver.d.ts +0 -304
  154. package/dest/archiver/archiver.d.ts.map +0 -1
  155. package/dest/archiver/archiver.js +0 -1645
  156. package/dest/archiver/archiver_store.d.ts +0 -308
  157. package/dest/archiver/archiver_store.d.ts.map +0 -1
  158. package/dest/archiver/archiver_store.js +0 -4
  159. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  160. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  161. package/dest/archiver/archiver_store_test_suite.js +0 -2790
  162. package/dest/archiver/config.d.ts +0 -22
  163. package/dest/archiver/config.d.ts.map +0 -1
  164. package/dest/archiver/config.js +0 -62
  165. package/dest/archiver/errors.d.ts +0 -36
  166. package/dest/archiver/errors.d.ts.map +0 -1
  167. package/dest/archiver/errors.js +0 -54
  168. package/dest/archiver/index.d.ts +0 -7
  169. package/dest/archiver/index.d.ts.map +0 -1
  170. package/dest/archiver/index.js +0 -4
  171. package/dest/archiver/instrumentation.d.ts +0 -37
  172. package/dest/archiver/instrumentation.d.ts.map +0 -1
  173. package/dest/archiver/instrumentation.js +0 -151
  174. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -157
  175. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  176. package/dest/archiver/kv_archiver_store/block_store.js +0 -607
  177. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  178. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  179. package/dest/archiver/kv_archiver_store/contract_class_store.js +0 -120
  180. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  181. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  182. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -158
  183. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  184. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -313
  185. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -45
  186. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  187. package/dest/archiver/kv_archiver_store/log_store.js +0 -404
  188. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -40
  189. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  190. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +0 -1
  191. package/dest/archiver/l1/calldata_retriever.d.ts +0 -112
  192. package/dest/archiver/l1/calldata_retriever.d.ts.map +0 -1
  193. package/dest/archiver/l1/calldata_retriever.js +0 -471
  194. package/dest/archiver/l1/data_retrieval.d.ts +0 -90
  195. package/dest/archiver/l1/data_retrieval.d.ts.map +0 -1
  196. package/dest/archiver/l1/debug_tx.d.ts.map +0 -1
  197. package/dest/archiver/l1/spire_proposer.d.ts.map +0 -1
  198. package/dest/archiver/l1/trace_tx.d.ts +0 -97
  199. package/dest/archiver/l1/trace_tx.d.ts.map +0 -1
  200. package/dest/archiver/l1/types.d.ts +0 -12
  201. package/dest/archiver/l1/types.d.ts.map +0 -1
  202. package/dest/archiver/l1/validate_trace.d.ts.map +0 -1
  203. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  204. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  205. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  206. package/dest/archiver/structs/published.d.ts.map +0 -1
  207. package/dest/archiver/validation.d.ts +0 -17
  208. package/dest/archiver/validation.d.ts.map +0 -1
  209. package/dest/rpc/index.d.ts +0 -9
  210. package/dest/rpc/index.d.ts.map +0 -1
  211. package/dest/rpc/index.js +0 -15
  212. package/src/archiver/archiver.ts +0 -2157
  213. package/src/archiver/archiver_store.ts +0 -372
  214. package/src/archiver/archiver_store_test_suite.ts +0 -2863
  215. package/src/archiver/errors.ts +0 -90
  216. package/src/archiver/index.ts +0 -6
  217. package/src/archiver/instrumentation.ts +0 -201
  218. package/src/archiver/kv_archiver_store/block_store.ts +0 -822
  219. package/src/archiver/kv_archiver_store/contract_class_store.ts +0 -176
  220. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -438
  221. package/src/archiver/kv_archiver_store/log_store.ts +0 -522
  222. package/src/archiver/l1/README.md +0 -98
  223. package/src/archiver/l1/calldata_retriever.ts +0 -641
  224. package/src/rpc/index.ts +0 -16
  225. /package/dest/{archiver/l1 → l1}/debug_tx.js +0 -0
  226. /package/dest/{archiver/l1 → l1}/trace_tx.js +0 -0
  227. /package/dest/{archiver/l1 → l1}/types.js +0 -0
  228. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  229. /package/dest/{archiver/structs → structs}/inbox_message.js +0 -0
  230. /package/dest/{archiver/structs → structs}/published.js +0 -0
  231. /package/src/{archiver/l1 → l1}/debug_tx.ts +0 -0
  232. /package/src/{archiver/l1 → l1}/trace_tx.ts +0 -0
  233. /package/src/{archiver/l1 → l1}/types.ts +0 -0
  234. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
  235. /package/src/{archiver/structs → structs}/published.ts +0 -0
@@ -1,176 +0,0 @@
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 { FunctionSelector } from '@aztec/stdlib/abi';
6
- import type {
7
- ContractClassPublic,
8
- ContractClassPublicWithBlockNumber,
9
- ExecutablePrivateFunctionWithMembershipProof,
10
- UtilityFunctionWithMembershipProof,
11
- } from '@aztec/stdlib/contract';
12
- import { Vector } from '@aztec/stdlib/types';
13
-
14
- /**
15
- * LMDB implementation of the ArchiverDataStore interface.
16
- */
17
- export class ContractClassStore {
18
- #contractClasses: AztecAsyncMap<string, Buffer>;
19
- #bytecodeCommitments: AztecAsyncMap<string, Buffer>;
20
-
21
- constructor(private db: AztecAsyncKVStore) {
22
- this.#contractClasses = db.openMap('archiver_contract_classes');
23
- this.#bytecodeCommitments = db.openMap('archiver_bytecode_commitments');
24
- }
25
-
26
- async addContractClass(
27
- contractClass: ContractClassPublic,
28
- bytecodeCommitment: Fr,
29
- blockNumber: number,
30
- ): Promise<void> {
31
- await this.#contractClasses.setIfNotExists(
32
- contractClass.id.toString(),
33
- serializeContractClassPublic({ ...contractClass, l2BlockNumber: blockNumber }),
34
- );
35
- await this.#bytecodeCommitments.setIfNotExists(contractClass.id.toString(), bytecodeCommitment.toBuffer());
36
- }
37
-
38
- async deleteContractClasses(contractClass: ContractClassPublic, blockNumber: number): Promise<void> {
39
- const restoredContractClass = await this.#contractClasses.getAsync(contractClass.id.toString());
40
- if (restoredContractClass && deserializeContractClassPublic(restoredContractClass).l2BlockNumber >= blockNumber) {
41
- await this.#contractClasses.delete(contractClass.id.toString());
42
- await this.#bytecodeCommitments.delete(contractClass.id.toString());
43
- }
44
- }
45
-
46
- async getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
47
- const contractClass = await this.#contractClasses.getAsync(id.toString());
48
- return contractClass && { ...deserializeContractClassPublic(contractClass), id };
49
- }
50
-
51
- async getBytecodeCommitment(id: Fr): Promise<Fr | undefined> {
52
- const value = await this.#bytecodeCommitments.getAsync(id.toString());
53
- return value === undefined ? undefined : Fr.fromBuffer(value);
54
- }
55
-
56
- async getContractClassIds(): Promise<Fr[]> {
57
- return (await toArray(this.#contractClasses.keysAsync())).map(key => Fr.fromHexString(key));
58
- }
59
-
60
- async addFunctions(
61
- contractClassId: Fr,
62
- newPrivateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
63
- newUtilityFunctions: UtilityFunctionWithMembershipProof[],
64
- ): Promise<boolean> {
65
- await this.db.transactionAsync(async () => {
66
- const existingClassBuffer = await this.#contractClasses.getAsync(contractClassId.toString());
67
- if (!existingClassBuffer) {
68
- throw new Error(`Unknown contract class ${contractClassId} when adding private functions to store`);
69
- }
70
-
71
- const existingClass = deserializeContractClassPublic(existingClassBuffer);
72
- const { privateFunctions: existingPrivateFns, utilityFunctions: existingUtilityFns } = existingClass;
73
-
74
- const updatedClass: Omit<ContractClassPublicWithBlockNumber, 'id'> = {
75
- ...existingClass,
76
- privateFunctions: [
77
- ...existingPrivateFns,
78
- ...newPrivateFunctions.filter(newFn => !existingPrivateFns.some(f => f.selector.equals(newFn.selector))),
79
- ],
80
- utilityFunctions: [
81
- ...existingUtilityFns,
82
- ...newUtilityFunctions.filter(newFn => !existingUtilityFns.some(f => f.selector.equals(newFn.selector))),
83
- ],
84
- };
85
- await this.#contractClasses.set(contractClassId.toString(), serializeContractClassPublic(updatedClass));
86
- });
87
-
88
- return true;
89
- }
90
- }
91
-
92
- function serializeContractClassPublic(contractClass: Omit<ContractClassPublicWithBlockNumber, 'id'>): Buffer {
93
- return serializeToBuffer(
94
- contractClass.l2BlockNumber,
95
- numToUInt8(contractClass.version),
96
- contractClass.artifactHash,
97
- contractClass.privateFunctions.length,
98
- contractClass.privateFunctions.map(serializePrivateFunction),
99
- contractClass.utilityFunctions.length,
100
- contractClass.utilityFunctions.map(serializeUtilityFunction),
101
- contractClass.packedBytecode.length,
102
- contractClass.packedBytecode,
103
- contractClass.privateFunctionsRoot,
104
- );
105
- }
106
-
107
- function serializePrivateFunction(fn: ExecutablePrivateFunctionWithMembershipProof): Buffer {
108
- return serializeToBuffer(
109
- fn.selector,
110
- fn.vkHash,
111
- fn.bytecode.length,
112
- fn.bytecode,
113
- fn.functionMetadataHash,
114
- fn.artifactMetadataHash,
115
- fn.utilityFunctionsTreeRoot,
116
- new Vector(fn.privateFunctionTreeSiblingPath),
117
- fn.privateFunctionTreeLeafIndex,
118
- new Vector(fn.artifactTreeSiblingPath),
119
- fn.artifactTreeLeafIndex,
120
- );
121
- }
122
-
123
- function serializeUtilityFunction(fn: UtilityFunctionWithMembershipProof): Buffer {
124
- return serializeToBuffer(
125
- fn.selector,
126
- fn.bytecode.length,
127
- fn.bytecode,
128
- fn.functionMetadataHash,
129
- fn.artifactMetadataHash,
130
- fn.privateFunctionsArtifactTreeRoot,
131
- new Vector(fn.artifactTreeSiblingPath),
132
- fn.artifactTreeLeafIndex,
133
- );
134
- }
135
-
136
- function deserializeContractClassPublic(buffer: Buffer): Omit<ContractClassPublicWithBlockNumber, 'id'> {
137
- const reader = BufferReader.asReader(buffer);
138
- return {
139
- l2BlockNumber: reader.readNumber(),
140
- version: reader.readUInt8() as 1,
141
- artifactHash: reader.readObject(Fr),
142
- privateFunctions: reader.readVector({ fromBuffer: deserializePrivateFunction }),
143
- utilityFunctions: reader.readVector({ fromBuffer: deserializeUtilityFunction }),
144
- packedBytecode: reader.readBuffer(),
145
- privateFunctionsRoot: reader.readObject(Fr),
146
- };
147
- }
148
-
149
- function deserializePrivateFunction(buffer: Buffer | BufferReader): ExecutablePrivateFunctionWithMembershipProof {
150
- const reader = BufferReader.asReader(buffer);
151
- return {
152
- selector: reader.readObject(FunctionSelector),
153
- vkHash: reader.readObject(Fr),
154
- bytecode: reader.readBuffer(),
155
- functionMetadataHash: reader.readObject(Fr),
156
- artifactMetadataHash: reader.readObject(Fr),
157
- utilityFunctionsTreeRoot: reader.readObject(Fr),
158
- privateFunctionTreeSiblingPath: reader.readVector(Fr),
159
- privateFunctionTreeLeafIndex: reader.readNumber(),
160
- artifactTreeSiblingPath: reader.readVector(Fr),
161
- artifactTreeLeafIndex: reader.readNumber(),
162
- };
163
- }
164
-
165
- function deserializeUtilityFunction(buffer: Buffer | BufferReader): UtilityFunctionWithMembershipProof {
166
- const reader = BufferReader.asReader(buffer);
167
- return {
168
- selector: reader.readObject(FunctionSelector),
169
- bytecode: reader.readBuffer(),
170
- functionMetadataHash: reader.readObject(Fr),
171
- artifactMetadataHash: reader.readObject(Fr),
172
- privateFunctionsArtifactTreeRoot: reader.readObject(Fr),
173
- artifactTreeSiblingPath: reader.readVector(Fr),
174
- artifactTreeLeafIndex: reader.readNumber(),
175
- };
176
- }
@@ -1,438 +0,0 @@
1
- import type { L1BlockId } from '@aztec/ethereum/l1-types';
2
- import type { BlockNumber, CheckpointNumber } from '@aztec/foundation/branded-types';
3
- import type { Fr } from '@aztec/foundation/curves/bn254';
4
- import { toArray } from '@aztec/foundation/iterable';
5
- import { createLogger } from '@aztec/foundation/log';
6
- import type { AztecAsyncKVStore, CustomRange, StoreSize } from '@aztec/kv-store';
7
- import { FunctionSelector } from '@aztec/stdlib/abi';
8
- import type { AztecAddress } from '@aztec/stdlib/aztec-address';
9
- import { CheckpointedL2Block, L2BlockHash, L2BlockNew, type ValidateBlockResult } from '@aztec/stdlib/block';
10
- import type { PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
11
- import type {
12
- ContractClassPublic,
13
- ContractDataSource,
14
- ContractInstanceUpdateWithAddress,
15
- ContractInstanceWithAddress,
16
- ExecutablePrivateFunctionWithMembershipProof,
17
- UtilityFunctionWithMembershipProof,
18
- } from '@aztec/stdlib/contract';
19
- import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
20
- import type { LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
21
- import type { BlockHeader, TxHash, TxReceipt } from '@aztec/stdlib/tx';
22
- import type { UInt64 } from '@aztec/stdlib/types';
23
-
24
- import { join } from 'path';
25
-
26
- import type { ArchiverDataStore, ArchiverL1SynchPoint } from '../archiver_store.js';
27
- import type { InboxMessage } from '../structs/inbox_message.js';
28
- import { BlockStore, type CheckpointData } from './block_store.js';
29
- import { ContractClassStore } from './contract_class_store.js';
30
- import { ContractInstanceStore } from './contract_instance_store.js';
31
- import { LogStore } from './log_store.js';
32
- import { MessageStore } from './message_store.js';
33
-
34
- export const ARCHIVER_DB_VERSION = 5;
35
- export const MAX_FUNCTION_SIGNATURES = 1000;
36
- export const MAX_FUNCTION_NAME_LEN = 256;
37
-
38
- /**
39
- * LMDB implementation of the ArchiverDataStore interface.
40
- */
41
- export class KVArchiverDataStore implements ArchiverDataStore, ContractDataSource {
42
- public static readonly SCHEMA_VERSION = ARCHIVER_DB_VERSION;
43
-
44
- #blockStore: BlockStore;
45
- #logStore: LogStore;
46
- #messageStore: MessageStore;
47
- #contractClassStore: ContractClassStore;
48
- #contractInstanceStore: ContractInstanceStore;
49
-
50
- private functionNames = new Map<string, string>();
51
-
52
- #log = createLogger('archiver:data-store');
53
-
54
- constructor(
55
- private db: AztecAsyncKVStore,
56
- logsMaxPageSize: number = 1000,
57
- ) {
58
- this.#blockStore = new BlockStore(db);
59
- this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize);
60
- this.#messageStore = new MessageStore(db);
61
- this.#contractClassStore = new ContractClassStore(db);
62
- this.#contractInstanceStore = new ContractInstanceStore(db);
63
- }
64
-
65
- public transactionAsync<T>(callback: () => Promise<T>): Promise<T> {
66
- return this.db.transactionAsync(callback);
67
- }
68
-
69
- public getBlockNumber(): Promise<BlockNumber> {
70
- return this.#blockStore.getLatestL2BlockNumber();
71
- }
72
-
73
- public async getContract(
74
- address: AztecAddress,
75
- maybeTimestamp?: UInt64,
76
- ): Promise<ContractInstanceWithAddress | undefined> {
77
- const [header] = await this.getBlockHeaders(await this.getBlockNumber(), 1);
78
- const timestamp = maybeTimestamp ?? header!.globalVariables.timestamp;
79
- return this.getContractInstance(address, timestamp);
80
- }
81
-
82
- public async backupTo(path: string, compress = true): Promise<string> {
83
- await this.db.backupTo(path, compress);
84
- return join(path, 'data.mdb');
85
- }
86
-
87
- public close() {
88
- return this.db.close();
89
- }
90
-
91
- getDebugFunctionName(_address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
92
- return Promise.resolve(this.functionNames.get(selector.toString()));
93
- }
94
-
95
- async registerContractFunctionSignatures(signatures: string[]): Promise<void> {
96
- for (const sig of signatures) {
97
- if (this.functionNames.size > MAX_FUNCTION_SIGNATURES) {
98
- return;
99
- }
100
- try {
101
- const selector = await FunctionSelector.fromSignature(sig);
102
- this.functionNames.set(selector.toString(), sig.slice(0, sig.indexOf('(')).slice(0, MAX_FUNCTION_NAME_LEN));
103
- } catch {
104
- this.#log.warn(`Failed to parse signature: ${sig}. Ignoring`);
105
- }
106
- }
107
- }
108
-
109
- getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
110
- return this.#contractClassStore.getContractClass(id);
111
- }
112
-
113
- getContractClassIds(): Promise<Fr[]> {
114
- return this.#contractClassStore.getContractClassIds();
115
- }
116
-
117
- getContractInstance(address: AztecAddress, timestamp: UInt64): Promise<ContractInstanceWithAddress | undefined> {
118
- return this.#contractInstanceStore.getContractInstance(address, timestamp);
119
- }
120
-
121
- getContractInstanceDeploymentBlockNumber(address: AztecAddress): Promise<number | undefined> {
122
- return this.#contractInstanceStore.getContractInstanceDeploymentBlockNumber(address);
123
- }
124
-
125
- async addContractClasses(
126
- data: ContractClassPublic[],
127
- bytecodeCommitments: Fr[],
128
- blockNumber: BlockNumber,
129
- ): Promise<boolean> {
130
- return (
131
- await Promise.all(
132
- data.map((c, i) => this.#contractClassStore.addContractClass(c, bytecodeCommitments[i], blockNumber)),
133
- )
134
- ).every(Boolean);
135
- }
136
-
137
- async deleteContractClasses(data: ContractClassPublic[], blockNumber: BlockNumber): Promise<boolean> {
138
- return (await Promise.all(data.map(c => this.#contractClassStore.deleteContractClasses(c, blockNumber)))).every(
139
- Boolean,
140
- );
141
- }
142
-
143
- getBytecodeCommitment(contractClassId: Fr): Promise<Fr | undefined> {
144
- return this.#contractClassStore.getBytecodeCommitment(contractClassId);
145
- }
146
-
147
- addFunctions(
148
- contractClassId: Fr,
149
- privateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
150
- utilityFunctions: UtilityFunctionWithMembershipProof[],
151
- ): Promise<boolean> {
152
- return this.#contractClassStore.addFunctions(contractClassId, privateFunctions, utilityFunctions);
153
- }
154
-
155
- async addContractInstances(data: ContractInstanceWithAddress[], blockNumber: BlockNumber): Promise<boolean> {
156
- return (await Promise.all(data.map(c => this.#contractInstanceStore.addContractInstance(c, blockNumber)))).every(
157
- Boolean,
158
- );
159
- }
160
-
161
- async deleteContractInstances(data: ContractInstanceWithAddress[], _blockNumber: BlockNumber): Promise<boolean> {
162
- return (await Promise.all(data.map(c => this.#contractInstanceStore.deleteContractInstance(c)))).every(Boolean);
163
- }
164
-
165
- async addContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], timestamp: UInt64): Promise<boolean> {
166
- return (
167
- await Promise.all(
168
- data.map((update, logIndex) =>
169
- this.#contractInstanceStore.addContractInstanceUpdate(update, timestamp, logIndex),
170
- ),
171
- )
172
- ).every(Boolean);
173
- }
174
- async deleteContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], timestamp: UInt64): Promise<boolean> {
175
- return (
176
- await Promise.all(
177
- data.map((update, logIndex) =>
178
- this.#contractInstanceStore.deleteContractInstanceUpdate(update, timestamp, logIndex),
179
- ),
180
- )
181
- ).every(Boolean);
182
- }
183
-
184
- /**
185
- * Append new blocks to the store's list.
186
- * @param blocks - The L2 blocks to be added to the store and the last processed L1 block.
187
- * @returns True if the operation is successful.
188
- */
189
- addBlocks(blocks: L2BlockNew[], opts: { force?: boolean; checkpointNumber?: number } = {}): Promise<boolean> {
190
- return this.#blockStore.addBlocks(blocks, opts);
191
- }
192
-
193
- getRangeOfCheckpoints(from: CheckpointNumber, limit: number): Promise<CheckpointData[]> {
194
- return this.#blockStore.getRangeOfCheckpoints(from, limit);
195
- }
196
- getLatestBlockNumber(): Promise<BlockNumber> {
197
- return this.#blockStore.getLatestBlockNumber();
198
- }
199
-
200
- /**
201
- * Unwinds checkpoints from the database
202
- * @param from - The tip of the chain, passed for verification purposes,
203
- * ensuring that we don't end up deleting something we did not intend
204
- * @param checkpointsToUnwind - The number of checkpoints we are to unwind
205
- * @returns True if the operation is successful
206
- */
207
- unwindCheckpoints(from: CheckpointNumber, checkpointsToUnwind: number): Promise<boolean> {
208
- return this.#blockStore.unwindCheckpoints(from, checkpointsToUnwind);
209
- }
210
-
211
- addCheckpoints(checkpoints: PublishedCheckpoint[]): Promise<boolean> {
212
- return this.#blockStore.addCheckpoints(checkpoints);
213
- }
214
-
215
- getCheckpointedBlock(number: BlockNumber): Promise<CheckpointedL2Block | undefined> {
216
- return this.#blockStore.getCheckpointedBlock(number);
217
- }
218
- getCheckpointedBlockByHash(blockHash: Fr): Promise<CheckpointedL2Block | undefined> {
219
- return this.#blockStore.getCheckpointedBlockByHash(blockHash);
220
- }
221
- getCheckpointedBlockByArchive(archive: Fr): Promise<CheckpointedL2Block | undefined> {
222
- return this.#blockStore.getCheckpointedBlockByArchive(archive);
223
- }
224
- getBlock(number: BlockNumber): Promise<L2BlockNew | undefined> {
225
- return this.#blockStore.getBlock(number);
226
- }
227
- getBlockByHash(blockHash: Fr): Promise<L2BlockNew | undefined> {
228
- return this.#blockStore.getBlockByHash(L2BlockHash.fromField(blockHash));
229
- }
230
- getBlockByArchive(archive: Fr): Promise<L2BlockNew | undefined> {
231
- return this.#blockStore.getBlockByArchive(archive);
232
- }
233
- getBlocks(from: BlockNumber, limit: BlockNumber): Promise<L2BlockNew[]> {
234
- return toArray(this.#blockStore.getBlocks(from, limit));
235
- }
236
-
237
- /**
238
- * Gets up to `limit` amount of L2 blocks headers starting from `from`.
239
- *
240
- * @param start - Number of the first block to return (inclusive).
241
- * @param limit - The number of blocks to return.
242
- * @returns The requested L2 blocks
243
- */
244
- getBlockHeaders(start: BlockNumber, limit: number): Promise<BlockHeader[]> {
245
- return toArray(this.#blockStore.getBlockHeaders(start, limit));
246
- }
247
-
248
- getBlockHeaderByHash(blockHash: Fr): Promise<BlockHeader | undefined> {
249
- return this.#blockStore.getBlockHeaderByHash(L2BlockHash.fromField(blockHash));
250
- }
251
-
252
- getBlockHeaderByArchive(archive: Fr): Promise<BlockHeader | undefined> {
253
- return this.#blockStore.getBlockHeaderByArchive(archive);
254
- }
255
-
256
- /**
257
- * Gets a tx effect.
258
- * @param txHash - The hash of the tx corresponding to the tx effect.
259
- * @returns The requested tx effect with block info (or undefined if not found).
260
- */
261
- getTxEffect(txHash: TxHash) {
262
- return this.#blockStore.getTxEffect(txHash);
263
- }
264
-
265
- /**
266
- * Gets a receipt of a settled tx.
267
- * @param txHash - The hash of a tx we try to get the receipt for.
268
- * @returns The requested tx receipt (or undefined if not found).
269
- */
270
- getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
271
- return this.#blockStore.getSettledTxReceipt(txHash);
272
- }
273
-
274
- /**
275
- * Append new logs to the store's list.
276
- * @param blocks - The blocks for which to add the logs.
277
- * @returns True if the operation is successful.
278
- */
279
- addLogs(blocks: L2BlockNew[]): Promise<boolean> {
280
- return this.#logStore.addLogs(blocks);
281
- }
282
-
283
- deleteLogs(blocks: L2BlockNew[]): Promise<boolean> {
284
- return this.#logStore.deleteLogs(blocks);
285
- }
286
-
287
- getTotalL1ToL2MessageCount(): Promise<bigint> {
288
- return this.#messageStore.getTotalL1ToL2MessageCount();
289
- }
290
-
291
- getLastL1ToL2Message(): Promise<InboxMessage | undefined> {
292
- return this.#messageStore.getLastMessage();
293
- }
294
-
295
- /**
296
- * Append L1 to L2 messages to the store.
297
- * @param messages - The L1 to L2 messages to be added to the store.
298
- */
299
- addL1ToL2Messages(messages: InboxMessage[]): Promise<void> {
300
- return this.#messageStore.addL1ToL2Messages(messages);
301
- }
302
-
303
- /**
304
- * Gets the L1 to L2 message index in the L1 to L2 message tree.
305
- * @param l1ToL2Message - The L1 to L2 message.
306
- * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
307
- */
308
- getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
309
- return this.#messageStore.getL1ToL2MessageIndex(l1ToL2Message);
310
- }
311
-
312
- /**
313
- * Gets L1 to L2 message (to be) included in a given checkpoint.
314
- * @param checkpointNumber - Checkpoint number to get messages for.
315
- * @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
316
- */
317
- getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
318
- return this.#messageStore.getL1ToL2Messages(checkpointNumber);
319
- }
320
-
321
- getPrivateLogsByTags(tags: SiloedTag[]): Promise<TxScopedL2Log[][]> {
322
- try {
323
- return this.#logStore.getPrivateLogsByTags(tags);
324
- } catch (err) {
325
- return Promise.reject(err);
326
- }
327
- }
328
-
329
- getPublicLogsByTagsFromContract(contractAddress: AztecAddress, tags: Tag[]): Promise<TxScopedL2Log[][]> {
330
- try {
331
- return this.#logStore.getPublicLogsByTagsFromContract(contractAddress, tags);
332
- } catch (err) {
333
- return Promise.reject(err);
334
- }
335
- }
336
-
337
- /**
338
- * Gets public logs based on the provided filter.
339
- * @param filter - The filter to apply to the logs.
340
- * @returns The requested logs.
341
- */
342
- getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
343
- try {
344
- return this.#logStore.getPublicLogs(filter);
345
- } catch (err) {
346
- return Promise.reject(err);
347
- }
348
- }
349
-
350
- /**
351
- * Gets contract class logs based on the provided filter.
352
- * @param filter - The filter to apply to the logs.
353
- * @returns The requested logs.
354
- */
355
- getContractClassLogs(filter: LogFilter): Promise<GetContractClassLogsResponse> {
356
- try {
357
- return this.#logStore.getContractClassLogs(filter);
358
- } catch (err) {
359
- return Promise.reject(err);
360
- }
361
- }
362
-
363
- getProvenCheckpointNumber(): Promise<CheckpointNumber> {
364
- return this.#blockStore.getProvenCheckpointNumber();
365
- }
366
-
367
- async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber) {
368
- await this.#blockStore.setProvenCheckpointNumber(checkpointNumber);
369
- }
370
-
371
- async setBlockSynchedL1BlockNumber(l1BlockNumber: bigint) {
372
- await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
373
- }
374
-
375
- async setMessageSynchedL1Block(l1Block: L1BlockId) {
376
- await this.#messageStore.setSynchedL1Block(l1Block);
377
- }
378
-
379
- getProvenBlockNumber(): Promise<BlockNumber> {
380
- return this.#blockStore.getProvenBlockNumber();
381
- }
382
-
383
- /**
384
- * Gets the last L1 block number processed by the archiver
385
- */
386
- async getSynchPoint(): Promise<ArchiverL1SynchPoint> {
387
- const [blocksSynchedTo, messagesSynchedTo] = await Promise.all([
388
- this.#blockStore.getSynchedL1BlockNumber(),
389
- this.#messageStore.getSynchedL1Block(),
390
- ]);
391
- return {
392
- blocksSynchedTo,
393
- messagesSynchedTo,
394
- };
395
- }
396
-
397
- public estimateSize(): Promise<StoreSize> {
398
- return this.db.estimateSize();
399
- }
400
-
401
- public rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber: CheckpointNumber): Promise<void> {
402
- return this.#messageStore.rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber);
403
- }
404
-
405
- public iterateL1ToL2Messages(range: CustomRange<bigint> = {}): AsyncIterableIterator<InboxMessage> {
406
- return this.#messageStore.iterateL1ToL2Messages(range);
407
- }
408
-
409
- public removeL1ToL2Messages(startIndex: bigint): Promise<void> {
410
- return this.#messageStore.removeL1ToL2Messages(startIndex);
411
- }
412
-
413
- public getPendingChainValidationStatus(): Promise<ValidateBlockResult | undefined> {
414
- return this.#blockStore.getPendingChainValidationStatus();
415
- }
416
-
417
- public setPendingChainValidationStatus(status: ValidateBlockResult | undefined): Promise<void> {
418
- return this.#blockStore.setPendingChainValidationStatus(status);
419
- }
420
-
421
- public getCheckpointedL2BlockNumber(): Promise<BlockNumber> {
422
- return this.#blockStore.getCheckpointedL2BlockNumber();
423
- }
424
- public getSynchedCheckpointNumber(): Promise<CheckpointNumber> {
425
- return this.#blockStore.getLatestCheckpointNumber();
426
- }
427
- async setCheckpointSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
428
- await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
429
- }
430
-
431
- getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2BlockNew[] | undefined> {
432
- return this.#blockStore.getBlocksForCheckpoint(checkpointNumber);
433
- }
434
-
435
- getCheckpointData(checkpointNumber: CheckpointNumber): Promise<CheckpointData | undefined> {
436
- return this.#blockStore.getCheckpointData(checkpointNumber);
437
- }
438
- }