Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use protocol_types::{
traits::Packable,
};

mod test;

/// Stores an immutable value in public state which can be read from public, private and unconstrained execution
/// contexts.
///
Expand Down Expand Up @@ -126,6 +128,7 @@ impl<T> PublicImmutable<T, UtilityContext> {
where
T: Packable<T_PACKED_LEN> + Eq,
{
// TODO(#15703): this fn should fail if the variable is not initialized
WithHash::utility_public_storage_read(self.context, self.storage_slot)
}
}
Expand All @@ -135,6 +138,7 @@ impl<T> PublicImmutable<T, &mut PrivateContext> {
where
T: Packable<T_PACKED_LEN> + Eq,
{
// TODO(#15703): this fn should fail if the variable is not initialized
WithHash::historical_public_storage_read(
self.context.get_block_header(),
self.context.this_address(),
Expand Down
191 changes: 162 additions & 29 deletions noir-projects/aztec-nr/aztec/src/state_vars/public_immutable/test.nr
Original file line number Diff line number Diff line change
@@ -1,53 +1,186 @@
use crate::{context::PublicContext, state_vars::public_immutable::PublicImmutable};
use crate::{
context::{PrivateContext, PublicContext, UtilityContext},
state_vars::public_immutable::PublicImmutable,
};
use crate::test::{helpers::test_environment::TestEnvironment, mocks::mock_struct::MockStruct};
use dep::protocol_types::traits::Serialize;
use std::mem::zeroed;

global storage_slot = 7;
global storage_slot: Field = 7;

fn setup() -> TestEnvironment {
TestEnvironment::new()
fn in_public(context: &mut PublicContext) -> PublicImmutable<MockStruct, &mut PublicContext> {
PublicImmutable::new(context, storage_slot)
}

fn in_public(env: TestEnvironment) -> PublicImmutable<MockStruct, &mut PublicContext> {
PublicImmutable::new(&mut env.public(), storage_slot)
fn in_private(context: &mut PrivateContext) -> PublicImmutable<MockStruct, &mut PrivateContext> {
PublicImmutable::new(context, storage_slot)
}

fn in_utility(context: UtilityContext) -> PublicImmutable<MockStruct, UtilityContext> {
PublicImmutable::new(context, storage_slot)
}

#[test]
fn test_uninitialized_by_default() {
let env = setup();
let state_var = in_public(env);
unconstrained fn is_uninitialized_by_default() {
let mut env = TestEnvironment::_new();

assert_eq(state_var.is_initialized(), false);
env.public_context(|context| {
let state_var = in_public(context);
assert_eq(state_var.is_initialized(), false);
});
}

#[test]
fn test_initialize_uninitialized() {
let env = setup();
let state_var = in_public(env);
unconstrained fn initialize_uninitialized_same_tx() {
let mut env = TestEnvironment::_new();

let value = MockStruct::new(5, 6);
env.public_context(|context| {
let state_var = in_public(context);

let value = MockStruct::new(5, 6);
state_var.initialize(value);

assert(state_var.is_initialized());
});
}

#[test]
unconstrained fn initialize_uninitialized_other_tx() {
let mut env = TestEnvironment::_new();

env.public_context(|context| {
let state_var = in_public(context);

let value = MockStruct::new(5, 6);
state_var.initialize(value);
});

env.public_context(|context| {
let state_var = in_public(context);
assert(state_var.is_initialized());
});
}

#[test(should_fail)]
unconstrained fn initialize_already_initialized_same_tx() {
let mut env = TestEnvironment::_new();

env.public_context(|context| {
let state_var = in_public(context);

let value = MockStruct::new(5, 6);
state_var.initialize(value);

let other_value = MockStruct::new(7, 8);
state_var.initialize(other_value);
});
}

#[test(should_fail)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error message?

unconstrained fn initialize_already_initialized_other_tx() {
let mut env = TestEnvironment::_new();

env.public_context(|context| {
let state_var = in_public(context);
let value = MockStruct::new(5, 6);
state_var.initialize(value);
});

env.public_context(|context| {
let state_var = in_public(context);
let other_value = MockStruct::new(7, 8);
state_var.initialize(other_value);
});
}

#[test(should_fail_with = "Trying to read from uninitialized PublicImmutable")]
unconstrained fn read_uninitialized_public_fails() {
let mut env = TestEnvironment::_new();

state_var.initialize(value);
env.public_context(|context| {
let state_var = in_public(context);
let _ = state_var.read();
});
}

// TODO(#15703): this test should be made to fail
#[test]
unconstrained fn read_uninitialized_private_returns_default() {
let mut env = TestEnvironment::_new();

env.private_context(|context| {
let state_var = in_private(context);
assert_eq(state_var.read(), zeroed());
});
}

// TODO(#15703): this test should be made to fail
#[test]
unconstrained fn read_uninitialized_utility_returns_default() {
let mut env = TestEnvironment::_new();

env.utility_context(|context| {
let state_var = in_utility(context);
assert_eq(state_var.read(), zeroed());
});
}

#[test]
unconstrained fn read_uninitialized_public_same_tx() {
Copy link
Contributor

@Thunkar Thunkar Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

read_initialized?

let mut env = TestEnvironment::_new();

assert(state_var.is_initialized());
assert(state_var.read() == value);
env.public_context(|context| {
let state_var = in_public(context);
let value = MockStruct::new(5, 6);
state_var.initialize(value);

assert_eq(state_var.read(), value);
});
}

#[test(should_fail_with = "PublicImmutable already initialized")]
fn test_initialize_already_initialized() {
let env = setup();
let state_var = in_public(env);
#[test]
unconstrained fn read_uninitialized_public_other_tx() {
let mut env = TestEnvironment::_new();
let value = MockStruct::new(5, 6);

env.public_context(|context| {
let state_var = in_public(context);
state_var.initialize(value);
});

env.public_context(|context| {
let state_var = in_public(context);
assert_eq(state_var.read(), value);
});
}

#[test]
unconstrained fn read_uninitialized_private() {
let mut env = TestEnvironment::_new();
let value = MockStruct::new(5, 6);

state_var.initialize(value);
state_var.initialize(value);
env.public_context(|context| {
let state_var = in_public(context);
state_var.initialize(value);
});

env.private_context(|context| {
let state_var = in_private(context);
assert_eq(state_var.read(), value);
});
}

#[test(should_fail_with = "PublicImmutable not initialized")]
fn test_read_uninitialized() {
let env = setup();
let state_var = in_public(env);
#[test]
unconstrained fn read_uninitialized_utility() {
let mut env = TestEnvironment::_new();
let value = MockStruct::new(5, 6);

env.public_context(|context| {
let state_var = in_public(context);
state_var.initialize(value);
});

let _ = state_var.read();
env.utility_context(|context| {
let state_var = in_utility(context);
assert_eq(state_var.read(), value);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use crate::context::{PublicContext, UtilityContext};
use crate::state_vars::storage::HasStorageSlot;
use dep::protocol_types::traits::Packable;

mod test;

// docs:start:public_mutable_struct
pub struct PublicMutable<T, Context> {
context: Context,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use crate::{context::{PublicContext, UtilityContext}, state_vars::public_mutable::PublicMutable};
use crate::test::{helpers::test_environment::TestEnvironment, mocks::mock_struct::MockStruct};
use std::mem::zeroed;

global storage_slot: Field = 7;

fn in_public(context: &mut PublicContext) -> PublicMutable<MockStruct, &mut PublicContext> {
PublicMutable::new(context, storage_slot)
}

fn in_utility(context: UtilityContext) -> PublicMutable<MockStruct, UtilityContext> {
PublicMutable::new(context, storage_slot)
}

#[test]
unconstrained fn read_uninitialized_public_returns_zero() {
let mut env = TestEnvironment::_new();

env.public_context(|context| {
let state_var = in_public(context);
assert_eq(state_var.read(), zeroed());
});
}

#[test]
unconstrained fn read_uninitialized_utility_returns_default() {
let mut env = TestEnvironment::_new();

env.utility_context(|context| {
let state_var = in_utility(context);
assert_eq(state_var.read(), zeroed());
});
}

#[test]
unconstrained fn write_read_public_same_tx() {
let mut env = TestEnvironment::_new();

env.public_context(|context| {
let state_var = in_public(context);
let value = MockStruct::new(5, 6);

state_var.write(value);
assert_eq(state_var.read(), value);
});
}

#[test]
unconstrained fn write_read_public_other_tx() {
let mut env = TestEnvironment::_new();

let value = MockStruct::new(5, 6);

env.public_context(|context| {
let state_var = in_public(context);
state_var.write(value);
});

env.public_context(|context| {
let state_var = in_public(context);
assert_eq(state_var.read(), value);
});
}

#[test]
unconstrained fn write_read_utility() {
let mut env = TestEnvironment::_new();
let value = MockStruct::new(5, 6);

env.public_context(|context| {
let state_var = in_public(context);
state_var.write(value);
});

env.utility_context(|context| {
let state_var = in_utility(context);
assert_eq(state_var.read(), value);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ impl TestEnvironment {
let ret_value = f(&mut context);
cheatcodes::set_top_level_txe_context();

// todo: should commit the context to mine a block with the side effects of the context
// todo: should commit the context to mine a block with the side effects of the context. we should have an
// oracle that receives the context we produced probably
self.mine_block();

ret_value
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::test::{helpers::test_environment::TestEnvironment, mocks::mock_struct::MockStruct};
use crate::test::helpers::test_environment::TestEnvironment;

use dep::std::mem::zeroed;

global storage_slot: Field = 13;
global value: MockStruct = MockStruct { a: 17, b: 42 };
mod public_context;

#[test(should_fail_with = "cannot be before next timestamp")]
unconstrained fn set_next_block_timestamp_to_past_fails() {
Expand Down Expand Up @@ -98,34 +95,6 @@ unconstrained fn public_context_advances_timestamp() {
assert(second_timestamp > first_timestamp);
}

#[test]
unconstrained fn public_context_default_storage_value() {
let mut env = TestEnvironment::_new();

env.public_context(|context| {
assert_eq(context.storage_read::<MockStruct, _>(storage_slot), zeroed());
});
}

#[test]
unconstrained fn public_context_storage_write_in_same_context() {
let mut env = TestEnvironment::_new();

env.public_context(|context| {
context.storage_write(storage_slot, value);
assert_eq(context.storage_read(storage_slot), value);
});
}

#[test]
unconstrained fn public_context_storage_write_in_future_context() {
let mut env = TestEnvironment::_new();

env.public_context(|context| { context.storage_write(storage_slot, value); });

env.public_context(|context| { assert_eq(context.storage_read(storage_slot), value); });
}

#[test]
unconstrained fn private_context_uses_previous_block_number() {
let mut env = TestEnvironment::_new();
Expand Down
Loading
Loading