@aztec/archiver 0.0.1-commit.96bb3f7 → 0.0.1-commit.993d240

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 (232) hide show
  1. package/README.md +164 -22
  2. package/dest/archiver.d.ts +158 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +881 -0
  5. package/dest/config.d.ts +33 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/{archiver/config.js → config.js} +31 -14
  8. package/dest/errors.d.ts +87 -0
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/errors.js +129 -0
  11. package/dest/factory.d.ts +16 -10
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +112 -20
  14. package/dest/index.d.ts +19 -4
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +17 -3
  17. package/dest/interfaces.d.ts +9 -0
  18. package/dest/interfaces.d.ts.map +1 -0
  19. package/dest/interfaces.js +3 -0
  20. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.d.ts +1 -1
  21. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  22. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.js +35 -32
  23. package/dest/l1/calldata_retriever.d.ts +136 -0
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/l1/calldata_retriever.js +412 -0
  26. package/dest/l1/data_retrieval.d.ts +97 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/{archiver/l1 → l1}/data_retrieval.js +65 -89
  29. package/dest/{archiver/l1 → l1}/debug_tx.d.ts +1 -1
  30. package/dest/l1/debug_tx.d.ts.map +1 -0
  31. package/dest/{archiver/l1 → l1}/spire_proposer.d.ts +5 -5
  32. package/dest/l1/spire_proposer.d.ts.map +1 -0
  33. package/dest/{archiver/l1 → l1}/spire_proposer.js +9 -17
  34. package/dest/l1/trace_tx.d.ts +43 -0
  35. package/dest/l1/trace_tx.d.ts.map +1 -0
  36. package/dest/l1/types.d.ts +12 -0
  37. package/dest/l1/types.d.ts.map +1 -0
  38. package/dest/l1/validate_historical_logs.d.ts +23 -0
  39. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  40. package/dest/l1/validate_historical_logs.js +108 -0
  41. package/dest/{archiver/l1 → l1}/validate_trace.d.ts +6 -3
  42. package/dest/l1/validate_trace.d.ts.map +1 -0
  43. package/dest/{archiver/l1 → l1}/validate_trace.js +13 -9
  44. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  45. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  46. package/dest/modules/contract_data_source_adapter.js +40 -0
  47. package/dest/modules/data_source_base.d.ts +113 -0
  48. package/dest/modules/data_source_base.d.ts.map +1 -0
  49. package/dest/modules/data_source_base.js +351 -0
  50. package/dest/modules/data_store_updater.d.ts +105 -0
  51. package/dest/modules/data_store_updater.d.ts.map +1 -0
  52. package/dest/modules/data_store_updater.js +392 -0
  53. package/dest/modules/instrumentation.d.ts +55 -0
  54. package/dest/modules/instrumentation.d.ts.map +1 -0
  55. package/dest/{archiver → modules}/instrumentation.js +61 -19
  56. package/dest/modules/l1_synchronizer.d.ts +77 -0
  57. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  58. package/dest/modules/l1_synchronizer.js +1344 -0
  59. package/dest/modules/validation.d.ts +18 -0
  60. package/dest/modules/validation.d.ts.map +1 -0
  61. package/dest/{archiver → modules}/validation.js +12 -6
  62. package/dest/store/block_store.d.ts +300 -0
  63. package/dest/store/block_store.d.ts.map +1 -0
  64. package/dest/store/block_store.js +1219 -0
  65. package/dest/store/contract_class_store.d.ts +31 -0
  66. package/dest/store/contract_class_store.d.ts.map +1 -0
  67. package/dest/store/contract_class_store.js +80 -0
  68. package/dest/store/contract_instance_store.d.ts +51 -0
  69. package/dest/store/contract_instance_store.d.ts.map +1 -0
  70. package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +38 -3
  71. package/dest/store/data_stores.d.ts +68 -0
  72. package/dest/store/data_stores.d.ts.map +1 -0
  73. package/dest/store/data_stores.js +54 -0
  74. package/dest/store/function_names_cache.d.ts +17 -0
  75. package/dest/store/function_names_cache.d.ts.map +1 -0
  76. package/dest/store/function_names_cache.js +30 -0
  77. package/dest/store/l2_tips_cache.d.ts +25 -0
  78. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  79. package/dest/store/l2_tips_cache.js +26 -0
  80. package/dest/store/log_store.d.ts +59 -0
  81. package/dest/store/log_store.d.ts.map +1 -0
  82. package/dest/store/log_store.js +310 -0
  83. package/dest/store/log_store_codec.d.ts +70 -0
  84. package/dest/store/log_store_codec.d.ts.map +1 -0
  85. package/dest/store/log_store_codec.js +101 -0
  86. package/dest/store/message_store.d.ts +50 -0
  87. package/dest/store/message_store.d.ts.map +1 -0
  88. package/dest/{archiver/kv_archiver_store → store}/message_store.js +51 -9
  89. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  90. package/dest/structs/data_retrieval.d.ts.map +1 -0
  91. package/dest/structs/inbox_message.d.ts +15 -0
  92. package/dest/structs/inbox_message.d.ts.map +1 -0
  93. package/dest/{archiver/structs → structs}/published.d.ts +1 -1
  94. package/dest/structs/published.d.ts.map +1 -0
  95. package/dest/test/fake_l1_state.d.ts +214 -0
  96. package/dest/test/fake_l1_state.d.ts.map +1 -0
  97. package/dest/test/fake_l1_state.js +517 -0
  98. package/dest/test/index.d.ts +2 -1
  99. package/dest/test/index.d.ts.map +1 -1
  100. package/dest/test/index.js +4 -1
  101. package/dest/test/mock_archiver.d.ts +2 -2
  102. package/dest/test/mock_archiver.d.ts.map +1 -1
  103. package/dest/test/mock_archiver.js +3 -3
  104. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  105. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  106. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  107. package/dest/test/mock_l2_block_source.d.ts +65 -41
  108. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  109. package/dest/test/mock_l2_block_source.js +330 -151
  110. package/dest/test/mock_structs.d.ts +81 -3
  111. package/dest/test/mock_structs.d.ts.map +1 -1
  112. package/dest/test/mock_structs.js +152 -7
  113. package/dest/test/noop_l1_archiver.d.ts +29 -0
  114. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  115. package/dest/test/noop_l1_archiver.js +85 -0
  116. package/package.json +17 -18
  117. package/src/archiver.ts +681 -0
  118. package/src/{archiver/config.ts → config.ts} +43 -12
  119. package/src/errors.ts +203 -0
  120. package/src/factory.ts +175 -22
  121. package/src/index.ts +27 -3
  122. package/src/interfaces.ts +9 -0
  123. package/src/l1/README.md +55 -0
  124. package/src/{archiver/l1 → l1}/bin/retrieve-calldata.ts +45 -33
  125. package/src/l1/calldata_retriever.ts +522 -0
  126. package/src/{archiver/l1 → l1}/data_retrieval.ts +106 -134
  127. package/src/{archiver/l1 → l1}/spire_proposer.ts +7 -15
  128. package/src/l1/validate_historical_logs.ts +140 -0
  129. package/src/{archiver/l1 → l1}/validate_trace.ts +24 -6
  130. package/src/modules/contract_data_source_adapter.ts +55 -0
  131. package/src/modules/data_source_base.ts +493 -0
  132. package/src/modules/data_store_updater.ts +518 -0
  133. package/src/{archiver → modules}/instrumentation.ts +72 -20
  134. package/src/modules/l1_synchronizer.ts +1257 -0
  135. package/src/{archiver → modules}/validation.ts +15 -9
  136. package/src/store/block_store.ts +1590 -0
  137. package/src/store/contract_class_store.ts +108 -0
  138. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +52 -6
  139. package/src/store/data_stores.ts +104 -0
  140. package/src/store/function_names_cache.ts +37 -0
  141. package/src/store/l2_tips_cache.ts +35 -0
  142. package/src/store/log_store.ts +379 -0
  143. package/src/store/log_store_codec.ts +132 -0
  144. package/src/{archiver/kv_archiver_store → store}/message_store.ts +60 -10
  145. package/src/{archiver/structs → structs}/inbox_message.ts +1 -1
  146. package/src/test/fake_l1_state.ts +770 -0
  147. package/src/test/index.ts +4 -0
  148. package/src/test/mock_archiver.ts +4 -3
  149. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  150. package/src/test/mock_l2_block_source.ts +403 -171
  151. package/src/test/mock_structs.ts +283 -8
  152. package/src/test/noop_l1_archiver.ts +139 -0
  153. package/dest/archiver/archiver.d.ts +0 -307
  154. package/dest/archiver/archiver.d.ts.map +0 -1
  155. package/dest/archiver/archiver.js +0 -2102
  156. package/dest/archiver/archiver_store.d.ts +0 -315
  157. package/dest/archiver/archiver_store.d.ts.map +0 -1
  158. package/dest/archiver/archiver_store.js +0 -4
  159. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  160. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  161. package/dest/archiver/archiver_store_test_suite.js +0 -2770
  162. package/dest/archiver/config.d.ts +0 -22
  163. package/dest/archiver/config.d.ts.map +0 -1
  164. package/dest/archiver/errors.d.ts +0 -36
  165. package/dest/archiver/errors.d.ts.map +0 -1
  166. package/dest/archiver/errors.js +0 -54
  167. package/dest/archiver/index.d.ts +0 -7
  168. package/dest/archiver/index.d.ts.map +0 -1
  169. package/dest/archiver/index.js +0 -4
  170. package/dest/archiver/instrumentation.d.ts +0 -37
  171. package/dest/archiver/instrumentation.d.ts.map +0 -1
  172. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -164
  173. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  174. package/dest/archiver/kv_archiver_store/block_store.js +0 -626
  175. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  176. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  177. package/dest/archiver/kv_archiver_store/contract_class_store.js +0 -120
  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 -159
  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 -316
  183. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -45
  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 -401
  186. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -40
  187. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  188. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +0 -1
  189. package/dest/archiver/l1/calldata_retriever.d.ts +0 -112
  190. package/dest/archiver/l1/calldata_retriever.d.ts.map +0 -1
  191. package/dest/archiver/l1/calldata_retriever.js +0 -471
  192. package/dest/archiver/l1/data_retrieval.d.ts +0 -90
  193. package/dest/archiver/l1/data_retrieval.d.ts.map +0 -1
  194. package/dest/archiver/l1/debug_tx.d.ts.map +0 -1
  195. package/dest/archiver/l1/spire_proposer.d.ts.map +0 -1
  196. package/dest/archiver/l1/trace_tx.d.ts +0 -97
  197. package/dest/archiver/l1/trace_tx.d.ts.map +0 -1
  198. package/dest/archiver/l1/types.d.ts +0 -12
  199. package/dest/archiver/l1/types.d.ts.map +0 -1
  200. package/dest/archiver/l1/validate_trace.d.ts.map +0 -1
  201. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  202. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  203. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  204. package/dest/archiver/structs/published.d.ts.map +0 -1
  205. package/dest/archiver/validation.d.ts +0 -17
  206. package/dest/archiver/validation.d.ts.map +0 -1
  207. package/dest/rpc/index.d.ts +0 -9
  208. package/dest/rpc/index.d.ts.map +0 -1
  209. package/dest/rpc/index.js +0 -15
  210. package/src/archiver/archiver.ts +0 -2265
  211. package/src/archiver/archiver_store.ts +0 -380
  212. package/src/archiver/archiver_store_test_suite.ts +0 -2842
  213. package/src/archiver/errors.ts +0 -90
  214. package/src/archiver/index.ts +0 -6
  215. package/src/archiver/kv_archiver_store/block_store.ts +0 -850
  216. package/src/archiver/kv_archiver_store/contract_class_store.ts +0 -176
  217. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -442
  218. package/src/archiver/kv_archiver_store/log_store.ts +0 -516
  219. package/src/archiver/l1/README.md +0 -98
  220. package/src/archiver/l1/calldata_retriever.ts +0 -641
  221. package/src/rpc/index.ts +0 -16
  222. /package/dest/{archiver/l1 → l1}/debug_tx.js +0 -0
  223. /package/dest/{archiver/l1 → l1}/trace_tx.js +0 -0
  224. /package/dest/{archiver/l1 → l1}/types.js +0 -0
  225. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  226. /package/dest/{archiver/structs → structs}/inbox_message.js +0 -0
  227. /package/dest/{archiver/structs → structs}/published.js +0 -0
  228. /package/src/{archiver/l1 → l1}/debug_tx.ts +0 -0
  229. /package/src/{archiver/l1 → l1}/trace_tx.ts +0 -0
  230. /package/src/{archiver/l1 → l1}/types.ts +0 -0
  231. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
  232. /package/src/{archiver/structs → structs}/published.ts +0 -0
