Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
feat(aztec_noir): abstract storage initialisation (#2406)
Browse files Browse the repository at this point in the history
After a discussion with lasse, decided to get this over the line,
removes the storage init and places it inside the macro expansion

todo:
-  update docs
- release noir

related work:
- noir-lang/noir#2750


fixes: AztecProtocol/aztec-packages#2163
fixes: AztecProtocol/aztec-packages#1890
  • Loading branch information
Maddiaa0 authored and AztecBot committed Sep 27, 2023
1 parent 220b575 commit e6e9637
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 21 deletions.
8 changes: 8 additions & 0 deletions docs/dev_docs/contracts/syntax/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,14 @@ Each Aztec function has access to a [context](./context.mdx) object. This object
As previously mentioned we use the kernel to pass information between circuits. This means that the return values of functions must also be passed to the kernel (where they can be later passed on to another function).
We achieve this by pushing return values to the execution context, which we then pass to the kernel.

**Making the contract's storage available**
#include_code storage-example-context /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust

When a [`Storage` struct](./storage.md) is declared within a contract, the `storage` keyword is made available. As shown in the macro expansion above, this calls the init function on the storage struct with the current function's context.

Any state variables declared in the `Storage` struct can now be accessed as normal struct members.


**Returning the function context to the kernel.**
#include_code context-example-finish /yarn-project/noir-contracts/src/contracts/docs_example_contract/src/main.nr rust

Expand Down
9 changes: 1 addition & 8 deletions docs/dev_docs/contracts/syntax/storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,7 @@ impl Storage {
}
```

To use storage in functions, e.g., functions where you would read or write storage, you need to initialize the struct first, and then you can read and write afterwards. Below are snippets for initializing in private and public functions.

#include_code private_init_storage /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust
#include_code public_init_storage /yarn-project/noir-contracts/src/contracts/token_contract/src/main.nr rust

:::info
https://github.com/AztecProtocol/aztec-packages/pull/2406 is removing the need to explicitly initialize the storage in each function before reading or writing. This will be updated in the docs once the PR is merged.
:::
If you have defined a `Storage` struct following this naming scheme, then it will be made available to you through the reserved `storage` keyword within your contract functions.

:::warning Using slot `0` is not supported!
No storage values should be initialized at slot `0`. This is a known issue that will be fixed in the future.
Expand Down
15 changes: 2 additions & 13 deletions docs/dev_docs/tutorials/writing_token_contract.md
Original file line number Diff line number Diff line change
Expand Up @@ -293,13 +293,8 @@ Public functions are declared with the `#[aztec(public)]` macro above the functi

As described in the [execution contexts section above](#execution-contexts), public function logic and transaction information is transparent to the world. Public functions update public state, but can be used to prepare data to be used in a private context, as we will go over below (e.g. see the [shield](#shield) function).

Every public function initializes storage using the public context like so:

```rust
let storage = Storage::init(Context::public(&mut context));
```

After this, storage is referenced as `storage.variable`. We won't go over this step in any of the following function descriptions.
Storage is referenced as `storage.variable`.

#### `set_admin`

Expand Down Expand Up @@ -374,13 +369,7 @@ Private functions are declared with the `#[aztec(private)]` macro above the func

As described in the [execution contexts section above](#execution-contexts), private function logic and transaction information is hidden from the world and is executed on user devices. Private functions update private state, but can pass data to the public execution context (e.g. see the [`unshield`](#unshield) function).

Every private function initializes storage using the private context like so:

```rust
let storage = Storage::init(Context::private(&mut context));
```

After this, storage is referenced as `storage.variable`. We won't go over this step in any of the following function descriptions.
Storage is referenced as `storage.variable`.

#### `redeem_shield`

Expand Down

0 comments on commit e6e9637

Please sign in to comment.