@aztec/archiver 0.0.1-commit.fce3e4f → 0.0.1-commit.ff7989d6c

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 (209) hide show
  1. package/README.md +156 -22
  2. package/dest/archiver.d.ts +139 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +699 -0
  5. package/dest/config.d.ts +30 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/{archiver/config.js → config.js} +21 -5
  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 +9 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +97 -13
  14. package/dest/index.d.ts +11 -4
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +9 -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 +148 -0
  23. package/dest/l1/calldata_retriever.d.ts +118 -0
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/l1/calldata_retriever.js +484 -0
  26. package/dest/l1/data_retrieval.d.ts +89 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/{archiver → l1}/data_retrieval.js +80 -153
  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 +89 -0
  45. package/dest/modules/data_source_base.d.ts.map +1 -0
  46. package/dest/modules/data_source_base.js +216 -0
  47. package/dest/modules/data_store_updater.d.ts +80 -0
  48. package/dest/modules/data_store_updater.d.ts.map +1 -0
  49. package/dest/modules/data_store_updater.js +323 -0
  50. package/dest/modules/instrumentation.d.ts +39 -0
  51. package/dest/modules/instrumentation.d.ts.map +1 -0
  52. package/dest/{archiver → modules}/instrumentation.js +39 -62
  53. package/dest/modules/l1_synchronizer.d.ts +76 -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/{archiver → modules}/validation.js +7 -1
  59. package/dest/store/block_store.d.ts +196 -0
  60. package/dest/store/block_store.d.ts.map +1 -0
  61. package/dest/store/block_store.js +773 -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 +13 -9
  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/{archiver/kv_archiver_store → store}/contract_instance_store.js +1 -1
  68. package/dest/store/kv_archiver_store.d.ts +354 -0
  69. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  70. package/dest/store/kv_archiver_store.js +464 -0
  71. package/dest/store/l2_tips_cache.d.ts +19 -0
  72. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  73. package/dest/store/l2_tips_cache.js +89 -0
  74. package/dest/store/log_store.d.ts +54 -0
  75. package/dest/store/log_store.d.ts.map +1 -0
  76. package/dest/store/log_store.js +456 -0
  77. package/dest/store/message_store.d.ts +40 -0
  78. package/dest/store/message_store.d.ts.map +1 -0
  79. package/dest/{archiver/kv_archiver_store → store}/message_store.js +15 -14
  80. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  81. package/dest/structs/data_retrieval.d.ts.map +1 -0
  82. package/dest/structs/inbox_message.d.ts +15 -0
  83. package/dest/structs/inbox_message.d.ts.map +1 -0
  84. package/dest/{archiver/structs → structs}/inbox_message.js +6 -5
  85. package/dest/structs/published.d.ts +2 -0
  86. package/dest/structs/published.d.ts.map +1 -0
  87. package/dest/test/fake_l1_state.d.ts +193 -0
  88. package/dest/test/fake_l1_state.d.ts.map +1 -0
  89. package/dest/test/fake_l1_state.js +389 -0
  90. package/dest/test/index.d.ts +2 -1
  91. package/dest/test/index.d.ts.map +1 -1
  92. package/dest/test/index.js +4 -1
  93. package/dest/test/mock_archiver.d.ts +16 -8
  94. package/dest/test/mock_archiver.d.ts.map +1 -1
  95. package/dest/test/mock_archiver.js +19 -14
  96. package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
  97. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  98. package/dest/test/mock_l1_to_l2_message_source.js +21 -11
  99. package/dest/test/mock_l2_block_source.d.ts +51 -18
  100. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  101. package/dest/test/mock_l2_block_source.js +245 -82
  102. package/dest/test/mock_structs.d.ts +80 -4
  103. package/dest/test/mock_structs.d.ts.map +1 -1
  104. package/dest/test/mock_structs.js +145 -11
  105. package/dest/test/noop_l1_archiver.d.ts +23 -0
  106. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  107. package/dest/test/noop_l1_archiver.js +68 -0
  108. package/package.json +20 -20
  109. package/src/archiver.ts +443 -0
  110. package/src/{archiver/config.ts → config.ts} +28 -12
  111. package/src/errors.ts +102 -0
  112. package/src/factory.ts +143 -13
  113. package/src/index.ts +11 -3
  114. package/src/interfaces.ts +9 -0
  115. package/src/l1/README.md +98 -0
  116. package/src/l1/bin/retrieve-calldata.ts +181 -0
  117. package/src/l1/calldata_retriever.ts +663 -0
  118. package/src/{archiver → l1}/data_retrieval.ts +146 -224
  119. package/src/l1/debug_tx.ts +99 -0
  120. package/src/l1/spire_proposer.ts +160 -0
  121. package/src/l1/trace_tx.ts +128 -0
  122. package/src/l1/types.ts +13 -0
  123. package/src/l1/validate_trace.ts +229 -0
  124. package/src/modules/data_source_base.ts +328 -0
  125. package/src/modules/data_store_updater.ts +448 -0
  126. package/src/{archiver → modules}/instrumentation.ts +43 -66
  127. package/src/modules/l1_synchronizer.ts +932 -0
  128. package/src/{archiver → modules}/validation.ts +11 -6
  129. package/src/store/block_store.ts +1015 -0
  130. package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +13 -9
  131. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +2 -2
  132. package/src/store/kv_archiver_store.ts +671 -0
  133. package/src/store/l2_tips_cache.ts +89 -0
  134. package/src/store/log_store.ts +637 -0
  135. package/src/{archiver/kv_archiver_store → store}/message_store.ts +21 -18
  136. package/src/{archiver/structs → structs}/inbox_message.ts +8 -8
  137. package/src/{archiver/structs → structs}/published.ts +0 -1
  138. package/src/test/fake_l1_state.ts +607 -0
  139. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  140. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  141. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  142. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  143. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  144. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  145. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  146. package/src/test/index.ts +4 -0
  147. package/src/test/mock_archiver.ts +23 -16
  148. package/src/test/mock_l1_to_l2_message_source.ts +18 -11
  149. package/src/test/mock_l2_block_source.ts +296 -90
  150. package/src/test/mock_structs.ts +275 -13
  151. package/src/test/noop_l1_archiver.ts +109 -0
  152. package/dest/archiver/archiver.d.ts +0 -287
  153. package/dest/archiver/archiver.d.ts.map +0 -1
  154. package/dest/archiver/archiver.js +0 -1408
  155. package/dest/archiver/archiver_store.d.ts +0 -255
  156. package/dest/archiver/archiver_store.d.ts.map +0 -1
  157. package/dest/archiver/archiver_store.js +0 -4
  158. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  159. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  160. package/dest/archiver/archiver_store_test_suite.js +0 -1289
  161. package/dest/archiver/config.d.ts +0 -21
  162. package/dest/archiver/config.d.ts.map +0 -1
  163. package/dest/archiver/data_retrieval.d.ts +0 -79
  164. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  165. package/dest/archiver/errors.d.ts +0 -12
  166. package/dest/archiver/errors.d.ts.map +0 -1
  167. package/dest/archiver/errors.js +0 -17
  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 -35
  172. package/dest/archiver/instrumentation.d.ts.map +0 -1
  173. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -124
  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 -370
  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 -24
  179. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  180. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -168
  181. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  182. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -296
  183. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
  184. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  185. package/dest/archiver/kv_archiver_store/log_store.js +0 -336
  186. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -39
  187. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  188. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  189. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  190. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  191. package/dest/archiver/structs/published.d.ts +0 -3
  192. package/dest/archiver/structs/published.d.ts.map +0 -1
  193. package/dest/archiver/validation.d.ts +0 -17
  194. package/dest/archiver/validation.d.ts.map +0 -1
  195. package/dest/rpc/index.d.ts +0 -9
  196. package/dest/rpc/index.d.ts.map +0 -1
  197. package/dest/rpc/index.js +0 -15
  198. package/src/archiver/archiver.ts +0 -1858
  199. package/src/archiver/archiver_store.ts +0 -305
  200. package/src/archiver/archiver_store_test_suite.ts +0 -1264
  201. package/src/archiver/errors.ts +0 -26
  202. package/src/archiver/index.ts +0 -6
  203. package/src/archiver/kv_archiver_store/block_store.ts +0 -481
  204. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -422
  205. package/src/archiver/kv_archiver_store/log_store.ts +0 -406
  206. package/src/rpc/index.ts +0 -16
  207. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  208. /package/dest/{archiver/structs → structs}/published.js +0 -0
  209. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