@@ -0,0 +1,310 @@
1
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { createLogger } from '@aztec/foundation/log';
3
+ import { MAX_LOGS_PER_TAG } from '@aztec/stdlib/interfaces/api-limit';
4
+ import { decodeKeyTail, decodeValue, encodeKey, encodePublicPrefix, encodeValue, endOfTagRange, endOfTxRange, fieldHex, incKey } from './log_store_codec.js';
5
+ /**
6
+ * Indexes every emitted private and public log under a composite hex-string key
7
+ * `[contractAddress (public only)]-tag-blockNumber-txIndexWithinBlock-logIndexWithinTx`,
8
+ * where each numeric segment is zero-padded to 8 lowercase hex digits (4 bytes BE) and
9
+ * `contractAddress` / `tag` are the bare 64-hex-char field representations (no `0x` prefix). The
10
+ * fixed-width zero-padded hex segments sort lexicographically in the same order as the canonical
11
+ * `(contract, tag, blockNumber, txIndexWithinBlock, logIndexWithinTx)` tuple, so a single ordered
12
+ * range scan answers every {@link PrivateLogsQuery} / {@link PublicLogsQuery}.
13
+ *
14
+ * Per-block secondary indices (`#privateKeysByBlock`, `#publicKeysByBlock`) record the exact primary
15
+ * keys written for each block so {@link deleteLogs} can drop them on reorg without having to range
16
+ * scan by block (block isn't the leading key segment).
17
+ *
18
+ * Contract-class logs are no longer stored or served by the log store.
19
+ */ export class LogStore {
20
+ db;
21
+ blockStore;
22
+ genesisBlockHash;
23
+ /** Primary map: composite private key (tag + tail = 96 hex chars + separators) -> serialized {@link StoredLogValue}. */ #privateLogs;
24
+ /** Primary map: composite public key (contract + tag + tail) -> serialized {@link StoredLogValue}. */ #publicLogs;
25
+ /** Secondary deletion index: blockNumber -> the exact primary keys written for that block. */ #privateKeysByBlock;
26
+ #publicKeysByBlock;
27
+ #log;
28
+ /**
29
+ * @param genesisBlockHash - Hash of the synthetic genesis block. During early sync the PXE anchors to
30
+ * genesis and passes its hash as a query `referenceBlock`; since the archiver never indexes the
31
+ * genesis block, the store recognizes this hash directly and resolves it to the genesis block number
32
+ * rather than mistaking it for a reorg.
33
+ */ constructor(db, blockStore, genesisBlockHash){
34
+ this.db = db;
35
+ this.blockStore = blockStore;
36
+ this.genesisBlockHash = genesisBlockHash;
37
+ this.#log = createLogger('archiver:log_store');
38
+ this.#privateLogs = db.openMap('archiver_private_logs');
39
+ this.#publicLogs = db.openMap('archiver_public_logs');
40
+ this.#privateKeysByBlock = db.openMap('archiver_private_log_keys_by_block');
41
+ this.#publicKeysByBlock = db.openMap('archiver_public_log_keys_by_block');
42
+ }
43
+ /**
44
+ * Indexes every emitted private and public log from the given blocks. Wraps the write in a single
45
+ * `db.transactionAsync` so the primary entries and the per-block secondary indices stay consistent.
46
+ *
47
+ * A block is only ever added once; on reorg the archiver calls {@link deleteLogs} first, so we write
48
+ * the secondary index entries with a plain `set` (overwrite) rather than read-modify-append.
49
+ */ addLogs(blocks) {
50
+ return this.db.transactionAsync(async ()=>{
51
+ for (const block of blocks){
52
+ const blockHash = await block.hash();
53
+ const blockNumber = block.number;
54
+ const blockTimestamp = block.timestamp;
55
+ const privateKeys = [];
56
+ const privateValues = [];
57
+ const publicKeys = [];
58
+ const publicValues = [];
59
+ for(let txIndexWithinBlock = 0; txIndexWithinBlock < block.body.txEffects.length; txIndexWithinBlock++){
60
+ const txEffect = block.body.txEffects[txIndexWithinBlock];
61
+ const txHash = txEffect.txHash;
62
+ // Private and public log indices are counted independently per tx, each starting at 0.
63
+ let privateLogIndexWithinTx = 0;
64
+ let publicLogIndexWithinTx = 0;
65
+ for (const log of txEffect.privateLogs){
66
+ const tagHex = fieldHex(log.fields[0]);
67
+ const key = encodeKey(tagHex, blockNumber, txIndexWithinBlock, privateLogIndexWithinTx);
68
+ const value = encodeValue({
69
+ txHash,
70
+ blockHash,
71
+ blockTimestamp,
72
+ logData: log.getEmittedFields()
73
+ });
74
+ privateKeys.push(key);
75
+ privateValues.push(value);
76
+ privateLogIndexWithinTx++;
77
+ }
78
+ for (const log of txEffect.publicLogs){
79
+ const contractHex = fieldHex(log.contractAddress);
80
+ const tagHex = fieldHex(log.fields[0]);
81
+ const key = encodeKey(encodePublicPrefix(contractHex, tagHex), blockNumber, txIndexWithinBlock, publicLogIndexWithinTx);
82
+ const value = encodeValue({
83
+ txHash,
84
+ blockHash,
85
+ blockTimestamp,
86
+ logData: log.getEmittedFields()
87
+ });
88
+ publicKeys.push(key);
89
+ publicValues.push(value);
90
+ publicLogIndexWithinTx++;
91
+ }
92
+ }
93
+ for(let i = 0; i < privateKeys.length; i++){
94
+ await this.#privateLogs.set(privateKeys[i], privateValues[i]);
95
+ }
96
+ for(let i = 0; i < publicKeys.length; i++){
97
+ await this.#publicLogs.set(publicKeys[i], publicValues[i]);
98
+ }
99
+ await this.#privateKeysByBlock.set(blockNumber, privateKeys);
100
+ await this.#publicKeysByBlock.set(blockNumber, publicKeys);
101
+ this.#log.debug(`Indexed logs for block ${blockNumber}`, {
102
+ blockNumber,
103
+ privateCount: privateKeys.length,
104
+ publicCount: publicKeys.length
105
+ });
106
+ }
107
+ return true;
108
+ });
109
+ }
110
+ /**
111
+ * Deletes every log indexed under any of the given blocks. Secondary-index driven, so it doesn't
112
+ * have to range-scan the primary maps.
113
+ */ deleteLogs(blocks) {
114
+ return this.db.transactionAsync(async ()=>{
115
+ for (const block of blocks){
116
+ const blockNumber = block.number;
117
+ const [privateKeys, publicKeys] = await Promise.all([
118
+ this.#privateKeysByBlock.getAsync(blockNumber),
119
+ this.#publicKeysByBlock.getAsync(blockNumber)
120
+ ]);
121
+ if (privateKeys) {
122
+ for (const key of privateKeys){
123
+ await this.#privateLogs.delete(key);
124
+ }
125
+ await this.#privateKeysByBlock.delete(blockNumber);
126
+ }
127
+ if (publicKeys) {
128
+ for (const key of publicKeys){
129
+ await this.#publicLogs.delete(key);
130
+ }
131
+ await this.#publicKeysByBlock.delete(blockNumber);
132
+ }
133
+ }
134
+ return true;
135
+ });
136
+ }
137
+ /** Returns one inner array per element of `query.tags`, in input order. */ getPrivateLogsByTags(query) {
138
+ LogStore.#validateQuery(query);
139
+ return this.db.transactionAsync(()=>this.#runQuery(query, /* contractHex */ undefined));
140
+ }
141
+ /** Returns one inner array per element of `query.tags`, in input order. */ getPublicLogsByTags(query) {
142
+ LogStore.#validateQuery(query);
143
+ return this.db.transactionAsync(()=>this.#runQuery(query, fieldHex(query.contractAddress)));
144
+ }
145
+ static #validateQuery(query) {
146
+ if (query.txHash !== undefined && (query.fromBlock !== undefined || query.toBlock !== undefined)) {
147
+ throw new Error('`txHash` is mutually exclusive with `fromBlock`/`toBlock`');
148
+ }
149
+ }
150
+ async #runQuery(query, contractHex) {
151
+ const isPublic = contractHex !== undefined;
152
+ const tags = query.tags ?? [];
153
+ const primaryMap = isPublic ? this.#publicLogs : this.#privateLogs;
154
+ // referenceBlock reorg check, in-transaction, against the same db the log primary maps live on. The
155
+ // genesis block is a valid anchor during early sync but is synthetic and never indexed in the block
156
+ // store, so resolve it directly to the genesis block number rather than mistaking it for a reorg.
157
+ let referenceBlockNumber;
158
+ if (query.referenceBlock) {
159
+ if (query.referenceBlock.equals(this.genesisBlockHash)) {
160
+ referenceBlockNumber = INITIAL_L2_BLOCK_NUM - 1;
161
+ } else {
162
+ const refBlk = await this.blockStore.getBlockData({
163
+ hash: query.referenceBlock
164
+ });
165
+ if (!refBlk) {
166
+ throw new Error(`Reference block ${query.referenceBlock.toString()} not found in the node. This might indicate a reorg has occurred.`);
167
+ }
168
+ referenceBlockNumber = refBlk.header.globalVariables.blockNumber;
169
+ }
170
+ }
171
+ // Compute the exclusive upper-block bound across `toBlock` and `referenceBlock`.
172
+ // `toBlock` is already exclusive; `referenceBlock` caps inclusively, so its exclusive form is +1.
173
+ let upperExclusive;
174
+ if (query.toBlock !== undefined) {
175
+ upperExclusive = query.toBlock;
176
+ }
177
+ if (referenceBlockNumber !== undefined) {
178
+ const refExclusive = referenceBlockNumber + 1;
179
+ upperExclusive = upperExclusive === undefined ? refExclusive : Math.min(upperExclusive, refExclusive);
180
+ }
181
+ // Resolve txHash -> (blockNumber, txIndexInBlock) once for the whole query.
182
+ let txLocation;
183
+ if (query.txHash) {
184
+ const loc = await this.blockStore.getTxLocation(query.txHash);
185
+ if (!loc) {
186
+ return tags.map(()=>[]);
187
+ }
188
+ txLocation = loc;
189
+ if (upperExclusive !== undefined && txLocation[0] >= upperExclusive) {
190
+ return tags.map(()=>[]);
191
+ }
192
+ }
193
+ const fromBlock = query.fromBlock ?? INITIAL_L2_BLOCK_NUM;
194
+ const includeEffects = query.includeEffects === true;
195
+ const perTagResults = [];
196
+ for (const tagEntry of tags){
197
+ const { tagHex, afterLog } = normalizeTagEntry(tagEntry);
198
+ const prefix = contractHex !== undefined ? encodePublicPrefix(contractHex, tagHex) : tagHex;
199
+ const end = txLocation ? endOfTxRange(prefix, txLocation[0], txLocation[1]) : endOfTagRange(prefix, upperExclusive);
200
+ let start;
201
+ if (afterLog) {
202
+ // Cursor wins as the start; `fromBlock` is ignored (fine if the cursor sits below it). The cursor
203
+ // carries `(blockNumber, txIndexWithinBlock, logIndexWithinTx)`, which slot directly into the
204
+ // composite key — no tx-hash lookup needed.
205
+ start = incKey(encodeKey(prefix, afterLog.blockNumber, afterLog.txIndexWithinBlock, afterLog.logIndexWithinTx));
206
+ } else if (txLocation) {
207
+ start = encodeKey(prefix, txLocation[0], txLocation[1], 0);
208
+ } else {
209
+ start = encodeKey(prefix, fromBlock, 0, 0);
210
+ }
211
+ const limit = query.limitPerTag ?? MAX_LOGS_PER_TAG;
212
+ const out = [];
213
+ for await (const [rawKey, rawVal] of primaryMap.entriesAsync({
214
+ start,
215
+ end,
216
+ limit
217
+ })){
218
+ const tail = decodeKeyTail(rawKey);
219
+ const value = decodeValue(rawVal);
220
+ out.push({
221
+ logData: value.logData,
222
+ blockNumber: tail.blockNumber,
223
+ blockHash: value.blockHash,
224
+ blockTimestamp: value.blockTimestamp,
225
+ txHash: value.txHash,
226
+ txIndexWithinBlock: tail.txIndexWithinBlock,
227
+ logIndexWithinTx: tail.logIndexWithinTx
228
+ });
229
+ }
230
+ perTagResults.push(out);
231
+ }
232
+ if (includeEffects) {
233
+ // Dedupe by txHash across the entire page so a tx with many tagged logs costs one fetch.
234
+ const txHashByKey = new Map();
235
+ for (const arr of perTagResults){
236
+ for (const log of arr){
237
+ txHashByKey.set(log.txHash.toString(), log.txHash);
238
+ }
239
+ }
240
+ const uniqueTxs = Array.from(txHashByKey.values());
241
+ if (uniqueTxs.length > 0) {
242
+ const effects = await this.blockStore.getNoteHashesAndNullifiers(uniqueTxs);
243
+ const byTxHash = new Map();
244
+ uniqueTxs.forEach((tx, i)=>byTxHash.set(tx.toString(), effects[i]));
245
+ for(let i = 0; i < perTagResults.length; i++){
246
+ perTagResults[i] = perTagResults[i].map((log)=>{
247
+ const [noteHashes, nullifiers] = byTxHash.get(log.txHash.toString()) ?? [
248
+ [],
249
+ []
250
+ ];
251
+ return {
252
+ ...log,
253
+ noteHashes,
254
+ nullifiers
255
+ };
256
+ });
257
+ }
258
+ }
259
+ }
260
+ return perTagResults;
261
+ }
262
+ /**
263
+ * Reads back every private log indexed for the given block via the per-block secondary index. Order
264
+ * matches the canonical composite-key order (`tag`, `blockNumber`, `txIndexWithinBlock`,
265
+ * `logIndexWithinTx`). Used by the data-store-updater test suite to verify the indexed-vs-block-body
266
+ * counts without depending on the removed `getPublicLogs(LogFilter)` API.
267
+ */ getPrivateLogsForBlock(blockNumber) {
268
+ return this.db.transactionAsync(()=>this.#readBlockLogs(this.#privateKeysByBlock, this.#privateLogs, blockNumber));
269
+ }
270
+ /** {@inheritDoc LogStore.getPrivateLogsForBlock} */ getPublicLogsForBlock(blockNumber) {
271
+ return this.db.transactionAsync(()=>this.#readBlockLogs(this.#publicKeysByBlock, this.#publicLogs, blockNumber));
272
+ }
273
+ async #readBlockLogs(keysByBlock, primaryMap, blockNumber) {
274
+ const keys = await keysByBlock.getAsync(blockNumber);
275
+ if (!keys || keys.length === 0) {
276
+ return [];
277
+ }
278
+ const results = [];
279
+ for (const key of keys){
280
+ const raw = await primaryMap.getAsync(key);
281
+ if (!raw) {
282
+ continue;
283
+ }
284
+ const tail = decodeKeyTail(key);
285
+ const value = decodeValue(raw);
286
+ results.push({
287
+ logData: value.logData,
288
+ blockNumber: tail.blockNumber,
289
+ blockHash: value.blockHash,
290
+ blockTimestamp: value.blockTimestamp,
291
+ txHash: value.txHash,
292
+ txIndexWithinBlock: tail.txIndexWithinBlock,
293
+ logIndexWithinTx: tail.logIndexWithinTx
294
+ });
295
+ }
296
+ return results;
297
+ }
298
+ }
299
+ /** Pulls `{ tagHex, afterLog }` out of a {@link TagQuery}, normalizing the bare-tag form. */ function normalizeTagEntry(entry) {
300
+ if (typeof entry === 'object' && entry !== null && 'tag' in entry) {
301
+ return {
302
+ tagHex: fieldHex(entry.tag.value),
303
+ afterLog: entry.afterLog
304
+ };
305
+ }
306
+ return {
307
+ tagHex: fieldHex(entry.value),
308
+ afterLog: undefined
309
+ };
310
+ }
@@ -0,0 +1,70 @@
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import { BlockHash } from '@aztec/stdlib/block';
4
+ import { TxHash } from '@aztec/stdlib/tx';
5
+ export declare const NUMERIC_HEX_LEN = 8;
6
+ export declare const SEP = "-";
7
+ /**
8
+ * Sentinel appended after a numeric hex segment to build an end bound strictly greater than any
9
+ * real key for that namespace. `'g'` sorts lexicographically after every hex digit (`0`-`9`, `a`-`f`),
10
+ * so `prefix + '-g'` is a clean exclusive upper bound.
11
+ */
12
+ export declare const HEX_SENTINEL = "g";
13
+ export type ParsedKeyTail = {
14
+ blockNumber: BlockNumber;
15
+ txIndexWithinBlock: number;
16
+ logIndexWithinTx: number;
17
+ };
18
+ /**
19
+ * Per-kind stored value layout (no msgpackr):
20
+ * txHash(32) ++ blockHash(32) ++ blockTimestamp(u64 BE = 8) ++ logDataLen(u32 BE = 4) ++ logData[i].toBuffer()...
21
+ * `blockNumber`, `txIndexWithinBlock`, and `logIndexWithinTx` are decoded from the composite key.
22
+ */
23
+ export type StoredLogValue = {
24
+ txHash: TxHash;
25
+ blockHash: BlockHash;
26
+ blockTimestamp: bigint;
27
+ logData: Fr[];
28
+ };
29
+ /** Returns the 64-char lowercase hex representation of a field, stripping the `0x` prefix. */
30
+ export declare function fieldHex(value: Fr | {
31
+ toString: () => string;
32
+ }): string;
33
+ /** Encodes a number as 8-char zero-padded lowercase hex (matches a u32 big-endian byte buffer's lex order). */
34
+ export declare function u32Hex(n: number): string;
35
+ /**
36
+ * Encodes the composite primary key as `prefix-block-txIdx-logIdx` where `prefix` is the leading
37
+ * segment (`tag` for private; `contract-tag` for public) and the trailing triple is fixed-width
38
+ * 8-char zero-padded hex so byte-order matches `(blockNumber, txIndexWithinBlock, logIndexWithinTx)`.
39
+ */
40
+ export declare function encodeKey(prefix: string, blockNumber: number, txIndex: number, logIndex: number): string;
41
+ /**
42
+ * Decodes the trailing `(blockNumber, txIndexWithinBlock, logIndexWithinTx)` triple from a composite
43
+ * key. The leading prefix segments are ignored — we only ever read them off the input query, never
44
+ * back off the key.
45
+ */
46
+ export declare function decodeKeyTail(key: string): ParsedKeyTail;
47
+ /**
48
+ * Exclusive end bound for a `(contract, tag)`-prefix scan. With an `upperBlockExclusive` we cut at
49
+ * `(prefix, upper, 0, 0)`. With no bound we use `prefix + '-' + HEX_SENTINEL`, which sorts strictly
50
+ * after every real key under `prefix` (`g` is greater than any hex digit).
51
+ */
52
+ export declare function endOfTagRange(prefix: string, upperBlockExclusive: number | undefined): string;
53
+ /**
54
+ * Exclusive end bound for a tx-strict scan: every key strictly inside `(prefix, txBlk, txIdx, *)`.
55
+ * `prefix-block-txIdx-` followed by the hex sentinel is the first key past every real logIndex for
56
+ * this tx and strictly less than the next tx's first key.
57
+ */
58
+ export declare function endOfTxRange(prefix: string, txBlk: number, txIdx: number): string;
59
+ /**
60
+ * Returns the smallest string strictly greater than a fully-encoded composite key. The encoded key
61
+ * ends in a hex digit, and `'g'` sorts strictly after any hex digit, so appending `'g'` is the
62
+ * smallest possible successor in our key alphabet. Used to turn an inclusive cursor into an
63
+ * exclusive `start`.
64
+ */
65
+ export declare function incKey(key: string): string;
66
+ export declare function encodeValue(value: StoredLogValue): Buffer;
67
+ export declare function decodeValue(buffer: Buffer | Uint8Array): StoredLogValue;
68
+ /** Encodes the public-log key prefix as `contractHex-tagHex`. */
69
+ export declare function encodePublicPrefix(contractHex: string, tagHex: string): string;
70
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibG9nX3N0b3JlX2NvZGVjLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi9zcmMvc3RvcmUvbG9nX3N0b3JlX2NvZGVjLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sRUFBRSxXQUFXLEVBQUUsTUFBTSxpQ0FBaUMsQ0FBQztBQUM5RCxPQUFPLE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,GAAG,MAAM,CAQzD;AAED,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,UAAU,GAAG,cAAc,CAiBvE;AAED,iEAAiE;AACjE,wBAAgB,kBAAkB,CAAC,WAAW,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAE9E"}
@@ -0,0 +1,101 @@
1
+ import { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import { Fr } from '@aztec/foundation/curves/bn254';
3
+ import { BlockHash } from '@aztec/stdlib/block';
4
+ import { TxHash } from '@aztec/stdlib/tx';
5
+ export const NUMERIC_HEX_LEN = 8;
6
+ export const SEP = '-';
7
+ /**
8
+ * Sentinel appended after a numeric hex segment to build an end bound strictly greater than any
9
+ * real key for that namespace. `'g'` sorts lexicographically after every hex digit (`0`-`9`, `a`-`f`),
10
+ * so `prefix + '-g'` is a clean exclusive upper bound.
11
+ */ export const HEX_SENTINEL = 'g';
12
+ /** Returns the 64-char lowercase hex representation of a field, stripping the `0x` prefix. */ export function fieldHex(value) {
13
+ // Fr.toString() and AztecAddress.toString() both return `0x` + 64 lowercase hex chars.
14
+ return value.toString().slice(2);
15
+ }
16
+ /** Encodes a number as 8-char zero-padded lowercase hex (matches a u32 big-endian byte buffer's lex order). */ export function u32Hex(n) {
17
+ return n.toString(16).padStart(NUMERIC_HEX_LEN, '0');
18
+ }
19
+ /**
20
+ * Encodes the composite primary key as `prefix-block-txIdx-logIdx` where `prefix` is the leading
21
+ * segment (`tag` for private; `contract-tag` for public) and the trailing triple is fixed-width
22
+ * 8-char zero-padded hex so byte-order matches `(blockNumber, txIndexWithinBlock, logIndexWithinTx)`.
23
+ */ export function encodeKey(prefix, blockNumber, txIndex, logIndex) {
24
+ return `${prefix}${SEP}${u32Hex(blockNumber)}${SEP}${u32Hex(txIndex)}${SEP}${u32Hex(logIndex)}`;
25
+ }
26
+ /**
27
+ * Decodes the trailing `(blockNumber, txIndexWithinBlock, logIndexWithinTx)` triple from a composite
28
+ * key. The leading prefix segments are ignored — we only ever read them off the input query, never
29
+ * back off the key.
30
+ */ export function decodeKeyTail(key) {
31
+ const parts = key.split(SEP);
32
+ const len = parts.length;
33
+ return {
34
+ blockNumber: BlockNumber(parseInt(parts[len - 3], 16)),
35
+ txIndexWithinBlock: parseInt(parts[len - 2], 16),
36
+ logIndexWithinTx: parseInt(parts[len - 1], 16)
37
+ };
38
+ }
39
+ /**
40
+ * Exclusive end bound for a `(contract, tag)`-prefix scan. With an `upperBlockExclusive` we cut at
41
+ * `(prefix, upper, 0, 0)`. With no bound we use `prefix + '-' + HEX_SENTINEL`, which sorts strictly
42
+ * after every real key under `prefix` (`g` is greater than any hex digit).
43
+ */ export function endOfTagRange(prefix, upperBlockExclusive) {
44
+ if (upperBlockExclusive === undefined) {
45
+ return `${prefix}${SEP}${HEX_SENTINEL}`;
46
+ }
47
+ return encodeKey(prefix, upperBlockExclusive, 0, 0);
48
+ }
49
+ /**
50
+ * Exclusive end bound for a tx-strict scan: every key strictly inside `(prefix, txBlk, txIdx, *)`.
51
+ * `prefix-block-txIdx-` followed by the hex sentinel is the first key past every real logIndex for
52
+ * this tx and strictly less than the next tx's first key.
53
+ */ export function endOfTxRange(prefix, txBlk, txIdx) {
54
+ return `${prefix}${SEP}${u32Hex(txBlk)}${SEP}${u32Hex(txIdx)}${SEP}${HEX_SENTINEL}`;
55
+ }
56
+ /**
57
+ * Returns the smallest string strictly greater than a fully-encoded composite key. The encoded key
58
+ * ends in a hex digit, and `'g'` sorts strictly after any hex digit, so appending `'g'` is the
59
+ * smallest possible successor in our key alphabet. Used to turn an inclusive cursor into an
60
+ * exclusive `start`.
61
+ */ export function incKey(key) {
62
+ return key + HEX_SENTINEL;
63
+ }
64
+ export function encodeValue(value) {
65
+ const head = Buffer.allocUnsafe(32 + 32 + 8 + 4);
66
+ value.txHash.toBuffer().copy(head, 0);
67
+ value.blockHash.toBuffer().copy(head, 32);
68
+ head.writeBigUInt64BE(value.blockTimestamp, 64);
69
+ head.writeUInt32BE(value.logData.length, 72);
70
+ const fieldBufs = value.logData.map((f)=>f.toBuffer());
71
+ return Buffer.concat([
72
+ head,
73
+ ...fieldBufs
74
+ ]);
75
+ }
76
+ export function decodeValue(buffer) {
77
+ const buf = Buffer.isBuffer(buffer) ? buffer : Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
78
+ let off = 0;
79
+ const txHash = TxHash.fromBuffer(buf.subarray(off, off + 32));
80
+ off += 32;
81
+ const blockHash = BlockHash.fromBuffer(buf.subarray(off, off + 32));
82
+ off += 32;
83
+ const blockTimestamp = buf.readBigUInt64BE(off);
84
+ off += 8;
85
+ const logDataLen = buf.readUInt32BE(off);
86
+ off += 4;
87
+ const logData = new Array(logDataLen);
88
+ for(let i = 0; i < logDataLen; i++){
89
+ logData[i] = Fr.fromBuffer(buf.subarray(off, off + 32));
90
+ off += 32;
91
+ }
92
+ return {
93
+ txHash,
94
+ blockHash,
95
+ blockTimestamp,
96
+ logData
97
+ };
98
+ }
99
+ /** Encodes the public-log key prefix as `contractHex-tagHex`. */ export function encodePublicPrefix(contractHex, tagHex) {
100
+ return `${contractHex}${SEP}${tagHex}`;
101
+ }
@@ -0,0 +1,50 @@
1
+ import type { L1BlockId } from '@aztec/ethereum/l1-types';
2
+ import { CheckpointNumber } from '@aztec/foundation/branded-types';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
4
+ import { type AztecAsyncKVStore, type CustomRange } from '@aztec/kv-store';
5
+ import { type InboxMessage } from '../structs/inbox_message.js';
6
+ export declare class MessageStoreError extends Error {
7
+ readonly inboxMessage: InboxMessage;
8
+ constructor(message: string, inboxMessage: InboxMessage);
9
+ }
10
+ export declare class MessageStore {
11
+ #private;
12
+ private db;
13
+ constructor(db: AztecAsyncKVStore);
14
+ getTotalL1ToL2MessageCount(): Promise<bigint>;
15
+ /** Gets the last L1 block synced. */
16
+ getSynchedL1Block(): Promise<L1BlockId | undefined>;
17
+ /** Sets the last L1 block synced */
18
+ setSynchedL1Block(l1Block: L1BlockId): Promise<void>;
19
+ /** Gets the L1 finalized block as of the last successful message sync. */
20
+ getMessagesFinalizedL1Block(): Promise<L1BlockId | undefined>;
21
+ private maybeAdvanceFinalizedL1Block;
22
+ /**
23
+ * Append L1 to L2 messages to the store.
24
+ * Requires new messages to be in order and strictly after the last message added.
25
+ * Throws if out of order messages are added or if the rolling hash is invalid.
26
+ */
27
+ addL1ToL2Messages(messages: InboxMessage[]): Promise<void>;
28
+ /**
29
+ * Gets the L1 to L2 message index in the L1 to L2 message tree.
30
+ * @param l1ToL2Message - The L1 to L2 message.
31
+ * @returns The index of the L1 to L2 message in the L1 to L2 message tree (undefined if not found).
32
+ */
33
+ getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined>;
34
+ getLastMessage(): Promise<InboxMessage | undefined>;
35
+ /** Returns the inbox tree-in-progress checkpoint number from L1, or undefined if not yet set. */
36
+ getInboxTreeInProgress(): Promise<bigint | undefined>;
37
+ /**
38
+ * Atomically updates the message sync state: the L1 sync point, the inbox tree-in-progress marker, and
39
+ * (optionally) the L1 finalized block as of this sync. The finalized block is advanced monotonically.
40
+ */
41
+ setMessageSyncState(l1Block: L1BlockId, treeInProgress: bigint | undefined, finalizedL1Block?: L1BlockId): Promise<void>;
42
+ getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]>;
43
+ iterateL1ToL2Messages(range?: CustomRange<bigint>): AsyncIterableIterator<InboxMessage>;
44
+ removeL1ToL2Messages(startIndex: bigint): Promise<void>;
45
+ rollbackL1ToL2MessagesToCheckpoint(targetCheckpointNumber: CheckpointNumber): Promise<void>;
46
+ private indexToKey;
47
+ private leafToIndexKey;
48
+ private increaseTotalMessageCount;
49
+ }
50
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibWVzc2FnZV9zdG9yZS5kLnRzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL3N0b3JlL21lc3NhZ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MAAM,GAAG,SAAS,EAClC,gBAAgB,CAAC,EAAE,SAAS,GAC3B,OAAO,CAAC,IAAI,CAAC,CAYf;IAEY,iBAAiB,CAAC,gBAAgB,EAAE,gBAAgB,GAAG,OAAO,CAAC,EAAE,EAAE,CAAC,CA4BhF;IAEa,qBAAqB,CAAC,KAAK,GAAE,WAAW,CAAC,MAAM,CAAM,GAAG,qBAAqB,CAAC,YAAY,CAAC,CAKxG;IAEM,oBAAoB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAgB7D;IAEM,kCAAkC,CAAC,sBAAsB,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAIjG;IAED,OAAO,CAAC,UAAU;IAIlB,OAAO,CAAC,cAAc;YAIR,yBAAyB;CASxC"}
@@ -5,6 +5,7 @@ import { createLogger } from '@aztec/foundation/log';
5
5
  import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';
