From 74ed1429f71342c8323aa690f06ba302dc270708 Mon Sep 17 00:00:00 2001 From: saleel Date: Mon, 18 Nov 2024 22:16:14 +0530 Subject: [PATCH] feat(docs): add transaction profiler docs (#9932) Solves https://github.com/AztecProtocol/dev-rel/issues/443 --- .../smart_contracts/profiling_transactions.md | 71 +++++++++++++++++++ .../sandbox_reference/cli_wallet_reference.md | 10 +++ 2 files changed, 81 insertions(+) create mode 100644 docs/docs/guides/developer_guides/smart_contracts/profiling_transactions.md diff --git a/docs/docs/guides/developer_guides/smart_contracts/profiling_transactions.md b/docs/docs/guides/developer_guides/smart_contracts/profiling_transactions.md new file mode 100644 index 00000000000..40aa27888fa --- /dev/null +++ b/docs/docs/guides/developer_guides/smart_contracts/profiling_transactions.md @@ -0,0 +1,71 @@ +--- +title: Profiling Transactions +sidebar_position: 5 +tags: [contracts, profiling] +--- + +# Profiling Transactions + +An Aztec transaction typically consists of a private and a public part. The private part is where the user executes contract logic within the PXE and generates a proof of execution, which is then sent to the sequencer. + +Since proof generation is an expensive operation that needs to be done on the client side, it is important to optimize the private contract logic. It is desirable to keep the gate count of circuits representing the private contract logic as low as possible. + +A private transaction can involve multiple function calls. It starts with an account `entrypoint()` which may call several private functions to execute the application logic, which in turn might call other functions. Moreover, every private function call has to go through a round of kernel circuits. Read more about the transaction lifecycle [here](../../../aztec/concepts/transactions.md). + +In this guide, we will look at how to profile the private execution of a transaction, allowing you to get the gate count of each private function within the transaction, including the kernel circuits. + +## Prerequisites + +- `aztec-nargo` installed (go to [Sandbox section](../../../reference/developer_references/sandbox_reference/sandbox-reference.md) for installation instructions) +- `aztec-wallet` installed (installed as part of the Sandbox) +- Aztec Sandbox running with **proving enabled** (go to [Sandbox PXE Proving](../local_env/sandbox_proving.md) for instructions) + +## Profiling using aztec-wallet + +The profiling tool is integrated into the `aztec-wallet`. + +In this example, we will profile a simple "private token transfer" transaction which uses the [transfer](https://github.com/AztecProtocol/aztec-packages/blob/master/noir-projects/noir-contracts/contracts/token_contract/src/main.nr#L269) method in the token contract. +If you want to follow along, you'll need to clone the Aztec [monorepo](https://github.com/AztecProtocol/aztec-packages) and [compile](./how_to_compile_contract.md) the `token_contract` in `noir-projects/noir-contracts` by running `aztec-nargo compile --package token_contract`. + +Let's deploy the necessary account and token contracts first: + +```bash +# Deploy accounts +aztec-wallet create-account -a owner +aztec-wallet create-account -a user + +# Deploy a token contract and mint 100 tokens to the user +aztec-wallet deploy token_contract@Token --args accounts:owner Test TST 18 -f owner -a token +aztec-wallet send mint_to_private -ca token --args accounts:owner accounts:user 100 -f owner +``` + +Now, the `user` can transfer tokens by running: + +```bash +# Send the tokens back to the owner +aztec-wallet send transfer -ca token --args accounts:owner 40 -f user +``` + +Instead of sending the above transaction, you can simulate it by running the `simulate` command with the same parameters, and then add a `--profile` flag to profile the gate count of each private function in the transaction. + +```bash +aztec-wallet simulate --profile transfer -ca token --args accounts:owner 40 -f user +``` + +This will print the following results after some time: + +```bash +Gate count per circuit: + SchnorrAccount:entrypoint Gates: 26,363 Acc: 26,363 + private_kernel_init Gates: 34,887 Acc: 61,250 + Token:transfer Gates: 28,229 Acc: 89,479 + private_kernel_inner Gates: 57,530 Acc: 147,009 + private_kernel_reset Gates: 86,600 Acc: 233,609 + private_kernel_tail Gates: 13,045 Acc: 246,654 + +Total gates: 246,654 +``` + +Here you can see the gate count of each private function call in the transaction along with the kernel circuits needed in between, and the total gate count. + +This will help you understand which parts of your transaction are bottlenecks and optimize the contract logic accordingly. diff --git a/docs/docs/reference/developer_references/sandbox_reference/cli_wallet_reference.md b/docs/docs/reference/developer_references/sandbox_reference/cli_wallet_reference.md index 12a837c227a..76e094fb0b3 100644 --- a/docs/docs/reference/developer_references/sandbox_reference/cli_wallet_reference.md +++ b/docs/docs/reference/developer_references/sandbox_reference/cli_wallet_reference.md @@ -130,6 +130,16 @@ Simulates a transaction instead of sending it. This allows you to obtain i.e. th aztec-wallet simulate --from master_yoda --contract-address jedi_order --args "luke_skywalker" train_jedi ``` +### Profile + +Simulates a transaction with profiling enabled. This allows you to get the gate count of each private function in the transaction. Read more about profiling [here](../../../guides/developer_guides/smart_contracts/profiling_transactions.md). + +#### Example + +```bash +aztec-wallet simulate --profile --from master_yoda --contract-address jedi_order --args "luke_skywalker" train_jedi +``` + ### Bridge Fee Juice The wallet provides an easy way to mint the fee-paying asset on L1 and bridging it to L2. We call it Fee Juice and you can read more about it in the [protocol specs](../../../protocol-specs/gas-and-fees/fee-juice.md).