@aztec/archiver 0.0.1-commit.96bb3f7 → 0.0.1-commit.993d240

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (232) hide show
  1. package/README.md +164 -22
  2. package/dest/archiver.d.ts +158 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +881 -0
  5. package/dest/config.d.ts +33 -0
  6. package/dest/config.d.ts.map +1 -0
  7. package/dest/{archiver/config.js → config.js} +31 -14
  8. package/dest/errors.d.ts +87 -0
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/errors.js +129 -0
  11. package/dest/factory.d.ts +16 -10
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +112 -20
  14. package/dest/index.d.ts +19 -4
  15. package/dest/index.d.ts.map +1 -1
  16. package/dest/index.js +17 -3
  17. package/dest/interfaces.d.ts +9 -0
  18. package/dest/interfaces.d.ts.map +1 -0
  19. package/dest/interfaces.js +3 -0
  20. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.d.ts +1 -1
  21. package/dest/l1/bin/retrieve-calldata.d.ts.map +1 -0
  22. package/dest/{archiver/l1 → l1}/bin/retrieve-calldata.js +35 -32
  23. package/dest/l1/calldata_retriever.d.ts +136 -0
  24. package/dest/l1/calldata_retriever.d.ts.map +1 -0
  25. package/dest/l1/calldata_retriever.js +412 -0
  26. package/dest/l1/data_retrieval.d.ts +97 -0
  27. package/dest/l1/data_retrieval.d.ts.map +1 -0
  28. package/dest/{archiver/l1 → l1}/data_retrieval.js +65 -89
  29. package/dest/{archiver/l1 → l1}/debug_tx.d.ts +1 -1
  30. package/dest/l1/debug_tx.d.ts.map +1 -0
  31. package/dest/{archiver/l1 → l1}/spire_proposer.d.ts +5 -5
  32. package/dest/l1/spire_proposer.d.ts.map +1 -0
  33. package/dest/{archiver/l1 → l1}/spire_proposer.js +9 -17
  34. package/dest/l1/trace_tx.d.ts +43 -0
  35. package/dest/l1/trace_tx.d.ts.map +1 -0
  36. package/dest/l1/types.d.ts +12 -0
  37. package/dest/l1/types.d.ts.map +1 -0
  38. package/dest/l1/validate_historical_logs.d.ts +23 -0
  39. package/dest/l1/validate_historical_logs.d.ts.map +1 -0
  40. package/dest/l1/validate_historical_logs.js +108 -0
  41. package/dest/{archiver/l1 → l1}/validate_trace.d.ts +6 -3
  42. package/dest/l1/validate_trace.d.ts.map +1 -0
  43. package/dest/{archiver/l1 → l1}/validate_trace.js +13 -9
  44. package/dest/modules/contract_data_source_adapter.d.ts +25 -0
  45. package/dest/modules/contract_data_source_adapter.d.ts.map +1 -0
  46. package/dest/modules/contract_data_source_adapter.js +40 -0
  47. package/dest/modules/data_source_base.d.ts +113 -0
  48. package/dest/modules/data_source_base.d.ts.map +1 -0
  49. package/dest/modules/data_source_base.js +351 -0
  50. package/dest/modules/data_store_updater.d.ts +105 -0
  51. package/dest/modules/data_store_updater.d.ts.map +1 -0
  52. package/dest/modules/data_store_updater.js +392 -0
  53. package/dest/modules/instrumentation.d.ts +55 -0
  54. package/dest/modules/instrumentation.d.ts.map +1 -0
  55. package/dest/{archiver → modules}/instrumentation.js +61 -19
  56. package/dest/modules/l1_synchronizer.d.ts +77 -0
  57. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  58. package/dest/modules/l1_synchronizer.js +1344 -0
  59. package/dest/modules/validation.d.ts +18 -0
  60. package/dest/modules/validation.d.ts.map +1 -0
  61. package/dest/{archiver → modules}/validation.js +12 -6
  62. package/dest/store/block_store.d.ts +300 -0
  63. package/dest/store/block_store.d.ts.map +1 -0
  64. package/dest/store/block_store.js +1219 -0
  65. package/dest/store/contract_class_store.d.ts +31 -0
  66. package/dest/store/contract_class_store.d.ts.map +1 -0
  67. package/dest/store/contract_class_store.js +80 -0
  68. package/dest/store/contract_instance_store.d.ts +51 -0
  69. package/dest/store/contract_instance_store.d.ts.map +1 -0
  70. package/dest/{archiver/kv_archiver_store → store}/contract_instance_store.js +38 -3
  71. package/dest/store/data_stores.d.ts +68 -0
  72. package/dest/store/data_stores.d.ts.map +1 -0
  73. package/dest/store/data_stores.js +54 -0
  74. package/dest/store/function_names_cache.d.ts +17 -0
  75. package/dest/store/function_names_cache.d.ts.map +1 -0
  76. package/dest/store/function_names_cache.js +30 -0
  77. package/dest/store/l2_tips_cache.d.ts +25 -0
  78. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  79. package/dest/store/l2_tips_cache.js +26 -0
  80. package/dest/store/log_store.d.ts +59 -0
  81. package/dest/store/log_store.d.ts.map +1 -0
  82. package/dest/store/log_store.js +310 -0
  83. package/dest/store/log_store_codec.d.ts +70 -0
  84. package/dest/store/log_store_codec.d.ts.map +1 -0
  85. package/dest/store/log_store_codec.js +101 -0
  86. package/dest/store/message_store.d.ts +50 -0
  87. package/dest/store/message_store.d.ts.map +1 -0
  88. package/dest/{archiver/kv_archiver_store → store}/message_store.js +51 -9
  89. package/dest/{archiver/structs → structs}/data_retrieval.d.ts +1 -1
  90. package/dest/structs/data_retrieval.d.ts.map +1 -0
  91. package/dest/structs/inbox_message.d.ts +15 -0
  92. package/dest/structs/inbox_message.d.ts.map +1 -0
  93. package/dest/{archiver/structs → structs}/published.d.ts +1 -1
  94. package/dest/structs/published.d.ts.map +1 -0
  95. package/dest/test/fake_l1_state.d.ts +214 -0
  96. package/dest/test/fake_l1_state.d.ts.map +1 -0
  97. package/dest/test/fake_l1_state.js +517 -0
  98. package/dest/test/index.d.ts +2 -1
  99. package/dest/test/index.d.ts.map +1 -1
  100. package/dest/test/index.js +4 -1
  101. package/dest/test/mock_archiver.d.ts +2 -2
  102. package/dest/test/mock_archiver.d.ts.map +1 -1
  103. package/dest/test/mock_archiver.js +3 -3
  104. package/dest/test/mock_l1_to_l2_message_source.d.ts +1 -1
  105. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  106. package/dest/test/mock_l1_to_l2_message_source.js +2 -1
  107. package/dest/test/mock_l2_block_source.d.ts +65 -41
  108. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  109. package/dest/test/mock_l2_block_source.js +330 -151
  110. package/dest/test/mock_structs.d.ts +81 -3
  111. package/dest/test/mock_structs.d.ts.map +1 -1
  112. package/dest/test/mock_structs.js +152 -7
  113. package/dest/test/noop_l1_archiver.d.ts +29 -0
  114. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  115. package/dest/test/noop_l1_archiver.js +85 -0
  116. package/package.json +17 -18
  117. package/src/archiver.ts +681 -0
  118. package/src/{archiver/config.ts → config.ts} +43 -12
  119. package/src/errors.ts +203 -0
  120. package/src/factory.ts +175 -22
  121. package/src/index.ts +27 -3
  122. package/src/interfaces.ts +9 -0
  123. package/src/l1/README.md +55 -0
  124. package/src/{archiver/l1 → l1}/bin/retrieve-calldata.ts +45 -33
  125. package/src/l1/calldata_retriever.ts +522 -0
  126. package/src/{archiver/l1 → l1}/data_retrieval.ts +106 -134
  127. package/src/{archiver/l1 → l1}/spire_proposer.ts +7 -15
  128. package/src/l1/validate_historical_logs.ts +140 -0
  129. package/src/{archiver/l1 → l1}/validate_trace.ts +24 -6
  130. package/src/modules/contract_data_source_adapter.ts +55 -0
  131. package/src/modules/data_source_base.ts +493 -0
  132. package/src/modules/data_store_updater.ts +518 -0
  133. package/src/{archiver → modules}/instrumentation.ts +72 -20
  134. package/src/modules/l1_synchronizer.ts +1257 -0
  135. package/src/{archiver → modules}/validation.ts +15 -9
  136. package/src/store/block_store.ts +1590 -0
  137. package/src/store/contract_class_store.ts +108 -0
  138. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +52 -6
  139. package/src/store/data_stores.ts +104 -0
  140. package/src/store/function_names_cache.ts +37 -0
  141. package/src/store/l2_tips_cache.ts +35 -0
  142. package/src/store/log_store.ts +379 -0
  143. package/src/store/log_store_codec.ts +132 -0
  144. package/src/{archiver/kv_archiver_store → store}/message_store.ts +60 -10
  145. package/src/{archiver/structs → structs}/inbox_message.ts +1 -1
  146. package/src/test/fake_l1_state.ts +770 -0
  147. package/src/test/index.ts +4 -0
  148. package/src/test/mock_archiver.ts +4 -3
  149. package/src/test/mock_l1_to_l2_message_source.ts +1 -0
  150. package/src/test/mock_l2_block_source.ts +403 -171
  151. package/src/test/mock_structs.ts +283 -8
  152. package/src/test/noop_l1_archiver.ts +139 -0
  153. package/dest/archiver/archiver.d.ts +0 -307
  154. package/dest/archiver/archiver.d.ts.map +0 -1
  155. package/dest/archiver/archiver.js +0 -2102
  156. package/dest/archiver/archiver_store.d.ts +0 -315
  157. package/dest/archiver/archiver_store.d.ts.map +0 -1
  158. package/dest/archiver/archiver_store.js +0 -4
  159. package/dest/archiver/archiver_store_test_suite.d.ts +0 -8
  160. package/dest/archiver/archiver_store_test_suite.d.ts.map +0 -1
  161. package/dest/archiver/archiver_store_test_suite.js +0 -2770
  162. package/dest/archiver/config.d.ts +0 -22
  163. package/dest/archiver/config.d.ts.map +0 -1
  164. package/dest/archiver/errors.d.ts +0 -36
  165. package/dest/archiver/errors.d.ts.map +0 -1
  166. package/dest/archiver/errors.js +0 -54
  167. package/dest/archiver/index.d.ts +0 -7
  168. package/dest/archiver/index.d.ts.map +0 -1
  169. package/dest/archiver/index.js +0 -4
  170. package/dest/archiver/instrumentation.d.ts +0 -37
  171. package/dest/archiver/instrumentation.d.ts.map +0 -1
  172. package/dest/archiver/kv_archiver_store/block_store.d.ts +0 -164
  173. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +0 -1
  174. package/dest/archiver/kv_archiver_store/block_store.js +0 -626
  175. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +0 -18
  176. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +0 -1
  177. package/dest/archiver/kv_archiver_store/contract_class_store.js +0 -120
  178. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +0 -24
  179. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  180. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -159
  181. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  182. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -316
  183. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -45
  184. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  185. package/dest/archiver/kv_archiver_store/log_store.js +0 -401
  186. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -40
  187. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  188. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +0 -1
  189. package/dest/archiver/l1/calldata_retriever.d.ts +0 -112
  190. package/dest/archiver/l1/calldata_retriever.d.ts.map +0 -1
  191. package/dest/archiver/l1/calldata_retriever.js +0 -471
  192. package/dest/archiver/l1/data_retrieval.d.ts +0 -90
  193. package/dest/archiver/l1/data_retrieval.d.ts.map +0 -1
  194. package/dest/archiver/l1/debug_tx.d.ts.map +0 -1
  195. package/dest/archiver/l1/spire_proposer.d.ts.map +0 -1
  196. package/dest/archiver/l1/trace_tx.d.ts +0 -97
  197. package/dest/archiver/l1/trace_tx.d.ts.map +0 -1
  198. package/dest/archiver/l1/types.d.ts +0 -12
  199. package/dest/archiver/l1/types.d.ts.map +0 -1
  200. package/dest/archiver/l1/validate_trace.d.ts.map +0 -1
  201. package/dest/archiver/structs/data_retrieval.d.ts.map +0 -1
  202. package/dest/archiver/structs/inbox_message.d.ts +0 -15
  203. package/dest/archiver/structs/inbox_message.d.ts.map +0 -1
  204. package/dest/archiver/structs/published.d.ts.map +0 -1
  205. package/dest/archiver/validation.d.ts +0 -17
  206. package/dest/archiver/validation.d.ts.map +0 -1
  207. package/dest/rpc/index.d.ts +0 -9
  208. package/dest/rpc/index.d.ts.map +0 -1
  209. package/dest/rpc/index.js +0 -15
  210. package/src/archiver/archiver.ts +0 -2265
  211. package/src/archiver/archiver_store.ts +0 -380
  212. package/src/archiver/archiver_store_test_suite.ts +0 -2842
  213. package/src/archiver/errors.ts +0 -90
  214. package/src/archiver/index.ts +0 -6
  215. package/src/archiver/kv_archiver_store/block_store.ts +0 -850
  216. package/src/archiver/kv_archiver_store/contract_class_store.ts +0 -176
  217. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -442
  218. package/src/archiver/kv_archiver_store/log_store.ts +0 -516
  219. package/src/archiver/l1/README.md +0 -98
  220. package/src/archiver/l1/calldata_retriever.ts +0 -641
  221. package/src/rpc/index.ts +0 -16
  222. /package/dest/{archiver/l1 → l1}/debug_tx.js +0 -0
  223. /package/dest/{archiver/l1 → l1}/trace_tx.js +0 -0
  224. /package/dest/{archiver/l1 → l1}/types.js +0 -0
  225. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  226. /package/dest/{archiver/structs → structs}/inbox_message.js +0 -0
  227. /package/dest/{archiver/structs → structs}/published.js +0 -0
  228. /package/src/{archiver/l1 → l1}/debug_tx.ts +0 -0
  229. /package/src/{archiver/l1 → l1}/trace_tx.ts +0 -0
  230. /package/src/{archiver/l1 → l1}/types.ts +0 -0
  231. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
  232. /package/src/{archiver/structs → structs}/published.ts +0 -0
