@aztec/archiver 0.0.0-test.0

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 (106) hide show
  1. package/README.md +16 -0
  2. package/dest/archiver/archiver.d.ts +197 -0
  3. package/dest/archiver/archiver.d.ts.map +1 -0
  4. package/dest/archiver/archiver.js +900 -0
  5. package/dest/archiver/archiver_store.d.ts +220 -0
  6. package/dest/archiver/archiver_store.d.ts.map +1 -0
  7. package/dest/archiver/archiver_store.js +4 -0
  8. package/dest/archiver/archiver_store_test_suite.d.ts +8 -0
  9. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -0
  10. package/dest/archiver/archiver_store_test_suite.js +794 -0
  11. package/dest/archiver/config.d.ts +37 -0
  12. package/dest/archiver/config.d.ts.map +1 -0
  13. package/dest/archiver/config.js +46 -0
  14. package/dest/archiver/data_retrieval.d.ts +74 -0
  15. package/dest/archiver/data_retrieval.d.ts.map +1 -0
  16. package/dest/archiver/data_retrieval.js +283 -0
  17. package/dest/archiver/errors.d.ts +4 -0
  18. package/dest/archiver/errors.d.ts.map +1 -0
  19. package/dest/archiver/errors.js +5 -0
  20. package/dest/archiver/index.d.ts +8 -0
  21. package/dest/archiver/index.d.ts.map +1 -0
  22. package/dest/archiver/index.js +5 -0
  23. package/dest/archiver/instrumentation.d.ts +29 -0
  24. package/dest/archiver/instrumentation.d.ts.map +1 -0
  25. package/dest/archiver/instrumentation.js +99 -0
  26. package/dest/archiver/kv_archiver_store/block_store.d.ts +87 -0
  27. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -0
  28. package/dest/archiver/kv_archiver_store/block_store.js +217 -0
  29. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +18 -0
  30. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -0
  31. package/dest/archiver/kv_archiver_store/contract_class_store.js +126 -0
  32. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +21 -0
  33. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -0
  34. package/dest/archiver/kv_archiver_store/contract_instance_store.js +63 -0
  35. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +153 -0
  36. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -0
  37. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +254 -0
  38. package/dest/archiver/kv_archiver_store/log_store.d.ts +49 -0
  39. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -0
  40. package/dest/archiver/kv_archiver_store/log_store.js +364 -0
  41. package/dest/archiver/kv_archiver_store/message_store.d.ts +33 -0
  42. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -0
  43. package/dest/archiver/kv_archiver_store/message_store.js +85 -0
  44. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +12 -0
  45. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +1 -0
  46. package/dest/archiver/kv_archiver_store/nullifier_store.js +73 -0
  47. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +23 -0
  48. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +1 -0
  49. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +49 -0
  50. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +175 -0
  51. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +1 -0
  52. package/dest/archiver/memory_archiver_store/memory_archiver_store.js +636 -0
  53. package/dest/archiver/structs/data_retrieval.d.ts +27 -0
  54. package/dest/archiver/structs/data_retrieval.d.ts.map +1 -0
  55. package/dest/archiver/structs/data_retrieval.js +5 -0
  56. package/dest/archiver/structs/published.d.ts +11 -0
  57. package/dest/archiver/structs/published.d.ts.map +1 -0
  58. package/dest/archiver/structs/published.js +1 -0
  59. package/dest/factory.d.ts +24 -0
  60. package/dest/factory.d.ts.map +1 -0
  61. package/dest/factory.js +85 -0
  62. package/dest/index.d.ts +5 -0
  63. package/dest/index.d.ts.map +1 -0
  64. package/dest/index.js +4 -0
  65. package/dest/rpc/index.d.ts +10 -0
  66. package/dest/rpc/index.d.ts.map +1 -0
  67. package/dest/rpc/index.js +18 -0
  68. package/dest/test/index.d.ts +4 -0
  69. package/dest/test/index.d.ts.map +1 -0
  70. package/dest/test/index.js +3 -0
  71. package/dest/test/mock_archiver.d.ts +23 -0
  72. package/dest/test/mock_archiver.d.ts.map +1 -0
  73. package/dest/test/mock_archiver.js +40 -0
  74. package/dest/test/mock_l1_to_l2_message_source.d.ts +16 -0
  75. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -0
  76. package/dest/test/mock_l1_to_l2_message_source.js +25 -0
  77. package/dest/test/mock_l2_block_source.d.ts +79 -0
  78. package/dest/test/mock_l2_block_source.d.ts.map +1 -0
  79. package/dest/test/mock_l2_block_source.js +163 -0
  80. package/package.json +110 -0
  81. package/src/archiver/archiver.ts +1181 -0
  82. package/src/archiver/archiver_store.ts +263 -0
  83. package/src/archiver/archiver_store_test_suite.ts +810 -0
  84. package/src/archiver/config.ts +92 -0
  85. package/src/archiver/data_retrieval.ts +422 -0
  86. package/src/archiver/errors.ts +5 -0
  87. package/src/archiver/index.ts +7 -0
  88. package/src/archiver/instrumentation.ts +132 -0
  89. package/src/archiver/kv_archiver_store/block_store.ts +283 -0
  90. package/src/archiver/kv_archiver_store/contract_class_store.ts +186 -0
  91. package/src/archiver/kv_archiver_store/contract_instance_store.ts +107 -0
  92. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +358 -0
  93. package/src/archiver/kv_archiver_store/log_store.ts +444 -0
  94. package/src/archiver/kv_archiver_store/message_store.ts +102 -0
  95. package/src/archiver/kv_archiver_store/nullifier_store.ts +97 -0
  96. package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +61 -0
  97. package/src/archiver/memory_archiver_store/memory_archiver_store.ts +801 -0
  98. package/src/archiver/structs/data_retrieval.ts +27 -0
  99. package/src/archiver/structs/published.ts +11 -0
  100. package/src/factory.ts +107 -0
  101. package/src/index.ts +5 -0
  102. package/src/rpc/index.ts +20 -0
  103. package/src/test/index.ts +3 -0
  104. package/src/test/mock_archiver.ts +57 -0
  105. package/src/test/mock_l1_to_l2_message_source.ts +31 -0
  106. package/src/test/mock_l2_block_source.ts +204 -0
