@aztec/archiver 0.0.0-test.0 → 0.0.1-commit.023c3e5

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 (225) hide show
  1. package/README.md +164 -9
  2. package/dest/archiver.d.ts +136 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +781 -0
  5. package/dest/config.d.ts +30 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/config.js +71 -0
  8. package/dest/errors.d.ts +41 -0
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/errors.js +62 -0
  11. package/dest/factory.d.ts +13 -16
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +103 -55
  14. package/dest/index.d.ts +10 -4
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +8 -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 +149 -0
  23. package/dest/l1/calldata_retriever.d.ts +112 -0
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/l1/calldata_retriever.js +471 -0
  26. package/dest/l1/data_retrieval.d.ts +88 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/l1/data_retrieval.js +312 -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 +157 -0
  35. package/dest/l1/trace_tx.d.ts +97 -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_trace.d.ts +32 -0
  42. package/dest/l1/validate_trace.d.ts.map +1 -0
  43. package/dest/l1/validate_trace.js +154 -0
  44. package/dest/modules/data_source_base.d.ts +84 -0
  45. package/dest/modules/data_source_base.d.ts.map +1 -0
  46. package/dest/modules/data_source_base.js +260 -0
  47. package/dest/modules/data_store_updater.d.ts +73 -0
  48. package/dest/modules/data_store_updater.d.ts.map +1 -0
  49. package/dest/modules/data_store_updater.js +302 -0
  50. package/dest/modules/instrumentation.d.ts +37 -0
  51. package/dest/modules/instrumentation.d.ts.map +1 -0
  52. package/dest/modules/instrumentation.js +110 -0
  53. package/dest/modules/l1_synchronizer.d.ts +75 -0
  54. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  55. package/dest/modules/l1_synchronizer.js +1112 -0
  56. package/dest/modules/validation.d.ts +17 -0
  57. package/dest/modules/validation.d.ts.map +1 -0
  58. package/dest/modules/validation.js +104 -0
  59. package/dest/store/block_store.d.ts +192 -0
  60. package/dest/store/block_store.d.ts.map +1 -0
  61. package/dest/store/block_store.js +721 -0
  62. package/dest/store/contract_class_store.d.ts +18 -0
  63. package/dest/store/contract_class_store.d.ts.map +1 -0
  64. package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +25 -27
  65. package/dest/store/contract_instance_store.d.ts +24 -0
  66. package/dest/store/contract_instance_store.d.ts.map +1 -0
  67. package/dest/store/contract_instance_store.js +77 -0
  68. package/dest/store/kv_archiver_store.d.ts +340 -0
  69. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  70. package/dest/store/kv_archiver_store.js +446 -0
  71. package/dest/store/log_store.d.ts +54 -0
  72. package/dest/store/log_store.d.ts.map +1 -0
  73. package/dest/store/log_store.js +436 -0
  74. package/dest/store/message_store.d.ts +40 -0
  75. package/dest/store/message_store.d.ts.map +1 -0
  76. package/dest/store/message_store.js +188 -0
  77. package/dest/structs/data_retrieval.d.ts +27 -0
  78. package/dest/structs/data_retrieval.d.ts.map +1 -0
  79. package/dest/structs/inbox_message.d.ts +15 -0
  80. package/dest/structs/inbox_message.d.ts.map +1 -0
  81. package/dest/structs/inbox_message.js +39 -0
  82. package/dest/structs/published.d.ts +2 -0
  83. package/dest/structs/published.d.ts.map +1 -0
  84. package/dest/structs/published.js +1 -0
  85. package/dest/test/fake_l1_state.d.ts +190 -0
  86. package/dest/test/fake_l1_state.d.ts.map +1 -0
  87. package/dest/test/fake_l1_state.js +383 -0
  88. package/dest/test/index.d.ts +2 -1
  89. package/dest/test/index.d.ts.map +1 -1
  90. package/dest/test/index.js +4 -1
  91. package/dest/test/mock_archiver.d.ts +16 -8
  92. package/dest/test/mock_archiver.d.ts.map +1 -1
  93. package/dest/test/mock_archiver.js +18 -14
  94. package/dest/test/mock_l1_to_l2_message_source.d.ts +9 -6
  95. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  96. package/dest/test/mock_l1_to_l2_message_source.js +30 -7
  97. package/dest/test/mock_l2_block_source.d.ts +62 -16
  98. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  99. package/dest/test/mock_l2_block_source.js +263 -32
  100. package/dest/test/mock_structs.d.ts +85 -0
  101. package/dest/test/mock_structs.d.ts.map +1 -0
  102. package/dest/test/mock_structs.js +171 -0
  103. package/dest/test/noop_l1_archiver.d.ts +23 -0
  104. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  105. package/dest/test/noop_l1_archiver.js +68 -0
  106. package/package.json +31 -33
  107. package/src/archiver.ts +543 -0
  108. package/src/config.ts +95 -0
  109. package/src/errors.ts +102 -0
  110. package/src/factory.ts +144 -71
  111. package/src/index.ts +10 -3
  112. package/src/interfaces.ts +9 -0
  113. package/src/l1/README.md +98 -0
  114. package/src/l1/bin/retrieve-calldata.ts +187 -0
  115. package/src/l1/calldata_retriever.ts +641 -0
  116. package/src/l1/data_retrieval.ts +495 -0
  117. package/src/l1/debug_tx.ts +99 -0
  118. package/src/l1/spire_proposer.ts +160 -0
  119. package/src/l1/trace_tx.ts +128 -0
  120. package/src/l1/types.ts +13 -0
  121. package/src/l1/validate_trace.ts +229 -0
  122. package/src/modules/data_source_base.ts +367 -0
  123. package/src/modules/data_store_updater.ts +423 -0
  124. package/src/modules/instrumentation.ts +157 -0
  125. package/src/modules/l1_synchronizer.ts +930 -0
  126. package/src/modules/validation.ts +129 -0
  127. package/src/store/block_store.ts +966 -0
  128. package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +26 -32
  129. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +37 -29
  130. package/src/store/kv_archiver_store.ts +639 -0
  131. package/src/store/log_store.ts +575 -0
  132. package/src/store/message_store.ts +261 -0
  133. package/src/structs/inbox_message.ts +41 -0
  134. package/src/structs/published.ts +1 -0
  135. package/src/test/fake_l1_state.ts +599 -0
  136. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  137. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  138. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  139. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  140. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  141. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  142. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  143. package/src/test/index.ts +4 -0
  144. package/src/test/mock_archiver.ts +22 -16
  145. package/src/test/mock_l1_to_l2_message_source.ts +26 -8
  146. package/src/test/mock_l2_block_source.ts +313 -42
  147. package/src/test/mock_structs.ts +311 -0
  148. package/src/test/noop_l1_archiver.ts +109 -0
  149. package/dest/archiver/archiver.d.ts +0 -197
  150. package/dest/archiver/archiver.d.ts.map +0 -1
  151. package/dest/archiver/archiver.js +0 -900
  152. package/dest/archiver/archiver_store.d.ts +0 -220
  153. package/dest/archiver/archiver_store.d.ts.map +0 -1
  154. package/dest/archiver/archiver_store.js +0 -4
  155. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  156. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  157. package/dest/archiver/archiver_store_test_suite.js +0 -794
  158. package/dest/archiver/config.d.ts +0 -37
  159. package/dest/archiver/config.d.ts.map +0 -1
  160. package/dest/archiver/config.js +0 -46
  161. package/dest/archiver/data_retrieval.d.ts +0 -74
  162. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  163. package/dest/archiver/data_retrieval.js +0 -283
  164. package/dest/archiver/errors.d.ts +0 -4
  165. package/dest/archiver/errors.d.ts.map +0 -1
  166. package/dest/archiver/errors.js +0 -5
  167. package/dest/archiver/index.d.ts +0 -8
  168. package/dest/archiver/index.d.ts.map +0 -1
  169. package/dest/archiver/index.js +0 -5
  170. package/dest/archiver/instrumentation.d.ts +0 -29
  171. package/dest/archiver/instrumentation.d.ts.map +0 -1
  172. package/dest/archiver/instrumentation.js +0 -99
  173. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -87
  174. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  175. package/dest/archiver/kv_archiver_store/block_store.js +0 -217
  176. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  177. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  178. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -21
  179. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  180. package/dest/archiver/kv_archiver_store/contract_instance_store.js +0 -63
  181. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -153
  182. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  183. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -254
  184. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
  185. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  186. package/dest/archiver/kv_archiver_store/log_store.js +0 -364
  187. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -33
  188. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  189. package/dest/archiver/kv_archiver_store/message_store.js +0 -85
  190. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts +0 -12
  191. package/dest/archiver/kv_archiver_store/nullifier_store.d.ts.map +0 -1
  192. package/dest/archiver/kv_archiver_store/nullifier_store.js +0 -73
  193. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts +0 -23
  194. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.d.ts.map +0 -1
  195. package/dest/archiver/memory_archiver_store/l1_to_l2_message_store.js +0 -49
  196. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts +0 -175
  197. package/dest/archiver/memory_archiver_store/memory_archiver_store.d.ts.map +0 -1
  198. package/dest/archiver/memory_archiver_store/memory_archiver_store.js +0 -636
  199. package/dest/archiver/structs/data_retrieval.d.ts +0 -27
  200. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  201. package/dest/archiver/structs/published.d.ts +0 -11
  202. package/dest/archiver/structs/published.d.ts.map +0 -1
  203. package/dest/archiver/structs/published.js +0 -1
  204. package/dest/rpc/index.d.ts +0 -10
  205. package/dest/rpc/index.d.ts.map +0 -1
  206. package/dest/rpc/index.js +0 -18
  207. package/src/archiver/archiver.ts +0 -1181
  208. package/src/archiver/archiver_store.ts +0 -263
  209. package/src/archiver/archiver_store_test_suite.ts +0 -810
  210. package/src/archiver/config.ts +0 -92
  211. package/src/archiver/data_retrieval.ts +0 -422
  212. package/src/archiver/errors.ts +0 -5
  213. package/src/archiver/index.ts +0 -7
  214. package/src/archiver/instrumentation.ts +0 -132
  215. package/src/archiver/kv_archiver_store/block_store.ts +0 -283
  216. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -358
  217. package/src/archiver/kv_archiver_store/log_store.ts +0 -444
  218. package/src/archiver/kv_archiver_store/message_store.ts +0 -102
  219. package/src/archiver/kv_archiver_store/nullifier_store.ts +0 -97
  220. package/src/archiver/memory_archiver_store/l1_to_l2_message_store.ts +0 -61
  221. package/src/archiver/memory_archiver_store/memory_archiver_store.ts +0 -801
  222. package/src/archiver/structs/published.ts +0 -11
  223. package/src/rpc/index.ts +0 -20
  224. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  225. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
