@aztec/archiver 4.0.0-nightly.20250907 → 4.0.0-nightly.20260107

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 (133) hide show
  1. package/README.md +27 -6
  2. package/dest/archiver/archiver.d.ts +127 -84
  3. package/dest/archiver/archiver.d.ts.map +1 -1
  4. package/dest/archiver/archiver.js +1128 -380
  5. package/dest/archiver/archiver_store.d.ts +122 -45
  6. package/dest/archiver/archiver_store.d.ts.map +1 -1
  7. package/dest/archiver/archiver_store_test_suite.d.ts +1 -1
  8. package/dest/archiver/archiver_store_test_suite.d.ts.map +1 -1
  9. package/dest/archiver/archiver_store_test_suite.js +2013 -343
  10. package/dest/archiver/config.d.ts +7 -20
  11. package/dest/archiver/config.d.ts.map +1 -1
  12. package/dest/archiver/config.js +21 -5
  13. package/dest/archiver/errors.d.ts +25 -1
  14. package/dest/archiver/errors.d.ts.map +1 -1
  15. package/dest/archiver/errors.js +37 -0
  16. package/dest/archiver/index.d.ts +2 -2
  17. package/dest/archiver/index.d.ts.map +1 -1
  18. package/dest/archiver/instrumentation.d.ts +5 -3
  19. package/dest/archiver/instrumentation.d.ts.map +1 -1
  20. package/dest/archiver/instrumentation.js +14 -0
  21. package/dest/archiver/kv_archiver_store/block_store.d.ts +83 -15
  22. package/dest/archiver/kv_archiver_store/block_store.d.ts.map +1 -1
  23. package/dest/archiver/kv_archiver_store/block_store.js +396 -73
  24. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts +2 -2
  25. package/dest/archiver/kv_archiver_store/contract_class_store.d.ts.map +1 -1
  26. package/dest/archiver/kv_archiver_store/contract_class_store.js +1 -1
  27. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts +2 -2
  28. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +1 -1
  29. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +51 -55
  30. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +1 -1
  31. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +82 -46
  32. package/dest/archiver/kv_archiver_store/log_store.d.ts +12 -16
  33. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +1 -1
  34. package/dest/archiver/kv_archiver_store/log_store.js +149 -84
  35. package/dest/archiver/kv_archiver_store/message_store.d.ts +6 -5
  36. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +1 -1
  37. package/dest/archiver/kv_archiver_store/message_store.js +15 -14
  38. package/dest/archiver/l1/bin/retrieve-calldata.d.ts +3 -0
  39. package/dest/archiver/l1/bin/retrieve-calldata.d.ts.map +1 -0
  40. package/dest/archiver/l1/bin/retrieve-calldata.js +149 -0
  41. package/dest/archiver/l1/calldata_retriever.d.ts +112 -0
  42. package/dest/archiver/l1/calldata_retriever.d.ts.map +1 -0
  43. package/dest/archiver/l1/calldata_retriever.js +471 -0
  44. package/dest/archiver/l1/data_retrieval.d.ts +90 -0
  45. package/dest/archiver/l1/data_retrieval.d.ts.map +1 -0
  46. package/dest/archiver/l1/data_retrieval.js +331 -0
  47. package/dest/archiver/l1/debug_tx.d.ts +19 -0
  48. package/dest/archiver/l1/debug_tx.d.ts.map +1 -0
  49. package/dest/archiver/l1/debug_tx.js +73 -0
  50. package/dest/archiver/l1/spire_proposer.d.ts +70 -0
  51. package/dest/archiver/l1/spire_proposer.d.ts.map +1 -0
  52. package/dest/archiver/l1/spire_proposer.js +157 -0
  53. package/dest/archiver/l1/trace_tx.d.ts +97 -0
  54. package/dest/archiver/l1/trace_tx.d.ts.map +1 -0
  55. package/dest/archiver/l1/trace_tx.js +91 -0
  56. package/dest/archiver/l1/types.d.ts +12 -0
  57. package/dest/archiver/l1/types.d.ts.map +1 -0
  58. package/dest/archiver/l1/types.js +3 -0
  59. package/dest/archiver/l1/validate_trace.d.ts +29 -0
  60. package/dest/archiver/l1/validate_trace.d.ts.map +1 -0
  61. package/dest/archiver/l1/validate_trace.js +150 -0
  62. package/dest/archiver/structs/data_retrieval.d.ts +1 -1
  63. package/dest/archiver/structs/inbox_message.d.ts +4 -4
  64. package/dest/archiver/structs/inbox_message.d.ts.map +1 -1
  65. package/dest/archiver/structs/inbox_message.js +6 -5
  66. package/dest/archiver/structs/published.d.ts +2 -2
  67. package/dest/archiver/structs/published.d.ts.map +1 -1
  68. package/dest/archiver/validation.d.ts +10 -4
  69. package/dest/archiver/validation.d.ts.map +1 -1
  70. package/dest/archiver/validation.js +66 -44
  71. package/dest/factory.d.ts +4 -6
  72. package/dest/factory.d.ts.map +1 -1
  73. package/dest/factory.js +5 -4
  74. package/dest/index.d.ts +2 -2
  75. package/dest/index.d.ts.map +1 -1
  76. package/dest/index.js +1 -1
  77. package/dest/rpc/index.d.ts +2 -2
  78. package/dest/test/index.d.ts +1 -1
  79. package/dest/test/mock_archiver.d.ts +16 -8
  80. package/dest/test/mock_archiver.d.ts.map +1 -1
  81. package/dest/test/mock_archiver.js +19 -14
  82. package/dest/test/mock_l1_to_l2_message_source.d.ts +7 -6
  83. package/dest/test/mock_l1_to_l2_message_source.d.ts.map +1 -1
  84. package/dest/test/mock_l1_to_l2_message_source.js +10 -9
  85. package/dest/test/mock_l2_block_source.d.ts +31 -20
  86. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  87. package/dest/test/mock_l2_block_source.js +85 -18
  88. package/dest/test/mock_structs.d.ts +3 -2
  89. package/dest/test/mock_structs.d.ts.map +1 -1
  90. package/dest/test/mock_structs.js +9 -8
  91. package/package.json +18 -17
  92. package/src/archiver/archiver.ts +971 -475
  93. package/src/archiver/archiver_store.ts +141 -44
  94. package/src/archiver/archiver_store_test_suite.ts +2114 -331
  95. package/src/archiver/config.ts +30 -35
  96. package/src/archiver/errors.ts +64 -0
  97. package/src/archiver/index.ts +1 -1
  98. package/src/archiver/instrumentation.ts +19 -2
  99. package/src/archiver/kv_archiver_store/block_store.ts +541 -83
  100. package/src/archiver/kv_archiver_store/contract_class_store.ts +1 -1
  101. package/src/archiver/kv_archiver_store/contract_instance_store.ts +1 -1
  102. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +107 -67
  103. package/src/archiver/kv_archiver_store/log_store.ts +209 -99
  104. package/src/archiver/kv_archiver_store/message_store.ts +21 -18
  105. package/src/archiver/l1/README.md +98 -0
  106. package/src/archiver/l1/bin/retrieve-calldata.ts +182 -0
  107. package/src/archiver/l1/calldata_retriever.ts +641 -0
  108. package/src/archiver/l1/data_retrieval.ts +512 -0
  109. package/src/archiver/l1/debug_tx.ts +99 -0
  110. package/src/archiver/l1/spire_proposer.ts +160 -0
  111. package/src/archiver/l1/trace_tx.ts +128 -0
  112. package/src/archiver/l1/types.ts +13 -0
  113. package/src/archiver/l1/validate_trace.ts +211 -0
  114. package/src/archiver/structs/inbox_message.ts +8 -8
  115. package/src/archiver/structs/published.ts +1 -1
  116. package/src/archiver/validation.ts +86 -32
  117. package/src/factory.ts +6 -7
  118. package/src/index.ts +1 -1
  119. package/src/test/fixtures/debug_traceTransaction-multicall3.json +88 -0
  120. package/src/test/fixtures/debug_traceTransaction-multiplePropose.json +153 -0
  121. package/src/test/fixtures/debug_traceTransaction-proxied.json +122 -0
  122. package/src/test/fixtures/trace_transaction-multicall3.json +65 -0
  123. package/src/test/fixtures/trace_transaction-multiplePropose.json +319 -0
  124. package/src/test/fixtures/trace_transaction-proxied.json +128 -0
  125. package/src/test/fixtures/trace_transaction-randomRevert.json +216 -0
  126. package/src/test/mock_archiver.ts +22 -16
  127. package/src/test/mock_l1_to_l2_message_source.ts +10 -9
  128. package/src/test/mock_l2_block_source.ts +114 -27
  129. package/src/test/mock_structs.ts +10 -9
  130. package/dest/archiver/data_retrieval.d.ts +0 -78
  131. package/dest/archiver/data_retrieval.d.ts.map +0 -1
  132. package/dest/archiver/data_retrieval.js +0 -354
  133. package/src/archiver/data_retrieval.ts +0 -535
