@aztec/archiver 0.0.1-commit.9b94fc1 → 0.0.1-commit.9badcec54

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 (213) hide show
  1. package/README.md +162 -22
  2. package/dest/archiver.d.ts +140 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +750 -0
  5. package/dest/config.d.ts +30 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/{archiver/config.js → config.js} +24 -7
  8. package/dest/errors.d.ts +65 -0
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/errors.js +91 -0
  11. package/dest/factory.d.ts +8 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +99 -15
  14. package/dest/index.d.ts +11 -4
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +9 -3
  17. package/dest/interfaces.d.ts +9 -0
  18. package/dest/interfaces.d.ts.map +1 -0
  19. package/dest/interfaces.js +3 -0
  20. package/dest/l1/bin/retrieve-calldata.d.ts +3 -0
  21. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  22. package/dest/l1/bin/retrieve-calldata.js +152 -0
  23. package/dest/l1/calldata_retriever.d.ts +135 -0
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/l1/calldata_retriever.js +403 -0
  26. package/dest/l1/data_retrieval.d.ts +88 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/{archiver → l1}/data_retrieval.js +87 -160
  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 +97 -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_trace.d.ts +32 -0
  42. package/dest/l1/validate_trace.d.ts.map +1 -0
  43. package/dest/l1/validate_trace.js +154 -0
  44. package/dest/modules/data_source_base.d.ts +91 -0
  45. package/dest/modules/data_source_base.d.ts.map +1 -0
  46. package/dest/modules/data_source_base.js +222 -0
  47. package/dest/modules/data_store_updater.d.ts +86 -0
  48. package/dest/modules/data_store_updater.d.ts.map +1 -0
  49. package/dest/modules/data_store_updater.js +333 -0
  50. package/dest/modules/instrumentation.d.ts +52 -0
  51. package/dest/modules/instrumentation.d.ts.map +1 -0
  52. package/dest/modules/instrumentation.js +138 -0
  53. package/dest/modules/l1_synchronizer.d.ts +73 -0
  54. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  55. package/dest/modules/l1_synchronizer.js +1154 -0
  56. package/dest/modules/validation.d.ts +17 -0
  57. package/dest/modules/validation.d.ts.map +1 -0
  58. package/dest/{archiver → modules}/validation.js +9 -3
  59. package/dest/store/block_store.d.ts +230 -0
  60. package/dest/store/block_store.d.ts.map +1 -0
  61. package/dest/store/block_store.js +942 -0
  62. package/dest/store/contract_class_store.d.ts +17 -0
  63. package/dest/store/contract_class_store.d.ts.map +1 -0
  64. package/dest/store/contract_class_store.js +64 -0
  65. package/dest/store/contract_instance_store.d.ts +24 -0
  66. package/dest/store/contract_instance_store.d.ts.map +1 -0
  67. package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +7 -3
  68. package/dest/store/kv_archiver_store.d.ts +377 -0
  69. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  70. package/dest/store/kv_archiver_store.js +494 -0
  71. package/dest/store/l2_tips_cache.d.ts +20 -0
  72. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  73. package/dest/store/l2_tips_cache.js +109 -0
  74. package/dest/store/log_store.d.ts +57 -0
  75. package/dest/store/log_store.d.ts.map +1 -0
  76. package/dest/store/log_store.js +531 -0
  77. package/dest/store/message_store.d.ts +44 -0
  78. package/dest/store/message_store.d.ts.map +1 -0
  79. package/dest/{archiver/kv_archiver_store → store}/message_store.js +36 -23
  80. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  81. package/dest/structs/data_retrieval.d.ts.map +1 -0
  82. package/dest/structs/inbox_message.d.ts +15 -0
  83. package/dest/structs/inbox_message.d.ts.map +1 -0
  84. package/dest/{archiver/structs → structs}/inbox_message.js +6 -5
  85. package/dest/structs/published.d.ts +2 -0
  86. package/dest/structs/published.d.ts.map +1 -0
  87. package/dest/test/fake_l1_state.d.ts +210 -0
  88. package/dest/test/fake_l1_state.d.ts.map +1 -0
  89. package/dest/test/fake_l1_state.js +490 -0
  90. package/dest/test/index.d.ts +2 -1
  91. package/dest/test/index.d.ts.map +1 -1
  92. package/dest/test/index.js +4 -1
  93. package/dest/test/mock_archiver.d.ts +16 -8
  94. package/dest/test/mock_archiver.d.ts.map +1 -1
  95. package/dest/test/mock_archiver.js +19 -14
  96. package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
  97. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  98. package/dest/test/mock_l1_to_l2_message_source.js +23 -12
  99. package/dest/test/mock_l2_block_source.d.ts +59 -20
  100. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  101. package/dest/test/mock_l2_block_source.js +276 -85
  102. package/dest/test/mock_structs.d.ts +83 -4
  103. package/dest/test/mock_structs.d.ts.map +1 -1
  104. package/dest/test/mock_structs.js +157 -11
  105. package/dest/test/noop_l1_archiver.d.ts +26 -0
  106. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  107. package/dest/test/noop_l1_archiver.js +71 -0
  108. package/package.json +20 -20
  109. package/src/archiver.ts +510 -0
  110. package/src/{archiver/config.ts → config.ts} +37 -14
  111. package/src/errors.ts +146 -0
  112. package/src/factory.ts +154 -17
  113. package/src/index.ts +11 -3
  114. package/src/interfaces.ts +9 -0
  115. package/src/l1/README.md +55 -0
  116. package/src/l1/bin/retrieve-calldata.ts +194 -0
  117. package/src/l1/calldata_retriever.ts +512 -0
  118. package/src/{archiver → l1}/data_retrieval.ts +146 -233
  119. package/src/l1/debug_tx.ts +99 -0
  120. package/src/l1/spire_proposer.ts +152 -0
  121. package/src/l1/trace_tx.ts +128 -0
  122. package/src/l1/types.ts +13 -0
  123. package/src/l1/validate_trace.ts +229 -0
  124. package/src/modules/data_source_base.ts +347 -0
  125. package/src/modules/data_store_updater.ts +437 -0
  126. package/src/modules/instrumentation.ts +196 -0
  127. package/src/modules/l1_synchronizer.ts +961 -0
  128. package/src/{archiver → modules}/validation.ts +13 -8
  129. package/src/store/block_store.ts +1245 -0
  130. package/src/store/contract_class_store.ts +82 -0
  131. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +10 -7
  132. package/src/store/kv_archiver_store.ts +713 -0
  133. package/src/store/l2_tips_cache.ts +134 -0
  134. package/src/store/log_store.ts +733 -0
  135. package/src/{archiver/kv_archiver_store → store}/message_store.ts +48 -28
  136. package/src/{archiver/structs → structs}/inbox_message.ts +7 -7
  137. package/src/{archiver/structs → structs}/published.ts +0 -1
  138. package/src/test/fake_l1_state.ts +747 -0
  139. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  140. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  141. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  142. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  143. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  144. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  145. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  146. package/src/test/index.ts +4 -0
  147. package/src/test/mock_archiver.ts +23 -16
  148. package/src/test/mock_l1_to_l2_message_source.ts +19 -11
  149. package/src/test/mock_l2_block_source.ts +346 -93
  150. package/src/test/mock_structs.ts +289 -13
  151. package/src/test/noop_l1_archiver.ts +114 -0
  152. package/dest/archiver/archiver.d.ts +0 -287
  153. package/dest/archiver/archiver.d.ts.map +0 -1
  154. package/dest/archiver/archiver.js +0 -1408
  155. package/dest/archiver/archiver_store.d.ts +0 -255
  156. package/dest/archiver/archiver_store.d.ts.map +0 -1
  157. package/dest/archiver/archiver_store.js +0 -4
  158. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  159. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  160. package/dest/archiver/archiver_store_test_suite.js +0 -1289
  161. package/dest/archiver/config.d.ts +0 -21
  162. package/dest/archiver/config.d.ts.map +0 -1
  163. package/dest/archiver/data_retrieval.d.ts +0 -79
  164. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  165. package/dest/archiver/errors.d.ts +0 -12
  166. package/dest/archiver/errors.d.ts.map +0 -1
  167. package/dest/archiver/errors.js +0 -17
  168. package/dest/archiver/index.d.ts +0 -7
  169. package/dest/archiver/index.d.ts.map +0 -1
  170. package/dest/archiver/index.js +0 -4
  171. package/dest/archiver/instrumentation.d.ts +0 -35
  172. package/dest/archiver/instrumentation.d.ts.map +0 -1
  173. package/dest/archiver/instrumentation.js +0 -140
  174. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -124
  175. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  176. package/dest/archiver/kv_archiver_store/block_store.js +0 -370
  177. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  178. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  179. package/dest/archiver/kv_archiver_store/contract_class_store.js +0 -120
  180. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  181. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  182. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -168
  183. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  184. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -296
  185. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
  186. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  187. package/dest/archiver/kv_archiver_store/log_store.js +0 -336
  188. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -39
  189. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  190. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  191. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  192. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  193. package/dest/archiver/structs/published.d.ts +0 -3
  194. package/dest/archiver/structs/published.d.ts.map +0 -1
  195. package/dest/archiver/validation.d.ts +0 -17
  196. package/dest/archiver/validation.d.ts.map +0 -1
  197. package/dest/rpc/index.d.ts +0 -9
  198. package/dest/rpc/index.d.ts.map +0 -1
  199. package/dest/rpc/index.js +0 -15
  200. package/src/archiver/archiver.ts +0 -1858
  201. package/src/archiver/archiver_store.ts +0 -305
  202. package/src/archiver/archiver_store_test_suite.ts +0 -1264
  203. package/src/archiver/errors.ts +0 -26
  204. package/src/archiver/index.ts +0 -6
  205. package/src/archiver/instrumentation.ts +0 -187
  206. package/src/archiver/kv_archiver_store/block_store.ts +0 -481
  207. package/src/archiver/kv_archiver_store/contract_class_store.ts +0 -176
  208. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -422
  209. package/src/archiver/kv_archiver_store/log_store.ts +0 -406
  210. package/src/rpc/index.ts +0 -16
  211. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  212. /package/dest/{archiver/structs → structs}/published.js +0 -0
  213. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
