@aztec/archiver 4.0.0-nightly.20260114 → 4.0.0-nightly.20260115

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 (169) hide show
  1. package/dest/archiver.d.ts +134 -0
  2. package/dest/archiver.d.ts.map +1 -0
  3. package/dest/archiver.js +767 -0
  4. package/dest/{archiver/config.d.ts → config.d.ts} +9 -1
  5. package/dest/config.d.ts.map +1 -0
  6. package/dest/{archiver/config.js → config.js} +9 -0
  7. package/dest/{archiver/errors.d.ts → errors.d.ts} +1 -1
  8. package/dest/errors.d.ts.map +1 -0
  9. package/dest/factory.d.ts +5 -6
  10. package/dest/factory.d.ts.map +1 -1
  11. package/dest/factory.js +82 -5
  12. package/dest/index.d.ts +10 -4
  13. package/dest/index.d.ts.map +1 -1
  14. package/dest/index.js +8 -3
  15. package/dest/interfaces.d.ts +9 -0
  16. package/dest/interfaces.d.ts.map +1 -0
  17. package/dest/interfaces.js +3 -0
  18. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.d.ts +1 -1
  19. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  20. package/dest/{archiver/l1 → l1}/calldata_retriever.d.ts +2 -2
  21. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  22. package/dest/{archiver/l1 → l1}/data_retrieval.d.ts +2 -2
  23. package/dest/l1/data_retrieval.d.ts.map +1 -0
  24. package/dest/{archiver/l1 → l1}/debug_tx.d.ts +1 -1
  25. package/dest/l1/debug_tx.d.ts.map +1 -0
  26. package/dest/{archiver/l1 → l1}/spire_proposer.d.ts +1 -1
  27. package/dest/l1/spire_proposer.d.ts.map +1 -0
  28. package/dest/{archiver/l1 → l1}/trace_tx.d.ts +1 -1
  29. package/dest/l1/trace_tx.d.ts.map +1 -0
  30. package/dest/l1/types.d.ts +12 -0
  31. package/dest/l1/types.d.ts.map +1 -0
  32. package/dest/{archiver/l1 → l1}/validate_trace.d.ts +1 -1
  33. package/dest/l1/validate_trace.d.ts.map +1 -0
  34. package/dest/modules/data_source_base.d.ts +83 -0
  35. package/dest/modules/data_source_base.d.ts.map +1 -0
  36. package/dest/{archiver/archive_source_base.js → modules/data_source_base.js} +109 -10
  37. package/dest/modules/data_store_updater.d.ts +46 -0
  38. package/dest/modules/data_store_updater.d.ts.map +1 -0
  39. package/dest/modules/data_store_updater.js +216 -0
  40. package/dest/modules/instrumentation.d.ts +37 -0
  41. package/dest/modules/instrumentation.d.ts.map +1 -0
  42. package/dest/modules/l1_synchronizer.d.ts +67 -0
  43. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  44. package/dest/{archiver/archiver.js → modules/l1_synchronizer.js} +60 -543
  45. package/dest/{archiver → modules}/validation.d.ts +1 -1
  46. package/dest/modules/validation.d.ts.map +1 -0
  47. package/dest/{archiver/kv_archiver_store → store}/block_store.d.ts +1 -1
  48. package/dest/store/block_store.d.ts.map +1 -0
  49. package/dest/store/contract_class_store.d.ts +18 -0
  50. package/dest/store/contract_class_store.d.ts.map +1 -0
  51. package/dest/store/contract_instance_store.d.ts +24 -0
  52. package/dest/store/contract_instance_store.d.ts.map +1 -0
  53. package/dest/{archiver/kv_archiver_store → store}/kv_archiver_store.d.ts +2 -2
  54. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  55. package/dest/{archiver/kv_archiver_store → store}/log_store.d.ts +1 -1
  56. package/dest/store/log_store.d.ts.map +1 -0
  57. package/dest/{archiver/kv_archiver_store → store}/message_store.d.ts +1 -1
  58. package/dest/store/message_store.d.ts.map +1 -0
  59. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  60. package/dest/structs/data_retrieval.d.ts.map +1 -0
  61. package/dest/structs/inbox_message.d.ts +15 -0
  62. package/dest/structs/inbox_message.d.ts.map +1 -0
  63. package/dest/{archiver/structs → structs}/published.d.ts +1 -1
  64. package/dest/structs/published.d.ts.map +1 -0
  65. package/dest/{archiver/test → test}/fake_l1_state.d.ts +1 -1
  66. package/dest/test/fake_l1_state.d.ts.map +1 -0
  67. package/dest/test/index.d.ts +2 -1
  68. package/dest/test/index.d.ts.map +1 -1
  69. package/dest/test/index.js +1 -0
  70. package/dest/test/mock_structs.d.ts +76 -2
  71. package/dest/test/mock_structs.d.ts.map +1 -1
  72. package/dest/test/mock_structs.js +133 -2
  73. package/package.json +15 -17
  74. package/src/archiver.ts +522 -0
  75. package/src/{archiver/config.ts → config.ts} +11 -0
  76. package/src/factory.ts +118 -6
  77. package/src/index.ts +10 -3
  78. package/src/interfaces.ts +9 -0
  79. package/src/{archiver/l1 → l1}/calldata_retriever.ts +1 -1
  80. package/src/{archiver/l1 → l1}/data_retrieval.ts +1 -1
  81. package/src/{archiver/archive_source_base.ts → modules/data_source_base.ts} +130 -30
  82. package/src/modules/data_store_updater.ts +318 -0
  83. package/src/{archiver/archiver.ts → modules/l1_synchronizer.ts} +68 -717
  84. package/src/test/index.ts +1 -0
  85. package/src/test/mock_structs.ts +247 -2
  86. package/dest/archiver/archive_source_base.d.ts +0 -75
  87. package/dest/archiver/archive_source_base.d.ts.map +0 -1
  88. package/dest/archiver/archiver.d.ts +0 -168
  89. package/dest/archiver/archiver.d.ts.map +0 -1
  90. package/dest/archiver/archiver_store_updates.d.ts +0 -38
  91. package/dest/archiver/archiver_store_updates.d.ts.map +0 -1
  92. package/dest/archiver/archiver_store_updates.js +0 -212
  93. package/dest/archiver/config.d.ts.map +0 -1
  94. package/dest/archiver/errors.d.ts.map +0 -1
  95. package/dest/archiver/index.d.ts +0 -8
  96. package/dest/archiver/index.d.ts.map +0 -1
  97. package/dest/archiver/index.js +0 -6
  98. package/dest/archiver/instrumentation.d.ts +0 -37
  99. package/dest/archiver/instrumentation.d.ts.map +0 -1
  100. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  101. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  102. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  103. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  104. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  105. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  106. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  107. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  108. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +0 -1
  109. package/dest/archiver/l1/calldata_retriever.d.ts.map +0 -1
  110. package/dest/archiver/l1/data_retrieval.d.ts.map +0 -1
  111. package/dest/archiver/l1/debug_tx.d.ts.map +0 -1
  112. package/dest/archiver/l1/spire_proposer.d.ts.map +0 -1
  113. package/dest/archiver/l1/trace_tx.d.ts.map +0 -1
  114. package/dest/archiver/l1/types.d.ts +0 -12
  115. package/dest/archiver/l1/types.d.ts.map +0 -1
  116. package/dest/archiver/l1/validate_trace.d.ts.map +0 -1
  117. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  118. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  119. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  120. package/dest/archiver/structs/published.d.ts.map +0 -1
  121. package/dest/archiver/test/fake_l1_state.d.ts.map +0 -1
  122. package/dest/archiver/validation.d.ts.map +0 -1
  123. package/dest/rpc/index.d.ts +0 -9
  124. package/dest/rpc/index.d.ts.map +0 -1
  125. package/dest/rpc/index.js +0 -15
  126. package/src/archiver/archiver_store_updates.ts +0 -321
  127. package/src/archiver/index.ts +0 -7
  128. package/src/rpc/index.ts +0 -16
  129. /package/dest/{archiver/errors.js → errors.js} +0 -0
  130. /package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.js +0 -0
  131. /package/dest/{archiver/l1 → l1}/calldata_retriever.js +0 -0
  132. /package/dest/{archiver/l1 → l1}/data_retrieval.js +0 -0
  133. /package/dest/{archiver/l1 → l1}/debug_tx.js +0 -0
  134. /package/dest/{archiver/l1 → l1}/spire_proposer.js +0 -0
  135. /package/dest/{archiver/l1 → l1}/trace_tx.js +0 -0
  136. /package/dest/{archiver/l1 → l1}/types.js +0 -0
  137. /package/dest/{archiver/l1 → l1}/validate_trace.js +0 -0
  138. /package/dest/{archiver → modules}/instrumentation.js +0 -0
  139. /package/dest/{archiver → modules}/validation.js +0 -0
  140. /package/dest/{archiver/kv_archiver_store → store}/block_store.js +0 -0
  141. /package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +0 -0
  142. /package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +0 -0
  143. /package/dest/{archiver/kv_archiver_store → store}/kv_archiver_store.js +0 -0
  144. /package/dest/{archiver/kv_archiver_store → store}/log_store.js +0 -0
  145. /package/dest/{archiver/kv_archiver_store → store}/message_store.js +0 -0
  146. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  147. /package/dest/{archiver/structs → structs}/inbox_message.js +0 -0
  148. /package/dest/{archiver/structs → structs}/published.js +0 -0
  149. /package/dest/{archiver/test → test}/fake_l1_state.js +0 -0
  150. /package/src/{archiver/errors.ts → errors.ts} +0 -0
  151. /package/src/{archiver/l1 → l1}/README.md +0 -0
  152. /package/src/{archiver/l1 → l1}/bin/retrieve-calldata.ts +0 -0
  153. /package/src/{archiver/l1 → l1}/debug_tx.ts +0 -0
  154. /package/src/{archiver/l1 → l1}/spire_proposer.ts +0 -0
  155. /package/src/{archiver/l1 → l1}/trace_tx.ts +0 -0
  156. /package/src/{archiver/l1 → l1}/types.ts +0 -0
  157. /package/src/{archiver/l1 → l1}/validate_trace.ts +0 -0
  158. /package/src/{archiver → modules}/instrumentation.ts +0 -0
  159. /package/src/{archiver → modules}/validation.ts +0 -0
  160. /package/src/{archiver/kv_archiver_store → store}/block_store.ts +0 -0
  161. /package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +0 -0
  162. /package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +0 -0
  163. /package/src/{archiver/kv_archiver_store → store}/kv_archiver_store.ts +0 -0
  164. /package/src/{archiver/kv_archiver_store → store}/log_store.ts +0 -0
  165. /package/src/{archiver/kv_archiver_store → store}/message_store.ts +0 -0
  166. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
  167. /package/src/{archiver/structs → structs}/inbox_message.ts +0 -0
  168. /package/src/{archiver/structs → structs}/published.ts +0 -0
  169. /package/src/{archiver/test → test}/fake_l1_state.ts +0 -0
