Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions yarn-project/ethereum/src/l1_tx_utils/l1_tx_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,21 @@ describe('L1TxUtils', () => {
expect(result.receipt.status).toBe('reverted');
});

it('does not consume nonce when transaction times out before sending', async () => {
// Get the expected nonce before any transaction
const expectedNonce = await l1Client.getTransactionCount({ address: l1Client.account.address });

// Try to send with an already-expired timeout (epoch 0 is well in the past)
const pastTimeout = new Date(0);
await expect(gasUtils.sendTransaction(request, { txTimeoutAt: pastTimeout })).rejects.toThrow(
/timed out before sending/,
);

// The next transaction should use the same nonce (not skip one due to a leaked consume)
const { state } = await gasUtils.sendTransaction(request);
expect(state.nonce).toBe(expectedNonce);
}, 10_000);

it('stops trying after timeout once block is mined', async () => {
await cheatCodes.setAutomine(false);
await cheatCodes.setIntervalMining(0);
Expand Down
17 changes: 10 additions & 7 deletions yarn-project/ethereum/src/l1_tx_utils/l1_tx_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ export class L1TxUtils extends ReadOnlyL1TxUtils {
throw new InterruptError(`Transaction sending is interrupted`);
}

// Check timeout before consuming nonce to avoid leaking a nonce that was never sent.
// A leaked nonce creates a gap (e.g. nonce 107 consumed but unsent), so all subsequent
// transactions (108, 109, ...) can never be mined since the chain expects 107 first.
const now = new Date(await this.getL1Timestamp());
if (gasConfig.txTimeoutAt && now > gasConfig.txTimeoutAt) {
throw new TimeoutError(
`Transaction timed out before sending (now ${now.toISOString()} > timeoutAt ${gasConfig.txTimeoutAt.toISOString()})`,
);
}

const nonce = await this.nonceManager.consume({
client: this.client,
address: account,
Expand All @@ -253,13 +263,6 @@ export class L1TxUtils extends ReadOnlyL1TxUtils {
const baseState = { request, gasLimit, blobInputs, gasPrice, nonce };
const txData = this.makeTxData(baseState, { isCancelTx: false });

const now = new Date(await this.getL1Timestamp());
if (gasConfig.txTimeoutAt && now > gasConfig.txTimeoutAt) {
throw new TimeoutError(
`Transaction timed out before sending (now ${now.toISOString()} > timeoutAt ${gasConfig.txTimeoutAt.toISOString()})`,
);
}

// Send the new tx
const signedRequest = await this.prepareSignedTransaction(txData);
const txHash = await this.client.sendRawTransaction({ serializedTransaction: signedRequest });
Expand Down
Loading