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: add unconstrained getters to sharedmutable #7429

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::protocol_types::{
traits::{FromField, ToField}
};

use crate::context::{PrivateContext, PublicContext};
use crate::context::{PrivateContext, PublicContext, UnconstrainedContext};
use crate::state_vars::{
storage::Storage,
shared_mutable::{scheduled_value_change::ScheduledValueChange, scheduled_delay_change::ScheduledDelayChange}
Expand Down Expand Up @@ -225,6 +225,17 @@ impl<T, INITIAL_DELAY> SharedMutable<T, INITIAL_DELAY, &mut PrivateContext> wher
}
}

impl<T, INITIAL_DELAY> SharedMutable<T, INITIAL_DELAY, UnconstrainedContext> where T: ToField + FromField + Eq {
unconstrained pub fn get_current_value_in_unconstrained(self) -> T {
let block_number = self.context.block_number() as u32;
self.read_value_change().get_current_at(block_number)
}
nventuro marked this conversation as resolved.
Show resolved Hide resolved

fn read_value_change(self) -> ScheduledValueChange<T> {
sklppy88 marked this conversation as resolved.
Show resolved Hide resolved
self.context.storage_read(self.get_value_change_storage_slot())
}
}

unconstrained fn get_public_storage_hints<T, INITIAL_DELAY>(
address: AztecAddress,
storage_slot: Field,
Expand Down
60 changes: 60 additions & 0 deletions noir-projects/aztec-nr/aztec/src/state_vars/shared_mutable/test.nr
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,63 @@ fn test_get_current_value_in_private_bad_zero_hash_delay_hints() {

let _ = state_var.get_current_value_in_private();
}

#[test]
fn test_get_current_value_in_unconstrained_initial() {
let env = setup();
let state_var = in_unconstrained(env);

assert_eq(state_var.get_current_value_in_unconstrained(), zeroed());
}

#[test]
fn test_get_current_value_in_unconstrained_before_scheduled_change() {
let mut env = setup();
let state_var_public = in_public(env);

state_var_public.schedule_value_change(new_value);

let (_, block_of_change) = state_var_public.get_scheduled_value_in_public();

let original_value = zeroed();

let mut state_var_unconstrained = in_unconstrained(env);

// The current value has not changed
assert_eq(state_var_unconstrained.get_current_value_in_unconstrained(), original_value);

// The current value still does not change right before the block of change
env.advance_block_to(block_of_change - 1);

state_var_unconstrained = in_unconstrained(env);
assert_eq(state_var_unconstrained.get_current_value_in_unconstrained(), original_value);
}

#[test]
fn test_get_current_value_in_unconstrained_at_scheduled_change() {
let mut env = setup();
let state_var_public = in_public(env);

state_var_public.schedule_value_change(new_value);

let (_, block_of_change) = state_var_public.get_scheduled_value_in_public();

env.advance_block_to(block_of_change);

let state_var_unconstrained = in_unconstrained(env);
assert_eq(state_var_unconstrained.get_current_value_in_unconstrained(), new_value);
}

#[test]
fn test_get_current_value_in_unconstrained_after_scheduled_change() {
let mut env = setup();
let state_var_public = in_public(env);

state_var_public.schedule_value_change(new_value);

let (_, block_of_change) = state_var_public.get_scheduled_value_in_public();

env.advance_block_to(block_of_change + 10);
let state_var_unconstrained = in_unconstrained(env);
assert_eq(state_var_unconstrained.get_current_value_in_unconstrained(), new_value);
}
Loading