@@ -1,78 +0,0 @@
1
- import type { BlobSinkClientInterface } from '@aztec/blob-sink/client';
2
- import type { ViemClient, ViemPublicClient } from '@aztec/ethereum';
3
- import type { EthAddress } from '@aztec/foundation/eth-address';
4
- import { Fr } from '@aztec/foundation/fields';
5
- import { type Logger } from '@aztec/foundation/log';
6
- import { type InboxAbi, RollupAbi } from '@aztec/l1-artifacts';
7
- import { Body, CommitteeAttestation } from '@aztec/stdlib/block';
8
- import { Proof } from '@aztec/stdlib/proofs';
9
- import { ProposedBlockHeader, StateReference } from '@aztec/stdlib/tx';
10
- import { type GetContractReturnType, type Hex } from 'viem';
11
- import type { DataRetrieval } from './structs/data_retrieval.js';
12
- import type { InboxMessage } from './structs/inbox_message.js';
13
- import type { L1PublishedData, PublishedL2Block } from './structs/published.js';
14
- export type RetrievedL2Block = {
15
- l2BlockNumber: number;
16
- archiveRoot: Fr;
17
- stateReference: StateReference;
18
- header: ProposedBlockHeader;
19
- body: Body;
20
- l1: L1PublishedData;
21
- chainId: Fr;
22
- version: Fr;
23
- attestations: CommitteeAttestation[];
24
- };
25
- export declare function retrievedBlockToPublishedL2Block(retrievedBlock: RetrievedL2Block): PublishedL2Block;
26
- /**
27
- * Fetches new L2 blocks.
28
- * @param publicClient - The viem public client to use for transaction retrieval.
29
- * @param rollupAddress - The address of the rollup contract.
30
- * @param searchStartBlock - The block number to use for starting the search.
31
- * @param searchEndBlock - The highest block number that we should search up to.
32
- * @param expectedNextL2BlockNum - The next L2 block number that we expect to find.
33
- * @returns An array of block; as well as the next eth block to search from.
34
- */
35
- export declare function retrieveBlocksFromRollup(rollup: GetContractReturnType<typeof RollupAbi, ViemPublicClient>, publicClient: ViemPublicClient, blobSinkClient: BlobSinkClientInterface, searchStartBlock: bigint, searchEndBlock: bigint, logger?: Logger): Promise<RetrievedL2Block[]>;
36
- export declare function getL1BlockTime(publicClient: ViemPublicClient, blockNumber: bigint): Promise<bigint>;
37
- /** Given an L1 to L2 message, retrieves its corresponding event from the Inbox within a specific block range. */
38
- export declare function retrieveL1ToL2Message(inbox: GetContractReturnType<typeof InboxAbi, ViemClient>, leaf: Fr, fromBlock: bigint, toBlock: bigint): Promise<InboxMessage | undefined>;
39
- /**
40
- * Fetch L1 to L2 messages.
41
- * @param publicClient - The viem public client to use for transaction retrieval.
42
- * @param inboxAddress - The address of the inbox contract to fetch messages from.
43
- * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
44
- * @param searchStartBlock - The block number to use for starting the search.
45
- * @param searchEndBlock - The highest block number that we should search up to.
46
- * @returns An array of InboxLeaf and next eth block to search from.
47
- */
48
- export declare function retrieveL1ToL2Messages(inbox: GetContractReturnType<typeof InboxAbi, ViemClient>, searchStartBlock: bigint, searchEndBlock: bigint): Promise<InboxMessage[]>;
49
- /** Retrieves L2ProofVerified events from the rollup contract. */
50
- export declare function retrieveL2ProofVerifiedEvents(publicClient: ViemPublicClient, rollupAddress: EthAddress, searchStartBlock: bigint, searchEndBlock?: bigint): Promise<{
51
- l1BlockNumber: bigint;
52
- l2BlockNumber: number;
53
- proverId: Fr;
54
- txHash: Hex;
55
- }[]>;
56
- /** Retrieve submitted proofs from the rollup contract */
57
- export declare function retrieveL2ProofsFromRollup(publicClient: ViemPublicClient, rollupAddress: EthAddress, searchStartBlock: bigint, searchEndBlock?: bigint): Promise<DataRetrieval<{
58
- proof: Proof;
59
- proverId: Fr;
60
- l2BlockNumber: number;
61
- txHash: `0x${string}`;
62
- }>>;
63
- export type SubmitBlockProof = {
64
- archiveRoot: Fr;
65
- proverId: Fr;
66
- proof: Proof;
67
- };
68
- /**
69
- * Gets block metadata (header and archive snapshot) from the calldata of an L1 transaction.
70
- * Assumes that the block was published from an EOA.
71
- * TODO: Add retries and error management.
72
- * @param publicClient - The viem public client to use for transaction retrieval.
73
- * @param txHash - Hash of the tx that published it.
74
- * @param l2BlockNum - L2 block number.
75
- * @returns L2 block metadata (header and archive) from the calldata, deserialized
76
- */
77
- export declare function getProofFromSubmitProofTx(publicClient: ViemPublicClient, txHash: `0x${string}`, expectedProverId: Fr): Promise<SubmitBlockProof>;
78
- //# sourceMappingURL=data_retrieval.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3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AAE,gBAAgB,EAC9B,aAAa,EAAE,UAAU,EACzB,gBAAgB,EAAE,MAAM,EACxB,cAAc,CAAC,EAAE,MAAM,GACtB,OAAO,CAAC,aAAa,CAAC;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,QAAQ,EAAE,EAAE,CAAC;IAAC,aAAa,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,KAAK,MAAM,EAAE,CAAA;CAAE,CAAC,CAAC,CAatG;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC7B,WAAW,EAAE,EAAE,CAAC;IAChB,QAAQ,EAAE,EAAE,CAAC;IACb,KAAK,EAAE,KAAK,CAAC;CACd,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAsB,yBAAyB,CAC7C,YAAY,EAAE,gBAAgB,EAC9B,MAAM,EAAE,KAAK,MAAM,EAAE,EACrB,gBAAgB,EAAE,EAAE,GACnB,OAAO,CAAC,gBAAgB,CAAC,CAmC3B"}
@@ -1,354 +0,0 @@
1
- import { Blob, BlobDeserializationError } from '@aztec/blob-lib';
2
- import { asyncPool } from '@aztec/foundation/async-pool';
3
- import { Buffer16, Buffer32 } from '@aztec/foundation/buffer';
4
- import { Fr } from '@aztec/foundation/fields';
5
- import { createLogger } from '@aztec/foundation/log';
6
- import { RollupAbi } from '@aztec/l1-artifacts';
7
- import { Body, CommitteeAttestation, L2Block } from '@aztec/stdlib/block';
8
- import { Proof } from '@aztec/stdlib/proofs';
9
- import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
10
- import { BlockHeader, GlobalVariables, ProposedBlockHeader, StateReference } from '@aztec/stdlib/tx';
11
- import { decodeFunctionData, getAbiItem, hexToBytes, multicall3Abi } from 'viem';
12
- import { NoBlobBodiesFoundError } from './errors.js';
13
- export function retrievedBlockToPublishedL2Block(retrievedBlock) {
14
- const { l2BlockNumber, archiveRoot, stateReference, header: proposedHeader, body, l1, chainId, version, attestations } = retrievedBlock;
15
- const archive = new AppendOnlyTreeSnapshot(archiveRoot, l2BlockNumber + 1);
16
- const globalVariables = GlobalVariables.from({
17
- chainId,
18
- version,
19
- blockNumber: l2BlockNumber,
20
- slotNumber: proposedHeader.slotNumber,
21
- timestamp: proposedHeader.timestamp,
22
- coinbase: proposedHeader.coinbase,
23
- feeRecipient: proposedHeader.feeRecipient,
24
- gasFees: proposedHeader.gasFees
25
- });
26
- const header = BlockHeader.from({
27
- lastArchive: new AppendOnlyTreeSnapshot(proposedHeader.lastArchiveRoot, l2BlockNumber),
28
- contentCommitment: proposedHeader.contentCommitment,
29
- state: stateReference,
30
- globalVariables,
31
- totalFees: body.txEffects.reduce((accum, txEffect)=>accum.add(txEffect.transactionFee), Fr.ZERO),
32
- totalManaUsed: proposedHeader.totalManaUsed
33
- });
34
- const block = new L2Block(archive, header, body);
35
- return {
36
- block,
37
- l1,
38
- attestations
39
- };
40
- }
41
- /**
42
- * Fetches new L2 blocks.
43
- * @param publicClient - The viem public client to use for transaction retrieval.
44
- * @param rollupAddress - The address of the rollup contract.
45
- * @param searchStartBlock - The block number to use for starting the search.
46
- * @param searchEndBlock - The highest block number that we should search up to.
47
- * @param expectedNextL2BlockNum - The next L2 block number that we expect to find.
48
- * @returns An array of block; as well as the next eth block to search from.
49
- */ export async function retrieveBlocksFromRollup(rollup, publicClient, blobSinkClient, searchStartBlock, searchEndBlock, logger = createLogger('archiver')) {
50
- const retrievedBlocks = [];
51
- let rollupConstants;
52
- do {
53
- if (searchStartBlock > searchEndBlock) {
54
- break;
55
- }
56
- const l2BlockProposedLogs = (await rollup.getEvents.L2BlockProposed({}, {
57
- fromBlock: searchStartBlock,
58
- toBlock: searchEndBlock
59
- })).filter((log)=>log.blockNumber >= searchStartBlock && log.blockNumber <= searchEndBlock);
60
- if (l2BlockProposedLogs.length === 0) {
61
- break;
62
- }
63
- const lastLog = l2BlockProposedLogs[l2BlockProposedLogs.length - 1];
64
- logger.debug(`Got ${l2BlockProposedLogs.length} L2 block processed logs for L2 blocks ${l2BlockProposedLogs[0].args.blockNumber}-${lastLog.args.blockNumber} between L1 blocks ${searchStartBlock}-${searchEndBlock}`);
65
- if (rollupConstants === undefined) {
66
- const [chainId, version, targetCommitteeSize] = await Promise.all([
67
- publicClient.getChainId(),
68
- rollup.read.getVersion(),
69
- rollup.read.getTargetCommitteeSize()
70
- ]);
71
- rollupConstants = {
72
- chainId: new Fr(chainId),
73
- version: new Fr(version),
74
- targetCommitteeSize: Number(targetCommitteeSize)
75
- };
76
- }
77
- const newBlocks = await processL2BlockProposedLogs(rollup, publicClient, blobSinkClient, l2BlockProposedLogs, rollupConstants, logger);
78
- retrievedBlocks.push(...newBlocks);
79
- searchStartBlock = lastLog.blockNumber + 1n;
80
- }while (searchStartBlock <= searchEndBlock)
81
- // The asyncpool from processL2BlockProposedLogs will not necessarily return the blocks in order, so we sort them before returning.
82
- return retrievedBlocks.sort((a, b)=>Number(a.l1.blockNumber - b.l1.blockNumber));
83
- }
84
- /**
85
- * Processes newly received L2BlockProposed logs.
86
- * @param rollup - The rollup contract
87
- * @param publicClient - The viem public client to use for transaction retrieval.
88
- * @param logs - L2BlockProposed logs.
89
- * @returns - An array blocks.
90
- */ async function processL2BlockProposedLogs(rollup, publicClient, blobSinkClient, logs, { chainId, version, targetCommitteeSize }, logger) {
91
- const retrievedBlocks = [];
92
- await asyncPool(10, logs, async (log)=>{
93
- const l2BlockNumber = Number(log.args.blockNumber);
94
- const archive = log.args.archive;
95
- const archiveFromChain = await rollup.read.archiveAt([
96
- BigInt(l2BlockNumber)
97
- ]);
98
- const blobHashes = log.args.versionedBlobHashes.map((blobHash)=>Buffer.from(blobHash.slice(2), 'hex'));
99
- // The value from the event and contract will match only if the block is in the chain.
100
- if (archive === archiveFromChain) {
101
- const block = await getBlockFromRollupTx(publicClient, blobSinkClient, log.transactionHash, blobHashes, l2BlockNumber, rollup.address, targetCommitteeSize, logger);
102
- const l1 = {
103
- blockNumber: log.blockNumber,
104
- blockHash: log.blockHash,
105
- timestamp: await getL1BlockTime(publicClient, log.blockNumber)
106
- };
107
- retrievedBlocks.push({
108
- ...block,
109
- l1,
110
- chainId,
111
- version
112
- });
113
- logger.trace(`Retrieved L2 block ${l2BlockNumber} from L1 tx ${log.transactionHash}`, {
114
- l1BlockNumber: log.blockNumber,
115
- l2BlockNumber,
116
- archive: archive.toString(),
117
- attestations: block.attestations
118
- });
119
- } else {
120
- logger.warn(`Ignoring L2 block ${l2BlockNumber} due to archive root mismatch`, {
121
- actual: archive,
122
- expected: archiveFromChain
123
- });
124
- }
125
- });
126
- return retrievedBlocks;
127
- }
128
- export async function getL1BlockTime(publicClient, blockNumber) {
129
- const block = await publicClient.getBlock({
130
- blockNumber,
131
- includeTransactions: false
132
- });
133
- return block.timestamp;
134
- }
135
- /**
136
- * Extracts the first 'propose' method calldata from a multicall3 transaction's data.
137
- * @param multicall3Data - The multicall3 transaction input data
138
- * @param rollupAddress - The address of the rollup contract
139
- * @returns The calldata for the first 'propose' method call to the rollup contract
140
- */ function extractRollupProposeCalldata(multicall3Data, rollupAddress) {
141
- const { functionName: multicall3FunctionName, args: multicall3Args } = decodeFunctionData({
142
- abi: multicall3Abi,
143
- data: multicall3Data
144
- });
145
- if (multicall3FunctionName !== 'aggregate3') {
146
- throw new Error(`Unexpected multicall3 method called ${multicall3FunctionName}`);
147
- }
148
- if (multicall3Args.length !== 1) {
149
- throw new Error(`Unexpected number of arguments for multicall3`);
150
- }
151
- const [calls] = multicall3Args;
152
- // Find all rollup calls
153
- const rollupAddressLower = rollupAddress.toLowerCase();
154
- for(let i = 0; i < calls.length; i++){
155
- const addr = calls[i].target;
156
- if (addr.toLowerCase() !== rollupAddressLower) {
157
- continue;
158
- }
159
- const callData = calls[i].callData;
160
- try {
161
- const { functionName: rollupFunctionName } = decodeFunctionData({
162
- abi: RollupAbi,
163
- data: callData
164
- });
165
- if (rollupFunctionName === 'propose') {
166
- return callData;
167
- }
168
- } catch {
169
- continue;
170
- }
171
- }
172
- throw new Error(`Rollup address not found in multicall3 args`);
173
- }
174
- /**
175
- * Gets block from the calldata of an L1 transaction.
176
- * Assumes that the block was published from an EOA.
177
- * TODO: Add retries and error management.
178
- * @param publicClient - The viem public client to use for transaction retrieval.
179
- * @param txHash - Hash of the tx that published it.
180
- * @param l2BlockNumber - L2 block number.
181
- * @returns L2 block from the calldata, deserialized
182
- */ async function getBlockFromRollupTx(publicClient, blobSinkClient, txHash, blobHashes, l2BlockNumber, rollupAddress, targetCommitteeSize, logger) {
183
- const { input: forwarderData, blockHash } = await publicClient.getTransaction({
184
- hash: txHash
185
- });
186
- const rollupData = extractRollupProposeCalldata(forwarderData, rollupAddress);
187
- const { functionName: rollupFunctionName, args: rollupArgs } = decodeFunctionData({
188
- abi: RollupAbi,
189
- data: rollupData
190
- });
191
- if (rollupFunctionName !== 'propose') {
192
- throw new Error(`Unexpected rollup method called ${rollupFunctionName}`);
193
- }
194
- const [decodedArgs, packedAttestations, _signers, _blobInput] = rollupArgs;
195
- const attestations = CommitteeAttestation.fromPacked(packedAttestations, targetCommitteeSize);
196
- logger.trace(`Recovered propose calldata from tx ${txHash}`, {
197
- l2BlockNumber,
198
- archive: decodedArgs.archive,
199
- stateReference: decodedArgs.stateReference,
200
- header: decodedArgs.header,
201
- blobHashes,
202
- attestations,
203
- packedAttestations,
204
- targetCommitteeSize
205
- });
206
- // TODO(md): why is the proposed block header different to the actual block header?
207
- // This is likely going to be a footgun
208
- const header = ProposedBlockHeader.fromViem(decodedArgs.header);
209
- const blobBodies = await blobSinkClient.getBlobSidecar(blockHash, blobHashes);
210
- if (blobBodies.length === 0) {
211
- throw new NoBlobBodiesFoundError(l2BlockNumber);
212
- }
213
- let blockFields;
214
- try {
215
- blockFields = Blob.toEncodedFields(blobBodies.map((b)=>b.blob));
216
- } catch (err) {
217
- if (err instanceof BlobDeserializationError) {
218
- logger.fatal(err.message);
219
- } else {
220
- logger.fatal('Unable to sync: failed to decode fetched blob, this blob was likely not created by us');
221
- }
222
- throw err;
223
- }
224
- // The blob source gives us blockFields, and we must construct the body from them:
225
- const body = Body.fromBlobFields(blockFields);
226
- const archiveRoot = new Fr(Buffer.from(hexToBytes(decodedArgs.archive)));
227
- const stateReference = StateReference.fromViem(decodedArgs.stateReference);
228
- return {
229
- l2BlockNumber,
230
- archiveRoot,
231
- stateReference,
232
- header,
233
- body,
234
- attestations
235
- };
236
- }
237
- /** Given an L1 to L2 message, retrieves its corresponding event from the Inbox within a specific block range. */ export async function retrieveL1ToL2Message(inbox, leaf, fromBlock, toBlock) {
238
- const logs = await inbox.getEvents.MessageSent({
239
- hash: leaf.toString()
240
- }, {
241
- fromBlock,
242
- toBlock
243
- });
244
- const messages = mapLogsInboxMessage(logs);
245
- return messages.length > 0 ? messages[0] : undefined;
246
- }
247
- /**
248
- * Fetch L1 to L2 messages.
249
- * @param publicClient - The viem public client to use for transaction retrieval.
250
- * @param inboxAddress - The address of the inbox contract to fetch messages from.
251
- * @param blockUntilSynced - If true, blocks until the archiver has fully synced.
252
- * @param searchStartBlock - The block number to use for starting the search.
253
- * @param searchEndBlock - The highest block number that we should search up to.
254
- * @returns An array of InboxLeaf and next eth block to search from.
255
- */ export async function retrieveL1ToL2Messages(inbox, searchStartBlock, searchEndBlock) {
256
- const retrievedL1ToL2Messages = [];
257
- while(searchStartBlock <= searchEndBlock){
258
- const messageSentLogs = (await inbox.getEvents.MessageSent({}, {
259
- fromBlock: searchStartBlock,
260
- toBlock: searchEndBlock
261
- })).filter((log)=>log.blockNumber >= searchStartBlock && log.blockNumber <= searchEndBlock);
262
- if (messageSentLogs.length === 0) {
263
- break;
264
- }
265
- retrievedL1ToL2Messages.push(...mapLogsInboxMessage(messageSentLogs));
266
- searchStartBlock = messageSentLogs.at(-1).blockNumber + 1n;
267
- }
268
- return retrievedL1ToL2Messages;
269
- }
270
- function mapLogsInboxMessage(logs) {
271
- return logs.map((log)=>{
272
- const { index, hash, l2BlockNumber, rollingHash } = log.args;
273
- return {
274
- index: index,
275
- leaf: Fr.fromHexString(hash),
276
- l1BlockNumber: log.blockNumber,
277
- l1BlockHash: Buffer32.fromString(log.blockHash),
278
- l2BlockNumber: Number(l2BlockNumber),
279
- rollingHash: Buffer16.fromString(rollingHash)
280
- };
281
- });
282
- }
283
- /** Retrieves L2ProofVerified events from the rollup contract. */ export async function retrieveL2ProofVerifiedEvents(publicClient, rollupAddress, searchStartBlock, searchEndBlock) {
284
- const logs = await publicClient.getLogs({
285
- address: rollupAddress.toString(),
286
- fromBlock: searchStartBlock,
287
- toBlock: searchEndBlock ? searchEndBlock : undefined,
288
- strict: true,
289
- event: getAbiItem({
290
- abi: RollupAbi,
291
- name: 'L2ProofVerified'
292
- })
293
- });
294
- return logs.map((log)=>({
295
- l1BlockNumber: log.blockNumber,
296
- l2BlockNumber: Number(log.args.blockNumber),
297
- proverId: Fr.fromHexString(log.args.proverId),
298
- txHash: log.transactionHash
299
- }));
300
- }
301
- /** Retrieve submitted proofs from the rollup contract */ export async function retrieveL2ProofsFromRollup(publicClient, rollupAddress, searchStartBlock, searchEndBlock) {
302
- const logs = await retrieveL2ProofVerifiedEvents(publicClient, rollupAddress, searchStartBlock, searchEndBlock);
303
- const retrievedData = [];
304
- const lastProcessedL1BlockNumber = logs.length > 0 ? logs.at(-1).l1BlockNumber : searchStartBlock - 1n;
305
- for (const { txHash, proverId, l2BlockNumber } of logs){
306
- const proofData = await getProofFromSubmitProofTx(publicClient, txHash, proverId);
307
- retrievedData.push({
308
- proof: proofData.proof,
309
- proverId: proofData.proverId,
310
- l2BlockNumber,
311
- txHash
312
- });
313
- }
314
- return {
315
- retrievedData,
316
- lastProcessedL1BlockNumber
317
- };
318
- }
319
- /**
320
- * Gets block metadata (header and archive snapshot) from the calldata of an L1 transaction.
321
- * Assumes that the block was published from an EOA.
322
- * TODO: Add retries and error management.
323
- * @param publicClient - The viem public client to use for transaction retrieval.
324
- * @param txHash - Hash of the tx that published it.
325
- * @param l2BlockNum - L2 block number.
326
- * @returns L2 block metadata (header and archive) from the calldata, deserialized
327
- */ export async function getProofFromSubmitProofTx(publicClient, txHash, expectedProverId) {
328
- const { input: data } = await publicClient.getTransaction({
329
- hash: txHash
330
- });
331
- const { functionName, args } = decodeFunctionData({
332
- abi: RollupAbi,
333
- data
334
- });
335
- let proverId;
336
- let archiveRoot;
337
- let proof;
338
- if (functionName === 'submitEpochRootProof') {
339
- const [decodedArgs] = args;
340
- proverId = Fr.fromHexString(decodedArgs.args.proverId);
341
- archiveRoot = Fr.fromHexString(decodedArgs.args.endArchive);
342
- proof = Proof.fromBuffer(Buffer.from(hexToBytes(decodedArgs.proof)));
343
- } else {
344
- throw new Error(`Unexpected proof method called ${functionName}`);
345
- }
346
- if (!proverId.equals(expectedProverId)) {
347
- throw new Error(`Prover ID mismatch: expected ${expectedProverId} but got ${proverId}`);
348
- }
349
- return {
350
- proverId,
351
- archiveRoot,
352
- proof
353
- };
354
- }