@@ -1,3 +1,4 @@
1
+ import type { BlobClientInterface } from '@aztec/blob-client/client';
1
2
  import {
2
3
  BlobDeserializationError,
3
4
  type CheckpointBlobData,
@@ -5,46 +6,39 @@ import {
5
6
  decodeCheckpointBlobDataFromBlobs,
6
7
  encodeBlockBlobData,
7
8
  } from '@aztec/blob-lib';
8
- import type { BlobSinkClientInterface } from '@aztec/blob-sink/client';
9
9
  import type {
10
+ CheckpointProposedLog,
10
11
  EpochProofPublicInputArgs,
11
- ViemClient,
12
- ViemCommitteeAttestations,
13
- ViemHeader,
14
- ViemPublicClient,
15
- } from '@aztec/ethereum';
12
+ InboxContract,
13
+ MessageSentLog,
14
+ RollupContract,
15
+ } from '@aztec/ethereum/contracts';
16
+ import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
16
17
  import { asyncPool } from '@aztec/foundation/async-pool';
17
- import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
18
- import type { EthAddress } from '@aztec/foundation/eth-address';
19
- import type { ViemSignature } from '@aztec/foundation/eth-signature';
20
- import { Fr } from '@aztec/foundation/fields';
18
+ import { CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
19
+ import { Fr } from '@aztec/foundation/curves/bn254';
20
+ import { EthAddress } from '@aztec/foundation/eth-address';
21
21
  import { type Logger, createLogger } from '@aztec/foundation/log';
22
- import { type InboxAbi, RollupAbi } from '@aztec/l1-artifacts';
23
- import { Body, CommitteeAttestation, L2BlockNew } from '@aztec/stdlib/block';
24
- import { Checkpoint, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
22
+ import { RollupAbi } from '@aztec/l1-artifacts';
23
+ import { Body, CommitteeAttestation, L2Block } from '@aztec/stdlib/block';
24
+ import { Checkpoint, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
25
25
  import { Proof } from '@aztec/stdlib/proofs';
26
26
  import { CheckpointHeader } from '@aztec/stdlib/rollup';
27
27
  import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
28
28
  import { BlockHeader, GlobalVariables, PartialStateReference, StateReference } from '@aztec/stdlib/tx';
29
29
 
30
- import {
31
- type GetContractEventsReturnType,
32
- type GetContractReturnType,
33
- type Hex,
34
- decodeFunctionData,
35
- getAbiItem,
36
- hexToBytes,
37
- multicall3Abi,
38
- } from 'viem';
39
-
40
- import { NoBlobBodiesFoundError } from './errors.js';
41
- import type { DataRetrieval } from './structs/data_retrieval.js';
42
- import type { InboxMessage } from './structs/inbox_message.js';
43
- import type { L1PublishedData } from './structs/published.js';
30
+ import { type Hex, decodeFunctionData, getAbiItem, hexToBytes } from 'viem';
31
+
32
+ import { NoBlobBodiesFoundError } from '../errors.js';
33
+ import type { ArchiverInstrumentation } from '../modules/instrumentation.js';
34
+ import type { DataRetrieval } from '../structs/data_retrieval.js';
35
+ import type { InboxMessage } from '../structs/inbox_message.js';
36
+ import { CalldataRetriever } from './calldata_retriever.js';
44
37
 
45
38
  export type RetrievedCheckpoint = {
46
- checkpointNumber: number;
39
+ checkpointNumber: CheckpointNumber;
47
40
  archiveRoot: Fr;
41
+ feeAssetPriceModifier: bigint;
48
42
  header: CheckpointHeader;
49
43
  checkpointBlobData: CheckpointBlobData;
50
44
  l1: L1PublishedData;
@@ -56,6 +50,7 @@ export type RetrievedCheckpoint = {
56
50
  export async function retrievedToPublishedCheckpoint({
57
51
  checkpointNumber,
58
52
  archiveRoot,
53
+ feeAssetPriceModifier,
59
54
  header: checkpointHeader,
60
55
  checkpointBlobData,
61
56
  l1,
@@ -71,12 +66,12 @@ export async function retrievedToPublishedCheckpoint({
71
66
  .slice(1)
72
67
  .concat([archiveRoot]);
73
68
 
74
- // `blocksBlobData` is created from `decodeCheckpointBlobDataFromBlobs`. An error will be thrown if it can't read a
75
- // field for the `l1ToL2MessageRoot` of the first block. So below we can safely assume it exists:
69
+ // An error will be thrown from `decodeCheckpointBlobDataFromBlobs` if it can't read a field for the
70
+ // `l1ToL2MessageRoot` of the first block. So below we can safely assume it exists:
76
71
  const l1toL2MessageTreeRoot = blocksBlobData[0].l1ToL2MessageRoot!;
77
72
 
78
73
  const spongeBlob = SpongeBlob.init();
79
- const l2Blocks: L2BlockNew[] = [];
74
+ const l2Blocks: L2Block[] = [];
80
75
  for (let i = 0; i < blocksBlobData.length; i++) {
81
76
  const blockBlobData = blocksBlobData[i];
82
77
  const { blockEndMarker, blockEndStateField, lastArchiveRoot, noteHashRoot, nullifierRoot, publicDataRoot } =
@@ -107,7 +102,7 @@ export async function retrievedToPublishedCheckpoint({
107
102
  }),
108
103
  });
109
104
 
110
- const body = Body.fromTxBlobData(checkpointBlobData.blocks[0].txs);
105
+ const body = Body.fromTxBlobData(blockBlobData.txs);
111
106
 
112
107
  const blobFields = encodeBlockBlobData(blockBlobData);
113
108
  await spongeBlob.absorb(blobFields);
@@ -126,7 +121,7 @@ export async function retrievedToPublishedCheckpoint({
126
121
 
127
122
  const newArchive = new AppendOnlyTreeSnapshot(newArchiveRoots[i], l2BlockNumber + 1);
128
123
 
129
- l2Blocks.push(new L2BlockNew(newArchive, header, body));
124
+ l2Blocks.push(new L2Block(newArchive, header, body, checkpointNumber, IndexWithinCheckpoint(i)));
130
125
  }
131
126
 
132
127
  const lastBlock = l2Blocks.at(-1)!;
@@ -135,6 +130,7 @@ export async function retrievedToPublishedCheckpoint({
135
130
  header: checkpointHeader,
136
131
  blocks: l2Blocks,
137
132
  number: checkpointNumber,
133
+ feeAssetPriceModifier: feeAssetPriceModifier,
138
134
  });
139
135
 
140
136
  return PublishedCheckpoint.from({ checkpoint, l1, attestations });
@@ -142,20 +138,28 @@ export async function retrievedToPublishedCheckpoint({
142
138
 
143
139
  /**
144
140
  * Fetches new checkpoints.
141
+ * @param rollup - The rollup contract wrapper.
145
142
  * @param publicClient - The viem public client to use for transaction retrieval.
146
- * @param rollupAddress - The address of the rollup contract.
143
+ * @param debugClient - The viem debug client to use for trace/debug RPC methods (optional).
144
+ * @param blobClient - The blob client client for fetching blob data.
147
145
  * @param searchStartBlock - The block number to use for starting the search.
148
146
  * @param searchEndBlock - The highest block number that we should search up to.
149
- * @param expectedNextL2BlockNum - The next L2 block number that we expect to find.
150
- * @returns An array of block; as well as the next eth block to search from.
147
+ * @param contractAddresses - The contract addresses (governanceProposerAddress, slashingProposerAddress).
148
+ * @param instrumentation - The archiver instrumentation instance.
149
+ * @param logger - The logger instance.
150
+ * @param isHistoricalSync - Whether this is a historical sync.
151
+ * @returns An array of retrieved checkpoints.
151
152
  */
152
153
  export async function retrieveCheckpointsFromRollup(
153
- rollup: GetContractReturnType<typeof RollupAbi, ViemPublicClient>,
154
+ rollup: RollupContract,
154
155
  publicClient: ViemPublicClient,
155
- blobSinkClient: BlobSinkClientInterface,
156
+ debugClient: ViemPublicDebugClient,
157
+ blobClient: BlobClientInterface,
156
158
  searchStartBlock: bigint,
157
159
  searchEndBlock: bigint,
160
+ instrumentation: ArchiverInstrumentation,
158
161
  logger: Logger = createLogger('archiver'),
162
+ isHistoricalSync: boolean = false,
159
163
  ): Promise<RetrievedCheckpoint[]> {
160
164
  const retrievedCheckpoints: RetrievedCheckpoint[] = [];
161
165
 
@@ -165,15 +169,7 @@ export async function retrieveCheckpointsFromRollup(
165
169
  if (searchStartBlock > searchEndBlock) {
166
170
  break;
167
171
  }
168
- const checkpointProposedLogs = (
169
- await rollup.getEvents.CheckpointProposed(
170
- {},
171
- {
172
- fromBlock: searchStartBlock,
173
- toBlock: searchEndBlock,
174
- },
175
- )
176
- ).filter(log => log.blockNumber! >= searchStartBlock && log.blockNumber! <= searchEndBlock);
172
+ const checkpointProposedLogs = await rollup.getCheckpointProposedEvents(searchStartBlock, searchEndBlock);
177
173
 
178
174
  if (checkpointProposedLogs.length === 0) {
179
175
  break;
@@ -181,32 +177,35 @@ export async function retrieveCheckpointsFromRollup(
181
177
 
182
178
  const lastLog = checkpointProposedLogs.at(-1)!;
183
179
  logger.debug(
184
- `Got ${checkpointProposedLogs.length} processed logs for checkpoints ${checkpointProposedLogs[0].args.checkpointNumber}-${lastLog.args.checkpointNumber} between L1 blocks ${searchStartBlock}-${searchEndBlock}`,
180
+ `Got ${checkpointProposedLogs.length} processed logs for checkpoints ${checkpointProposedLogs[0].args.checkpointNumber}-${lastLog.args.checkpointNumber} between L1 blocks ${searchStartBlock}-${searchEndBlock}`,
185
181
  );
186
182
 
187
183
  if (rollupConstants === undefined) {
188
184
  const [chainId, version, targetCommitteeSize] = await Promise.all([
189
185
  publicClient.getChainId(),
190
- rollup.read.getVersion(),
191
- rollup.read.getTargetCommitteeSize(),
186
+ rollup.getVersion(),
187
+ rollup.getTargetCommitteeSize(),
192
188
  ]);
193
189
  rollupConstants = {
194
190
  chainId: new Fr(chainId),
195
191
  version: new Fr(version),
196
- targetCommitteeSize: Number(targetCommitteeSize),
192
+ targetCommitteeSize,
197
193
  };
198
194
  }
199
195
 
200
196
  const newCheckpoints = await processCheckpointProposedLogs(
201
197
  rollup,
202
198
  publicClient,
203
- blobSinkClient,
199
+ debugClient,
200
+ blobClient,
204
201
  checkpointProposedLogs,
205
202
  rollupConstants,
203
+ instrumentation,
206
204
  logger,
205
+ isHistoricalSync,
207
206
  );
208
207
  retrievedCheckpoints.push(...newCheckpoints);
209
- searchStartBlock = lastLog.blockNumber! + 1n;
208
+ searchStartBlock = lastLog.l1BlockNumber + 1n;
210
209
  } while (searchStartBlock <= searchEndBlock);
211
210
 
212
211
  // The asyncPool from processCheckpointProposedLogs will not necessarily return the checkpoints in order, so we sort them before returning.
@@ -215,56 +214,82 @@ export async function retrieveCheckpointsFromRollup(
215
214
 
216
215
  /**
217
216
  * Processes newly received CheckpointProposed logs.
218
- * @param rollup - The rollup contract
217
+ * @param rollup - The rollup contract wrapper.
219
218
  * @param publicClient - The viem public client to use for transaction retrieval.
219
+ * @param debugClient - The viem debug client to use for trace/debug RPC methods (optional).
220
+ * @param blobClient - The blob client client for fetching blob data.
220
221
  * @param logs - CheckpointProposed logs.
221
- * @returns - An array of checkpoints.
222
+ * @param rollupConstants - The rollup constants (chainId, version, targetCommitteeSize).
223
+ * @param instrumentation - The archiver instrumentation instance.
224
+ * @param logger - The logger instance.
225
+ * @param isHistoricalSync - Whether this is a historical sync.
226
+ * @returns An array of retrieved checkpoints.
222
227
  */
223
228
  async function processCheckpointProposedLogs(
224
- rollup: GetContractReturnType<typeof RollupAbi, ViemPublicClient>,
229
+ rollup: RollupContract,
225
230
  publicClient: ViemPublicClient,
226
- blobSinkClient: BlobSinkClientInterface,
227
- logs: GetContractEventsReturnType<typeof RollupAbi, 'CheckpointProposed'>,
231
+ debugClient: ViemPublicDebugClient,
232
+ blobClient: BlobClientInterface,
233
+ logs: CheckpointProposedLog[],
228
234
  { chainId, version, targetCommitteeSize }: { chainId: Fr; version: Fr; targetCommitteeSize: number },
235
+ instrumentation: ArchiverInstrumentation,
229
236
  logger: Logger,
237
+ isHistoricalSync: boolean,
230
238
  ): Promise<RetrievedCheckpoint[]> {
231
239
  const retrievedCheckpoints: RetrievedCheckpoint[] = [];
240
+ const calldataRetriever = new CalldataRetriever(
241
+ publicClient,
242
+ debugClient,
243
+ targetCommitteeSize,
244
+ instrumentation,
245
+ logger,
246
+ EthAddress.fromString(rollup.address),
247
+ );
248
+
232
249
  await asyncPool(10, logs, async log => {
233
- const checkpointNumber = Number(log.args.checkpointNumber!);
234
- const archive = log.args.archive!;
235
- const archiveFromChain = await rollup.read.archiveAt([BigInt(checkpointNumber)]);
236
- const blobHashes = log.args.versionedBlobHashes!.map(blobHash => Buffer.from(blobHash.slice(2), 'hex'));
250
+ const checkpointNumber = log.args.checkpointNumber;
251
+ const archive = log.args.archive;
252
+ const archiveFromChain = await rollup.archiveAt(checkpointNumber);
253
+ const blobHashes = log.args.versionedBlobHashes;
237
254
 
238
255
  // The value from the event and contract will match only if the checkpoint is in the chain.
239
- if (archive === archiveFromChain) {
240
- const checkpoint = await getCheckpointFromRollupTx(
241
- publicClient,
242
- blobSinkClient,
243
- log.transactionHash!,
256
+ if (archive.equals(archiveFromChain)) {
257
+ const expectedHashes = {
258
+ attestationsHash: log.args.attestationsHash.toString() as Hex,
259
+ payloadDigest: log.args.payloadDigest.toString() as Hex,
260
+ };
261
+
262
+ const checkpoint = await calldataRetriever.getCheckpointFromRollupTx(
263
+ log.l1TransactionHash,
244
264
  blobHashes,
245
265
  checkpointNumber,
246
- rollup.address,
247
- targetCommitteeSize,
248
- logger,
266
+ expectedHashes,
249
267
  );
268
+ const { timestamp, parentBeaconBlockRoot } = await getL1Block(publicClient, log.l1BlockNumber);
269
+ const l1 = new L1PublishedData(log.l1BlockNumber, timestamp, log.l1BlockHash.toString());
250
270
 
251
- const l1: L1PublishedData = {
252
- blockNumber: log.blockNumber,
253
- blockHash: log.blockHash,
254
- timestamp: await getL1BlockTime(publicClient, log.blockNumber),
255
- };
271
+ const checkpointBlobData = await getCheckpointBlobDataFromBlobs(
272
+ blobClient,
273
+ checkpoint.blockHash,
274
+ blobHashes,
275
+ checkpointNumber,
276
+ logger,
277
+ isHistoricalSync,
278
+ parentBeaconBlockRoot,
279
+ timestamp,
280
+ );
256
281
 
257
- retrievedCheckpoints.push({ ...checkpoint, l1, chainId, version });
258
- logger.trace(`Retrieved checkpoint ${checkpointNumber} from L1 tx ${log.transactionHash}`, {
259
- l1BlockNumber: log.blockNumber,
282
+ retrievedCheckpoints.push({ ...checkpoint, checkpointBlobData, l1, chainId, version });
283
+ logger.trace(`Retrieved checkpoint ${checkpointNumber} from L1 tx ${log.l1TransactionHash}`, {
284
+ l1BlockNumber: log.l1BlockNumber,
260
285
  checkpointNumber,
261
286
  archive: archive.toString(),
262
287
  attestations: checkpoint.attestations,
263
288
  });
264
289
  } else {
265
290
  logger.warn(`Ignoring checkpoint ${checkpointNumber} due to archive root mismatch`, {
266
- actual: archive,
267
- expected: archiveFromChain,
291
+ actual: archive.toString(),
292
+ expected: archiveFromChain.toString(),
268
293
  });
269
294
  }
270
295
  });
@@ -272,123 +297,29 @@ async function processCheckpointProposedLogs(
272
297
  return retrievedCheckpoints;
273
298
  }
274
299
 
275
- export async function getL1BlockTime(publicClient: ViemPublicClient, blockNumber: bigint): Promise<bigint> {
300
+ export async function getL1Block(
301
+ publicClient: ViemPublicClient,
302
+ blockNumber: bigint,
303
+ ): Promise<{ timestamp: bigint; parentBeaconBlockRoot: string | undefined }> {
276
304
  const block = await publicClient.getBlock({ blockNumber, includeTransactions: false });
277
- return block.timestamp;
305
+ return { timestamp: block.timestamp, parentBeaconBlockRoot: block.parentBeaconBlockRoot };
278
306
  }
279
307
 
280
- /**
281
- * Extracts the first 'propose' method calldata from a multicall3 transaction's data.
282
- * @param multicall3Data - The multicall3 transaction input data
283
- * @param rollupAddress - The address of the rollup contract
284
- * @returns The calldata for the first 'propose' method call to the rollup contract
285
- */
286
- function extractRollupProposeCalldata(multicall3Data: Hex, rollupAddress: Hex): Hex {
287
- const { functionName: multicall3FunctionName, args: multicall3Args } = decodeFunctionData({
288
- abi: multicall3Abi,
289
- data: multicall3Data,
290
- });
291
-
292
- if (multicall3FunctionName !== 'aggregate3') {
293
- throw new Error(`Unexpected multicall3 method called ${multicall3FunctionName}`);
294
- }
295
-
296
- if (multicall3Args.length !== 1) {
297
- throw new Error(`Unexpected number of arguments for multicall3`);
298
- }
299
-
300
- const [calls] = multicall3Args;
301
-
302
- // Find all rollup calls
303
- const rollupAddressLower = rollupAddress.toLowerCase();
304
-
305
- for (let i = 0; i < calls.length; i++) {
306
- const addr = calls[i].target;
307
- if (addr.toLowerCase() !== rollupAddressLower) {
308
- continue;
309
- }
310
- const callData = calls[i].callData;
311
-
312
- try {
313
- const { functionName: rollupFunctionName } = decodeFunctionData({
314
- abi: RollupAbi,
315
- data: callData,
316
- });
317
-
318
- if (rollupFunctionName === 'propose') {
319
- return callData;
320
- }
321
- } catch {
322
- // Skip invalid function data
323
- continue;
324
- }
325
- }
326
-
327
- throw new Error(`Rollup address not found in multicall3 args`);
328
- }
329
-
330
- /**
331
- * Gets checkpoint from the calldata of an L1 transaction.
332
- * Assumes that the checkpoint was published from an EOA.
333
- * TODO: Add retries and error management.
334
- * @param publicClient - The viem public client to use for transaction retrieval.
335
- * @param txHash - Hash of the tx that published it.
336
- * @param checkpointNumber - Checkpoint number.
337
- * @returns Checkpoint from the calldata, deserialized
338
- */
339
- async function getCheckpointFromRollupTx(
340
- publicClient: ViemPublicClient,
341
- blobSinkClient: BlobSinkClientInterface,
342
- txHash: `0x${string}`,
343
- blobHashes: Buffer[], // TODO(md): buffer32?
344
- checkpointNumber: number,
345
- rollupAddress: Hex,
346
- targetCommitteeSize: number,
308
+ export async function getCheckpointBlobDataFromBlobs(
309
+ blobClient: BlobClientInterface,
310
+ blockHash: string,
311
+ blobHashes: Buffer<ArrayBufferLike>[],
312
+ checkpointNumber: CheckpointNumber,
347
313
  logger: Logger,
348
- ): Promise<Omit<RetrievedCheckpoint, 'l1' | 'chainId' | 'version'>> {
349
- logger.trace(`Fetching checkpoint ${checkpointNumber} from rollup tx ${txHash}`);
350
- const { input: forwarderData, blockHash } = await publicClient.getTransaction({ hash: txHash });
351
-
352
- const rollupData = extractRollupProposeCalldata(forwarderData, rollupAddress);
353
- const { functionName: rollupFunctionName, args: rollupArgs } = decodeFunctionData({
354
- abi: RollupAbi,
355
- data: rollupData,
356
- });
357
-
358
- if (rollupFunctionName !== 'propose') {
359
- throw new Error(`Unexpected rollup method called ${rollupFunctionName}`);
360
- }
361
-
362
- const [decodedArgs, packedAttestations, _signers, _blobInput] = rollupArgs! as readonly [
363
- {
364
- archive: Hex;
365
- oracleInput: {
366
- feeAssetPriceModifier: bigint;
367
- };
368
- header: ViemHeader;
369
- txHashes: readonly Hex[];
370
- },
371
- ViemCommitteeAttestations,
372
- Hex[],
373
- ViemSignature,
374
- Hex,
375
- ];
376
-
377
- const attestations = CommitteeAttestation.fromPacked(packedAttestations, targetCommitteeSize);
378
-
379
- logger.trace(`Recovered propose calldata from tx ${txHash}`, {
380
- checkpointNumber,
381
- archive: decodedArgs.archive,
382
- header: decodedArgs.header,
383
- l1BlockHash: blockHash,
384
- blobHashes,
385
- attestations,
386
- packedAttestations,
387
- targetCommitteeSize,
314
+ isHistoricalSync: boolean,
315
+ parentBeaconBlockRoot?: string,
316
+ l1BlockTimestamp?: bigint,
317
+ ): Promise<CheckpointBlobData> {
318
+ const blobBodies = await blobClient.getBlobSidecar(blockHash, blobHashes, {
319
+ isHistoricalSync,
320
+ parentBeaconBlockRoot,
321
+ l1BlockTimestamp,
388
322
  });
389
-
390
- const header = CheckpointHeader.fromViem(decodedArgs.header);
391
- const blobBodies = await blobSinkClient.getBlobSidecar(blockHash, blobHashes);
392
323
  if (blobBodies.length === 0) {
393
324
  throw new NoBlobBodiesFoundError(checkpointNumber);
394
325
  }
@@ -396,83 +327,65 @@ async function getCheckpointFromRollupTx(
396
327
  let checkpointBlobData: CheckpointBlobData;
397
328
  try {
398
329
  // Attempt to decode the checkpoint blob data.
399
- checkpointBlobData = decodeCheckpointBlobDataFromBlobs(blobBodies.map(b => b.blob));
330
+ checkpointBlobData = decodeCheckpointBlobDataFromBlobs(blobBodies);
400
331
  } catch (err: any) {
401
332
  if (err instanceof BlobDeserializationError) {
402
333
  logger.fatal(err.message);
403
334
  } else {
404
335
  logger.fatal('Unable to sync: failed to decode fetched blob, this blob was likely not created by us');
405
336
  }
337
+ // Throwing an error since this is most likely caused by a bug.
406
338
  throw err;
407
339
  }
408
340
 
409
- const archiveRoot = new Fr(Buffer.from(hexToBytes(decodedArgs.archive)));
410
-
411
- return {
412
- checkpointNumber,
413
- archiveRoot,
414
- header,
415
- checkpointBlobData,
416
- attestations,
417
- };
341
+ return checkpointBlobData;
418
342
  }
419
343
 
420
344
  /** Given an L1 to L2 message, retrieves its corresponding event from the Inbox within a specific block range. */
421
345
  export async function retrieveL1ToL2Message(
422
- inbox: GetContractReturnType<typeof InboxAbi, ViemClient>,
423
- leaf: Fr,
424
- fromBlock: bigint,
425
- toBlock: bigint,
346
+ inbox: InboxContract,
347
+ message: InboxMessage,
426
348
  ): Promise<InboxMessage | undefined> {
427
- const logs = await inbox.getEvents.MessageSent({ hash: leaf.toString() }, { fromBlock, toBlock });
428
-
429
- const messages = mapLogsInboxMessage(logs);
430
- return messages.length > 0 ? messages[0] : undefined;
349
+ const log = await inbox.getMessageSentEventByHash(message.leaf.toString(), message.l1BlockHash.toString());
350
+ return log && mapLogInboxMessage(log);
431
351
  }
432
352
 
433
353
  /**
434
354
  * Fetch L1 to L2 messages.
435
- * @param publicClient - The viem public client to use for transaction retrieval.
436
- * @param inboxAddress - The address of the inbox contract to fetch messages from.
437
- * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
355
+ * @param inbox - The inbox contract wrapper.
438
356
  * @param searchStartBlock - The block number to use for starting the search.
439
357
  * @param searchEndBlock - The highest block number that we should search up to.
440
358
  * @returns An array of InboxLeaf and next eth block to search from.
441
359
  */
442
360
  export async function retrieveL1ToL2Messages(
443
- inbox: GetContractReturnType<typeof InboxAbi, ViemClient>,
361
+ inbox: InboxContract,
444
362
  searchStartBlock: bigint,
445
363
  searchEndBlock: bigint,
446
364
  ): Promise<InboxMessage[]> {
447
365
  const retrievedL1ToL2Messages: InboxMessage[] = [];
448
366
  while (searchStartBlock <= searchEndBlock) {
449
- const messageSentLogs = (
450
- await inbox.getEvents.MessageSent({}, { fromBlock: searchStartBlock, toBlock: searchEndBlock })
451
- ).filter(log => log.blockNumber! >= searchStartBlock && log.blockNumber! <= searchEndBlock);
367
+ const messageSentLogs = await inbox.getMessageSentEvents(searchStartBlock, searchEndBlock);
452
368
 
453
369
  if (messageSentLogs.length === 0) {
454
370
  break;
455
371
  }
456
372
 
457
- retrievedL1ToL2Messages.push(...mapLogsInboxMessage(messageSentLogs));
458
- searchStartBlock = messageSentLogs.at(-1)!.blockNumber + 1n;
373
+ retrievedL1ToL2Messages.push(...messageSentLogs.map(mapLogInboxMessage));
374
+ searchStartBlock = messageSentLogs.at(-1)!.l1BlockNumber + 1n;
459
375
  }
460
376
 
461
377
  return retrievedL1ToL2Messages;
462
378
  }
463
379
 
464
- function mapLogsInboxMessage(logs: GetContractEventsReturnType<typeof InboxAbi, 'MessageSent'>): InboxMessage[] {
465
- return logs.map(log => {
466
- const { index, hash, checkpointNumber, rollingHash } = log.args;
467
- return {
468
- index: index!,
469
- leaf: Fr.fromHexString(hash!),
470
- l1BlockNumber: log.blockNumber,
471
- l1BlockHash: Buffer32.fromString(log.blockHash),
472
- l2BlockNumber: Number(checkpointNumber!),
473
- rollingHash: Buffer16.fromString(rollingHash!),
474
- };
475
- });
380
+ function mapLogInboxMessage(log: MessageSentLog): InboxMessage {
381
+ return {
382
+ index: log.args.index,
383
+ leaf: log.args.leaf,
384
+ l1BlockNumber: log.l1BlockNumber,
385
+ l1BlockHash: log.l1BlockHash,
386
+ checkpointNumber: log.args.checkpointNumber,
387
+ rollingHash: log.args.rollingHash,
388
+ };
476
389
  }
477
390
 
478
391
  /** Retrieves L2ProofVerified events from the rollup contract. */
@@ -481,7 +394,7 @@ export async function retrieveL2ProofVerifiedEvents(
481
394
  rollupAddress: EthAddress,
482
395
  searchStartBlock: bigint,
483
396
  searchEndBlock?: bigint,
484
- ): Promise<{ l1BlockNumber: bigint; checkpointNumber: number; proverId: Fr; txHash: Hex }[]> {
397
+ ): Promise<{ l1BlockNumber: bigint; checkpointNumber: CheckpointNumber; proverId: Fr; txHash: Hex }[]> {
485
398
  const logs = await publicClient.getLogs({
486
399
  address: rollupAddress.toString(),
487
400
  fromBlock: searchStartBlock,
@@ -492,7 +405,7 @@ export async function retrieveL2ProofVerifiedEvents(
492
405
 
493
406
  return logs.map(log => ({
494
407
  l1BlockNumber: log.blockNumber,
495
- checkpointNumber: Number(log.args.checkpointNumber),
408
+ checkpointNumber: CheckpointNumber.fromBigInt(log.args.checkpointNumber),
496
409
  proverId: Fr.fromHexString(log.args.proverId),
497
410
  txHash: log.transactionHash,
498
411
  }));