@@ -0,0 +1,637 @@
1
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { BlockNumber } from '@aztec/foundation/branded-types';
3
+ import { filterAsync } from '@aztec/foundation/collection';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
5
+ import { createLogger } from '@aztec/foundation/log';
6
+ import { BufferReader, numToUInt32BE } from '@aztec/foundation/serialize';
7
+ import type { AztecAsyncKVStore, AztecAsyncMap } from '@aztec/kv-store';
8
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
9
+ import { BlockHash, L2Block } from '@aztec/stdlib/block';
10
+ import { MAX_LOGS_PER_TAG } from '@aztec/stdlib/interfaces/api-limit';
11
+ import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
12
+ import {
13
+ ContractClassLog,
14
+ ExtendedContractClassLog,
15
+ ExtendedPublicLog,
16
+ type LogFilter,
17
+ LogId,
18
+ PublicLog,
19
+ type SiloedTag,
20
+ Tag,
21
+ TxScopedL2Log,
22
+ } from '@aztec/stdlib/logs';
23
+ import { TxHash } from '@aztec/stdlib/tx';
24
+
25
+ import type { BlockStore } from './block_store.js';
26
+
27
+ /**
28
+ * A store for logs
29
+ */
30
+ export class LogStore {
31
+ // `tag` --> private logs
32
+ #privateLogsByTag: AztecAsyncMap<string, Buffer[]>;
33
+ // `{contractAddress}_${tag}` --> public logs
34
+ #publicLogsByContractAndTag: AztecAsyncMap<string, Buffer[]>;
35
+ #privateLogKeysByBlock: AztecAsyncMap<number, string[]>;
36
+ #publicLogKeysByBlock: AztecAsyncMap<number, string[]>;
37
+ #publicLogsByBlock: AztecAsyncMap<number, Buffer>;
38
+ #contractClassLogsByBlock: AztecAsyncMap<number, Buffer>;
39
+ #logsMaxPageSize: number;
40
+ #log = createLogger('archiver:log_store');
41
+
42
+ constructor(
43
+ private db: AztecAsyncKVStore,
44
+ private blockStore: BlockStore,
45
+ logsMaxPageSize: number = 1000,
46
+ ) {
47
+ this.#privateLogsByTag = db.openMap('archiver_private_tagged_logs_by_tag');
48
+ this.#publicLogsByContractAndTag = db.openMap('archiver_public_tagged_logs_by_tag');
49
+ this.#privateLogKeysByBlock = db.openMap('archiver_private_log_keys_by_block');
50
+ this.#publicLogKeysByBlock = db.openMap('archiver_public_log_keys_by_block');
51
+ this.#publicLogsByBlock = db.openMap('archiver_public_logs_by_block');
52
+ this.#contractClassLogsByBlock = db.openMap('archiver_contract_class_logs_by_block');
53
+
54
+ this.#logsMaxPageSize = logsMaxPageSize;
55
+ }
56
+
57
+ /**
58
+ * Extracts tagged logs from a single block, grouping them into private and public maps.
59
+ *
60
+ * @param block - The L2 block to extract logs from.
61
+ * @returns An object containing the private and public tagged logs for the block.
62
+ */
63
+ #extractTaggedLogsFromBlock(block: L2Block) {
64
+ // SiloedTag (as string) -> array of log buffers.
65
+ const privateTaggedLogs = new Map<string, Buffer[]>();
66
+ // "{contractAddress}_{tag}" (as string) -> array of log buffers.
67
+ const publicTaggedLogs = new Map<string, Buffer[]>();
68
+
69
+ block.body.txEffects.forEach(txEffect => {
70
+ const txHash = txEffect.txHash;
71
+
72
+ txEffect.privateLogs.forEach(log => {
73
+ // Private logs use SiloedTag (already siloed by kernel)
74
+ const tag = log.fields[0];
75
+ this.#log.debug(`Found private log with tag ${tag.toString()} in block ${block.number}`);
76
+
77
+ const currentLogs = privateTaggedLogs.get(tag.toString()) ?? [];
78
+ currentLogs.push(
79
+ new TxScopedL2Log(
80
+ txHash,
81
+ block.number,
82
+ block.timestamp,
83
+ log.getEmittedFields(),
84
+ txEffect.noteHashes,
85
+ txEffect.nullifiers[0],
86
+ ).toBuffer(),
87
+ );
88
+ privateTaggedLogs.set(tag.toString(), currentLogs);
89
+ });
90
+
91
+ txEffect.publicLogs.forEach(log => {
92
+ // Public logs use Tag directly (not siloed) and are stored with contract address
93
+ const tag = log.fields[0];
94
+ const contractAddress = log.contractAddress;
95
+ const key = `${contractAddress.toString()}_${tag.toString()}`;
96
+ this.#log.debug(
97
+ `Found public log with tag ${tag.toString()} from contract ${contractAddress.toString()} in block ${block.number}`,
98
+ );
99
+
100
+ const currentLogs = publicTaggedLogs.get(key) ?? [];
101
+ currentLogs.push(
102
+ new TxScopedL2Log(
103
+ txHash,
104
+ block.number,
105
+ block.timestamp,
106
+ log.getEmittedFields(),
107
+ txEffect.noteHashes,
108
+ txEffect.nullifiers[0],
109
+ ).toBuffer(),
110
+ );
111
+ publicTaggedLogs.set(key, currentLogs);
112
+ });
113
+ });
114
+
115
+ return { privateTaggedLogs, publicTaggedLogs };
116
+ }
117
+
118
+ /**
119
+ * Extracts and aggregates tagged logs from a list of blocks.
120
+ * @param blocks - The blocks to extract logs from.
121
+ * @returns A map from tag (as string) to an array of serialized private logs belonging to that tag, and a map from
122
+ * "{contractAddress}_{tag}" (as string) to an array of serialized public logs belonging to that key.
123
+ */
124
+ #extractTaggedLogs(blocks: L2Block[]): {
125
+ privateTaggedLogs: Map<string, Buffer[]>;
126
+ publicTaggedLogs: Map<string, Buffer[]>;
127
+ } {
128
+ const taggedLogsInBlocks = blocks.map(block => this.#extractTaggedLogsFromBlock(block));
129
+
130
+ // Now we merge the maps from each block into a single map.
131
+ const privateTaggedLogs = taggedLogsInBlocks.reduce((acc, { privateTaggedLogs }) => {
132
+ for (const [tag, logs] of privateTaggedLogs.entries()) {
133
+ const currentLogs = acc.get(tag) ?? [];
134
+ acc.set(tag, currentLogs.concat(logs));
135
+ }
136
+ return acc;
137
+ }, new Map<string, Buffer[]>());
138
+
139
+ const publicTaggedLogs = taggedLogsInBlocks.reduce((acc, { publicTaggedLogs }) => {
140
+ for (const [key, logs] of publicTaggedLogs.entries()) {
141
+ const currentLogs = acc.get(key) ?? [];
142
+ acc.set(key, currentLogs.concat(logs));
143
+ }
144
+ return acc;
145
+ }, new Map<string, Buffer[]>());
146
+
147
+ return { privateTaggedLogs, publicTaggedLogs };
148
+ }
149
+
150
+ async #addPrivateLogs(blocks: L2Block[]): Promise<void> {
151
+ const newBlocks = await filterAsync(
152
+ blocks,
153
+ async block => !(await this.#privateLogKeysByBlock.hasAsync(block.number)),
154
+ );
155
+
156
+ const { privateTaggedLogs } = this.#extractTaggedLogs(newBlocks);
157
+ const keysOfPrivateLogsToUpdate = Array.from(privateTaggedLogs.keys());
158
+
159
+ const currentPrivateTaggedLogs = await Promise.all(
160
+ keysOfPrivateLogsToUpdate.map(async key => ({
161
+ tag: key,
162
+ logBuffers: await this.#privateLogsByTag.getAsync(key),
163
+ })),
164
+ );
165
+
166
+ for (const taggedLogBuffer of currentPrivateTaggedLogs) {
167
+ if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
168
+ privateTaggedLogs.set(
169
+ taggedLogBuffer.tag,
170
+ taggedLogBuffer.logBuffers!.concat(privateTaggedLogs.get(taggedLogBuffer.tag)!),
171
+ );
172
+ }
173
+ }
174
+
175
+ for (const block of newBlocks) {
176
+ const privateTagsInBlock: string[] = [];
177
+ for (const [tag, logs] of privateTaggedLogs.entries()) {
178
+ await this.#privateLogsByTag.set(tag, logs);
179
+ privateTagsInBlock.push(tag);
180
+ }
181
+ await this.#privateLogKeysByBlock.set(block.number, privateTagsInBlock);
182
+ }
183
+ }
184
+
185
+ async #addPublicLogs(blocks: L2Block[]): Promise<void> {
186
+ const newBlocks = await filterAsync(
187
+ blocks,
188
+ async block => !(await this.#publicLogKeysByBlock.hasAsync(block.number)),
189
+ );
190
+
191
+ const { publicTaggedLogs } = this.#extractTaggedLogs(newBlocks);
192
+ const keysOfPublicLogsToUpdate = Array.from(publicTaggedLogs.keys());
193
+
194
+ const currentPublicTaggedLogs = await Promise.all(
195
+ keysOfPublicLogsToUpdate.map(async key => ({
196
+ tag: key,
197
+ logBuffers: await this.#publicLogsByContractAndTag.getAsync(key),
198
+ })),
199
+ );
200
+
201
+ for (const taggedLogBuffer of currentPublicTaggedLogs) {
202
+ if (taggedLogBuffer.logBuffers && taggedLogBuffer.logBuffers.length > 0) {
203
+ publicTaggedLogs.set(
204
+ taggedLogBuffer.tag,
205
+ taggedLogBuffer.logBuffers!.concat(publicTaggedLogs.get(taggedLogBuffer.tag)!),
206
+ );
207
+ }
208
+ }
209
+
210
+ for (const block of newBlocks) {
211
+ const blockHash = await block.hash();
212
+ const publicTagsInBlock: string[] = [];
213
+ for (const [tag, logs] of publicTaggedLogs.entries()) {
214
+ await this.#publicLogsByContractAndTag.set(tag, logs);
215
+ publicTagsInBlock.push(tag);
216
+ }
217
+ await this.#publicLogKeysByBlock.set(block.number, publicTagsInBlock);
218
+
219
+ const publicLogsInBlock = block.body.txEffects
220
+ .map((txEffect, txIndex) =>
221
+ [
222
+ numToUInt32BE(txIndex),
223
+ txEffect.txHash.toBuffer(),
224
+ numToUInt32BE(txEffect.publicLogs.length),
225
+ txEffect.publicLogs.map(log => log.toBuffer()),
226
+ ].flat(),
227
+ )
228
+ .flat();
229
+
230
+ await this.#publicLogsByBlock.set(block.number, this.#packWithBlockHash(blockHash, publicLogsInBlock));
231
+ }
232
+ }
233
+
234
+ async #addContractClassLogs(blocks: L2Block[]): Promise<void> {
235
+ const newBlocks = await filterAsync(
236
+ blocks,
237
+ async block => !(await this.#contractClassLogsByBlock.hasAsync(block.number)),
238
+ );
239
+
240
+ for (const block of newBlocks) {
241
+ const blockHash = await block.hash();
242
+
243
+ const contractClassLogsInBlock = block.body.txEffects
244
+ .map((txEffect, txIndex) =>
245
+ [
246
+ numToUInt32BE(txIndex),
247
+ txEffect.txHash.toBuffer(),
248
+ numToUInt32BE(txEffect.contractClassLogs.length),
249
+ txEffect.contractClassLogs.map(log => log.toBuffer()),
250
+ ].flat(),
251
+ )
252
+ .flat();
253
+
254
+ await this.#contractClassLogsByBlock.set(
255
+ block.number,
256
+ this.#packWithBlockHash(blockHash, contractClassLogsInBlock),
257
+ );
258
+ }
259
+ }
260
+
261
+ /**
262
+ * Append new logs to the store's list.
263
+ * @param blocks - The blocks for which to add the logs.
264
+ * @returns True if the operation is successful.
265
+ */
266
+ addLogs(blocks: L2Block[]): Promise<boolean> {
267
+ return this.db.transactionAsync(async () => {
268
+ await Promise.all([
269
+ this.#addPrivateLogs(blocks),
270
+ this.#addPublicLogs(blocks),
271
+ this.#addContractClassLogs(blocks),
272
+ ]);
273
+ return true;
274
+ });
275
+ }
276
+
277
+ #packWithBlockHash(blockHash: BlockHash, data: Buffer<ArrayBufferLike>[]): Buffer<ArrayBufferLike> {
278
+ return Buffer.concat([blockHash.toBuffer(), ...data]);
279
+ }
280
+
281
+ #unpackBlockHash(reader: BufferReader): BlockHash {
282
+ const blockHash = reader.remainingBytes() > 0 ? reader.readObject(Fr) : undefined;
283
+
284
+ if (!blockHash) {
285
+ throw new Error('Failed to read block hash from log entry buffer');
286
+ }
287
+
288
+ return new BlockHash(blockHash);
289
+ }
290
+
291
+ deleteLogs(blocks: L2Block[]): Promise<boolean> {
292
+ return this.db.transactionAsync(async () => {
293
+ await Promise.all(
294
+ blocks.map(async block => {
295
+ // Delete private logs
296
+ const privateKeys = (await this.#privateLogKeysByBlock.getAsync(block.number)) ?? [];
297
+ await Promise.all(privateKeys.map(tag => this.#privateLogsByTag.delete(tag)));
298
+
299
+ // Delete public logs
300
+ const publicKeys = (await this.#publicLogKeysByBlock.getAsync(block.number)) ?? [];
301
+ await Promise.all(publicKeys.map(key => this.#publicLogsByContractAndTag.delete(key)));
302
+ }),
303
+ );
304
+
305
+ await Promise.all(
306
+ blocks.map(block =>
307
+ Promise.all([
308
+ this.#publicLogsByBlock.delete(block.number),
309
+ this.#privateLogKeysByBlock.delete(block.number),
310
+ this.#publicLogKeysByBlock.delete(block.number),
311
+ this.#contractClassLogsByBlock.delete(block.number),
312
+ ]),
313
+ ),
314
+ );
315
+
316
+ return true;
317
+ });
318
+ }
319
+
320
+ /**
321
+ * Gets private logs that match any of the `tags`. For each tag, an array of matching logs is returned. An empty
322
+ * array implies no logs match that tag.
323
+ * @param tags - The tags to search for.
324
+ * @param page - The page number (0-indexed) for pagination.
325
+ * @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
326
+ * MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
327
+ */
328
+ async getPrivateLogsByTags(tags: SiloedTag[], page: number = 0): Promise<TxScopedL2Log[][]> {
329
+ const logs = await Promise.all(tags.map(tag => this.#privateLogsByTag.getAsync(tag.toString())));
330
+ const start = page * MAX_LOGS_PER_TAG;
331
+ const end = start + MAX_LOGS_PER_TAG;
332
+
333
+ return logs.map(
334
+ logBuffers => logBuffers?.slice(start, end).map(logBuffer => TxScopedL2Log.fromBuffer(logBuffer)) ?? [],
335
+ );
336
+ }
337
+
338
+ /**
339
+ * Gets public logs that match any of the `tags` from the specified contract. For each tag, an array of matching
340
+ * logs is returned. An empty array implies no logs match that tag.
341
+ * @param contractAddress - The contract address to search logs for.
342
+ * @param tags - The tags to search for.
343
+ * @param page - The page number (0-indexed) for pagination.
344
+ * @returns An array of log arrays, one per tag. Returns at most MAX_LOGS_PER_TAG logs per tag per page. If
345
+ * MAX_LOGS_PER_TAG logs are returned for a tag, the caller should fetch the next page to check for more logs.
346
+ */
347
+ async getPublicLogsByTagsFromContract(
348
+ contractAddress: AztecAddress,
349
+ tags: Tag[],
350
+ page: number = 0,
351
+ ): Promise<TxScopedL2Log[][]> {
352
+ const logs = await Promise.all(
353
+ tags.map(tag => {
354
+ const key = `${contractAddress.toString()}_${tag.value.toString()}`;
355
+ return this.#publicLogsByContractAndTag.getAsync(key);
356
+ }),
357
+ );
358
+ const start = page * MAX_LOGS_PER_TAG;
359
+ const end = start + MAX_LOGS_PER_TAG;
360
+
361
+ return logs.map(
362
+ logBuffers => logBuffers?.slice(start, end).map(logBuffer => TxScopedL2Log.fromBuffer(logBuffer)) ?? [],
363
+ );
364
+ }
365
+
366
+ /**
367
+ * Gets public logs based on the provided filter.
368
+ * @param filter - The filter to apply to the logs.
369
+ * @returns The requested logs.
370
+ */
371
+ getPublicLogs(filter: LogFilter): Promise<GetPublicLogsResponse> {
372
+ if (filter.afterLog) {
373
+ return this.#filterPublicLogsBetweenBlocks(filter);
374
+ } else if (filter.txHash) {
375
+ return this.#filterPublicLogsOfTx(filter);
376
+ } else {
377
+ return this.#filterPublicLogsBetweenBlocks(filter);
378
+ }
379
+ }
380
+
381
+ async #filterPublicLogsOfTx(filter: LogFilter): Promise<GetPublicLogsResponse> {
382
+ if (!filter.txHash) {
383
+ throw new Error('Missing txHash');
384
+ }
385
+
386
+ const [blockNumber, txIndex] = (await this.blockStore.getTxLocation(filter.txHash)) ?? [];
387
+ if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
388
+ return { logs: [], maxLogsHit: false };
389
+ }
390
+
391
+ const buffer = (await this.#publicLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
392
+ const publicLogsInBlock: { txHash: TxHash; logs: PublicLog[] }[] = [];
393
+ const reader = new BufferReader(buffer);
394
+
395
+ const blockHash = this.#unpackBlockHash(reader);
396
+
397
+ while (reader.remainingBytes() > 0) {
398
+ const indexOfTx = reader.readNumber();
399
+ const txHash = reader.readObject(TxHash);
400
+ const numLogsInTx = reader.readNumber();
401
+ publicLogsInBlock[indexOfTx] = { txHash, logs: [] };
402
+ for (let i = 0; i < numLogsInTx; i++) {
403
+ publicLogsInBlock[indexOfTx].logs.push(reader.readObject(PublicLog));
404
+ }
405
+ }
406
+
407
+ const txData = publicLogsInBlock[txIndex];
408
+
409
+ const logs: ExtendedPublicLog[] = [];
410
+ const maxLogsHit = this.#accumulatePublicLogs(
411
+ logs,
412
+ blockNumber,
413
+ blockHash,
414
+ txIndex,
415
+ txData.txHash,
416
+ txData.logs,
417
+ filter,
418
+ );
419
+
420
+ return { logs, maxLogsHit };
421
+ }
422
+
423
+ async #filterPublicLogsBetweenBlocks(filter: LogFilter): Promise<GetPublicLogsResponse> {
424
+ const start =
425
+ filter.afterLog?.blockNumber ?? Math.max(filter.fromBlock ?? INITIAL_L2_BLOCK_NUM, INITIAL_L2_BLOCK_NUM);
426
+ const end = filter.toBlock;
427
+
428
+ if (typeof end === 'number' && end < start) {
429
+ return {
430
+ logs: [],
431
+ maxLogsHit: true,
432
+ };
433
+ }
434
+
435
+ const logs: ExtendedPublicLog[] = [];
436
+
437
+ let maxLogsHit = false;
438
+ loopOverBlocks: for await (const [blockNumber, logBuffer] of this.#publicLogsByBlock.entriesAsync({ start, end })) {
439
+ const publicLogsInBlock: { txHash: TxHash; logs: PublicLog[] }[] = [];
440
+ const reader = new BufferReader(logBuffer);
441
+
442
+ const blockHash = this.#unpackBlockHash(reader);
443
+
444
+ while (reader.remainingBytes() > 0) {
445
+ const indexOfTx = reader.readNumber();
446
+ const txHash = reader.readObject(TxHash);
447
+ const numLogsInTx = reader.readNumber();
448
+ publicLogsInBlock[indexOfTx] = { txHash, logs: [] };
449
+ for (let i = 0; i < numLogsInTx; i++) {
450
+ publicLogsInBlock[indexOfTx].logs.push(reader.readObject(PublicLog));
451
+ }
452
+ }
453
+ for (let txIndex = filter.afterLog?.txIndex ?? 0; txIndex < publicLogsInBlock.length; txIndex++) {
454
+ const txData = publicLogsInBlock[txIndex];
455
+ maxLogsHit = this.#accumulatePublicLogs(
456
+ logs,
457
+ blockNumber,
458
+ blockHash,
459
+ txIndex,
460
+ txData.txHash,
461
+ txData.logs,
462
+ filter,
463
+ );
464
+ if (maxLogsHit) {
465
+ this.#log.debug(`Max logs hit at block ${blockNumber}`);
466
+ break loopOverBlocks;
467
+ }
468
+ }
469
+ }
470
+
471
+ return { logs, maxLogsHit };
472
+ }
473
+
474
+ /**
475
+ * Gets contract class logs based on the provided filter.
476
+ * @param filter - The filter to apply to the logs.
477
+ * @returns The requested logs.
478
+ */
479
+ getContractClassLogs(filter: LogFilter): Promise<GetContractClassLogsResponse> {
480
+ if (filter.afterLog) {
481
+ return this.#filterContractClassLogsBetweenBlocks(filter);
482
+ } else if (filter.txHash) {
483
+ return this.#filterContractClassLogsOfTx(filter);
484
+ } else {
485
+ return this.#filterContractClassLogsBetweenBlocks(filter);
486
+ }
487
+ }
488
+
489
+ async #filterContractClassLogsOfTx(filter: LogFilter): Promise<GetContractClassLogsResponse> {
490
+ if (!filter.txHash) {
491
+ throw new Error('Missing txHash');
492
+ }
493
+
494
+ const [blockNumber, txIndex] = (await this.blockStore.getTxLocation(filter.txHash)) ?? [];
495
+ if (typeof blockNumber !== 'number' || typeof txIndex !== 'number') {
496
+ return { logs: [], maxLogsHit: false };
497
+ }
498
+ const contractClassLogsBuffer = (await this.#contractClassLogsByBlock.getAsync(blockNumber)) ?? Buffer.alloc(0);
499
+ const contractClassLogsInBlock: { txHash: TxHash; logs: ContractClassLog[] }[] = [];
500
+
501
+ const reader = new BufferReader(contractClassLogsBuffer);
502
+ const blockHash = this.#unpackBlockHash(reader);
503
+
504
+ while (reader.remainingBytes() > 0) {
505
+ const indexOfTx = reader.readNumber();
506
+ const txHash = reader.readObject(TxHash);
507
+ const numLogsInTx = reader.readNumber();
508
+ contractClassLogsInBlock[indexOfTx] = { txHash, logs: [] };
509
+ for (let i = 0; i < numLogsInTx; i++) {
510
+ contractClassLogsInBlock[indexOfTx].logs.push(reader.readObject(ContractClassLog));
511
+ }
512
+ }
513
+
514
+ const txData = contractClassLogsInBlock[txIndex];
515
+
516
+ const logs: ExtendedContractClassLog[] = [];
517
+ const maxLogsHit = this.#accumulateContractClassLogs(
518
+ logs,
519
+ blockNumber,
520
+ blockHash,
521
+ txIndex,
522
+ txData.txHash,
523
+ txData.logs,
524
+ filter,
525
+ );
526
+
527
+ return { logs, maxLogsHit };
528
+ }
529
+
530
+ async #filterContractClassLogsBetweenBlocks(filter: LogFilter): Promise<GetContractClassLogsResponse> {
531
+ const start =
532
+ filter.afterLog?.blockNumber ?? Math.max(filter.fromBlock ?? INITIAL_L2_BLOCK_NUM, INITIAL_L2_BLOCK_NUM);
533
+ const end = filter.toBlock;
534
+
535
+ if (typeof end === 'number' && end < start) {
536
+ return {
537
+ logs: [],
538
+ maxLogsHit: true,
539
+ };
540
+ }
541
+
542
+ const logs: ExtendedContractClassLog[] = [];
543
+
544
+ let maxLogsHit = false;
545
+ loopOverBlocks: for await (const [blockNumber, logBuffer] of this.#contractClassLogsByBlock.entriesAsync({
546
+ start,
547
+ end,
548
+ })) {
549
+ const contractClassLogsInBlock: { txHash: TxHash; logs: ContractClassLog[] }[] = [];
550
+ const reader = new BufferReader(logBuffer);
551
+ const blockHash = this.#unpackBlockHash(reader);
552
+ while (reader.remainingBytes() > 0) {
553
+ const indexOfTx = reader.readNumber();
554
+ const txHash = reader.readObject(TxHash);
555
+ const numLogsInTx = reader.readNumber();
556
+ contractClassLogsInBlock[indexOfTx] = { txHash, logs: [] };
557
+ for (let i = 0; i < numLogsInTx; i++) {
558
+ contractClassLogsInBlock[indexOfTx].logs.push(reader.readObject(ContractClassLog));
559
+ }
560
+ }
561
+ for (let txIndex = filter.afterLog?.txIndex ?? 0; txIndex < contractClassLogsInBlock.length; txIndex++) {
562
+ const txData = contractClassLogsInBlock[txIndex];
563
+ maxLogsHit = this.#accumulateContractClassLogs(
564
+ logs,
565
+ blockNumber,
566
+ blockHash,
567
+ txIndex,
568
+ txData.txHash,
569
+ txData.logs,
570
+ filter,
571
+ );
572
+ if (maxLogsHit) {
573
+ this.#log.debug(`Max logs hit at block ${blockNumber}`);
574
+ break loopOverBlocks;
575
+ }
576
+ }
577
+ }
578
+
579
+ return { logs, maxLogsHit };
580
+ }
581
+
582
+ #accumulatePublicLogs(
583
+ results: ExtendedPublicLog[],
584
+ blockNumber: number,
585
+ blockHash: BlockHash,
586
+ txIndex: number,
587
+ txHash: TxHash,
588
+ txLogs: PublicLog[],
589
+ filter: LogFilter = {},
590
+ ): boolean {
591
+ let maxLogsHit = false;
592
+ let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
593
+ for (; logIndex < txLogs.length; logIndex++) {
594
+ const log = txLogs[logIndex];
595
+ if (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) {
596
+ results.push(
597
+ new ExtendedPublicLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
598
+ );
599
+
600
+ if (results.length >= this.#logsMaxPageSize) {
601
+ maxLogsHit = true;
602
+ break;
603
+ }
604
+ }
605
+ }
606
+
607
+ return maxLogsHit;
608
+ }
609
+
610
+ #accumulateContractClassLogs(
611
+ results: ExtendedContractClassLog[],
612
+ blockNumber: number,
613
+ blockHash: BlockHash,
614
+ txIndex: number,
615
+ txHash: TxHash,
616
+ txLogs: ContractClassLog[],
617
+ filter: LogFilter = {},
618
+ ): boolean {
619
+ let maxLogsHit = false;
620
+ let logIndex = typeof filter.afterLog?.logIndex === 'number' ? filter.afterLog.logIndex + 1 : 0;
621
+ for (; logIndex < txLogs.length; logIndex++) {
622
+ const log = txLogs[logIndex];
623
+ if (!filter.contractAddress || log.contractAddress.equals(filter.contractAddress)) {
624
+ results.push(
625
+ new ExtendedContractClassLog(new LogId(BlockNumber(blockNumber), blockHash, txHash, txIndex, logIndex), log),
626
+ );
627
+
628
+ if (results.length >= this.#logsMaxPageSize) {
629
+ maxLogsHit = true;
630
+ break;
631
+ }
632
+ }
633
+ }
634
+
635
+ return maxLogsHit;
636
+ }
637
+ }