Skip to content

Commit

Permalink
feat: Add proven flag to sent tx wait opts (#7950)
Browse files Browse the repository at this point in the history
Allows to wait for a tx to be proven instead of it just being mined from
aztec.js sent-tx class.
  • Loading branch information
spalladino authored Aug 13, 2024
1 parent 806a370 commit e80e7d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions yarn-project/aztec.js/src/contract/sent_tx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,15 @@ describe('SentTx', () => {
pxe.getSyncStatus.mockResolvedValue({ blocks: 19, notes: { '0x1': 19, '0x2': 19 } });
await expect(sentTx.wait({ timeout: 1, interval: 0.4 })).rejects.toThrow(/dropped/);
});

it('waits for the tx to be proven', async () => {
const waitOpts = { timeout: 1, interval: 0.4, waitForNotesSync: false, proven: true, provenTimeout: 2 };
pxe.getProvenBlockNumber.mockResolvedValue(10);
await expect(sentTx.wait(waitOpts)).rejects.toThrow(/timeout/i);

pxe.getProvenBlockNumber.mockResolvedValue(20);
const actual = await sentTx.wait(waitOpts);
expect(actual).toEqual(txReceipt);
});
});
});
20 changes: 20 additions & 0 deletions yarn-project/aztec.js/src/contract/sent_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ import { type FieldsOf } from '@aztec/foundation/types';
export type WaitOpts = {
/** The maximum time (in seconds) to wait for the transaction to be mined. Defaults to 60. */
timeout?: number;
/** The maximum time (in seconds) to wait for the transaction to be proven. Defaults to 600. */
provenTimeout?: number;
/** The time interval (in seconds) between retries to fetch the transaction receipt. Defaults to 1. */
interval?: number;
/** Whether to wait for the tx to be proven. */
proven?: boolean;
/**
* Whether to wait for the PXE Service to sync all notes up to the block in which this tx was mined.
* If false, then any queries that depend on state set by this transaction may return stale data. Defaults to true.
Expand All @@ -28,6 +32,7 @@ export type WaitOpts = {

export const DefaultWaitOpts: WaitOpts = {
timeout: 60,
provenTimeout: 600,
interval: 1,
waitForNotesSync: true,
debug: false,
Expand Down Expand Up @@ -78,6 +83,9 @@ export class SentTx {
`Transaction ${await this.getTxHash()} was ${receipt.status}. Reason: ${receipt.error ?? 'unknown'}`,
);
}
if (opts?.proven && receipt.blockNumber !== undefined) {
await this.waitForProven(receipt.blockNumber, opts);
}
if (opts?.debug) {
const txHash = await this.getTxHash();
const tx = (await this.pxe.getTxEffect(txHash))!;
Expand Down Expand Up @@ -144,4 +152,16 @@ export class SentTx {
opts?.interval ?? DefaultWaitOpts.interval,
);
}

protected async waitForProven(minedBlock: number, opts?: WaitOpts) {
return await retryUntil(
async () => {
const provenBlock = await this.pxe.getProvenBlockNumber();
return provenBlock >= minedBlock ? provenBlock : undefined;
},
'isProven',
opts?.provenTimeout ?? DefaultWaitOpts.provenTimeout,
opts?.interval ?? DefaultWaitOpts.interval,
);
}
}

0 comments on commit e80e7d2

Please sign in to comment.