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(aztec-nr): add enqueue functions to AvmCallInterface #6264

Merged
merged 2 commits into from
May 8, 2024
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
63 changes: 51 additions & 12 deletions noir-projects/aztec-nr/aztec/src/context/interface.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use dep::protocol_types::{abis::function_selector::FunctionSelector, address::{AztecAddress, EthAddress}, traits::Deserialize};

use crate::hash::hash_args;
use crate::context::private_context::PrivateContext;
use crate::context::public_context::PublicContext;
use crate::context::avm_context::AvmContext;
Expand Down Expand Up @@ -206,48 +207,86 @@ struct AvmCallInterface<T> {
target_contract: AztecAddress,
selector: FunctionSelector,
args: [Field],
gas_opts: GasOpts,
}

impl<T> AvmCallInterface<T> {
pub fn call<N>(self, context: &mut AvmContext, gas_opts: GasOpts) -> T where T: Deserialize<N> {
let returns = context.call_public_function(self.target_contract, self.selector, self.args, gas_opts);
pub fn with_gas(self: &mut Self, gas_opts: GasOpts) -> &mut Self {
self.gas_opts = gas_opts;
self
}

pub fn call<N>(self, context: &mut AvmContext) -> T where T: Deserialize<N> {
let returns = context.call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
returns.deserialize_into()
}

pub fn static_call<N>(
self,
context: &mut AvmContext,
gas_opts: GasOpts
) -> T where T: Deserialize<N> {
let returns = context.static_call_public_function(self.target_contract, self.selector, self.args, gas_opts);
pub fn static_call<N>(self, context: &mut AvmContext) -> T where T: Deserialize<N> {
let returns = context.static_call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
returns.deserialize_into()
}

pub fn delegate_call<N>(self, context: &mut AvmContext) -> T where T: Deserialize<N> {
let returns = context.delegate_call_public_function(self.target_contract, self.selector, self.args);
returns.deserialize_into()
}

pub fn enqueue(self, context: &mut PrivateContext) {
let args_hash = hash_args(self.args);
context.call_public_function_with_packed_args(self.target_contract, self.selector, args_hash, false, false)
}

pub fn static_enqueue(self, context: &mut PrivateContext) {
let args_hash = hash_args(self.args);
context.call_public_function_with_packed_args(self.target_contract, self.selector, args_hash, true, false)
Copy link
Contributor

Choose a reason for hiding this comment

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

please add /*name=*/ to the "true", "false" params.

(same in other occurrences)

}

pub fn delegate_enqueue(self, context: &mut PrivateContext) {
let args_hash = hash_args(self.args);
context.call_public_function_with_packed_args(self.target_contract, self.selector, args_hash, false, true)
}
}

struct AvmVoidCallInterface {
target_contract: AztecAddress,
selector: FunctionSelector,
args: [Field],
gas_opts: GasOpts,
}

impl AvmVoidCallInterface {
pub fn call<N>(self, context: &mut AvmContext, gas_opts: GasOpts) {
let returns = context.call_public_function(self.target_contract, self.selector, self.args, gas_opts);
pub fn with_gas(self: &mut Self, gas_opts: GasOpts) -> &mut Self {
self.gas_opts = gas_opts;
self
}

pub fn call<N>(self, context: &mut AvmContext) {
let returns = context.call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
returns.assert_empty()
}

pub fn static_call<N>(self, context: &mut AvmContext, gas_opts: GasOpts) {
let returns = context.static_call_public_function(self.target_contract, self.selector, self.args, gas_opts);
pub fn static_call<N>(self, context: &mut AvmContext) {
let returns = context.static_call_public_function(self.target_contract, self.selector, self.args, self.gas_opts);
returns.assert_empty()
}

pub fn delegate_call<N>(self, context: &mut AvmContext) {
let returns = context.delegate_call_public_function(self.target_contract, self.selector, self.args);
returns.assert_empty()
}

pub fn enqueue(self, context: &mut PrivateContext) {
let args_hash = hash_args(self.args);
context.call_public_function_with_packed_args(self.target_contract, self.selector, args_hash, false, false)
}

pub fn static_enqueue(self, context: &mut PrivateContext) {
let args_hash = hash_args(self.args);
context.call_public_function_with_packed_args(self.target_contract, self.selector, args_hash, true, false)
}

pub fn delegate_enqueue(self, context: &mut PrivateContext) {
let args_hash = hash_args(self.args);
context.call_public_function_with_packed_args(self.target_contract, self.selector, args_hash, false, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ contract AvmAcvmInteropTest {
}

#[aztec(public)]
fn new_nullifier_acvm(nullifier: Field) -> pub Field {
fn new_nullifier_acvm(nullifier: Field) {
context.push_new_nullifier(nullifier, 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,36 +40,36 @@ contract AvmNestedCallsTest {
l2_gas: Field,
da_gas: Field
) -> pub Field {
AvmNestedCallsTest::at(context.this_address()).add_args_return(arg_a, arg_b).call(&mut context, GasOpts::new(l2_gas, da_gas))
AvmNestedCallsTest::at(context.this_address()).add_args_return(arg_a, arg_b).with_gas(GasOpts::new(l2_gas, da_gas)).call(&mut context)
}

// Use the `call_public_function` wrapper to initiate a nested call to the add function
#[aztec(public-vm)]
fn nested_call_to_add(arg_a: Field, arg_b: Field) -> pub Field {
AvmNestedCallsTest::at(context.this_address()).add_args_return(arg_a, arg_b).call(&mut context, GasOpts::default())
AvmNestedCallsTest::at(context.this_address()).add_args_return(arg_a, arg_b).call(&mut context)
}

// Indirectly call_static the external call opcode to initiate a nested call to the add function
#[aztec(public-vm)]
fn nested_static_call_to_add(arg_a: Field, arg_b: Field) -> pub Field {
AvmNestedCallsTest::at(context.this_address()).add_args_return(arg_a, arg_b).static_call(&mut context, GasOpts::default())
AvmNestedCallsTest::at(context.this_address()).add_args_return(arg_a, arg_b).static_call(&mut context)
}

// Indirectly call_static `set_storage_single`. Should revert since it's accessing storage.
#[aztec(public-vm)]
fn nested_static_call_to_set_storage() {
AvmNestedCallsTest::at(context.this_address()).set_storage_single(20).static_call(&mut context, GasOpts::default());
AvmNestedCallsTest::at(context.this_address()).set_storage_single(20).static_call(&mut context);
}

#[aztec(public-vm)]
fn create_same_nullifier_in_nested_call(nestedAddress: AztecAddress, nullifier: Field) {
context.push_new_nullifier(nullifier, 0);
AvmNestedCallsTest::at(nestedAddress).new_nullifier(nullifier).call(&mut context, GasOpts::default());
AvmNestedCallsTest::at(nestedAddress).new_nullifier(nullifier).call(&mut context);
}

#[aztec(public-vm)]
fn create_different_nullifier_in_nested_call(nestedAddress: AztecAddress, nullifier: Field) {
context.push_new_nullifier(nullifier, 0);
AvmNestedCallsTest::at(nestedAddress).new_nullifier(nullifier + 1).call(&mut context, GasOpts::default());
AvmNestedCallsTest::at(nestedAddress).new_nullifier(nullifier + 1).call(&mut context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ pub fn stub_function(aztec_visibility: &str, func: &NoirFunction) -> String {
target_contract: self.target_contract,
selector: {},
args: args_acc,
gas_opts: dep::aztec::context::gas::GasOpts::default(),
}}",
args, is_void, fn_selector,
);
Expand Down
Loading