Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: more test info in tx receipt #3221

Merged
merged 5 commits into from
Nov 6, 2023
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
23 changes: 17 additions & 6 deletions yarn-project/aztec.js/src/contract/sent_tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ export type WaitOpts = {
* If false, then any queries that depend on state set by this transaction may return stale data. Defaults to true.
**/
waitForNotesSync?: boolean;
/** Whether newly created notes should be included in the receipt. */
getNotes?: boolean;
/** Whether to include information useful for debugging/testing in the receipt. */
debug?: boolean;
};

const DefaultWaitOpts: WaitOpts = {
timeout: 60,
interval: 1,
waitForNotesSync: true,
getNotes: false,
debug: false,
};

/**
Expand Down Expand Up @@ -61,14 +61,25 @@ export class SentTx {
* @returns The transaction receipt.
*/
public async wait(opts?: WaitOpts): Promise<FieldsOf<TxReceipt>> {
if (opts?.getNotes && opts.waitForNotesSync === false) {
if (opts?.debug && opts.waitForNotesSync === false) {
throw new Error('Cannot set getNotes to true if waitForNotesSync is false');
}
const receipt = await this.waitForReceipt(opts);
if (receipt.status !== TxStatus.MINED)
throw new Error(`Transaction ${await this.getTxHash()} was ${receipt.status}`);
if (opts?.getNotes) {
receipt.visibleNotes = await this.pxe.getNotes({ txHash: await this.getTxHash() });
if (opts?.debug) {
const txHash = await this.getTxHash();
const tx = (await this.pxe.getTx(txHash))!;
const visibleNotes = await this.pxe.getNotes({ txHash });
receipt.debugInfo = {
newCommitments: tx.newCommitments,
newNullifiers: tx.newNullifiers,
newPublicDataWrites: tx.newPublicDataWrites,
newL2ToL1Msgs: tx.newL2ToL1Msgs,
newContracts: tx.newContracts,
newContractData: tx.newContractData,
visibleNotes,
};
}
return receipt;
}
Expand Down
8 changes: 4 additions & 4 deletions yarn-project/end-to-end/src/e2e_token_contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,13 @@ describe('e2e_token_contract', () => {
it('redeem as recipient', async () => {
await addPendingShieldNoteToPXE(0, amount, secretHash, txHash);
const txClaim = asset.methods.redeem_shield(accounts[0].address, amount, secret).send();
const receiptClaim = await txClaim.wait({ getNotes: true });
const receiptClaim = await txClaim.wait({ debug: true });
expect(receiptClaim.status).toBe(TxStatus.MINED);
tokenSim.redeemShield(accounts[0].address, amount);
// 1 note should be created containing `amount` of tokens
const notes = receiptClaim.visibleNotes!;
expect(notes.length).toBe(1);
expect(notes[0].note.items[0].toBigInt()).toBe(amount);
const { visibleNotes } = receiptClaim.debugInfo!;
expect(visibleNotes.length).toBe(1);
expect(visibleNotes[0].note.items[0].toBigInt()).toBe(amount);
});
});

Expand Down
46 changes: 41 additions & 5 deletions yarn-project/types/src/tx/tx_receipt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AztecAddress } from '@aztec/foundation/aztec-address';
import { ExtendedNote, TxHash } from '@aztec/types';
import { Fr } from '@aztec/foundation/fields';
import { ContractData, ExtendedNote, PublicDataWrite, TxHash } from '@aztec/types';

/**
* Possible status of a transaction.
Expand Down Expand Up @@ -41,11 +42,9 @@ export class TxReceipt {
*/
public contractAddress?: AztecAddress,
/**
* Notes created in this tx which belong to accounts which are registered in the PXE which was used to submit the
* tx. You will not receive notes of accounts which are not registered in the PXE here even though they were
* created in this tx.
* Information useful for testing/debugging, set when test flag is set to true in `waitOpts`.
*/
public visibleNotes?: ExtendedNote[],
public debugInfo?: DebugInfo,
) {}

/**
Expand Down Expand Up @@ -78,3 +77,40 @@ export class TxReceipt {
return new TxReceipt(txHash, status, error, blockHash, blockNumber, contractAddress);
}
}

/**
* Information useful for debugging/testing purposes included in the receipt when the debug flag is set to true
* in `WaitOpts`.
*/
interface DebugInfo {
/**
* New commitments created by the transaction.
*/
newCommitments: Fr[];
/**
* New nullifiers created by the transaction.
*/
newNullifiers: Fr[];
/**
* New public data writes created by the transaction.
*/
newPublicDataWrites: PublicDataWrite[];
/**
* New L2 to L1 messages created by the transaction.
*/
newL2ToL1Msgs: Fr[];
/**
* New contracts leafs created by the transaction to be inserted into the contract tree.
*/
newContracts: Fr[];
/**
* New contract data created by the transaction.
*/
newContractData: ContractData[];
/**
* Notes created in this tx which belong to accounts which are registered in the PXE which was used to submit the
* tx. You will not receive notes of accounts which are not registered in the PXE here even though they were
* created in this tx.
*/
visibleNotes: ExtendedNote[];
}