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

chore(docs): Split contract storage pages #4202

Merged
merged 31 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
01da516
split pages
critesjosh Jan 19, 2024
973c2fb
Merge branch 'master' into jc/update-contract-storage
critesjosh Jan 24, 2024
11d82f9
Revert "Merge branch 'master' into jc/update-contract-storage"
critesjosh Jan 24, 2024
ef7b567
Revert "Revert "Merge branch 'master' into jc/update-contract-storage""
critesjosh Jan 24, 2024
0b25423
Update scalar_multiplication.test.cpp
critesjosh Jan 24, 2024
64de80b
Update scalar_multiplication.test.cpp
critesjosh Jan 24, 2024
ea39172
Update thread.hpp
critesjosh Jan 24, 2024
de1b625
Update hmac.hpp
critesjosh Jan 24, 2024
7132dad
Update affine_element.hpp
critesjosh Jan 24, 2024
56da562
Update affine_element_impl.hpp
critesjosh Jan 24, 2024
e6efe03
Update affine_element_impl.hpp
critesjosh Jan 24, 2024
17059a3
Update polynomial.hpp
critesjosh Jan 24, 2024
b6b843b
Update polynomial.hpp
critesjosh Jan 24, 2024
e1e7c1e
Update polynomial.hpp
critesjosh Jan 24, 2024
1f7ae0f
Update thread.hpp
critesjosh Jan 24, 2024
a322930
Update thread.hpp
critesjosh Jan 24, 2024
96546ce
remove last line
critesjosh Jan 24, 2024
ed3fd24
undo fmt
critesjosh Jan 24, 2024
8ba302c
undo fmt
critesjosh Jan 24, 2024
e496064
undo fmt
critesjosh Jan 24, 2024
968b214
undo fmt
critesjosh Jan 24, 2024
86f11cc
Update thread.hpp
critesjosh Jan 24, 2024
c2b648f
Merge branch 'jc/update-contract-storage' of https://github.com/Aztec…
critesjosh Jan 24, 2024
c0d3cd6
update public state
critesjosh Jan 24, 2024
abd7796
update private state docs
critesjosh Jan 25, 2024
69e9eae
Update thread.hpp
critesjosh Jan 25, 2024
24e0b01
Update thread.hpp
critesjosh Jan 25, 2024
d9d753b
Update thread.hpp
critesjosh Jan 25, 2024
5435cc0
Merge branch 'master' into jc/update-contract-storage
catmcgee Jan 26, 2024
525cbca
fixed thread.hpp
catmcgee Jan 26, 2024
0cdec83
Merge branch 'master' into jc/update-contract-storage
catmcgee Jan 26, 2024
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
@@ -1,5 +1,5 @@
---
title: Private <--> Public execution
title: Private - Public execution
---

import Image from "@theme/IdealImage";
Expand Down
434 changes: 14 additions & 420 deletions docs/docs/dev_docs/contracts/syntax/storage/main.md

Large diffs are not rendered by default.

337 changes: 337 additions & 0 deletions docs/docs/dev_docs/contracts/syntax/storage/private_state.md

Large diffs are not rendered by default.

95 changes: 95 additions & 0 deletions docs/docs/dev_docs/contracts/syntax/storage/public_state.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: Public State
---

On this page we will look at how to manage public state in Aztec contracts. We will look at how to declare public state, how to read and write to it, and how to use it in your contracts.

For a higher level overview of the state model in Aztec, see the [state model](../../../../concepts/foundation/state_model/main.md) page, or jump back to the previous page on [Storage](./main.md).

## Overview

The `PublicState` struct is generic over the variable type `T` and its serialized size `T_SERIALIZED_LEN`.

:::info
Currently, the length of the types must be specified when declaring the storage struct but the intention is that this will be inferred in the future.
:::

