Skip to content

Commit

Permalink
chore: purge unconstrained + batch simulate improvements (#6639)
Browse files Browse the repository at this point in the history
Recovers work done by @LHerskind in
#5819 combining it
with batched simulations to avoid 💀 ⏳ in tests

---------

Co-authored-by: LHerskind <[email protected]>
  • Loading branch information
Thunkar and LHerskind authored May 27, 2024
1 parent 1512017 commit 1945ed9
Show file tree
Hide file tree
Showing 39 changed files with 212 additions and 296 deletions.
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ library Constants {
uint256 internal constant DEPLOYER_CONTRACT_ADDRESS =
0x02f1337e8c79dd0247ccbde85241ad65ee991ae283a63479e095e51f0abbc7e3;
uint256 internal constant GAS_TOKEN_ADDRESS =
0x03d751d1a8655b35b0d0c8d74e35219104fe0b011bd262ee26e6c6a5c557c801;
0x2271d994fae5e4279485ca23d5c2408f408155676cd31d487d127bae206d026f;
uint256 internal constant AZTEC_ADDRESS_LENGTH = 1;
uint256 internal constant GAS_FEES_LENGTH = 2;
uint256 internal constant GAS_LENGTH = 2;
Expand Down
1 change: 0 additions & 1 deletion noir-projects/noir-contracts/Nargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ members = [
"contracts/token_blacklist_contract",
"contracts/token_bridge_contract",
"contracts/uniswap_contract",
"contracts/reader_contract",
"contracts/multi_call_entrypoint_contract",
"contracts/static_child_contract",
"contracts/static_parent_contract"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,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)]
fn set_storage_single(a: Field) {
storage.single.write(a);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ contract GasToken {
}

#[aztec(public)]
#[aztec(view)]
fn check_balance(fee_limit: Field) {
let fee_limit = U128::from_integer(fee_limit);
assert(storage.balances.at(context.msg_sender()).read() >= fee_limit, "Balance too low");
}

// utility function for testing
unconstrained fn balance_of_public(owner: AztecAddress) -> pub Field {
#[aztec(public)]
#[aztec(view)]
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
@@ -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, 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 @@ -269,11 +265,15 @@ 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)]
#[aztec(view)]
fn get_asset(asset_id: Field) -> pub Asset {
storage.assets.at(asset_id).read()
}

unconstrained fn get_position(owner: AztecAddress) -> pub Position {
#[aztec(public)]
#[aztec(view)]
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 @@ -284,7 +284,9 @@ contract Lending {
Position { collateral, static_debt, debt }
}

unconstrained fn get_assets() -> pub [AztecAddress; 2] {
#[aztec(public)]
#[aztec(view)]
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 @@ -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.

71 changes: 0 additions & 71 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 @@ -93,7 +93,10 @@ contract StatefulTest {
balance_utils::get_balance(owner_balance)
}

unconstrained fn get_public_value(owner: AztecAddress) -> pub Field {
#[aztec(public)]
#[aztec(noinitcheck)]
#[aztec(view)]
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 @@ -40,6 +40,19 @@ contract TokenBlacklist {
}

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

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

#[aztec(public)]
#[aztec(view)]
fn get_roles(user: AztecAddress) -> UserFlags {
storage.roles.at(user).get_current_value_in_public()
}
Expand Down Expand Up @@ -182,8 +195,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 @@ -218,8 +230,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 @@ -241,15 +252,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 @@ -98,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 @@ -123,35 +119,22 @@ 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:get_token
#[aztec(public)]
#[aztec(view)]
fn get_token() -> AztecAddress {
storage.token.read()
}
// docs:end:get_token

// /// Unconstrained ///

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

// docs:start:call_mint_on_token
// This is a public call as we need to read from public storage.
// Also, note that user hashes their secret in private and only sends the hash in public
Expand Down
Loading

0 comments on commit 1945ed9

Please sign in to comment.