@aztec/ethereum 3.0.0-canary.a9708bd → 3.0.0-devnet.3

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 (144) hide show
  1. package/dest/client.d.ts +1 -1
  2. package/dest/client.d.ts.map +1 -1
  3. package/dest/config.d.ts +11 -6
  4. package/dest/config.d.ts.map +1 -1
  5. package/dest/config.js +124 -64
  6. package/dest/contracts/empire_base.d.ts +1 -1
  7. package/dest/contracts/empire_base.d.ts.map +1 -1
  8. package/dest/contracts/empire_slashing_proposer.d.ts +2 -2
  9. package/dest/contracts/empire_slashing_proposer.d.ts.map +1 -1
  10. package/dest/contracts/empire_slashing_proposer.js +1 -1
  11. package/dest/contracts/fee_asset_handler.d.ts +3 -3
  12. package/dest/contracts/fee_asset_handler.d.ts.map +1 -1
  13. package/dest/contracts/governance.js +7 -3
  14. package/dest/contracts/governance_proposer.d.ts +1 -2
  15. package/dest/contracts/governance_proposer.d.ts.map +1 -1
  16. package/dest/contracts/governance_proposer.js +1 -2
  17. package/dest/contracts/multicall.d.ts +3 -5
  18. package/dest/contracts/multicall.d.ts.map +1 -1
  19. package/dest/contracts/multicall.js +6 -4
  20. package/dest/contracts/rollup.d.ts +39 -19
  21. package/dest/contracts/rollup.d.ts.map +1 -1
  22. package/dest/contracts/rollup.js +84 -88
  23. package/dest/contracts/slasher_contract.d.ts +10 -0
  24. package/dest/contracts/slasher_contract.d.ts.map +1 -1
  25. package/dest/contracts/slasher_contract.js +18 -0
  26. package/dest/contracts/tally_slashing_proposer.d.ts +22 -3
  27. package/dest/contracts/tally_slashing_proposer.d.ts.map +1 -1
  28. package/dest/contracts/tally_slashing_proposer.js +55 -5
  29. package/dest/deploy_l1_contracts.d.ts +22 -7
  30. package/dest/deploy_l1_contracts.d.ts.map +1 -1
  31. package/dest/deploy_l1_contracts.js +555 -362
  32. package/dest/index.d.ts +1 -1
  33. package/dest/index.d.ts.map +1 -1
  34. package/dest/index.js +1 -1
  35. package/dest/l1_artifacts.d.ts +8729 -6014
  36. package/dest/l1_artifacts.d.ts.map +1 -1
  37. package/dest/l1_artifacts.js +10 -5
  38. package/dest/l1_contract_addresses.d.ts +5 -1
  39. package/dest/l1_contract_addresses.d.ts.map +1 -1
  40. package/dest/l1_contract_addresses.js +16 -26
  41. package/dest/l1_reader.d.ts +1 -1
  42. package/dest/l1_reader.d.ts.map +1 -1
  43. package/dest/l1_reader.js +8 -8
  44. package/dest/l1_tx_utils/config.d.ts +59 -0
  45. package/dest/l1_tx_utils/config.d.ts.map +1 -0
  46. package/dest/l1_tx_utils/config.js +73 -0
  47. package/dest/l1_tx_utils/constants.d.ts +6 -0
  48. package/dest/l1_tx_utils/constants.d.ts.map +1 -0
  49. package/dest/l1_tx_utils/constants.js +14 -0
  50. package/dest/l1_tx_utils/factory.d.ts +24 -0
  51. package/dest/l1_tx_utils/factory.d.ts.map +1 -0
  52. package/dest/l1_tx_utils/factory.js +12 -0
  53. package/dest/l1_tx_utils/index.d.ts +10 -0
  54. package/dest/l1_tx_utils/index.d.ts.map +1 -0
  55. package/dest/l1_tx_utils/index.js +10 -0
  56. package/dest/l1_tx_utils/interfaces.d.ts +76 -0
  57. package/dest/l1_tx_utils/interfaces.d.ts.map +1 -0
  58. package/dest/l1_tx_utils/interfaces.js +4 -0
  59. package/dest/l1_tx_utils/l1_tx_utils.d.ts +95 -0
  60. package/dest/l1_tx_utils/l1_tx_utils.d.ts.map +1 -0
  61. package/dest/l1_tx_utils/l1_tx_utils.js +610 -0
  62. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.d.ts +26 -0
  63. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.d.ts.map +1 -0
  64. package/dest/l1_tx_utils/l1_tx_utils_with_blobs.js +26 -0
  65. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts +81 -0
  66. package/dest/l1_tx_utils/readonly_l1_tx_utils.d.ts.map +1 -0
  67. package/dest/l1_tx_utils/readonly_l1_tx_utils.js +294 -0
  68. package/dest/l1_tx_utils/signer.d.ts +4 -0
  69. package/dest/l1_tx_utils/signer.d.ts.map +1 -0
  70. package/dest/l1_tx_utils/signer.js +16 -0
  71. package/dest/l1_tx_utils/types.d.ts +67 -0
  72. package/dest/l1_tx_utils/types.d.ts.map +1 -0
  73. package/dest/l1_tx_utils/types.js +26 -0
  74. package/dest/l1_tx_utils/utils.d.ts +4 -0
  75. package/dest/l1_tx_utils/utils.d.ts.map +1 -0
  76. package/dest/l1_tx_utils/utils.js +14 -0
  77. package/dest/publisher_manager.d.ts +7 -2
  78. package/dest/publisher_manager.d.ts.map +1 -1
  79. package/dest/publisher_manager.js +36 -8
  80. package/dest/queries.d.ts.map +1 -1
  81. package/dest/queries.js +11 -12
  82. package/dest/test/chain_monitor.d.ts +11 -0
  83. package/dest/test/chain_monitor.d.ts.map +1 -1
  84. package/dest/test/chain_monitor.js +81 -12
  85. package/dest/test/delayed_tx_utils.d.ts +2 -2
  86. package/dest/test/delayed_tx_utils.d.ts.map +1 -1
  87. package/dest/test/delayed_tx_utils.js +2 -2
  88. package/dest/test/eth_cheat_codes.d.ts +32 -6
  89. package/dest/test/eth_cheat_codes.d.ts.map +1 -1
  90. package/dest/test/eth_cheat_codes.js +115 -28
  91. package/dest/test/rollup_cheat_codes.d.ts +11 -9
  92. package/dest/test/rollup_cheat_codes.d.ts.map +1 -1
  93. package/dest/test/rollup_cheat_codes.js +38 -6
  94. package/dest/test/upgrade_utils.d.ts.map +1 -1
  95. package/dest/test/upgrade_utils.js +3 -2
  96. package/dest/utils.d.ts.map +1 -1
  97. package/dest/utils.js +10 -161
  98. package/dest/zkPassportVerifierAddress.js +1 -1
  99. package/package.json +7 -7
  100. package/src/client.ts +1 -1
  101. package/src/config.ts +136 -68
  102. package/src/contracts/empire_base.ts +1 -1
  103. package/src/contracts/empire_slashing_proposer.ts +7 -3
  104. package/src/contracts/fee_asset_handler.ts +1 -1
  105. package/src/contracts/governance.ts +3 -3
  106. package/src/contracts/governance_proposer.ts +3 -4
  107. package/src/contracts/multicall.ts +12 -10
  108. package/src/contracts/rollup.ts +104 -106
  109. package/src/contracts/slasher_contract.ts +22 -0
  110. package/src/contracts/tally_slashing_proposer.ts +54 -6
  111. package/src/deploy_l1_contracts.ts +570 -328
  112. package/src/index.ts +1 -1
  113. package/src/l1_artifacts.ts +14 -6
  114. package/src/l1_contract_addresses.ts +17 -26
  115. package/src/l1_reader.ts +9 -9
  116. package/src/l1_tx_utils/README.md +177 -0
  117. package/src/l1_tx_utils/config.ts +140 -0
  118. package/src/l1_tx_utils/constants.ts +18 -0
  119. package/src/l1_tx_utils/factory.ts +64 -0
  120. package/src/l1_tx_utils/index.ts +12 -0
  121. package/src/l1_tx_utils/interfaces.ts +86 -0
  122. package/src/l1_tx_utils/l1_tx_utils.ts +718 -0
  123. package/src/l1_tx_utils/l1_tx_utils_with_blobs.ts +77 -0
  124. package/src/l1_tx_utils/readonly_l1_tx_utils.ts +372 -0
  125. package/src/l1_tx_utils/signer.ts +28 -0
  126. package/src/l1_tx_utils/types.ts +85 -0
  127. package/src/l1_tx_utils/utils.ts +16 -0
  128. package/src/publisher_manager.ts +51 -9
  129. package/src/queries.ts +13 -8
  130. package/src/test/chain_monitor.ts +89 -9
  131. package/src/test/delayed_tx_utils.ts +2 -2
  132. package/src/test/eth_cheat_codes.ts +142 -29
  133. package/src/test/rollup_cheat_codes.ts +54 -14
  134. package/src/test/upgrade_utils.ts +3 -2
  135. package/src/utils.ts +13 -185
  136. package/src/zkPassportVerifierAddress.ts +1 -1
  137. package/dest/l1_tx_utils.d.ts +0 -250
  138. package/dest/l1_tx_utils.d.ts.map +0 -1
  139. package/dest/l1_tx_utils.js +0 -826
  140. package/dest/l1_tx_utils_with_blobs.d.ts +0 -19
  141. package/dest/l1_tx_utils_with_blobs.d.ts.map +0 -1
  142. package/dest/l1_tx_utils_with_blobs.js +0 -85
  143. package/src/l1_tx_utils.ts +0 -1105
  144. package/src/l1_tx_utils_with_blobs.ts +0 -144
