From 9edf1cb235cb33ef5eac81bd252fc711dcd929eb Mon Sep 17 00:00:00 2001 From: benesjan Date: Tue, 27 Feb 2024 09:41:04 +0000 Subject: [PATCH] WIP --- .../aztec-nr/aztec/src/state_vars.nr | 2 ++ .../aztec/src/state_vars/public_immutable.nr | 27 ++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/state_vars.nr b/noir-projects/aztec-nr/aztec/src/state_vars.nr index 4201723eb07..f10b2ac487e 100644 --- a/noir-projects/aztec-nr/aztec/src/state_vars.nr +++ b/noir-projects/aztec-nr/aztec/src/state_vars.nr @@ -1,6 +1,7 @@ mod map; mod private_immutable; mod private_mutable; +mod public_immutable; mod public_mutable; mod private_set; mod shared_immutable; @@ -9,6 +10,7 @@ mod storage; use crate::state_vars::map::Map; use crate::state_vars::private_immutable::PrivateImmutable; use crate::state_vars::private_mutable::PrivateMutable; +use crate::state_vars::public_immutable::PublicImmutable; use crate::state_vars::public_mutable::PublicMutable; use crate::state_vars::private_set::PrivateSet; use crate::state_vars::shared_immutable::SharedImmutable; diff --git a/noir-projects/aztec-nr/aztec/src/state_vars/public_immutable.nr b/noir-projects/aztec-nr/aztec/src/state_vars/public_immutable.nr index bbda1e0f509..17dd73efed6 100644 --- a/noir-projects/aztec-nr/aztec/src/state_vars/public_immutable.nr +++ b/noir-projects/aztec-nr/aztec/src/state_vars/public_immutable.nr @@ -1,9 +1,5 @@ -use crate::context::{Context}; -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; +use crate::{context::Context, oracle::{storage::{storage_read, storage_write}}, state_vars::storage::Storage}; +use dep::protocol_types::{constants::INITIALIZATION_SLOT_SEPARATOR, traits::{Deserialize, Serialize}}; // docs:start:public_immutable_struct struct PublicImmutable { @@ -29,10 +25,23 @@ impl PublicImmutable { // docs:start:public_immutable_struct_write pub fn initialize(self, value: T) where T: Serialize { assert( - self.context.private.is_none(), "PublicImmutable initialization only supported in public functions" + self.context.private.is_none(), "PublicImmutable can only be initialized from public functions" ); - let fields = T::serialize(value); - storage_write(self.storage_slot, fields); + // TODO(#4738): Uncomment the following assert + // assert( + // self.context.public.unwrap_unchecked().is_deployment(), "PublicImmutable can only be initialized during contract deployment" + // ); + + // 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 fields_read: [Field; 1] = storage_read(initialization_slot); + assert(fields_read[0] == 0, "PublicImmutable already initialized"); + + // We populate the initialization slot with a non-zero value to indicate that the struct is initialized + storage_write(initialization_slot, [0xdead]); + + let fields_write = T::serialize(value); + storage_write(self.storage_slot, fields_write); } // docs:end:public_immutable_struct_write