Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Feb 27, 2024
1 parent 5cc9f1f commit 9edf1cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions noir-projects/aztec-nr/aztec/src/state_vars.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod map;
mod private_immutable;
mod private_mutable;
mod public_immutable;
mod public_mutable;
mod private_set;
mod shared_immutable;
Expand All @@ -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;
Expand Down
27 changes: 18 additions & 9 deletions noir-projects/aztec-nr/aztec/src/state_vars/public_immutable.nr
Original file line number Diff line number Diff line change
@@ -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<T> {
Expand All @@ -29,10 +25,23 @@ impl<T> PublicImmutable<T> {
// docs:start:public_immutable_struct_write
pub fn initialize<T_SERIALIZED_LEN>(self, value: T) where T: Serialize<T_SERIALIZED_LEN> {
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

Expand Down

0 comments on commit 9edf1cb

Please sign in to comment.