Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: throw compile error if read/write public state from private #2804

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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