Skip to content

Commit

Permalink
feat!: init storage macro (#4200)
Browse files Browse the repository at this point in the history
Closes: AztecProtocol/aztec-packages#3198,
AztecProtocol/aztec-packages#2928

~~Requires AztecProtocol/aztec-packages#4135,
which is blocked by noir-lang/noir#4124

Automatic storage initialization via aztec macro. 

Full support of public and private state from
`dep::aztec::state_vars::*`, including Maps (and nested Maps!)
Limited support for custom types (as long as they have a single
serializable generic and their constructor is `::new(context,
storage_slot`).

~~Pending: better errors, code comments and some cleanup.~~

Hijacking my own
[comment](AztecProtocol/aztec-packages#4200 (comment))
for the explanation:

The idea behind this is that in 99% of cases, storage initialization
(that is, the `impl` for a given `struct Storage...` is redundant, and
the only need for its existence was assigning storage slots...which in
turn were necessary because we didn't know how to serialize the data
structures that were used in a given contract or how much space they
used once serialized (relevant for the public state).

After AztecProtocol/aztec-packages#4135 is
merged, both of those things don't have to be explicitly provided since
we're using traits, so the aztec macro can infer the implementation of
the Storage struct just by taking hints from the definition. An example:

```rust
    struct Storage {
        // MyAwesomeStuff implements Serialize<2>, so we assign it slot 1 (and remember that it will take 2 slots due to its size)
        public_var: PublicState<MyAwesomeSuff>, 
        // Right after the first one, assign it to slot: current_slot + previous_size = 3
        another_public_var: PublicState<MyAwesomeSuff>,
        // Private and Public state don't share slots since they "live" in different trees, but keeping the slot count simplifies implementation. 
        // Notes also implement Serialize<N>, but they only take up 1 slot anyways because of hashing, assign it slot 5
        a_singleton: Singleton<ANote>,
        // Maps derive slots via hashing, so we can assume they only "take" 1 slot. We assign it slot 6
        balances: Map<AztecAddress, Singleton<ANote>>,
        // Slot 7
        a_set: Set<ANote>,
        // Slot 8
        imm_singleton: ImmutableSingleton<ANote>,
        // Slot 9. 
        profiles: Map<AztecAddress, Map<Singleton<ANote>>>,
    }

```

We have all the info we need in the AST and HIR to build this
automatically:

```rust
impl Storage {
    fn init(context: Context) -> Self {
        Storage {
            public_var: PublicState::new(context, 1), // No need for serialization methods, taken from the the trait impl
            another_public_var: PublicState::new(context, 3),
            a_singleton: Singleton::new(context, 5),
            // Map init lambda always takes the same form for known storage structs
            balances: Map::new(context, 6, |context, slot| { 
                Singleton::new(context, slot) 
            }),
            a_set: Set::new(context, 7),
            imm_singleton: ImmutableSingleton::new(context, 8),
            // A map of maps is just nesting lambdas, we can infer this too
            profiles: Map::new(context, 9, |context, slot| { 
                Map::new(context, slot, |context, slot| { 
                    Singleton::new(context, slot) 
                })
            })
        }
    }
}

```

...as long as we use "canonical" storage implementations. This means
`AStoragePrimitive<SomethingSerializable>` and
`Map<SomethingWithToField, AStoragePrimitive<SomethingSerializable>>`.

**TLDR:** define the Storage struct, in 99% of cases the macro takes
care of the implementation!

Implementing custom storage will look just like it does know, the macro
will skip automatic generation if it finds one.

---------

Co-authored-by: sirasistant <sirasistant@gmail.com>
  • Loading branch information
2 people authored and AztecBot committed Feb 3, 2024
1 parent 0f83729 commit 2585f25
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions aztec/src/state_vars.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ mod public_state;
mod set;
mod singleton;
mod stable_public_state;
mod storage;
3 changes: 3 additions & 0 deletions aztec/src/state_vars/immutable_singleton.nr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::note::{
note_viewer_options::NoteViewerOptions,
};
use crate::oracle::notes::check_nullifier_exists;
use crate::state_vars::storage::Storage;

// docs:start:struct
struct ImmutableSingleton<Note> {
Expand All @@ -24,6 +25,8 @@ struct ImmutableSingleton<Note> {
}
// docs:end:struct

impl<T> Storage<T> for ImmutableSingleton<T> {}

impl<Note> ImmutableSingleton<Note> {
// docs:start:new
pub fn new(
Expand Down
3 changes: 3 additions & 0 deletions aztec/src/state_vars/map.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use dep::protocol_types::{
hash::pedersen_hash,
traits::{ToField}
};
use crate::state_vars::storage::Storage;

// docs:start:map
struct Map<K, V> {
Expand All @@ -13,6 +14,8 @@ struct Map<K, V> {
}
// docs:end:map

impl<K, T> Storage<T> for Map<K, T> {}

impl<K, V> Map<K, V> {
// docs:start:new
pub fn new(
Expand Down
3 changes: 3 additions & 0 deletions aztec/src/state_vars/public_state.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::oracle::storage::storage_read;
use crate::oracle::storage::storage_write;
use dep::std::option::Option;
use dep::protocol_types::traits::{Deserialize, Serialize};
use crate::state_vars::storage::Storage;

// docs:start:public_state_struct
struct PublicState<T> {
Expand All @@ -11,6 +12,8 @@ struct PublicState<T> {
}
// docs:end:public_state_struct

impl<T> Storage<T> for PublicState<T> {}

impl<T> PublicState<T> {
// docs:start:public_state_struct_new
pub fn new(
Expand Down
4 changes: 3 additions & 1 deletion aztec/src/state_vars/set.nr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::note::{
note_viewer_options::NoteViewerOptions,
utils::compute_note_hash_for_consumption,
};
use crate::state_vars::storage::Storage;

// docs:start:struct
struct Set<Note> {
Expand All @@ -23,6 +24,8 @@ struct Set<Note> {
}
// docs:end:struct

impl<T> Storage<T> for Set<T> {}

impl<Note> Set<Note> {
// docs:start:new
pub fn new(
Expand All @@ -36,7 +39,6 @@ impl<Note> Set<Note> {
}
}
// docs:end:new

// docs:start:insert
pub fn insert<N>(self,
note: &mut Note,
Expand Down
3 changes: 3 additions & 0 deletions aztec/src/state_vars/singleton.nr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::oracle::{
nullifier_key::get_nullifier_secret_key,
notes::check_nullifier_exists,
};
use crate::state_vars::storage::Storage;

// docs:start:struct
struct Singleton<Note> {
Expand All @@ -28,6 +29,8 @@ struct Singleton<Note> {
}
// docs:end:struct

impl<T> Storage<T> for Singleton<T> {}

impl<Note> Singleton<Note> {
// docs:start:new
pub fn new(
Expand Down
3 changes: 3 additions & 0 deletions aztec/src/state_vars/stable_public_state.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ use crate::oracle::{
use crate::history::public_value_inclusion::prove_public_value_inclusion;
use dep::std::option::Option;
use dep::protocol_types::traits::{Deserialize, Serialize};
use crate::state_vars::storage::Storage;

struct StablePublicState<T>{
context: Context,
storage_slot: Field,
}

impl<T> Storage<T> for StablePublicState<T> {}

impl<T> StablePublicState<T> {
pub fn new(
// Note: Passing the contexts to new(...) just to have an interface compatible with a Map.
Expand Down
8 changes: 8 additions & 0 deletions aztec/src/state_vars/storage.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use crate::context::{Context};
use dep::protocol_types::traits::{Deserialize, Serialize};

trait Storage<T> where T: Serialize<N> + Deserialize<N> {
fn get_storage_slot(self) -> Field {
self.storage_slot
}
}

0 comments on commit 2585f25

Please sign in to comment.