@aztec/archiver 0.0.1-commit.b655e406 → 0.0.1-commit.c0b82b2

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 (214) hide show
  1. package/README.md +156 -22
  2. package/dest/archiver.d.ts +139 -0
  3. package/dest/archiver.d.ts.map +1 -0
  4. package/dest/archiver.js +699 -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} +26 -5
  8. package/dest/errors.d.ts +41 -0
  9. package/dest/errors.d.ts.map +1 -0
  10. package/dest/errors.js +62 -0
  11. package/dest/factory.d.ts +9 -7
  12. package/dest/factory.d.ts.map +1 -1
  13. package/dest/factory.js +94 -13
  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 +402 -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/l1/data_retrieval.js +314 -0
  29. package/dest/l1/debug_tx.d.ts +19 -0
  30. package/dest/l1/debug_tx.d.ts.map +1 -0
  31. package/dest/l1/debug_tx.js +73 -0
  32. package/dest/l1/spire_proposer.d.ts +70 -0
  33. package/dest/l1/spire_proposer.d.ts.map +1 -0
  34. package/dest/l1/spire_proposer.js +149 -0
  35. package/dest/l1/trace_tx.d.ts +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 +89 -0
  45. package/dest/modules/data_source_base.d.ts.map +1 -0
  46. package/dest/modules/data_source_base.js +216 -0
  47. package/dest/modules/data_store_updater.d.ts +80 -0
  48. package/dest/modules/data_store_updater.d.ts.map +1 -0
  49. package/dest/modules/data_store_updater.js +323 -0
  50. package/dest/modules/instrumentation.d.ts +50 -0
  51. package/dest/modules/instrumentation.d.ts.map +1 -0
  52. package/dest/{archiver → modules}/instrumentation.js +49 -62
  53. package/dest/modules/l1_synchronizer.d.ts +71 -0
  54. package/dest/modules/l1_synchronizer.d.ts.map +1 -0
  55. package/dest/modules/l1_synchronizer.js +1117 -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 +35 -21
  59. package/dest/store/block_store.d.ts +196 -0
  60. package/dest/store/block_store.d.ts.map +1 -0
  61. package/dest/store/block_store.js +773 -0
  62. package/dest/store/contract_class_store.d.ts +18 -0
  63. package/dest/store/contract_class_store.d.ts.map +1 -0
  64. package/dest/{archiver/kv_archiver_store → store}/contract_class_store.js +13 -9
  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 +1 -1
  68. package/dest/store/kv_archiver_store.d.ts +354 -0
  69. package/dest/store/kv_archiver_store.d.ts.map +1 -0
  70. package/dest/store/kv_archiver_store.js +464 -0
  71. package/dest/store/l2_tips_cache.d.ts +19 -0
  72. package/dest/store/l2_tips_cache.d.ts.map +1 -0
  73. package/dest/store/l2_tips_cache.js +89 -0
  74. package/dest/store/log_store.d.ts +54 -0
  75. package/dest/store/log_store.d.ts.map +1 -0
  76. package/dest/store/log_store.js +456 -0
  77. package/dest/store/message_store.d.ts +40 -0
  78. package/dest/store/message_store.d.ts.map +1 -0
  79. package/dest/{archiver/kv_archiver_store → store}/message_store.js +15 -14
  80. package/dest/structs/data_retrieval.d.ts +27 -0
  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 +195 -0
  88. package/dest/test/fake_l1_state.d.ts.map +1 -0
  89. package/dest/test/fake_l1_state.js +421 -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 +21 -11
  99. package/dest/test/mock_l2_block_source.d.ts +54 -20
  100. package/dest/test/mock_l2_block_source.d.ts.map +1 -1
  101. package/dest/test/mock_l2_block_source.js +245 -82
  102. package/dest/test/mock_structs.d.ts +80 -4
  103. package/dest/test/mock_structs.d.ts.map +1 -1
  104. package/dest/test/mock_structs.js +145 -11
  105. package/dest/test/noop_l1_archiver.d.ts +23 -0
  106. package/dest/test/noop_l1_archiver.d.ts.map +1 -0
  107. package/dest/test/noop_l1_archiver.js +68 -0
  108. package/package.json +20 -20
  109. package/src/archiver.ts +443 -0
  110. package/src/{archiver/config.ts → config.ts} +33 -12
  111. package/src/errors.ts +102 -0
  112. package/src/factory.ts +142 -13
  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 +511 -0
  118. package/src/l1/data_retrieval.ts +493 -0
  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 +328 -0
  125. package/src/modules/data_store_updater.ts +448 -0
  126. package/src/{archiver → modules}/instrumentation.ts +61 -64
  127. package/src/modules/l1_synchronizer.ts +933 -0
  128. package/src/modules/validation.ts +129 -0
  129. package/src/store/block_store.ts +1015 -0
  130. package/src/{archiver/kv_archiver_store → store}/contract_class_store.ts +13 -9
  131. package/src/{archiver/kv_archiver_store → store}/contract_instance_store.ts +2 -2
  132. package/src/store/kv_archiver_store.ts +671 -0
  133. package/src/store/l2_tips_cache.ts +89 -0
  134. package/src/store/log_store.ts +637 -0
  135. package/src/{archiver/kv_archiver_store → store}/message_store.ts +21 -18
  136. package/src/{archiver/structs → structs}/inbox_message.ts +8 -8
  137. package/src/structs/published.ts +1 -0
  138. package/src/test/fake_l1_state.ts +657 -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 +18 -11
  149. package/src/test/mock_l2_block_source.ts +299 -92
  150. package/src/test/mock_structs.ts +275 -13
  151. package/src/test/noop_l1_archiver.ts +109 -0
  152. package/dest/archiver/archiver.d.ts +0 -277
  153. package/dest/archiver/archiver.d.ts.map +0 -1
  154. package/dest/archiver/archiver.js +0 -1322
  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 -1288
  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/data_retrieval.js +0 -362
  166. package/dest/archiver/errors.d.ts +0 -12
  167. package/dest/archiver/errors.d.ts.map +0 -1
  168. package/dest/archiver/errors.js +0 -17
  169. package/dest/archiver/index.d.ts +0 -7
  170. package/dest/archiver/index.d.ts.map +0 -1
  171. package/dest/archiver/index.js +0 -4
  172. package/dest/archiver/instrumentation.d.ts +0 -35
  173. package/dest/archiver/instrumentation.d.ts.map +0 -1
  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_instance_store.d.ts +0 -24
  180. package/dest/archiver/kv_archiver_store/contract_instance_store.d.ts.map +0 -1
  181. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts +0 -168
  182. package/dest/archiver/kv_archiver_store/kv_archiver_store.d.ts.map +0 -1
  183. package/dest/archiver/kv_archiver_store/kv_archiver_store.js +0 -296
  184. package/dest/archiver/kv_archiver_store/log_store.d.ts +0 -49
  185. package/dest/archiver/kv_archiver_store/log_store.d.ts.map +0 -1
  186. package/dest/archiver/kv_archiver_store/log_store.js +0 -336
  187. package/dest/archiver/kv_archiver_store/message_store.d.ts +0 -39
  188. package/dest/archiver/kv_archiver_store/message_store.d.ts.map +0 -1
  189. package/dest/archiver/structs/data_retrieval.d.ts +0 -27
  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 -2
  194. package/dest/archiver/structs/published.d.ts.map +0 -1
  195. package/dest/archiver/validation.d.ts +0 -11
  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 -1722
  201. package/src/archiver/archiver_store.ts +0 -305
  202. package/src/archiver/archiver_store_test_suite.ts +0 -1263
  203. package/src/archiver/data_retrieval.ts +0 -545
  204. package/src/archiver/errors.ts +0 -26
  205. package/src/archiver/index.ts +0 -6
  206. package/src/archiver/kv_archiver_store/block_store.ts +0 -481
  207. package/src/archiver/kv_archiver_store/kv_archiver_store.ts +0 -422
  208. package/src/archiver/kv_archiver_store/log_store.ts +0 -406
  209. package/src/archiver/structs/published.ts +0 -1
  210. package/src/archiver/validation.ts +0 -99
  211. package/src/rpc/index.ts +0 -16
  212. /package/dest/{archiver/structs → structs}/data_retrieval.js +0 -0
  213. /package/dest/{archiver/structs → structs}/published.js +0 -0
  214. /package/src/{archiver/structs → structs}/data_retrieval.ts +0 -0
