@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
@@ -1,5 +1,5 @@
1
1
  import type { ViemPublicDebugClient } from '@aztec/ethereum/types';
2
- import { createLogger } from '@aztec/foundation/log';
2
+ import { type Logger, type LoggerBindings, createLogger } from '@aztec/foundation/log';
3
3
 
4
4
  import type { Hex } from 'viem';
5
5
  import type { ZodSchema } from 'zod';
@@ -7,8 +7,6 @@ import type { ZodSchema } from 'zod';
7
7
  import { callTraceSchema } from './debug_tx.js';
8
8
  import { traceTransactionResponseSchema } from './trace_tx.js';
9
9
 
10
- const logger = createLogger('aztec:archiver:validate_trace');
11
-
12
10
  /**
13
11
  * Helper function to test a trace method with validation
14
12
  *
@@ -17,6 +15,7 @@ const logger = createLogger('aztec:archiver:validate_trace');
17
15
  * @param schema - Zod schema to validate the response
18
16
  * @param method - Name of the RPC method ('debug_traceTransaction' or 'trace_transaction')
19
17
  * @param blockType - Type of block being tested ('recent' or 'old')
18
+ * @param logger - Logger instance
20
19
  * @returns true if the method works and validation passes, false otherwise
21
20
  */
22
21
  async function testTraceMethod(
@@ -25,6 +24,7 @@ async function testTraceMethod(
25
24
  schema: ZodSchema,
26
25
  method: 'debug_traceTransaction' | 'trace_transaction',
27
26
  blockType: string,
27
+ logger: Logger,
28
28
  ): Promise<boolean> {
29
29
  try {
30
30
  // Make request with appropriate params based on method name
@@ -59,9 +59,14 @@ export interface TraceAvailability {
59
59
  * Validates the availability of debug/trace methods on the Ethereum client.
60
60
  *
61
61
  * @param client - The Viem public debug client
62
+ * @param bindings - Optional logger bindings for context
62
63
  * @returns Object indicating which trace methods are available for recent and old blocks
63
64
  */
64
- export async function validateTraceAvailability(client: ViemPublicDebugClient): Promise<TraceAvailability> {
65
+ export async function validateTraceAvailability(
66
+ client: ViemPublicDebugClient,
67
+ bindings?: LoggerBindings,
68
+ ): Promise<TraceAvailability> {
69
+ const logger = createLogger('archiver:validate_trace', bindings);
65
70
  const result: TraceAvailability = {
66
71
  debugTraceRecent: false,
67
72
  traceTransactionRecent: false,
@@ -95,6 +100,7 @@ export async function validateTraceAvailability(client: ViemPublicDebugClient):
95
100
  callTraceSchema,
96
101
  'debug_traceTransaction',
97
102
  'recent',
103
+ logger,
98
104
  );
99
105
 
100
106
  // Test trace_transaction with recent block
@@ -104,6 +110,7 @@ export async function validateTraceAvailability(client: ViemPublicDebugClient):
104
110
  traceTransactionResponseSchema,
105
111
  'trace_transaction',
106
112
  'recent',
113
+ logger,
107
114
  );
108
115
 
109
116
  // Get a block from 512 blocks ago
@@ -132,7 +139,14 @@ export async function validateTraceAvailability(client: ViemPublicDebugClient):
132
139
  const oldTxHash = oldBlock.transactions[0] as Hex;
133
140
 
134
141
  // Test debug_traceTransaction with old block
135
- result.debugTraceOld = await testTraceMethod(client, oldTxHash, callTraceSchema, 'debug_traceTransaction', 'old');
142
+ result.debugTraceOld = await testTraceMethod(
143
+ client,
144
+ oldTxHash,
145
+ callTraceSchema,
146
+ 'debug_traceTransaction',
147
+ 'old',
148
+ logger,
149
+ );
136
150
 
137
151
  // Test trace_transaction with old block
138
152
  result.traceTransactionOld = await testTraceMethod(
@@ -141,6 +155,7 @@ export async function validateTraceAvailability(client: ViemPublicDebugClient):
141
155
  traceTransactionResponseSchema,
142
156
  'trace_transaction',
143
157
  'old',
158
+ logger,
144
159
  );
145
160
  } catch (error) {
146
161
  logger.warn(`Error validating debug_traceTransaction and trace_transaction availability: ${error}`);
@@ -159,15 +174,18 @@ function hasTxs(block: { transactions?: Hex[] }): boolean {
159
174
  *
160
175
  * @param client - The Viem public debug client
161
176
  * @param ethereumAllowNoDebugHosts - If false, throws an error when no trace methods are available
177
+ * @param bindings - Optional logger bindings for context
162
178
  * @throws Error if ethereumAllowNoDebugHosts is false and no trace methods are available
163
179
  */
164
180
  export async function validateAndLogTraceAvailability(
165
181
  client: ViemPublicDebugClient,
166
182
  ethereumAllowNoDebugHosts: boolean,
183
+ bindings?: LoggerBindings,
167
184
  ): Promise<void> {
185
+ const logger = createLogger('archiver:validate_trace', bindings);
168
186
  logger.debug('Validating trace/debug method availability...');
169
187
 
170
- const availability = await validateTraceAvailability(client);
188
+ const availability = await validateTraceAvailability(client, bindings);
171
189
 
172
190
  // Check if we have support for old blocks (either debug or trace)
173
191
  const hasOldBlockSupport = availability.debugTraceOld || availability.traceTransactionOld;
@@ -0,0 +1,55 @@
1
+ import type { BlockNumber } from '@aztec/foundation/branded-types';
2
+ import type { Fr } from '@aztec/foundation/curves/bn254';
3
+ import type { FunctionSelector } from '@aztec/stdlib/abi';
4
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
5
+ import type { ContractClassPublic, ContractDataSource, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
6
+ import type { UInt64 } from '@aztec/stdlib/types';
7
+
8
+ import type { ArchiverDataStores } from '../store/data_stores.js';
9
+
10
+ /**
11
+ * Thin {@link ContractDataSource} adapter over {@link ArchiverDataStores}.
12
+ *
13
+ * Used by contexts (e.g. offline epoch re-prover tools) that need a ContractDataSource
14
+ * but do not need a full archiver instance.
15
+ */
16
+ export class ArchiverContractDataSourceAdapter implements ContractDataSource {
17
+ constructor(private readonly stores: ArchiverDataStores) {}
18
+
19
+ public getBlockNumber(): Promise<BlockNumber> {
20
+ return this.stores.blocks.getLatestL2BlockNumber();
21
+ }
22
+
23
+ public getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
24
+ return this.stores.contractClasses.getContractClass(id);
25
+ }
26
+
27
+ public getBytecodeCommitment(id: Fr): Promise<Fr | undefined> {
28
+ return this.stores.contractClasses.getBytecodeCommitment(id);
29
+ }
30
+
31
+ public async getContract(
32
+ address: AztecAddress,
33
+ maybeTimestamp?: UInt64,
34
+ ): Promise<ContractInstanceWithAddress | undefined> {
35
+ let timestamp = maybeTimestamp;
36
+ if (timestamp === undefined) {
37
+ const latest = await this.stores.blocks.getLatestL2BlockNumber();
38
+ const blockData = latest > 0 ? await this.stores.blocks.getBlockData({ number: latest }) : undefined;
39
+ timestamp = blockData ? blockData.header.globalVariables.timestamp : 0n;
40
+ }
41
+ return this.stores.contractInstances.getContractInstance(address, timestamp);
42
+ }
43
+
44
+ public getContractClassIds(): Promise<Fr[]> {
45
+ return this.stores.contractClasses.getContractClassIds();
46
+ }
47
+
48
+ public getDebugFunctionName(_address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
49
+ return Promise.resolve(this.stores.functionNames.get(selector));
50
+ }
51
+
52
+ public registerContractFunctionSignatures(signatures: string[]): Promise<void> {
53
+ return this.stores.functionNames.register(signatures);
54
+ }
55
+ }
@@ -0,0 +1,493 @@
1
+ import { INITIAL_L2_BLOCK_NUM } from '@aztec/constants';
2
+ import {
3
+ BlockNumber,
4
+ CheckpointNumber,
5
+ type EpochNumber,
6
+ IndexWithinCheckpoint,
7
+ type SlotNumber,
8
+ } from '@aztec/foundation/branded-types';
9
+ import type { Fr } from '@aztec/foundation/curves/bn254';
10
+ import type { EthAddress } from '@aztec/foundation/eth-address';
11
+ import type { FunctionSelector } from '@aztec/stdlib/abi';
12
+ import type { AztecAddress } from '@aztec/stdlib/aztec-address';
13
+ import {
14
+ type BlockData,
15
+ type BlockHash,
16
+ type BlockQuery,
17
+ type BlockTag,
18
+ type BlocksQuery,
19
+ Body,
20
+ type CheckpointQuery,
21
+ type CheckpointsQuery,
22
+ L2Block,
23
+ type L2Tips,
24
+ type ProposedCheckpointQuery,
25
+ } from '@aztec/stdlib/block';
26
+ import {
27
+ Checkpoint,
28
+ type CheckpointData,
29
+ type ProposedCheckpointData,
30
+ PublishedCheckpoint,
31
+ } from '@aztec/stdlib/checkpoint';
32
+ import type { ContractClassPublic, ContractDataSource, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
33
+ import {
34
+ type L1RollupConstants,
35
+ getEpochAtSlot,
36
+ getEpochNumberAtTimestamp,
37
+ getLastL1SlotTimestampForL2Slot,
38
+ getProofSubmissionDeadlineEpoch,
39
+ getSlotRangeForEpoch,
40
+ } from '@aztec/stdlib/epoch-helpers';
41
+ import type { L2LogsSource } from '@aztec/stdlib/interfaces/server';
42
+ import type { LogResult, PrivateLogsQuery, PublicLogsQuery } from '@aztec/stdlib/logs';
43
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
44
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
45
+ import type { BlockHeader, IndexedTxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
46
+ import type { UInt64 } from '@aztec/stdlib/types';
47
+
48
+ import type { ArchiverDataSource } from '../interfaces.js';
49
+ import type { ResolvedBlockQuery, ResolvedBlocksQuery } from '../store/block_store.js';
50
+ import type { ArchiverDataStores } from '../store/data_stores.js';
51
+ import type { ValidateCheckpointResult } from './validation.js';
52
+
53
+ /**
54
+ * Sentinel returned by {@link ArchiverDataSourceBase#resolveBlockQuery} when a query resolves
55
+ * to the genesis block. Forces single-block lookup methods to take the genesis branch
56
+ * explicitly rather than silently falling through to the BlockStore (which never has a block 0).
57
+ */
58
+ type GenesisBlockQuery = { genesis: true };
59
+
60
+ /**
61
+ * Abstract base class implementing ArchiverDataSource using a bundle of archiver substores.
62
+ * Provides implementations for all read-side methods and declares abstract methods for
63
+ * L1-dependent functionality that subclasses must implement.
64
+ */
65
+ export abstract class ArchiverDataSourceBase
66
+ implements ArchiverDataSource, L2LogsSource, ContractDataSource, L1ToL2MessageSource
67
+ {
68
+ /** The injected genesis block header. */
69
+ protected readonly initialHeader: BlockHeader;
70
+ /** Precomputed hash of the initial header, exposed via {@link getGenesisBlockHash}. */
71
+ protected readonly initialBlockHash: BlockHash;
72
+ /** Archive root after block 0 was appended — read from L1 (`Rollup.getGenesisArchiveTreeRoot`). */
73
+ protected readonly genesisArchiveRoot: Fr;
74
+
75
+ /** Memoized synthetic genesis block — callers rely on referential identity for caching. */
76
+ private readonly genesisBlock: L2Block;
77
+ /** Memoized synthetic genesis block data — kept consistent with {@link genesisBlock}. */
78
+ private readonly genesisBlockData: BlockData;
79
+
80
+ constructor(
81
+ protected readonly stores: ArchiverDataStores,
82
+ protected readonly l1Constants: L1RollupConstants | undefined,
83
+ initialHeader: BlockHeader,
84
+ initialBlockHash: BlockHash,
85
+ genesisArchiveRoot: Fr,
86
+ ) {
87
+ this.initialHeader = initialHeader;
88
+ this.initialBlockHash = initialBlockHash;
89
+ this.genesisArchiveRoot = genesisArchiveRoot;
90
+
91
+ const genesisArchive = new AppendOnlyTreeSnapshot(genesisArchiveRoot, 1);
92
+ this.genesisBlock = new L2Block(
93
+ genesisArchive,
94
+ initialHeader,
95
+ Body.empty(),
96
+ CheckpointNumber.ZERO,
97
+ IndexWithinCheckpoint(0),
98
+ );
99
+ this.genesisBlockData = {
100
+ header: initialHeader,
101
+ archive: genesisArchive,
102
+ blockHash: initialBlockHash,
103
+ checkpointNumber: CheckpointNumber.ZERO,
104
+ indexWithinCheckpoint: IndexWithinCheckpoint(0),
105
+ };
106
+ }
107
+
108
+ /** Returns the precomputed hash of the genesis block header. */
109
+ public getGenesisBlockHash(): BlockHash {
110
+ return this.initialBlockHash;
111
+ }
112
+
113
+ /** Returns the synthetic genesis L2Block (memoized — same instance across calls). */
114
+ private getGenesisBlock(): L2Block {
115
+ return this.genesisBlock;
116
+ }
117
+
118
+ /** Returns genesis block data (memoized — same instance across calls). */
119
+ private getGenesisBlockData(): BlockData {
120
+ return this.genesisBlockData;
121
+ }
122
+
123
+ /**
124
+ * Type guard distinguishing the genesis sentinel from a {@link ResolvedBlockQuery}.
125
+ * `resolveBlockQuery` already rewrites every genesis-matching shape to the sentinel,
126
+ * so callers only need this single sync check.
127
+ */
128
+ private isGenesisBlockQuery(query: ResolvedBlockQuery | GenesisBlockQuery): query is GenesisBlockQuery {
129
+ return 'genesis' in query;
130
+ }
131
+
132
+ abstract getRollupAddress(): Promise<EthAddress>;
133
+
134
+ abstract getRegistryAddress(): Promise<EthAddress>;
135
+
136
+ abstract getL1Constants(): Promise<L1RollupConstants>;
137
+
138
+ abstract getGenesisValues(): Promise<{ genesisArchiveRoot: Fr }>;
139
+
140
+ abstract getL1Timestamp(): Promise<bigint | undefined>;
141
+
142
+ abstract getL2Tips(): Promise<L2Tips>;
143
+
144
+ abstract getSyncedL2SlotNumber(): Promise<SlotNumber | undefined>;
145
+
146
+ abstract getSyncedL2EpochNumber(): Promise<EpochNumber | undefined>;
147
+
148
+ abstract isEpochComplete(epochNumber: EpochNumber): Promise<boolean>;
149
+
150
+ abstract syncImmediate(): Promise<void>;
151
+
152
+ public async isPruneDueAtSlot(slot: SlotNumber): Promise<boolean> {
153
+ if (!this.l1Constants) {
154
+ throw new Error('isPruneDueAtSlot requires l1Constants');
155
+ }
156
+ const tips = await this.getL2Tips();
157
+ const proven = tips.proven.checkpoint.number;
158
+ const pending = tips.checkpointed.checkpoint.number;
159
+ if (pending === proven) {
160
+ return false;
161
+ }
162
+
163
+ const oldestUnproven = await this.getCheckpointData({ number: CheckpointNumber(Number(proven) + 1) });
164
+ if (!oldestUnproven) {
165
+ return false;
166
+ }
167
+
168
+ const slotTs = getLastL1SlotTimestampForL2Slot(slot, this.l1Constants);
169
+ const slotEpoch = getEpochNumberAtTimestamp(slotTs, this.l1Constants);
170
+ const oldestUnprovenEpoch = getEpochAtSlot(oldestUnproven.header.slotNumber, this.l1Constants);
171
+ const deadlineEpoch = getProofSubmissionDeadlineEpoch(oldestUnprovenEpoch, this.l1Constants);
172
+ return slotEpoch >= deadlineEpoch;
173
+ }
174
+
175
+ public getCheckpointNumber(): Promise<CheckpointNumber> {
176
+ return this.stores.blocks.getLatestCheckpointNumber();
177
+ }
178
+
179
+ public getProvenCheckpointNumber(): Promise<CheckpointNumber> {
180
+ return this.stores.blocks.getProvenCheckpointNumber();
181
+ }
182
+
183
+ public getBlockNumber(): Promise<BlockNumber>;
184
+ public getBlockNumber(query: BlockQuery): Promise<BlockNumber | undefined>;
185
+ public async getBlockNumber(query?: BlockQuery): Promise<BlockNumber | undefined> {
186
+ if (!query) {
187
+ return this.stores.blocks.getLatestL2BlockNumber();
188
+ }
189
+ const resolved = await this.resolveBlockQuery(query);
190
+ if (resolved === undefined) {
191
+ return undefined;
192
+ }
193
+ if (this.isGenesisBlockQuery(resolved)) {
194
+ return BlockNumber.ZERO;
195
+ }
196
+ return this.stores.blocks.getBlockNumber(resolved);
197
+ }
198
+
199
+ /**
200
+ * Resolves a {@link CheckpointQuery} to a concrete `CheckpointNumber`, or undefined when the
201
+ * query refers to a position that has no checkpoint yet (e.g. `{ slot }` not found).
202
+ */
203
+ private resolveCheckpointQuery(query: CheckpointQuery): Promise<CheckpointNumber | undefined> {
204
+ if ('number' in query) {
205
+ return Promise.resolve(query.number);
206
+ }
207
+ if ('slot' in query) {
208
+ return this.stores.blocks.getCheckpointNumberBySlot(query.slot);
209
+ }
210
+ // tag variant
211
+ switch (query.tag) {
212
+ case 'checkpointed':
213
+ return this.stores.blocks.getLatestCheckpointNumber();
214
+ case 'proven':
215
+ return this.stores.blocks.getProvenCheckpointNumber();
216
+ case 'finalized':
217
+ return this.stores.blocks.getFinalizedCheckpointNumber();
218
+ }
219
+ }
220
+
221
+ /**
222
+ * Resolves a {@link CheckpointsQuery} to a concrete `{from, limit}` pair used by BlockStore,
223
+ * or undefined when the epoch has no checkpoints.
224
+ */
225
+ private async resolveCheckpointsQuery(
226
+ query: CheckpointsQuery,
227
+ ): Promise<{ from: CheckpointNumber; limit: number } | undefined> {
228
+ if ('from' in query) {
229
+ return query;
230
+ }
231
+ const numbers = await this.getCheckpointNumbersForEpoch(query.epoch);
232
+ if (numbers.length === 0) {
233
+ return undefined;
234
+ }
235
+ return { from: numbers[0], limit: numbers.length };
236
+ }
237
+
238
+ public async getCheckpoint(query: CheckpointQuery): Promise<PublishedCheckpoint | undefined> {
239
+ const number = await this.resolveCheckpointQuery(query);
240
+ if (number === undefined || number === 0) {
241
+ return undefined;
242
+ }
243
+ const data = await this.stores.blocks.getCheckpointData(number);
244
+ if (!data) {
245
+ return undefined;
246
+ }
247
+ return this.getPublishedCheckpointFromCheckpointData(data);
248
+ }
249
+
250
+ public async getCheckpoints(query: CheckpointsQuery): Promise<PublishedCheckpoint[]> {
251
+ const resolved = await this.resolveCheckpointsQuery(query);
252
+ if (!resolved) {
253
+ return [];
254
+ }
255
+ const checkpoints = await this.stores.blocks.getRangeOfCheckpoints(resolved.from, resolved.limit);
256
+ return Promise.all(checkpoints.map(ch => this.getPublishedCheckpointFromCheckpointData(ch)));
257
+ }
258
+
259
+ public async getCheckpointData(query: CheckpointQuery): Promise<CheckpointData | undefined> {
260
+ const number = await this.resolveCheckpointQuery(query);
261
+ if (number === undefined || number === 0) {
262
+ return undefined;
263
+ }
264
+ return this.stores.blocks.getCheckpointData(number);
265
+ }
266
+
267
+ public async getCheckpointsData(query: CheckpointsQuery): Promise<CheckpointData[]> {
268
+ const resolved = await this.resolveCheckpointsQuery(query);
269
+ if (!resolved) {
270
+ return [];
271
+ }
272
+ return this.stores.blocks.getRangeOfCheckpoints(resolved.from, resolved.limit);
273
+ }
274
+
275
+ public getProposedCheckpointData(query?: ProposedCheckpointQuery): Promise<ProposedCheckpointData | undefined> {
276
+ if (!query || 'tag' in query) {
277
+ return this.stores.blocks.getLastProposedCheckpoint();
278
+ }
279
+ if ('number' in query) {
280
+ return this.stores.blocks.getProposedCheckpointByNumber(query.number);
281
+ }
282
+ return this.stores.blocks.getProposedCheckpointBySlot(query.slot);
283
+ }
284
+
285
+ public getTxEffect(txHash: TxHash): Promise<IndexedTxEffect | undefined> {
286
+ return this.stores.blocks.getTxEffect(txHash);
287
+ }
288
+
289
+ public getSettledTxReceipt(txHash: TxHash): Promise<TxReceipt | undefined> {
290
+ return this.stores.blocks.getSettledTxReceipt(txHash, this.l1Constants);
291
+ }
292
+
293
+ public isPendingChainInvalid(): Promise<boolean> {
294
+ return this.getPendingChainValidationStatus().then(status => !status.valid);
295
+ }
296
+
297
+ public async getPendingChainValidationStatus(): Promise<ValidateCheckpointResult> {
298
+ return (await this.stores.blocks.getPendingChainValidationStatus()) ?? { valid: true };
299
+ }
300
+
301
+ public getPrivateLogsByTags(query: PrivateLogsQuery): Promise<LogResult[][]> {
302
+ return this.stores.logs.getPrivateLogsByTags(query);
303
+ }
304
+
305
+ public getPublicLogsByTags(query: PublicLogsQuery): Promise<LogResult[][]> {
306
+ return this.stores.logs.getPublicLogsByTags(query);
307
+ }
308
+
309
+ public getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
310
+ return this.stores.contractClasses.getContractClass(id);
311
+ }
312
+
313
+ public getBytecodeCommitment(id: Fr): Promise<Fr | undefined> {
314
+ return this.stores.contractClasses.getBytecodeCommitment(id);
315
+ }
316
+
317
+ public async getContract(
318
+ address: AztecAddress,
319
+ maybeTimestamp?: UInt64,
320
+ ): Promise<ContractInstanceWithAddress | undefined> {
321
+ let timestamp;
322
+ if (maybeTimestamp === undefined) {
323
+ const latestBlockData = await this.getBlockData({ tag: 'proposed' });
324
+ timestamp = latestBlockData ? latestBlockData.header.globalVariables.timestamp : 0n;
325
+ } else {
326
+ timestamp = maybeTimestamp;
327
+ }
328
+
329
+ return this.stores.contractInstances.getContractInstance(address, timestamp);
330
+ }
331
+
332
+ public getContractClassIds(): Promise<Fr[]> {
333
+ return this.stores.contractClasses.getContractClassIds();
334
+ }
335
+
336
+ /** Looks up a public function name given a selector. */
337
+ public getDebugFunctionName(_address: AztecAddress, selector: FunctionSelector): Promise<string | undefined> {
338
+ return Promise.resolve(this.stores.functionNames.get(selector));
339
+ }
340
+
341
+ /** Register public function signatures so they can be looked up by selector. */
342
+ public registerContractFunctionSignatures(signatures: string[]): Promise<void> {
343
+ return this.stores.functionNames.register(signatures);
344
+ }
345
+
346
+ public getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
347
+ return this.stores.messages.getL1ToL2Messages(checkpointNumber);
348
+ }
349
+
350
+ public getL1ToL2MessageIndex(l1ToL2Message: Fr): Promise<bigint | undefined> {
351
+ return this.stores.messages.getL1ToL2MessageIndex(l1ToL2Message);
352
+ }
353
+
354
+ private async getPublishedCheckpointFromCheckpointData(checkpoint: CheckpointData): Promise<PublishedCheckpoint> {
355
+ const blocksForCheckpoint = await this.stores.blocks.getBlocksForCheckpoint(checkpoint.checkpointNumber);
356
+ if (!blocksForCheckpoint) {
357
+ throw new Error(`Blocks for checkpoint ${checkpoint.checkpointNumber} not found`);
358
+ }
359
+ const fullCheckpoint = new Checkpoint(
360
+ checkpoint.archive,
361
+ checkpoint.header,
362
+ blocksForCheckpoint,
363
+ checkpoint.checkpointNumber,
364
+ checkpoint.feeAssetPriceModifier,
365
+ );
366
+ return new PublishedCheckpoint(fullCheckpoint, checkpoint.l1, checkpoint.attestations);
367
+ }
368
+
369
+ public getBlocksForSlot(slotNumber: SlotNumber): Promise<L2Block[]> {
370
+ return this.stores.blocks.getBlocksForSlot(slotNumber);
371
+ }
372
+
373
+ /** Returns just the checkpoint numbers for all checkpoints whose slot falls within the given epoch. */
374
+ private getCheckpointNumbersForEpoch(epochNumber: EpochNumber): Promise<CheckpointNumber[]> {
375
+ if (!this.l1Constants) {
376
+ throw new Error('L1 constants not set');
377
+ }
378
+
379
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
380
+ return this.stores.blocks.getCheckpointNumbersForSlotRange(start, end);
381
+ }
382
+
383
+ public async getBlock(query: BlockQuery): Promise<L2Block | undefined> {
384
+ const resolved = await this.resolveBlockQuery(query);
385
+ if (resolved === undefined) {
386
+ return undefined;
387
+ }
388
+ if (this.isGenesisBlockQuery(resolved)) {
389
+ return this.getGenesisBlock();
390
+ }
391
+ return this.stores.blocks.getBlock(resolved);
392
+ }
393
+
394
+ /**
395
+ * Range queries iterate physical blocks only; the genesis block is NOT prepended.
396
+ * `L2BlockStream` consumers (`world-state.handleL2Blocks`, etc.) emit `blocks-added` events for
397
+ * real blocks and would be surprised by a synthetic block 0. Use {@link getBlock} or
398
+ * {@link getBlockData} for genesis-aware single-block lookups.
399
+ */
400
+ public async getBlocks(query: BlocksQuery): Promise<L2Block[]> {
401
+ const resolved = await this.resolveBlocksQuery(query);
402
+ return resolved ? this.stores.blocks.getBlocks(resolved) : [];
403
+ }
404
+
405
+ public async getBlockData(query: BlockQuery): Promise<BlockData | undefined> {
406
+ const resolved = await this.resolveBlockQuery(query);
407
+ if (resolved === undefined) {
408
+ return undefined;
409
+ }
410
+ if (this.isGenesisBlockQuery(resolved)) {
411
+ return this.getGenesisBlockData();
412
+ }
413
+ return this.stores.blocks.getBlockData(resolved);
414
+ }
415
+
416
+ /** See {@link getBlocks} — range queries do not prepend the genesis block. */
417
+ public async getBlocksData(query: BlocksQuery): Promise<BlockData[]> {
418
+ const resolved = await this.resolveBlocksQuery(query);
419
+ return resolved ? this.stores.blocks.getBlocksData(resolved) : [];
420
+ }
421
+
422
+ /**
423
+ * Resolves a {@link BlockQuery} to either the genesis sentinel or a {@link ResolvedBlockQuery}
424
+ * understood by BlockStore. Detects every shape that points at block 0 — `{number:0}`,
425
+ * `{hash}` matching the initial header, `{archive}` matching the post-genesis archive root,
426
+ * and `{tag}` resolving to 0 — and rewrites them to the sentinel so callers branch once.
427
+ */
428
+ private async resolveBlockQuery(query: BlockQuery): Promise<ResolvedBlockQuery | GenesisBlockQuery | undefined> {
429
+ if ('number' in query) {
430
+ return query.number === BlockNumber.ZERO ? { genesis: true } : query;
431
+ }
432
+ if ('hash' in query) {
433
+ return query.hash.equals(this.initialBlockHash) ? { genesis: true } : query;
434
+ }
435
+ if ('archive' in query) {
436
+ return query.archive.equals(this.genesisArchiveRoot) ? { genesis: true } : query;
437
+ }
438
+ const number = await this.resolveBlockTag(query.tag);
439
+ if (number === BlockNumber.ZERO) {
440
+ return { genesis: true };
441
+ }
442
+ return { number };
443
+ }
444
+
445
+ /** Maps a {@link BlockTag} to the matching block number for the current chain state. */
446
+ private resolveBlockTag(tag: BlockTag): Promise<BlockNumber> {
447
+ switch (tag) {
448
+ case 'latest':
449
+ case 'proposed':
450
+ return this.stores.blocks.getLatestL2BlockNumber();
451
+ case 'checkpointed':
452
+ return this.stores.blocks.getCheckpointedL2BlockNumber();
453
+ case 'proven':
454
+ return this.stores.blocks.getProvenBlockNumber();
455
+ case 'finalized':
456
+ return this.stores.blocks.getFinalizedL2BlockNumber();
457
+ }
458
+ }
459
+
460
+ /**
461
+ * Converts an epoch-based BlocksQuery to a from/limit query using l1Constants.
462
+ * Returns undefined when the epoch has no checkpoints, so callers can return [] without
463
+ * entering BlockStore. Reads only the two endpoint checkpoints rather than the whole epoch.
464
+ */
465
+ private async resolveBlocksQuery(query: BlocksQuery): Promise<ResolvedBlocksQuery | undefined> {
466
+ if (!('epoch' in query)) {
467
+ if (query.from < INITIAL_L2_BLOCK_NUM) {
468
+ throw new Error(
469
+ `getBlocks/getBlocksData: 'from' must be >= ${INITIAL_L2_BLOCK_NUM}, got ${query.from}. ` +
470
+ `Use getBlock({number:0})/getBlockData({number:0}) for genesis-aware single-block lookups.`,
471
+ );
472
+ }
473
+ return query;
474
+ }
475
+ const checkpointNumbers = await this.getCheckpointNumbersForEpoch(query.epoch);
476
+ if (checkpointNumbers.length === 0) {
477
+ return undefined;
478
+ }
479
+ const firstNumber = checkpointNumbers[0];
480
+ const lastNumber = checkpointNumbers[checkpointNumbers.length - 1];
481
+ const first = await this.stores.blocks.getCheckpointData(firstNumber);
482
+ if (!first) {
483
+ return undefined;
484
+ }
485
+ const last = firstNumber === lastNumber ? first : await this.stores.blocks.getCheckpointData(lastNumber);
486
+ if (!last) {
487
+ return undefined;
488
+ }
489
+ const from = BlockNumber(first.startBlock);
490
+ const limit = last.startBlock + last.blockCount - first.startBlock;
491
+ return { from, limit, onlyCheckpointed: true };
492
+ }
493
+ }