Skip to content

Commit

Permalink
feat: public function call counter on context
Browse files Browse the repository at this point in the history
  • Loading branch information
alexghr committed Mar 11, 2024
1 parent 87c2bb7 commit bdced68
Show file tree
Hide file tree
Showing 18 changed files with 154 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
79 changes: 67 additions & 12 deletions noir-projects/aztec-nr/aztec/src/context/public_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -204,59 +204,114 @@ impl PublicContext {
}

pub fn call_public_function<ARGS_COUNT>(
_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<ARGS_COUNT>(
_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<ARGS_COUNT>(
_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
)
}
}
3 changes: 3 additions & 0 deletions noir-projects/aztec-nr/aztec/src/oracle/public_call.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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] {}
Expand All @@ -13,13 +14,15 @@ 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] {
call_public_function_oracle(
contract_address,
function_selector,
args_hash,
side_effect_counter,
is_static_call,
is_delegate_call
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Token {

pub fn transfer_public(
self: Self,
context: PublicContext,
context: &mut PublicContext,
from: AztecAddress,
to: AztecAddress,
amount: Field,
Expand All @@ -29,7 +29,7 @@ impl Token {

pub fn shield(
self: Self,
context: PublicContext,
context: &mut PublicContext,
from: AztecAddress,
amount: Field,
secret_hash: Field,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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)]
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)"),
Expand All @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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()")
Expand Down
28 changes: 14 additions & 14 deletions noir-projects/noir-contracts/contracts/lending_contract/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand All @@ -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));
Expand All @@ -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]
Expand All @@ -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();
Expand Down Expand Up @@ -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)]
Expand All @@ -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]
Expand All @@ -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());
Expand All @@ -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)]
Expand All @@ -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()]
Expand All @@ -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()));
Expand Down
Loading

0 comments on commit bdced68

Please sign in to comment.