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

fix(avm-context): enqueueing of public from private #6299

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

use crate::hash::hash_args;
use crate::oracle::arguments;
use crate::context::private_context::PrivateContext;
use crate::context::public_context::PublicContext;
use crate::context::avm_context::AvmContext;
Expand Down Expand Up @@ -118,7 +118,6 @@ struct PublicCallInterface<T> {
}

impl<T> PublicCallInterface<T> {

pub fn call<N>(self, context: &mut PublicContext) -> T where T: Deserialize<N> {
let returns = context.call_public_function_with_packed_args(
self.target_contract,
Expand Down Expand Up @@ -232,18 +231,39 @@ impl<T> AvmCallInterface<T> {
}

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)
// This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args.
let args_hash = arguments::pack_arguments(self.args);
context.call_public_function_with_packed_args(
self.target_contract,
self.selector,
args_hash,
/*static=*/ false,
/*delegate=*/ 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)
// This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args.
let args_hash = arguments::pack_arguments(self.args);
context.call_public_function_with_packed_args(
self.target_contract,
self.selector,
args_hash,
/*static=*/ true,
/*delegate=*/ 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)
// This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args.
let args_hash = arguments::pack_arguments(self.args);
context.call_public_function_with_packed_args(
self.target_contract,
self.selector,
args_hash,
/*static=*/ false,
/*delegate=*/ true
)
}
}

Expand Down Expand Up @@ -276,17 +296,38 @@ impl AvmVoidCallInterface {
}

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)
// This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args.
let args_hash = arguments::pack_arguments(self.args);
context.call_public_function_with_packed_args(
self.target_contract,
self.selector,
args_hash,
/*static=*/ false,
/*delegate=*/ 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)
// This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args.
let args_hash = arguments::pack_arguments(self.args);
context.call_public_function_with_packed_args(
self.target_contract,
self.selector,
args_hash,
/*static=*/ true,
/*delegate=*/ 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)
// This packing is only here because PrivateContext's call_public* functions do not accept a slice for the args.
let args_hash = arguments::pack_arguments(self.args);
context.call_public_function_with_packed_args(
self.target_contract,
self.selector,
args_hash,
/*static=*/ false,
/*delegate=*/ true
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ contract AvmTest {
U128::from_integer(should_overflow)
}

#[aztec(private)]
fn enqueue_public_from_private() {
AvmTest::at(context.this_address()).set_opcode_u8().static_enqueue(&mut context);
AvmTest::at(context.this_address()).set_read_storage_single(5).enqueue(&mut context);
}

/************************************************************************
* Hashing functions
************************************************************************/
Expand Down
6 changes: 6 additions & 0 deletions yarn-project/end-to-end/src/e2e_avm_simulator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ describe('e2e_avm_simulator', () => {
});
});

describe('From private', () => {
it('Should enqueue a public function correctly', async () => {
await avmContract.methods.enqueue_public_from_private().simulate();
});
});

describe('Gas metering', () => {
it('Tracks L2 gas usage on simulation', async () => {
const request = await avmContract.methods.add_args_return(20n, 30n).create();
Expand Down
3 changes: 2 additions & 1 deletion yarn-project/simulator/src/avm/journal/journal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ export class AvmPersistableStateManager {
contractStorageUpdateRequests: [],
unencryptedLogsHashes: [],
unencryptedLogs: [],
unencryptedLogPreimagesLength: new Fr(0),
// The length starts at 4 because it will always include the size.
unencryptedLogPreimagesLength: new Fr(4),
allUnencryptedLogs: [],
nestedExecutions: [],
};
Expand Down
Loading