Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Jul 15, 2024
1 parent 7a31314 commit 77b0685
Show file tree
Hide file tree
Showing 2 changed files with 200 additions and 1 deletion.
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,34 @@ 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)
}

unconstrained pub fn get_current_delay_in_unconstrained(self) -> u32 {
let block_number = self.context.block_number() as u32;
self.read_delay_change().get_current(block_number)
}

unconstrained pub fn get_scheduled_value_in_unconstrained(self) -> (T, u32) {
self.read_value_change().get_scheduled()
}

unconstrained pub fn get_scheduled_delay_in_unconstrained(self) -> (u32, u32) {
self.read_delay_change().get_scheduled()
}

fn read_value_change(self) -> ScheduledValueChange<T> {
self.context.storage_read(self.get_value_change_storage_slot())
}

fn read_delay_change(self) -> ScheduledDelayChange<INITIAL_DELAY> {
self.context.storage_read(self.get_delay_change_storage_slot())
}
}

unconstrained fn get_public_storage_hints<T, INITIAL_DELAY>(
address: AztecAddress,
storage_slot: Field,
Expand Down
171 changes: 171 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,174 @@ 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_scheduled_value_in_unconstrained() {
let mut env = setup();
let state_var_public = in_public(env);

state_var_public.schedule_value_change(new_value);

let state_var_unconstrained = in_unconstrained(env);

let (scheduled, block_of_change) = state_var_unconstrained.get_scheduled_value_in_unconstrained();
assert_eq(scheduled, new_value);
assert_eq(block_of_change, env.block_number() + TEST_INITIAL_DELAY);
}

#[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 state_var_unconstrained = in_unconstrained(env);

let (_, block_of_change) = state_var_unconstrained.get_scheduled_value_in_unconstrained();

let original_value = zeroed();

// 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);
assert_eq(state_var_unconstrained.get_current_value_in_unconstrained(), original_value);
}

// #[test]
// fn test_get_current_delay_in_public_at_scheduled_change() {
// let mut env = setup();
// let state_var = in_public(env);

// state_var.schedule_delay_change(new_delay);

// let (_, block_of_change) = state_var.get_scheduled_delay_in_public();

// env.advance_block_to(block_of_change);
// assert_eq(state_var.get_current_delay_in_public(), new_delay);
// }

// #[test]
// fn test_get_current_delay_in_public_after_scheduled_change() {
// let mut env = setup();
// let state_var = in_public(env);

// state_var.schedule_delay_change(new_delay);

// let (_, block_of_change) = state_var.get_scheduled_delay_in_public();

// env.advance_block_to(block_of_change + 10);
// assert_eq(state_var.get_current_delay_in_public(), new_delay);
// }

#[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);
}

#[test]
fn test_get_current_delay_in_unconstrained_initial() {
let env = setup();
let state_var_unconstrained = in_unconstrained(env);

assert_eq(state_var_unconstrained.get_current_delay_in_unconstrained(), TEST_INITIAL_DELAY);
}

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

state_var_public.schedule_delay_change(new_delay);

let state_var_unconstrained = in_unconstrained(env);

let (scheduled, block_of_change) = state_var_unconstrained.get_scheduled_delay_in_unconstrained();
assert_eq(scheduled, new_delay);
// The new delay is smaller, therefore we need to wait for the difference between current and new
assert_eq(block_of_change, env.block_number() + TEST_INITIAL_DELAY - new_delay);
}

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

state_var_public.schedule_delay_change(new_delay);

let state_var_unconstrained = in_unconstrained(env);

let (_, block_of_change) = state_var_unconstrained.get_scheduled_delay_in_unconstrained();

let original_delay = TEST_INITIAL_DELAY;

// The current delay has not changed
assert_eq(state_var_unconstrained.get_current_delay_in_unconstrained(), original_delay);

// The current delay still does not change right before the block of change
env.advance_block_to(block_of_change - 1);
assert_eq(state_var_unconstrained.get_current_delay_in_unconstrained(), original_delay);
}

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

state_var_public.schedule_delay_change(new_delay);

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

env.advance_block_to(block_of_change);

let state_var_unconstrained = in_unconstrained(env);
assert_eq(state_var_unconstrained.get_current_delay_in_unconstrained(), new_delay);
}

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

state_var_public.schedule_delay_change(new_delay);

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

env.advance_block_to(block_of_change + 10);

let state_var_unconstrained = in_unconstrained(env);
assert_eq(state_var_unconstrained.get_current_delay_in_unconstrained(), new_delay);
}

0 comments on commit 77b0685

Please sign in to comment.