package/src/factory.ts CHANGED
@@ -1,15 +1,30 @@
1
+ import { EpochCache } from '@aztec/epoch-cache';
2
+ import { createEthereumChain } from '@aztec/ethereum/chain';
3
+ import { InboxContract, RollupContract } from '@aztec/ethereum/contracts';
4
+ import type { ViemPublicDebugClient } from '@aztec/ethereum/types';
1
5
  import { BlockNumber } from '@aztec/foundation/branded-types';
6
+ import { Buffer32 } from '@aztec/foundation/buffer';
7
+ import { merge } from '@aztec/foundation/collection';
8
+ import { Fr } from '@aztec/foundation/curves/bn254';
2
9
  import { createLogger } from '@aztec/foundation/log';
10
+ import { DateProvider } from '@aztec/foundation/timer';
3
11
  import type { DataStoreConfig } from '@aztec/kv-store/config';
4
12
  import { createStore } from '@aztec/kv-store/lmdb-v2';
5
13
  import { protocolContractNames } from '@aztec/protocol-contracts';
6
14
  import { BundledProtocolContractsProvider } from '@aztec/protocol-contracts/providers/bundle';
7
15
  import { FunctionType, decodeFunctionSignature } from '@aztec/stdlib/abi';
16
+ import type { ArchiverEmitter } from '@aztec/stdlib/block';
8
17
  import { type ContractClassPublic, computePublicBytecodeCommitment } from '@aztec/stdlib/contract';
