From bdced68ff0af48d6d40067915477a8654a9f6348 Mon Sep 17 00:00:00 2001 From: Alex Gherghisan Date: Mon, 11 Mar 2024 11:42:23 +0000 Subject: [PATCH] feat: public function call counter on context --- .../aztec/src/context/private_context.nr | 2 +- .../aztec/src/context/public_context.nr | 79 ++++++++++++++++--- .../aztec-nr/aztec/src/oracle/public_call.nr | 3 + .../contracts/fpc_contract/src/interfaces.nr | 4 +- .../contracts/fpc_contract/src/main.nr | 6 +- .../import_test_contract/src/main.nr | 2 +- .../lending_contract/src/interfaces.nr | 16 ++-- .../contracts/lending_contract/src/main.nr | 28 +++---- .../contracts/test_contract/src/interface.nr | 14 ++-- .../src/interfaces.nr | 8 +- .../token_blacklist_contract/src/main.nr | 24 +++--- .../token_bridge_contract/src/main.nr | 6 +- .../src/token_interface.nr | 6 +- .../uniswap_contract/src/interfaces.nr | 12 +-- .../contracts/uniswap_contract/src/main.nr | 14 ++-- .../simulator/src/acvm/oracle/oracle.ts | 10 ++- .../simulator/src/acvm/oracle/typed_oracle.ts | 5 +- .../src/public/public_execution_context.ts | 3 +- 18 files changed, 154 insertions(+), 88 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/context/private_context.nr b/noir-projects/aztec-nr/aztec/src/context/private_context.nr index 214c1d261f7c..aec670f433f3 100644 --- a/noir-projects/aztec-nr/aztec/src/context/private_context.nr +++ b/noir-projects/aztec-nr/aztec/src/context/private_context.nr @@ -94,7 +94,7 @@ impl ContextInterface for PrivateContext { // Returns the header of a block whose state is used during private execution (not the block the transaction is // included in). - pub fn get_header(self) -> Header { + fn get_header(self) -> Header { self.historical_header } diff --git a/noir-projects/aztec-nr/aztec/src/context/public_context.nr b/noir-projects/aztec-nr/aztec/src/context/public_context.nr index 8add2be5c980..6079b8343d88 100644 --- a/noir-projects/aztec-nr/aztec/src/context/public_context.nr +++ b/noir-projects/aztec-nr/aztec/src/context/public_context.nr @@ -204,59 +204,114 @@ impl PublicContext { } pub fn call_public_function( - _self: Self, + self: &mut Self, contract_address: AztecAddress, function_selector: FunctionSelector, args: [Field; ARGS_COUNT] ) -> [Field; RETURN_VALUES_LENGTH] { let args_hash = hash_args(args); assert(args_hash == arguments::pack_arguments(args)); - call_public_function_internal(contract_address, function_selector, args_hash, false, false) + let side_effect_counter = self.side_effect_counter; + self.side_effect_counter += 1; + + call_public_function_internal( + contract_address, + function_selector, + args_hash, + side_effect_counter, + false, + false + ) } pub fn static_call_public_function( - _self: Self, + self: &mut Self, contract_address: AztecAddress, function_selector: FunctionSelector, args: [Field; ARGS_COUNT] ) -> [Field; RETURN_VALUES_LENGTH] { let args_hash = hash_args(args); assert(args_hash == arguments::pack_arguments(args)); - call_public_function_internal(contract_address, function_selector, args_hash, true, false) + let side_effect_counter = self.side_effect_counter; + self.side_effect_counter += 1; + call_public_function_internal( + contract_address, + function_selector, + args_hash, + side_effect_counter, + true, + false + ) } pub fn delegate_call_public_function( - _self: Self, + self: &mut Self, contract_address: AztecAddress, function_selector: FunctionSelector, args: [Field; ARGS_COUNT] ) -> [Field; RETURN_VALUES_LENGTH] { let args_hash = hash_args(args); assert(args_hash == arguments::pack_arguments(args)); - call_public_function_internal(contract_address, function_selector, args_hash, false, true) + let side_effect_counter = self.side_effect_counter; + self.side_effect_counter += 1; + call_public_function_internal( + contract_address, + function_selector, + args_hash, + side_effect_counter, + false, + true + ) } pub fn call_public_function_no_args( - _self: Self, + self: &mut Self, contract_address: AztecAddress, function_selector: FunctionSelector ) -> [Field; RETURN_VALUES_LENGTH] { - call_public_function_internal(contract_address, function_selector, 0, false, false) + let side_effect_counter = self.side_effect_counter; + self.side_effect_counter += 1; + call_public_function_internal( + contract_address, + function_selector, + 0, + side_effect_counter, + false, + false + ) } pub fn static_call_public_function_no_args( - _self: Self, + self: &mut Self, contract_address: AztecAddress, function_selector: FunctionSelector ) -> [Field; RETURN_VALUES_LENGTH] { - call_public_function_internal(contract_address, function_selector, 0, true, false) + let side_effect_counter = self.side_effect_counter; + self.side_effect_counter += 1; + call_public_function_internal( + contract_address, + function_selector, + 0, + side_effect_counter, + true, + false + ) } pub fn delegate_call_public_function_no_args( - _self: Self, + self: &mut Self, contract_address: AztecAddress, function_selector: FunctionSelector ) -> [Field; RETURN_VALUES_LENGTH] { - call_public_function_internal(contract_address, function_selector, 0, false, true) + let side_effect_counter = self.side_effect_counter; + self.side_effect_counter += 1; + call_public_function_internal( + contract_address, + function_selector, + 0, + side_effect_counter, + false, + true + ) } } diff --git a/noir-projects/aztec-nr/aztec/src/oracle/public_call.nr b/noir-projects/aztec-nr/aztec/src/oracle/public_call.nr index 9675401570cc..c12ab74c510e 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/public_call.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/public_call.nr @@ -5,6 +5,7 @@ fn call_public_function_oracle( _contract_address: AztecAddress, _function_selector: FunctionSelector, _args_hash: Field, + _side_effect_counter: u32, _is_static_call: bool, _is_delegate_call: bool ) -> [Field; RETURN_VALUES_LENGTH] {} @@ -13,6 +14,7 @@ unconstrained pub fn call_public_function_internal( contract_address: AztecAddress, function_selector: FunctionSelector, args_hash: Field, + side_effect_counter: u32, is_static_call: bool, is_delegate_call: bool ) -> [Field; RETURN_VALUES_LENGTH] { @@ -20,6 +22,7 @@ unconstrained pub fn call_public_function_internal( contract_address, function_selector, args_hash, + side_effect_counter, is_static_call, is_delegate_call ) diff --git a/noir-projects/noir-contracts/contracts/fpc_contract/src/interfaces.nr b/noir-projects/noir-contracts/contracts/fpc_contract/src/interfaces.nr index d6fe72126074..dd18df1295f4 100644 --- a/noir-projects/noir-contracts/contracts/fpc_contract/src/interfaces.nr +++ b/noir-projects/noir-contracts/contracts/fpc_contract/src/interfaces.nr @@ -14,7 +14,7 @@ impl Token { pub fn transfer_public( self: Self, - context: PublicContext, + context: &mut PublicContext, from: AztecAddress, to: AztecAddress, amount: Field, @@ -29,7 +29,7 @@ impl Token { pub fn shield( self: Self, - context: PublicContext, + context: &mut PublicContext, from: AztecAddress, amount: Field, secret_hash: Field, diff --git a/noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr b/noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr index 2355896c2d13..72b347198888 100644 --- a/noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/fpc_contract/src/main.nr @@ -63,7 +63,7 @@ contract FPC { #[aztec(public)] #[aztec(internal)] fn prepare_fee(from: AztecAddress, amount: Field, asset: AztecAddress, nonce: Field) { - let _res = Token::at(asset).transfer_public(context, from, context.this_address(), amount, nonce); + let _res = Token::at(asset).transfer_public(&mut context, from, context.this_address(), amount, nonce); } #[aztec(public)] @@ -76,7 +76,7 @@ contract FPC { )[0]; // Just do public refunds for the present - Token::at(asset).transfer_public(context, context.this_address(), refund_address, refund, 0) + Token::at(asset).transfer_public(&mut context, context.this_address(), refund_address, refund, 0) } #[aztec(public)] @@ -88,6 +88,6 @@ contract FPC { [amount] )[0]; - Token::at(asset).shield(context, context.this_address(), refund, 1, 0) + Token::at(asset).shield(&mut context, context.this_address(), refund, 1, 0) } } diff --git a/noir-projects/noir-contracts/contracts/import_test_contract/src/main.nr b/noir-projects/noir-contracts/contracts/import_test_contract/src/main.nr index 0c06ea7e276a..375bc2edaa55 100644 --- a/noir-projects/noir-contracts/contracts/import_test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/import_test_contract/src/main.nr @@ -71,7 +71,7 @@ contract ImportTest { #[aztec(public)] fn pubCallOpenFn(target: AztecAddress) -> Field { let test_contract_instance = TestPublicContextInterface::at(target); - let ret = test_contract_instance.create_nullifier_public(context, 1, 2); + let ret = test_contract_instance.create_nullifier_public(&mut context, 1, 2); ret[0] } diff --git a/noir-projects/noir-contracts/contracts/lending_contract/src/interfaces.nr b/noir-projects/noir-contracts/contracts/lending_contract/src/interfaces.nr index 7593f8353d22..f86f484562f3 100644 --- a/noir-projects/noir-contracts/contracts/lending_contract/src/interfaces.nr +++ b/noir-projects/noir-contracts/contracts/lending_contract/src/interfaces.nr @@ -14,7 +14,7 @@ impl PriceFeed { Self { address } } - pub fn get_price(self: Self, context: PublicContext) -> U128 { + pub fn get_price(self: Self, context: &mut PublicContext) -> U128 { let return_values = context.call_public_function( self.address, FunctionSelector::from_signature("get_price(Field)"), @@ -36,21 +36,21 @@ impl Token { pub fn transfer_public( self: Self, - context: PublicContext, + context: &mut PublicContext, from: AztecAddress, to: AztecAddress, amount: Field, nonce: Field ) { - context.call_public_function( + let _ = context.call_public_function( self.address, FunctionSelector::from_signature("transfer_public((Field),(Field),Field,Field)"), [from.to_field(), to.to_field(), amount, nonce] ); } - pub fn mint_public(self: Self, context: PublicContext, to: AztecAddress, amount: Field) { - context.call_public_function( + pub fn mint_public(self: Self, context: &mut PublicContext, to: AztecAddress, amount: Field) { + let _ = context.call_public_function( self.address, FunctionSelector::from_signature("mint_public((Field),Field)"), [to.to_field(), amount] @@ -59,12 +59,12 @@ impl Token { pub fn burn_public( self: Self, - context: PublicContext, + context: &mut PublicContext, from: AztecAddress, amount: Field, nonce: Field ) { - context.call_public_function( + let _ = context.call_public_function( self.address, FunctionSelector::from_signature("burn_public((Field),Field,Field)"), [from.to_field(), amount, nonce] @@ -111,7 +111,7 @@ impl Lending { Self { address } } - pub fn update_accumulator(self: Self, context: PublicContext) -> Asset { + pub fn update_accumulator(self: Self, context: &mut PublicContext) -> Asset { let return_values = context.call_public_function_no_args( self.address, FunctionSelector::from_signature("update_accumulator()") diff --git a/noir-projects/noir-contracts/contracts/lending_contract/src/main.nr b/noir-projects/noir-contracts/contracts/lending_contract/src/main.nr index 7c06b7bc7c70..721c3d149abb 100644 --- a/noir-projects/noir-contracts/contracts/lending_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/lending_contract/src/main.nr @@ -115,14 +115,14 @@ contract Lending { #[aztec(public)] fn deposit_public(amount: Field, nonce: Field, on_behalf_of: Field, collateral_asset: AztecAddress) { Token::at(collateral_asset).transfer_public( - context, + &mut context, context.msg_sender(), context.this_address(), amount, nonce ); let selector = FunctionSelector::from_signature("_deposit((Field),Field,(Field))"); - context.call_public_function( + let _ = context.call_public_function( context.this_address(), selector, [on_behalf_of, amount, collateral_asset.to_field()] @@ -132,7 +132,7 @@ contract Lending { #[aztec(public)] #[aztec(internal)] fn _deposit(owner: AztecAddress, amount: Field, collateral_asset: AztecAddress) { - let _asset = Lending::at(context.this_address()).update_accumulator(context); + let _asset = Lending::at(context.this_address()).update_accumulator(&mut context); let coll_asset = storage.collateral_asset.read(); assert(coll_asset.eq(collateral_asset)); @@ -156,7 +156,7 @@ contract Lending { #[aztec(public)] fn withdraw_public(to: AztecAddress, amount: Field) { let selector = FunctionSelector::from_signature("_withdraw((Field),(Field),Field)"); - context.call_public_function( + let _ = context.call_public_function( context.this_address(), selector, [context.msg_sender().to_field(), to.to_field(), amount] @@ -166,8 +166,8 @@ contract Lending { #[aztec(public)] #[aztec(internal)] fn _withdraw(owner: AztecAddress, recipient: AztecAddress, amount: Field) { - let asset = Lending::at(context.this_address()).update_accumulator(context); - let price = PriceFeed::at(asset.oracle).get_price(context); + let asset = Lending::at(context.this_address()).update_accumulator(&mut context); + let price = PriceFeed::at(asset.oracle).get_price(&mut context); let coll_loc = storage.collateral.at(owner); let collateral: Field = coll_loc.read(); @@ -197,7 +197,7 @@ contract Lending { // @todo @LHerskind Support both shielding and transfers (for now just transfer) let collateral_asset = storage.collateral_asset.read(); - Token::at(collateral_asset).transfer_public(context, context.this_address(), recipient, amount, 0); + Token::at(collateral_asset).transfer_public(&mut context, context.this_address(), recipient, amount, 0); } #[aztec(private)] @@ -214,7 +214,7 @@ contract Lending { #[aztec(public)] fn borrow_public(to: AztecAddress, amount: Field) { let selector = FunctionSelector::from_signature("_borrow((Field),(Field),Field)"); - context.call_public_function( + let _ = context.call_public_function( context.this_address(), selector, [context.msg_sender().to_field(), to.to_field(), amount] @@ -224,8 +224,8 @@ contract Lending { #[aztec(public)] #[aztec(internal)] fn _borrow(owner: AztecAddress, to: AztecAddress, amount: Field) { - let asset = Lending::at(context.this_address()).update_accumulator(context); - let price = PriceFeed::at(asset.oracle).get_price(context); + let asset = Lending::at(context.this_address()).update_accumulator(&mut context); + let price = PriceFeed::at(asset.oracle).get_price(&mut context); // Fetch collateral and static_debt, compute health of current position let collateral = U128::from_integer(storage.collateral.at(owner).read()); @@ -251,7 +251,7 @@ contract Lending { // @todo @LHerskind Need to support both private and public minting. let stable_coin = storage.stable_coin.read(); - Token::at(stable_coin).mint_public(context, to, amount); + Token::at(stable_coin).mint_public(&mut context, to, amount); } #[aztec(private)] @@ -275,9 +275,9 @@ contract Lending { #[aztec(public)] fn repay_public(amount: Field, nonce: Field, owner: AztecAddress, stable_coin: AztecAddress) { - Token::at(stable_coin).burn_public(context, context.msg_sender(), amount, nonce); + Token::at(stable_coin).burn_public(&mut context, context.msg_sender(), amount, nonce); let selector = FunctionSelector::from_signature("_repay((Field),Field,(Field))"); - context.call_public_function( + let _ = context.call_public_function( context.this_address(), selector, [owner.to_field(), amount, stable_coin.to_field()] @@ -287,7 +287,7 @@ contract Lending { #[aztec(public)] #[aztec(internal)] fn _repay(owner: AztecAddress, amount: Field, stable_coin: AztecAddress) { - let asset = Lending::at(context.this_address()).update_accumulator(context); + let asset = Lending::at(context.this_address()).update_accumulator(&mut context); // To ensure that private is using the correct token. assert(stable_coin.eq(storage.stable_coin.read())); diff --git a/noir-projects/noir-contracts/contracts/test_contract/src/interface.nr b/noir-projects/noir-contracts/contracts/test_contract/src/interface.nr index 64c3a2d7fc94..f46f123d5c47 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/interface.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/interface.nr @@ -288,7 +288,7 @@ impl TestPublicContextInterface { pub fn create_nullifier_public( self, - context: PublicContext, + context: &mut PublicContext, amount: Field, secret_hash: Field ) -> [Field; RETURN_VALUES_LENGTH] { @@ -303,7 +303,11 @@ impl TestPublicContextInterface { ) } - pub fn emit_unencrypted(self, context: PublicContext, value: Field) -> [Field; RETURN_VALUES_LENGTH] { + pub fn emit_unencrypted( + self, + context: &mut PublicContext, + value: Field + ) -> [Field; RETURN_VALUES_LENGTH] { let mut serialized_args = [0; 1]; serialized_args[0] = value; @@ -314,7 +318,7 @@ impl TestPublicContextInterface { ) } - pub fn is_time_equal(self, context: PublicContext, time: Field) -> [Field; RETURN_VALUES_LENGTH] { + pub fn is_time_equal(self, context: &mut PublicContext, time: Field) -> [Field; RETURN_VALUES_LENGTH] { let mut serialized_args = [0; 1]; serialized_args[0] = time; @@ -327,7 +331,7 @@ impl TestPublicContextInterface { pub fn create_l2_to_l1_message_public( self, - context: PublicContext, + context: &mut PublicContext, amount: Field, secret_hash: Field ) -> [Field; RETURN_VALUES_LENGTH] { @@ -344,7 +348,7 @@ impl TestPublicContextInterface { pub fn consume_mint_public_message( self, - context: PublicContext, + context: &mut PublicContext, to: ToConsumeMintPublicMessageStruct, amount: Field, canceller: CancellerConsumeMintPublicMessageStruct, diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/interfaces.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/interfaces.nr index 3f4bf9983beb..9cb8253c9275 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/interfaces.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/interfaces.nr @@ -11,14 +11,14 @@ impl SlowMap { Self { address } } - pub fn initialize(self: Self, context: PublicContext) { - context.call_public_function_no_args( + pub fn initialize(self: Self, context: &mut PublicContext) { + let _ = context.call_public_function_no_args( self.address, FunctionSelector::from_signature("initialize()") ); } - pub fn read_at_pub(self: Self, context: PublicContext, index: Field) -> Field { + pub fn read_at_pub(self: Self, context: &mut PublicContext, index: Field) -> Field { let _return_values = context.call_public_function( self.address, FunctionSelector::from_signature("read_at_pub(Field)"), @@ -37,7 +37,7 @@ impl SlowMap { } pub fn update_at_private(self: Self, context: &mut PrivateContext, index: Field, new_value: Field) { - context.call_private_function( + let _ = context.call_private_function( self.address, FunctionSelector::from_signature("update_at_private(Field,Field)"), [index, new_value] diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr index 42b042711ce4..72879e4694c1 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr @@ -31,11 +31,11 @@ contract TokenBlacklist { // docs:end:interface struct Storage { - admin: PublicMutable, - balances: BalancesMap, + admin: PublicMutable, + balances: BalancesMap, total_supply: PublicMutable, - pending_shields: PrivateSet, - public_balances: Map>, + pending_shields: PrivateSet, + public_balances: Map>, slow_update: SharedImmutable, } @@ -50,7 +50,7 @@ contract TokenBlacklist { storage.slow_update.initialize(slow_updates_contract); // docs:end:write_slow_update_public // docs:start:slowmap_initialize - SlowMap::at(slow_updates_contract).initialize(context); + SlowMap::at(slow_updates_contract).initialize(&mut context); // docs:end:slowmap_initialize // We cannot do the following atm // let roles = UserFlags { is_admin: true, is_minter: false, is_blacklisted: false }.get_value().to_field(); @@ -94,11 +94,11 @@ contract TokenBlacklist { let slow = SlowMap::at(storage.slow_update.read_public()); // docs:end:get_public // docs:start:read_at_pub - let to_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(context, to.to_field()))); + let to_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(&mut context, to.to_field()))); // docs:end:read_at_pub assert(!to_roles.is_blacklisted, "Blacklisted: Recipient"); - let caller_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(context, context.msg_sender().to_field()))); + let caller_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(&mut context, context.msg_sender().to_field()))); assert(caller_roles.is_minter, "caller is not minter"); let amount = U128::from_integer(amount); @@ -112,7 +112,7 @@ contract TokenBlacklist { #[aztec(public)] fn mint_private(amount: Field, secret_hash: Field) { let slow = SlowMap::at(storage.slow_update.read_public()); - let caller_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(context, context.msg_sender().to_field()))); + let caller_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(&mut context, context.msg_sender().to_field()))); assert(caller_roles.is_minter, "caller is not minter"); let pending_shields = storage.pending_shields; @@ -126,7 +126,7 @@ contract TokenBlacklist { #[aztec(public)] fn shield(from: AztecAddress, amount: Field, secret_hash: Field, nonce: Field) { let slow = SlowMap::at(storage.slow_update.read_public()); - let from_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(context, from.to_field()))); + let from_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(&mut context, from.to_field()))); assert(!from_roles.is_blacklisted, "Blacklisted: Sender"); if (!from.eq(context.msg_sender())) { @@ -149,9 +149,9 @@ contract TokenBlacklist { #[aztec(public)] fn transfer_public(from: AztecAddress, to: AztecAddress, amount: Field, nonce: Field) { let slow = SlowMap::at(storage.slow_update.read_public()); - let from_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(context, from.to_field()))); + let from_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(&mut context, from.to_field()))); assert(!from_roles.is_blacklisted, "Blacklisted: Sender"); - let to_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(context, to.to_field()))); + let to_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(&mut context, to.to_field()))); assert(!to_roles.is_blacklisted, "Blacklisted: Recipient"); if (!from.eq(context.msg_sender())) { @@ -171,7 +171,7 @@ contract TokenBlacklist { #[aztec(public)] fn burn_public(from: AztecAddress, amount: Field, nonce: Field) { let slow = SlowMap::at(storage.slow_update.read_public()); - let from_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(context, from.to_field()))); + let from_roles = UserFlags::new(U128::from_integer(slow.read_at_pub(&mut context, from.to_field()))); assert(!from_roles.is_blacklisted, "Blacklisted: Sender"); if (!from.eq(context.msg_sender())) { diff --git a/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr index 1870537b2b9d..44ced870387b 100644 --- a/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_bridge_contract/src/main.nr @@ -41,7 +41,7 @@ contract TokenBridge { context.consume_l1_to_l2_message(content_hash, secret, context.this_portal_address()); // Mint tokens - Token::at(storage.token.read()).mint_public(context, to, amount); + Token::at(storage.token.read()).mint_public(&mut context, to, amount); } // docs:end:claim_public @@ -60,7 +60,7 @@ contract TokenBridge { context.message_portal(context.this_portal_address(), content); // Burn tokens - Token::at(storage.token.read()).burn_public(context, context.msg_sender(), amount, nonce); + Token::at(storage.token.read()).burn_public(&mut context, context.msg_sender(), amount, nonce); } // docs:end:exit_to_l1_public // docs:start:claim_private @@ -151,7 +151,7 @@ contract TokenBridge { #[aztec(public)] #[aztec(internal)] fn _call_mint_on_token(amount: Field, secret_hash: Field) { - Token::at(storage.token.read()).mint_private(context, amount, secret_hash); + Token::at(storage.token.read()).mint_private(&mut context, amount, secret_hash); } // docs:end:call_mint_on_token diff --git a/noir-projects/noir-contracts/contracts/token_bridge_contract/src/token_interface.nr b/noir-projects/noir-contracts/contracts/token_bridge_contract/src/token_interface.nr index 26c852a58bb0..684f19ebd448 100644 --- a/noir-projects/noir-contracts/contracts/token_bridge_contract/src/token_interface.nr +++ b/noir-projects/noir-contracts/contracts/token_bridge_contract/src/token_interface.nr @@ -11,7 +11,7 @@ impl Token { Self { address } } - pub fn mint_public(self: Self, context: PublicContext, to: AztecAddress, amount: Field) { + pub fn mint_public(self: Self, context: &mut PublicContext, to: AztecAddress, amount: Field) { let _return_values = context.call_public_function( self.address, FunctionSelector::from_signature("mint_public((Field),Field)"), @@ -22,7 +22,7 @@ impl Token { // docs:start:public_burn_interface pub fn burn_public( self: Self, - context: PublicContext, + context: &mut PublicContext, from: AztecAddress, amount: Field, nonce: Field @@ -35,7 +35,7 @@ impl Token { } // docs:end:public_burn_interface - pub fn mint_private(self: Self, context: PublicContext, amount: Field, secret_hash: Field) { + pub fn mint_private(self: Self, context: &mut PublicContext, amount: Field, secret_hash: Field) { let _return_values = context.call_public_function( self.address, FunctionSelector::from_signature("mint_private(Field,Field)"), diff --git a/noir-projects/noir-contracts/contracts/uniswap_contract/src/interfaces.nr b/noir-projects/noir-contracts/contracts/uniswap_contract/src/interfaces.nr index 77ef15753903..69972b4fb7aa 100644 --- a/noir-projects/noir-contracts/contracts/uniswap_contract/src/interfaces.nr +++ b/noir-projects/noir-contracts/contracts/uniswap_contract/src/interfaces.nr @@ -13,13 +13,13 @@ impl Token { pub fn transfer_public( self: Self, - context: PublicContext, + context: &mut PublicContext, from: AztecAddress, to: AztecAddress, amount: Field, nonce: Field ) { - context.call_public_function( + let _ = context.call_public_function( self.address, FunctionSelector::from_signature("transfer_public((Field),(Field),Field,Field)"), [from.to_field(), to.to_field(), amount, nonce] @@ -34,7 +34,7 @@ impl Token { amount: Field, nonce: Field ) { - context.call_private_function( + let _ = context.call_private_function( self.address, FunctionSelector::from_signature("unshield((Field),(Field),Field,Field)"), [from.to_field(), to.to_field(), amount, nonce] @@ -51,7 +51,7 @@ impl TokenBridge { Self { address } } - pub fn token(self: Self, context: PublicContext) -> AztecAddress { + pub fn token(self: Self, context: &mut PublicContext) -> AztecAddress { let return_values = context.call_public_function( self.address, FunctionSelector::from_signature("get_token()"), @@ -62,13 +62,13 @@ impl TokenBridge { pub fn exit_to_l1_public( self: Self, - context: PublicContext, + context: &mut PublicContext, recipient: EthAddress, amount: Field, callerOnL1: EthAddress, nonce: Field ) { - context.call_public_function( + let _ = context.call_public_function( self.address, FunctionSelector::from_signature("exit_to_l1_public((Field),Field,(Field),Field)"), [recipient.to_field(), amount, callerOnL1.to_field(), nonce] diff --git a/noir-projects/noir-contracts/contracts/uniswap_contract/src/main.nr b/noir-projects/noir-contracts/contracts/uniswap_contract/src/main.nr index 7a1fd038c80b..811e3e20cd12 100644 --- a/noir-projects/noir-contracts/contracts/uniswap_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/uniswap_contract/src/main.nr @@ -4,7 +4,7 @@ mod util; // Demonstrates how to use portal contracts to swap on L1 Uniswap with funds on L2 // Has two separate flows for private and public respectively -// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1 +// Uses the token bridge contract, which tells which input token we need to talk to and handles the exit funds to L1 contract Uniswap { use dep::aztec::prelude::{FunctionSelector, AztecAddress, EthAddress, Map, PublicMutable}; use dep::aztec::oracle::context::get_portal_address; @@ -54,11 +54,11 @@ contract Uniswap { assert_current_call_valid_authwit_public(&mut context, sender); } - let input_asset = TokenBridge::at(input_asset_bridge).token(context); + let input_asset = TokenBridge::at(input_asset_bridge).token(&mut context); // Transfer funds to this contract Token::at(input_asset).transfer_public( - context, + &mut context, sender, context.this_address(), input_amount, @@ -190,7 +190,7 @@ contract Uniswap { // docs:start:authwit_uniswap_set // This helper method approves the bridge to burn this contract's funds and exits the input asset to L1 - // Assumes contract already has funds. + // Assumes contract already has funds. // Assume `token` relates to `token_bridge` (ie token_bridge.token == token) // Note that private can't read public return values so created an internal public that handles everything // this method is used for both private and public swaps. @@ -213,7 +213,7 @@ contract Uniswap { // Exit to L1 Uniswap Portal ! TokenBridge::at(token_bridge).exit_to_l1_public( - context, + &mut context, context.this_portal_address(), amount, context.this_portal_address(), @@ -227,12 +227,12 @@ contract Uniswap { #[aztec(internal)] fn _assert_token_is_same(token: AztecAddress, token_bridge: AztecAddress) { assert( - token.eq(TokenBridge::at(token_bridge).token(context)), "input_asset address is not the same as seen in the bridge contract" + token.eq(TokenBridge::at(token_bridge).token(&mut context)), "input_asset address is not the same as seen in the bridge contract" ); } // docs:end:assert_token_is_same - // /// Unconstrained /// + // /// Unconstrained /// // this method exists solely for e2e tests to test that nonce gets incremented each time. unconstrained fn nonce_for_burn_approval() -> pub Field { diff --git a/yarn-project/simulator/src/acvm/oracle/oracle.ts b/yarn-project/simulator/src/acvm/oracle/oracle.ts index 48824adcae4b..f35b3fe1ec1f 100644 --- a/yarn-project/simulator/src/acvm/oracle/oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/oracle.ts @@ -303,7 +303,7 @@ export class Oracle { [contractAddress]: ACVMField[], [functionSelector]: ACVMField[], [argsHash]: ACVMField[], - [sideffectCounter]: ACVMField[], + [sideEffectCounter]: ACVMField[], [isStaticCall]: ACVMField[], [isDelegateCall]: ACVMField[], ): Promise { @@ -311,7 +311,7 @@ export class Oracle { AztecAddress.fromField(fromACVMField(contractAddress)), FunctionSelector.fromField(fromACVMField(functionSelector)), fromACVMField(argsHash), - frToNumber(fromACVMField(sideffectCounter)), + frToNumber(fromACVMField(sideEffectCounter)), frToBoolean(fromACVMField(isStaticCall)), frToBoolean(fromACVMField(isDelegateCall)), ); @@ -322,6 +322,7 @@ export class Oracle { [contractAddress]: ACVMField[], [functionSelector]: ACVMField[], [argsHash]: ACVMField[], + [sideEffectCounter]: ACVMField[], [isStaticCall]: ACVMField[], [isDelegateCall]: ACVMField[], ): Promise { @@ -329,6 +330,7 @@ export class Oracle { AztecAddress.fromField(fromACVMField(contractAddress)), FunctionSelector.fromField(fromACVMField(functionSelector)), fromACVMField(argsHash), + frToNumber(fromACVMField(sideEffectCounter)), frToBoolean(fromACVMField(isStaticCall)), frToBoolean(fromACVMField(isDelegateCall)), ); @@ -339,7 +341,7 @@ export class Oracle { [contractAddress]: ACVMField[], [functionSelector]: ACVMField[], [argsHash]: ACVMField[], - [sideffectCounter]: ACVMField[], + [sideEffectCounter]: ACVMField[], [isStaticCall]: ACVMField[], [isDelegateCall]: ACVMField[], ) { @@ -347,7 +349,7 @@ export class Oracle { AztecAddress.fromString(contractAddress), FunctionSelector.fromField(fromACVMField(functionSelector)), fromACVMField(argsHash), - frToNumber(fromACVMField(sideffectCounter)), + frToNumber(fromACVMField(sideEffectCounter)), frToBoolean(fromACVMField(isStaticCall)), frToBoolean(fromACVMField(isDelegateCall)), ); diff --git a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts index 22eaf2f29386..14ed76f4a949 100644 --- a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts @@ -195,7 +195,7 @@ export abstract class TypedOracle { _targetContractAddress: AztecAddress, _functionSelector: FunctionSelector, _argsHash: Fr, - _sideffectCounter: number, + _sideEffectCounter: number, _isStaticCall: boolean, _isDelegateCall: boolean, ): Promise { @@ -206,6 +206,7 @@ export abstract class TypedOracle { _targetContractAddress: AztecAddress, _functionSelector: FunctionSelector, _argsHash: Fr, + _sideEffectCounter: number, _isStaticCall: boolean, _isDelegateCall: boolean, ): Promise { @@ -216,7 +217,7 @@ export abstract class TypedOracle { _targetContractAddress: AztecAddress, _functionSelector: FunctionSelector, _argsHash: Fr, - _sideffectCounter: number, + _sideEffectCounter: number, _isStaticCall: boolean, _isDelegateCall: boolean, ): Promise { diff --git a/yarn-project/simulator/src/public/public_execution_context.ts b/yarn-project/simulator/src/public/public_execution_context.ts index 68ac9c7f6f20..b75dbdee2776 100644 --- a/yarn-project/simulator/src/public/public_execution_context.ts +++ b/yarn-project/simulator/src/public/public_execution_context.ts @@ -166,6 +166,7 @@ export class PublicExecutionContext extends TypedOracle { targetContractAddress: AztecAddress, functionSelector: FunctionSelector, argsHash: Fr, + sideEffectCounter: number, isStaticCall: boolean, isDelegateCall: boolean, ) { @@ -196,7 +197,7 @@ export class PublicExecutionContext extends TypedOracle { isDelegateCall, isStaticCall, // TODO (alexg) move this to noir - enqueuedSideEffectCounter: this.sideEffectCounter.count(), + enqueuedSideEffectCounter: sideEffectCounter, }); const nestedExecution: PublicExecution = {