The struct contains a `storage_slot` which, similar to Ethereum, is used to figure out _where_ in storage the variable is located. Notice that while we don't have the exact same [state model](../../../../concepts/foundation/state_model/main.md) as EVM chains it will look similar from the contract developers point of view.

Beyond the struct, the `PublicState` also contains `serialization_methods`, which is a struct with methods that instruct the `PublicState` how to serialize and deserialize the variable. You can find the details of `PublicState` in the implementation [here](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/yarn-project/aztec-nr/aztec/src/state_vars/public_state.nr).

The Aztec.nr library provides serialization methods for various common types.

:::info
An example using a larger struct can be found in the [lending example](https://github.com/AztecProtocol/aztec-packages/tree/master/yarn-project/noir-contracts/contracts/lending_contract)'s use of an [`Asset`](https://github.com/AztecProtocol/aztec-packages/tree/#include_aztec_version/yarn-project/noir-contracts/contracts/lending_contract/src/asset.nr).
:::

### `new`

When declaring the storage for `T` as a persistent public storage variable, we use the `PublicState::new()` constructor. As seen below, this takes the `storage_slot` and the `serialization_methods` as arguments along with the [`Context`](../context.mdx), which in this case is used to share interface with other structures. You can view the implementation [here](https://github.com/AztecProtocol/aztec-packages/blob/#include_aztec_version/yarn-project/aztec-nr/aztec/src/state_vars/public_state.nr).

#### Single value example

Say that we wish to add `admin` public state variable into our storage struct. In the struct we can define it as:

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

And then when initializing it in the `Storage::init` function we can do:

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

We have specified that we are storing a `Field` that should be placed in storage slot `1`. This is just a single value, and is similar to the following in solidity:

```solidity
address internal admin;
```

:::info
We know its verbose, and are working on making it less so.
:::

#### Mapping example

Say we want to have a group of `minters` that are able to mint assets in our contract, and we want them in public storage, because [access control in private is quite cumbersome](../../../../concepts/foundation/communication/public_private_calls/main.md#a-note-on-l2-access-control). In the `Storage` struct we can add it as follows:

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

And then when initializing it in the `Storage::init` function we can do it as follows:

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

In this case, specifying that we are dealing with a map of Fields, and that it should be put at slot 2.

This would be similar to the following in solidity:

```solidity
mapping(address => bool) internal minters;
```

### `read`

On the `PublicState` structs we have a `read` method to read the value at the location in storage and using the specified deserialization method to deserialize it.

#### Reading from our `admin` example

For our `admin` example from earlier, this could be used as follows to check that the stored value matches the `msg_sender()`.

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

#### Reading from our `minters` example

As we saw in the Map earlier, a very similar operation can be done to perform a lookup in a map.

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

### `write`

We have a `write` method on the `PublicState` struct that takes the value to write as an input and saves this in storage. It uses the serialization method to serialize the value which inserts (possibly multiple) values into storage.

#### Writing to our `admin` example

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

#### Writing to our `minters` example

#include_code write_minter /yarn-project/noir-contracts/contracts/token_contract/src/main.nr rust
6 changes: 5 additions & 1 deletion docs/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,11 @@ const sidebars = {
type: "doc",
id: "dev_docs/contracts/syntax/storage/main",
},
items: ["dev_docs/contracts/syntax/storage/storage_slots"],
items: [
"dev_docs/contracts/syntax/storage/public_state",
"dev_docs/contracts/syntax/storage/private_state",
"dev_docs/contracts/syntax/storage/storage_slots",
],
},
"dev_docs/contracts/syntax/events",
"dev_docs/contracts/syntax/functions",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,11 @@ contract DocsExample {
storage.legendary_card.view_note()
}

// docs:start:singleton_is_initialized
unconstrained fn is_legendary_initialized() -> pub bool {
storage.legendary_card.is_initialized()
}
// docs:end:singleton_is_initialized

unconstrained fn get_imm_card() -> pub CardNote {
storage.imm_singleton.view_note()
Expand Down
Loading