From f56f7aa3796b84e2e06b56a8aa59c9385d020f64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=A1s=20Venturo?= Date: Mon, 25 Nov 2024 17:52:17 -0300 Subject: [PATCH] feat!: remove SharedImmutable (#10183) Working on https://github.com/AztecProtocol/aztec-packages/pull/10164 made me realize that this state variable is now useless: it is the exact same as `PublicImmutable` except it also has getters for private execution contexts. I deleted it entirely in favor of `PublicImmutable`, which should be less surprising for a library user. After all, reading publicly known immutable values during private execution is fine. I updated the docs and tests accordingly, mostly keeping the shared immut versions as those were more complete. --- aztec/src/prelude.nr | 2 +- aztec/src/state_vars/mod.nr | 2 - aztec/src/state_vars/public_immutable.nr | 28 ++++++++- aztec/src/state_vars/shared_immutable.nr | 78 ------------------------ 4 files changed, 26 insertions(+), 84 deletions(-) delete mode 100644 aztec/src/state_vars/shared_immutable.nr diff --git a/aztec/src/prelude.nr b/aztec/src/prelude.nr index d1b5b34..861ef71 100644 --- a/aztec/src/prelude.nr +++ b/aztec/src/prelude.nr @@ -11,7 +11,7 @@ pub use crate::{ state_vars::{ map::Map, private_immutable::PrivateImmutable, private_mutable::PrivateMutable, private_set::PrivateSet, public_immutable::PublicImmutable, public_mutable::PublicMutable, - shared_immutable::SharedImmutable, shared_mutable::SharedMutable, storage::Storable, + shared_mutable::SharedMutable, storage::Storable, }, }; pub use dep::protocol_types::{ diff --git a/aztec/src/state_vars/mod.nr b/aztec/src/state_vars/mod.nr index 609cf77..38230bd 100644 --- a/aztec/src/state_vars/mod.nr +++ b/aztec/src/state_vars/mod.nr @@ -4,7 +4,6 @@ pub mod private_mutable; pub mod public_immutable; pub mod public_mutable; pub mod private_set; -pub mod shared_immutable; pub mod shared_mutable; pub mod storage; @@ -14,6 +13,5 @@ pub use crate::state_vars::private_mutable::PrivateMutable; pub use crate::state_vars::private_set::PrivateSet; pub use crate::state_vars::public_immutable::PublicImmutable; pub use crate::state_vars::public_mutable::PublicMutable; -pub use crate::state_vars::shared_immutable::SharedImmutable; pub use crate::state_vars::shared_mutable::SharedMutable; pub use crate::state_vars::storage::Storage; diff --git a/aztec/src/state_vars/public_immutable.nr b/aztec/src/state_vars/public_immutable.nr index ae971bc..a3e5df8 100644 --- a/aztec/src/state_vars/public_immutable.nr +++ b/aztec/src/state_vars/public_immutable.nr @@ -1,10 +1,14 @@ -use crate::{context::{PublicContext, UnconstrainedContext}, state_vars::storage::Storage}; +use crate::{ + context::{PrivateContext, PublicContext, UnconstrainedContext}, + state_vars::storage::Storage, +}; use dep::protocol_types::{ constants::INITIALIZATION_SLOT_SEPARATOR, traits::{Deserialize, Serialize}, }; -// Just like SharedImmutable but without the ability to read from private functions. +/// Stores an immutable value in public state which can be read from public, private and unconstrained execution +/// contexts. // docs:start:public_immutable_struct pub struct PublicImmutable { context: Context, @@ -57,9 +61,27 @@ where impl PublicImmutable where - T: Deserialize, + T: Serialize + Deserialize, { pub unconstrained fn read(self) -> T { self.context.storage_read(self.storage_slot) } } + +impl PublicImmutable +where + T: Serialize + Deserialize, +{ + pub fn read(self) -> T { + let header = self.context.get_header(); + let mut fields = [0; T_SERIALIZED_LEN]; + + for i in 0..fields.len() { + fields[i] = header.public_storage_historical_read( + self.storage_slot + i as Field, + (*self.context).this_address(), + ); + } + T::deserialize(fields) + } +} diff --git a/aztec/src/state_vars/shared_immutable.nr b/aztec/src/state_vars/shared_immutable.nr deleted file mode 100644 index ea5ac0b..0000000 --- a/aztec/src/state_vars/shared_immutable.nr +++ /dev/null @@ -1,78 +0,0 @@ -use crate::{ - context::{PrivateContext, PublicContext, UnconstrainedContext}, - state_vars::storage::Storage, -}; -use dep::protocol_types::{ - constants::INITIALIZATION_SLOT_SEPARATOR, - traits::{Deserialize, Serialize}, -}; - -// Just like PublicImmutable but with the ability to read from private functions. -pub struct SharedImmutable { - context: Context, - storage_slot: Field, -} - -impl Storage for SharedImmutable -where - T: Serialize + Deserialize, -{} - -impl SharedImmutable { - pub fn new( - // Note: Passing the contexts to new(...) just to have an interface compatible with a Map. - context: Context, - storage_slot: Field, - ) -> Self { - assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1."); - Self { context, storage_slot } - } -} - -impl SharedImmutable -where - T: Serialize + Deserialize, -{ - // Intended to be only called once. - pub fn initialize(self, value: T) { - // We check that the struct is not yet initialized by checking if the initialization slot is 0 - let initialization_slot = INITIALIZATION_SLOT_SEPARATOR + self.storage_slot; - let init_field: Field = self.context.storage_read(initialization_slot); - assert(init_field == 0, "SharedImmutable already initialized"); - - // We populate the initialization slot with a non-zero value to indicate that the struct is initialized - self.context.storage_write(initialization_slot, 0xdead); - self.context.storage_write(self.storage_slot, value); - } - - pub fn read(self) -> T { - self.context.storage_read(self.storage_slot) - } -} - -impl SharedImmutable -where - T: Serialize + Deserialize, -{ - pub unconstrained fn read(self) -> T { - self.context.storage_read(self.storage_slot) - } -} - -impl SharedImmutable -where - T: Serialize + Deserialize, -{ - pub fn read(self) -> T { - let header = self.context.get_header(); - let mut fields = [0; T_SERIALIZED_LEN]; - - for i in 0..fields.len() { - fields[i] = header.public_storage_historical_read( - self.storage_slot + i as Field, - (*self.context).this_address(), - ); - } - T::deserialize(fields) - } -}