6
6
  import { mapRange } from '@aztec/kv-store';
7
7
  import { InboxLeaf } from '@aztec/stdlib/messaging';
8
+ import { L1ToL2MessagesNotReadyError } from '../errors.js';
8
9
  import { deserializeInboxMessage, serializeInboxMessage, updateRollingHash } from '../structs/inbox_message.js';
9
10
  export class MessageStoreError extends Error {
10
11
  inboxMessage;
@@ -19,6 +20,8 @@ export class MessageStore {
19
20
  /** Maps from hex-stringified message leaf to its index */ #l1ToL2MessageIndices;
20
21
  /** Stores L1 block number and hash of the L1 synchpoint */ #lastSynchedL1Block;
21
22
  /** Stores total messages stored */ #totalMessageCount;
23
+ /** Stores the checkpoint number whose message tree is currently being filled on L1. */ #inboxTreeInProgress;
24
+ /** Stores the L1 finalized block as of the last successful message sync. */ #messagesFinalizedL1Block;
22
25
  #log;
23
26
  constructor(db){
24
27
  this.db = db;
@@ -27,6 +30,8 @@ export class MessageStore {
27
30
  this.#l1ToL2MessageIndices = db.openMap('archiver_l1_to_l2_message_indices');
28
31
  this.#lastSynchedL1Block = db.openSingleton('archiver_last_l1_block_id');
29
32
  this.#totalMessageCount = db.openSingleton('archiver_l1_to_l2_message_count');
33
+ this.#inboxTreeInProgress = db.openSingleton('archiver_inbox_tree_in_progress');
34
+ this.#messagesFinalizedL1Block = db.openSingleton('archiver_messages_finalized_l1_block');
30
35
  }
31
36
  async getTotalL1ToL2MessageCount() {
32
37
  return await this.#totalMessageCount.getAsync() ?? 0n;
@@ -49,6 +54,28 @@ export class MessageStore {
49
54
  ]);
50
55
  await this.#lastSynchedL1Block.set(buffer);
51
56
  }
57
+ /** Gets the L1 finalized block as of the last successful message sync. */ async getMessagesFinalizedL1Block() {
58
+ const buffer = await this.#messagesFinalizedL1Block.getAsync();
59
+ if (!buffer) {
60
+ return undefined;
61
+ }
62
+ const reader = BufferReader.asReader(buffer);
63
+ return {
64
+ l1BlockNumber: reader.readUInt256(),
65
+ l1BlockHash: Buffer32.fromBuffer(reader.readBytes(Buffer32.SIZE))
66
+ };
67
+ }
68
+ /** Monotonically advances the persisted L1 finalized block for message sync. Never regresses. */ async maybeAdvanceFinalizedL1Block(l1Block) {
69
+ const existing = await this.getMessagesFinalizedL1Block();
70
+ if (existing && l1Block.l1BlockNumber <= existing.l1BlockNumber) {
71
+ return;
72
+ }
73
+ const buffer = serializeToBuffer([
74
+ l1Block.l1BlockNumber,
75
+ l1Block.l1BlockHash
76
+ ]);
77
+ await this.#messagesFinalizedL1Block.set(buffer);
78
+ }
52
79
  /**
53
80
  * Append L1 to L2 messages to the store.
54
81
  * Requires new messages to be in order and strictly after the last message added.
@@ -87,7 +114,7 @@ export class MessageStore {
87
114
  if (lastMessage && message.checkpointNumber === lastMessage.checkpointNumber && message.index !== lastMessage.index + 1n) {
88
115
  throw new MessageStoreError(`Missing prior message for incoming L1 to L2 message ${message.leaf.toString()} ` + `with index ${message.index}`, message);
89
116
  }
90
- // Check the first message in a block has the correct index.
117
+ // Check the first message in a checkpoint has the correct index.
91
118
  if ((!lastMessage || message.checkpointNumber > lastMessage.checkpointNumber) && message.index !== expectedStart) {
92
119
  throw new MessageStoreError(`Message ${message.leaf.toString()} for checkpoint ${message.checkpointNumber} has wrong index ` + `${message.index} (expected ${expectedStart})`, message);
93
120
  }
@@ -98,14 +125,6 @@ export class MessageStore {
98
125
  this.#log.trace(`Inserted L1 to L2 message ${message.leaf} with index ${message.index} into the store`);
99
126
  lastMessage = message;
100
127
  }
101
- // Update the L1 sync point to that of the last message added.
102
- const currentSyncPoint = await this.getSynchedL1Block();
103
- if (!currentSyncPoint || currentSyncPoint.l1BlockNumber < lastMessage.l1BlockNumber) {
104
- await this.setSynchedL1Block({
105
- l1BlockNumber: lastMessage.l1BlockNumber,
106
- l1BlockHash: lastMessage.l1BlockHash
107
- });
108
- }
109
128
  // Update total message count with the number of inserted messages.
110
129
  await this.increaseTotalMessageCount(messageCount);
111
130
  });
@@ -124,7 +143,30 @@ export class MessageStore {
124
143
  }));
125
144
  return msg ? deserializeInboxMessage(msg) : undefined;
126
145
  }
146
+ /** Returns the inbox tree-in-progress checkpoint number from L1, or undefined if not yet set. */ getInboxTreeInProgress() {
147
+ return this.#inboxTreeInProgress.getAsync();
148
+ }
149
+ /**
150
+ * Atomically updates the message sync state: the L1 sync point, the inbox tree-in-progress marker, and
151
+ * (optionally) the L1 finalized block as of this sync. The finalized block is advanced monotonically.
152
+ */ setMessageSyncState(l1Block, treeInProgress, finalizedL1Block) {
153
+ return this.db.transactionAsync(async ()=>{
154
+ await this.setSynchedL1Block(l1Block);
155
+ if (treeInProgress !== undefined) {
156
+ await this.#inboxTreeInProgress.set(treeInProgress);
157
+ } else {
158
+ await this.#inboxTreeInProgress.delete();
159
+ }
160
+ if (finalizedL1Block !== undefined) {
161
+ await this.maybeAdvanceFinalizedL1Block(finalizedL1Block);
162
+ }
163
+ });
164
+ }
127
165
  async getL1ToL2Messages(checkpointNumber) {
166
+ const treeInProgress = await this.#inboxTreeInProgress.getAsync();
167
+ if (treeInProgress !== undefined && BigInt(checkpointNumber) >= treeInProgress) {
168
+ throw new L1ToL2MessagesNotReadyError(checkpointNumber, treeInProgress);
169
+ }
128
170
  const messages = [];
129
171
  const [startIndex, endIndex] = InboxLeaf.indexRangeForCheckpoint(checkpointNumber);
130
172
  let lastIndex = startIndex - 1n;