@aztec-labs/archiver 6.0.0-nightly.20260829

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 (174) hide show
  1. package/README.md +178 -0
  2. package/dest/archiver.d.ts +194 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +948 -0
  5. package/dest/config.d.ts +34 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/config.js +100 -0
  8. package/dest/errors.d.ts +97 -0
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/errors.js +141 -0
  11. package/dest/factory.d.ts +32 -0
  12. package/dest/factory.d.ts.map +1 -0
  13. package/dest/factory.js +183 -0
  14. package/dest/index.d.ts +20 -0
  15. package/dest/index.d.ts.map +1 -0
  16. package/dest/index.js +18 -0
  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/l1/data_retrieval.js +308 -0
  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 +116 -0
  51. package/dest/modules/data_source_base.d.ts.map +1 -0
  52. package/dest/modules/data_source_base.js +334 -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 +392 -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 +1369 -0
  62. package/dest/modules/outbox_trees_resolver.d.ts +64 -0
  63. package/dest/modules/outbox_trees_resolver.d.ts.map +1 -0
  64. package/dest/modules/outbox_trees_resolver.js +184 -0
  65. package/dest/modules/validation.d.ts +40 -0
  66. package/dest/modules/validation.d.ts.map +1 -0
  67. package/dest/modules/validation.js +122 -0
  68. package/dest/store/block_store.d.ts +304 -0
  69. package/dest/store/block_store.d.ts.map +1 -0
  70. package/dest/store/block_store.js +1252 -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/store/contract_instance_store.js +129 -0
  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 +111 -0
  93. package/dest/store/message_store.d.ts.map +1 -0
  94. package/dest/store/message_store.js +522 -0
  95. package/dest/structs/data_retrieval.d.ts +27 -0
  96. package/dest/structs/data_retrieval.d.ts.map +1 -0
  97. package/dest/structs/data_retrieval.js +5 -0
  98. package/dest/structs/inbox_message.d.ts +17 -0
  99. package/dest/structs/inbox_message.d.ts.map +1 -0
  100. package/dest/structs/inbox_message.js +33 -0
  101. package/dest/structs/published.d.ts +2 -0
  102. package/dest/structs/published.d.ts.map +1 -0
  103. package/dest/structs/published.js +1 -0
  104. package/dest/test/fake_l1_state.d.ts +220 -0
  105. package/dest/test/fake_l1_state.d.ts.map +1 -0
  106. package/dest/test/fake_l1_state.js +565 -0
  107. package/dest/test/index.d.ts +5 -0
  108. package/dest/test/index.d.ts.map +1 -0
  109. package/dest/test/index.js +6 -0
  110. package/dest/test/mock_archiver.d.ts +35 -0
  111. package/dest/test/mock_archiver.d.ts.map +1 -0
  112. package/dest/test/mock_archiver.js +93 -0
  113. package/dest/test/mock_l1_to_l2_message_source.d.ts +24 -0
  114. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -0
  115. package/dest/test/mock_l1_to_l2_message_source.js +79 -0
  116. package/dest/test/mock_l2_block_source.d.ts +139 -0
  117. package/dest/test/mock_l2_block_source.d.ts.map +1 -0
  118. package/dest/test/mock_l2_block_source.js +489 -0
  119. package/dest/test/mock_structs.d.ts +92 -0
  120. package/dest/test/mock_structs.d.ts.map +1 -0
  121. package/dest/test/mock_structs.js +188 -0
  122. package/dest/test/noop_l1_archiver.d.ts +34 -0
  123. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  124. package/dest/test/noop_l1_archiver.js +88 -0
  125. package/package.json +108 -0
  126. package/src/archiver.ts +785 -0
  127. package/src/config.ts +133 -0
  128. package/src/errors.ts +211 -0
  129. package/src/factory.ts +262 -0
  130. package/src/index.ts +29 -0
  131. package/src/interfaces.ts +9 -0
  132. package/src/l1/README.md +55 -0
  133. package/src/l1/bin/retrieve-calldata.ts +194 -0
  134. package/src/l1/calldata_retriever.ts +529 -0
  135. package/src/l1/data_retrieval.ts +498 -0
  136. package/src/l1/debug_tx.ts +98 -0
  137. package/src/l1/spire_proposer.ts +151 -0
  138. package/src/l1/trace_tx.ts +127 -0
  139. package/src/l1/types.ts +12 -0
  140. package/src/l1/validate_historical_logs.ts +140 -0
  141. package/src/l1/validate_trace.ts +228 -0
  142. package/src/modules/contract_data_source_adapter.ts +46 -0
  143. package/src/modules/data_source_base.ts +482 -0
  144. package/src/modules/data_store_updater.ts +520 -0
  145. package/src/modules/instrumentation.ts +217 -0
  146. package/src/modules/l1_synchronizer.ts +1296 -0
  147. package/src/modules/outbox_trees_resolver.ts +220 -0
  148. package/src/modules/validation.ts +186 -0
  149. package/src/store/block_store.ts +1604 -0
  150. package/src/store/contract_class_store.ts +126 -0
  151. package/src/store/contract_instance_store.ts +178 -0
  152. package/src/store/data_stores.ts +103 -0
  153. package/src/store/function_names_cache.ts +37 -0
  154. package/src/store/l2_tips_cache.ts +35 -0
  155. package/src/store/log_store.ts +380 -0
  156. package/src/store/log_store_codec.ts +143 -0
  157. package/src/store/message_store.ts +621 -0
  158. package/src/structs/data_retrieval.ts +27 -0
  159. package/src/structs/inbox_message.ts +48 -0
  160. package/src/structs/published.ts +1 -0
  161. package/src/test/fake_l1_state.ts +807 -0
  162. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  163. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  164. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  165. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  166. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  167. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  168. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  169. package/src/test/index.ts +7 -0
  170. package/src/test/mock_archiver.ts +122 -0
  171. package/src/test/mock_l1_to_l2_message_source.ts +81 -0
  172. package/src/test/mock_l2_block_source.ts +620 -0
  173. package/src/test/mock_structs.ts +317 -0
  174. package/src/test/noop_l1_archiver.ts +157 -0
