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(docs): add transaction profiler docs #9932

Merged
merged 8 commits into from
Nov 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
---
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we not need to add this to the main docs reference file so it shows up on the sidebars?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its shows up on the sidebar now under "Aztec.nr" - https://6736ede7171fe516edd8a7ec--aztec-docs-dev.netlify.app/guides

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. That is, 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 "private token transfer" transaction where an `operator` transfers tokens from a `user` account using [Authwit](../../../aztec/concepts/accounts/authwit.md).

Let's deploy the necessary account and token contracts first:

```bash
# Deploy account contracts for the user, owner (token deployer and minter),
# and an operator (who uses authwit to transfer token from user's account)
aztec-wallet create-account -a owner
aztec-wallet create-account -a user
aztec-wallet create-account -a operator

# 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a note that readers need to clone the monorepo and compile the token contract with aztec-nargo compile --package token_contract in noir-projects/noir-contracts?

Copy link
Member Author

@saleel saleel Nov 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. I have added this note.

This is a lot to ask readers to go through, any ideas on how we can make this faster/easier?

In practice, devs would be trying this with their own contracts in their nargo workspace. This mostly serves as an example right?
But if they want to follow along and use the same steps then they would need to clone the monorepo and deploy the token contract. I think it is ok considering its not a mandatory step for anyone to profile their contract.

aztec-wallet send mint_to_private -ca token --args accounts:owner accounts:user 100 -f owner

# Create an authwit for the operator to transfer tokens from the user's account (to operator's own acc)
aztec-wallet create-secret -a auth_nonce
aztec-wallet create-authwit transfer_from operator -ca token --args accounts:user accounts:operator 100 secrets:auth_nonce -f user
aztec-wallet add-authwit authwits:last user -f operator
```

Running the above will add an Authwit that allows the `operator` to transfer 100 TST tokens from the `user`'s account. The `operator` can now transfer tokens by running:

```bash
# operator transfers 100 TST tokens to themselves
aztec-wallet send transfer_from -ca token --args accounts:user accounts:operator 100 secrets:auth_nonce -f operator
```

Instead of sending the 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_from -ca token --args accounts:user accounts:operator 100 secrets:auth_nonce -f operator
```

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,866 Acc: 61,229
Token:transfer_from Gates: 128,094 Acc: 189,323
private_kernel_inner Gates: 57,508 Acc: 246,831
SchnorrAccount:verify_private_authwit Gates: 12,646 Acc: 259,477
private_kernel_inner Gates: 57,508 Acc: 316,985
private_kernel_reset Gates: 86,599 Acc: 403,584
private_kernel_tail Gates: 13,042 Acc: 416,626

Total gates: 416,626
```

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.
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
Loading