Skip to content
Closed
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
2 changes: 2 additions & 0 deletions noir-projects/aztec-nr/aztec/src/context/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ pub use private_context::PrivateContext;
pub use public_context::PublicContext;
pub use returns_hash::ReturnsHash;
pub use utility_context::UtilityContext;

pub mod self_contract;
104 changes: 104 additions & 0 deletions noir-projects/aztec-nr/aztec/src/context/self_contract.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use crate::{
context::{
call_interfaces::{PrivateCallInterface, PublicCallInterface, PublicStaticCallInterface},
private_context::PrivateContext,
public_context::PublicContext,
utility_context::UtilityContext,
},
event::{
event_emission::{emit_event_in_private, emit_event_in_public},
event_interface::EventInterface,
},
};
use protocol_types::{address::AztecAddress, traits::{Deserialize, Serialize}};

pub struct SelfContract<Context> {
context: Context,
}

impl<Context> SelfContract<Context> {
pub fn new(context: Context) -> Self {
SelfContract { context }
}
}

impl SelfContract<&mut PrivateContext> {
pub fn msg_sender(self) -> AztecAddress {
self.context.msg_sender()
}

pub fn address(self) -> AztecAddress {
self.context.this_address()
}

pub fn call<T, let M: u32>(self, call_interface: PrivateCallInterface<M, T>) -> T
where
T: Deserialize,
{
call_interface.call(self.context)
}

pub fn enqueue<T, let M: u32>(self, call_interface: PublicCallInterface<M, T>)
where
T: Deserialize,
{
call_interface.enqueue(self.context)
}

pub fn emit_event<Event>(self, event: Event, recipient: AztecAddress, delivery_mode: u8)
where
Event: EventInterface + Serialize,
{
emit_event_in_private(event, self.context, recipient, delivery_mode);
}
// aztec-nr does not know about the router contract (because the router contract depends on aztec-nr)
// pub fn check_timestamp(self, operation: u8, value: u64) {
// privately_check_timestamp(operation, value, self.context);
// }
// pub fn check_block_number(self, operation: u8, value: u32) {
// privately_check_block_number(operation, value, self.context);
// }
}

impl SelfContract<PublicContext> {
pub fn msg_sender(self) -> AztecAddress {
self.context.msg_sender()
}

pub fn address(self) -> AztecAddress {
self.context.this_address()
}

pub unconstrained fn call<T, let M: u32>(self, call_interface: PublicCallInterface<M, T>) -> T
where
T: Deserialize,
{
let mut ctx = self.context;
call_interface.call(&mut ctx)
}

pub unconstrained fn view<T, let M: u32>(
self,
call_interface: PublicStaticCallInterface<M, T>,
) -> T
where
T: Deserialize,
{
let mut ctx = self.context;
call_interface.view(&mut ctx)
}

pub fn emit_event<Event>(self, event: Event)
where
Event: EventInterface + Serialize,
{
let mut ctx = self.context;
emit_event_in_public(event, &mut ctx);
}
}

impl SelfContract<UtilityContext> {
pub fn address(self) -> AztecAddress {
self.context.this_address()
}
}
27 changes: 20 additions & 7 deletions noir-projects/aztec-nr/aztec/src/macros/functions/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ pub(crate) comptime fn transform_private(f: FunctionDefinition) {
},
);

let context_creation = quote {
let context_and_self_creation = quote {
let mut context = dep::aztec::context::private_context::PrivateContext::new(inputs, dep::aztec::protocol_types::traits::Hash::hash($args_hasher_name));
// Some functions don't use `self`, but it'd be quite difficult to only inject this variable if it is
// referenced. We instead ignore 'unused variable' warnings for it.
#[allow(unused_variables)]
let self = dep::aztec::context::self_contract::SelfContract::new(&mut context);
};

// Modifications introduced by the different marker attributes.
Expand Down Expand Up @@ -155,7 +159,7 @@ pub(crate) comptime fn transform_private(f: FunctionDefinition) {
let to_prepend = quote {
dep::aztec::oracle::version::assert_compatible_oracle_version();
$args_hasher
$context_creation
$context_and_self_creation
$assert_initializer
$init_check
$internal_check
Expand Down Expand Up @@ -204,12 +208,16 @@ pub(crate) comptime fn transform_public(f: FunctionDefinition) {
};

// Unlike in the private case, in public the `context` does not need to receive the hash of the original params.
let context_creation = quote {
let context_and_self_creation = quote {
let mut context = dep::aztec::context::public_context::PublicContext::new(|| {
// We start from 1 because we skip the selector for the dispatch function.
let serialized_args : [Field; $args_len_quote] = dep::aztec::context::public_context::calldata_copy(1, $args_len_quote);
dep::aztec::hash::hash_args_array(serialized_args)
});
// Some functions don't use `self`, but it'd be quite difficult to only inject this variable if it is
// referenced. We instead ignore 'unused variable' warnings for it.
#[allow(unused_variables)]
let self = dep::aztec::context::self_contract::SelfContract::new(context);
};

// Modifications introduced by the different marker attributes.
Expand Down Expand Up @@ -257,7 +265,7 @@ pub(crate) comptime fn transform_public(f: FunctionDefinition) {
};

let to_prepend = quote {
$context_creation
$context_and_self_creation
$assert_initializer
$init_check
$internal_check
Expand Down Expand Up @@ -286,8 +294,13 @@ pub(crate) comptime fn transform_utility(f: FunctionDefinition) {
stub_registry::register(f.module(), fn_stub);

// Create utility context
let context_creation =
quote { let mut context = dep::aztec::context::utility_context::UtilityContext::new(); };
let context_and_self_creation = quote {
let mut context = dep::aztec::context::utility_context::UtilityContext::new();
// Some functions don't use `self`, but it'd be quite difficult to only inject this variable if it is
// referenced. We instead ignore 'unused variable' warnings for it.
#[allow(unused_variables)]
let self = dep::aztec::context::self_contract::SelfContract::new(context);
};

// Initialize Storage if module has storage
let storage_init = if module_has_storage(f.module()) {
Expand All @@ -309,7 +322,7 @@ pub(crate) comptime fn transform_utility(f: FunctionDefinition) {
// A quote to be injected at the beginning of the function body.
let to_prepend = quote {
dep::aztec::oracle::version::assert_compatible_oracle_version();
$context_creation
$context_and_self_creation
$storage_init
$message_discovery_call
};
Expand Down
Loading
Loading