package/src/config.ts ADDED
@@ -0,0 +1,133 @@
1
+ import { type BlobClientConfig, blobClientConfigMapping } from '@aztec-labs/blob-client/client/config';
2
+ import { type L1ContractsConfig, l1ContractsConfigMappings } from '@aztec-labs/ethereum/config';
3
+ import { type L1ReaderConfig, l1ReaderConfigMappings } from '@aztec-labs/ethereum/l1-reader';
4
+ import {
5
+ type ConfigMappingsType,
6
+ booleanConfigHelper,
7
+ getConfigFromMappings,
8
+ numberConfigHelper,
9
+ optionalNumberConfigHelper,
10
+ } from '@aztec-labs/foundation/config';
11
+ import { type ChainConfig, type SequencerConfig, chainConfigMappings } from '@aztec-labs/stdlib/config';
12
+ import type { ArchiverSpecificConfig } from '@aztec-labs/stdlib/interfaces/server';
13
+ import { DEFAULT_ORPHAN_PRUNE_NO_PROPOSAL_TOLERANCE } from '@aztec-labs/stdlib/timetable';
14
+
15
+ /**
16
+ * The archiver configuration.
17
+ * There are 2 polling intervals used in this configuration. The first is the archiver polling interval, archiverPollingIntervalMS.
18
+ * This is the interval between successive calls to eth_blockNumber via viem.
19
+ * Results of calls to eth_blockNumber are cached by viem with this cache being updated periodically at the interval specified by viemPollingIntervalMS.
20
+ * As a result the maximum observed polling time for new blocks will be viemPollingIntervalMS + archiverPollingIntervalMS.
21
+ */
22
+ export type ArchiverConfig = ArchiverSpecificConfig &
23
+ L1ReaderConfig &
24
+ L1ContractsConfig &
25
+ BlobClientConfig &
26
+ ChainConfig &
27
+ Pick<SequencerConfig, 'blockDurationMs' | 'checkpointProposalSyncGraceSeconds'>;
28
+
29
+ export const archiverConfigMappings: ConfigMappingsType<ArchiverConfig> = {
30
+ ...blobClientConfigMapping,
31
+ archiverPollingIntervalMS: {
32
+ env: 'ARCHIVER_POLLING_INTERVAL_MS',
33
+ description: 'The polling interval in ms for retrieving new L2 blocks and encrypted logs.',
34
+ ...numberConfigHelper(500),
35
+ },
36
+ archiverBatchSize: {
37
+ env: 'ARCHIVER_BATCH_SIZE',
38
+ description: 'The number of L2 blocks the archiver will attempt to download at a time.',
39
+ ...numberConfigHelper(100),
40
+ },
41
+ archiverStoreMapSizeKb: {
42
+ env: 'ARCHIVER_STORE_MAP_SIZE_KB',
43
+ ...optionalNumberConfigHelper(),
44
+ description: 'The maximum possible size of the archiver DB in KB. Overwrites the general dataStoreMapSizeKb.',
45
+ },
46
+ blockDurationMs: {
47
+ env: 'SEQ_BLOCK_DURATION_MS',
48
+ description:
49
+ 'Duration per block in milliseconds when building multiple blocks per slot. Used to derive orphan proposed block pruning timing.',
50
+ ...optionalNumberConfigHelper(),
51
+ },
52
+ checkpointProposalSyncGraceSeconds: {
53
+ env: 'CHECKPOINT_PROPOSAL_SYNC_GRACE_SECONDS',
54
+ description:
55
+ 'Consensus grace in seconds for a received checkpoint proposal to materialize into local proposed state.',
56
+ ...optionalNumberConfigHelper(),
57
+ },
58
+ skipValidateCheckpointAttestations: {
59
+ description: 'Skip validating checkpoint attestations (for testing purposes only)',
60
+ ...booleanConfigHelper(false),
61
+ },
62
+ skipPromoteProposedCheckpointDuringL1Sync: {
63
+ description: 'Skip promoting proposed checkpoints during L1 sync (for testing purposes only)',
64
+ ...booleanConfigHelper(false),
65
+ },
66
+ maxAllowedEthClientDriftSeconds: {
67
+ env: 'MAX_ALLOWED_ETH_CLIENT_DRIFT_SECONDS',
68
+ description: 'Maximum allowed drift in seconds between the Ethereum client and current time.',
69
+ ...numberConfigHelper(300),
70
+ },
71
+ ethereumAllowNoDebugHosts: {
72
+ env: 'ETHEREUM_ALLOW_NO_DEBUG_HOSTS',
73
+ description: 'Whether to allow starting the archiver without debug/trace method support on Ethereum hosts',
74
+ ...booleanConfigHelper(true),
75
+ },
76
+ archiverSkipHistoricalLogsCheck: {
77
+ env: 'ARCHIVER_SKIP_HISTORICAL_LOGS_CHECK',
78
+ description:
79
+ 'Skip the startup check that probes the L1 RPC for historical Rollup contract logs. ' +
80
+ 'Set to true to bypass the check when the connected RPC node is known to prune old logs.',
81
+ ...booleanConfigHelper(false),
82
+ },
83
+ orphanPruneNoProposalTolerance: {
84
+ env: 'ARCHIVER_ORPHAN_PRUNE_NO_PROPOSAL_TOLERANCE',
85
+ description: 'Local tolerance in seconds before pruning an orphan block when no checkpoint proposal was received.',
86
+ ...numberConfigHelper(DEFAULT_ORPHAN_PRUNE_NO_PROPOSAL_TOLERANCE),
87
+ },
88
+ skipOrphanProposedBlockPruning: {
89
+ env: 'ARCHIVER_SKIP_ORPHAN_PROPOSED_BLOCK_PRUNING',
90
+ description: 'Skip pruning orphan proposed blocks that have no matching proposed checkpoint.',
91
+ ...booleanConfigHelper(false),
92
+ },
93
+ testPreloadStandardContracts: {
94
+ env: 'TEST_PRELOAD_STANDARD_CONTRACTS',
95
+ description:
96
+ 'Preload the standard contracts (AuthRegistry, PublicChecks, HandshakeRegistry) into the contract store at ' +
97
+ 'block 0. For test environments only, and only safe when genesis seeds the matching registration/deployment ' +
98
+ 'nullifiers; otherwise a later onchain publish would collide with the block-0 preload.',
99
+ ...booleanConfigHelper(false),
100
+ },
101
+ ...chainConfigMappings,
102
+ ...l1ReaderConfigMappings,
103
+ viemPollingIntervalMS: {
104
+ env: 'ARCHIVER_VIEM_POLLING_INTERVAL_MS',
105
+ description: 'The polling interval viem uses in ms',
106
+ ...numberConfigHelper(1000),
107
+ },
108
+ ...l1ContractsConfigMappings,
109
+ };
110
+
111
+ /**
112
+ * Returns the archiver configuration from the environment variables.
113
+ * Note: If an environment variable is not set, the default value is used.
114
+ * @returns The archiver configuration.
115
+ */
116
+ export function getArchiverConfigFromEnv(): ArchiverConfig {
117
+ return getConfigFromMappings<ArchiverConfig>(archiverConfigMappings);
118
+ }
119
+
120
+ /** Extracts the archiver-specific configuration from the full ArchiverConfig */
121
+ export function mapArchiverConfig(config: Partial<ArchiverConfig>) {
122
+ return {
123
+ pollingIntervalMs: config.archiverPollingIntervalMS,
124
+ batchSize: config.archiverBatchSize,
125
+ skipValidateCheckpointAttestations: config.skipValidateCheckpointAttestations,
126
+ skipPromoteProposedCheckpointDuringL1Sync: config.skipPromoteProposedCheckpointDuringL1Sync,
127
+ maxAllowedEthClientDriftSeconds: config.maxAllowedEthClientDriftSeconds,
128
+ ethereumAllowNoDebugHosts: config.ethereumAllowNoDebugHosts,
129
+ skipHistoricalLogsCheck: config.archiverSkipHistoricalLogsCheck,
130
+ orphanPruneNoProposalTolerance: config.orphanPruneNoProposalTolerance,
131
+ skipOrphanProposedBlockPruning: config.skipOrphanProposedBlockPruning,
132
+ };
133
+ }
package/src/errors.ts ADDED
@@ -0,0 +1,211 @@
1
+ import type { BlockNumber, CheckpointNumber } from '@aztec-labs/foundation/branded-types';
2
+ import type { Fr } from '@aztec-labs/foundation/schemas';
3
+
4
+ export class NoBlobBodiesFoundError extends Error {
5
+ constructor(l2BlockNum: number) {
6
+ super(`No blob bodies found for block ${l2BlockNum}`);
7
+ this.name = 'NoBlobBodiesFoundError';
8
+ }
9
+ }
10
+
11
+ export class BlockNumberNotSequentialError extends Error {
12
+ constructor(newBlockNumber: number, previous: number | undefined) {
13
+ super(`Cannot insert new block ${newBlockNumber} given previous block number is ${previous ?? 'undefined'}`);
14
+ this.name = 'BlockNumberNotSequentialError';
15
+ }
16
+ }
17
+
18
+ export class InitialCheckpointNumberNotSequentialError extends Error {
19
+ constructor(
20
+ public readonly newCheckpointNumber: number,
21
+ public readonly previousCheckpointNumber: number | undefined,
22
+ ) {
23
+ super(
24
+ `Cannot insert new checkpoint ${newCheckpointNumber} given previous checkpoint number in store is ${
25
+ previousCheckpointNumber ?? 'undefined'
26
+ }`,
27
+ );
28
+ this.name = 'InitialCheckpointNumberNotSequentialError';
29
+ }
30
+ }
31
+
32
+ export class CheckpointNumberNotSequentialError extends Error {
33
+ constructor(
34
+ public readonly newCheckpointNumber: CheckpointNumber,
35
+ public readonly previousCheckpointNumber: CheckpointNumber | undefined,
36
+ source?: 'proposed' | 'confirmed',
37
+ ) {
38
+ const qualifier = source ? `${source} ` : '';
39
+ super(
40
+ `Cannot insert new checkpoint ${newCheckpointNumber} given previous ${qualifier}checkpoint number is ${previousCheckpointNumber ?? 'undefined'}`,
41
+ );
42
+ this.name = 'CheckpointNumberNotSequentialError';
43
+ }
44
+ }
45
+
46
+ /** Thrown when a proposed block carries a checkpoint number that does not follow the latest one. */
47
+ export class BlockCheckpointNumberNotSequentialError extends Error {
48
+ constructor(
49
+ blockNumber: BlockNumber,
50
+ blockCheckpointNumber: CheckpointNumber,
51
+ previous: CheckpointNumber | undefined,
52
+ ) {
53
+ super(
54
+ `Cannot insert new block ${blockNumber} for checkpoint ${blockCheckpointNumber} given previous checkpoint number is ${previous ?? 'undefined'}`,
55
+ );
56
+ this.name = 'BlockCheckpointNumberNotSequentialError';
57
+ }
58
+ }
59
+
60
+ export class BlockIndexNotSequentialError extends Error {
61
+ constructor(newBlockIndex: number, previousBlockIndex: number | undefined) {
62
+ super(
63
+ `Cannot insert new block at checkpoint index ${newBlockIndex} given previous block index is ${previousBlockIndex ?? 'undefined'}`,
64
+ );
65
+ this.name = 'BlockIndexNotSequentialError';
66
+ }
67
+ }
68
+
69
+ export class BlockArchiveNotConsistentError extends Error {
70
+ constructor(
71
+ newBlockNumber: number,
72
+ previousBlockNumber: number | undefined,
73
+ newBlockArchive: Fr,
74
+ previousBlockArchive: Fr,
75
+ ) {
76
+ super(
77
+ `Cannot insert new block number ${newBlockNumber} with archive ${newBlockArchive.toString()} previous block number is ${previousBlockNumber ?? 'undefined'}, previous archive is ${previousBlockArchive?.toString() ?? 'undefined'}`,
78
+ );
79
+ this.name = 'BlockArchiveNotConsistentError';
80
+ }
81
+ }
82
+
83
+ export class CheckpointNotFoundError extends Error {
84
+ constructor(checkpointNumber: number) {
85
+ super(`Failed to find expected checkpoint number ${checkpointNumber}`);
86
+ this.name = 'CheckpointNotFoundError';
87
+ }
88
+ }
89
+
90
+ export class BlockNotFoundError extends Error {
91
+ constructor(blockNumber: number) {
92
+ super(`Failed to find expected block number ${blockNumber}`);
93
+ this.name = 'BlockNotFoundError';
94
+ }
95
+ }
96
+
97
+ /** Thrown when a proposed block matches a block that was already checkpointed. This is expected for late proposals. */
98
+ export class BlockAlreadyCheckpointedError extends Error {
99
+ constructor(public readonly blockNumber: number) {
100
+ super(`Block ${blockNumber} has already been checkpointed with the same content`);
101
+ this.name = 'BlockAlreadyCheckpointedError';
102
+ }
103
+ }
104
+
105
+ /**
106
+ * Thrown when a query names an Inbox bucket this archiver has not synced. Distinguishes "not synced yet, retry once
107
+ * L1 sync catches up" from a genuinely empty result.
108
+ */
109
+ export class InboxBucketNotSyncedError extends Error {
110
+ constructor(public readonly bucketSeq: bigint) {
111
+ super(`Inbox bucket ${bucketSeq} has not been synced`);
112
+ this.name = 'InboxBucketNotSyncedError';
113
+ }
114
+ }
115
+
116
+ /**
117
+ * Thrown when a cumulative Inbox message count does not resolve to a bucket boundary this archiver has synced, either
118
+ * because the count sits inside a bucket or because the bucket is not synced yet.
119
+ */
120
+ export class InboxBucketBoundaryNotSyncedError extends Error {
121
+ constructor(public readonly totalMsgCount: bigint) {
122
+ super(`No synced Inbox bucket ends at cumulative message count ${totalMsgCount}`);
123
+ this.name = 'InboxBucketBoundaryNotSyncedError';
124
+ }
125
+ }
126
+
127
+ /** Thrown when a proposed checkpoint number is stale (already processed). */
128
+ export class ProposedCheckpointStaleError extends Error {
129
+ constructor(
130
+ public readonly proposedCheckpointNumber: number,
131
+ public readonly currentProposedNumber: number,
132
+ ) {
133
+ super(`Stale proposed checkpoint ${proposedCheckpointNumber}: current proposed is ${currentProposedNumber}`);
134
+ this.name = 'ProposedCheckpointStaleError';
135
+ }
136
+ }
137
+
138
+ /** Thrown when a proposed checkpoint number is not the expected latestTip + 1. */
139
+ export class ProposedCheckpointNotSequentialError extends Error {
140
+ constructor(
141
+ public readonly proposedCheckpointNumber: number,
142
+ public readonly latestTipNumber: number,
143
+ ) {
144
+ super(
145
+ `Proposed checkpoint ${proposedCheckpointNumber} is not sequential: expected ${latestTipNumber + 1} (latest tip + 1, where tip is highest of confirmed or pending)`,
146
+ );
147
+ this.name = 'ProposedCheckpointNotSequentialError';
148
+ }
149
+ }
150
+
151
+ /** Thrown when a proposed checkpoint or block L2 slot has already expired on L1. */
152
+ export class BlockOrCheckpointSlotExpiredError extends Error {
153
+ constructor(
154
+ public readonly slot: number,
155
+ public readonly nextSlotStart: bigint,
156
+ public readonly l1TimestampSynced: bigint | undefined,
157
+ ) {
158
+ super(
159
+ `Checkpoint or block for slot ${slot} is expired: L1 synced to ${l1TimestampSynced} which is past the next slot start ${nextSlotStart}. ` +
160
+ `If the checkpoint still lands via a late L1 tx, the archiver will pick it up via normal L1-sync (not the pending-queue shortcut).`,
161
+ );
162
+ this.name = 'BlockOrCheckpointSlotExpiredError';
163
+ }
164
+ }
165
+
166
+ /** Thrown when attempting to promote a proposed checkpoint but no proposed checkpoint exists in the store. */
167
+ export class NoProposedCheckpointToPromoteError extends Error {
168
+ constructor() {
169
+ super('Cannot promote proposed checkpoint: no proposed checkpoint exists');
170
+ this.name = 'NoProposedCheckpointToPromoteError';
171
+ }
172
+ }
173
+
174
+ /** Thrown when the archive root of the proposed checkpoint does not match the expected one. */
175
+ export class ProposedCheckpointArchiveRootMismatchError extends Error {
176
+ constructor(
177
+ public readonly expectedArchiveRoot: Fr,
178
+ public readonly actualArchiveRoot: Fr,
179
+ ) {
180
+ super(
181
+ `Cannot promote proposed checkpoint: archive root mismatch (expected ${expectedArchiveRoot}, got ${actualArchiveRoot})`,
182
+ );
183
+ this.name = 'ProposedCheckpointArchiveRootMismatchError';
184
+ }
185
+ }
186
+
187
+ /** Thrown when the proposed checkpoint does not directly follow the latest confirmed checkpoint. */
188
+ export class ProposedCheckpointPromotionNotSequentialError extends Error {
189
+ constructor(
190
+ public readonly proposedCheckpointNumber: number,
191
+ public readonly latestCheckpointNumber: number,
192
+ ) {
193
+ super(
194
+ `Cannot promote proposed checkpoint: not sequential (latest ${latestCheckpointNumber}, proposed ${proposedCheckpointNumber})`,
195
+ );
196
+ this.name = 'ProposedCheckpointPromotionNotSequentialError';
197
+ }
198
+ }
199
+
200
+ /** Thrown when a proposed block conflicts with an already checkpointed block (different content). */
201
+ export class CannotOverwriteCheckpointedBlockError extends Error {
202
+ constructor(
203
+ public readonly blockNumber: number,
204
+ public readonly lastCheckpointedBlock: number,
205
+ ) {
206
+ super(
207
+ `Cannot add block ${blockNumber}: would overwrite checkpointed data (checkpointed up to block ${lastCheckpointedBlock})`,
208
+ );
209
+ this.name = 'CannotOverwriteCheckpointedBlockError';
210
+ }
211
+ }
package/src/factory.ts ADDED
@@ -0,0 +1,262 @@
1
+ import { EpochCache } from '@aztec-labs/epoch-cache';
2
+ import { createEthereumChain } from '@aztec-labs/ethereum/chain';
3
+ import { makeL1HttpTransport } from '@aztec-labs/ethereum/client';
4
+ import { InboxContract, OutboxContract, RollupContract } from '@aztec-labs/ethereum/contracts';
5
+ import { pickL1ContractAddresses } from '@aztec-labs/ethereum/l1-contract-addresses';
6
+ import type { ViemPublicDebugClient } from '@aztec-labs/ethereum/types';
7
+ import { BlockNumber } from '@aztec-labs/foundation/branded-types';
8
+ import { Buffer32 } from '@aztec-labs/foundation/buffer';
9
+ import { merge } from '@aztec-labs/foundation/collection';
10
+ import { Fr } from '@aztec-labs/foundation/curves/bn254';
11
+ import { DateProvider } from '@aztec-labs/foundation/timer';
12
+ import { createStore } from '@aztec-labs/kv-store/lmdb-v2';
13
+ import { protocolContractNames } from '@aztec-labs/protocol-contracts';
14
+ import { BundledProtocolContractsProvider } from '@aztec-labs/protocol-contracts/providers/bundle';
15
+ import { getPublishableStandardContracts } from '@aztec-labs/standard-contracts';
16
+ import { FunctionType, decodeFunctionSignature } from '@aztec-labs/stdlib/abi';
17
+ import type { ArchiverEmitter, BlockHash } from '@aztec-labs/stdlib/block';
18
+ import { DEFAULT_BLOCK_DURATION_MS } from '@aztec-labs/stdlib/config';
19
+ import { type ContractClassPublicWithCommitment, computePublicBytecodeCommitment } from '@aztec-labs/stdlib/contract';
20
+ import type { DataStoreConfig } from '@aztec-labs/stdlib/kv-store';
21
+ import {
22
+ DEFAULT_ORPHAN_PRUNE_NO_PROPOSAL_TOLERANCE,
23
+ getDefaultCheckpointProposalSyncGrace,
24
+ } from '@aztec-labs/stdlib/timetable';
25
+ import type { BlockHeader } from '@aztec-labs/stdlib/tx';
26
+ import { getTelemetryClient } from '@aztec-labs/telemetry-client';
27
+ import { EventEmitter } from 'events';
28
+ import { createPublicClient } from 'viem';
29
+
30
+ import { Archiver, type ArchiverDeps } from './archiver.js';
31
+ import { type ArchiverConfig, mapArchiverConfig } from './config.js';
32
+ import { ArchiverInstrumentation } from './modules/instrumentation.js';
33
+ import { ArchiverL1Synchronizer } from './modules/l1_synchronizer.js';
34
+ import { ARCHIVER_DB_VERSION, type ArchiverDataStores, createArchiverDataStores } from './store/data_stores.js';
35
+ import { L2TipsCache } from './store/l2_tips_cache.js';
36
+
37
+ export const ARCHIVER_STORE_NAME = 'archiver';
38
+
39
+ /** Creates an archiver store. */
40
+ export async function createArchiverStore(
41
+ userConfig: Pick<ArchiverConfig, 'archiverStoreMapSizeKb'> & DataStoreConfig,
42
+ genesisBlockHash: BlockHash,
43
+ ): Promise<ArchiverDataStores> {
44
+ const config = {
45
+ ...userConfig,
46
+ dataStoreMapSizeKb: userConfig.archiverStoreMapSizeKb ?? userConfig.dataStoreMapSizeKb,
47
+ };
48
+ const store = await createStore(ARCHIVER_STORE_NAME, ARCHIVER_DB_VERSION, config);
49
+ return createArchiverDataStores(store, genesisBlockHash);
50
+ }
51
+
52
+ /**
53
+ * Creates a local archiver.
54
+ * @param config - The archiver configuration.
55
+ * @param deps - The archiver dependencies (blobClient, epochCache, dateProvider, telemetry).
56
+ * @param opts - The options.
57
+ * @param initialHeader - The genesis block header from world-state, used to answer block-0 queries.
58
+ * @param initialBlockHash - Precomputed hash of `initialHeader`. Hoisted to the caller so the archiver
59
+ * can expose `getGenesisBlockHash()` synchronously.
60
+ * @returns The local archiver.
61
+ */
62
+ export async function createArchiver(
63
+ config: ArchiverConfig & DataStoreConfig,
64
+ deps: ArchiverDeps,
65
+ opts: { blockUntilSync: boolean } = { blockUntilSync: true },
66
+ initialHeader: BlockHeader,
67
+ initialBlockHash: BlockHash,
68
+ ): Promise<Archiver> {
69
+ const archiverStore = await createArchiverStore(config, initialBlockHash);
70
+ await registerProtocolContracts(archiverStore);
71
+ if (config.testPreloadStandardContracts) {
72
+ await registerStandardContracts(archiverStore);
73
+ }
74
+
75
+ // Create Ethereum clients
76
+ const chain = createEthereumChain(config.l1RpcUrls, config.l1ChainId);
77
+ const httpTimeout = config.l1HttpTimeoutMS;
78
+ const publicClient = createPublicClient({
79
+ chain: chain.chainInfo,
80
+ transport: makeL1HttpTransport(config.l1RpcUrls, { timeout: httpTimeout }),
81
+ pollingInterval: config.viemPollingIntervalMS,
82
+ });
83
+
84
+ // Create debug client using debug RPC URLs if available, otherwise fall back to regular RPC URLs
85
+ const debugRpcUrls = config.l1DebugRpcUrls.length > 0 ? config.l1DebugRpcUrls : config.l1RpcUrls;
86
+ const debugClient = createPublicClient({
87
+ chain: chain.chainInfo,
88
+ transport: makeL1HttpTransport(debugRpcUrls, { timeout: httpTimeout }),
89
+ pollingInterval: config.viemPollingIntervalMS,
90
+ }) as ViemPublicDebugClient;
91
+
92
+ // Create L1 contract instances
93
+ const rollup = new RollupContract(publicClient, config.rollupAddress);
94
+ const inbox = new InboxContract(publicClient, config.inboxAddress);
95
+ const outbox = new OutboxContract(publicClient, config.outboxAddress);
96
+
97
+ // Fetch L1 constants from rollup contract
98
+ const [
99
+ l1StartBlock,
100
+ l1GenesisTime,
101
+ proofSubmissionEpochs,
102
+ genesisArchiveRoot,
103
+ slashingProposerAddress,
104
+ targetCommitteeSize,
105
+ rollupManaLimit,
106
+ ] = await Promise.all([
107
+ rollup.getL1StartBlock(),
108
+ rollup.getL1GenesisTime(),
109
+ rollup.getProofSubmissionEpochs(),
110
+ rollup.getGenesisArchiveTreeRoot(),
111
+ rollup.getSlashingProposerAddress(),
112
+ rollup.getTargetCommitteeSize(),
113
+ rollup.getManaLimit(),
114
+ ] as const);
115
+
116
+ const l1StartBlockHash = await publicClient
117
+ .getBlock({ blockNumber: l1StartBlock, includeTransactions: false })
118
+ .then(block => Buffer32.fromString(block.hash));
119
+
120
+ const { aztecEpochDuration: epochDuration, aztecSlotDuration: slotDuration, ethereumSlotDuration } = config;
121
+
122
+ const l1Constants = {
123
+ l1StartBlockHash,
124
+ l1StartBlock,
125
+ l1GenesisTime,
126
+ epochDuration,
127
+ slotDuration,
128
+ ethereumSlotDuration,
129
+ proofSubmissionEpochs: Number(proofSubmissionEpochs),
130
+ targetCommitteeSize,
131
+ genesisArchiveRoot: Fr.fromString(genesisArchiveRoot.toString()),
132
+ rollupManaLimit: Number(rollupManaLimit),
133
+ };
134
+
135
+ const archiverConfig = merge(
136
+ {
137
+ pollingIntervalMs: 10_000,
138
+ batchSize: 100,
139
+ maxAllowedEthClientDriftSeconds: 300,
140
+ ethereumAllowNoDebugHosts: false,
141
+ skipHistoricalLogsCheck: false,
142
+ checkpointProposalSyncGrace:
143
+ config.checkpointProposalSyncGraceSeconds ??
144
+ getDefaultCheckpointProposalSyncGrace((config.blockDurationMs ?? DEFAULT_BLOCK_DURATION_MS) / 1000),
145
+ orphanPruneNoProposalTolerance: DEFAULT_ORPHAN_PRUNE_NO_PROPOSAL_TOLERANCE,
146
+ skipOrphanProposedBlockPruning: false,
147
+ blockDuration: (config.blockDurationMs ?? DEFAULT_BLOCK_DURATION_MS) / 1000,
148
+ },
149
+ mapArchiverConfig(config),
150
+ );
151
+
152
+ const epochCache = deps.epochCache ?? (await EpochCache.create(config.rollupAddress, config, deps));
153
+ const telemetry = deps.telemetry ?? getTelemetryClient();
154
+ const instrumentation = await ArchiverInstrumentation.new(telemetry, () => archiverStore.db.estimateSize());
155
+
156
+ // Create the event emitter that will be shared by archiver and synchronizer
157
+ const events = new EventEmitter() as ArchiverEmitter;
158
+
159
+ // Create L2 tips cache shared by archiver and synchronizer. The genesis block hash is dynamic —
160
+ // it depends on the injected initial header (genesisTimestamp + prefilled state). Hoisted to the
161
+ // caller so we can pass the same value to the archiver and expose it via `getGenesisBlockHash()`.
162
+ const l2TipsCache = new L2TipsCache(archiverStore.blocks, initialBlockHash);
163
+
164
+ // Create the L1 synchronizer
165
+ const synchronizer = new ArchiverL1Synchronizer(
166
+ publicClient,
167
+ debugClient,
168
+ rollup,
169
+ inbox,
170
+ archiverStore,
171
+ archiverConfig,
172
+ deps.blobClient,
173
+ epochCache,
174
+ deps.dateProvider ?? new DateProvider(),
175
+ instrumentation,
176
+ l1Constants,
177
+ events,
178
+ instrumentation.tracer,
179
+ l2TipsCache,
180
+ undefined, // log (use default)
181
+ );
182
+
183
+ const archiver = new Archiver(
184
+ publicClient,
185
+ debugClient,
186
+ rollup,
187
+ outbox,
188
+ { ...pickL1ContractAddresses(config), slashingProposerAddress },
189
+ archiverStore,
190
+ archiverConfig,
191
+ deps.blobClient,
192
+ instrumentation,
193
+ l1Constants,
194
+ synchronizer,
195
+ events,
196
+ initialHeader,
197
+ initialBlockHash,
198
+ l2TipsCache,
199
+ deps.dateProvider ?? new DateProvider(),
200
+ );
201
+
202
+ await archiver.start(opts.blockUntilSync);
203
+ return archiver;
204
+ }
205
+
206
+ /** Registers protocol contracts in the archiver store. Idempotent — skips contracts that already exist (e.g. on node restart). */
207
+ export async function registerProtocolContracts(stores: ArchiverDataStores) {
208
+ const blockNumber = 0;
209
+ for (const name of protocolContractNames) {
210
+ const provider = new BundledProtocolContractsProvider();
211
+ const contract = await provider.getProtocolContractArtifact(name);
212
+
213
+ // Skip if already registered (happens on node restart with a persisted store).
214
+ if (await stores.contractClasses.getContractClass(contract.contractClass.id)) {
215
+ continue;
216
+ }
217
+
218
+ const publicBytecodeCommitment = await computePublicBytecodeCommitment(contract.contractClass.packedBytecode);
219
+ const contractClassPublic: ContractClassPublicWithCommitment = {
220
+ ...contract.contractClass,
221
+ publicBytecodeCommitment,
222
+ };
223
+
224
+ const publicFunctionSignatures = contract.artifact.functions
225
+ .filter(fn => fn.functionType === FunctionType.PUBLIC)
226
+ .map(fn => decodeFunctionSignature(fn.name, fn.parameters));
227
+
228
+ await stores.functionNames.register(publicFunctionSignatures);
229
+ await stores.contractClasses.addContractClasses([contractClassPublic], BlockNumber(blockNumber));
230
+ await stores.contractInstances.addContractInstances([contract.instance], BlockNumber(blockNumber));
231
+ }
232
+ }
233
+
234
+ /**
235
+ * Preloads the standard contracts (AuthRegistry, PublicChecks, HandshakeRegistry) into the archiver store at block 0,
236
+ * mirroring {@link registerProtocolContracts}. Only invoked for test environments (via `testPreloadStandardContracts`),
237
+ * which also seed the matching registration/deployment nullifiers into the genesis nullifier tree so the store and tree
238
+ * stay consistent. Idempotent — skips contracts that already exist (e.g. on node restart).
239
+ */
240
+ export async function registerStandardContracts(stores: ArchiverDataStores) {
241
+ const blockNumber = 0;
242
+ for (const contract of await getPublishableStandardContracts()) {
243
+ // Skip if already registered (happens on node restart with a persisted store).
244
+ if (await stores.contractClasses.getContractClass(contract.contractClass.id)) {
245
+ continue;
246
+ }
247
+
248
+ const publicBytecodeCommitment = await computePublicBytecodeCommitment(contract.contractClass.packedBytecode);
249
+ const contractClassPublic: ContractClassPublicWithCommitment = {
250
+ ...contract.contractClass,
251
+ publicBytecodeCommitment,
252
+ };
253
+
254
+ const publicFunctionSignatures = contract.artifact.functions
255
+ .filter(fn => fn.functionType === FunctionType.PUBLIC)
256
+ .map(fn => decodeFunctionSignature(fn.name, fn.parameters));
257
+
258
+ await stores.functionNames.register(publicFunctionSignatures);
259
+ await stores.contractClasses.addContractClasses([contractClassPublic], BlockNumber(blockNumber));
260
+ await stores.contractInstances.addContractInstances([contract.instance], BlockNumber(blockNumber));
261
+ }
262
+ }
package/src/index.ts ADDED
@@ -0,0 +1,29 @@
1
+ export * from './factory.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';
7
+ export * from './errors.js';
8
+
9
+ export { type L1PublishedData } from './structs/published.js';
10
+ export {
11
+ ARCHIVER_DB_VERSION,
12
+ type ArchiverDataStores,
13
+ type ArchiverL1SynchPoint,
14
+ backupArchiverDataStores,
15
+ createArchiverDataStores,
16
+ createContractDataSource,
17
+ getArchiverSynchPoint,
18
+ } from './store/data_stores.js';
19
+ export { FunctionNamesCache } from './store/function_names_cache.js';
20
+ export { ArchiverContractDataSourceAdapter } from './modules/contract_data_source_adapter.js';
21
+ export { BlockStore } from './store/block_store.js';
22
+ export { LogStore } from './store/log_store.js';
23
+ export { MessageStore } from './store/message_store.js';
24
+ export { ContractClassStore } from './store/contract_class_store.js';
25
+ export { ContractInstanceStore } from './store/contract_instance_store.js';
26
+ export { L2TipsCache } from './store/l2_tips_cache.js';
27
+
28
+ export { retrieveL2ProofVerifiedEvents } from './l1/data_retrieval.js';
29
+ export { CalldataRetriever } from './l1/calldata_retriever.js';
@@ -0,0 +1,9 @@
1
+ import type { L2BlockSource } from '@aztec-labs/stdlib/block';
2
+ import type { ContractDataSource } from '@aztec-labs/stdlib/contract';
3
+ import type { L2LogsSource } from '@aztec-labs/stdlib/interfaces/server';
4
+ import type { L1ToL2MessageSource } from '@aztec-labs/stdlib/messaging';
5
+
6
+ /**
7
+ * Helper interface to combine all sources this archiver implementation provides.
8
+ */
9
+ export type ArchiverDataSource = L2BlockSource & L2LogsSource & ContractDataSource & L1ToL2MessageSource;