Skip to content

Commit

Permalink
refactor: purge unconstrained functions where possible (#5819)
Browse files Browse the repository at this point in the history
Fixes #5451
  • Loading branch information
LHerskind authored Apr 22, 2024
1 parent 566f25c commit ce84161
Show file tree
Hide file tree
Showing 18 changed files with 112 additions and 250 deletions.
1 change: 0 additions & 1 deletion noir-projects/noir-contracts/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,5 @@ members = [
"contracts/token_blacklist_contract",
"contracts/token_bridge_contract",
"contracts/uniswap_contract",
"contracts/reader_contract",
"contracts/multi_call_entrypoint_contract",
]
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@ contract AvmTest {
/************************************************************************
* Storage
************************************************************************/
unconstrained fn view_storage_single() -> pub Field {
storage.single.read()
}

unconstrained fn view_storage_list() -> pub [Field; 2] {
storage.list.read().serialize()
}

unconstrained fn view_storage_map(address: AztecAddress) -> pub u32 {
storage.map.at(address).read()
}

#[aztec(public-vm)]
fn set_storage_single(a: Field) {
storage.single.write(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ contract GasToken {
rebate.to_field()
}

// utility function for testing
unconstrained fn balance_of_public(owner: AztecAddress) -> pub Field {
#[aztec(public)]
fn balance_of_public(owner: AztecAddress) -> pub Field {
storage.balances.at(owner).read().to_field()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ struct Asset {
oracle: AztecAddress,
}

global SERIALIZED_LEN: Field = 4;
global SERIALIZED_LEN: Field = 6;

impl Serialize<SERIALIZED_LEN> for Asset {
fn serialize(Asset: Asset) -> [Field; SERIALIZED_LEN] {
[
Asset.interest_accumulator.to_integer(),
Asset.interest_accumulator.lo as Field,
Asset.interest_accumulator.hi as Field,
Asset.last_updated_ts as Field,
Asset.loan_to_value.to_integer(),
Asset.loan_to_value.lo as Field,
Asset.loan_to_value.hi as Field,
Asset.oracle.to_field()
]
}
Expand All @@ -30,10 +32,10 @@ impl Deserialize<SERIALIZED_LEN> for Asset {
// Right now we are wasting so many writes. If changing last_updated_ts
// we will end up rewriting all of them, wasting writes.
fn deserialize(fields: [Field; SERIALIZED_LEN]) -> Asset {
let interest_accumulator = U128::from_integer(fields[0]);
let last_updated_ts = fields[1] as u64;
let loan_to_value = U128::from_integer(fields[2]);
let oracle = AztecAddress::from_field(fields[3]);
let interest_accumulator = U128 {lo: fields[0], hi: fields[1]};
let last_updated_ts = fields[2] as u64;
let loan_to_value = U128 {lo: fields[3], hi: fields[4]};
let oracle = AztecAddress::from_field(fields[5]);

Asset {
interest_accumulator,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod asset;
mod position;
mod interest_math;
mod helpers;

Expand All @@ -14,6 +15,7 @@ contract Lending {
use dep::aztec::context::{PublicContext, Context, gas::GasOpts};

use crate::asset::Asset;
use crate::position::Position;
use crate::interest_math::compute_multiplier;
use crate::helpers::{covered_by_collateral, DebtReturn, debt_updates, debt_value, compute_identifier};
use dep::token::Token;
Expand All @@ -29,12 +31,6 @@ contract Lending {
static_debt: Map<AztecAddress, PublicMutable<Field>>, // abusing keys very heavily
}

struct Position {
collateral: Field,
static_debt: Field,
debt: Field,
}

// Constructs the contract.
#[aztec(private)]
#[aztec(initializer)]
Expand Down Expand Up @@ -265,11 +261,13 @@ contract Lending {
storage.static_debt.at(owner).write(debt_returns.static_debt.to_integer());
}

unconstrained fn get_asset(asset_id: Field) -> pub Asset {
#[aztec(public)]
fn get_asset(asset_id: Field) -> pub Asset {
storage.assets.at(asset_id).read()
}

unconstrained fn get_position(owner: AztecAddress) -> pub Position {
#[aztec(public)]
fn get_position(owner: AztecAddress) -> pub Position {
let collateral = storage.collateral.at(owner).read();
let static_debt = storage.static_debt.at(owner).read();
let asset: Asset = storage.assets.at(0).read();
Expand All @@ -280,7 +278,8 @@ contract Lending {
Position { collateral, static_debt, debt }
}

unconstrained fn get_assets() -> pub [AztecAddress; 2] {
#[aztec(public)]
fn get_assets() -> pub [AztecAddress; 2] {
[storage.collateral_asset.read(), storage.stable_coin.read()]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use dep::aztec::prelude::AztecAddress;
use dep::aztec::protocol_types::traits::{Deserialize, Serialize};

struct Position {
collateral: Field,
static_debt: Field,
debt: Field,
}

global POSITION_SERIALIZED_LEN: Field = 3;

impl Serialize<POSITION_SERIALIZED_LEN> for Position {
fn serialize(position: Position) -> [Field; POSITION_SERIALIZED_LEN] {
[
position.collateral.to_field(),
position.static_debt.to_field(),
position.debt.to_field(),
]
}
}

impl Deserialize<POSITION_SERIALIZED_LEN> for Position {
fn deserialize(fields: [Field; POSITION_SERIALIZED_LEN]) -> Position {
Position {
collateral: fields[0],
static_debt: fields[1],
debt: fields[2],
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ global ASSET_SERIALIZED_LEN: Field = 1;

impl Serialize<ASSET_SERIALIZED_LEN> for Asset {
fn serialize(asset: Asset) -> [Field; ASSET_SERIALIZED_LEN] {
[asset.price.to_integer()]
[asset.price.to_field()]
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,4 @@ contract PriceFeed {
fn get_price(asset_id: Field) -> Asset {
storage.assets.at(asset_id).read()
}

unconstrained fn fetch_price(asset_id: Field) -> pub Asset {
storage.assets.at(asset_id).read()
}
}

This file was deleted.

69 changes: 0 additions & 69 deletions noir-projects/noir-contracts/contracts/reader_contract/src/main.nr

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ contract StatefulTest {
balance_utils::get_balance(owner_balance)
}

unconstrained fn get_public_value(owner: AztecAddress) -> pub Field {
#[aztec(public)]
fn get_public_value(owner: AztecAddress) -> pub Field {
storage.public_values.at(owner).read()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ contract TokenBlacklist {
slow_update: SharedImmutable<AztecAddress>,
}

#[aztec(public)]
fn total_supply() -> pub Field {
storage.total_supply.read().to_field()
}

#[aztec(public)]
fn balance_of_public(owner: AztecAddress) -> pub Field {
storage.public_balances.at(owner).read().to_field()
}

// docs:start:constructor
#[aztec(public)]
#[aztec(initializer)]
Expand Down Expand Up @@ -227,8 +237,7 @@ contract TokenBlacklist {

storage.balances.sub(from, U128::from_integer(amount));

let selector = FunctionSelector::from_signature("_increase_public_balance((Field),Field)");
context.call_public_function(context.this_address(), selector, [to.to_field(), amount]);
TokenBlacklist::at(context.this_address())._increase_public_balance(to, amount).enqueue(&mut context);
}

// docs:start:transfer_private
Expand Down Expand Up @@ -266,8 +275,7 @@ contract TokenBlacklist {

storage.balances.sub(from, U128::from_integer(amount));

let selector = FunctionSelector::from_signature("_reduce_total_supply(Field)");
context.call_public_function(context.this_address(), selector, [amount]);
TokenBlacklist::at(context.this_address())._reduce_total_supply(amount).enqueue(&mut context);
}

/// Internal ///
Expand All @@ -289,15 +297,7 @@ contract TokenBlacklist {

/// Unconstrained ///
unconstrained fn total_supply() -> pub Field {
storage.total_supply.read().to_field()
}

unconstrained fn balance_of_private(owner: AztecAddress) -> pub Field {
storage.balances.balance_of(owner).to_field()
}

unconstrained fn balance_of_public(owner: AztecAddress) -> pub Field {
storage.public_balances.at(owner).read().to_field()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ contract TokenBridge {
#[aztec(private)]
#[aztec(initializer)]
fn constructor(token: AztecAddress) {
let selector = FunctionSelector::from_signature("_initialize((Field))");
context.call_public_function(context.this_address(), selector, [token.to_field()]);
TokenBridge::at(context.this_address())._initialize(token).enqueue(&mut context);
}
// docs:end:token_bridge_storage_and_constructor

Expand Down Expand Up @@ -99,11 +98,7 @@ contract TokenBridge {
// `mint_private` on token is public. So we call an internal public function
// which then calls the public method on the token contract.
// Since the secret_hash is passed, no secret is leaked.
context.call_public_function(
context.this_address(),
FunctionSelector::from_signature("_call_mint_on_token(Field,Field)"),
[amount, secret_hash_for_redeeming_minted_notes]
);
TokenBridge::at(context.this_address())._call_mint_on_token(amount, secret_hash_for_redeeming_minted_notes).enqueue(&mut context);
}
// docs:end:claim_private

Expand All @@ -124,31 +119,19 @@ contract TokenBridge {

// docs:start:call_assert_token_is_same
// Assert that user provided token address is same as seen in storage.
context.call_public_function(
context.this_address(),
FunctionSelector::from_signature("_assert_token_is_same((Field))"),
[token.to_field()]
);
TokenBridge::at(context.this_address())._assert_token_is_same(token).enqueue(&mut context);
// docs:end:call_assert_token_is_same

// Burn tokens
Token::at(token).burn(context.msg_sender(), amount, nonce).call(&mut context);
}
/// docs:end:exit_to_l1_private
// View function that is callable by other contracts.
// Unconstrained can't be called by others since it isn't safe.
// docs:start:read_token
#[aztec(public)]
fn get_token() -> AztecAddress {
storage.token.read()
}

// /// Unconstrained ///

// docs:start:read_token
unconstrained fn token() -> pub AztecAddress {
storage.token.read()
}
// docs:end:read_token

#[aztec(public)]
Expand Down
Loading

0 comments on commit ce84161

Please sign in to comment.