18
+ import { getTelemetryClient } from '@aztec/telemetry-client';
9
19
 
10
- import { Archiver, type ArchiverDeps } from './archiver/archiver.js';
11
- import type { ArchiverConfig } from './archiver/config.js';
12
- import { ARCHIVER_DB_VERSION, KVArchiverDataStore } from './archiver/kv_archiver_store/kv_archiver_store.js';
20
+ import { EventEmitter } from 'events';
21
+ import { createPublicClient, fallback, http } from 'viem';
22
+
23
+ import { Archiver, type ArchiverDeps } from './archiver.js';
24
+ import { type ArchiverConfig, mapArchiverConfig } from './config.js';
25
+ import { ArchiverInstrumentation } from './modules/instrumentation.js';
26
+ import { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
27
+ import { ARCHIVER_DB_VERSION, KVArchiverDataStore } from './store/kv_archiver_store.js';
13
28
 
14
29
  export const ARCHIVER_STORE_NAME = 'archiver';
15
30
 
@@ -28,9 +43,8 @@ export async function createArchiverStore(
28
43
  /**
29
44
  * Creates a local archiver.
30
45
  * @param config - The archiver configuration.
31
- * @param blobClient - The blob client client.
46
+ * @param deps - The archiver dependencies (blobClient, epochCache, dateProvider, telemetry).
32
47
  * @param opts - The options.
33
- * @param telemetry - The telemetry client.
34
48
  * @returns The local archiver.
35
49
  */
36
50
  export async function createArchiver(
@@ -40,7 +54,105 @@ export async function createArchiver(
40
54
  ): Promise<Archiver> {
41
55
  const archiverStore = await createArchiverStore(config);
42
56
  await registerProtocolContracts(archiverStore);
43
- return Archiver.createAndSync(config, archiverStore, deps, opts.blockUntilSync);
57
+
58
+ // Create Ethereum clients
59
+ const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
60
+ const publicClient = createPublicClient({
61
+ chain: chain.chainInfo,
62
+ transport: fallback(config.l1RpcUrls.map(url => http(url, { batch: false }))),
63
+ pollingInterval: config.viemPollingIntervalMS,
64
+ });
65
+
66
+ // Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
67
+ const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
68
+ const debugClient = createPublicClient({
69
+ chain: chain.chainInfo,
70
+ transport: fallback(debugRpcUrls.map(url => http(url, { batch: false }))),
71
+ pollingInterval: config.viemPollingIntervalMS,
72
+ }) as ViemPublicDebugClient;
73
+
74
+ // Create L1 contract instances
75
+ const rollup = new RollupContract(publicClient, config.l1Contracts.rollupAddress);
76
+ const inbox = new InboxContract(publicClient, config.l1Contracts.inboxAddress);
77
+
78
+ // Fetch L1 constants from rollup contract
79
+ const [l1StartBlock, l1GenesisTime, proofSubmissionEpochs, genesisArchiveRoot, slashingProposerAddress] =
80
+ await Promise.all([
81
+ rollup.getL1StartBlock(),
82
+ rollup.getL1GenesisTime(),
83
+ rollup.getProofSubmissionEpochs(),
84
+ rollup.getGenesisArchiveTreeRoot(),
85
+ rollup.getSlashingProposerAddress(),
86
+ ] as const);
87
+
88
+ const l1StartBlockHash = await publicClient
89
+ .getBlock({ blockNumber: l1StartBlock, includeTransactions: false })
90
+ .then(block => Buffer32.fromString(block.hash));
91
+
92
+ const { aztecEpochDuration: epochDuration, aztecSlotDuration: slotDuration, ethereumSlotDuration } = config;
93
+
94
+ const l1Constants = {
95
+ l1StartBlockHash,
96
+ l1StartBlock,
97
+ l1GenesisTime,
98
+ epochDuration,
99
+ slotDuration,
100
+ ethereumSlotDuration,
101
+ proofSubmissionEpochs: Number(proofSubmissionEpochs),
102
+ genesisArchiveRoot: Fr.fromString(genesisArchiveRoot.toString()),
103
+ };
104
+
105
+ const archiverConfig = merge(
106
+ {
107
+ pollingIntervalMs: 10_000,
108
+ batchSize: 100,
109
+ maxAllowedEthClientDriftSeconds: 300,
110
+ ethereumAllowNoDebugHosts: false,
111
+ },
112
+ mapArchiverConfig(config),
113
+ );
114
+
115
+ const epochCache = deps.epochCache ?? (await EpochCache.create(config.l1Contracts.rollupAddress, config, deps));
116
+ const telemetry = deps.telemetry ?? getTelemetryClient();
117
+ const instrumentation = await ArchiverInstrumentation.new(telemetry, () => archiverStore.estimateSize());
118
+
119
+ // Create the event emitter that will be shared by archiver and synchronizer
120
+ const events = new EventEmitter() as ArchiverEmitter;
121
+
122
+ // Create the L1 synchronizer
123
+ const synchronizer = new ArchiverL1Synchronizer(
124
+ publicClient,
125
+ debugClient,
126
+ rollup,
127
+ inbox,
128
+ { ...config.l1Contracts, slashingProposerAddress },
129
+ archiverStore,
130
+ archiverConfig,
131
+ deps.blobClient,
132
+ epochCache,
133
+ deps.dateProvider ?? new DateProvider(),
134
+ instrumentation,
135
+ l1Constants,
136
+ events,
137
+ instrumentation.tracer,
138
+ );
139
+
140
+ const archiver = new Archiver(
141
+ publicClient,
142
+ debugClient,
143
+ rollup,
144
+ { ...config.l1Contracts, slashingProposerAddress },
145
+ archiverStore,
146
+ archiverConfig,
147
+ deps.blobClient,
148
+ instrumentation,
149
+ l1Constants,
150
+ synchronizer,
151
+ events,
152
+ );
153
+
154
+ await archiver.start(opts.blockUntilSync);
155
+ return archiver;
44
156
  }
45
157
 
46
158
  async function registerProtocolContracts(store: KVArchiverDataStore) {
package/src/index.ts CHANGED
@@ -1,5 +1,12 @@
1
- export * from './archiver/index.js';
2
1
  export * from './factory.js';
3
- export * from './rpc/index.js';
2
+ export * from './interfaces.js';
3
+ export * from './archiver.js';
4
+ export * from './modules/data_source_base.js';
5
+ export * from './modules/data_store_updater.js';
6
+ export * from './config.js';
4
7
 
5
- export { retrieveCheckpointsFromRollup, retrieveL2ProofVerifiedEvents } from './archiver/l1/data_retrieval.js';
8
+ export { type L1PublishedData } from './structs/published.js';
9
+ export { KVArchiverDataStore, ARCHIVER_DB_VERSION } from './store/kv_archiver_store.js';
10
+ export { ContractInstanceStore } from './store/contract_instance_store.js';
11
+
12
+ export { retrieveCheckpointsFromRollup, retrieveL2ProofVerifiedEvents } from './l1/data_retrieval.js';
@@ -0,0 +1,9 @@
1
+ import type { L2BlockSource } from '@aztec/stdlib/block';
2
+ import type { ContractDataSource } from '@aztec/stdlib/contract';
3
+ import type { L2LogsSource } from '@aztec/stdlib/interfaces/server';
4
+ import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
5
+
6
+ /**
7
+ * Helper interface to combine all sources this archiver implementation provides.
8
+ */
9
+ export type ArchiverDataSource = L2BlockSource & L2LogsSource & ContractDataSource & L1ToL2MessageSource;
@@ -28,7 +28,7 @@ import {
28
28
  toFunctionSelector,
29
29
  } from 'viem';
30
30
 
31
- import type { ArchiverInstrumentation } from '../instrumentation.js';
31
+ import type { ArchiverInstrumentation } from '../modules/instrumentation.js';
32
32
  import { getSuccessfulCallsFromDebug } from './debug_tx.js';
33
33
  import { getCallFromSpireProposer } from './spire_proposer.js';
34
34
  import { getSuccessfulCallsFromTrace } from './trace_tx.js';
@@ -30,7 +30,7 @@ import { BlockHeader, GlobalVariables, PartialStateReference, StateReference } f
30
30
  import { type Hex, decodeFunctionData, getAbiItem, hexToBytes } from 'viem';
31
31
 
32
32
  import { NoBlobBodiesFoundError } from '../errors.js';
33
- import type { ArchiverInstrumentation } from '../instrumentation.js';
33
+ import type { ArchiverInstrumentation } from '../modules/instrumentation.js';
34
34
  import type { DataRetrieval } from '../structs/data_retrieval.js';
35
35
  import type { InboxMessage } from '../structs/inbox_message.js';
36
36
  import { CalldataRetriever } from './calldata_retriever.js';
@@ -14,33 +14,32 @@ import {
14
14
  } from '@aztec/stdlib/block';
15
15
  import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
16
16
  import type { ContractClassPublic, ContractDataSource, ContractInstanceWithAddress } from '@aztec/stdlib/contract';
17
- import type { L1RollupConstants } from '@aztec/stdlib/epoch-helpers';
17
+ import { type L1RollupConstants, getSlotRangeForEpoch } from '@aztec/stdlib/epoch-helpers';
18
18
  import type { GetContractClassLogsResponse, GetPublicLogsResponse } from '@aztec/stdlib/interfaces/client';
19
19
  import type { L2LogsSource } from '@aztec/stdlib/interfaces/server';
20
20
  import type { LogFilter, SiloedTag, Tag, TxScopedL2Log } from '@aztec/stdlib/logs';
21
21
  import type { L1ToL2MessageSource } from '@aztec/stdlib/messaging';
22
+ import type { CheckpointHeader } from '@aztec/stdlib/rollup';
22
23
  import type { BlockHeader, IndexedTxEffect, TxHash, TxReceipt } from '@aztec/stdlib/tx';
23
24
  import type { UInt64 } from '@aztec/stdlib/types';
24
25
 
25
- import type { ArchiveSource } from './archiver.js';
26
- import type { KVArchiverDataStore } from './kv_archiver_store/kv_archiver_store.js';
26
+ import type { ArchiverDataSource } from '../interfaces.js';
27
+ import type { CheckpointData } from '../store/block_store.js';
28
+ import type { KVArchiverDataStore } from '../store/kv_archiver_store.js';
27
29
  import type { ValidateCheckpointResult } from './validation.js';
28
30
 
29
31
  /**
30
- * Abstract base class implementing ArchiveSource using a KVArchiverDataStore.
32
+ * Abstract base class implementing ArchiverDataSource using a KVArchiverDataStore.
31
33
  * Provides implementations for all store-delegating methods and declares abstract methods
32
34
  * for L1-dependent functionality that subclasses must implement.
33
35
  */
34
- export abstract class ArchiveSourceBase
35
- implements ArchiveSource, L2LogsSource, ContractDataSource, L1ToL2MessageSource
36
+ export abstract class ArchiverDataSourceBase
37
+ implements ArchiverDataSource, L2LogsSource, ContractDataSource, L1ToL2MessageSource
36
38
  {
37
- protected readonly store: KVArchiverDataStore;
38
-
39
- constructor(store: KVArchiverDataStore) {
40
- this.store = store;
41
- }
42
-
43
- // Abstract methods that require L1 dependencies
39
+ constructor(
40
+ protected readonly store: KVArchiverDataStore,
41
+ protected readonly l1Constants?: L1RollupConstants,
42
+ ) {}
44
43
 
45
44
  abstract getRollupAddress(): Promise<EthAddress>;
46
45
 
@@ -58,17 +57,21 @@ export abstract class ArchiveSourceBase
58
57
 
59
58
  abstract getL2EpochNumber(): Promise<EpochNumber | undefined>;
60
59
 
61
- abstract getCheckpointsForEpoch(epochNumber: EpochNumber): Promise<Checkpoint[]>;
62
-
63
- abstract getBlocksForEpoch(epochNumber: EpochNumber): Promise<L2Block[]>;
64
-
65
- abstract getBlockHeadersForEpoch(epochNumber: EpochNumber): Promise<BlockHeader[]>;
66
-
67
60
  abstract isEpochComplete(epochNumber: EpochNumber): Promise<boolean>;
68
61
 
69
62
  abstract syncImmediate(): Promise<void>;
70
63
 
71
- // Store-delegating methods
64
+ public getCheckpointNumber(): Promise<CheckpointNumber> {
65
+ return this.store.getSynchedCheckpointNumber();
66
+ }
67
+
68
+ public getSynchedCheckpointNumber(): Promise<CheckpointNumber> {
69
+ return this.store.getSynchedCheckpointNumber();
70
+ }
71
+
72
+ public getProvenCheckpointNumber(): Promise<CheckpointNumber> {
73
+ return this.store.getProvenCheckpointNumber();
74
+ }
72
75
 
73
76
  public getBlockNumber(): Promise<BlockNumber> {
74
77
  return this.store.getLatestBlockNumber();
@@ -91,6 +94,32 @@ export abstract class ArchiveSourceBase
91
94
  return this.store.getCheckpointedBlock(number);
92
95
  }
93
96
 
97
+ public getCheckpointedBlockNumber(): Promise<BlockNumber> {
98
+ return this.store.getCheckpointedL2BlockNumber();
99
+ }
100
+
101
+ public async getCheckpointHeader(number: CheckpointNumber | 'latest'): Promise<CheckpointHeader | undefined> {
102
+ if (number === 'latest') {
103
+ number = await this.store.getSynchedCheckpointNumber();
104
+ }
105
+ if (number === 0) {
106
+ return undefined;
107
+ }
108
+ const checkpoint = await this.store.getCheckpointData(number);
109
+ if (!checkpoint) {
110
+ return undefined;
111
+ }
112
+ return checkpoint.header;
113
+ }
114
+
115
+ public async getLastBlockNumberInCheckpoint(checkpointNumber: CheckpointNumber): Promise<BlockNumber | undefined> {
116
+ const checkpointData = await this.store.getCheckpointData(checkpointNumber);
117
+ if (!checkpointData) {
118
+ return undefined;
119
+ }
120
+ return BlockNumber(checkpointData.startBlock + checkpointData.numBlocks - 1);
121
+ }
122
+
94
123
  public async getCheckpointedBlocks(
95
124
  from: BlockNumber,
96
125
  limit: number,
@@ -151,8 +180,6 @@ export abstract class ArchiveSourceBase
151
180
  return blocks;
152
181
  }
153
182
 
154
- // L2LogsSource methods
155
-
156
183
  public getPrivateLogsByTags(tags: SiloedTag[]): Promise<TxScopedL2Log[][]> {
157
184
  return this.store.getPrivateLogsByTags(tags);
158
185
  }
@@ -169,8 +196,6 @@ export abstract class ArchiveSourceBase
169
196
  return this.store.getContractClassLogs(filter);
170
197
  }
171
198
 
172
- // ContractDataSource methods
173
-
174
199
  public getContractClass(id: Fr): Promise<ContractClassPublic | undefined> {
175
200
  return this.store.getContractClass(id);
176
201
  }
@@ -207,8 +232,6 @@ export abstract class ArchiveSourceBase
207
232
  return this.store.registerContractFunctionSignatures(signatures);
208
233
  }
209
234
 
210
- // L1ToL2MessageSource methods
211
-
212
235
  public getL1ToL2Messages(checkpointNumber: CheckpointNumber): Promise<Fr[]> {
213
236
  return this.store.getL1ToL2Messages(checkpointNumber);
214
237
  }
@@ -217,8 +240,6 @@ export abstract class ArchiveSourceBase
217
240
  return this.store.getL1ToL2MessageIndex(l1ToL2Message);
218
241
  }
219
242
 
220
- // Published checkpoint methods
221
-
222
243
  public async getPublishedCheckpoints(
223
244
  checkpointNumber: CheckpointNumber,
224
245
  limit: number,
@@ -248,6 +269,87 @@ export abstract class ArchiveSourceBase
248
269
  return fullCheckpoints;
249
270
  }
250
271
 
272
+ public async getBlocksForEpoch(epochNumber: EpochNumber): Promise<L2Block[]> {
273
+ if (!this.l1Constants) {
274
+ throw new Error('L1 constants not set');
275
+ }
276
+
277
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
278
+ const blocks: L2Block[] = [];
279
+
280
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
281
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
282
+ let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
283
+ const slot = (b: CheckpointData) => b.header.slotNumber;
284
+ while (checkpoint && slot(checkpoint) >= start) {
285
+ if (slot(checkpoint) <= end) {
286
+ // push the blocks on backwards
287
+ const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
288
+ for (let i = endBlock; i >= checkpoint.startBlock; i--) {
289
+ const block = await this.getBlock(BlockNumber(i));
290
+ if (block) {
291
+ blocks.push(block);
292
+ }
293
+ }
294
+ }
295
+ checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
296
+ }
297
+
298
+ return blocks.reverse();
299
+ }
300
+
301
+ public async getBlockHeadersForEpoch(epochNumber: EpochNumber): Promise<BlockHeader[]> {
302
+ if (!this.l1Constants) {
303
+ throw new Error('L1 constants not set');
304
+ }
305
+
306
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
307
+ const blocks: BlockHeader[] = [];
308
+
309
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
310
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
311
+ let checkpoint = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
312
+ const slot = (b: CheckpointData) => b.header.slotNumber;
313
+ while (checkpoint && slot(checkpoint) >= start) {
314
+ if (slot(checkpoint) <= end) {
315
+ // push the blocks on backwards
316
+ const endBlock = checkpoint.startBlock + checkpoint.numBlocks - 1;
317
+ for (let i = endBlock; i >= checkpoint.startBlock; i--) {
318
+ const block = await this.getBlockHeader(BlockNumber(i));
319
+ if (block) {
320
+ blocks.push(block);
321
+ }
322
+ }
323
+ }
324
+ checkpoint = await this.store.getCheckpointData(CheckpointNumber(checkpoint.checkpointNumber - 1));
325
+ }
326
+ return blocks.reverse();
327
+ }
328
+
329
+ public async getCheckpointsForEpoch(epochNumber: EpochNumber): Promise<Checkpoint[]> {
330
+ if (!this.l1Constants) {
331
+ throw new Error('L1 constants not set');
332
+ }
333
+
334
+ const [start, end] = getSlotRangeForEpoch(epochNumber, this.l1Constants);
335
+ const checkpoints: Checkpoint[] = [];
336
+
337
+ // Walk the list of checkpoints backwards and filter by slots matching the requested epoch.
338
+ // We'll typically ask for checkpoints for a very recent epoch, so we shouldn't need an index here.
339
+ let checkpointData = await this.store.getCheckpointData(await this.store.getSynchedCheckpointNumber());
340
+ const slot = (b: CheckpointData) => b.header.slotNumber;
341
+ while (checkpointData && slot(checkpointData) >= start) {
342
+ if (slot(checkpointData) <= end) {
343
+ // push the checkpoints on backwards
344
+ const [checkpoint] = await this.getPublishedCheckpoints(checkpointData.checkpointNumber, 1);
345
+ checkpoints.push(checkpoint.checkpoint);
346
+ }
347
+ checkpointData = await this.store.getCheckpointData(CheckpointNumber(checkpointData.checkpointNumber - 1));
348
+ }
349
+
350
+ return checkpoints.reverse();
351
+ }
352
+
251
353
  public async getPublishedBlocks(from: BlockNumber, limit: number, proven?: boolean): Promise<PublishedL2Block[]> {
252
354
  const checkpoints = await this.store.getRangeOfCheckpoints(CheckpointNumber(from), limit);
253
355
  const provenCheckpointNumber = await this.store.getProvenCheckpointNumber();
@@ -280,8 +382,6 @@ export abstract class ArchiveSourceBase
280
382
  return olbBlocks;
281
383
  }
282
384
 
283
- // Legacy APIs
284
-
285
385
  public async getBlock(number: BlockNumber): Promise<L2Block | undefined> {
286
386
  // If the number provided is -ve, then return the latest block.
287
387
  if (number < 0) {