@@ -3,8 +3,9 @@ import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/ty
3
3
  import { CheckpointNumber } from '@aztec/foundation/branded-types';
4
4
  import { EthAddress } from '@aztec/foundation/eth-address';
5
5
  import { createLogger } from '@aztec/foundation/log';
6
+ import { RollupAbi } from '@aztec/l1-artifacts/RollupAbi';
6
7
 
7
- import { type Hex, createPublicClient, http } from 'viem';
8
+ import { type Hex, createPublicClient, decodeEventLog, getAbiItem, http, toEventSelector } from 'viem';
8
9
  import { mainnet } from 'viem/chains';
9
10
 
10
11
  import { CalldataRetriever } from '../calldata_retriever.js';
@@ -88,14 +89,6 @@ async function main() {
88
89
 
89
90
  logger.info(`Transaction found in block ${tx.blockNumber}`);
90
91
 
91
- // For simplicity, use zero addresses for optional contract addresses
92
- // In production, these would be fetched from the rollup contract or configuration
93
- const slashingProposerAddress = EthAddress.ZERO;
94
- const governanceProposerAddress = EthAddress.ZERO;
95
- const slashFactoryAddress = undefined;
96
-
97
- logger.info('Using zero addresses for governance/slashing (can be configured if needed)');
98
-
99
92
  // Create CalldataRetriever
100
93
  const retriever = new CalldataRetriever(
101
94
  publicClient as unknown as ViemPublicClient,
@@ -103,48 +96,67 @@ async function main() {
103
96
  targetCommitteeSize,
104
97
  undefined,
105
98
  logger,
106
- {
107
- rollupAddress,
108
- governanceProposerAddress,
109
- slashingProposerAddress,
110
- slashFactoryAddress,
111
- },
99
+ rollupAddress,
112
100
  );
113
101
 
114
- // Extract L2 block number from transaction logs
115
- logger.info('Decoding transaction to extract L2 block number...');
102
+ // Extract checkpoint number and hashes from transaction logs
103
+ logger.info('Decoding transaction to extract checkpoint number and hashes...');
116
104
  const receipt = await publicClient.getTransactionReceipt({ hash: txHash });
117
- const l2BlockProposedEvent = receipt.logs.find(log => {
105
+
106
+ // Look for CheckpointProposed event
107
+ const checkpointProposedEventAbi = getAbiItem({ abi: RollupAbi, name: 'CheckpointProposed' });
108
+ const checkpointProposedLog = receipt.logs.find(log => {
118
109
  try {
119
- // Try to match the L2BlockProposed event
120
110
  return (
121
111
  log.address.toLowerCase() === rollupAddress.toString().toLowerCase() &&
122
- log.topics[0] === '0x2f1d0e696fa5186494a2f2f89a0e0bcbb15d607f6c5eac4637e07e1e5e7d3c00' // L2BlockProposed event signature
112
+ log.topics[0] === toEventSelector(checkpointProposedEventAbi)
123
113
  );
124
114
  } catch {
125
115
  return false;
126
116
  }
127
117
  });
128
118
 
129
- let l2BlockNumber: number;
130
- if (l2BlockProposedEvent && l2BlockProposedEvent.topics[1]) {
131
- // L2 block number is typically the first indexed parameter
132
- l2BlockNumber = Number(BigInt(l2BlockProposedEvent.topics[1]));
133
- logger.info(`L2 Block Number (from event): ${l2BlockNumber}`);
134
- } else {
135
- // Fallback: try to extract from transaction data or use a default
136
- logger.warn('Could not extract L2 block number from event, using block number as fallback');
137
- l2BlockNumber = Number(tx.blockNumber);
119
+ if (!checkpointProposedLog || checkpointProposedLog.topics[1] === undefined) {
120
+ throw new Error(`Checkpoint proposed event not found`);
138
121
  }
139
122
 
123
+ const checkpointNumber = CheckpointNumber.fromBigInt(BigInt(checkpointProposedLog.topics[1]));
124
+
125
+ // Decode the full event to extract attestationsHash and payloadDigest
126
+ const decodedEvent = decodeEventLog({
127
+ abi: RollupAbi,
128
+ data: checkpointProposedLog.data,
129
+ topics: checkpointProposedLog.topics,
130
+ });
131
+
132
+ const eventArgs = decodedEvent.args as {
133
+ checkpointNumber: bigint;
134
+ archive: Hex;
135
+ versionedBlobHashes: Hex[];
136
+ attestationsHash: Hex;
137
+ payloadDigest: Hex;
138
+ };
139
+
140
+ if (!eventArgs.attestationsHash || !eventArgs.payloadDigest) {
141
+ throw new Error(`CheckpointProposed event missing attestationsHash or payloadDigest`);
142
+ }
143
+
144
+ const expectedHashes = {
145
+ attestationsHash: eventArgs.attestationsHash,
146
+ payloadDigest: eventArgs.payloadDigest,
147
+ };
148
+
149
+ logger.info(`Checkpoint Number: ${checkpointNumber}`);
150
+ logger.info(`Attestations Hash: ${expectedHashes.attestationsHash}`);
151
+ logger.info(`Payload Digest: ${expectedHashes.payloadDigest}`);
152
+
140
153
  logger.info('');
141
- logger.info('Retrieving block header from rollup transaction...');
154
+ logger.info('Retrieving checkpoint from rollup transaction...');
142
155
  logger.info('');
143
156
 
144
- // For this script, we don't have blob hashes or expected hashes, so pass empty arrays/objects
145
- const result = await retriever.getCheckpointFromRollupTx(txHash, [], CheckpointNumber(l2BlockNumber), {});
157
+ const result = await retriever.getCheckpointFromRollupTx(txHash, [], checkpointNumber, expectedHashes);
146
158
 
147
- logger.info(' Successfully retrieved block header!');
159
+ logger.info(' Successfully retrieved block header!');
148
160
  logger.info('');
149
161
  logger.info('Block Header Details:');
150
162
  logger.info('====================');
@@ -0,0 +1,522 @@
1
+ import { MULTI_CALL_3_ADDRESS, type ViemCommitteeAttestations, type ViemHeader } from '@aztec/ethereum/contracts';
2
+ import type { ViemPublicClient, ViemPublicDebugClient } from '@aztec/ethereum/types';
3
+ import { CheckpointNumber } from '@aztec/foundation/branded-types';
4
+ import { LruSet } from '@aztec/foundation/collection';
5
+ import { Fr } from '@aztec/foundation/curves/bn254';
6
+ import { EthAddress } from '@aztec/foundation/eth-address';
7
+ import type { Logger } from '@aztec/foundation/log';
8
+ import { RollupAbi } from '@aztec/l1-artifacts';
9
+ import { CommitteeAttestation } from '@aztec/stdlib/block';
10
+ import { computeCheckpointPayloadDigest } from '@aztec/stdlib/checkpoint';
11
+ import { CheckpointHeader } from '@aztec/stdlib/rollup';
12
+
13
+ import {
14
+ type AbiParameter,
15
+ type Hex,
16
+ type Transaction,
17
+ decodeFunctionData,
18
+ encodeAbiParameters,
19
+ hexToBytes,
20
+ keccak256,
21
+ multicall3Abi,
22
+ toFunctionSelector,
23
+ } from 'viem';
24
+
25
+ import type { ArchiverInstrumentation } from '../modules/instrumentation.js';
26
+ import { getSuccessfulCallsFromDebug } from './debug_tx.js';
27
+ import { getCallsFromSpireProposer } from './spire_proposer.js';
28
+ import { getSuccessfulCallsFromTrace } from './trace_tx.js';
29
+ import type { CallInfo } from './types.js';
30
+
31
+ /** Decoded checkpoint data from a propose calldata. */
32
+ type CheckpointData = {
33
+ checkpointNumber: CheckpointNumber;
34
+ archiveRoot: Fr;
35
+ header: CheckpointHeader;
36
+ attestations: CommitteeAttestation[];
37
+ blockHash: string;
38
+ feeAssetPriceModifier: bigint;
39
+ };
40
+
41
+ /**
42
+ * Extracts calldata to the `propose` method of the rollup contract from an L1 transaction
43
+ * in order to reconstruct an L2 block header. Uses hash matching against expected hashes
44
+ * from the CheckpointProposed event to verify the correct propose calldata.
45
+ */
46
+ export class CalldataRetriever {
47
+ /** Tx hashes we've already logged for trace+debug failure (log once per tx per process). */
48
+ private static readonly traceFailureWarnedTxHashes = new LruSet<string>(1000);
49
+
50
+ /** Clears the trace-failure warned set. For testing only. */
51
+ static resetTraceFailureWarnedForTesting(): void {
52
+ CalldataRetriever.traceFailureWarnedTxHashes.clear();
53
+ }
54
+
55
+ constructor(
56
+ private readonly publicClient: ViemPublicClient,
57
+ private readonly debugClient: ViemPublicDebugClient,
58
+ private readonly targetCommitteeSize: number,
59
+ private readonly instrumentation: ArchiverInstrumentation | undefined,
60
+ private readonly logger: Logger,
61
+ private readonly rollupAddress: EthAddress,
62
+ ) {}
63
+
64
+ private getSignatureContext() {
65
+ return {
66
+ chainId: this.publicClient.chain.id,
67
+ rollupAddress: this.rollupAddress,
68
+ };
69
+ }
70
+
71
+ /**
72
+ * Gets checkpoint header and metadata from the calldata of an L1 transaction.
73
+ * Tries multicall3 decoding, falls back to trace-based extraction.
74
+ * @param txHash - Hash of the tx that published it.
75
+ * @param blobHashes - Blob hashes for the checkpoint.
76
+ * @param checkpointNumber - Checkpoint number.
77
+ * @param expectedHashes - Expected hashes from the CheckpointProposed event for validation
78
+ * @returns Checkpoint header and metadata from the calldata, deserialized
79
+ */
80
+ async getCheckpointFromRollupTx(
81
+ txHash: `0x${string}`,
82
+ _blobHashes: Buffer[],
83
+ checkpointNumber: CheckpointNumber,
84
+ expectedHashes: {
85
+ attestationsHash: Hex;
86
+ payloadDigest: Hex;
87
+ },
88
+ ): Promise<CheckpointData> {
89
+ this.logger.trace(`Fetching checkpoint ${checkpointNumber} from rollup tx ${txHash}`);
90
+ const tx = await this.publicClient.getTransaction({ hash: txHash });
91
+ return this.getCheckpointFromTx(tx, checkpointNumber, expectedHashes);
92
+ }
93
+
94
+ /** Gets checkpoint data from a transaction by trying decode strategies then falling back to trace. */
95
+ protected async getCheckpointFromTx(
96
+ tx: Transaction,
97
+ checkpointNumber: CheckpointNumber,
98
+ expectedHashes: { attestationsHash: Hex; payloadDigest: Hex },
99
+ ): Promise<CheckpointData> {
100
+ // Try to decode as multicall3 with hash-verified matching
101
+ const multicall3Result = this.tryDecodeMulticall3(tx, expectedHashes, checkpointNumber, tx.blockHash!);
102
+ if (multicall3Result) {
103
+ this.logger.trace(`Decoded propose calldata from multicall3 for tx ${tx.hash}`);
104
+ this.instrumentation?.recordBlockProposalTxTarget(tx.to!, false);
105
+ return multicall3Result;
106
+ }
107
+
108
+ // Try to decode as direct propose call
109
+ const directResult = this.tryDecodeDirectPropose(tx, expectedHashes, checkpointNumber, tx.blockHash!);
110
+ if (directResult) {
111
+ this.logger.trace(`Decoded propose calldata from direct call for tx ${tx.hash}`);
112
+ this.instrumentation?.recordBlockProposalTxTarget(tx.to!, false);
113
+ return directResult;
114
+ }
115
+
116
+ // Try to decode as Spire Proposer multicall wrapper
117
+ const spireResult = await this.tryDecodeSpireProposer(tx, expectedHashes, checkpointNumber, tx.blockHash!);
118
+ if (spireResult) {
119
+ this.logger.trace(`Decoded propose calldata from Spire Proposer for tx ${tx.hash}`);
120
+ this.instrumentation?.recordBlockProposalTxTarget(tx.to!, false);
121
+ return spireResult;
122
+ }
123
+
124
+ // Fall back to trace-based extraction
125
+ this.logger.warn(
126
+ `Failed to decode multicall3, direct propose, or Spire proposer for L1 tx ${tx.hash}, falling back to trace for checkpoint ${checkpointNumber}`,
127
+ );
128
+ this.instrumentation?.recordBlockProposalTxTarget(tx.to ?? EthAddress.ZERO.toString(), true);
129
+ const tracedCalldata = await this.extractCalldataViaTrace(tx.hash);
130
+ const tracedResult = this.tryDecodeAndVerifyPropose(
131
+ tracedCalldata,
132
+ expectedHashes,
133
+ checkpointNumber,
134
+ tx.blockHash!,
135
+ );
136
+ if (!tracedResult) {
137
+ throw new Error(`Hash mismatch for traced propose calldata in tx ${tx.hash} for checkpoint ${checkpointNumber}`);
138
+ }
139
+ return tracedResult;
140
+ }
141
+
142
+ /**
143
+ * Attempts to decode a transaction as a Spire Proposer multicall wrapper.
144
+ * If successful, iterates all wrapped calls and validates each as either multicall3
145
+ * or direct propose, verifying against expected hashes.
146
+ * @param tx - The transaction to decode
147
+ * @param expectedHashes - Expected hashes for hash-verified matching
148
+ * @param checkpointNumber - The checkpoint number
149
+ * @param blockHash - The L1 block hash
150
+ * @returns The checkpoint data if successfully decoded and validated, undefined otherwise
151
+ */
152
+ protected async tryDecodeSpireProposer(
153
+ tx: Transaction,
154
+ expectedHashes: { attestationsHash: Hex; payloadDigest: Hex },
155
+ checkpointNumber: CheckpointNumber,
156
+ blockHash: Hex,
157
+ ): Promise<CheckpointData | undefined> {
158
+ // Try to decode as Spire Proposer multicall (extracts all wrapped calls)
159
+ const spireWrappedCalls = await getCallsFromSpireProposer(tx, this.publicClient, this.logger);
160
+ if (!spireWrappedCalls) {
161
+ return undefined;
162
+ }
163
+
164
+ this.logger.trace(`Decoded Spire Proposer wrapping for tx ${tx.hash}, ${spireWrappedCalls.length} inner call(s)`);
165
+
166
+ // Try each wrapped call as either multicall3 or direct propose
167
+ for (const spireWrappedCall of spireWrappedCalls) {
168
+ const wrappedTx = { to: spireWrappedCall.to, input: spireWrappedCall.data, hash: tx.hash };
169
+
170
+ const multicall3Result = this.tryDecodeMulticall3(wrappedTx, expectedHashes, checkpointNumber, blockHash);
171
+ if (multicall3Result) {
172
+ this.logger.trace(`Decoded propose calldata from Spire Proposer to multicall3 for tx ${tx.hash}`);
173
+ return multicall3Result;
174
+ }
175
+
176
+ const directResult = this.tryDecodeDirectPropose(wrappedTx, expectedHashes, checkpointNumber, blockHash);
177
+ if (directResult) {
178
+ this.logger.trace(`Decoded propose calldata from Spire Proposer to direct propose for tx ${tx.hash}`);
179
+ return directResult;
180
+ }
181
+ }
182
+
183
+ this.logger.warn(
184
+ `Spire Proposer wrapped calls could not be decoded as multicall3 or direct propose for tx ${tx.hash}`,
185
+ );
186
+ return undefined;
187
+ }
188
+
189
+ /**
190
+ * Attempts to decode transaction input as multicall3 and extract propose calldata.
191
+ * Finds all calls matching the rollup address and propose selector, then decodes
192
+ * and verifies each candidate against expected hashes from the CheckpointProposed event.
193
+ * @param tx - The transaction-like object with to, input, and hash
194
+ * @param expectedHashes - Expected hashes from CheckpointProposed event
195
+ * @param checkpointNumber - The checkpoint number
196
+ * @param blockHash - The L1 block hash
197
+ * @returns The checkpoint data if successfully validated, undefined otherwise
198
+ */
199
+ protected tryDecodeMulticall3(
200
+ tx: { to: Hex | null | undefined; input: Hex; hash: Hex },
201
+ expectedHashes: { attestationsHash: Hex; payloadDigest: Hex },
202
+ checkpointNumber: CheckpointNumber,
203
+ blockHash: Hex,
204
+ ): CheckpointData | undefined {
205
+ const txHash = tx.hash;
206
+
207
+ try {
208
+ // Check if transaction is to Multicall3 address
209
+ if (!tx.to || !EthAddress.areEqual(tx.to, MULTI_CALL_3_ADDRESS)) {
210
+ this.logger.debug(`Transaction is not to Multicall3 address (to: ${tx.to})`, { txHash, to: tx.to });
211
+ return undefined;
212
+ }
213
+
214
+ // Try to decode as multicall3 aggregate3 call
215
+ const { functionName: multicall3Fn, args: multicall3Args } = decodeFunctionData({
216
+ abi: multicall3Abi,
217
+ data: tx.input,
218
+ });
219
+
220
+ // If not aggregate3, return undefined (not a multicall3 transaction)
221
+ if (multicall3Fn !== 'aggregate3') {
222
+ this.logger.warn(`Transaction is not multicall3 aggregate3 (got ${multicall3Fn})`, { txHash });
223
+ return undefined;
224
+ }
225
+
226
+ if (multicall3Args.length !== 1) {
227
+ this.logger.warn(`Unexpected number of arguments for multicall3 (got ${multicall3Args.length})`, { txHash });
228
+ return undefined;
229
+ }
230
+
231
+ const [calls] = multicall3Args;
232
+
233
+ // Find all calls matching rollup address + propose selector
234
+ const rollupAddressLower = this.rollupAddress.toString().toLowerCase();
235
+ const proposeSelectorLower = PROPOSE_SELECTOR.toLowerCase();
236
+ const candidates: Hex[] = [];
237
+
238
+ for (const call of calls) {
239
+ const addr = call.target.toLowerCase();
240
+ const callData = call.callData;
241
+
242
+ if (callData.length < 10) {
243
+ continue;
244
+ }
245
+
246
+ const selector = callData.slice(0, 10).toLowerCase();
247
+ if (addr === rollupAddressLower && selector === proposeSelectorLower) {
248
+ candidates.push(callData);
249
+ }
250
+ }
251
+
252
+ if (candidates.length === 0) {
253
+ this.logger.debug(`No propose candidates found in multicall3`, { txHash });
254
+ return undefined;
255
+ }
256
+
257
+ // Decode, verify, and build for each candidate
258
+ const verified: CheckpointData[] = [];
259
+ for (const candidate of candidates) {
260
+ const result = this.tryDecodeAndVerifyPropose(candidate, expectedHashes, checkpointNumber, blockHash);
261
+ if (result) {
262
+ verified.push(result);
263
+ }
264
+ }
265
+
266
+ if (verified.length === 1) {
267
+ this.logger.trace(`Verified single propose candidate via hash matching`, { txHash });
268
+ return verified[0];
269
+ }
270
+
271
+ if (verified.length > 1) {
272
+ this.logger.warn(
273
+ `Multiple propose candidates verified (${verified.length}), returning first (identical data)`,
274
+ { txHash },
275
+ );
276
+ return verified[0];
277
+ }
278
+
279
+ this.logger.debug(`No candidates verified against expected hashes`, { txHash });
280
+ return undefined;
281
+ } catch (err) {
282
+ // Any decoding error triggers fallback to trace
283
+ this.logger.warn(`Failed to decode multicall3: ${err}`, { txHash });
284
+ return undefined;
285
+ }
286
+ }
287
+
288
+ /**
289
+ * Attempts to decode transaction as a direct propose call to the rollup contract.
290
+ * Decodes, verifies hashes, and builds checkpoint data in a single pass.
291
+ * @param tx - The transaction-like object with to, input, and hash
292
+ * @param expectedHashes - Expected hashes from CheckpointProposed event
293
+ * @param checkpointNumber - The checkpoint number
294
+ * @param blockHash - The L1 block hash
295
+ * @returns The checkpoint data if successfully validated, undefined otherwise
296
+ */
297
+ protected tryDecodeDirectPropose(
298
+ tx: { to: Hex | null | undefined; input: Hex; hash: Hex },
299
+ expectedHashes: { attestationsHash: Hex; payloadDigest: Hex },
300
+ checkpointNumber: CheckpointNumber,
301
+ blockHash: Hex,
302
+ ): CheckpointData | undefined {
303
+ const txHash = tx.hash;
304
+ try {
305
+ // Check if transaction is to the rollup address
306
+ if (!tx.to || !EthAddress.areEqual(tx.to, this.rollupAddress)) {
307
+ this.logger.debug(`Transaction is not to rollup address (to: ${tx.to})`, { txHash });
308
+ return undefined;
309
+ }
310
+
311
+ // Validate it's a propose call before full decode+verify
312
+ const { functionName } = decodeFunctionData({ abi: RollupAbi, data: tx.input });
313
+ if (functionName !== 'propose') {
314
+ this.logger.warn(`Transaction to rollup is not propose (got ${functionName})`, { txHash });
315
+ return undefined;
316
+ }
317
+
318
+ // Decode, verify hashes, and build checkpoint data
319
+ this.logger.trace(`Validated direct propose call to rollup`, { txHash });
320
+ return this.tryDecodeAndVerifyPropose(tx.input, expectedHashes, checkpointNumber, blockHash);
321
+ } catch (err) {
322
+ // Any decoding error means it's not a valid propose call
323
+ this.logger.warn(`Failed to decode as direct propose: ${err}`, { txHash });
324
+ return undefined;
325
+ }
326
+ }
327
+
328
+ /**
329
+ * Uses debug/trace RPC to extract the actual calldata from the successful propose call.
330
+ * This is the definitive fallback that works for any transaction pattern.
331
+ * Tries trace_transaction first, then falls back to debug_traceTransaction.
332
+ * @param txHash - The transaction hash to trace
333
+ * @returns The propose calldata from the successful call
334
+ */
335
+ protected async extractCalldataViaTrace(txHash: Hex): Promise<Hex> {
336
+ const rollupAddress = this.rollupAddress;
337
+ const selector = PROPOSE_SELECTOR;
338
+
339
+ let calls: CallInfo[];
340
+ try {
341
+ // Try trace_transaction first (using Parity/OpenEthereum/Erigon RPC)
342
+ this.logger.debug(`Attempting to trace transaction ${txHash} using trace_transaction`);
343
+ calls = await getSuccessfulCallsFromTrace(this.debugClient, txHash, rollupAddress, selector, this.logger);
344
+ this.logger.debug(`Successfully traced using trace_transaction, found ${calls.length} calls`);
345
+ } catch (err) {
346
+ const traceError = err instanceof Error ? err : new Error(String(err));
347
+ this.logger.verbose(`Failed trace_transaction for ${txHash}: ${traceError.message}`);
348
+ this.logger.debug(`Trace failure details for ${txHash}`, { traceError });
349
+
350
+ try {
351
+ // Fall back to debug_traceTransaction (Geth RPC)
352
+ this.logger.debug(`Attempting to trace transaction ${txHash} using debug_traceTransaction`);
353
+ calls = await getSuccessfulCallsFromDebug(this.debugClient, txHash, rollupAddress, selector, this.logger);
354
+ this.logger.debug(`Successfully traced using debug_traceTransaction, found ${calls.length} calls`);
355
+ } catch (debugErr) {
356
+ const debugError = debugErr instanceof Error ? debugErr : new Error(String(debugErr));
357
+ // Log once per tx so we don't spam on every sync cycle when sync point doesn't advance
358
+ if (!CalldataRetriever.traceFailureWarnedTxHashes.has(txHash)) {
359
+ CalldataRetriever.traceFailureWarnedTxHashes.add(txHash);
360
+ this.logger.warn(
361
+ `Cannot decode L1 tx ${txHash}: trace and debug RPC failed or unavailable. ` +
362
+ `trace_transaction: ${traceError.message}; debug_traceTransaction: ${debugError.message}`,
363
+ );
364
+ }
365
+ // Full error objects can be very long; keep at debug only
366
+ this.logger.debug(`Trace/debug failure details for tx ${txHash}`, {
367
+ traceError,
368
+ debugError,
369
+ txHash,
370
+ });
371
+ throw new Error(`Failed to trace transaction ${txHash} to extract propose calldata`);
372
+ }
373
+ }
374
+
375
+ // Validate exactly ONE successful propose call
376
+ if (calls.length === 0) {
377
+ throw new Error(`No successful propose calls found in transaction ${txHash}`);
378
+ }
379
+
380
+ if (calls.length > 1) {
381
+ throw new Error(`Multiple successful propose calls found in transaction ${txHash} (${calls.length})`);
382
+ }
383
+
384
+ // Return the calldata from the single successful propose call
385
+ return calls[0].input;
386
+ }
387
+
388
+ /**
389
+ * Decodes propose calldata, verifies against expected hashes, and builds checkpoint data.
390
+ * Returns undefined on decode errors or hash mismatches (soft failure for try-based callers).
391
+ * @param proposeCalldata - The propose function calldata
392
+ * @param expectedHashes - Expected hashes from the CheckpointProposed event
393
+ * @param checkpointNumber - The checkpoint number
394
+ * @param blockHash - The L1 block hash
395
+ * @returns The decoded checkpoint data, or undefined on failure
396
+ */
397
+ protected tryDecodeAndVerifyPropose(
398
+ proposeCalldata: Hex,
399
+ expectedHashes: { attestationsHash: Hex; payloadDigest: Hex },
400
+ checkpointNumber: CheckpointNumber,
401
+ blockHash: Hex,
402
+ ): CheckpointData | undefined {
403
+ try {
404
+ const { functionName, args } = decodeFunctionData({ abi: RollupAbi, data: proposeCalldata });
405
+ if (functionName !== 'propose') {
406
+ return undefined;
407
+ }
408
+
409
+ const [decodedArgs, packedAttestations] = args! as readonly [
410
+ { archive: Hex; oracleInput: { feeAssetPriceModifier: bigint }; header: ViemHeader },
411
+ ViemCommitteeAttestations,
412
+ ...unknown[],
413
+ ];
414
+
415
+ // Verify attestationsHash
416
+ const computedAttestationsHash = this.computeAttestationsHash(packedAttestations);
417
+ if (
418
+ !Buffer.from(hexToBytes(computedAttestationsHash)).equals(
419
+ Buffer.from(hexToBytes(expectedHashes.attestationsHash)),
420
+ )
421
+ ) {
422
+ this.logger.warn(`Attestations hash mismatch during verification`, {
423
+ computed: computedAttestationsHash,
424
+ expected: expectedHashes.attestationsHash,
425
+ });
426
+ return undefined;
427
+ }
428
+
429
+ // Verify payloadDigest
430
+ const header = CheckpointHeader.fromViem(decodedArgs.header);
431
+ const archiveRoot = new Fr(Buffer.from(hexToBytes(decodedArgs.archive)));
432
+ const feeAssetPriceModifier = decodedArgs.oracleInput.feeAssetPriceModifier;
433
+ const computedPayloadDigest = this.computePayloadDigest(header, archiveRoot, feeAssetPriceModifier);
434
+ if (
435
+ !Buffer.from(hexToBytes(computedPayloadDigest)).equals(Buffer.from(hexToBytes(expectedHashes.payloadDigest)))
436
+ ) {
437
+ this.logger.warn(`Payload digest mismatch during verification`, {
438
+ computed: computedPayloadDigest,
439
+ expected: expectedHashes.payloadDigest,
440
+ });
441
+ return undefined;
442
+ }
443
+
444
+ const attestations = CommitteeAttestation.fromPacked(packedAttestations, this.targetCommitteeSize);
445
+
446
+ this.logger.trace(`Validated and decoded propose calldata for checkpoint ${checkpointNumber}`, {
447
+ checkpointNumber,
448
+ archive: decodedArgs.archive,
449
+ header: decodedArgs.header,
450
+ l1BlockHash: blockHash,
451
+ attestations,
452
+ packedAttestations,
453
+ targetCommitteeSize: this.targetCommitteeSize,
454
+ });
455
+
456
+ return {
457
+ checkpointNumber,
458
+ archiveRoot,
459
+ header,
460
+ attestations,
461
+ blockHash,
462
+ feeAssetPriceModifier,
463
+ };
464
+ } catch {
465
+ return undefined;
466
+ }
467
+ }
468
+
469
+ /** Computes the keccak256 hash of ABI-encoded CommitteeAttestations. */
470
+ private computeAttestationsHash(packedAttestations: ViemCommitteeAttestations): Hex {
471
+ return keccak256(encodeAbiParameters([this.getCommitteeAttestationsStructDef()], [packedAttestations]));
472
+ }
473
+
474
+ /** Computes the keccak256 payload digest from the checkpoint header, archive root, and fee asset price modifier. */
475
+ private computePayloadDigest(header: CheckpointHeader, archiveRoot: Fr, feeAssetPriceModifier: bigint): Hex {
476
+ return computeCheckpointPayloadDigest({
477
+ header,
478
+ archiveRoot,
479
+ feeAssetPriceModifier,
480
+ signatureContext: this.getSignatureContext(),
481
+ }).toString();
482
+ }
483
+
484
+ /**
485
+ * Extracts the CommitteeAttestations struct definition from RollupAbi.
486
+ * Finds the _attestations parameter by name in the propose function.
487
+ */
488
+ private getCommitteeAttestationsStructDef(): AbiParameter {
489
+ const proposeFunction = RollupAbi.find(item => item.type === 'function' && item.name === 'propose') as
490
+ | { type: 'function'; name: string; inputs: readonly AbiParameter[] }
491
+ | undefined;
492
+
493
+ if (!proposeFunction) {
494
+ throw new Error('propose function not found in RollupAbi');
495
+ }
496
+
497
+ // Find the _attestations parameter by name, not by index
498
+ const attestationsParam = proposeFunction.inputs.find(param => param.name === '_attestations');
499
+
500
+ if (!attestationsParam) {
501
+ throw new Error('_attestations parameter not found in propose function');
502
+ }
503
+
504
+ if (attestationsParam.type !== 'tuple') {
505
+ throw new Error(`Expected _attestations parameter to be a tuple, got ${attestationsParam.type}`);
506
+ }
507
+
508
+ // Extract the tuple components (struct fields)
509
+ const tupleParam = attestationsParam as unknown as {
510
+ type: 'tuple';
511
+ components?: readonly AbiParameter[];
512
+ };
513
+
514
+ return {
515
+ type: 'tuple',
516
+ components: tupleParam.components || [],
517
+ } as AbiParameter;
518
+ }
519
+ }
520
+
521
+ /** Function selector for the `propose` method of the rollup contract. */
522
+ const PROPOSE_SELECTOR = toFunctionSelector(RollupAbi.find(x => x.type === 'function' && x.name === 'propose')!);