@@ -0,0 +1,610 @@
1
+ import { maxBigint } from '@aztec/foundation/bigint';
2
+ import { merge, pick } from '@aztec/foundation/collection';
3
+ import { InterruptError, TimeoutError } from '@aztec/foundation/error';
4
+ import { createLogger } from '@aztec/foundation/log';
5
+ import { retryUntil } from '@aztec/foundation/retry';
6
+ import { sleep } from '@aztec/foundation/sleep';
7
+ import { DateProvider } from '@aztec/foundation/timer';
8
+ import { RollupAbi } from '@aztec/l1-artifacts/RollupAbi';
9
+ import pickBy from 'lodash.pickby';
10
+ import { createNonceManager, formatGwei, serializeTransaction } from 'viem';
11
+ import { jsonRpc } from 'viem/nonce';
12
+ import { formatViemError } from '../utils.js';
13
+ import { l1TxUtilsConfigMappings } from './config.js';
14
+ import { LARGE_GAS_LIMIT } from './constants.js';
15
+ import { ReadOnlyL1TxUtils } from './readonly_l1_tx_utils.js';
16
+ import { DroppedTransactionError, TerminalTxUtilsState, TxUtilsState, UnknownMinedTxError } from './types.js';
17
+ const MAX_L1_TX_STATES = 32;
18
+ export class L1TxUtils extends ReadOnlyL1TxUtils {
19
+ client;
20
+ address;
21
+ signer;
22
+ store;
23
+ metrics;
24
+ nonceManager;
25
+ txs;
26
+ constructor(client, address, signer, logger = createLogger('ethereum:publisher'), dateProvider = new DateProvider(), config, debugMaxGasLimit = false, store, metrics){
27
+ super(client, logger, dateProvider, config, debugMaxGasLimit), this.client = client, this.address = address, this.signer = signer, this.store = store, this.metrics = metrics, this.txs = [];
28
+ this.nonceManager = createNonceManager({
29
+ source: jsonRpc()
30
+ });
31
+ }
32
+ get state() {
33
+ return this.txs.at(-1)?.status ?? TxUtilsState.IDLE;
34
+ }
35
+ get lastMinedAtBlockNumber() {
36
+ const minedBlockNumbers = this.txs.map((tx)=>tx.receipt?.blockNumber).filter((bn)=>bn !== undefined);
37
+ return minedBlockNumbers.length === 0 ? undefined : maxBigint(...minedBlockNumbers);
38
+ }
39
+ async updateState(l1TxState, newState, l1Timestamp) {
40
+ const oldState = l1TxState.status;
41
+ l1TxState.status = newState;
42
+ const sender = this.getSenderAddress().toString();
43
+ this.logger.debug(`Tx state changed from ${TxUtilsState[oldState]} to ${TxUtilsState[newState]} for nonce ${l1TxState.nonce} account ${sender}`);
44
+ // Record metrics
45
+ if (newState === TxUtilsState.MINED && l1Timestamp !== undefined) {
46
+ this.metrics?.recordMinedTx(l1TxState, new Date(l1Timestamp));
47
+ } else if (newState === TxUtilsState.NOT_MINED) {
48
+ this.metrics?.recordDroppedTx(l1TxState);
49
+ }
50
+ // Update state in the store
51
+ await this.store?.saveState(sender, l1TxState).catch((err)=>this.logger.error('Failed to persist L1 tx state', err));
52
+ }
53
+ updateConfig(newConfig) {
54
+ this.config = merge(this.config, newConfig);
55
+ this.logger.info('Updated L1TxUtils config', pickBy(newConfig, (_, key)=>key in l1TxUtilsConfigMappings));
56
+ }
57
+ getSenderAddress() {
58
+ return this.address;
59
+ }
60
+ getSenderBalance() {
61
+ return this.client.getBalance({
62
+ address: this.getSenderAddress().toString()
63
+ });
64
+ }
65
+ /**
66
+ * Rehydrates transaction states from the store and resumes monitoring for pending transactions.
67
+ * This should be called on startup to restore state and resume monitoring of any in-flight transactions.
68
+ */ async loadStateAndResumeMonitoring() {
69
+ if (!this.store) {
70
+ return;
71
+ }
72
+ const account = this.getSenderAddress().toString();
73
+ const loadedStates = await this.store.loadStates(account);
74
+ if (loadedStates.length === 0) {
75
+ this.logger.debug(`No states to rehydrate for account ${account}`);
76
+ return;
77
+ }
78
+ // Convert loaded states (which have id) to the txs format
79
+ this.txs = loadedStates;
80
+ this.logger.info(`Rehydrated ${loadedStates.length} tx states for account ${account}`);
81
+ // Find all pending states and resume monitoring
82
+ const pendingStates = loadedStates.filter((state)=>!TerminalTxUtilsState.includes(state.status));
83
+ if (pendingStates.length === 0) {
84
+ return;
85
+ }
86
+ this.logger.info(`Resuming monitoring for ${pendingStates.length} pending transactions for account ${account}`, {
87
+ txs: pendingStates.map((s)=>({
88
+ id: s.id,
89
+ nonce: s.nonce,
90
+ status: TxUtilsState[s.status]
91
+ }))
92
+ });
93
+ for (const state of pendingStates){
94
+ void this.monitorTransaction(state).catch((err)=>{
95
+ this.logger.error(`Error monitoring rehydrated tx with nonce ${state.nonce} for account ${account}`, formatViemError(err), {
96
+ nonce: state.nonce,
97
+ account
98
+ });
99
+ });
100
+ }
101
+ }
102
+ async signTransaction(txRequest) {
103
+ const signature = await this.signer(txRequest, this.getSenderAddress());
104
+ return serializeTransaction(txRequest, signature);
105
+ }
106
+ async prepareSignedTransaction(txData) {
107
+ const txRequest = await this.client.prepareTransactionRequest(txData);
108
+ return await this.signTransaction(txRequest);
109
+ }
110
+ /**
111
+ * Sends a transaction with gas estimation and pricing
112
+ * @param request - The transaction request (to, data, value)
113
+ * @param gasConfig - Optional gas configuration
114
+ * @returns The transaction hash and parameters used
115
+ */ async sendTransaction(request, gasConfigOverrides, blobInputs, stateChange = TxUtilsState.SENT) {
116
+ if (this.interrupted) {
117
+ throw new InterruptError(`Transaction sending is interrupted`);
118
+ }
119
+ try {
120
+ const gasConfig = merge(this.config, gasConfigOverrides);
121
+ const account = this.getSenderAddress().toString();
122
+ let gasLimit;
123
+ if (this.debugMaxGasLimit) {
124
+ gasLimit = LARGE_GAS_LIMIT;
125
+ } else if (gasConfig.gasLimit) {
126
+ gasLimit = gasConfig.gasLimit;
127
+ } else {
128
+ gasLimit = await this.estimateGas(account, request, gasConfig);
129
+ }
130
+ this.logger.trace(`Computed gas limit ${gasLimit}`, {
131
+ gasLimit,
132
+ ...request
133
+ });
134
+ const gasPrice = await this.getGasPrice(gasConfig, !!blobInputs);
135
+ if (this.interrupted) {
136
+ throw new InterruptError(`Transaction sending is interrupted`);
137
+ }
138
+ const nonce = await this.nonceManager.consume({
139
+ client: this.client,
140
+ address: account,
141
+ chainId: this.client.chain.id
142
+ });
143
+ const baseState = {
144
+ request,
145
+ gasLimit,
146
+ blobInputs,
147
+ gasPrice,
148
+ nonce
149
+ };
150
+ const txData = this.makeTxData(baseState, {
151
+ isCancelTx: false
152
+ });
153
+ const now = new Date(await this.getL1Timestamp());
154
+ if (gasConfig.txTimeoutAt && now > gasConfig.txTimeoutAt) {
155
+ throw new TimeoutError(`Transaction timed out before sending (now ${now.toISOString()} > timeoutAt ${gasConfig.txTimeoutAt.toISOString()})`);
156
+ }
157
+ // Send the new tx
158
+ const signedRequest = await this.prepareSignedTransaction(txData);
159
+ const txHash = await this.client.sendRawTransaction({
160
+ serializedTransaction: signedRequest
161
+ });
162
+ // Create the new state for monitoring
163
+ const l1TxState = {
164
+ ...baseState,
165
+ id: await this.store?.consumeNextStateId(account) ?? Math.max(...this.txs.map((tx)=>tx.id), 0),
166
+ txHashes: [
167
+ txHash
168
+ ],
169
+ cancelTxHashes: [],
170
+ status: TxUtilsState.IDLE,
171
+ txConfigOverrides: gasConfigOverrides ?? {},
172
+ sentAtL1Ts: now,
173
+ lastSentAtL1Ts: now
174
+ };
175
+ // And persist it
176
+ await this.updateState(l1TxState, stateChange);
177
+ await this.store?.saveBlobs(account, l1TxState.id, blobInputs);
178
+ this.txs.push(l1TxState);
179
+ // Clean up stale states
180
+ if (this.txs.length > MAX_L1_TX_STATES) {
181
+ const removed = this.txs.shift();
182
+ if (removed && this.store) {
183
+ await this.store.deleteState(account, removed.id);
184
+ }
185
+ }
186
+ this.logger.info(`Sent L1 transaction ${txHash}`, {
187
+ to: request.to,
188
+ value: request.value,
189
+ nonce,
190
+ account,
191
+ sentAt: now,
192
+ gasLimit,
193
+ maxFeePerGas: formatGwei(gasPrice.maxFeePerGas),
194
+ maxPriorityFeePerGas: formatGwei(gasPrice.maxPriorityFeePerGas),
195
+ ...gasPrice.maxFeePerBlobGas && {
196
+ maxFeePerBlobGas: formatGwei(gasPrice.maxFeePerBlobGas)
197
+ },
198
+ isBlobTx: !!blobInputs,
199
+ txConfig: gasConfigOverrides
200
+ });
201
+ return {
202
+ txHash,
203
+ state: l1TxState
204
+ };
205
+ } catch (err) {
206
+ const viemError = formatViemError(err, request.abi);
207
+ this.logger.error(`Failed to send L1 transaction`, viemError, {
208
+ request: pick(request, 'to', 'value')
209
+ });
210
+ throw viemError;
211
+ }
212
+ }
213
+ async tryGetTxReceipt(txHashes, nonce, isCancelTx) {
214
+ for (const hash of txHashes){
215
+ try {
216
+ const receipt = await this.client.getTransactionReceipt({
217
+ hash
218
+ });
219
+ if (receipt) {
220
+ const account = this.getSenderAddress().toString();
221
+ const what = isCancelTx ? 'Cancellation L1 transaction' : 'L1 transaction';
222
+ if (receipt.status === 'reverted') {
223
+ this.logger.warn(`${what} ${hash} with nonce ${nonce} reverted`, {
224
+ receipt,
225
+ nonce,
226
+ account
227
+ });
228
+ } else {
229
+ this.logger.info(`${what} ${hash} with nonce ${nonce} mined`, {
230
+ receipt,
231
+ nonce,
232
+ account
233
+ });
234
+ }
235
+ return receipt;
236
+ }
237
+ } catch (err) {
238
+ if (err instanceof Error && err.name === 'TransactionReceiptNotFoundError') {
239
+ continue;
240
+ } else {
241
+ this.logger.error(`Error getting receipt for tx ${hash}`, err);
242
+ continue;
243
+ }
244
+ }
245
+ }
246
+ }
247
+ /**
248
+ * Returns whether a given tx should be considered as timed out and no longer monitored.
249
+ * Relies on the txTimeout setting for user txs, and on the txCancellationTimeout for cancel txs.
250
+ * @remarks We check against the latestBlockTimestamp as opposed to the current time to avoid a race condition where
251
+ * the tx is mined in a block with the same timestamp as txTimeoutAt, but our execution node has not yet processed it,
252
+ * or the loop here has not yet checked the tx before that timeout.
253
+ */ isTxTimedOut(state, l1Timestamp) {
254
+ const account = this.getSenderAddress().toString();
255
+ const { nonce } = state;
256
+ const txConfig = merge(this.config, state.txConfigOverrides);
257
+ const { txTimeoutAt, txTimeoutMs, maxSpeedUpAttempts } = txConfig;
258
+ const isCancelTx = state.cancelTxHashes.length > 0;
259
+ this.logger.trace(`Tx timeout check for ${account} with nonce ${nonce}`, {
260
+ nonce,
261
+ account,
262
+ l1Timestamp,
263
+ lastAttemptSent: state.lastSentAtL1Ts.getTime(),
264
+ initialTxTime: state.sentAtL1Ts.getTime(),
265
+ now: this.dateProvider.now(),
266
+ txTimeoutAt,
267
+ txTimeoutMs,
268
+ interrupted: this.interrupted
269
+ });
270
+ if (this.interrupted) {
271
+ this.logger.warn(`Tx monitoring interrupted during nonce ${nonce} for ${account} check`, {
272
+ nonce,
273
+ account
274
+ });
275
+ return true;
276
+ }
277
+ if (isCancelTx) {
278
+ // Note that we check against the lastSentAt time for cancellations, since we time them out
279
+ // after the last attempt to submit them, not the initial time.
280
+ const attempts = state.cancelTxHashes.length;
281
+ return attempts > maxSpeedUpAttempts && state.lastSentAtL1Ts.getTime() + txConfig.txCancellationFinalTimeoutMs <= l1Timestamp;
282
+ }
283
+ return txTimeoutAt !== undefined && l1Timestamp >= txTimeoutAt.getTime() || txTimeoutMs !== undefined && txTimeoutMs > 0 && l1Timestamp - state.sentAtL1Ts.getTime() >= txTimeoutMs;
284
+ }
285
+ /**
286
+ * Monitors a transaction until completion, handling speed-ups if needed
287
+ */ async monitorTransaction(state) {
288
+ const { nonce, gasLimit, blobInputs, txConfigOverrides: gasConfigOverrides } = state;
289
+ const gasConfig = merge(this.config, gasConfigOverrides);
290
+ const { maxSpeedUpAttempts, stallTimeMs } = gasConfig;
291
+ const isCancelTx = state.cancelTxHashes.length > 0;
292
+ const txHashes = isCancelTx ? state.cancelTxHashes : state.txHashes;
293
+ const isBlobTx = !!blobInputs;
294
+ const account = this.getSenderAddress().toString();
295
+ const initialTxHash = txHashes[0];
296
+ let currentTxHash = txHashes.at(-1);
297
+ let l1Timestamp;
298
+ while(true){
299
+ l1Timestamp = await this.getL1Timestamp();
300
+ try {
301
+ const timePassed = l1Timestamp - state.lastSentAtL1Ts.getTime();
302
+ const [currentNonce, pendingNonce] = await Promise.all([
303
+ this.client.getTransactionCount({
304
+ address: account,
305
+ blockTag: 'latest'
306
+ }),
307
+ this.client.getTransactionCount({
308
+ address: account,
309
+ blockTag: 'pending'
310
+ })
311
+ ]);
312
+ // If the current nonce on our account is greater than our transaction's nonce then a tx with the same nonce has been mined.
313
+ if (currentNonce > nonce) {
314
+ // We try getting the receipt twice, since sometimes anvil fails to return it if the tx has just been mined
315
+ const receipt = await this.tryGetTxReceipt(state.cancelTxHashes, nonce, true) ?? await this.tryGetTxReceipt(state.txHashes, nonce, false) ?? await sleep(500) ?? await this.tryGetTxReceipt(state.cancelTxHashes, nonce, true) ?? await this.tryGetTxReceipt(state.txHashes, nonce, false);
316
+ if (receipt) {
317
+ state.receipt = receipt;
318
+ await this.updateState(state, TxUtilsState.MINED, l1Timestamp);
319
+ return receipt;
320
+ }
321
+ // If we get here then we have checked all of our tx versions and not found anything.
322
+ // We should consider the nonce as MINED
323
+ await this.updateState(state, TxUtilsState.MINED, l1Timestamp);
324
+ throw new UnknownMinedTxError(nonce, account);
325
+ }
326
+ // If this is a cancel tx and its nonce is no longer on the mempool, we consider it dropped and stop monitoring
327
+ // If it is a regular tx, we let the loop speed it up after the stall time
328
+ if (isCancelTx && pendingNonce <= nonce && timePassed >= gasConfig.txUnseenConsideredDroppedMs) {
329
+ this.logger.warn(`Cancellation tx with nonce ${nonce} for account ${account} has been dropped from the visible mempool`, {
330
+ nonce,
331
+ account,
332
+ pendingNonce,
333
+ timePassed
334
+ });
335
+ await this.updateState(state, TxUtilsState.NOT_MINED);
336
+ this.nonceManager.reset({
337
+ address: account,
338
+ chainId: this.client.chain.id
339
+ });
340
+ throw new DroppedTransactionError(nonce, account);
341
+ }
342
+ // Break if the tx has timed out (ie expired)
343
+ if (this.isTxTimedOut(state, l1Timestamp)) {
344
+ break;
345
+ }
346
+ // Speed up the transaction if it appears to be stuck (exceeded stall time and still have retry attempts)
347
+ const attempts = txHashes.length;
348
+ if (timePassed >= stallTimeMs && attempts <= maxSpeedUpAttempts) {
349
+ const newGasPrice = await this.getGasPrice(gasConfig, isBlobTx, attempts, state.gasPrice);
350
+ state.gasPrice = newGasPrice;
351
+ this.logger.debug(`Tx ${currentTxHash} with nonce ${nonce} from ${account} appears stuck. ` + `Attempting speed-up ${attempts}/${maxSpeedUpAttempts} ` + `with new priority fee ${formatGwei(newGasPrice.maxPriorityFeePerGas)} gwei.`, {
352
+ account,
353
+ nonce,
354
+ maxFeePerGas: formatGwei(newGasPrice.maxFeePerGas),
355
+ maxPriorityFeePerGas: formatGwei(newGasPrice.maxPriorityFeePerGas),
356
+ ...newGasPrice.maxFeePerBlobGas && {
357
+ maxFeePerBlobGas: formatGwei(newGasPrice.maxFeePerBlobGas)
358
+ }
359
+ });
360
+ const txData = this.makeTxData(state, {
361
+ isCancelTx
362
+ });
363
+ const signedRequest = await this.prepareSignedTransaction(txData);
364
+ const newHash = await this.client.sendRawTransaction({
365
+ serializedTransaction: signedRequest
366
+ });
367
+ this.logger.verbose(`Sent L1 speed-up tx ${newHash} replacing ${currentTxHash} for nonce ${nonce} from ${account}`, {
368
+ nonce,
369
+ account,
370
+ gasLimit,
371
+ maxFeePerGas: formatGwei(newGasPrice.maxFeePerGas),
372
+ maxPriorityFeePerGas: formatGwei(newGasPrice.maxPriorityFeePerGas),
373
+ txConfig: state.txConfigOverrides,
374
+ ...newGasPrice.maxFeePerBlobGas && {
375
+ maxFeePerBlobGas: formatGwei(newGasPrice.maxFeePerBlobGas)
376
+ }
377
+ });
378
+ currentTxHash = newHash;
379
+ txHashes.push(currentTxHash);
380
+ state.lastSentAtL1Ts = new Date(l1Timestamp);
381
+ await this.updateState(state, isCancelTx ? TxUtilsState.CANCELLED : TxUtilsState.SPEED_UP);
382
+ await sleep(gasConfig.checkIntervalMs);
383
+ continue;
384
+ }
385
+ this.logger.debug(`Tx ${currentTxHash} from ${account} with nonce ${nonce} still pending after ${timePassed}ms`, {
386
+ account,
387
+ nonce,
388
+ pendingNonce,
389
+ attempts,
390
+ timePassed,
391
+ isBlobTx,
392
+ isCancelTx,
393
+ ...pick(state.gasPrice, 'maxFeePerGas', 'maxPriorityFeePerGas', 'maxFeePerBlobGas'),
394
+ ...pick(gasConfig, 'txUnseenConsideredDroppedMs', 'txCancellationFinalTimeoutMs', 'maxSpeedUpAttempts', 'stallTimeMs', 'txTimeoutAt', 'txTimeoutMs')
395
+ });
396
+ await sleep(gasConfig.checkIntervalMs);
397
+ } catch (err) {
398
+ if (err instanceof DroppedTransactionError || err instanceof UnknownMinedTxError) {
399
+ throw err;
400
+ }
401
+ const viemError = formatViemError(err);
402
+ this.logger.error(`Error while monitoring L1 tx ${currentTxHash}`, viemError, {
403
+ nonce,
404
+ account
405
+ });
406
+ await sleep(gasConfig.checkIntervalMs);
407
+ }
408
+ }
409
+ // Oh no, the transaction has timed out!
410
+ if (isCancelTx || !gasConfig.cancelTxOnTimeout) {
411
+ // If this was already a cancellation tx, or we are configured to not cancel txs, we just mark it as NOT_MINED
412
+ // and reset the nonce manager, so the next tx that comes along can reuse the nonce if/when this tx gets dropped.
413
+ // This is the nastiest scenario for us, since the new tx could acquire the next nonce, but then this tx is dropped,
414
+ // and the new tx would never get mined. Eventually, the new tx would also drop.
415
+ await this.updateState(state, TxUtilsState.NOT_MINED);
416
+ this.nonceManager.reset({
417
+ address: account,
418
+ chainId: this.client.chain.id
419
+ });
420
+ } else {
421
+ // Otherwise we fire the cancellation without awaiting to avoid blocking the caller,
422
+ // and monitor it in the background so we can speed it up as needed.
423
+ void this.attemptTxCancellation(state).catch(async (err)=>{
424
+ await this.updateState(state, TxUtilsState.NOT_MINED);
425
+ this.logger.error(`Failed to send cancellation for timed out tx ${initialTxHash} with nonce ${nonce}`, err, {
426
+ account,
427
+ nonce,
428
+ initialTxHash
429
+ });
430
+ });
431
+ }
432
+ const what = isCancelTx ? 'Cancellation L1' : 'L1';
433
+ this.logger.warn(`${what} transaction ${initialTxHash} with nonce ${nonce} from ${account} timed out`, {
434
+ initialTxHash,
435
+ currentTxHash,
436
+ nonce,
437
+ account,
438
+ txTimeoutAt: gasConfig.txTimeoutAt?.getTime(),
439
+ txTimeoutMs: gasConfig.txTimeoutMs,
440
+ txInitialTime: state.sentAtL1Ts.getTime(),
441
+ l1Timestamp,
442
+ now: this.dateProvider.now(),
443
+ attempts: txHashes.length - 1,
444
+ isInterrupted: this.interrupted
445
+ });
446
+ throw new TimeoutError(`L1 transaction ${initialTxHash} timed out`);
447
+ }
448
+ /**
449
+ * Creates tx data to be signed by viem signTransaction method, using the state as input.
450
+ * If isCancelTx is true, creates a 0-value tx to self with 21k gas and no data instead,
451
+ * and an empty blob input if the original tx also had blobs.
452
+ */ makeTxData(state, opts) {
453
+ const { request, gasLimit, blobInputs, gasPrice, nonce } = state;
454
+ const isBlobTx = blobInputs !== undefined;
455
+ const baseTxOpts = {
456
+ nonce,
457
+ ...pick(gasPrice, 'maxFeePerGas', 'maxPriorityFeePerGas')
458
+ };
459
+ if (opts.isCancelTx) {
460
+ const baseTxData = {
461
+ to: this.getSenderAddress().toString(),
462
+ value: 0n,
463
+ data: '0x',
464
+ gas: 21_000n,
465
+ ...baseTxOpts
466
+ };
467
+ return isBlobTx ? {
468
+ ...baseTxData,
469
+ ...this.makeEmptyBlobInputs(gasPrice.maxFeePerBlobGas)
470
+ } : baseTxData;
471
+ }
472
+ const baseTxData = {
473
+ ...request,
474
+ ...baseTxOpts,
475
+ gas: gasLimit
476
+ };
477
+ return blobInputs ? {
478
+ ...baseTxData,
479
+ ...blobInputs,
480
+ maxFeePerBlobGas: gasPrice.maxFeePerBlobGas
481
+ } : baseTxData;
482
+ }
483
+ /** Returns when all monitor loops have stopped. */ async waitMonitoringStopped(timeoutSeconds = 10) {
484
+ const account = this.getSenderAddress().toString();
485
+ await retryUntil(()=>this.txs.every((tx)=>TerminalTxUtilsState.includes(tx.status)), `monitoring stopped for ${account}`, timeoutSeconds, 0.1).catch(()=>this.logger.warn(`Timeout waiting for monitoring loops to stop for ${account}`));
486
+ }
487
+ /**
488
+ * Sends a transaction and monitors it until completion
489
+ * @param request - The transaction request (to, data, value)
490
+ * @param gasConfig - Optional gas configuration
491
+ * @returns The receipt of the successful transaction
492
+ */ async sendAndMonitorTransaction(request, gasConfig, blobInputs) {
493
+ const { state } = await this.sendTransaction(request, gasConfig, blobInputs);
494
+ const receipt = await this.monitorTransaction(state);
495
+ return {
496
+ receipt,
497
+ state
498
+ };
499
+ }
500
+ async simulate(request, _blockOverrides = {}, stateOverrides = [], abi = RollupAbi, _gasConfig) {
501
+ const blockOverrides = {
502
+ ..._blockOverrides
503
+ };
504
+ const gasConfig = merge(this.config, _gasConfig);
505
+ const gasPrice = await this.getGasPrice(gasConfig, false);
506
+ const call = {
507
+ to: request.to,
508
+ data: request.data,
509
+ from: request.from ?? this.getSenderAddress().toString(),
510
+ maxFeePerGas: gasPrice.maxFeePerGas,
511
+ maxPriorityFeePerGas: gasPrice.maxPriorityFeePerGas,
512
+ gas: request.gas ?? LARGE_GAS_LIMIT
513
+ };
514
+ if (!request.gas && !gasConfig.ignoreBlockGasLimit) {
515
+ // LARGE_GAS_LIMIT is set as call.gas, increase block gasLimit
516
+ blockOverrides.gasLimit = LARGE_GAS_LIMIT * 2n;
517
+ }
518
+ return this._simulate(call, blockOverrides, stateOverrides, gasConfig, abi);
519
+ }
520
+ /**
521
+ * Attempts to cancel a transaction by sending a 0-value tx to self with same nonce but higher gas prices
522
+ * Only sends the cancellation if the original tx is still pending, not if it was dropped
523
+ * @returns The hash of the cancellation transaction
524
+ */ async attemptTxCancellation(state) {
525
+ const isBlobTx = state.blobInputs !== undefined;
526
+ const { nonce, gasPrice: previousGasPrice } = state;
527
+ const account = this.getSenderAddress().toString();
528
+ // Do not send cancellation if interrupted
529
+ if (this.interrupted) {
530
+ this.logger.warn(`Not sending cancellation for L1 tx from account ${account} with nonce ${nonce} as interrupted`, {
531
+ nonce,
532
+ account
533
+ });
534
+ await this.updateState(state, TxUtilsState.NOT_MINED);
535
+ this.nonceManager.reset({
536
+ address: account,
537
+ chainId: this.client.chain.id
538
+ });
539
+ return;
540
+ }
541
+ // Check if the original tx is still pending
542
+ const currentNonce = await this.client.getTransactionCount({
543
+ address: account,
544
+ blockTag: 'pending'
545
+ });
546
+ if (currentNonce < nonce) {
547
+ this.logger.verbose(`Not sending cancellation for L1 tx from account ${account} with nonce ${nonce} as it is dropped`, {
548
+ nonce,
549
+ account,
550
+ currentNonce
551
+ });
552
+ await this.updateState(state, TxUtilsState.NOT_MINED);
553
+ this.nonceManager.reset({
554
+ address: account,
555
+ chainId: this.client.chain.id
556
+ });
557
+ return;
558
+ }
559
+ // Get gas price with higher priority fee for cancellation
560
+ const cancelGasPrice = await this.getGasPrice({
561
+ ...this.config,
562
+ // Use high bump for cancellation to ensure it replaces the original tx
563
+ priorityFeeRetryBumpPercentage: 150
564
+ }, isBlobTx, state.txHashes.length, previousGasPrice);
565
+ const { maxFeePerGas, maxPriorityFeePerGas, maxFeePerBlobGas } = cancelGasPrice;
566
+ this.logger.verbose(`Attempting to cancel L1 ${isBlobTx ? 'blob' : 'vanilla'} transaction from account ${account} with nonce ${nonce} after time out`, {
567
+ maxFeePerGas: formatGwei(maxFeePerGas),
568
+ maxPriorityFeePerGas: formatGwei(maxPriorityFeePerGas),
569
+ ...maxFeePerBlobGas && {
570
+ maxFeePerBlobGas: formatGwei(maxFeePerBlobGas)
571
+ }
572
+ });
573
+ // Send 0-value tx to self with higher gas price
574
+ state.gasPrice = cancelGasPrice;
575
+ state.lastSentAtL1Ts = new Date(await this.getL1Timestamp());
576
+ const txData = this.makeTxData(state, {
577
+ isCancelTx: true
578
+ });
579
+ const signedRequest = await this.prepareSignedTransaction(txData);
580
+ const cancelTxHash = await this.client.sendRawTransaction({
581
+ serializedTransaction: signedRequest
582
+ });
583
+ state.cancelTxHashes.push(cancelTxHash);
584
+ await this.updateState(state, TxUtilsState.CANCELLED);
585
+ this.logger.warn(`Sent cancellation tx ${cancelTxHash} for timed out tx from ${account} with nonce ${nonce}`, {
586
+ nonce,
587
+ cancelGasPrice,
588
+ isBlobTx,
589
+ txHashes: state.txHashes
590
+ });
591
+ // Do not await the cancel tx to be mined
592
+ void this.monitorTransaction(state).catch((err)=>{
593
+ this.logger.error(`Failed to mine cancellation tx ${cancelTxHash} for nonce ${nonce} account ${account}`, err, {
594
+ nonce,
595
+ account,
596
+ cancelTxHash
597
+ });
598
+ });
599
+ }
600
+ /** Returns L1 timestamps in milliseconds */ async getL1Timestamp() {
601
+ const { timestamp } = await this.client.getBlock({
602
+ blockTag: 'latest',
603
+ includeTransactions: false
604
+ });
605
+ return Number(timestamp) * 1000;
606
+ }
607
+ /** Makes empty blob inputs for the cancellation tx. To be overridden in L1TxUtilsWithBlobs. */ makeEmptyBlobInputs(_maxFeePerBlobGas) {
608
+ throw new Error('Cannot make empty blob inputs for cancellation');
609
+ }
610
+ }
@@ -0,0 +1,26 @@
1
+ import type { Logger } from '@aztec/foundation/log';
2
+ import { DateProvider } from '@aztec/foundation/timer';
3
+ import type { EthSigner } from '../eth-signer/eth-signer.js';
4
+ import type { ExtendedViemWalletClient, ViemClient } from '../types.js';
5
+ import type { L1TxUtilsConfig } from './config.js';
6
+ import type { IL1TxMetrics, IL1TxStore } from './interfaces.js';
7
+ import { L1TxUtils } from './l1_tx_utils.js';
8
+ import type { L1BlobInputs } from './types.js';
9
+ /** Extends L1TxUtils with the capability to cancel blobs. This needs to be a separate class so we don't require a dependency on blob-lib unnecessarily. */
10
+ export declare class L1TxUtilsWithBlobs extends L1TxUtils {
11
+ /** Makes empty blob inputs for the cancellation tx. */
12
+ protected makeEmptyBlobInputs(maxFeePerBlobGas: bigint): Required<L1BlobInputs>;
13
+ }
14
+ export declare function createL1TxUtilsWithBlobsFromViemWallet(client: ExtendedViemWalletClient, deps?: {
15
+ logger?: Logger;
16
+ dateProvider?: DateProvider;
17
+ store?: IL1TxStore;
18
+ metrics?: IL1TxMetrics;
19
+ }, config?: Partial<L1TxUtilsConfig>, debugMaxGasLimit?: boolean): L1TxUtilsWithBlobs;
20
+ export declare function createL1TxUtilsWithBlobsFromEthSigner(client: ViemClient, signer: EthSigner, deps?: {
21
+ logger?: Logger;
22
+ dateProvider?: DateProvider;
23
+ store?: IL1TxStore;
24
+ metrics?: IL1TxMetrics;
25
+ }, config?: Partial<L1TxUtilsConfig>, debugMaxGasLimit?: boolean): L1TxUtilsWithBlobs;
26
+ //# sourceMappingURL=l1_tx_utils_with_blobs.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"l1_tx_utils_with_blobs.d.ts","sourceRoot":"","sources":["../../src/l1_tx_utils/l1_tx_utils_with_blobs.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,MAAM,yBAAyB,CAAC;AAIvD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAC;AAC7D,OAAO,KAAK,EAAE,wBAAwB,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACxE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAChE,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAE7C,OAAO,KAAK,EAAE,YAAY,EAAmB,MAAM,YAAY,CAAC;AAEhE,2JAA2J;AAC3J,qBAAa,kBAAmB,SAAQ,SAAS;IAC/C,uDAAuD;cACpC,mBAAmB,CAAC,gBAAgB,EAAE,MAAM,GAAG,QAAQ,CAAC,YAAY,CAAC;CAKzF;AAED,wBAAgB,sCAAsC,CACpD,MAAM,EAAE,wBAAwB,EAChC,IAAI,GAAE;IACJ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,OAAO,CAAC,EAAE,YAAY,CAAC;CACnB,EACN,MAAM,GAAE,OAAO,CAAC,eAAe,CAAM,EACrC,gBAAgB,GAAE,OAAe,sBAalC;AAED,wBAAgB,qCAAqC,CACnD,MAAM,EAAE,UAAU,EAClB,MAAM,EAAE,SAAS,EACjB,IAAI,GAAE;IACJ,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,YAAY,CAAC;IAC5B,KAAK,CAAC,EAAE,UAAU,CAAC;IACnB,OAAO,CAAC,EAAE,YAAY,CAAC;CACnB,EACN,MAAM,GAAE,OAAO,CAAC,eAAe,CAAM,EACrC,gBAAgB,GAAE,OAAe,sBAiBlC"}
@@ -0,0 +1,26 @@
1
+ import { Blob } from '@aztec/blob-lib';
2
+ import { EthAddress } from '@aztec/foundation/eth-address';
3
+ import { L1TxUtils } from './l1_tx_utils.js';
4
+ import { createViemSigner } from './signer.js';
5
+ /** Extends L1TxUtils with the capability to cancel blobs. This needs to be a separate class so we don't require a dependency on blob-lib unnecessarily. */ export class L1TxUtilsWithBlobs extends L1TxUtils {
6
+ /** Makes empty blob inputs for the cancellation tx. */ makeEmptyBlobInputs(maxFeePerBlobGas) {
7
+ const blobData = new Uint8Array(131072).fill(0);
8
+ const kzg = Blob.getViemKzgInstance();
9
+ return {
10
+ blobs: [
11
+ blobData
12
+ ],
13
+ kzg,
14
+ maxFeePerBlobGas
15
+ };
16
+ }
17
+ }
18
+ export function createL1TxUtilsWithBlobsFromViemWallet(client, deps = {}, config = {}, debugMaxGasLimit = false) {
19
+ return new L1TxUtilsWithBlobs(client, EthAddress.fromString(client.account.address), createViemSigner(client), deps.logger, deps.dateProvider, config, debugMaxGasLimit, deps.store, deps.metrics);
20
+ }
21
+ export function createL1TxUtilsWithBlobsFromEthSigner(client, signer, deps = {}, config = {}, debugMaxGasLimit = false) {
22
+ const callback = async (transaction, _signingAddress)=>{
23
+ return (await signer.signTransaction(transaction)).toViemTransactionSignature();
24
+ };
25
+ return new L1TxUtilsWithBlobs(client, signer.address, callback, deps.logger, deps.dateProvider, config, debugMaxGasLimit, deps.store, deps.metrics);
26
+ }