You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
These contain a basic explainer on some state variable concepts, add
some missing bits there (like a table with different kinds of statevars
and their usecases), and greatly expands on (completes) `Map`
documentation.
/// ## Usage Maps are typically declared in your contract's [`storage`](crate::macros::storage::storage) struct and accessed using the `at(key)` method to get the state variable for a specific key. The resulting state variable can then be read from or written to using its own methods.
35
+
/// ### Multiple `Map`s
36
36
///
37
-
/// Note that maps cannot be used with owned state variables (variables that implement the OwnedStateVariable trait) -
38
-
/// those need to be wrapped in an `Owned` state variable instead.
37
+
/// There is no limit to how many `Map` containers a contract can have, and `Map`s can themselves wrap other `Map`s,
38
+
/// resulting in nested layouts.
39
39
///
40
-
/// ## Advanced Internally, `Map` uses a single base storage slot to represent the mapping itself, similar to Solidity's approach. Individual key-value pairs are stored at derived storage slots computed by hashing the base storage slot with the key using Poseidon2. This ensures:
41
-
/// - No storage slot collisions between different keys
42
-
/// - Uniform distribution of storage slots across the storage space
43
-
/// - Compatibility with Aztec's storage tree structure
44
-
/// - Gas-efficient storage access patterns similar to Solidity mappings
40
+
/// ```noir
41
+
/// #[storage]
42
+
/// struct Storage<C> {
43
+
/// // A nested map where the first key is an address and the second a `Year` type, such that
44
+
/// // self.storage.user_yearly_config.at(user).at(year) results in distinct `UserConfig` variables for each
//! | [`PublicMutable`](crate::state_vars::PublicMutable) | yes | no | no | Configuration of admins, global state (e.g. token total supply, total votes) |
40
+
//! | [`PublicImmutable`](crate::state_vars::PublicImmutable) | no | yes | no | Fixed configuration, one-way actions (e.g. initialization settings for a proposal) |
41
+
//! | [`DelayedPublicMutable`](crate::state_vars::DelayedPublicMutable) | yes (after a delay) | yes | no | Non time sensitive system configuration |
42
+
//!
43
+
//! ### Private State Variables
44
+
//!
45
+
//! Private state variables typically store their contents in the note and nullifier trees, and are therefore mainly
46
+
//! interacted with from private contract functions.
47
+
//!
48
+
//! | State Variable | Mutable? | Cost to Read? | Writable by Third Parties? | Example Use Case |
//! | [`PrivateMutable`](crate::state_vars::PrivateMutable) | yes | yes | no | Mutable user state only accessible by them (e.g. user settings or keys) |
51
+
//! | [`PrivateImmutable`](crate::state_vars::PrivateImmutable) | no | no | no | Fixed configuration, one-way actions (e.g. initialization settings for a proposal) |
52
+
//! | [`PrivateSet`](crate::state_vars::PrivateSet) | yes | yes | yes | Aggregated state others can add to, e.g. token balance (set of amount notes), NFT collections (set of NFT ids) |
53
+
//!
54
+
//! ## Storage Slots
55
+
//!
56
+
//! Each state variable in a contract gets assigned a unique storage slot, which isolates the different variables so
57
+
//! that reads and writes to one do not interfere with others.
58
+
//!
59
+
//! Storage slot allocation is automatic and cannot be overridden. A state variable's storage slot can be queried via
0 commit comments