Skip to content

Commit

Permalink
docs: remove view and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Apr 2, 2024
1 parent 85f99d9 commit 5ba8a79
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 22 deletions.
14 changes: 9 additions & 5 deletions docs/docs/developers/aztecjs/guides/call_view_function.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
title: How to Call a View Function
title: How to Simulate a Function Call
---

This guide explains how to call a `view` function using [Aztec.js](../main.md).
This guide explains how to `simulate` a function call using [Aztec.js](../main.md).

To do this from the CLI, go [here](../../sandbox/references/cli-commands.md#calling-an-unconstrained-view-function).

Expand All @@ -26,9 +26,13 @@ Get a previously deployed contract like this:

#include_code get_contract yarn-project/end-to-end/src/docs_examples.test.ts typescript

## Call view function
## Simulating function calls

Call the `view` function on the contract like this:
Call the `simulate` function on the typescript contract wrapper like this:

#include_code call_view_function yarn-project/end-to-end/src/docs_examples.test.ts typescript
#include_code simulate_function yarn-project/end-to-end/src/docs_examples.test.ts typescript

:::info Note
- If the simulated function is `unconstrained` you will get a properly typed value.
- If the simulated function is `public` or `private` it will return a Field array of size 4.
:::
26 changes: 26 additions & 0 deletions docs/docs/misc/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,32 @@ This change was made to communicate that we do not constrain the value in circui
+ let random_value = unsafe_rand();
```

### [AztecJS] Simulate and get return values for ANY call
Historically it have been possible to "view" `unconstrained` functions to simulate them and get the return values, but not for `public` nor `private` functions.
This have lead to a lot of bad code where we have the same function implemented thrice, once in `private`, once in `public` and once in `unconstrained`.
It is not possible to call `simulate` on any call to get the return values!
However, beware that it currently always return a Field array of size 4 for private and public.
This will change to become similar to the return values of the `unconstrained` with proper return times.

```diff
- #[aztec(private)]
- fn get_shared_immutable_constrained_private() -> pub Leader {
- storage.shared_immutable.read_private()
- }
-
- unconstrained fn get_shared_immutable() -> pub Leader {
- storage.shared_immutable.read_public()
- }

+ #[aztec(private)]
+ fn get_shared_immutable_private() -> pub Leader {
+ storage.shared_immutable.read_private()
+ }

- const returnValues = await contract.methods.get_shared_immutable().view();
+ const returnValues = await contract.methods.get_shared_immutable_private().simulate();
```

## 0.31.0

### [Aztec.nr] Public storage historical read API improvement
Expand Down
1 change: 0 additions & 1 deletion docs/internal_notes/building_dapps.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ Explain how to write a dapp using [`aztec.js`](https://github.com/AztecProtocol/
- Instantiate a contract
- Deploy a contract
- How to generate a nice typescript interface for an Aztec.nr contract's functions (we have a little `.ts` program in `noir-contracts` to generate a types file at the moment... how would a user do this?)
- Call 'view' functions
- Simulate functions (simulate the result, without sending to the 'network')
- Execute functions (send them to the 'network')
- Tx hashes and tx receipts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import { BaseContractInteraction, SendMethodOptions } from './base_contract_inte
export { SendMethodOptions };

/**
* Represents the options for a view method in a contract function interaction.
* Represents the options for simulating a contract function interaction.
* Allows specifying the address from which the view method should be called.
* Disregarded for simulation of public functions
*/
export type ViewMethodOptions = {
export type SimulateMethodOptions = {
/**
* The sender's Aztec address.
*/
Expand Down Expand Up @@ -64,10 +65,20 @@ export class ContractFunctionInteraction extends BaseContractInteraction {

/**
* Simulate a transaction and get its return values
* Differs from prove in a few important ways:
* 1. It returns the values of the function execution
* 2. It supports `unconstrained`, `private` and `public` functions
* 3. For `private` execution it:
* 3.a SKIPS the entrypoint and starts directly at the function
* 3.b SKIPS public execution entirely
* 4. For `public` execution it:
* 4.a Removes the `txRequest` value after ended simulation
* 4.b Ignores the `from` in the options
*
* @param options - An optional object containing additional configuration for the transaction.
* @returns The result of the view transaction as returned by the contract function.
* @returns The result of the transaction as returned by the contract function.
*/
public async simulate(options: ViewMethodOptions = {}): Promise<any> {
public async simulate(options: SimulateMethodOptions = {}): Promise<any> {
if (this.functionDao.functionType == FunctionType.UNCONSTRAINED) {
return this.wallet.viewTx(this.functionDao.name, this.args, this.contractAddress, options.from);
}
Expand All @@ -93,6 +104,7 @@ export class ContractFunctionInteraction extends BaseContractInteraction {
} else {
const txRequest = await this.create();
const vue = await this.pxe.simulateTx(txRequest, true);
this.txRequest = undefined;
return vue.publicReturnValues && vue.publicReturnValues[0];
}
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuit-types/src/tx/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
SideEffect,
SideEffectLinkedToNoteHash,
} from '@aztec/circuits.js';
import { ProcessReturnValues } from '@aztec/foundation/abi';
import { type ProcessReturnValues } from '@aztec/foundation/abi';
import { arrayNonEmptyLength } from '@aztec/foundation/collection';
import { BufferReader, serializeToBuffer } from '@aztec/foundation/serialize';

Expand Down
4 changes: 2 additions & 2 deletions yarn-project/end-to-end/src/docs_examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ describe('docs_examples', () => {
const _tx = await contract.methods.mint_public(wallet.getAddress(), 1).send().wait();
// docs:end:send_transaction

// docs:start:call_view_function
// docs:start:simulate_function
const balance = await contract.methods.balance_of_public(wallet.getAddress()).simulate();
expect(balance).toEqual(1n);
// docs:end:call_view_function
// docs:end:simulate_function
}, 120_000);
});
2 changes: 1 addition & 1 deletion yarn-project/end-to-end/src/e2e_avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('e2e_avm_simulator', () => {
describe('Storage', () => {
// FIX: Enable once the contract function works.
// it('Read immutable (initialized) storage (Field)', async () => {
// expect(await avmContact.methods.view_storage_immutable().view()).toEqual(42n);
// expect(await avmContact.methods.view_storage_immutable().simulate()).toEqual(42n);
// });

it('Modifies storage (Field)', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ import {
} from '@aztec/circuits.js';
import { computeVarArgsHash } from '@aztec/circuits.js/hash';
import {
ABIType,
DecodedReturn,
FunctionArtifact,
ProcessReturnValues,
type ABIType,
type DecodedReturn,
type FunctionArtifact,
type ProcessReturnValues,
decodeReturnValues,
} from '@aztec/foundation/abi';
import { arrayNonEmptyLength, padArrayEnd } from '@aztec/foundation/collection';
Expand Down
8 changes: 5 additions & 3 deletions yarn-project/simulator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ They are run with the assistance of an oracle that provides any value read from

Public functions can call other public function, but no private functions.

### Unconstrained (view) Functions
### Unconstrained Functions

Unconstrained functions are used to extract useful data for users, such as the user balance. They are not proved, and are simulated client-side.
Unconstrained functions are useful to extract useful data for users that could produce very large execution traces - such as the summed balance of all a users notes
They are not proved, and are simulated client-side.

They are run with the assistance of a DB oracle that provides any private data requested by the function.

At the moment, unconstrained functions cannot call any other function. It would be possible to allow them to call other unconstrained functions.
At the moment, unconstrained functions cannot call any other function.
It would be possible to allow them to call other unconstrained functions.

## Usage

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/simulator/src/client/private_execution.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { type FunctionData, PrivateCallStackItem, PrivateCircuitPublicInputs } from '@aztec/circuits.js';
import { type FunctionArtifactWithDebugMetadata, decodeReturnValues, type ABIType } from '@aztec/foundation/abi';
import { type ABIType, type FunctionArtifactWithDebugMetadata, decodeReturnValues } from '@aztec/foundation/abi';
import { type AztecAddress } from '@aztec/foundation/aztec-address';
import { Fr } from '@aztec/foundation/fields';
import { createDebugLogger } from '@aztec/foundation/log';
Expand Down

0 comments on commit 5ba8a79

Please sign in to comment.