@@ -0,0 +1,314 @@
1
+ import { BlobDeserializationError, SpongeBlob, decodeCheckpointBlobDataFromBlobs, encodeBlockBlobData } from '@aztec/blob-lib';
2
+ import { asyncPool } from '@aztec/foundation/async-pool';
3
+ import { CheckpointNumber, IndexWithinCheckpoint } from '@aztec/foundation/branded-types';
4
+ import { Fr } from '@aztec/foundation/curves/bn254';
5
+ import { EthAddress } from '@aztec/foundation/eth-address';
6
+ import { createLogger } from '@aztec/foundation/log';
7
+ import { RollupAbi } from '@aztec/l1-artifacts';
8
+ import { Body, L2Block } from '@aztec/stdlib/block';
9
+ import { Checkpoint, L1PublishedData, PublishedCheckpoint } from '@aztec/stdlib/checkpoint';
10
+ import { Proof } from '@aztec/stdlib/proofs';
11
+ import { AppendOnlyTreeSnapshot } from '@aztec/stdlib/trees';
12
+ import { BlockHeader, GlobalVariables, PartialStateReference, StateReference } from '@aztec/stdlib/tx';
13
+ import { decodeFunctionData, getAbiItem, hexToBytes } from 'viem';
14
+ import { NoBlobBodiesFoundError } from '../errors.js';
15
+ import { CalldataRetriever } from './calldata_retriever.js';
16
+ export async function retrievedToPublishedCheckpoint({ checkpointNumber, archiveRoot, feeAssetPriceModifier, header: checkpointHeader, checkpointBlobData, l1, chainId, version, attestations }) {
17
+ const { blocks: blocksBlobData } = checkpointBlobData;
18
+ // The lastArchiveRoot of a block is the new archive for the previous block.
19
+ const newArchiveRoots = blocksBlobData.map((b)=>b.lastArchiveRoot).slice(1).concat([
20
+ archiveRoot
21
+ ]);
22
+ // An error will be thrown from `decodeCheckpointBlobDataFromBlobs` if it can't read a field for the
23
+ // `l1ToL2MessageRoot` of the first block. So below we can safely assume it exists:
24
+ const l1toL2MessageTreeRoot = blocksBlobData[0].l1ToL2MessageRoot;
25
+ const spongeBlob = SpongeBlob.init();
26
+ const l2Blocks = [];
27
+ for(let i = 0; i < blocksBlobData.length; i++){
28
+ const blockBlobData = blocksBlobData[i];
29
+ const { blockEndMarker, blockEndStateField, lastArchiveRoot, noteHashRoot, nullifierRoot, publicDataRoot } = blockBlobData;
30
+ const l2BlockNumber = blockEndMarker.blockNumber;
31
+ const globalVariables = GlobalVariables.from({
32
+ chainId,
33
+ version,
34
+ blockNumber: l2BlockNumber,
35
+ slotNumber: checkpointHeader.slotNumber,
36
+ timestamp: blockEndMarker.timestamp,
37
+ coinbase: checkpointHeader.coinbase,
38
+ feeRecipient: checkpointHeader.feeRecipient,
39
+ gasFees: checkpointHeader.gasFees
40
+ });
41
+ const state = StateReference.from({
42
+ l1ToL2MessageTree: new AppendOnlyTreeSnapshot(l1toL2MessageTreeRoot, blockEndStateField.l1ToL2MessageNextAvailableLeafIndex),
43
+ partial: PartialStateReference.from({
44
+ noteHashTree: new AppendOnlyTreeSnapshot(noteHashRoot, blockEndStateField.noteHashNextAvailableLeafIndex),
45
+ nullifierTree: new AppendOnlyTreeSnapshot(nullifierRoot, blockEndStateField.nullifierNextAvailableLeafIndex),
46
+ publicDataTree: new AppendOnlyTreeSnapshot(publicDataRoot, blockEndStateField.publicDataNextAvailableLeafIndex)
47
+ })
48
+ });
49
+ const body = Body.fromTxBlobData(blockBlobData.txs);
50
+ const blobFields = encodeBlockBlobData(blockBlobData);
51
+ await spongeBlob.absorb(blobFields);
52
+ const clonedSpongeBlob = spongeBlob.clone();
53
+ const spongeBlobHash = await clonedSpongeBlob.squeeze();
54
+ const header = BlockHeader.from({
55
+ lastArchive: new AppendOnlyTreeSnapshot(lastArchiveRoot, l2BlockNumber),
56
+ state,
57
+ spongeBlobHash,
58
+ globalVariables,
59
+ totalFees: body.txEffects.reduce((accum, txEffect)=>accum.add(txEffect.transactionFee), Fr.ZERO),
60
+ totalManaUsed: new Fr(blockEndStateField.totalManaUsed)
61
+ });
62
+ const newArchive = new AppendOnlyTreeSnapshot(newArchiveRoots[i], l2BlockNumber + 1);
63
+ l2Blocks.push(new L2Block(newArchive, header, body, checkpointNumber, IndexWithinCheckpoint(i)));
64
+ }
65
+ const lastBlock = l2Blocks.at(-1);
66
+ const checkpoint = Checkpoint.from({
67
+ archive: new AppendOnlyTreeSnapshot(archiveRoot, lastBlock.number + 1),
68
+ header: checkpointHeader,
69
+ blocks: l2Blocks,
70
+ number: checkpointNumber,
71
+ feeAssetPriceModifier: feeAssetPriceModifier
72
+ });
73
+ return PublishedCheckpoint.from({
74
+ checkpoint,
75
+ l1,
76
+ attestations
77
+ });
78
+ }
79
+ /**
80
+ * Fetches new checkpoints.
81
+ * @param rollup - The rollup contract wrapper.
82
+ * @param publicClient - The viem public client to use for transaction retrieval.
83
+ * @param debugClient - The viem debug client to use for trace/debug RPC methods (optional).
84
+ * @param blobClient - The blob client client for fetching blob data.
85
+ * @param searchStartBlock - The block number to use for starting the search.
86
+ * @param searchEndBlock - The highest block number that we should search up to.
87
+ * @param contractAddresses - The contract addresses (governanceProposerAddress, slashFactoryAddress, slashingProposerAddress).
88
+ * @param instrumentation - The archiver instrumentation instance.
89
+ * @param logger - The logger instance.
90
+ * @param isHistoricalSync - Whether this is a historical sync.
91
+ * @returns An array of retrieved checkpoints.
92
+ */ export async function retrieveCheckpointsFromRollup(rollup, publicClient, debugClient, blobClient, searchStartBlock, searchEndBlock, instrumentation, logger = createLogger('archiver'), isHistoricalSync = false) {
93
+ const retrievedCheckpoints = [];
94
+ let rollupConstants;
95
+ do {
96
+ if (searchStartBlock > searchEndBlock) {
97
+ break;
98
+ }
99
+ const checkpointProposedLogs = await rollup.getCheckpointProposedEvents(searchStartBlock, searchEndBlock);
100
+ if (checkpointProposedLogs.length === 0) {
101
+ break;
102
+ }
103
+ const lastLog = checkpointProposedLogs.at(-1);
104
+ logger.debug(`Got ${checkpointProposedLogs.length} processed logs for checkpoints ${checkpointProposedLogs[0].args.checkpointNumber}-${lastLog.args.checkpointNumber} between L1 blocks ${searchStartBlock}-${searchEndBlock}`);
105
+ if (rollupConstants === undefined) {
106
+ const [chainId, version, targetCommitteeSize] = await Promise.all([
107
+ publicClient.getChainId(),
108
+ rollup.getVersion(),
109
+ rollup.getTargetCommitteeSize()
110
+ ]);
111
+ rollupConstants = {
112
+ chainId: new Fr(chainId),
113
+ version: new Fr(version),
114
+ targetCommitteeSize
115
+ };
116
+ }
117
+ const newCheckpoints = await processCheckpointProposedLogs(rollup, publicClient, debugClient, blobClient, checkpointProposedLogs, rollupConstants, instrumentation, logger, isHistoricalSync);
118
+ retrievedCheckpoints.push(...newCheckpoints);
119
+ searchStartBlock = lastLog.l1BlockNumber + 1n;
120
+ }while (searchStartBlock <= searchEndBlock)
121
+ // The asyncPool from processCheckpointProposedLogs will not necessarily return the checkpoints in order, so we sort them before returning.
122
+ return retrievedCheckpoints.sort((a, b)=>Number(a.l1.blockNumber - b.l1.blockNumber));
123
+ }
124
+ /**
125
+ * Processes newly received CheckpointProposed logs.
126
+ * @param rollup - The rollup contract wrapper.
127
+ * @param publicClient - The viem public client to use for transaction retrieval.
128
+ * @param debugClient - The viem debug client to use for trace/debug RPC methods (optional).
129
+ * @param blobClient - The blob client client for fetching blob data.
130
+ * @param logs - CheckpointProposed logs.
131
+ * @param rollupConstants - The rollup constants (chainId, version, targetCommitteeSize).
132
+ * @param instrumentation - The archiver instrumentation instance.
133
+ * @param logger - The logger instance.
134
+ * @param isHistoricalSync - Whether this is a historical sync.
135
+ * @returns An array of retrieved checkpoints.
136
+ */ async function processCheckpointProposedLogs(rollup, publicClient, debugClient, blobClient, logs, { chainId, version, targetCommitteeSize }, instrumentation, logger, isHistoricalSync) {
137
+ const retrievedCheckpoints = [];
138
+ const calldataRetriever = new CalldataRetriever(publicClient, debugClient, targetCommitteeSize, instrumentation, logger, EthAddress.fromString(rollup.address));
139
+ await asyncPool(10, logs, async (log)=>{
140
+ const checkpointNumber = log.args.checkpointNumber;
141
+ const archive = log.args.archive;
142
+ const archiveFromChain = await rollup.archiveAt(checkpointNumber);
143
+ const blobHashes = log.args.versionedBlobHashes;
144
+ // The value from the event and contract will match only if the checkpoint is in the chain.
145
+ if (archive.equals(archiveFromChain)) {
146
+ const expectedHashes = {
147
+ attestationsHash: log.args.attestationsHash.toString(),
148
+ payloadDigest: log.args.payloadDigest.toString()
149
+ };
150
+ const checkpoint = await calldataRetriever.getCheckpointFromRollupTx(log.l1TransactionHash, blobHashes, checkpointNumber, expectedHashes);
151
+ const { timestamp, parentBeaconBlockRoot } = await getL1Block(publicClient, log.l1BlockNumber);
152
+ const l1 = new L1PublishedData(log.l1BlockNumber, timestamp, log.l1BlockHash.toString());
153
+ const checkpointBlobData = await getCheckpointBlobDataFromBlobs(blobClient, checkpoint.blockHash, blobHashes, checkpointNumber, logger, isHistoricalSync, parentBeaconBlockRoot, timestamp);
154
+ retrievedCheckpoints.push({
155
+ ...checkpoint,
156
+ checkpointBlobData,
157
+ l1,
158
+ chainId,
159
+ version
160
+ });
161
+ logger.trace(`Retrieved checkpoint ${checkpointNumber} from L1 tx ${log.l1TransactionHash}`, {
162
+ l1BlockNumber: log.l1BlockNumber,
163
+ checkpointNumber,
164
+ archive: archive.toString(),
165
+ attestations: checkpoint.attestations
166
+ });
167
+ } else {
168
+ logger.warn(`Ignoring checkpoint ${checkpointNumber} due to archive root mismatch`, {
169
+ actual: archive.toString(),
170
+ expected: archiveFromChain.toString()
171
+ });
172
+ }
173
+ });
174
+ return retrievedCheckpoints;
175
+ }
176
+ export async function getL1Block(publicClient, blockNumber) {
177
+ const block = await publicClient.getBlock({
178
+ blockNumber,
179
+ includeTransactions: false
180
+ });
181
+ return {
182
+ timestamp: block.timestamp,
183
+ parentBeaconBlockRoot: block.parentBeaconBlockRoot
184
+ };
185
+ }
186
+ export async function getCheckpointBlobDataFromBlobs(blobClient, blockHash, blobHashes, checkpointNumber, logger, isHistoricalSync, parentBeaconBlockRoot, l1BlockTimestamp) {
187
+ const blobBodies = await blobClient.getBlobSidecar(blockHash, blobHashes, {
188
+ isHistoricalSync,
189
+ parentBeaconBlockRoot,
190
+ l1BlockTimestamp
191
+ });
192
+ if (blobBodies.length === 0) {
193
+ throw new NoBlobBodiesFoundError(checkpointNumber);
194
+ }
195
+ let checkpointBlobData;
196
+ try {
197
+ // Attempt to decode the checkpoint blob data.
198
+ checkpointBlobData = decodeCheckpointBlobDataFromBlobs(blobBodies);
199
+ } catch (err) {
200
+ if (err instanceof BlobDeserializationError) {
201
+ logger.fatal(err.message);
202
+ } else {
203
+ logger.fatal('Unable to sync: failed to decode fetched blob, this blob was likely not created by us');
204
+ }
205
+ // Throwing an error since this is most likely caused by a bug.
206
+ throw err;
207
+ }
208
+ return checkpointBlobData;
209
+ }
210
+ /** 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) {
211
+ const logs = await inbox.getMessageSentEventByHash(leaf.toString(), fromBlock, toBlock);
212
+ const messages = mapLogsInboxMessage(logs);
213
+ return messages.length > 0 ? messages[0] : undefined;
214
+ }
215
+ /**
216
+ * Fetch L1 to L2 messages.
217
+ * @param inbox - The inbox contract wrapper.
218
+ * @param searchStartBlock - The block number to use for starting the search.
219
+ * @param searchEndBlock - The highest block number that we should search up to.
220
+ * @returns An array of InboxLeaf and next eth block to search from.
221
+ */ export async function retrieveL1ToL2Messages(inbox, searchStartBlock, searchEndBlock) {
222
+ const retrievedL1ToL2Messages = [];
223
+ while(searchStartBlock <= searchEndBlock){
224
+ const messageSentLogs = await inbox.getMessageSentEvents(searchStartBlock, searchEndBlock);
225
+ if (messageSentLogs.length === 0) {
226
+ break;
227
+ }
228
+ retrievedL1ToL2Messages.push(...mapLogsInboxMessage(messageSentLogs));
229
+ searchStartBlock = messageSentLogs.at(-1).l1BlockNumber + 1n;
230
+ }
231
+ return retrievedL1ToL2Messages;
232
+ }
233
+ function mapLogsInboxMessage(logs) {
234
+ return logs.map((log)=>({
235
+ index: log.args.index,
236
+ leaf: log.args.leaf,
237
+ l1BlockNumber: log.l1BlockNumber,
238
+ l1BlockHash: log.l1BlockHash,
239
+ checkpointNumber: log.args.checkpointNumber,
240
+ rollingHash: log.args.rollingHash
241
+ }));
242
+ }
243
+ /** Retrieves L2ProofVerified events from the rollup contract. */ export async function retrieveL2ProofVerifiedEvents(publicClient, rollupAddress, searchStartBlock, searchEndBlock) {
244
+ const logs = await publicClient.getLogs({
245
+ address: rollupAddress.toString(),
246
+ fromBlock: searchStartBlock,
247
+ toBlock: searchEndBlock ? searchEndBlock : undefined,
248
+ strict: true,
249
+ event: getAbiItem({
250
+ abi: RollupAbi,
251
+ name: 'L2ProofVerified'
252
+ })
253
+ });
254
+ return logs.map((log)=>({
255
+ l1BlockNumber: log.blockNumber,
256
+ checkpointNumber: CheckpointNumber.fromBigInt(log.args.checkpointNumber),
257
+ proverId: Fr.fromHexString(log.args.proverId),
258
+ txHash: log.transactionHash
259
+ }));
260
+ }
261
+ /** Retrieve submitted proofs from the rollup contract */ export async function retrieveL2ProofsFromRollup(publicClient, rollupAddress, searchStartBlock, searchEndBlock) {
262
+ const logs = await retrieveL2ProofVerifiedEvents(publicClient, rollupAddress, searchStartBlock, searchEndBlock);
263
+ const retrievedData = [];
264
+ const lastProcessedL1BlockNumber = logs.length > 0 ? logs.at(-1).l1BlockNumber : searchStartBlock - 1n;
265
+ for (const { txHash, proverId, checkpointNumber } of logs){
266
+ const proofData = await getProofFromSubmitProofTx(publicClient, txHash, proverId);
267
+ retrievedData.push({
268
+ proof: proofData.proof,
269
+ proverId: proofData.proverId,
270
+ checkpointNumber,
271
+ txHash
272
+ });
273
+ }
274
+ return {
275
+ retrievedData,
276
+ lastProcessedL1BlockNumber
277
+ };
278
+ }
279
+ /**
280
+ * Gets epoch proof metadata (archive root and proof) from the calldata of an L1 transaction.
281
+ * Assumes that the block was published from an EOA.
282
+ * TODO: Add retries and error management.
283
+ * @param publicClient - The viem public client to use for transaction retrieval.
284
+ * @param txHash - Hash of the tx that published it.
285
+ * @param expectedProverId - Expected prover ID.
286
+ * @returns Epoch proof metadata from the calldata, deserialized.
287
+ */ export async function getProofFromSubmitProofTx(publicClient, txHash, expectedProverId) {
288
+ const { input: data } = await publicClient.getTransaction({
289
+ hash: txHash
290
+ });
291
+ const { functionName, args } = decodeFunctionData({
292
+ abi: RollupAbi,
293
+ data
294
+ });
295
+ let proverId;
296
+ let archiveRoot;
297
+ let proof;
298
+ if (functionName === 'submitEpochRootProof') {
299
+ const [decodedArgs] = args;
300
+ proverId = Fr.fromHexString(decodedArgs.args.proverId);
301
+ archiveRoot = Fr.fromHexString(decodedArgs.args.endArchive);
302
+ proof = Proof.fromBuffer(Buffer.from(hexToBytes(decodedArgs.proof)));
303
+ } else {
304
+ throw new Error(`Unexpected proof method called ${functionName}`);
305
+ }
306
+ if (!proverId.equals(expectedProverId)) {
307
+ throw new Error(`Prover ID mismatch: expected ${expectedProverId} but got ${proverId}`);
308
+ }
309
+ return {
310
+ proverId,
311
+ archiveRoot,
312
+ proof
313
+ };
314
+ }
@@ -0,0 +1,19 @@
1
+ import type { DebugCallTrace, ViemPublicDebugClient } from '@aztec/ethereum/types';
2
+ import { EthAddress } from '@aztec/foundation/eth-address';
3
+ import type { Logger } from '@aztec/foundation/log';
4
+ import { type ZodFor } from '@aztec/foundation/schemas';
5
+ import type { Hex } from 'viem';
6
+ import type { CallInfo } from './types.js';
7
+ /** Zod schema for validating call trace from debug_traceTransaction */
8
+ export declare const callTraceSchema: ZodFor<DebugCallTrace>;
9
+ /**
10
+ * Traces a transaction and extracts all CALL operations to a specific contract and function selector.
11
+ *
12
+ * @param client - The Viem public client
13
+ * @param txHash - The transaction hash to trace
14
+ * @param targetAddress - The contract address to filter for
15
+ * @param functionSelector - The 4-byte function selector to filter for (with or without 0x prefix)
16
+ * @returns Array of CallInfo objects containing from, gasUsed, input, and value for matching calls
17
+ */
18
+ export declare function getSuccessfulCallsFromDebug(client: ViemPublicDebugClient, txHash: Hex, targetAddress: EthAddress, functionSelector: string, logger?: Logger): Promise<CallInfo[]>;
19
+ //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVidWdfdHguZC50cyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3NyYy9sMS9kZWJ1Z190eC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssRUFBRSxjQUFjLEVBQUUscUJBQXFCLEVBQUUsTUFBTS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;AAE3C,uEAAuE;AACvE,eAAO,MAAM,eAAe,EAAE,MAAM,CAAC,cAAc,CAalD,CAAC;AAEF;;;;;;;;GAQG;AACH,wBAAsB,2BAA2B,CAC/C,MAAM,EAAE,qBAAqB,EAC7B,MAAM,EAAE,GAAG,EACX,aAAa,EAAE,UAAU,EACzB,gBAAgB,EAAE,MAAM,EACxB,MAAM,CAAC,EAAE,MAAM,GACd,OAAO,CAAC,QAAQ,EAAE,CAAC,CAwDrB"}
@@ -0,0 +1,73 @@
1
+ import { EthAddress } from '@aztec/foundation/eth-address';
2
+ import { schemas } from '@aztec/foundation/schemas';
3
+ import { withHexPrefix } from '@aztec/foundation/string';
4
+ import { z } from 'zod';
5
+ /** Zod schema for validating call trace from debug_traceTransaction */ export const callTraceSchema = z.lazy(()=>z.object({
6
+ from: schemas.HexStringWith0x,
7
+ to: schemas.HexStringWith0x.optional(),
8
+ type: z.string(),
9
+ input: schemas.HexStringWith0x.optional(),
10
+ output: schemas.HexStringWith0x.optional(),
11
+ gas: schemas.HexStringWith0x.optional(),
12
+ gasUsed: schemas.HexStringWith0x.optional(),
13
+ value: schemas.HexStringWith0x.optional(),
14
+ error: z.string().optional(),
15
+ calls: z.array(callTraceSchema).optional()
16
+ }));
17
+ /**
18
+ * Traces a transaction and extracts all CALL operations to a specific contract and function selector.
19
+ *
20
+ * @param client - The Viem public client
21
+ * @param txHash - The transaction hash to trace
22
+ * @param targetAddress - The contract address to filter for
23
+ * @param functionSelector - The 4-byte function selector to filter for (with or without 0x prefix)
24
+ * @returns Array of CallInfo objects containing from, gasUsed, input, and value for matching calls
25
+ */ export async function getSuccessfulCallsFromDebug(client, txHash, targetAddress, functionSelector, logger) {
26
+ // Normalize inputs for comparison
27
+ const normalizedTarget = targetAddress.toString().toLowerCase();
28
+ const normalizedSelector = withHexPrefix(functionSelector.toLowerCase());
29
+ // Call debug_traceTransaction with callTracer
30
+ // Using 'any' here because debug_traceTransaction is not in viem's standard RPC types
31
+ const rawTrace = await client.request({
32
+ method: 'debug_traceTransaction',
33
+ params: [
34
+ txHash,
35
+ {
36
+ tracer: 'callTracer'
37
+ }
38
+ ]
39
+ });
40
+ if (rawTrace === null || rawTrace === undefined) {
41
+ throw new Error(`Failed to retrieve debug_traceTransaction for ${txHash}`);
42
+ }
43
+ logger?.trace(`Retrieved debug_traceTransaction for ${txHash}`, {
44
+ trace: rawTrace
45
+ });
46
+ // Validate the response with zod
47
+ const trace = callTraceSchema.parse(rawTrace);
48
+ const results = [];
49
+ /**
50
+ * Recursively traverse the call trace tree
51
+ */ function traverseCalls(callTrace) {
52
+ // Skip calls that have errors, and all its descendants
53
+ if (callTrace.error) {
54
+ return;
55
+ }
56
+ // Check if this is a CALL (not DELEGATECALL or STATICCALL) to the target address with matching selector
57
+ if (callTrace.type.toUpperCase() === 'CALL' && callTrace.to?.toLowerCase() === normalizedTarget && callTrace.input?.toLowerCase().startsWith(normalizedSelector)) {
58
+ results.push({
59
+ from: EthAddress.fromString(callTrace.from),
60
+ gasUsed: callTrace.gasUsed === undefined ? undefined : BigInt(callTrace.gasUsed),
61
+ input: callTrace.input,
62
+ value: callTrace.value ? BigInt(callTrace.value) : 0n
63
+ });
64
+ }
65
+ // Recursively process nested calls
66
+ for (const nestedCall of callTrace.calls ?? []){
67
+ traverseCalls(nestedCall);
68
+ }
69
+ }
70
+ // Start traversal from the root trace
71
+ traverseCalls(trace);
72
+ return results;
73
+ }
@@ -0,0 +1,70 @@
1
+ import type { Logger } from '@aztec/foundation/log';
2
+ import { type Hex, type Transaction } from 'viem';
3
+ export declare const SPIRE_PROPOSER_ADDRESS = "0x9ccc2f3ecde026230e11a5c8799ac7524f2bb294";
4
+ export declare const SPIRE_PROPOSER_EXPECTED_IMPLEMENTATION = "0x7d38d47e7c82195e6e607d3b0f1c20c615c7bf42";
5
+ export declare const EIP1967_IMPLEMENTATION_SLOT: "0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc";
6
+ export declare const SpireProposerAbi: readonly [{
7
+ readonly inputs: readonly [{
8
+ readonly components: readonly [{
9
+ readonly internalType: "address";
10
+ readonly name: "proposer";
11
+ readonly type: "address";
12
+ }, {
13
+ readonly internalType: "address";
14
+ readonly name: "target";
15
+ readonly type: "address";
16
+ }, {
17
+ readonly internalType: "bytes";
18
+ readonly name: "data";
19
+ readonly type: "bytes";
20
+ }, {
21
+ readonly internalType: "uint256";
22
+ readonly name: "value";
23
+ readonly type: "uint256";
24
+ }, {
25
+ readonly internalType: "uint256";
26
+ readonly name: "gasLimit";
27
+ readonly type: "uint256";
28
+ }];
29
+ readonly internalType: "struct IProposerMulticall.Call[]";
30
+ readonly name: "_calls";
31
+ readonly type: "tuple[]";
32
+ }];
33
+ readonly name: "multicall";
34
+ readonly outputs: readonly [];
35
+ readonly stateMutability: "nonpayable";
36
+ readonly type: "function";
37
+ }];
38
+ /**
39
+ * Verifies that a proxy contract points to the expected implementation using EIP-1967.
40
+ * @param publicClient - The viem public client
41
+ * @param proxyAddress - The proxy contract address
42
+ * @param expectedImplementation - The expected implementation address
43
+ * @param logger - Logger instance
44
+ * @returns True if the proxy points to the expected implementation
45
+ */
46
+ export declare function verifyProxyImplementation(publicClient: {
47
+ getStorageAt: (params: {
48
+ address: Hex;
49
+ slot: Hex;
50
+ }) => Promise<Hex | undefined>;
51
+ }, proxyAddress: Hex, expectedImplementation: Hex, logger: Logger): Promise<boolean>;
52
+ /**
53
+ * Attempts to decode transaction as a Spire Proposer Multicall.
54
+ * Spire Proposer is a proxy contract that wraps multiple calls.
55
+ * Returns all wrapped calls if validation succeeds (caller handles hash matching to find the propose call).
56
+ * @param tx - The transaction to decode
57
+ * @param publicClient - The viem public client for proxy verification
58
+ * @param logger - Logger instance
59
+ * @returns Array of wrapped calls with 'to' and 'data', or undefined if not a valid Spire Proposer tx
60
+ */
61
+ export declare function getCallsFromSpireProposer(tx: Transaction, publicClient: {
62
+ getStorageAt: (params: {
63
+ address: Hex;
64
+ slot: Hex;
65
+ }) => Promise<Hex |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ClB;AAED;;;;;;;;GAQG;AACH,wBAAsB,yBAAyB,CAC7C,EAAE,EAAE,WAAW,EACf,YAAY,EAAE;IAAE,YAAY,EAAE,CAAC,MAAM,EAAE;QAAE,OAAO,EAAE,GAAG,CAAC;QAAC,IAAI,EAAE,GAAG,CAAA;KAAE,KAAK,OAAO,CAAC,GAAG,GAAG,SAAS,CAAC,CAAA;CAAE,EACjG,MAAM,EAAE,MAAM,GACb,OAAO,CAAC;IAAE,EAAE,EAAE,GAAG,CAAC;IAAC,IAAI,EAAE,GAAG,CAAA;CAAE,EAAE,GAAG,SAAS,CAAC,CAoD/C"}
@@ -0,0 +1,149 @@
1
+ import { EthAddress } from '@aztec/foundation/eth-address';
2
+ import { decodeFunctionData, getAddress, trim } from 'viem';
3
+ // Spire Proposer Multicall constants
4
+ export const SPIRE_PROPOSER_ADDRESS = '0x9ccc2f3ecde026230e11a5c8799ac7524f2bb294';
5
+ export const SPIRE_PROPOSER_EXPECTED_IMPLEMENTATION = '0x7d38d47e7c82195e6e607d3b0f1c20c615c7bf42';
6
+ // EIP-1967 storage slot for implementation address
7
+ // keccak256("eip1967.proxy.implementation") - 1 = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc
8
+ export const EIP1967_IMPLEMENTATION_SLOT = '0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc';
9
+ // Spire Proposer Multicall ABI
10
+ export const SpireProposerAbi = [
11
+ {
12
+ inputs: [
13
+ {
14
+ components: [
15
+ {
16
+ internalType: 'address',
17
+ name: 'proposer',
18
+ type: 'address'
19
+ },
20
+ {
21
+ internalType: 'address',
22
+ name: 'target',
23
+ type: 'address'
24
+ },
25
+ {
26
+ internalType: 'bytes',
27
+ name: 'data',
28
+ type: 'bytes'
29
+ },
30
+ {
31
+ internalType: 'uint256',
32
+ name: 'value',
33
+ type: 'uint256'
34
+ },
35
+ {
36
+ internalType: 'uint256',
37
+ name: 'gasLimit',
38
+ type: 'uint256'
39
+ }
40
+ ],
41
+ internalType: 'struct IProposerMulticall.Call[]',
42
+ name: '_calls',
43
+ type: 'tuple[]'
44
+ }
45
+ ],
46
+ name: 'multicall',
47
+ outputs: [],
48
+ stateMutability: 'nonpayable',
49
+ type: 'function'
50
+ }
51
+ ];
52
+ /**
53
+ * Verifies that a proxy contract points to the expected implementation using EIP-1967.
54
+ * @param publicClient - The viem public client
55
+ * @param proxyAddress - The proxy contract address
56
+ * @param expectedImplementation - The expected implementation address
57
+ * @param logger - Logger instance
58
+ * @returns True if the proxy points to the expected implementation
59
+ */ export async function verifyProxyImplementation(publicClient, proxyAddress, expectedImplementation, logger) {
60
+ try {
61
+ // Read the EIP-1967 implementation slot
62
+ const implementationData = await publicClient.getStorageAt({
63
+ address: proxyAddress,
64
+ slot: EIP1967_IMPLEMENTATION_SLOT
65
+ });
66
+ if (!implementationData) {
67
+ logger.warn(`No implementation found in EIP-1967 slot for proxy ${proxyAddress}`);
68
+ return false;
69
+ }
70
+ // The implementation address is stored in the last 20 bytes of the slot
71
+ // We need to extract and normalize it for comparison
72
+ const implementationAddress = getAddress(trim(implementationData));
73
+ const expectedAddress = getAddress(expectedImplementation);
74
+ const matches = implementationAddress.toLowerCase() === expectedAddress.toLowerCase();
75
+ if (!matches) {
76
+ logger.warn(`Proxy implementation mismatch: expected ${expectedAddress}, got ${implementationAddress}`, {
77
+ proxyAddress,
78
+ expectedImplementation,
79
+ actualImplementation: implementationAddress
80
+ });
81
+ }
82
+ return matches;
83
+ } catch (err) {
84
+ logger.warn(`Failed to verify proxy implementation for ${proxyAddress}: ${err}`);
85
+ return false;
86
+ }
87
+ }
88
+ /**
89
+ * Attempts to decode transaction as a Spire Proposer Multicall.
90
+ * Spire Proposer is a proxy contract that wraps multiple calls.
91
+ * Returns all wrapped calls if validation succeeds (caller handles hash matching to find the propose call).
92
+ * @param tx - The transaction to decode
93
+ * @param publicClient - The viem public client for proxy verification
94
+ * @param logger - Logger instance
95
+ * @returns Array of wrapped calls with 'to' and 'data', or undefined if not a valid Spire Proposer tx
96
+ */ export async function getCallsFromSpireProposer(tx, publicClient, logger) {
97
+ const txHash = tx.hash;
98
+ try {
99
+ // Check if transaction is to the Spire Proposer address
100
+ if (!tx.to || !EthAddress.areEqual(tx.to, SPIRE_PROPOSER_ADDRESS)) {
101
+ logger.debug(`Transaction is not to Spire Proposer address (to: ${tx.to})`, {
102
+ txHash
103
+ });
104
+ return undefined;
105
+ }
106
+ // Verify the proxy points to the expected implementation
107
+ const isValidProxy = await verifyProxyImplementation(publicClient, tx.to, SPIRE_PROPOSER_EXPECTED_IMPLEMENTATION, logger);
108
+ if (!isValidProxy) {
109
+ logger.warn(`Spire Proposer proxy implementation verification failed`, {
110
+ txHash,
111
+ to: tx.to
112
+ });
113
+ return undefined;
114
+ }
115
+ // Try to decode as Spire Proposer multicall
116
+ const { functionName: spireFunctionName, args: spireArgs } = decodeFunctionData({
117
+ abi: SpireProposerAbi,
118
+ data: tx.input
119
+ });
120
+ // If not multicall, return undefined
121
+ if (spireFunctionName !== 'multicall') {
122
+ logger.warn(`Transaction to Spire Proposer is not multicall (got ${spireFunctionName})`, {
123
+ txHash
124
+ });
125
+ return undefined;
126
+ }
127
+ if (spireArgs.length !== 1) {
128
+ logger.warn(`Unexpected number of arguments for Spire Proposer multicall (got ${spireArgs.length})`, {
129
+ txHash
130
+ });
131
+ return undefined;
132
+ }
133
+ const [calls] = spireArgs;
134
+ // Return all wrapped calls (hash matching in the caller determines which is the propose call)
135
+ logger.trace(`Decoded Spire Proposer with ${calls.length} call(s)`, {
136
+ txHash
137
+ });
138
+ return calls.map((call)=>({
139
+ to: call.target,
140
+ data: call.data
141
+ }));
142
+ } catch (err) {
143
+ // Any decoding error triggers fallback to trace
144
+ logger.warn(`Failed to decode Spire Proposer: ${err}`, {
145
+ txHash
146
+ });
147
+ return undefined;
148
+ }
149
+ }