Skip to content

Commit

Permalink
feat: throw compile error if read/write public state from private (#2804
Browse files Browse the repository at this point in the history
)

Currently if accessing public state from private functions you will only
get an error at runtime. This pr changes such that you will receive a
compile-time error instead.
  • Loading branch information
LHerskind authored Oct 12, 2023
1 parent 7ae554a commit a3649df
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
6 changes: 5 additions & 1 deletion yarn-project/aztec-nr/aztec/src/state_vars/public_state.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use dep::std::option::Option;

// docs:start:public_state_struct
struct PublicState<T, T_SERIALIZED_LEN> {
context: Context,
storage_slot: Field,
serialization_methods: TypeSerializationInterface<T, T_SERIALIZED_LEN>,
}
Expand All @@ -15,12 +16,13 @@ impl<T, T_SERIALIZED_LEN> PublicState<T, T_SERIALIZED_LEN> {
// docs:start:public_state_struct_new
pub fn new(
// Note: Passing the contexts to new(...) just to have an interface compatible with a Map.
_: Context,
context: Context,
storage_slot: Field,
serialization_methods: TypeSerializationInterface<T, T_SERIALIZED_LEN>,
) -> Self {
assert(storage_slot != 0, "Storage slot 0 not allowed. Storage slots must start from 1.");
PublicState {
context,
storage_slot,
serialization_methods,
}
Expand All @@ -29,12 +31,14 @@ impl<T, T_SERIALIZED_LEN> PublicState<T, T_SERIALIZED_LEN> {

// docs:start:public_state_struct_read
pub fn read(self) -> T {
assert(self.context.private.is_none(), "Public state writes only supported in public functions");
storage_read(self.storage_slot, self.serialization_methods.deserialize)
}
// docs:end:public_state_struct_read

// docs:start:public_state_struct_write
pub fn write(self, value: T) {
assert(self.context.private.is_none(), "Public state writes only supported in public functions");
let serialize = self.serialization_methods.serialize;
let fields = serialize(value);
storage_write(self.storage_slot, fields);
Expand Down
Loading

0 comments on commit a3649df

Please sign in to comment.