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

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