@aztec/archiver 0.0.1-commit.d3ec352c → 0.0.1-commit.d58ff9d0

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 (236) hide show
  1. package/README.md +164 -22
  2. package/dest/archiver.d.ts +194 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +949 -0
  5. package/dest/config.d.ts +34 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/config.js +95 -0
  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 +117 -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/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 +152 -0
  23. package/dest/l1/calldata_retriever.d.ts +143 -0
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/l1/calldata_retriever.js +413 -0
  26. package/dest/l1/data_retrieval.d.ts +102 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/{archiver → l1}/data_retrieval.js +85 -165
  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 +149 -0
  35. package/dest/l1/trace_tx.d.ts +43 -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_historical_logs.d.ts +23 -0
  42. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  43. package/dest/l1/validate_historical_logs.js +108 -0
  44. package/dest/l1/validate_trace.d.ts +32 -0
  45. package/dest/l1/validate_trace.d.ts.map +1 -0
  46. package/dest/l1/validate_trace.js +154 -0
  47. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  48. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  49. package/dest/modules/contract_data_source_adapter.js +32 -0
  50. package/dest/modules/data_source_base.d.ts +112 -0
  51. package/dest/modules/data_source_base.d.ts.map +1 -0
  52. package/dest/modules/data_source_base.js +322 -0
  53. package/dest/modules/data_store_updater.d.ts +105 -0
  54. package/dest/modules/data_store_updater.d.ts.map +1 -0
  55. package/dest/modules/data_store_updater.js +393 -0
  56. package/dest/modules/instrumentation.d.ts +63 -0
  57. package/dest/modules/instrumentation.d.ts.map +1 -0
  58. package/dest/modules/instrumentation.js +166 -0
  59. package/dest/modules/l1_synchronizer.d.ts +77 -0
  60. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  61. package/dest/modules/l1_synchronizer.js +1381 -0
  62. package/dest/modules/outbox_trees_resolver.d.ts +62 -0
  63. package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
  64. package/dest/modules/outbox_trees_resolver.js +162 -0
  65. package/dest/modules/validation.d.ts +40 -0
  66. package/dest/modules/validation.d.ts.map +1 -0
  67. package/dest/{archiver → modules}/validation.js +41 -17
  68. package/dest/store/block_store.d.ts +303 -0
  69. package/dest/store/block_store.d.ts.map +1 -0
  70. package/dest/store/block_store.js +1206 -0
  71. package/dest/store/contract_class_store.d.ts +31 -0
  72. package/dest/store/contract_class_store.d.ts.map +1 -0
  73. package/dest/store/contract_class_store.js +98 -0
  74. package/dest/store/contract_instance_store.d.ts +51 -0
  75. package/dest/store/contract_instance_store.d.ts.map +1 -0
  76. package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +55 -3
  77. package/dest/store/data_stores.d.ts +68 -0
  78. package/dest/store/data_stores.d.ts.map +1 -0
  79. package/dest/store/data_stores.js +54 -0
  80. package/dest/store/function_names_cache.d.ts +17 -0
  81. package/dest/store/function_names_cache.d.ts.map +1 -0
  82. package/dest/store/function_names_cache.js +30 -0
  83. package/dest/store/l2_tips_cache.d.ts +25 -0
  84. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  85. package/dest/store/l2_tips_cache.js +26 -0
  86. package/dest/store/log_store.d.ts +59 -0
  87. package/dest/store/log_store.d.ts.map +1 -0
  88. package/dest/store/log_store.js +310 -0
  89. package/dest/store/log_store_codec.d.ts +78 -0
  90. package/dest/store/log_store_codec.d.ts.map +1 -0
  91. package/dest/store/log_store_codec.js +110 -0
  92. package/dest/store/message_store.d.ts +50 -0
  93. package/dest/store/message_store.d.ts.map +1 -0
  94. package/dest/{archiver/kv_archiver_store → store}/message_store.js +66 -23
  95. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  96. package/dest/structs/data_retrieval.d.ts.map +1 -0
  97. package/dest/structs/inbox_message.d.ts +15 -0
  98. package/dest/structs/inbox_message.d.ts.map +1 -0
  99. package/dest/{archiver/structs → structs}/inbox_message.js +6 -6
  100. package/dest/structs/published.d.ts +2 -0
  101. package/dest/structs/published.d.ts.map +1 -0
  102. package/dest/test/fake_l1_state.d.ts +214 -0
  103. package/dest/test/fake_l1_state.d.ts.map +1 -0
  104. package/dest/test/fake_l1_state.js +517 -0
  105. package/dest/test/index.d.ts +2 -1
  106. package/dest/test/index.d.ts.map +1 -1
  107. package/dest/test/index.js +4 -1
  108. package/dest/test/mock_archiver.d.ts +5 -6
  109. package/dest/test/mock_archiver.d.ts.map +1 -1
  110. package/dest/test/mock_archiver.js +8 -12
  111. package/dest/test/mock_l1_to_l2_message_source.d.ts +6 -7
  112. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  113. package/dest/test/mock_l1_to_l2_message_source.js +19 -14
  114. package/dest/test/mock_l2_block_source.d.ts +68 -40
  115. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  116. package/dest/test/mock_l2_block_source.js +347 -145
  117. package/dest/test/mock_structs.d.ts +83 -4
  118. package/dest/test/mock_structs.d.ts.map +1 -1
  119. package/dest/test/mock_structs.js +157 -12
  120. package/dest/test/noop_l1_archiver.d.ts +34 -0
  121. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  122. package/dest/test/noop_l1_archiver.js +88 -0
  123. package/package.json +18 -18
  124. package/src/archiver.ts +785 -0
  125. package/src/config.ts +125 -0
  126. package/src/errors.ts +203 -0
  127. package/src/factory.ts +185 -22
  128. package/src/index.ts +27 -3
  129. package/src/interfaces.ts +9 -0
  130. package/src/l1/README.md +55 -0
  131. package/src/l1/bin/retrieve-calldata.ts +194 -0
  132. package/src/l1/calldata_retriever.ts +529 -0
  133. package/src/{archiver → l1}/data_retrieval.ts +157 -243
  134. package/src/l1/debug_tx.ts +99 -0
  135. package/src/l1/spire_proposer.ts +152 -0
  136. package/src/l1/trace_tx.ts +128 -0
  137. package/src/l1/types.ts +13 -0
  138. package/src/l1/validate_historical_logs.ts +140 -0
  139. package/src/l1/validate_trace.ts +229 -0
  140. package/src/modules/contract_data_source_adapter.ts +46 -0
  141. package/src/modules/data_source_base.ts +466 -0
  142. package/src/modules/data_store_updater.ts +519 -0
  143. package/src/modules/instrumentation.ts +217 -0
  144. package/src/modules/l1_synchronizer.ts +1320 -0
  145. package/src/modules/outbox_trees_resolver.ts +199 -0
  146. package/src/modules/validation.ts +186 -0
  147. package/src/store/block_store.ts +1551 -0
  148. package/src/store/contract_class_store.ts +126 -0
  149. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +70 -7
  150. package/src/store/data_stores.ts +104 -0
  151. package/src/store/function_names_cache.ts +37 -0
  152. package/src/store/l2_tips_cache.ts +35 -0
  153. package/src/store/log_store.ts +380 -0
  154. package/src/store/log_store_codec.ts +143 -0
  155. package/src/{archiver/kv_archiver_store → store}/message_store.ts +81 -28
  156. package/src/{archiver/structs → structs}/inbox_message.ts +8 -9
  157. package/src/{archiver/structs → structs}/published.ts +0 -1
  158. package/src/test/fake_l1_state.ts +770 -0
  159. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  160. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  161. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  162. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  163. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  164. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  165. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  166. package/src/test/index.ts +4 -0
  167. package/src/test/mock_archiver.ts +10 -14
  168. package/src/test/mock_l1_to_l2_message_source.ts +16 -15
  169. package/src/test/mock_l2_block_source.ts +416 -161
  170. package/src/test/mock_structs.ts +292 -14
  171. package/src/test/noop_l1_archiver.ts +158 -0
  172. package/dest/archiver/archiver.d.ts +0 -290
  173. package/dest/archiver/archiver.d.ts.map +0 -1
  174. package/dest/archiver/archiver.js +0 -1434
  175. package/dest/archiver/archiver_store.d.ts +0 -256
  176. package/dest/archiver/archiver_store.d.ts.map +0 -1
  177. package/dest/archiver/archiver_store.js +0 -4
  178. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  179. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  180. package/dest/archiver/archiver_store_test_suite.js +0 -1289
  181. package/dest/archiver/config.d.ts +0 -21
  182. package/dest/archiver/config.d.ts.map +0 -1
  183. package/dest/archiver/config.js +0 -55
  184. package/dest/archiver/data_retrieval.d.ts +0 -80
  185. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  186. package/dest/archiver/errors.d.ts +0 -12
  187. package/dest/archiver/errors.d.ts.map +0 -1
  188. package/dest/archiver/errors.js +0 -17
  189. package/dest/archiver/index.d.ts +0 -7
  190. package/dest/archiver/index.d.ts.map +0 -1
  191. package/dest/archiver/index.js +0 -4
  192. package/dest/archiver/instrumentation.d.ts +0 -35
  193. package/dest/archiver/instrumentation.d.ts.map +0 -1
  194. package/dest/archiver/instrumentation.js +0 -140
  195. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -125
  196. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  197. package/dest/archiver/kv_archiver_store/block_store.js +0 -371
  198. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  199. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  200. package/dest/archiver/kv_archiver_store/contract_class_store.js +0 -120
  201. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  202. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  203. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -169
  204. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  205. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -296
  206. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
  207. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  208. package/dest/archiver/kv_archiver_store/log_store.js +0 -337
  209. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -39
  210. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  211. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  212. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  213. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  214. package/dest/archiver/structs/published.d.ts +0 -3
  215. package/dest/archiver/structs/published.d.ts.map +0 -1
  216. package/dest/archiver/validation.d.ts +0 -17
  217. package/dest/archiver/validation.d.ts.map +0 -1
  218. package/dest/rpc/index.d.ts +0 -9
  219. package/dest/rpc/index.d.ts.map +0 -1
  220. package/dest/rpc/index.js +0 -15
  221. package/src/archiver/archiver.ts +0 -1880
  222. package/src/archiver/archiver_store.ts +0 -310
  223. package/src/archiver/archiver_store_test_suite.ts +0 -1295
  224. package/src/archiver/config.ts +0 -79
  225. package/src/archiver/errors.ts +0 -26
  226. package/src/archiver/index.ts +0 -6
  227. package/src/archiver/instrumentation.ts +0 -187
  228. package/src/archiver/kv_archiver_store/block_store.ts +0 -482
  229. package/src/archiver/kv_archiver_store/contract_class_store.ts +0 -176
  230. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -423
  231. package/src/archiver/kv_archiver_store/log_store.ts +0 -407
  232. package/src/archiver/validation.ts +0 -124
  233. package/src/rpc/index.ts +0 -16
  234. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  235. /package/dest/{archiver/structs → structs}/published.js +0 -0
  236. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
