From 9da91cf52e33d2917352bc7204e0db68b1e7c9d4 Mon Sep 17 00:00:00 2001 From: fcarreiro Date: Thu, 9 May 2024 12:27:15 +0000 Subject: [PATCH] fix(avm-simulator): enqueueing of public from private --- .../aztec-nr/aztec/src/context/interface.nr | 69 +++++++++++++++---- .../contracts/avm_test_contract/src/main.nr | 6 ++ .../end-to-end/src/e2e_avm_simulator.test.ts | 6 ++ .../simulator/src/avm/journal/journal.ts | 3 +- 4 files changed, 69 insertions(+), 15 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/context/interface.nr b/noir-projects/aztec-nr/aztec/src/context/interface.nr index 7f72656252b..0ceb66a05a8 100644 --- a/noir-projects/aztec-nr/aztec/src/context/interface.nr +++ b/noir-projects/aztec-nr/aztec/src/context/interface.nr @@ -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; @@ -118,7 +118,6 @@ struct PublicCallInterface { } impl PublicCallInterface { - pub fn call(self, context: &mut PublicContext) -> T where T: Deserialize { let returns = context.call_public_function_with_packed_args( self.target_contract, @@ -232,18 +231,39 @@ impl AvmCallInterface { } 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 + ) } } @@ -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 + ) } } diff --git a/noir-projects/noir-contracts/contracts/avm_test_contract/src/main.nr b/noir-projects/noir-contracts/contracts/avm_test_contract/src/main.nr index 94d70614a13..e71861ffbef 100644 --- a/noir-projects/noir-contracts/contracts/avm_test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/avm_test_contract/src/main.nr @@ -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 ************************************************************************/ diff --git a/yarn-project/end-to-end/src/e2e_avm_simulator.test.ts b/yarn-project/end-to-end/src/e2e_avm_simulator.test.ts index 56cca9370f4..4869cc90162 100644 --- a/yarn-project/end-to-end/src/e2e_avm_simulator.test.ts +++ b/yarn-project/end-to-end/src/e2e_avm_simulator.test.ts @@ -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(); diff --git a/yarn-project/simulator/src/avm/journal/journal.ts b/yarn-project/simulator/src/avm/journal/journal.ts index 7bea5f1c42a..c43418d1e6d 100644 --- a/yarn-project/simulator/src/avm/journal/journal.ts +++ b/yarn-project/simulator/src/avm/journal/journal.ts @@ -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: [], };