From 3211c154286f31e1f30b886710e2f5dbf8de78a1 Mon Sep 17 00:00:00 2001 From: superstar0402 Date: Fri, 2 Feb 2024 15:34:25 +0100 Subject: [PATCH] feat!: init storage macro (#4200) Closes: https://github.com/AztecProtocol/aztec-packages/issues/3198, https://github.com/AztecProtocol/aztec-packages/issues/2928 ~~Requires https://github.com/AztecProtocol/aztec-packages/pull/4135, which is blocked by https://github.com/noir-lang/noir/issues/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](https://github.com/AztecProtocol/aztec-packages/pull/4200#issuecomment-1914494507) 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 https://github.com/AztecProtocol/aztec-packages/pull/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, // Right after the first one, assign it to slot: current_slot + previous_size = 3 another_public_var: PublicState, // 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, but they only take up 1 slot anyways because of hashing, assign it slot 5 a_singleton: Singleton, // Maps derive slots via hashing, so we can assume they only "take" 1 slot. We assign it slot 6 balances: Map>, // Slot 7 a_set: Set, // Slot 8 imm_singleton: ImmutableSingleton, // Slot 9. profiles: Map>>, } ``` 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` and `Map>`. **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 --- aztec/src/state_vars.nr | 1 + aztec/src/state_vars/immutable_singleton.nr | 3 +++ aztec/src/state_vars/map.nr | 3 +++ aztec/src/state_vars/public_state.nr | 3 +++ aztec/src/state_vars/set.nr | 4 +++- aztec/src/state_vars/singleton.nr | 3 +++ aztec/src/state_vars/stable_public_state.nr | 3 +++ aztec/src/state_vars/storage.nr | 8 ++++++++ 8 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 aztec/src/state_vars/storage.nr diff --git a/aztec/src/state_vars.nr b/aztec/src/state_vars.nr index d8213bb..177844f 100644 --- a/aztec/src/state_vars.nr +++ b/aztec/src/state_vars.nr @@ -4,3 +4,4 @@ mod public_state; mod set; mod singleton; mod stable_public_state; +mod storage; diff --git a/aztec/src/state_vars/immutable_singleton.nr b/aztec/src/state_vars/immutable_singleton.nr index 187feca..9d59508 100644 --- a/aztec/src/state_vars/immutable_singleton.nr +++ b/aztec/src/state_vars/immutable_singleton.nr @@ -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 { @@ -24,6 +25,8 @@ struct ImmutableSingleton { } // docs:end:struct +impl Storage for ImmutableSingleton {} + impl ImmutableSingleton { // docs:start:new pub fn new( diff --git a/aztec/src/state_vars/map.nr b/aztec/src/state_vars/map.nr index fefa84e..7e38552 100644 --- a/aztec/src/state_vars/map.nr +++ b/aztec/src/state_vars/map.nr @@ -4,6 +4,7 @@ use dep::protocol_types::{ hash::pedersen_hash, traits::{ToField} }; +use crate::state_vars::storage::Storage; // docs:start:map struct Map { @@ -13,6 +14,8 @@ struct Map { } // docs:end:map +impl Storage for Map {} + impl Map { // docs:start:new pub fn new( diff --git a/aztec/src/state_vars/public_state.nr b/aztec/src/state_vars/public_state.nr index 9309f3d..081b0d5 100644 --- a/aztec/src/state_vars/public_state.nr +++ b/aztec/src/state_vars/public_state.nr @@ -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 { @@ -11,6 +12,8 @@ struct PublicState { } // docs:end:public_state_struct +impl Storage for PublicState {} + impl PublicState { // docs:start:public_state_struct_new pub fn new( diff --git a/aztec/src/state_vars/set.nr b/aztec/src/state_vars/set.nr index 03e53b3..6813a9f 100644 --- a/aztec/src/state_vars/set.nr +++ b/aztec/src/state_vars/set.nr @@ -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 { @@ -23,6 +24,8 @@ struct Set { } // docs:end:struct +impl Storage for Set {} + impl Set { // docs:start:new pub fn new( @@ -36,7 +39,6 @@ impl Set { } } // docs:end:new - // docs:start:insert pub fn insert(self, note: &mut Note, diff --git a/aztec/src/state_vars/singleton.nr b/aztec/src/state_vars/singleton.nr index 19b4e91..47d8aef 100644 --- a/aztec/src/state_vars/singleton.nr +++ b/aztec/src/state_vars/singleton.nr @@ -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 { @@ -28,6 +29,8 @@ struct Singleton { } // docs:end:struct +impl Storage for Singleton {} + impl Singleton { // docs:start:new pub fn new( diff --git a/aztec/src/state_vars/stable_public_state.nr b/aztec/src/state_vars/stable_public_state.nr index 013f059..af4925f 100644 --- a/aztec/src/state_vars/stable_public_state.nr +++ b/aztec/src/state_vars/stable_public_state.nr @@ -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{ context: Context, storage_slot: Field, } +impl Storage for StablePublicState {} + impl StablePublicState { pub fn new( // Note: Passing the contexts to new(...) just to have an interface compatible with a Map. diff --git a/aztec/src/state_vars/storage.nr b/aztec/src/state_vars/storage.nr new file mode 100644 index 0000000..5f28b55 --- /dev/null +++ b/aztec/src/state_vars/storage.nr @@ -0,0 +1,8 @@ +use crate::context::{Context}; +use dep::protocol_types::traits::{Deserialize, Serialize}; + +trait Storage where T: Serialize + Deserialize { + fn get_storage_slot(self) -> Field { + self.storage_slot + } +}