@@ -0,0 +1,1551 @@
1
+ import { INITIAL_CHECKPOINT_NUMBER, INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import { BlockNumber, CheckpointNumber, IndexWithinCheckpoint, SlotNumber } from '@aztec/foundation/branded-types';
3
+ import { Fr } from '@aztec/foundation/curves/bn254';
4
+ import { toArray } from '@aztec/foundation/iterable';
5
+ import { createLogger } from '@aztec/foundation/log';
6
+ import { BufferReader } from '@aztec/foundation/serialize';
7
+ import { bufferToHex } from '@aztec/foundation/string';
8
+ import { isDefined } from '@aztec/foundation/types';
9
+ import type { AztecAsyncKVStore, AztecAsyncMap, AztecAsyncSingleton, Range } from '@aztec/kv-store';
10
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
11
+ import {
12
+ type BlockData,
13
+ BlockHash,
14
+ Body,
15
+ CommitteeAttestation,
16
+ GENESIS_CHECKPOINT_HEADER_HASH,
17
+ L2Block,
18
+ type L2TipId,
19
+ type L2Tips,
20
+ type ValidateCheckpointResult,
21
+ deserializeValidateCheckpointResult,
22
+ serializeValidateCheckpointResult,
23
+ } from '@aztec/stdlib/block';
24
+ import {
25
+ Checkpoint,
26
+ type CheckpointData,
27
+ type CommonCheckpointData,
28
+ L1PublishedData,
29
+ type ProposedCheckpointData,
30
+ type ProposedCheckpointInput,
31
+ PublishedCheckpoint,
32
+ } from '@aztec/stdlib/checkpoint';
33
+ import { CheckpointHeader } from '@aztec/stdlib/rollup';
34
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
35
+ import {
36
+ BlockHeader,
37
+ type IndexedTxEffect,
38
+ TxEffect,
39
+ TxHash,
40
+ deserializeIndexedTxEffect,
41
+ serializeIndexedTxEffect,
42
+ } from '@aztec/stdlib/tx';
43
+
44
+ import {
45
+ BlockAlreadyCheckpointedError,
46
+ BlockArchiveNotConsistentError,
47
+ BlockCheckpointNumberNotSequentialError,
48
+ BlockIndexNotSequentialError,
49
+ BlockNotFoundError,
50
+ BlockNumberNotSequentialError,
51
+ CannotOverwriteCheckpointedBlockError,
52
+ CheckpointNotFoundError,
53
+ CheckpointNumberNotSequentialError,
54
+ InitialCheckpointNumberNotSequentialError,
55
+ NoProposedCheckpointToPromoteError,
56
+ ProposedCheckpointArchiveRootMismatchError,
57
+ ProposedCheckpointNotSequentialError,
58
+ ProposedCheckpointPromotionNotSequentialError,
59
+ } from '../errors.js';
60
+
61
+ export type { TxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
62
+
63
+ type BlockIndexValue = [blockNumber: number, index: number];
64
+
65
+ type BlockStorage = {
66
+ header: Buffer;
67
+ blockHash: Buffer;
68
+ archive: Buffer;
69
+ checkpointNumber: number;
70
+ indexWithinCheckpoint: number;
71
+ };
72
+
73
+ /** Reason a checkpoint was rejected during sync. */
74
+ export type RejectedCheckpointReason = 'invalid-attestations' | 'descends-from-invalid-attestations';
75
+
76
+ /**
77
+ * A checkpoint observed on L1 that the archiver decided not to ingest, recorded so that
78
+ * any descendant that builds on top of it can also be skipped (rather than throwing
79
+ * `InitialCheckpointNumberNotSequentialError` and looping). An entry is dropped via
80
+ * {@link BlockStore.removeRejectedCheckpointByArchiveRoot} once a checkpoint with the same
81
+ * archive root is later ingested as valid (e.g. it gathered enough attestations), which
82
+ * re-enables its descendants.
83
+ */
84
+ export type RejectedCheckpoint = {
85
+ /** Checkpoint number this entry represents. */
86
+ checkpointNumber: CheckpointNumber;
87
+ /** Archive root produced by this rejected checkpoint (matched against descendants' `lastArchiveRoot`). */
88
+ archiveRoot: Fr;
89
+ /** `lastArchiveRoot` from this checkpoint's header (the ancestor it built on). */
90
+ parentArchiveRoot: Fr;
91
+ /** Slot number of the rejected checkpoint. */
92
+ slotNumber: SlotNumber;
93
+ /** L1 publication data for the rejected checkpoint (block number, hash, timestamp). */
94
+ l1: L1PublishedData;
95
+ /** Why the entry was recorded. */
96
+ reason: RejectedCheckpointReason;
97
+ };
98
+
99
+ type RejectedCheckpointStorage = {
100
+ checkpointNumber: number;
101
+ archiveRoot: Buffer;
102
+ parentArchiveRoot: Buffer;
103
+ slotNumber: number;
104
+ l1: Buffer;
105
+ reason: RejectedCheckpointReason;
106
+ };
107
+
108
+ /** Checkpoint Storage shared between Checkpoints + Proposed Checkpoints */
109
+ type CommonCheckpointStorage = {
110
+ header: Buffer;
111
+ archive: Buffer;
112
+ checkpointOutHash: Buffer;
113
+ checkpointNumber: number;
114
+ startBlock: number;
115
+ blockCount: number;
116
+ };
117
+
118
+ type CheckpointStorage = CommonCheckpointStorage & {
119
+ l1: Buffer;
120
+ attestations: Buffer[];
121
+ feeAssetPriceModifier: string;
122
+ };
123
+
124
+ /** Storage format for a proposed checkpoint (attested but not yet L1-confirmed). */
125
+ type ProposedCheckpointStorage = CommonCheckpointStorage & {
126
+ totalManaUsed: string;
127
+ feeAssetPriceModifier: string;
128
+ };
129
+
130
+ export type RemoveCheckpointsResult = { blocksRemoved: L2Block[] | undefined };
131
+
132
+ /**
133
+ * Single-block lookup with the chain-tip `tag` variant of {@link BlockQuery} already resolved
134
+ * to a concrete block number. The `tag` branch is unrepresentable here so storage code does
135
+ * not need to handle it at runtime.
136
+ */
137
+ export type ResolvedBlockQuery = { number: BlockNumber } | { hash: BlockHash } | { archive: Fr };
138
+
139
+ /**
140
+ * Range lookup with the `epoch` variant of {@link BlocksQuery} already resolved to a
141
+ * `{ from, limit }` pair. Storage code never needs to map epoch numbers to block ranges.
142
+ */
143
+ export type ResolvedBlocksQuery = { from: BlockNumber; limit: number; onlyCheckpointed?: boolean };
144
+
145
+ /**
146
+ * LMDB-based block storage for the archiver.
147
+ */
148
+ export class BlockStore {
149
+ /** Map block number to block data */
150
+ #blocks: AztecAsyncMap<number, BlockStorage>;
151
+
152
+ /** Map keyed by checkpoint number holding proposed (locally-validated, not yet L1-confirmed) checkpoints. */
153
+ #proposedCheckpoints: AztecAsyncMap<number, ProposedCheckpointStorage>;
154
+
155
+ /** Map checkpoint number to checkpoint data for mined checkpoints only */
156
+ #checkpoints: AztecAsyncMap<number, CheckpointStorage>;
157
+
158
+ /** Map slot number to checkpoint number, for looking up checkpoints by slot range. */
159
+ #slotToCheckpoint: AztecAsyncMap<number, number>;
160
+
161
+ /** Map block hash to list of tx hashes */
162
+ #blockTxs: AztecAsyncMap<string, Buffer>;
163
+
164
+ /** Tx hash to serialized IndexedTxEffect */
165
+ #txEffects: AztecAsyncMap<string, Buffer>;
166
+
167
+ /** Stores L1 block number in which the last processed L2 block was included */
168
+ #lastSynchedL1Block: AztecAsyncSingleton<bigint>;
169
+
170
+ /** Stores last proven checkpoint */
171
+ #lastProvenCheckpoint: AztecAsyncSingleton<number>;
172
+
173
+ /** Stores last finalized checkpoint (proven at or before the finalized L1 block) */
174
+ #lastFinalizedCheckpoint: AztecAsyncSingleton<number>;
175
+
176
+ /** Stores the pending chain validation status */
177
+ #pendingChainValidationStatus: AztecAsyncSingleton<Buffer>;
178
+
179
+ /** Index mapping a contract's address (as a string) to its location in a block */
180
+ #contractIndex: AztecAsyncMap<string, BlockIndexValue>;
181
+
182
+ /** Index mapping block hash to block number */
183
+ #blockHashIndex: AztecAsyncMap<string, number>;
184
+
185
+ /** Index mapping block archive to block number */
186
+ #blockArchiveIndex: AztecAsyncMap<string, number>;
187
+
188
+ /** Map rejected checkpoints (due to invalid attestations) by archive root */
189
+ #rejectedCheckpoints: AztecAsyncMap<string, RejectedCheckpointStorage>;
190
+
191
+ /** Index mapping a rejected checkpoint's number to its archive root, so the latest can be read in reverse order */
192
+ #rejectedCheckpointsByNumber: AztecAsyncMap<number, string>;
193
+
194
+ #log = createLogger('archiver:block_store');
195
+
196
+ constructor(private db: AztecAsyncKVStore) {
197
+ this.#blocks = db.openMap('archiver_blocks');
198
+ this.#blockTxs = db.openMap('archiver_block_txs');
199
+ this.#txEffects = db.openMap('archiver_tx_effects');
200
+ this.#contractIndex = db.openMap('archiver_contract_index');
201
+ this.#blockHashIndex = db.openMap('archiver_block_hash_index');
202
+ this.#blockArchiveIndex = db.openMap('archiver_block_archive_index');
203
+ this.#lastSynchedL1Block = db.openSingleton('archiver_last_synched_l1_block');
204
+ this.#lastProvenCheckpoint = db.openSingleton('archiver_last_proven_l2_checkpoint');
205
+ this.#lastFinalizedCheckpoint = db.openSingleton('archiver_last_finalized_l2_checkpoint');
206
+ this.#pendingChainValidationStatus = db.openSingleton('archiver_pending_chain_validation_status');
207
+ this.#checkpoints = db.openMap('archiver_checkpoints');
208
+ this.#slotToCheckpoint = db.openMap('archiver_slot_to_checkpoint');
209
+ this.#proposedCheckpoints = db.openMap('archiver_proposed_checkpoints');
210
+ this.#rejectedCheckpoints = db.openMap('archiver_rejected_checkpoints');
211
+ this.#rejectedCheckpointsByNumber = db.openMap('archiver_rejected_checkpoints_by_number');
212
+ }
213
+
214
+ /**
215
+ * Returns the finalized L2 block number. An L2 block is finalized when it was proven
216
+ * in an L1 block that has itself been finalized on Ethereum.
217
+ * @returns The finalized block number.
218
+ */
219
+ async getFinalizedL2BlockNumber(): Promise<BlockNumber> {
220
+ const finalizedCheckpointNumber = await this.getFinalizedCheckpointNumber();
221
+ if (finalizedCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
222
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
223
+ }
224
+ const checkpointStorage = await this.#checkpoints.getAsync(finalizedCheckpointNumber);
225
+ if (!checkpointStorage) {
226
+ throw new CheckpointNotFoundError(finalizedCheckpointNumber);
227
+ }
228
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
229
+ }
230
+
231
+ /**
232
+ * Append a new proposed block to the store.
233
+ * This is an uncheckpointed block that has been proposed by the sequencer but not yet included in a checkpoint on L1.
234
+ * For checkpointed blocks (already published to L1), use addCheckpoints() instead.
235
+ * @param block - The proposed L2 block to be added to the store.
236
+ * @returns True if the operation is successful.
237
+ */
238
+ async addProposedBlock(block: L2Block, opts: { force?: boolean } = {}): Promise<boolean> {
239
+ return await this.db.transactionAsync(async () => {
240
+ const blockNumber = block.number;
241
+ const blockCheckpointNumber = block.checkpointNumber;
242
+ const blockIndex = block.indexWithinCheckpoint;
243
+ const blockLastArchive = block.header.lastArchive.root;
244
+
245
+ // Extract the latest block and checkpoint numbers
246
+ const previousBlockNumber = await this.getLatestL2BlockNumber();
247
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
248
+
249
+ // Verify we're not overwriting checkpointed blocks
250
+ const lastCheckpointedBlockNumber = await this.getCheckpointedL2BlockNumber();
251
+ if (!opts.force && blockNumber <= lastCheckpointedBlockNumber) {
252
+ // Check if the proposed block matches the already-checkpointed one
253
+ const existingBlock = await this.getBlockData({ number: BlockNumber(blockNumber) });
254
+ if (existingBlock && existingBlock.archive.root.equals(block.archive.root)) {
255
+ throw new BlockAlreadyCheckpointedError(blockNumber);
256
+ }
257
+ throw new CannotOverwriteCheckpointedBlockError(blockNumber, lastCheckpointedBlockNumber);
258
+ }
259
+
260
+ // Check that the block number is the expected one
261
+ if (!opts.force && previousBlockNumber !== blockNumber - 1) {
262
+ throw new BlockNumberNotSequentialError(blockNumber, previousBlockNumber);
263
+ }
264
+
265
+ // Accept the block if either the confirmed checkpoint or a pending checkpoint matches
266
+ // the expected predecessor. We look for a pending entry at exactly blockCheckpointNumber - 1.
267
+ const expectedCheckpointNumber = blockCheckpointNumber - 1;
268
+ const hasPendingAtExpected = await this.#proposedCheckpoints.hasAsync(expectedCheckpointNumber);
269
+ if (!opts.force && latestCheckpointNumber !== expectedCheckpointNumber && !hasPendingAtExpected) {
270
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
271
+ const previous = CheckpointNumber(Math.max(latestCheckpointNumber, latestPendingKey ?? 0));
272
+ throw new BlockCheckpointNumberNotSequentialError(BlockNumber(blockNumber), blockCheckpointNumber, previous);
273
+ }
274
+
275
+ // Extract the previous block if there is one and see if it is for the same checkpoint or not
276
+ const previousBlockResult = await this.getBlockData({ number: previousBlockNumber });
277
+
278
+ let expectedBlockIndex = 0;
279
+ let previousBlockIndex: number | undefined = undefined;
280
+ if (previousBlockResult !== undefined) {
281
+ if (previousBlockResult.checkpointNumber === blockCheckpointNumber) {
282
+ // The previous block is for the same checkpoint, therefore our index should follow it
283
+ previousBlockIndex = previousBlockResult.indexWithinCheckpoint;
284
+ expectedBlockIndex = previousBlockIndex + 1;
285
+ }
286
+ if (!previousBlockResult.archive.root.equals(blockLastArchive)) {
287
+ throw new BlockArchiveNotConsistentError(
288
+ blockNumber,
289
+ previousBlockResult.header.globalVariables.blockNumber,
290
+ blockLastArchive,
291
+ previousBlockResult.archive.root,
292
+ );
293
+ }
294
+ }
295
+
296
+ // Now check that the block has the expected index value
297
+ if (!opts.force && expectedBlockIndex !== blockIndex) {
298
+ throw new BlockIndexNotSequentialError(blockIndex, previousBlockIndex);
299
+ }
300
+
301
+ await this.addBlockToDatabase(block, block.checkpointNumber, block.indexWithinCheckpoint);
302
+
303
+ return true;
304
+ });
305
+ }
306
+
307
+ /**
308
+ * Append new checkpoints to the store's list.
309
+ * Checkpoints at the start of the batch that are already stored (e.g. re-included by an L1 reorg)
310
+ * are accepted if their archive root matches: their L1 metadata is updated but they are not
311
+ * re-inserted, and they are excluded from the returned array.
312
+ * @param checkpoints - The L2 checkpoints to be added to the store.
313
+ * @returns The checkpoints that were actually inserted (excluding already-stored ones).
314
+ */
315
+ async addCheckpoints(
316
+ checkpoints: PublishedCheckpoint[],
317
+ opts: { force?: boolean } = {},
318
+ ): Promise<PublishedCheckpoint[]> {
319
+ if (checkpoints.length === 0) {
320
+ return [];
321
+ }
322
+
323
+ return await this.db.transactionAsync(async () => {
324
+ const firstCheckpointNumber = checkpoints[0].checkpoint.number;
325
+ const previousCheckpointNumber = await this.getLatestCheckpointNumber();
326
+
327
+ // Handle already-stored checkpoints at the start of the batch.
328
+ // This can happen after an L1 reorg re-includes a checkpoint in a different L1 block.
329
+ // We accept them if archives match (same content) and update their L1 metadata.
330
+ if (!opts.force && firstCheckpointNumber <= previousCheckpointNumber) {
331
+ checkpoints = await this.skipOrUpdateAlreadyStoredCheckpoints(checkpoints, previousCheckpointNumber);
332
+ if (checkpoints.length === 0) {
333
+ return [];
334
+ }
335
+ // Re-check sequentiality after skipping
336
+ const newFirstNumber = checkpoints[0].checkpoint.number;
337
+ if (previousCheckpointNumber !== newFirstNumber - 1) {
338
+ throw new InitialCheckpointNumberNotSequentialError(newFirstNumber, previousCheckpointNumber);
339
+ }
340
+ } else if (previousCheckpointNumber !== firstCheckpointNumber - 1 && !opts.force) {
341
+ throw new InitialCheckpointNumberNotSequentialError(firstCheckpointNumber, previousCheckpointNumber);
342
+ }
343
+
344
+ // Get the last block of the previous checkpoint for archive chaining
345
+ let previousBlock = await this.getPreviousCheckpointBlock(checkpoints[0].checkpoint.number);
346
+
347
+ // Iterate over checkpoints array and insert them, checking that the block numbers are sequential.
348
+ let previousCheckpoint: PublishedCheckpoint | undefined = undefined;
349
+ for (const checkpoint of checkpoints) {
350
+ if (
351
+ !opts.force &&
352
+ previousCheckpoint &&
353
+ previousCheckpoint.checkpoint.number + 1 !== checkpoint.checkpoint.number
354
+ ) {
355
+ throw new CheckpointNumberNotSequentialError(
356
+ checkpoint.checkpoint.number,
357
+ previousCheckpoint.checkpoint.number,
358
+ );
359
+ }
360
+ previousCheckpoint = checkpoint;
361
+
362
+ // Validate block sequencing, indexes, and archive chaining
363
+ this.validateCheckpointBlocks(checkpoint.checkpoint.blocks, previousBlock);
364
+
365
+ // Store every block in the database (may already exist, but L1 data is authoritative)
366
+ for (let i = 0; i < checkpoint.checkpoint.blocks.length; i++) {
367
+ await this.addBlockToDatabase(checkpoint.checkpoint.blocks[i], checkpoint.checkpoint.number, i);
368
+ }
369
+ previousBlock = checkpoint.checkpoint.blocks.at(-1);
370
+
371
+ // Store the checkpoint in the database
372
+ await this.#checkpoints.set(checkpoint.checkpoint.number, {
373
+ header: checkpoint.checkpoint.header.toBuffer(),
374
+ archive: checkpoint.checkpoint.archive.toBuffer(),
375
+ checkpointOutHash: checkpoint.checkpoint.getCheckpointOutHash().toBuffer(),
376
+ l1: checkpoint.l1.toBuffer(),
377
+ attestations: checkpoint.attestations.map(attestation => attestation.toBuffer()),
378
+ checkpointNumber: checkpoint.checkpoint.number,
379
+ startBlock: checkpoint.checkpoint.blocks[0].number,
380
+ blockCount: checkpoint.checkpoint.blocks.length,
381
+ feeAssetPriceModifier: checkpoint.checkpoint.feeAssetPriceModifier.toString(),
382
+ });
383
+
384
+ // Update slot-to-checkpoint index
385
+ await this.#slotToCheckpoint.set(checkpoint.checkpoint.header.slotNumber, checkpoint.checkpoint.number);
386
+
387
+ // Remove proposed checkpoint if it exists, since L1 is authoritative
388
+ await this.#proposedCheckpoints.delete(checkpoint.checkpoint.number);
389
+
390
+ // Drop any rejected entry for this archive root: a checkpoint that was previously rejected
391
+ // (e.g. invalid attestations) is now being ingested as valid, so its descendants are allowed.
392
+ await this.removeRejectedCheckpointByArchiveRoot(checkpoint.checkpoint.archive.root);
393
+ }
394
+
395
+ await this.advanceSynchedL1BlockNumber(checkpoints[checkpoints.length - 1].l1.blockNumber);
396
+ return checkpoints;
397
+ });
398
+ }
399
+
400
+ /**
401
+ * Handles checkpoints at the start of a batch that are already stored (e.g. due to L1 reorg).
402
+ * Verifies the archive root matches, updates L1 metadata, and returns only the new checkpoints.
403
+ */
404
+ private async skipOrUpdateAlreadyStoredCheckpoints(
405
+ checkpoints: PublishedCheckpoint[],
406
+ latestStored: CheckpointNumber,
407
+ ): Promise<PublishedCheckpoint[]> {
408
+ let i = 0;
409
+ for (; i < checkpoints.length && checkpoints[i].checkpoint.number <= latestStored; i++) {
410
+ const incoming = checkpoints[i];
411
+ const stored = await this.getCheckpointData(incoming.checkpoint.number);
412
+ if (!stored) {
413
+ // Should not happen if latestStored is correct, but be safe
414
+ break;
415
+ }
416
+ // Verify the checkpoint content matches (archive root)
417
+ if (!stored.archive.root.equals(incoming.checkpoint.archive.root)) {
418
+ throw new Error(
419
+ `Checkpoint ${incoming.checkpoint.number} already exists in store but with a different archive root. ` +
420
+ `Stored: ${stored.archive.root}, incoming: ${incoming.checkpoint.archive.root}`,
421
+ );
422
+ }
423
+ // Update L1 metadata and attestations for the already-stored checkpoint
424
+ this.#log.warn(
425
+ `Checkpoint ${incoming.checkpoint.number} already stored, updating L1 info ` +
426
+ `(L1 block ${stored.l1.blockNumber} -> ${incoming.l1.blockNumber})`,
427
+ );
428
+ await this.#checkpoints.set(incoming.checkpoint.number, {
429
+ header: incoming.checkpoint.header.toBuffer(),
430
+ archive: incoming.checkpoint.archive.toBuffer(),
431
+ checkpointOutHash: incoming.checkpoint.getCheckpointOutHash().toBuffer(),
432
+ l1: incoming.l1.toBuffer(),
433
+ attestations: incoming.attestations.map(a => a.toBuffer()),
434
+ checkpointNumber: incoming.checkpoint.number,
435
+ startBlock: incoming.checkpoint.blocks[0].number,
436
+ blockCount: incoming.checkpoint.blocks.length,
437
+ feeAssetPriceModifier: incoming.checkpoint.feeAssetPriceModifier.toString(),
438
+ });
439
+ // Update the sync point to reflect the new L1 block
440
+ await this.advanceSynchedL1BlockNumber(incoming.l1.blockNumber);
441
+ }
442
+ return checkpoints.slice(i);
443
+ }
444
+
445
+ /**
446
+ * Gets the last block of the checkpoint before the given one.
447
+ * Returns undefined if there is no previous checkpoint (i.e. genesis).
448
+ */
449
+ private async getPreviousCheckpointBlock(checkpointNumber: CheckpointNumber): Promise<L2Block | undefined> {
450
+ const previousCheckpointNumber = CheckpointNumber(checkpointNumber - 1);
451
+ if (previousCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
452
+ return undefined;
453
+ }
454
+
455
+ // Check across both proposed and mined checkpoints
456
+ const predecessor =
457
+ (await this.getProposedCheckpointByNumber(previousCheckpointNumber)) ??
458
+ (await this.getCheckpointData(previousCheckpointNumber));
459
+
460
+ if (!predecessor) {
461
+ throw new CheckpointNotFoundError(previousCheckpointNumber);
462
+ }
463
+
464
+ const previousBlockNumber = BlockNumber(predecessor.startBlock + predecessor.blockCount - 1);
465
+ const previousBlock = await this.getBlock({ number: previousBlockNumber });
466
+ if (previousBlock === undefined) {
467
+ throw new BlockNotFoundError(previousBlockNumber);
468
+ }
469
+ return previousBlock;
470
+ }
471
+
472
+ /**
473
+ * Validates that blocks are sequential, have correct indexes, and chain via archive roots.
474
+ * This is the same validation used for both confirmed checkpoints (addCheckpoints) and
475
+ * proposed checkpoints (addProposedCheckpoint).
476
+ */
477
+ private validateCheckpointBlocks(blocks: L2Block[], previousBlock: L2Block | undefined): void {
478
+ for (const block of blocks) {
479
+ if (previousBlock) {
480
+ if (previousBlock.number !== block.number - 1) {
481
+ throw new BlockNumberNotSequentialError(block.number, previousBlock.number);
482
+ }
483
+ if (previousBlock.checkpointNumber === block.checkpointNumber) {
484
+ if (previousBlock.indexWithinCheckpoint !== block.indexWithinCheckpoint - 1) {
485
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
486
+ }
487
+ } else if (block.indexWithinCheckpoint !== 0) {
488
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, previousBlock.indexWithinCheckpoint);
489
+ }
490
+ if (!previousBlock.archive.root.equals(block.header.lastArchive.root)) {
491
+ throw new BlockArchiveNotConsistentError(
492
+ block.number,
493
+ previousBlock.number,
494
+ block.header.lastArchive.root,
495
+ previousBlock.archive.root,
496
+ );
497
+ }
498
+ } else {
499
+ if (block.indexWithinCheckpoint !== 0) {
500
+ throw new BlockIndexNotSequentialError(block.indexWithinCheckpoint, undefined);
501
+ }
502
+ if (block.number !== INITIAL_L2_BLOCK_NUM) {
503
+ throw new BlockNumberNotSequentialError(block.number, undefined);
504
+ }
505
+ }
506
+ previousBlock = block;
507
+ }
508
+ }
509
+
510
+ private async addBlockToDatabase(block: L2Block, checkpointNumber: number, indexWithinCheckpoint: number) {
511
+ const blockHash = await block.hash();
512
+
513
+ await this.#blocks.set(block.number, {
514
+ header: block.header.toBuffer(),
515
+ blockHash: blockHash.toBuffer(),
516
+ archive: block.archive.toBuffer(),
517
+ checkpointNumber,
518
+ indexWithinCheckpoint,
519
+ });
520
+
521
+ for (let i = 0; i < block.body.txEffects.length; i++) {
522
+ const txEffect: IndexedTxEffect = {
523
+ data: block.body.txEffects[i],
524
+ l2BlockNumber: block.number,
525
+ l2BlockHash: blockHash,
526
+ txIndexInBlock: i,
527
+ slotNumber: block.header.globalVariables.slotNumber,
528
+ };
529
+ await this.#txEffects.set(txEffect.data.txHash.toString(), serializeIndexedTxEffect(txEffect));
530
+ }
531
+
532
+ await this.#blockTxs.set(blockHash.toString(), Buffer.concat(block.body.txEffects.map(tx => tx.txHash.toBuffer())));
533
+
534
+ // Update indices for block hash and archive
535
+ await this.#blockHashIndex.set(blockHash.toString(), block.number);
536
+ await this.#blockArchiveIndex.set(block.archive.root.toString(), block.number);
537
+ }
538
+
539
+ /** Deletes a block and all associated data (tx effects, indices). */
540
+ private async deleteBlock(block: L2Block): Promise<void> {
541
+ // Delete the block from the main blocks map
542
+ await this.#blocks.delete(block.number);
543
+
544
+ // Delete all tx effects for this block
545
+ await Promise.all(block.body.txEffects.map(tx => this.#txEffects.delete(tx.txHash.toString())));
546
+
547
+ // Delete block txs mapping
548
+ const blockHash = (await block.hash()).toString();
549
+ await this.#blockTxs.delete(blockHash);
550
+
551
+ // Clean up indices
552
+ await this.#blockHashIndex.delete(blockHash);
553
+ await this.#blockArchiveIndex.delete(block.archive.root.toString());
554
+ }
555
+
556
+ /**
557
+ * Removes all checkpoints with checkpoint number > checkpointNumber.
558
+ * Also removes ALL blocks (both checkpointed and uncheckpointed) after the last block of the given checkpoint.
559
+ * @param checkpointNumber - Remove all checkpoints strictly after this one.
560
+ */
561
+ async removeCheckpointsAfter(checkpointNumber: CheckpointNumber): Promise<RemoveCheckpointsResult> {
562
+ return await this.db.transactionAsync(async () => {
563
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
564
+
565
+ if (checkpointNumber >= latestCheckpointNumber) {
566
+ this.#log.warn(`No checkpoints to remove after ${checkpointNumber} (latest is ${latestCheckpointNumber})`);
567
+ return { blocksRemoved: undefined };
568
+ }
569
+
570
+ // If the proven checkpoint is beyond the target, update it
571
+ const proven = await this.getProvenCheckpointNumber();
572
+ if (proven > checkpointNumber) {
573
+ this.#log.warn(`Updating proven checkpoint ${proven} to last valid checkpoint ${checkpointNumber}`);
574
+ await this.setProvenCheckpointNumber(checkpointNumber);
575
+ }
576
+
577
+ // Find the last block number to keep (last block of the given checkpoint, or 0 if no checkpoint)
578
+ let lastBlockToKeep: BlockNumber;
579
+ if (checkpointNumber <= 0) {
580
+ lastBlockToKeep = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
581
+ } else {
582
+ const targetCheckpoint = await this.#checkpoints.getAsync(checkpointNumber);
583
+ if (!targetCheckpoint) {
584
+ throw new Error(`Target checkpoint ${checkpointNumber} not found in store`);
585
+ }
586
+ lastBlockToKeep = BlockNumber(targetCheckpoint.startBlock + targetCheckpoint.blockCount - 1);
587
+ }
588
+
589
+ // Remove all blocks after lastBlockToKeep (both checkpointed and uncheckpointed)
590
+ const blocksRemoved = await this.removeBlocksAfter(lastBlockToKeep);
591
+
592
+ // Remove all checkpoints after the target
593
+ for (let c = latestCheckpointNumber; c > checkpointNumber; c = CheckpointNumber(c - 1)) {
594
+ const checkpointStorage = await this.#checkpoints.getAsync(c);
595
+ if (checkpointStorage) {
596
+ const slotNumber = CheckpointHeader.fromBuffer(checkpointStorage.header).slotNumber;
597
+ await this.#slotToCheckpoint.delete(slotNumber);
598
+ }
599
+ await this.#checkpoints.delete(c);
600
+ this.#log.debug(`Removed checkpoint ${c}`);
601
+ }
602
+
603
+ // Evict all pending checkpoints > checkpointNumber (their base chain no longer exists)
604
+ await this.evictProposedCheckpointsFrom(CheckpointNumber(checkpointNumber + 1));
605
+
606
+ return { blocksRemoved };
607
+ });
608
+ }
609
+
610
+ async getCheckpointData(checkpointNumber: CheckpointNumber): Promise<CheckpointData | undefined> {
611
+ const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
612
+ if (!checkpointStorage) {
613
+ return undefined;
614
+ }
615
+ return this.checkpointDataFromCheckpointStorage(checkpointStorage);
616
+ }
617
+
618
+ async getRangeOfCheckpoints(from: CheckpointNumber, limit: number): Promise<CheckpointData[]> {
619
+ const checkpoints: CheckpointData[] = [];
620
+ for (let checkpointNumber = from; checkpointNumber < from + limit; checkpointNumber++) {
621
+ const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
622
+ if (!checkpoint) {
623
+ break;
624
+ }
625
+ checkpoints.push(this.checkpointDataFromCheckpointStorage(checkpoint));
626
+ }
627
+ return checkpoints;
628
+ }
629
+
630
+ /**
631
+ * Returns up to `limit` checkpoints anchored at `fromSlot`, ordered nearest-first, walking the slot index.
632
+ * With `reverse`, takes the checkpoints at or before `fromSlot` (descending by slot); otherwise the
633
+ * checkpoints at or after it (ascending). `limit: 1, reverse: true` yields the latest checkpoint at or
634
+ * before the slot in a single range scan.
635
+ */
636
+ async getCheckpointsBySlot(fromSlot: SlotNumber, limit: number, reverse: boolean): Promise<CheckpointData[]> {
637
+ // The KV range bounds are direction-dependent: forward is [start, end), reverse is (start, end], so a
638
+ // reverse scan uses `end: fromSlot` (inclusive) with no +1 to include the checkpoint at fromSlot itself.
639
+ const range = reverse ? { end: fromSlot, reverse: true, limit } : { start: fromSlot, limit };
640
+ const result: CheckpointData[] = [];
641
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync(range)) {
642
+ const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
643
+ if (checkpointStorage) {
644
+ result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
645
+ }
646
+ }
647
+ return result;
648
+ }
649
+
650
+ /** Returns checkpoint data for all checkpoints whose slot falls within the given range (inclusive). */
651
+ async getCheckpointDataForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointData[]> {
652
+ const result: CheckpointData[] = [];
653
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
654
+ start: startSlot,
655
+ end: endSlot + 1,
656
+ })) {
657
+ const checkpointStorage = await this.#checkpoints.getAsync(checkpointNumber);
658
+ if (checkpointStorage) {
659
+ result.push(this.checkpointDataFromCheckpointStorage(checkpointStorage));
660
+ }
661
+ }
662
+ return result;
663
+ }
664
+
665
+ /**
666
+ * Returns the checkpoint numbers for all checkpoints whose slot falls within the given range (inclusive).
667
+ * Lighter than {@link getCheckpointDataForSlotRange} when callers only need to identify which
668
+ * checkpoints fall in the range and will fetch full data for at most a few of them.
669
+ */
670
+ async getCheckpointNumbersForSlotRange(startSlot: SlotNumber, endSlot: SlotNumber): Promise<CheckpointNumber[]> {
671
+ const result: CheckpointNumber[] = [];
672
+ for await (const [, checkpointNumber] of this.#slotToCheckpoint.entriesAsync({
673
+ start: startSlot,
674
+ end: endSlot + 1,
675
+ })) {
676
+ result.push(CheckpointNumber(checkpointNumber));
677
+ }
678
+ return result;
679
+ }
680
+
681
+ private checkpointDataFromCheckpointStorage(checkpointStorage: CheckpointStorage): CheckpointData {
682
+ return {
683
+ header: CheckpointHeader.fromBuffer(checkpointStorage.header),
684
+ archive: AppendOnlyTreeSnapshot.fromBuffer(checkpointStorage.archive),
685
+ checkpointOutHash: Fr.fromBuffer(checkpointStorage.checkpointOutHash),
686
+ checkpointNumber: CheckpointNumber(checkpointStorage.checkpointNumber),
687
+ startBlock: BlockNumber(checkpointStorage.startBlock),
688
+ blockCount: checkpointStorage.blockCount,
689
+ feeAssetPriceModifier: BigInt(checkpointStorage.feeAssetPriceModifier),
690
+ l1: L1PublishedData.fromBuffer(checkpointStorage.l1),
691
+ attestations: checkpointStorage.attestations.map(buf => CommitteeAttestation.fromBuffer(buf)),
692
+ };
693
+ }
694
+
695
+ async getBlocksForCheckpoint(checkpointNumber: CheckpointNumber): Promise<L2Block[] | undefined> {
696
+ const checkpoint = await this.#checkpoints.getAsync(checkpointNumber);
697
+ if (!checkpoint) {
698
+ return undefined;
699
+ }
700
+
701
+ const blocksForCheckpoint = await toArray(
702
+ this.#blocks.entriesAsync({
703
+ start: checkpoint.startBlock,
704
+ end: checkpoint.startBlock + checkpoint.blockCount,
705
+ }),
706
+ );
707
+
708
+ const converted = await Promise.all(blocksForCheckpoint.map(x => this.getBlockFromBlockStorage(x[0], x[1])));
709
+ return converted.filter(isDefined);
710
+ }
711
+
712
+ /**
713
+ * Gets all blocks that have the given slot number.
714
+ * Iterates backwards through blocks for efficiency since we usually query for the last slot.
715
+ * @param slotNumber - The slot number to search for.
716
+ * @returns All blocks with the given slot number, in ascending block number order.
717
+ */
718
+ async getBlocksForSlot(slotNumber: SlotNumber): Promise<L2Block[]> {
719
+ const blocks: L2Block[] = [];
720
+
721
+ // Iterate backwards through all blocks and filter by slot number
722
+ // This is more efficient since we usually query for the most recent slot
723
+ for await (const [blockNumber, blockStorage] of this.#blocks.entriesAsync({ reverse: true })) {
724
+ const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
725
+ const blockSlot = block?.header.globalVariables.slotNumber;
726
+ if (block && blockSlot === slotNumber) {
727
+ blocks.push(block);
728
+ } else if (blockSlot && blockSlot < slotNumber) {
729
+ break; // Blocks are stored in slot ascending order, so we can stop searching
730
+ }
731
+ }
732
+
733
+ // Reverse to return blocks in ascending order (block number order)
734
+ return blocks.reverse();
735
+ }
736
+
737
+ /**
738
+ * Removes all blocks with block number > blockNumber.
739
+ * Does not remove any associated checkpoints.
740
+ * @param blockNumber - The block number to remove after.
741
+ * @returns The removed blocks (for event emission).
742
+ */
743
+ async removeBlocksAfter(blockNumber: BlockNumber): Promise<L2Block[]> {
744
+ return await this.db.transactionAsync(async () => {
745
+ const removedBlocks: L2Block[] = [];
746
+
747
+ // Get the latest block number to determine the range
748
+ const latestBlockNumber = await this.getLatestL2BlockNumber();
749
+
750
+ // Iterate from blockNumber + 1 to latestBlockNumber
751
+ for (let bn = blockNumber + 1; bn <= latestBlockNumber; bn++) {
752
+ const block = await this.getBlock({ number: BlockNumber(bn) });
753
+
754
+ if (block === undefined) {
755
+ this.#log.warn(`Cannot remove block ${bn} from the store since we don't have it`);
756
+ continue;
757
+ }
758
+
759
+ removedBlocks.push(block);
760
+ await this.deleteBlock(block);
761
+ this.#log.debug(`Removed block ${bn} ${(await block.hash()).toString()}`);
762
+ }
763
+
764
+ return removedBlocks;
765
+ });
766
+ }
767
+
768
+ async getProvenBlockNumber(): Promise<BlockNumber> {
769
+ const provenCheckpointNumber = await this.getProvenCheckpointNumber();
770
+ if (provenCheckpointNumber === INITIAL_CHECKPOINT_NUMBER - 1) {
771
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
772
+ }
773
+ const checkpointStorage = await this.#checkpoints.getAsync(provenCheckpointNumber);
774
+ if (!checkpointStorage) {
775
+ throw new CheckpointNotFoundError(provenCheckpointNumber);
776
+ } else {
777
+ return BlockNumber(checkpointStorage.startBlock + checkpointStorage.blockCount - 1);
778
+ }
779
+ }
780
+
781
+ async getLatestCheckpointNumber(): Promise<CheckpointNumber> {
782
+ const [latestCheckpointNumber] = await toArray(this.#checkpoints.keysAsync({ reverse: true, limit: 1 }));
783
+ if (latestCheckpointNumber === undefined) {
784
+ return CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
785
+ }
786
+ return CheckpointNumber(latestCheckpointNumber);
787
+ }
788
+
789
+ async hasProposedCheckpoint(): Promise<boolean> {
790
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ limit: 1 }));
791
+ return key !== undefined;
792
+ }
793
+
794
+ /** Deletes all pending proposed checkpoints from storage. */
795
+ async deleteProposedCheckpoints(): Promise<void> {
796
+ for await (const key of this.#proposedCheckpoints.keysAsync()) {
797
+ await this.#proposedCheckpoints.delete(key);
798
+ }
799
+ }
800
+
801
+ /**
802
+ * Promotes a specific pending checkpoint to a confirmed checkpoint entry.
803
+ * This persists the checkpoint to the store, removes only that pending entry, and updates the L1 sync point.
804
+ * Remaining pending entries (e.g. N+1, N+2) are left intact — they chain off the just-promoted one.
805
+ * @param checkpointNumber - The checkpoint number to promote.
806
+ * @param l1 - L1 published data for the checkpoint.
807
+ * @param attestations - Committee attestations.
808
+ * @param expectedArchiveRoot - Archive root guard against races.
809
+ */
810
+ async promoteProposedToCheckpointed(
811
+ checkpointNumber: CheckpointNumber,
812
+ l1: L1PublishedData,
813
+ attestations: CommitteeAttestation[],
814
+ expectedArchiveRoot: Fr,
815
+ ): Promise<void> {
816
+ return await this.db.transactionAsync(async () => {
817
+ const proposed = await this.getProposedCheckpointByNumber(checkpointNumber);
818
+ if (!proposed) {
819
+ throw new NoProposedCheckpointToPromoteError();
820
+ }
821
+ if (!proposed.archive.root.equals(expectedArchiveRoot)) {
822
+ throw new ProposedCheckpointArchiveRootMismatchError(expectedArchiveRoot, proposed.archive.root);
823
+ }
824
+
825
+ // Verify sequentiality: promoted checkpoint must follow the latest confirmed one
826
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
827
+ if (latestCheckpointNumber !== proposed.checkpointNumber - 1) {
828
+ throw new ProposedCheckpointPromotionNotSequentialError(proposed.checkpointNumber, latestCheckpointNumber);
829
+ }
830
+
831
+ // Write the checkpoint entry
832
+ await this.#checkpoints.set(proposed.checkpointNumber, {
833
+ header: proposed.header.toBuffer(),
834
+ archive: proposed.archive.toBuffer(),
835
+ checkpointOutHash: proposed.checkpointOutHash.toBuffer(),
836
+ l1: l1.toBuffer(),
837
+ attestations: attestations.map(attestation => attestation.toBuffer()),
838
+ checkpointNumber: proposed.checkpointNumber,
839
+ startBlock: proposed.startBlock,
840
+ blockCount: proposed.blockCount,
841
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
842
+ });
843
+
844
+ // Update the slot-to-checkpoint index
845
+ await this.#slotToCheckpoint.set(proposed.header.slotNumber, proposed.checkpointNumber);
846
+
847
+ // Remove only this pending entry — remaining entries N+1, N+2, ... stay valid
848
+ await this.#proposedCheckpoints.delete(proposed.checkpointNumber);
849
+
850
+ // Drop any rejected entry for this archive root: a checkpoint that was previously rejected
851
+ // (e.g. invalid attestations) is now being promoted as valid, so its descendants are allowed.
852
+ await this.removeRejectedCheckpointByArchiveRoot(proposed.archive.root);
853
+
854
+ // Update the last synced L1 block
855
+ await this.advanceSynchedL1BlockNumber(l1.blockNumber);
856
+ });
857
+ }
858
+
859
+ /**
860
+ * Returns the latest pending checkpoint (highest-numbered entry), or undefined if none.
861
+ * No fallback to confirmed.
862
+ */
863
+ async getLastProposedCheckpoint(): Promise<ProposedCheckpointData | undefined> {
864
+ const [key] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
865
+ if (key === undefined) {
866
+ return undefined;
867
+ }
868
+ const stored = await this.#proposedCheckpoints.getAsync(key);
869
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
870
+ }
871
+
872
+ /** Returns the pending checkpoint for a specific checkpoint number, or undefined if not found. */
873
+ async getProposedCheckpointByNumber(n: CheckpointNumber): Promise<ProposedCheckpointData | undefined> {
874
+ const stored = await this.#proposedCheckpoints.getAsync(n);
875
+ return stored ? this.convertToProposedCheckpointData(stored) : undefined;
876
+ }
877
+
878
+ /**
879
+ * Returns the pending checkpoint whose header slot matches the given slot, or undefined if not found.
880
+ * Iterates `#proposedCheckpoints` rather than reading an index because the map carries 0–1 entries
881
+ * in normal operation (bounded by the proposer pipelining window). Returns the first match.
882
+ */
883
+ async getProposedCheckpointBySlot(slot: SlotNumber): Promise<ProposedCheckpointData | undefined> {
884
+ for await (const [, stored] of this.#proposedCheckpoints.entriesAsync()) {
885
+ const header = CheckpointHeader.fromBuffer(stored.header);
886
+ if (header.slotNumber === slot) {
887
+ return this.convertToProposedCheckpointData(stored);
888
+ }
889
+ }
890
+ return undefined;
891
+ }
892
+
893
+ /**
894
+ * Evicts all pending checkpoints with checkpoint number >= fromNumber.
895
+ * Used for divergent-mined-checkpoint cleanup: when L1 mines checkpoint N with a different archive,
896
+ * all pending >= N must be evicted since they chain off the now-invalid pending N.
897
+ */
898
+ async evictProposedCheckpointsFrom(fromNumber: CheckpointNumber): Promise<void> {
899
+ const keysToDelete: number[] = [];
900
+ for await (const key of this.#proposedCheckpoints.keysAsync()) {
901
+ if (key >= fromNumber) {
902
+ keysToDelete.push(key);
903
+ }
904
+ }
905
+ for (const key of keysToDelete) {
906
+ await this.#proposedCheckpoints.delete(key);
907
+ }
908
+ }
909
+
910
+ /**
911
+ * Gets the checkpoint at the proposed tip:
912
+ * - latest pending checkpoint if any exist
913
+ * - fallsback to latest confirmed checkpoint otherwise
914
+ */
915
+ async getLastCheckpoint(): Promise<CommonCheckpointData | undefined> {
916
+ const latest = await this.getLastProposedCheckpoint();
917
+ if (!latest) {
918
+ return this.getCheckpointData(await this.getLatestCheckpointNumber());
919
+ }
920
+ return latest;
921
+ }
922
+
923
+ private convertToProposedCheckpointData(stored: ProposedCheckpointStorage): ProposedCheckpointData {
924
+ return {
925
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
926
+ header: CheckpointHeader.fromBuffer(stored.header),
927
+ archive: AppendOnlyTreeSnapshot.fromBuffer(stored.archive),
928
+ checkpointOutHash: Fr.fromBuffer(stored.checkpointOutHash),
929
+ startBlock: BlockNumber(stored.startBlock),
930
+ blockCount: stored.blockCount,
931
+ totalManaUsed: BigInt(stored.totalManaUsed),
932
+ feeAssetPriceModifier: BigInt(stored.feeAssetPriceModifier),
933
+ };
934
+ }
935
+
936
+ /**
937
+ * Attempts to get the proposedCheckpoint's number, if there is not one, then fallback to the latest confirmed checkpoint number.
938
+ * @returns CheckpointNumber
939
+ */
940
+ async getProposedCheckpointNumber(): Promise<CheckpointNumber> {
941
+ const proposed = await this.getLastCheckpoint();
942
+ if (!proposed) {
943
+ return await this.getLatestCheckpointNumber();
944
+ }
945
+ return CheckpointNumber(proposed.checkpointNumber);
946
+ }
947
+
948
+ /**
949
+ * Attempts to get the proposedCheckpoint's block number, if there is not one, then fallback to the checkpointed block number
950
+ * @returns BlockNumber
951
+ */
952
+ async getProposedCheckpointL2BlockNumber(): Promise<BlockNumber> {
953
+ const proposed = await this.getLastCheckpoint();
954
+ if (!proposed) {
955
+ return await this.getCheckpointedL2BlockNumber();
956
+ }
957
+ return BlockNumber(proposed.startBlock + proposed.blockCount - 1);
958
+ }
959
+
960
+ /** Returns the checkpoint number that contains the given slot (or undefined if not found). */
961
+ async getCheckpointNumberBySlot(slot: SlotNumber): Promise<CheckpointNumber | undefined> {
962
+ const checkpointNumber = await this.#slotToCheckpoint.getAsync(slot);
963
+ return checkpointNumber === undefined ? undefined : CheckpointNumber(checkpointNumber);
964
+ }
965
+
966
+ /** Gets a single L2 block matching the given resolved query. */
967
+ async getBlock(query: ResolvedBlockQuery): Promise<L2Block | undefined> {
968
+ const blockNumber = await this.getBlockNumber(query);
969
+ if (blockNumber === undefined) {
970
+ return undefined;
971
+ }
972
+ const blockStorage = await this.#blocks.getAsync(blockNumber);
973
+ if (!blockStorage) {
974
+ return undefined;
975
+ }
976
+ return this.getBlockFromBlockStorage(blockNumber, blockStorage);
977
+ }
978
+
979
+ /** Gets a collection of L2 blocks for a resolved range. */
980
+ getBlocks(query: ResolvedBlocksQuery): Promise<L2Block[]> {
981
+ return toArray(this.iterateBlocks(query));
982
+ }
983
+
984
+ /** Gets single block metadata matching the given resolved query. */
985
+ async getBlockData(query: ResolvedBlockQuery): Promise<BlockData | undefined> {
986
+ const blockNumber = await this.getBlockNumber(query);
987
+ if (blockNumber === undefined) {
988
+ return undefined;
989
+ }
990
+ const blockStorage = await this.#blocks.getAsync(blockNumber);
991
+ if (!blockStorage || !blockStorage.header) {
992
+ return undefined;
993
+ }
994
+ return this.getBlockDataFromBlockStorage(blockStorage);
995
+ }
996
+
997
+ /** Gets a collection of block metadata entries for a resolved range. */
998
+ getBlocksData(query: ResolvedBlocksQuery): Promise<BlockData[]> {
999
+ return toArray(this.iterateBlocksData(query));
1000
+ }
1001
+
1002
+ /** Async iterator over L2 blocks for a resolved range. */
1003
+ private async *iterateBlocks(query: ResolvedBlocksQuery): AsyncIterableIterator<L2Block> {
1004
+ const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
1005
+ for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)) {
1006
+ if (cap !== undefined && blockNumber > cap) {
1007
+ break;
1008
+ }
1009
+ const block = await this.getBlockFromBlockStorage(blockNumber, blockStorage);
1010
+ if (block) {
1011
+ yield block;
1012
+ }
1013
+ }
1014
+ }
1015
+
1016
+ /** Async iterator over block metadata for a resolved range. */
1017
+ private async *iterateBlocksData(query: ResolvedBlocksQuery): AsyncIterableIterator<BlockData> {
1018
+ const cap = query.onlyCheckpointed ? await this.getCheckpointedL2BlockNumber() : undefined;
1019
+ for await (const [blockNumber, blockStorage] of this.getBlockStorages(query.from, query.limit)) {
1020
+ if (cap !== undefined && blockNumber > cap) {
1021
+ break;
1022
+ }
1023
+ yield this.getBlockDataFromBlockStorage(blockStorage);
1024
+ }
1025
+ }
1026
+
1027
+ private async *getBlockStorages(start: BlockNumber, limit: number) {
1028
+ let expectedBlockNumber = start;
1029
+ for await (const [blockNumber, blockStorage] of this.#blocks.entriesAsync(this.#computeBlockRange(start, limit))) {
1030
+ if (blockNumber !== expectedBlockNumber) {
1031
+ throw new Error(
1032
+ `Block number mismatch when iterating blocks from archive (expected ${expectedBlockNumber} but got ${blockNumber})`,
1033
+ );
1034
+ }
1035
+ expectedBlockNumber++;
1036
+ yield [blockNumber, blockStorage] as const;
1037
+ }
1038
+ }
1039
+
1040
+ /** Resolves a ResolvedBlockQuery discriminant to a block number, or undefined if not found. */
1041
+ async getBlockNumber(query: ResolvedBlockQuery): Promise<BlockNumber | undefined> {
1042
+ let blockNumber: BlockNumber | undefined;
1043
+ if ('number' in query) {
1044
+ blockNumber = query.number;
1045
+ } else if ('hash' in query) {
1046
+ const n = await this.#blockHashIndex.getAsync(query.hash.toString());
1047
+ blockNumber = n !== undefined ? BlockNumber(n) : undefined;
1048
+ } else {
1049
+ const n = await this.#blockArchiveIndex.getAsync(query.archive.toString());
1050
+ blockNumber = n !== undefined ? BlockNumber(n) : undefined;
1051
+ }
1052
+ if (blockNumber === undefined) {
1053
+ return undefined;
1054
+ }
1055
+ return blockNumber;
1056
+ }
1057
+
1058
+ private getBlockDataFromBlockStorage(blockStorage: BlockStorage): BlockData {
1059
+ return {
1060
+ header: BlockHeader.fromBuffer(blockStorage.header),
1061
+ archive: AppendOnlyTreeSnapshot.fromBuffer(blockStorage.archive),
1062
+ blockHash: BlockHash.fromBuffer(blockStorage.blockHash),
1063
+ checkpointNumber: CheckpointNumber(blockStorage.checkpointNumber),
1064
+ indexWithinCheckpoint: IndexWithinCheckpoint(blockStorage.indexWithinCheckpoint),
1065
+ };
1066
+ }
1067
+
1068
+ private async getBlockFromBlockStorage(
1069
+ blockNumber: number,
1070
+ blockStorage: BlockStorage,
1071
+ ): Promise<L2Block | undefined> {
1072
+ const { header, archive, blockHash, checkpointNumber, indexWithinCheckpoint } =
1073
+ this.getBlockDataFromBlockStorage(blockStorage);
1074
+ header.setHash(blockHash);
1075
+ const blockHashString = bufferToHex(blockStorage.blockHash);
1076
+ const blockTxsBuffer = await this.#blockTxs.getAsync(blockHashString);
1077
+ if (blockTxsBuffer === undefined) {
1078
+ this.#log.warn(`Could not find body for block ${header.globalVariables.blockNumber} ${blockHash}`);
1079
+ return undefined;
1080
+ }
1081
+
1082
+ const txEffects: TxEffect[] = [];
1083
+ const reader = BufferReader.asReader(blockTxsBuffer);
1084
+ while (!reader.isEmpty()) {
1085
+ const txHash = reader.readObject(TxHash);
1086
+ const txEffect = await this.#txEffects.getAsync(txHash.toString());
1087
+ if (txEffect === undefined) {
1088
+ this.#log.warn(`Could not find tx effect for tx ${txHash} in block ${blockNumber}`);
1089
+ return undefined;
1090
+ }
1091
+ txEffects.push(deserializeIndexedTxEffect(txEffect).data);
1092
+ }
1093
+ const body = new Body(txEffects);
1094
+ const block = new L2Block(archive, header, body, checkpointNumber, indexWithinCheckpoint);
1095
+
1096
+ if (block.number !== blockNumber) {
1097
+ throw new Error(
1098
+ `Block number mismatch when retrieving block from archive (expected ${blockNumber} but got ${
1099
+ block.number
1100
+ } with hash ${blockHashString})`,
1101
+ );
1102
+ }
1103
+ return block;
1104
+ }
1105
+
1106
+ /**
1107
+ * Gets a tx effect.
1108
+ * @param txHash - The hash of the tx corresponding to the tx effect.
1109
+ * @returns The requested tx effect with block info (or undefined if not found).
1110
+ */
1111
+ async getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
1112
+ const buffer = await this.#txEffects.getAsync(txHash.toString());
1113
+ if (!buffer) {
1114
+ return undefined;
1115
+ }
1116
+ return deserializeIndexedTxEffect(buffer);
1117
+ }
1118
+
1119
+ /**
1120
+ * Looks up which block included the requested tx effect.
1121
+ * @param txHash - The txHash of the tx.
1122
+ * @returns The block number and index of the tx.
1123
+ */
1124
+ public async getTxLocation(txHash: TxHash): Promise<[blockNumber: number, txIndex: number] | undefined> {
1125
+ const txEffect = await this.#txEffects.getAsync(txHash.toString());
1126
+ if (!txEffect) {
1127
+ return undefined;
1128
+ }
1129
+ // Read only the IndexedTxEffect header (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4)`); the
1130
+ // large tail (the full TxEffect with logs etc.) is irrelevant here.
1131
+ const view = Buffer.from(txEffect.buffer, txEffect.byteOffset, txEffect.byteLength);
1132
+ const l2BlockNumber = view.readUInt32BE(32);
1133
+ const txIndexInBlock = view.readUInt32BE(36);
1134
+ return [l2BlockNumber, txIndexInBlock];
1135
+ }
1136
+
1137
+ /**
1138
+ * Batched, partial deserializer that fetches `noteHashes` and `nullifiers` (all of them) for the given
1139
+ * txs. For each input txHash, returns a `[noteHashes, nullifiers]` tuple. Returns `[[], []]` for any
1140
+ * unknown txHash. Preserves input order. Used by the log read path when `includeEffects` is set to
1141
+ * attach effect data on demand without paying for a full {@link TxEffect} deserialization.
1142
+ *
1143
+ * The on-disk `IndexedTxEffect` layout starts with a fixed-length header
1144
+ * (`blockHash(32) + l2BlockNumber(4) + txIndexInBlock(4) + slotNumber(4) + revertCode(1) + txHash(32) +
1145
+ * transactionFee(32)` = 109 bytes), followed by `noteHashes` and `nullifiers` (both u8-length-prefixed `Fr`
1146
+ * vectors). We
1147
+ * skip the header, then read the two vectors, and stop — the large tail (`l2ToL1Msgs`,
1148
+ * `publicDataWrites`, `privateLogs`, `publicLogs`, `contractClassLogs`) is never touched.
1149
+ */
1150
+ public getNoteHashesAndNullifiers(txHashes: TxHash[]): Promise<[Fr[], Fr[]][]> {
1151
+ return Promise.all(
1152
+ txHashes.map(async (txHash): Promise<[Fr[], Fr[]]> => {
1153
+ const buffer = await this.#txEffects.getAsync(txHash.toString());
1154
+ if (!buffer) {
1155
+ return [[], []];
1156
+ }
1157
+ const reader = BufferReader.asReader(buffer);
1158
+ // Skip the fixed-length header: blockHash + l2BlockNumber + txIndexInBlock + slotNumber + revertCode +
1159
+ // txHash + transactionFee.
1160
+ reader.readBytes(32 + 4 + 4 + 4 + 1 + 32 + 32);
1161
+ const noteHashes = reader.readVectorUint8Prefix(Fr);
1162
+ const nullifiers = reader.readVectorUint8Prefix(Fr);
1163
+ return [noteHashes, nullifiers];
1164
+ }),
1165
+ );
1166
+ }
1167
+
1168
+ /**
1169
+ * Looks up which block deployed a particular contract.
1170
+ * @param contractAddress - The address of the contract to look up.
1171
+ * @returns The block number and index of the contract.
1172
+ */
1173
+ getContractLocation(contractAddress: AztecAddress): Promise<[blockNumber: number, index: number] | undefined> {
1174
+ return this.#contractIndex.getAsync(contractAddress.toString());
1175
+ }
1176
+
1177
+ /**
1178
+ * Gets the number of the latest L2 block checkpointed.
1179
+ * @returns The number of the latest L2 block checkpointed.
1180
+ */
1181
+ async getCheckpointedL2BlockNumber(): Promise<BlockNumber> {
1182
+ const latestCheckpointNumber = await this.getLatestCheckpointNumber();
1183
+ const checkpoint = await this.getCheckpointData(latestCheckpointNumber);
1184
+ if (!checkpoint) {
1185
+ return BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
1186
+ }
1187
+ return BlockNumber(checkpoint.startBlock + checkpoint.blockCount - 1);
1188
+ }
1189
+
1190
+ async getLatestL2BlockNumber(): Promise<BlockNumber> {
1191
+ const [lastBlockNumber] = await toArray(this.#blocks.keysAsync({ reverse: true, limit: 1 }));
1192
+ return typeof lastBlockNumber === 'number' ? BlockNumber(lastBlockNumber) : BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
1193
+ }
1194
+
1195
+ /**
1196
+ * Resolves all four L2 chain tips (proposed, checkpointed, proven, finalized) in a single
1197
+ * read-only transaction so the snapshot is internally consistent. Each underlying record is
1198
+ * read at most once: latest block and latest confirmed checkpoint are loaded directly (no
1199
+ * separate "find the number, then look up data" hop), the proven/finalized checkpoint
1200
+ * singletons are read once and their storage entries are reused if they coincide with the
1201
+ * latest checkpoint, and per-tip block hashes are deduped when two tips land on the same block
1202
+ * (e.g. finalized == proven).
1203
+ *
1204
+ * The result is guaranteed to satisfy `finalized <= proven <= checkpointed <= proposed` (by
1205
+ * block number). Genesis is represented by `(INITIAL_L2_BLOCK_NUM - 1)` and the supplied
1206
+ * `genesisBlockHash`, paired with the synthetic genesis checkpoint id.
1207
+ *
1208
+ * @param genesisBlockHash - Block hash to report for the synthetic pre-initial block (used when
1209
+ * a tip is still at genesis).
1210
+ */
1211
+ async getL2TipsData(genesisBlockHash: BlockHash): Promise<L2Tips> {
1212
+ return await this.db.transactionAsync(async () => {
1213
+ // Define genesis tips
1214
+ const genesisBlockNumber = BlockNumber(INITIAL_L2_BLOCK_NUM - 1);
1215
+ const genesisCheckpointNumber = CheckpointNumber(INITIAL_CHECKPOINT_NUMBER - 1);
1216
+ const genesisBlockId = { number: genesisBlockNumber, hash: genesisBlockHash.toString() };
1217
+ const genesisCheckpointId = {
1218
+ number: genesisCheckpointNumber,
1219
+ hash: GENESIS_CHECKPOINT_HEADER_HASH.toString(),
1220
+ };
1221
+ const genesisTip: L2TipId = { block: genesisBlockId, checkpoint: genesisCheckpointId };
1222
+
1223
+ // Load latest block and checkpoint entries
1224
+ const [latestBlockEntry] = await toArray(this.#blocks.entriesAsync({ reverse: true, limit: 1 }));
1225
+ const [latestCheckpointEntry] = await toArray(this.#checkpoints.entriesAsync({ reverse: true, limit: 1 }));
1226
+ const latestCheckpointNumber = latestCheckpointEntry
1227
+ ? CheckpointNumber(latestCheckpointEntry[0])
1228
+ : genesisCheckpointNumber;
1229
+
1230
+ // Load proven and finalized checkpoint number pointers
1231
+ const [provenRaw, finalizedRaw] = await Promise.all([
1232
+ this.#lastProvenCheckpoint.getAsync(),
1233
+ this.#lastFinalizedCheckpoint.getAsync(),
1234
+ ]);
1235
+
1236
+ // Clamp to enforce finalized <= proven <= checkpointed.
1237
+ const provenCheckpointNumber = CheckpointNumber(Math.min(provenRaw ?? 0, latestCheckpointNumber));
1238
+ const finalizedCheckpointNumber = CheckpointNumber(Math.min(finalizedRaw ?? 0, provenCheckpointNumber));
1239
+
1240
+ // Avoid loading the same checkpoint more than once
1241
+ const checkpointStorageCache = new Map<CheckpointNumber, CheckpointStorage>();
1242
+ if (latestCheckpointEntry) {
1243
+ checkpointStorageCache.set(CheckpointNumber(latestCheckpointEntry[0]), latestCheckpointEntry[1]);
1244
+ }
1245
+ const loadCheckpointStorage = async (n: CheckpointNumber): Promise<CheckpointStorage | undefined> => {
1246
+ if (n === 0) {
1247
+ return undefined;
1248
+ }
1249
+ if (!checkpointStorageCache.has(n)) {
1250
+ const checkpointStorage = await this.#checkpoints.getAsync(n);
1251
+ if (!checkpointStorage) {
1252
+ throw new CheckpointNotFoundError(n);
1253
+ }
1254
+ checkpointStorageCache.set(n, checkpointStorage);
1255
+ }
1256
+ return checkpointStorageCache.get(n)!;
1257
+ };
1258
+
1259
+ // Load proven and finalized checkpoint storage entries
1260
+ const provenCheckpoint = await loadCheckpointStorage(provenCheckpointNumber);
1261
+ const finalizedCheckpoint = await loadCheckpointStorage(finalizedCheckpointNumber);
1262
+
1263
+ // Avoid loading the same block hash multiple times when tips land on the same block
1264
+ const blockHashCache = new Map<number, string>();
1265
+ blockHashCache.set(genesisBlockNumber, genesisBlockHash.toString());
1266
+ if (latestBlockEntry) {
1267
+ blockHashCache.set(latestBlockEntry[0], BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString());
1268
+ }
1269
+ const loadBlockHash = async (n: BlockNumber): Promise<string> => {
1270
+ if (!blockHashCache.has(n)) {
1271
+ const blockStorage = await this.#blocks.getAsync(n);
1272
+ if (!blockStorage) {
1273
+ throw new BlockNotFoundError(n);
1274
+ }
1275
+ const blockHash = BlockHash.fromBuffer(blockStorage.blockHash).toString();
1276
+ blockHashCache.set(n, blockHash);
1277
+ }
1278
+ return blockHashCache.get(n)!;
1279
+ };
1280
+
1281
+ // Build proposed chain tip (this one has block only, no checkpoint)
1282
+ const proposedBlockId =
1283
+ latestBlockEntry === undefined
1284
+ ? genesisBlockId
1285
+ : {
1286
+ number: BlockNumber(latestBlockEntry[0]),
1287
+ hash: BlockHash.fromBuffer(latestBlockEntry[1].blockHash).toString(),
1288
+ };
1289
+
1290
+ // Build other tips from checkpoint data, reading corresponding block data from the cache
1291
+ const buildTipFromCheckpoint = async (
1292
+ stored: ProposedCheckpointStorage | CheckpointStorage | undefined,
1293
+ ): Promise<L2TipId> => {
1294
+ if (!stored) {
1295
+ return genesisTip;
1296
+ }
1297
+ const blockNumber = BlockNumber(stored.startBlock + stored.blockCount - 1);
1298
+ const blockHash = await loadBlockHash(blockNumber);
1299
+ const header = CheckpointHeader.fromBuffer(stored.header);
1300
+ return {
1301
+ block: { number: blockNumber, hash: blockHash },
1302
+ checkpoint: { number: CheckpointNumber(stored.checkpointNumber), hash: header.hash().toString() },
1303
+ };
1304
+ };
1305
+
1306
+ const checkpointedTip = await buildTipFromCheckpoint(latestCheckpointEntry?.[1]);
1307
+ const provenTip = await buildTipFromCheckpoint(provenCheckpoint);
1308
+ const finalizedTip = await buildTipFromCheckpoint(finalizedCheckpoint);
1309
+
1310
+ // A checkpointed block past the latest stored block would mean a checkpoint
1311
+ // references blocks that aren't in blocks.
1312
+ if (proposedBlockId.number < checkpointedTip.block.number) {
1313
+ throw new Error(
1314
+ `Inconsistent block store: latest block ${proposedBlockId.number} is behind checkpointed block ${checkpointedTip.block.number}`,
1315
+ );
1316
+ }
1317
+
1318
+ // Assert that checkpoint numbers are increasing
1319
+ if (
1320
+ finalizedTip.checkpoint.number > provenTip.checkpoint.number ||
1321
+ provenTip.checkpoint.number > checkpointedTip.checkpoint.number
1322
+ ) {
1323
+ throw new Error(
1324
+ `Inconsistent checkpoint numbers in chain tips: finalized=${finalizedTip.checkpoint.number} proven=${provenTip.checkpoint.number} checkpointed=${checkpointedTip.checkpoint.number}`,
1325
+ );
1326
+ }
1327
+
1328
+ // Assert block numbers are increasing
1329
+ if (
1330
+ finalizedTip.block.number > provenTip.block.number ||
1331
+ provenTip.block.number > checkpointedTip.block.number ||
1332
+ checkpointedTip.block.number > proposedBlockId.number
1333
+ ) {
1334
+ throw new Error(
1335
+ `Inconsistent block numbers in chain tips: finalized=${finalizedTip.block.number} proven=${provenTip.block.number} checkpointed=${checkpointedTip.block.number} proposed=${proposedBlockId.number}`,
1336
+ );
1337
+ }
1338
+
1339
+ return {
1340
+ proposed: proposedBlockId,
1341
+ checkpointed: checkpointedTip,
1342
+ proven: provenTip,
1343
+ finalized: finalizedTip,
1344
+ };
1345
+ });
1346
+ }
1347
+
1348
+ /**
1349
+ * Gets the most recent L1 block processed.
1350
+ * @returns The L1 block that published the latest L2 block
1351
+ */
1352
+ getSynchedL1BlockNumber(): Promise<bigint | undefined> {
1353
+ return this.#lastSynchedL1Block.getAsync();
1354
+ }
1355
+
1356
+ setSynchedL1BlockNumber(l1BlockNumber: bigint) {
1357
+ return this.#lastSynchedL1Block.set(l1BlockNumber);
1358
+ }
1359
+
1360
+ /**
1361
+ * Adds a proposed checkpoint to the pending queue.
1362
+ * Accepts proposed.checkpointNumber === latestTip + 1, where latestTip is the highest of
1363
+ * confirmed and the highest pending checkpoint number.
1364
+ * Computes archive and checkpointOutHash from the stored blocks.
1365
+ */
1366
+ async addProposedCheckpoint(proposed: ProposedCheckpointInput) {
1367
+ return await this.db.transactionAsync(async () => {
1368
+ const confirmed = await this.getLatestCheckpointNumber();
1369
+ const [latestPendingKey] = await toArray(this.#proposedCheckpoints.keysAsync({ reverse: true, limit: 1 }));
1370
+ const latestTip = CheckpointNumber(
1371
+ latestPendingKey !== undefined ? Math.max(latestPendingKey, confirmed) : confirmed,
1372
+ );
1373
+
1374
+ if (proposed.checkpointNumber !== latestTip + 1) {
1375
+ throw new ProposedCheckpointNotSequentialError(proposed.checkpointNumber, latestTip);
1376
+ }
1377
+
1378
+ // Ensure the predecessor block (from pending or confirmed chain) exists
1379
+ const previousBlock = await this.getPreviousCheckpointBlock(proposed.checkpointNumber);
1380
+ const blocks: L2Block[] = [];
1381
+ for (let i = 0; i < proposed.blockCount; i++) {
1382
+ const block = await this.getBlock({ number: BlockNumber(proposed.startBlock + i) });
1383
+ if (!block) {
1384
+ throw new BlockNotFoundError(proposed.startBlock + i);
1385
+ }
1386
+ blocks.push(block);
1387
+ }
1388
+ this.validateCheckpointBlocks(blocks, previousBlock);
1389
+
1390
+ const archive = blocks[blocks.length - 1].archive;
1391
+ const checkpointOutHash = Checkpoint.getCheckpointOutHash(blocks);
1392
+
1393
+ await this.#proposedCheckpoints.set(proposed.checkpointNumber, {
1394
+ header: proposed.header.toBuffer(),
1395
+ archive: archive.toBuffer(),
1396
+ checkpointOutHash: checkpointOutHash.toBuffer(),
1397
+ checkpointNumber: proposed.checkpointNumber,
1398
+ startBlock: proposed.startBlock,
1399
+ blockCount: proposed.blockCount,
1400
+ totalManaUsed: proposed.totalManaUsed.toString(),
1401
+ feeAssetPriceModifier: proposed.feeAssetPriceModifier.toString(),
1402
+ });
1403
+ });
1404
+ }
1405
+
1406
+ async getProvenCheckpointNumber(): Promise<CheckpointNumber> {
1407
+ return await this.db.transactionAsync(async () => {
1408
+ const [latestCheckpointNumber, provenCheckpointNumber] = await Promise.all([
1409
+ this.getLatestCheckpointNumber(),
1410
+ this.#lastProvenCheckpoint.getAsync(),
1411
+ ]);
1412
+ return (provenCheckpointNumber ?? 0) > latestCheckpointNumber
1413
+ ? latestCheckpointNumber
1414
+ : CheckpointNumber(provenCheckpointNumber ?? 0);
1415
+ });
1416
+ }
1417
+
1418
+ async setProvenCheckpointNumber(checkpointNumber: CheckpointNumber) {
1419
+ const result = await this.#lastProvenCheckpoint.set(checkpointNumber);
1420
+ return result;
1421
+ }
1422
+
1423
+ async getFinalizedCheckpointNumber(): Promise<CheckpointNumber> {
1424
+ return await this.db.transactionAsync(async () => {
1425
+ const [provenCheckpointNumber, finalizedCheckpointNumber] = await Promise.all([
1426
+ this.getProvenCheckpointNumber(),
1427
+ this.#lastFinalizedCheckpoint.getAsync(),
1428
+ ]);
1429
+ return (finalizedCheckpointNumber ?? 0) > provenCheckpointNumber
1430
+ ? provenCheckpointNumber
1431
+ : CheckpointNumber(finalizedCheckpointNumber ?? 0);
1432
+ });
1433
+ }
1434
+
1435
+ setFinalizedCheckpointNumber(checkpointNumber: CheckpointNumber) {
1436
+ return this.#lastFinalizedCheckpoint.set(checkpointNumber);
1437
+ }
1438
+
1439
+ #computeBlockRange(start: BlockNumber, limit: number): Required<Pick<Range<number>, 'start' | 'limit'>> {
1440
+ if (limit < 1) {
1441
+ throw new Error(`Invalid limit: ${limit}`);
1442
+ }
1443
+
1444
+ if (start < INITIAL_L2_BLOCK_NUM) {
1445
+ throw new Error(`Invalid start: ${start}`);
1446
+ }
1447
+
1448
+ return { start, limit };
1449
+ }
1450
+
1451
+ /**
1452
+ * Gets the pending chain validation status.
1453
+ * @returns The validation status or undefined if not set.
1454
+ */
1455
+ async getPendingChainValidationStatus(): Promise<ValidateCheckpointResult | undefined> {
1456
+ const buffer = await this.#pendingChainValidationStatus.getAsync();
1457
+ if (!buffer) {
1458
+ return undefined;
1459
+ }
1460
+ return deserializeValidateCheckpointResult(buffer);
1461
+ }
1462
+
1463
+ /**
1464
+ * Sets the pending chain validation status.
1465
+ * @param status - The validation status to store.
1466
+ */
1467
+ async setPendingChainValidationStatus(status: ValidateCheckpointResult | undefined): Promise<void> {
1468
+ if (status) {
1469
+ const buffer = serializeValidateCheckpointResult(status);
1470
+ await this.#pendingChainValidationStatus.set(buffer);
1471
+ } else {
1472
+ await this.#pendingChainValidationStatus.delete();
1473
+ }
1474
+ }
1475
+
1476
+ /** Records a rejected-checkpoint entry, keyed by its own archive root. */
1477
+ async addRejectedCheckpoint(entry: RejectedCheckpoint): Promise<void> {
1478
+ const archiveRootHex = entry.archiveRoot.toString();
1479
+ await this.#rejectedCheckpoints.set(archiveRootHex, {
1480
+ checkpointNumber: entry.checkpointNumber,
1481
+ archiveRoot: entry.archiveRoot.toBuffer(),
1482
+ parentArchiveRoot: entry.parentArchiveRoot.toBuffer(),
1483
+ slotNumber: entry.slotNumber,
1484
+ l1: entry.l1.toBuffer(),
1485
+ reason: entry.reason,
1486
+ });
1487
+ await this.#rejectedCheckpointsByNumber.set(entry.checkpointNumber, archiveRootHex);
1488
+ await this.advanceSynchedL1BlockNumber(entry.l1.blockNumber);
1489
+ }
1490
+
1491
+ /** Returns the rejected-checkpoint entry with the given archive root, or undefined if not present. */
1492
+ async getRejectedCheckpointByArchiveRoot(archiveRoot: Fr): Promise<RejectedCheckpoint | undefined> {
1493
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRoot.toString());
1494
+ return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
1495
+ }
1496
+
1497
+ /** Returns the rejected-checkpoint entry recorded for the given checkpoint number, or undefined if none. */
1498
+ async getRejectedCheckpointByNumber(checkpointNumber: CheckpointNumber): Promise<RejectedCheckpoint | undefined> {
1499
+ const archiveRootHex = await this.#rejectedCheckpointsByNumber.getAsync(checkpointNumber);
1500
+ if (archiveRootHex === undefined) {
1501
+ return undefined;
1502
+ }
1503
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
1504
+ return stored ? this.rejectedCheckpointFromStorage(stored) : undefined;
1505
+ }
1506
+
1507
+ /** Returns the highest checkpoint number recorded across all rejected entries, or `INITIAL_CHECKPOINT_NUMBER - 1` if none. */
1508
+ async getLatestRejectedCheckpointNumber(): Promise<CheckpointNumber> {
1509
+ const [latest] = await toArray(this.#rejectedCheckpointsByNumber.keysAsync({ reverse: true, limit: 1 }));
1510
+ return CheckpointNumber(latest ?? INITIAL_CHECKPOINT_NUMBER - 1);
1511
+ }
1512
+
1513
+ /** Removes a rejected-checkpoint entry by its archive root (used when an entry no longer matches L1). */
1514
+ async removeRejectedCheckpointByArchiveRoot(archiveRoot: Fr): Promise<void> {
1515
+ const archiveRootHex = archiveRoot.toString();
1516
+ const stored = await this.#rejectedCheckpoints.getAsync(archiveRootHex);
1517
+ await this.#rejectedCheckpoints.delete(archiveRootHex);
1518
+ if (stored) {
1519
+ // Only clear the by-number index if it still points at this archive root, so a distinct
1520
+ // entry that shares the checkpoint number (e.g. an L1 reorg replacement) is not dropped.
1521
+ const indexed = await this.#rejectedCheckpointsByNumber.getAsync(stored.checkpointNumber);
1522
+ if (indexed === archiveRootHex) {
1523
+ await this.#rejectedCheckpointsByNumber.delete(stored.checkpointNumber);
1524
+ }
1525
+ }
1526
+ }
1527
+
1528
+ /**
1529
+ * Advances the stored last-synched L1 block number to `l1BlockNumber` only if it is strictly
1530
+ * greater than the current value. Use this whenever ingesting checkpoint-shaped data so the
1531
+ * sync pointer never walks backwards on out-of-order writes (e.g. an invalid checkpoint
1532
+ * advance followed by a valid-checkpoint commit landing at an earlier L1 block).
1533
+ */
1534
+ private async advanceSynchedL1BlockNumber(l1BlockNumber: bigint): Promise<void> {
1535
+ const current = await this.#lastSynchedL1Block.getAsync();
1536
+ if (current === undefined || l1BlockNumber > current) {
1537
+ await this.#lastSynchedL1Block.set(l1BlockNumber);
1538
+ }
1539
+ }
1540
+
1541
+ private rejectedCheckpointFromStorage(stored: RejectedCheckpointStorage): RejectedCheckpoint {
1542
+ return {
1543
+ checkpointNumber: CheckpointNumber(stored.checkpointNumber),
1544
+ archiveRoot: Fr.fromBuffer(stored.archiveRoot),
1545
+ parentArchiveRoot: Fr.fromBuffer(stored.parentArchiveRoot),
1546
+ slotNumber: SlotNumber(stored.slotNumber),
1547
+ l1: L1PublishedData.fromBuffer(stored.l1),
1548
+ reason: stored.reason,
1549
+ };
1550
+ }
1551
+ }