@@ -1,444 +0,0 @@
1
- import { INITIAL_L2_BLOCK_NUM, MAX_NOTE_HASHES_PER_TX, PUBLIC_LOG_DATA_SIZE_IN_FIELDS } from '@aztec/constants';
2
- import type { Fr } from '@aztec/foundation/fields';
3
- import { createLogger } from '@aztec/foundation/log';
4
- import { BufferReader, numToUInt32BE } from '@aztec/foundation/serialize';
5
- import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
6
- import type { L2Block } from '@aztec/stdlib/block';
7
- import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
8
- import {
9
- ContractClassLog,
10
- ExtendedContractClassLog,
11
- ExtendedPublicLog,
12
- type LogFilter,
13
- LogId,
14
- PrivateLog,
15
- PublicLog,
16
- TxScopedL2Log,
17
- } from '@aztec/stdlib/logs';
18
-
19
- import type { BlockStore } from './block_store.js';
20
-
21
- /**
22
- * A store for logs
23
- */
24
- export class LogStore {
25
- #logsByTag: AztecAsyncMap<string, Buffer[]>;
26
- #logTagsByBlock: AztecAsyncMap<number, string[]>;
27
- #privateLogsByBlock: AztecAsyncMap<number, Buffer>;
28
- #publicLogsByBlock: AztecAsyncMap<number, Buffer>;
29
- #contractClassLogsByBlock: AztecAsyncMap<number, Buffer>;
30
- #logsMaxPageSize: number;
31
- #log = createLogger('archiver:log_store');
32
-
33
- constructor(private db: AztecAsyncKVStore, private blockStore: BlockStore, logsMaxPageSize: number = 1000) {
34
- this.#logsByTag = db.openMap('archiver_tagged_logs_by_tag');
35
- this.#logTagsByBlock = db.openMap('archiver_log_tags_by_block');
36
- this.#privateLogsByBlock = db.openMap('archiver_private_logs_by_block');
37
- this.#publicLogsByBlock = db.openMap('archiver_public_logs_by_block');
38
- this.#contractClassLogsByBlock = db.openMap('archiver_contract_class_logs_by_block');
39
-
40
- this.#logsMaxPageSize = logsMaxPageSize;
41
- }
42
-
43
- #extractTaggedLogsFromPrivate(block: L2Block) {
44
- const taggedLogs = new Map<string, Buffer[]>();
45
- const dataStartIndexForBlock =
46
- block.header.state.partial.noteHashTree.nextAvailableLeafIndex -
47
- block.body.txEffects.length * MAX_NOTE_HASHES_PER_TX;
48
- block.body.txEffects.forEach((txEffect, txIndex) => {
49
- const txHash = txEffect.txHash;
50
- const dataStartIndexForTx = dataStartIndexForBlock + txIndex * MAX_NOTE_HASHES_PER_TX;
51
- txEffect.privateLogs.forEach(log => {
52
- const tag = log.fields[0];
53
- const currentLogs = taggedLogs.get(tag.toString()) ?? [];
54
- currentLogs.push(
55
- new TxScopedL2Log(
56
- txHash,
57
- dataStartIndexForTx,
58
- block.number,
59
- /* isFromPublic */ false,
60
- log.toBuffer(),
61
- ).toBuffer(),
62
- );
63
- taggedLogs.set(tag.toString(), currentLogs);
64
- });
65
- });
66
- return taggedLogs;
67
- }
68
-
69
- #extractTaggedLogsFromPublic(block: L2Block) {
70
- const taggedLogs = new Map<string, Buffer[]>();
71
- const dataStartIndexForBlock =
72
- block.header.state.partial.noteHashTree.nextAvailableLeafIndex -
73
- block.body.txEffects.length * MAX_NOTE_HASHES_PER_TX;
74
- block.body.txEffects.forEach((txEffect, txIndex) => {
75
- const txHash = txEffect.txHash;
76
- const dataStartIndexForTx = dataStartIndexForBlock + txIndex * MAX_NOTE_HASHES_PER_TX;
77
- txEffect.publicLogs.forEach(log => {
78
- // Check that each log stores 2 lengths in its first field. If not, it's not a tagged log:
79
- const firstFieldBuf = log.log[0].toBuffer();
80
- // See macros/note/mod/ and see how finalization_log[0] is constructed, to understand this monstrosity. (It wasn't me).
81
- // Search the codebase for "disgusting encoding" to see other hardcoded instances of this encoding, that you might need to change if you ever find yourself here.
82
- if (!firstFieldBuf.subarray(0, 27).equals(Buffer.alloc(27)) || firstFieldBuf[29] !== 0) {
83
- // See parseLogFromPublic - the first field of a tagged log is 5 bytes structured:
84
- // [ publicLen[0], publicLen[1], 0, privateLen[0], privateLen[1]]
85
- this.#log.warn(`Skipping public log with invalid first field: ${log.log[0]}`);
86
- return;
87
- }
88
- // Check that the length values line up with the log contents
89
- const publicValuesLength = firstFieldBuf.subarray(-5).readUint16BE();
90
- const privateValuesLength = firstFieldBuf.subarray(-5).readUint16BE(3);
91
- // Add 1 for the first field holding lengths
92
- const totalLogLength = 1 + publicValuesLength + privateValuesLength;
93
- // Note that zeroes can be valid log values, so we can only assert that we do not go over the given length
94
- if (totalLogLength > PUBLIC_LOG_DATA_SIZE_IN_FIELDS || log.log.slice(totalLogLength).find(f => !f.isZero())) {
95
- this.#log.warn(`Skipping invalid tagged public log with first field: ${log.log[0]}`);
96
- return;
97
- }
98
-
99
- // The first elt stores lengths as above => tag is in fields[1]
100
- const tag = log.log[1];
101
-
102
- this.#log.debug(`Found tagged public log with tag ${tag.toString()} in block ${block.number}`);
103
- const currentLogs = taggedLogs.get(tag.toString()) ?? [];
104
- currentLogs.push(
105
- new TxScopedL2Log(
106
- txHash,
107
- dataStartIndexForTx,
108
- block.number,
109
- /* isFromPublic */ true,
110
- log.toBuffer(),
111
- ).toBuffer(),
112
- );
113
- taggedLogs.set(tag.toString(), currentLogs);
114
- });
115
- });
116
- return taggedLogs;
117
- }
118
-
119
- /**
120
- * Append new logs to the store's list.
121
- * @param blocks - The blocks for which to add the logs.
122
- * @returns True if the operation is successful.
123
- */
124
- addLogs(blocks: L2Block[]): Promise<boolean> {
125
- const taggedLogsToAdd = blocks
126
- .flatMap(block => [this.#extractTaggedLogsFromPrivate(block), this.#extractTaggedLogsFromPublic(block)])
127
- .reduce((acc, val) => {
128
- for (const [tag, logs] of val.entries()) {
129
- const currentLogs = acc.get(tag) ?? [];
130
- acc.set(tag, currentLogs.concat(logs));
131
- }
132
- return acc;
133
- });
134
- const tagsToUpdate = Array.from(taggedLogsToAdd.keys());
135
-
136
- return this.db.transactionAsync(async () => {
137
- const currentTaggedLogs = await Promise.all(
138
- tagsToUpdate.map(async tag => ({ tag, logBuffers: await this.#logsByTag.getAsync(tag) })),
139
- );
140
- currentTaggedLogs.forEach(taggedLogBuffer => {
141
- if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
142
- taggedLogsToAdd.set(
143
- taggedLogBuffer.tag,
144
- taggedLogBuffer.logBuffers!.concat(taggedLogsToAdd.get(taggedLogBuffer.tag)!),
145
- );
146
- }
147
- });
148
- for (const block of blocks) {
149
- const tagsInBlock = [];
150
- for (const [tag, logs] of taggedLogsToAdd.entries()) {
151
- await this.#logsByTag.set(tag, logs);
152
- tagsInBlock.push(tag);
153
- }
154
- await this.#logTagsByBlock.set(block.number, tagsInBlock);
155
-
156
- const privateLogsInBlock = block.body.txEffects
157
- .map(txEffect => txEffect.privateLogs)
158
- .flat()
159
- .map(log => log.toBuffer());
160
- await this.#privateLogsByBlock.set(block.number, Buffer.concat(privateLogsInBlock));
161
-
162
- const publicLogsInBlock = block.body.txEffects
163
- .map((txEffect, txIndex) =>
164
- [
165
- numToUInt32BE(txIndex),
166
- numToUInt32BE(txEffect.publicLogs.length),
167
- txEffect.publicLogs.map(log => log.toBuffer()),
168
- ].flat(),
169
- )
170
- .flat();
171
-
172
- const contractClassLogsInBlock = block.body.txEffects
173
- .map((txEffect, txIndex) =>
174
- [
175
- numToUInt32BE(txIndex),
176
- numToUInt32BE(txEffect.contractClassLogs.length),
177
- txEffect.contractClassLogs.map(log => log.toBuffer()),
178
- ].flat(),
179
- )
180
- .flat();
181
-
182
- await this.#publicLogsByBlock.set(block.number, Buffer.concat(publicLogsInBlock));
183
- await this.#contractClassLogsByBlock.set(block.number, Buffer.concat(contractClassLogsInBlock));
184
- }
185
-
186
- return true;
187
- });
188
- }
189
-
190
- deleteLogs(blocks: L2Block[]): Promise<boolean> {
191
- return this.db.transactionAsync(async () => {
192
- const tagsToDelete = (
193
- await Promise.all(
194
- blocks.map(async block => {
195
- const tags = await this.#logTagsByBlock.getAsync(block.number);
196
- return tags ?? [];
197
- }),
198
- )
199
- ).flat();
200
-
201
- await Promise.all(
202
- blocks.map(block =>
203
- Promise.all([
204
- this.#privateLogsByBlock.delete(block.number),
205
- this.#publicLogsByBlock.delete(block.number),
206
- this.#logTagsByBlock.delete(block.number),
207
- ]),
208
- ),
209
- );
210
-
211
- await Promise.all(tagsToDelete.map(tag => this.#logsByTag.delete(tag.toString())));
212
- return true;
213
- });
214
- }
215
-
216
- /**
217
- * Retrieves all private logs from up to `limit` blocks, starting from the block number `start`.
218
- * @param start - The block number from which to begin retrieving logs.
219
- * @param limit - The maximum number of blocks to retrieve logs from.
220
- * @returns An array of private logs from the specified range of blocks.
221
- */
222
- async getPrivateLogs(start: number, limit: number): Promise<PrivateLog[]> {
223
- const logs = [];
224
- for await (const buffer of this.#privateLogsByBlock.valuesAsync({ start, limit })) {
225
- const reader = new BufferReader(buffer);
226
- while (reader.remainingBytes() > 0) {
227
- logs.push(reader.readObject(PrivateLog));
228
- }
229
- }
230
- return logs;
231
- }
232
-
233
- /**
234
- * Gets all logs that match any of the received tags (i.e. logs with their first field equal to a tag).
235
- * @param tags - The tags to filter the logs by.
236
- * @returns For each received tag, an array of matching logs is returned. An empty array implies no logs match
237
- * that tag.
238
- */
239
- async getLogsByTags(tags: Fr[]): Promise<TxScopedL2Log[][]> {
240
- const logs = await Promise.all(tags.map(tag => this.#logsByTag.getAsync(tag.toString())));
241
- return logs.map(
242
- noteLogBuffers => noteLogBuffers?.map(noteLogBuffer => TxScopedL2Log.fromBuffer(noteLogBuffer)) ?? [],
243
- );
244
- }
245
-
246
- /**
247
- * Gets public logs based on the provided filter.
248
- * @param filter - The filter to apply to the logs.
249
- * @returns The requested logs.
250
- */
251
- getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
252
- if (filter.afterLog) {
253
- return this.#filterPublicLogsBetweenBlocks(filter);
254
- } else if (filter.txHash) {
255
- return this.#filterPublicLogsOfTx(filter);
256
- } else {
257
- return this.#filterPublicLogsBetweenBlocks(filter);
258
- }
259
- }
260
-
261
- async #filterPublicLogsOfTx(filter: LogFilter): Promise<GetPublicLogsResponse> {
262
- if (!filter.txHash) {
263
- throw new Error('Missing txHash');
264
- }
265
-
266
- const [blockNumber, txIndex] = (await this.blockStore.getTxLocation(filter.txHash)) ?? [];
267
- if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
268
- return { logs: [], maxLogsHit: false };
269
- }
270
-
271
- const buffer = (await this.#publicLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
272
- const publicLogsInBlock: [PublicLog[]] = [[]];
273
- const reader = new BufferReader(buffer);
274
- while (reader.remainingBytes() > 0) {
275
- const indexOfTx = reader.readNumber();
276
- const numLogsInTx = reader.readNumber();
277
- publicLogsInBlock[indexOfTx] = [];
278
- for (let i = 0; i < numLogsInTx; i++) {
279
- publicLogsInBlock[indexOfTx].push(reader.readObject(PublicLog));
280
- }
281
- }
282
-
283
- const txLogs = publicLogsInBlock[txIndex];
284
-
285
- const logs: ExtendedPublicLog[] = [];
286
- const maxLogsHit = this.#accumulateLogs(logs, blockNumber, txIndex, txLogs, filter);
287
-
288
- return { logs, maxLogsHit };
289
- }
290
-
291
- async #filterPublicLogsBetweenBlocks(filter: LogFilter): Promise<GetPublicLogsResponse> {
292
- const start =
293
- filter.afterLog?.blockNumber ?? Math.max(filter.fromBlock ?? INITIAL_L2_BLOCK_NUM, INITIAL_L2_BLOCK_NUM);
294
- const end = filter.toBlock;
295
-
296
- if (typeof end === 'number' && end < start) {
297
- return {
298
- logs: [],
299
- maxLogsHit: true,
300
- };
301
- }
302
-
303
- const logs: ExtendedPublicLog[] = [];
304
-
305
- let maxLogsHit = false;
306
- loopOverBlocks: for await (const [blockNumber, logBuffer] of this.#publicLogsByBlock.entriesAsync({ start, end })) {
307
- const publicLogsInBlock: [PublicLog[]] = [[]];
308
- const reader = new BufferReader(logBuffer);
309
- while (reader.remainingBytes() > 0) {
310
- const indexOfTx = reader.readNumber();
311
- const numLogsInTx = reader.readNumber();
312
- publicLogsInBlock[indexOfTx] = [];
313
- for (let i = 0; i < numLogsInTx; i++) {
314
- publicLogsInBlock[indexOfTx].push(reader.readObject(PublicLog));
315
- }
316
- }
317
- for (let txIndex = filter.afterLog?.txIndex ?? 0; txIndex < publicLogsInBlock.length; txIndex++) {
318
- const txLogs = publicLogsInBlock[txIndex];
319
- maxLogsHit = this.#accumulateLogs(logs, blockNumber, txIndex, txLogs, filter);
320
- if (maxLogsHit) {
321
- this.#log.debug(`Max logs hit at block ${blockNumber}`);
322
- break loopOverBlocks;
323
- }
324
- }
325
- }
326
-
327
- return { logs, maxLogsHit };
328
- }
329
-
330
- /**
331
- * Gets contract class logs based on the provided filter.
332
- * @param filter - The filter to apply to the logs.
333
- * @returns The requested logs.
334
- */
335
- getContractClassLogs(filter: LogFilter): Promise<GetContractClassLogsResponse> {
336
- if (filter.afterLog) {
337
- return this.#filterContractClassLogsBetweenBlocks(filter);
338
- } else if (filter.txHash) {
339
- return this.#filterContractClassLogsOfTx(filter);
340
- } else {
341
- return this.#filterContractClassLogsBetweenBlocks(filter);
342
- }
343
- }
344
-
345
- async #filterContractClassLogsOfTx(filter: LogFilter): Promise<GetContractClassLogsResponse> {
346
- if (!filter.txHash) {
347
- throw new Error('Missing txHash');
348
- }
349
-
350
- const [blockNumber, txIndex] = (await this.blockStore.getTxLocation(filter.txHash)) ?? [];
351
- if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
352
- return { logs: [], maxLogsHit: false };
353
- }
354
- const contractClassLogsBuffer = (await this.#contractClassLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
355
- const contractClassLogsInBlock: [ContractClassLog[]] = [[]];
356
-
357
- const reader = new BufferReader(contractClassLogsBuffer);
358
- while (reader.remainingBytes() > 0) {
359
- const indexOfTx = reader.readNumber();
360
- const numLogsInTx = reader.readNumber();
361
- contractClassLogsInBlock[indexOfTx] = [];
362
- for (let i = 0; i < numLogsInTx; i++) {
363
- contractClassLogsInBlock[indexOfTx].push(reader.readObject(ContractClassLog));
364
- }
365
- }
366
-
367
- const txLogs = contractClassLogsInBlock[txIndex];
368
-
369
- const logs: ExtendedContractClassLog[] = [];
370
- const maxLogsHit = this.#accumulateLogs(logs, blockNumber, txIndex, txLogs, filter);
371
-
372
- return { logs, maxLogsHit };
373
- }
374
-
375
- async #filterContractClassLogsBetweenBlocks(filter: LogFilter): Promise<GetContractClassLogsResponse> {
376
- const start =
377
- filter.afterLog?.blockNumber ?? Math.max(filter.fromBlock ?? INITIAL_L2_BLOCK_NUM, INITIAL_L2_BLOCK_NUM);
378
- const end = filter.toBlock;
379
-
380
- if (typeof end === 'number' && end < start) {
381
- return {
382
- logs: [],
383
- maxLogsHit: true,
384
- };
385
- }
386
-
387
- const logs: ExtendedContractClassLog[] = [];
388
-
389
- let maxLogsHit = false;
390
- loopOverBlocks: for await (const [blockNumber, logBuffer] of this.#contractClassLogsByBlock.entriesAsync({
391
- start,
392
- end,
393
- })) {
394
- const contractClassLogsInBlock: [ContractClassLog[]] = [[]];
395
- const reader = new BufferReader(logBuffer);
396
- while (reader.remainingBytes() > 0) {
397
- const indexOfTx = reader.readNumber();
398
- const numLogsInTx = reader.readNumber();
399
- contractClassLogsInBlock[indexOfTx] = [];
400
- for (let i = 0; i < numLogsInTx; i++) {
401
- contractClassLogsInBlock[indexOfTx].push(reader.readObject(ContractClassLog));
402
- }
403
- }
404
- for (let txIndex = filter.afterLog?.txIndex ?? 0; txIndex < contractClassLogsInBlock.length; txIndex++) {
405
- const txLogs = contractClassLogsInBlock[txIndex];
406
- maxLogsHit = this.#accumulateLogs(logs, blockNumber, txIndex, txLogs, filter);
407
- if (maxLogsHit) {
408
- this.#log.debug(`Max logs hit at block ${blockNumber}`);
409
- break loopOverBlocks;
410
- }
411
- }
412
- }
413
-
414
- return { logs, maxLogsHit };
415
- }
416
-
417
- #accumulateLogs(
418
- results: (ExtendedContractClassLog | ExtendedPublicLog)[],
419
- blockNumber: number,
420
- txIndex: number,
421
- txLogs: (ContractClassLog | PublicLog)[],
422
- filter: LogFilter,
423
- ): boolean {
424
- let maxLogsHit = false;
425
- let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
426
- for (; logIndex < txLogs.length; logIndex++) {
427
- const log = txLogs[logIndex];
428
- if (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) {
429
- if (log instanceof ContractClassLog) {
430
- results.push(new ExtendedContractClassLog(new LogId(blockNumber, txIndex, logIndex), log));
431
- } else {
432
- results.push(new ExtendedPublicLog(new LogId(blockNumber, txIndex, logIndex), log));
433
- }
434
-
435
- if (results.length >= this.#logsMaxPageSize) {
436
- maxLogsHit = true;
437
- break;
438
- }
439
- }
440
- }
441
-
442
- return maxLogsHit;
443
- }
444
- }
@@ -1,102 +0,0 @@
1
- import { L1_TO_L2_MSG_SUBTREE_HEIGHT } from '@aztec/constants';
2
- import { Fr } from '@aztec/foundation/fields';
3
- import { createLogger } from '@aztec/foundation/log';
4
- import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton } from '@aztec/kv-store';
5
- import { InboxLeaf } from '@aztec/stdlib/messaging';
6
-
7
- import type { DataRetrieval } from '../structs/data_retrieval.js';
8
-
9
- /**
10
- * LMDB implementation of the ArchiverDataStore interface.
11
- */
12
- export class MessageStore {
13
- #l1ToL2Messages: AztecAsyncMap<string, Buffer>;
14
- #l1ToL2MessageIndices: AztecAsyncMap<string, bigint>;
15
- #lastSynchedL1Block: AztecAsyncSingleton<bigint>;
16
- #totalMessageCount: AztecAsyncSingleton<bigint>;
17
-
18
- #log = createLogger('archiver:message_store');
19
-
20
- #l1ToL2MessagesSubtreeSize = 2 ** L1_TO_L2_MSG_SUBTREE_HEIGHT;
21
-
22
- constructor(private db: AztecAsyncKVStore) {
23
- this.#l1ToL2Messages = db.openMap('archiver_l1_to_l2_messages');
24
- this.#l1ToL2MessageIndices = db.openMap('archiver_l1_to_l2_message_indices');
25
- this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_new_messages');
26
- this.#totalMessageCount = db.openSingleton('archiver_l1_to_l2_message_count');
27
- }
28
-
29
- async getTotalL1ToL2MessageCount(): Promise<bigint> {
30
- return (await this.#totalMessageCount.getAsync()) ?? 0n;
31
- }
32
-
33
- /**
34
- * Gets the last L1 block number that emitted new messages.
35
- * @returns The last L1 block number processed
36
- */
37
- getSynchedL1BlockNumber(): Promise<bigint | undefined> {
38
- return this.#lastSynchedL1Block.getAsync();
39
- }
40
-
41
- async setSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
42
- await this.#lastSynchedL1Block.set(l1BlockNumber);
43
- }
44
-
45
- /**
46
- * Append L1 to L2 messages to the store.
47
- * @param messages - The L1 to L2 messages to be added to the store and the last processed L1 block.
48
- * @returns True if the operation is successful.
49
- */
50
- addL1ToL2Messages(messages: DataRetrieval<InboxLeaf>): Promise<boolean> {
51
- return this.db.transactionAsync(async () => {
52
- const lastL1BlockNumber = (await this.#lastSynchedL1Block.getAsync()) ?? 0n;
53
- if (lastL1BlockNumber >= messages.lastProcessedL1BlockNumber) {
54
- return false;
55
- }
56
-
57
- await this.#lastSynchedL1Block.set(messages.lastProcessedL1BlockNumber);
58
-
59
- for (const message of messages.retrievedData) {
60
- const key = `${message.index}`;
61
- await this.#l1ToL2Messages.set(key, message.leaf.toBuffer());
62
- await this.#l1ToL2MessageIndices.set(message.leaf.toString(), message.index);
63
- }
64
-
65
- const lastTotalMessageCount = await this.getTotalL1ToL2MessageCount();
66
- await this.#totalMessageCount.set(lastTotalMessageCount + BigInt(messages.retrievedData.length));
67
-
68
- return true;
69
- });
70
- }
71
-
72
- /**
73
- * Gets the L1 to L2 message index in the L1 to L2 message tree.
74
- * @param l1ToL2Message - The L1 to L2 message.
75
- * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
76
- */
77
- getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
78
- return this.#l1ToL2MessageIndices.getAsync(l1ToL2Message.toString());
79
- }
80
-
81
- async getL1ToL2Messages(blockNumber: bigint): Promise<Fr[]> {
82
- const messages: Fr[] = [];
83
- let undefinedMessageFound = false;
84
- const startIndex = Number(InboxLeaf.smallestIndexFromL2Block(blockNumber));
85
- for (let i = startIndex; i < startIndex + this.#l1ToL2MessagesSubtreeSize; i++) {
86
- // This is inefficient but probably fine for now.
87
- const key = `${i}`;
88
- const message = await this.#l1ToL2Messages.getAsync(key);
89
- if (message) {
90
- if (undefinedMessageFound) {
91
- throw new Error(`L1 to L2 message gap found in block ${blockNumber}`);
92
- }
93
- messages.push(Fr.fromBuffer(message));
94
- } else {
95
- undefinedMessageFound = true;
96
- // We continue iterating over messages here to verify that there are no more messages after the undefined one.
97
- // --> If this was the case this would imply there is some issue with log fetching.
98
- }
99
- }
100
- return messages;
101
- }
102
- }
@@ -1,97 +0,0 @@
1
- import { MAX_NULLIFIERS_PER_TX } from '@aztec/constants';
2
- import type { Fr } from '@aztec/foundation/fields';
3
- import { createLogger } from '@aztec/foundation/log';
4
- import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
5
- import type { InBlock, L2Block } from '@aztec/stdlib/block';
6
-
7
- export class NullifierStore {
8
- #nullifiersToBlockNumber: AztecAsyncMap<string, number>;
9
- #nullifiersToBlockHash: AztecAsyncMap<string, string>;
10
- #nullifiersToIndex: AztecAsyncMap<string, number>;
11
- #log = createLogger('archiver:log_store');
12
-
13
- constructor(private db: AztecAsyncKVStore) {
14
- this.#nullifiersToBlockNumber = db.openMap('archiver_nullifiers_to_block_number');
15
- this.#nullifiersToBlockHash = db.openMap('archiver_nullifiers_to_block_hash');
16
- this.#nullifiersToIndex = db.openMap('archiver_nullifiers_to_index');
17
- }
18
-
19
- async addNullifiers(blocks: L2Block[]): Promise<boolean> {
20
- const blockHashes = await Promise.all(blocks.map(block => block.hash()));
21
- await this.db.transactionAsync(async () => {
22
- await Promise.all(
23
- blocks.map((block, i) => {
24
- const dataStartIndexForBlock =
25
- block.header.state.partial.nullifierTree.nextAvailableLeafIndex -
26
- block.body.txEffects.length * MAX_NULLIFIERS_PER_TX;
27
- return Promise.all(
28
- block.body.txEffects.map((txEffects, txIndex) => {
29
- const dataStartIndexForTx = dataStartIndexForBlock + txIndex * MAX_NULLIFIERS_PER_TX;
30
- return Promise.all(
31
- txEffects.nullifiers.map(async (nullifier, nullifierIndex) => {
32
- await this.#nullifiersToBlockNumber.set(nullifier.toString(), block.number);
33
- await this.#nullifiersToBlockHash.set(nullifier.toString(), blockHashes[i].toString());
34
- await this.#nullifiersToIndex.set(nullifier.toString(), dataStartIndexForTx + nullifierIndex);
35
- }),
36
- );
37
- }),
38
- );
39
- }),
40
- );
41
- });
42
- return true;
43
- }
44
-
45
- async deleteNullifiers(blocks: L2Block[]): Promise<boolean> {
46
- await this.db.transactionAsync(async () => {
47
- for (const block of blocks) {
48
- for (const nullifier of block.body.txEffects.flatMap(tx => tx.nullifiers)) {
49
- await Promise.all([
50
- this.#nullifiersToBlockNumber.delete(nullifier.toString()),
51
- this.#nullifiersToBlockHash.delete(nullifier.toString()),
52
- this.#nullifiersToIndex.delete(nullifier.toString()),
53
- ]);
54
- }
55
- }
56
- });
57
- return true;
58
- }
59
-
60
- async findNullifiersIndexesWithBlock(
61
- blockNumber: number,
62
- nullifiers: Fr[],
63
- ): Promise<(InBlock<bigint> | undefined)[]> {
64
- const asStrings = nullifiers.map(x => x.toString());
65
-
66
- const maybeNullifiers = await Promise.all(
67
- asStrings.map(async nullifier => {
68
- const [data, l2BlockNumber, l2BlockHash] = await Promise.all([
69
- this.#nullifiersToIndex.getAsync(nullifier),
70
- this.#nullifiersToBlockNumber.getAsync(nullifier),
71
- this.#nullifiersToBlockHash.getAsync(nullifier),
72
- ]);
73
- return {
74
- data,
75
- l2BlockNumber,
76
- l2BlockHash,
77
- };
78
- }),
79
- );
80
- return maybeNullifiers.map(({ data, l2BlockNumber, l2BlockHash }) => {
81
- if (
82
- data === undefined ||
83
- l2BlockNumber === undefined ||
84
- l2BlockHash === undefined ||
85
- l2BlockNumber > blockNumber
86
- ) {
87
- return undefined;
88
- } else {
89
- return {
90
- data: BigInt(data),
91
- l2BlockNumber,
92
- l2BlockHash,
93
- } as InBlock<bigint>;
94
- }
95
- });
96
- }
97
- }