@@ -0,0 +1,358 @@
1
+ import type { Fr } from '@aztec/foundation/fields';
2
+ import { toArray } from '@aztec/foundation/iterable';
3
+ import { createLogger } from '@aztec/foundation/log';
4
+ import type { AztecAsyncKVStore, StoreSize } from '@aztec/kv-store';
5
+ import { FunctionSelector } from '@aztec/stdlib/abi';
6
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
7
+ import type { InBlock, L2Block } from '@aztec/stdlib/block';
8
+ import type {
9
+ ContractClassPublic,
10
+ ContractInstanceUpdateWithAddress,
11
+ ContractInstanceWithAddress,
12
+ ExecutablePrivateFunctionWithMembershipProof,
13
+ UnconstrainedFunctionWithMembershipProof,
14
+ } from '@aztec/stdlib/contract';
15
+ import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
16
+ import { type LogFilter, PrivateLog, type TxScopedL2Log } from '@aztec/stdlib/logs';
17
+ import type { InboxLeaf } from '@aztec/stdlib/messaging';
18
+ import type { BlockHeader, TxHash, TxReceipt } from '@aztec/stdlib/tx';
19
+
20
+ import type { ArchiverDataStore, ArchiverL1SynchPoint } from '../archiver_store.js';
21
+ import type { DataRetrieval } from '../structs/data_retrieval.js';
22
+ import type { L1Published } from '../structs/published.js';
23
+ import { BlockStore } from './block_store.js';
24
+ import { ContractClassStore } from './contract_class_store.js';
25
+ import { ContractInstanceStore } from './contract_instance_store.js';
26
+ import { LogStore } from './log_store.js';
27
+ import { MessageStore } from './message_store.js';
28
+ import { NullifierStore } from './nullifier_store.js';
29
+
30
+ /**
31
+ * LMDB implementation of the ArchiverDataStore interface.
32
+ */
33
+ export class KVArchiverDataStore implements ArchiverDataStore {
34
+ public static readonly SCHEMA_VERSION = 1;
35
+
36
+ #blockStore: BlockStore;
37
+ #logStore: LogStore;
38
+ #nullifierStore: NullifierStore;
39
+ #messageStore: MessageStore;
40
+ #contractClassStore: ContractClassStore;
41
+ #contractInstanceStore: ContractInstanceStore;
42
+ private functionNames = new Map<string, string>();
43
+
44
+ #log = createLogger('archiver:data-store');
45
+
46
+ constructor(private db: AztecAsyncKVStore, logsMaxPageSize: number = 1000) {
47
+ this.#blockStore = new BlockStore(db);
48
+ this.#logStore = new LogStore(db, this.#blockStore, logsMaxPageSize);
49
+ this.#messageStore = new MessageStore(db);
50
+ this.#contractClassStore = new ContractClassStore(db);
51
+ this.#contractInstanceStore = new ContractInstanceStore(db);
52
+ this.#nullifierStore = new NullifierStore(db);
53
+ }
54
+
55
+ // TODO: These function names are in memory only as they are for development/debugging. They require the full contract
56
+ // artifact supplied to the node out of band. This should be reviewed and potentially removed as part of
57
+ // the node api cleanup process.
58
+ getContractFunctionName(_address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
59
+ return Promise.resolve(this.functionNames.get(selector.toString()));
60
+ }
61
+
62
+ async registerContractFunctionSignatures(_address: AztecAddress, signatures: string[]): Promise<void> {
63
+ for (const sig of signatures) {
64
+ try {
65
+ const selector = await FunctionSelector.fromSignature(sig);
66
+ this.functionNames.set(selector.toString(), sig.slice(0, sig.indexOf('(')));
67
+ } catch {
68
+ this.#log.warn(`Failed to parse signature: ${sig}. Ignoring`);
69
+ }
70
+ }
71
+ }
72
+
73
+ getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
74
+ return this.#contractClassStore.getContractClass(id);
75
+ }
76
+
77
+ getContractClassIds(): Promise<Fr[]> {
78
+ return this.#contractClassStore.getContractClassIds();
79
+ }
80
+
81
+ async getContractInstance(address: AztecAddress): Promise<ContractInstanceWithAddress | undefined> {
82
+ const contract = this.#contractInstanceStore.getContractInstance(address, await this.getSynchedL2BlockNumber());
83
+ return contract;
84
+ }
85
+
86
+ async addContractClasses(
87
+ data: ContractClassPublic[],
88
+ bytecodeCommitments: Fr[],
89
+ blockNumber: number,
90
+ ): Promise<boolean> {
91
+ return (
92
+ await Promise.all(
93
+ data.map((c, i) => this.#contractClassStore.addContractClass(c, bytecodeCommitments[i], blockNumber)),
94
+ )
95
+ ).every(Boolean);
96
+ }
97
+
98
+ async deleteContractClasses(data: ContractClassPublic[], blockNumber: number): Promise<boolean> {
99
+ return (await Promise.all(data.map(c => this.#contractClassStore.deleteContractClasses(c, blockNumber)))).every(
100
+ Boolean,
101
+ );
102
+ }
103
+
104
+ getBytecodeCommitment(contractClassId: Fr): Promise<Fr | undefined> {
105
+ return this.#contractClassStore.getBytecodeCommitment(contractClassId);
106
+ }
107
+
108
+ addFunctions(
109
+ contractClassId: Fr,
110
+ privateFunctions: ExecutablePrivateFunctionWithMembershipProof[],
111
+ unconstrainedFunctions: UnconstrainedFunctionWithMembershipProof[],
112
+ ): Promise<boolean> {
113
+ return this.#contractClassStore.addFunctions(contractClassId, privateFunctions, unconstrainedFunctions);
114
+ }
115
+
116
+ async addContractInstances(data: ContractInstanceWithAddress[], _blockNumber: number): Promise<boolean> {
117
+ return (await Promise.all(data.map(c => this.#contractInstanceStore.addContractInstance(c)))).every(Boolean);
118
+ }
119
+
120
+ async deleteContractInstances(data: ContractInstanceWithAddress[], _blockNumber: number): Promise<boolean> {
121
+ return (await Promise.all(data.map(c => this.#contractInstanceStore.deleteContractInstance(c)))).every(Boolean);
122
+ }
123
+
124
+ async addContractInstanceUpdates(data: ContractInstanceUpdateWithAddress[], blockNumber: number): Promise<boolean> {
125
+ return (
126
+ await Promise.all(
127
+ data.map((update, logIndex) =>
128
+ this.#contractInstanceStore.addContractInstanceUpdate(update, blockNumber, logIndex),
129
+ ),
130
+ )
131
+ ).every(Boolean);
132
+ }
133
+ async deleteContractInstanceUpdates(
134
+ data: ContractInstanceUpdateWithAddress[],
135
+ blockNumber: number,
136
+ ): Promise<boolean> {
137
+ return (
138
+ await Promise.all(
139
+ data.map((update, logIndex) =>
140
+ this.#contractInstanceStore.deleteContractInstanceUpdate(update, blockNumber, logIndex),
141
+ ),
142
+ )
143
+ ).every(Boolean);
144
+ }
145
+
146
+ /**
147
+ * Append new blocks to the store's list.
148
+ * @param blocks - The L2 blocks to be added to the store and the last processed L1 block.
149
+ * @returns True if the operation is successful.
150
+ */
151
+ addBlocks(blocks: L1Published<L2Block>[]): Promise<boolean> {
152
+ return this.#blockStore.addBlocks(blocks);
153
+ }
154
+
155
+ /**
156
+ * Unwinds blocks from the database
157
+ * @param from - The tip of the chain, passed for verification purposes,
158
+ * ensuring that we don't end up deleting something we did not intend
159
+ * @param blocksToUnwind - The number of blocks we are to unwind
160
+ * @returns True if the operation is successful
161
+ */
162
+ unwindBlocks(from: number, blocksToUnwind: number): Promise<boolean> {
163
+ return this.#blockStore.unwindBlocks(from, blocksToUnwind);
164
+ }
165
+
166
+ /**
167
+ * Gets up to `limit` amount of L2 blocks starting from `from`.
168
+ *
169
+ * @param start - Number of the first block to return (inclusive).
170
+ * @param limit - The number of blocks to return.
171
+ * @returns The requested L2 blocks
172
+ */
173
+ getBlocks(start: number, limit: number): Promise<L1Published<L2Block>[]> {
174
+ return toArray(this.#blockStore.getBlocks(start, limit));
175
+ }
176
+
177
+ /**
178
+ * Gets up to `limit` amount of L2 blocks headers starting from `from`.
179
+ *
180
+ * @param start - Number of the first block to return (inclusive).
181
+ * @param limit - The number of blocks to return.
182
+ * @returns The requested L2 blocks
183
+ */
184
+ getBlockHeaders(start: number, limit: number): Promise<BlockHeader[]> {
185
+ return toArray(this.#blockStore.getBlockHeaders(start, limit));
186
+ }
187
+
188
+ /**
189
+ * Gets a tx effect.
190
+ * @param txHash - The txHash of the tx corresponding to the tx effect.
191
+ * @returns The requested tx effect (or undefined if not found).
192
+ */
193
+ getTxEffect(txHash: TxHash) {
194
+ return this.#blockStore.getTxEffect(txHash);
195
+ }
196
+
197
+ /**
198
+ * Gets a receipt of a settled tx.
199
+ * @param txHash - The hash of a tx we try to get the receipt for.
200
+ * @returns The requested tx receipt (or undefined if not found).
201
+ */
202
+ getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
203
+ return this.#blockStore.getSettledTxReceipt(txHash);
204
+ }
205
+
206
+ /**
207
+ * Append new logs to the store's list.
208
+ * @param blocks - The blocks for which to add the logs.
209
+ * @returns True if the operation is successful.
210
+ */
211
+ addLogs(blocks: L2Block[]): Promise<boolean> {
212
+ return this.#logStore.addLogs(blocks);
213
+ }
214
+
215
+ deleteLogs(blocks: L2Block[]): Promise<boolean> {
216
+ return this.#logStore.deleteLogs(blocks);
217
+ }
218
+
219
+ /**
220
+ * Append new nullifiers to the store's list.
221
+ * @param blocks - The blocks for which to add the nullifiers.
222
+ * @returns True if the operation is successful.
223
+ */
224
+ addNullifiers(blocks: L2Block[]): Promise<boolean> {
225
+ return this.#nullifierStore.addNullifiers(blocks);
226
+ }
227
+
228
+ deleteNullifiers(blocks: L2Block[]): Promise<boolean> {
229
+ return this.#nullifierStore.deleteNullifiers(blocks);
230
+ }
231
+
232
+ findNullifiersIndexesWithBlock(blockNumber: number, nullifiers: Fr[]): Promise<(InBlock<bigint> | undefined)[]> {
233
+ return this.#nullifierStore.findNullifiersIndexesWithBlock(blockNumber, nullifiers);
234
+ }
235
+
236
+ getTotalL1ToL2MessageCount(): Promise<bigint> {
237
+ return this.#messageStore.getTotalL1ToL2MessageCount();
238
+ }
239
+
240
+ /**
241
+ * Append L1 to L2 messages to the store.
242
+ * @param messages - The L1 to L2 messages to be added to the store and the last processed L1 block.
243
+ * @returns True if the operation is successful.
244
+ */
245
+ addL1ToL2Messages(messages: DataRetrieval<InboxLeaf>): Promise<boolean> {
246
+ return this.#messageStore.addL1ToL2Messages(messages);
247
+ }
248
+
249
+ /**
250
+ * Gets the L1 to L2 message index in the L1 to L2 message tree.
251
+ * @param l1ToL2Message - The L1 to L2 message.
252
+ * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
253
+ */
254
+ getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
255
+ return this.#messageStore.getL1ToL2MessageIndex(l1ToL2Message);
256
+ }
257
+
258
+ /**
259
+ * Gets L1 to L2 message (to be) included in a given block.
260
+ * @param blockNumber - L2 block number to get messages for.
261
+ * @returns The L1 to L2 messages/leaves of the messages subtree (throws if not found).
262
+ */
263
+ getL1ToL2Messages(blockNumber: bigint): Promise<Fr[]> {
264
+ return this.#messageStore.getL1ToL2Messages(blockNumber);
265
+ }
266
+
267
+ /**
268
+ * Retrieves all private logs from up to `limit` blocks, starting from the block number `from`.
269
+ * @param from - The block number from which to begin retrieving logs.
270
+ * @param limit - The maximum number of blocks to retrieve logs from.
271
+ * @returns An array of private logs from the specified range of blocks.
272
+ */
273
+ getPrivateLogs(from: number, limit: number): Promise<PrivateLog[]> {
274
+ return this.#logStore.getPrivateLogs(from, limit);
275
+ }
276
+
277
+ /**
278
+ * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
279
+ * @param tags - The tags to filter the logs by.
280
+ * @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
281
+ * that tag.
282
+ */
283
+ getLogsByTags(tags: Fr[]): Promise<TxScopedL2Log[][]> {
284
+ try {
285
+ return this.#logStore.getLogsByTags(tags);
286
+ } catch (err) {
287
+ return Promise.reject(err);
288
+ }
289
+ }
290
+
291
+ /**
292
+ * Gets public logs based on the provided filter.
293
+ * @param filter - The filter to apply to the logs.
294
+ * @returns The requested logs.
295
+ */
296
+ getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
297
+ try {
298
+ return this.#logStore.getPublicLogs(filter);
299
+ } catch (err) {
300
+ return Promise.reject(err);
301
+ }
302
+ }
303
+
304
+ /**
305
+ * Gets contract class logs based on the provided filter.
306
+ * @param filter - The filter to apply to the logs.
307
+ * @returns The requested logs.
308
+ */
309
+ getContractClassLogs(filter: LogFilter): Promise<GetContractClassLogsResponse> {
310
+ try {
311
+ return this.#logStore.getContractClassLogs(filter);
312
+ } catch (err) {
313
+ return Promise.reject(err);
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Gets the number of the latest L2 block processed.
319
+ * @returns The number of the latest L2 block processed.
320
+ */
321
+ getSynchedL2BlockNumber(): Promise<number> {
322
+ return this.#blockStore.getSynchedL2BlockNumber();
323
+ }
324
+
325
+ getProvenL2BlockNumber(): Promise<number> {
326
+ return this.#blockStore.getProvenL2BlockNumber();
327
+ }
328
+
329
+ async setProvenL2BlockNumber(blockNumber: number) {
330
+ await this.#blockStore.setProvenL2BlockNumber(blockNumber);
331
+ }
332
+
333
+ async setBlockSynchedL1BlockNumber(l1BlockNumber: bigint) {
334
+ await this.#blockStore.setSynchedL1BlockNumber(l1BlockNumber);
335
+ }
336
+
337
+ async setMessageSynchedL1BlockNumber(l1BlockNumber: bigint) {
338
+ await this.#messageStore.setSynchedL1BlockNumber(l1BlockNumber);
339
+ }
340
+
341
+ /**
342
+ * Gets the last L1 block number processed by the archiver
343
+ */
344
+ async getSynchPoint(): Promise<ArchiverL1SynchPoint> {
345
+ const [blocksSynchedTo, messagesSynchedTo] = await Promise.all([
346
+ this.#blockStore.getSynchedL1BlockNumber(),
347
+ this.#messageStore.getSynchedL1BlockNumber(),
348
+ ]);
349
+ return {
350
+ blocksSynchedTo,
351
+ messagesSynchedTo,
352
+ };
353
+ }
354
+
355
+ public estimateSize(): Promise<StoreSize> {
356
+ return this.db.estimateSize();
357
+ }
358
+ }