Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: benchmark foreign investments worst case #1565

Merged
merged 27 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
3189ed3
feat: orderbook bench helper
wischli Sep 19, 2023
b7227b4
feat: add bench_fill_order_full
wischli Sep 19, 2023
bb563a0
refactor: bench_create_pool
wischli Sep 19, 2023
80f4c2f
refactor: orderbook bench helper
wischli Sep 19, 2023
ac3ae0c
feat: add InvestmentIdBenchmarkHelper
wischli Sep 20, 2023
6681346
Merge branch 'main' into feat/orderbook-bench-helper
wischli Sep 20, 2023
32e28bd
feat: support single pallet check benchmark
wischli Sep 20, 2023
d1b80a5
fix: use fixed weight for fn process_msg()
mustermeiszer Sep 22, 2023
2131eda
Merge branch 'main' into feat/orderbook-bench-helper
wischli Sep 22, 2023
ff04c51
fix: mock_bench_default_investment_id
wischli Sep 22, 2023
6adb198
fix: order book bench
wischli Sep 22, 2023
daddfb6
refactor: use Ratio in FI
wischli Sep 22, 2023
0af85e5
feat: ForeignInvestmentBenchmarkHelper
wischli Sep 22, 2023
1d6a5b1
bench: fund pool account in create setup
wischli Sep 22, 2023
9cd2f50
wip: bench lp inbound msg
wischli Sep 22, 2023
67aef5b
Merge remote-tracking branch 'origin/feat/orderbook-bench-helper' int…
wischli Sep 22, 2023
4fd7682
feat: bench inbound collect redeem
wischli Sep 22, 2023
99e7087
tmp: present generated weights
wischli Sep 22, 2023
a9f5b3f
Merge remote-tracking branch 'origin/main' into bench/foreign-investm…
wischli Sep 22, 2023
d34c7ce
fix: Quantity remnant
wischli Sep 25, 2023
a22054e
refactor: defensive weights
wischli Sep 25, 2023
239fa89
refactor: add FI bench setup return type
wischli Sep 25, 2023
0d1c6f3
Merge remote-tracking branch 'origin/fix/unweighted-process-msg' into…
wischli Sep 25, 2023
030c3a0
refactor: explicit return type
wischli Sep 25, 2023
1936e32
Merge remote-tracking branch 'origin/main' into bench/foreign-investm…
wischli Sep 25, 2023
04ea57a
Merge branch 'main' into bench/foreign-investments
wischli Sep 25, 2023
90eb02e
Merge branch 'main' into bench/foreign-investments
wischli Sep 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions libs/traits/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub trait ForeignInvestmentBenchmarkHelper {
type Balance;
type CurrencyId;
type InvestmentId;
type SetupInfo;
lemunozm marked this conversation as resolved.
Show resolved Hide resolved

/// Perform necessary setup to enable an investor to invest with or redeem
/// into a foreign currency.
Expand All @@ -78,13 +79,7 @@ pub trait ForeignInvestmentBenchmarkHelper {
/// * The foreign currency id
/// * A trading account which can bidirectionally fulfill swap orders for
/// the (foreign, pool) currency pair
fn bench_prepare_foreign_investments_setup() -> (
Self::AccountId,
Self::InvestmentId,
Self::CurrencyId,
Self::CurrencyId,
Self::AccountId,
);
fn bench_prepare_foreign_investments_setup() -> Self::SetupInfo;

/// Perform necessary setup to prepare for the worst benchmark case by
/// calling just a single subsequent function.
Expand Down
17 changes: 17 additions & 0 deletions libs/types/src/investments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,20 @@ pub struct ExecutedForeignCollect<Balance, Currency> {
/// pool currency)
pub amount_remaining: Balance,
}

/// A representation of information helpful when doing a foreign investment
/// benchmark setup.
#[cfg(feature = "runtime-benchmarks")]
pub struct BenchForeignInvestmentSetupInfo<AccountId, InvestmentId, CurrencyId> {
/// The substrate investor address
pub investor: AccountId,
/// The investment id
pub investment_id: InvestmentId,
/// The pool currency which eventually will be invested
pub pool_currency: CurrencyId,
/// The foreign currency which shall be invested and thus swapped into pool
/// currency beforehand
pub foreign_currency: CurrencyId,
/// Bidirectionally funded to fulfill token swap orders
pub funded_trader: AccountId,
}
24 changes: 10 additions & 14 deletions pallets/foreign-investments/src/impls/benchmark_utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Centrifuge Foundation (centrifuge.io).
// Copyright 2023 Centrifuge Foundation (centrifuge.io).
// This file is part of Centrifuge Chain project.

// Centrifuge is free software: you can redistribute it and/or modify
Expand All @@ -20,6 +20,7 @@ use cfg_traits::{
};
use cfg_types::{
fixed_point::Ratio,
investments::BenchForeignInvestmentSetupInfo,
orders::{FulfillmentWithPrice, TotalOrder},
tokens::CurrencyId,
};
Expand Down Expand Up @@ -60,20 +61,15 @@ where
type Balance = T::Balance;
type CurrencyId = T::CurrencyId;
type InvestmentId = T::InvestmentId;
type SetupInfo = BenchForeignInvestmentSetupInfo<T::AccountId, T::InvestmentId, T::CurrencyId>;

fn bench_prepare_foreign_investments_setup() -> (
T::AccountId,
T::InvestmentId,
T::CurrencyId,
T::CurrencyId,
T::AccountId,
) {
fn bench_prepare_foreign_investments_setup() -> Self::SetupInfo {
let pool_id = Default::default();
let pool_admin: T::AccountId = frame_benchmarking::account("pool_admin", 0, 0);
<T::PoolInspect as PoolBenchmarkHelper>::bench_create_pool(pool_id, &pool_admin);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment: I really like how the bench_* methods are called inside bench_* methods, leaving a clean API to use from the benchmark side with 0 knowledge about what's happening under the hood. i.e. the pool_id used is totally transparent here ❤️

// Add bidirectional trading pair and fund both accounts
let (investor, trader) =
let (investor, funded_trader) =
<T::TokenSwaps as OrderBookBenchmarkHelper>::bench_setup_trading_pair(
CURRENCY_POOL.into(),
CURRENCY_FOREIGN.into(),
Expand All @@ -100,13 +96,13 @@ where
let investment_id =
<T::PoolInspect as InvestmentIdBenchmarkHelper>::bench_default_investment_id(pool_id);

(
Self::SetupInfo {
investor,
investment_id,
CURRENCY_POOL.into(),
CURRENCY_FOREIGN.into(),
trader,
)
pool_currency: CURRENCY_POOL.into(),
foreign_currency: CURRENCY_FOREIGN.into(),
funded_trader,
}
}

fn bench_prep_foreign_investments_worst_case(
Expand Down
6 changes: 0 additions & 6 deletions pallets/foreign-investments/src/impls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1252,9 +1252,3 @@ impl<T: Config> Pallet<T> {
)
}
}

// #[cfg(feature = "runtime-benchmarks")]
// impl<T: Config> cfg_traits::benchmarking::ForeignInvestmentBenchmarkHelper
// for Pallet<T> {

// }
2 changes: 1 addition & 1 deletion pallets/liquidity-pools-gateway/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ pub mod pallet {
}

/// Process an incoming message.
#[pallet::weight(0)]
#[pallet::weight(T::WeightInfo::process_msg())]
#[pallet::call_index(5)]
pub fn process_msg(
origin: OriginFor<T>,
Expand Down
5 changes: 3 additions & 2 deletions pallets/liquidity-pools/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ use cfg_traits::{
benchmarking::ForeignInvestmentBenchmarkHelper,
investments::{ForeignInvestment, TrancheCurrency},
};
use cfg_types::investments::BenchForeignInvestmentSetupInfo;
use frame_benchmarking::v2::*;

use super::*;
use crate::Pallet;

#[benchmarks(
where
T::ForeignInvestment: ForeignInvestmentBenchmarkHelper<AccountId = T::AccountId, Balance = T::Balance, CurrencyId = T::CurrencyId, InvestmentId = T::TrancheCurrency>,
T::ForeignInvestment: ForeignInvestmentBenchmarkHelper<AccountId = T::AccountId, Balance = T::Balance, CurrencyId = T::CurrencyId, InvestmentId = T::TrancheCurrency, SetupInfo = BenchForeignInvestmentSetupInfo<T::AccountId, T::TrancheCurrency, T::CurrencyId>>,
T::Balance: From<u128>,
T::AccountId: From<[u8; 32]> + Into<[u8; 32]>,
)]
Expand All @@ -31,7 +32,7 @@ mod benchmarks {

#[benchmark]
fn inbound_collect_redeem() {
let (investor, investment_id, pool_currency, foreign_currency, _) = <T::ForeignInvestment as ForeignInvestmentBenchmarkHelper>::bench_prepare_foreign_investments_setup();
let BenchForeignInvestmentSetupInfo { investor, investment_id, pool_currency, foreign_currency, .. } = <T::ForeignInvestment as ForeignInvestmentBenchmarkHelper>::bench_prepare_foreign_investments_setup();

// Fund investor with foreign currency and tranche tokens
T::Tokens::mint_into(
Expand Down
95 changes: 95 additions & 0 deletions pallets/liquidity-pools/src/defensive_weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2023 Centrifuge Foundation (centrifuge.io).
// This file is part of Centrifuge chain project.

// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).

// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
use frame_support::weights::{constants::RocksDbWeight, Weight};

pub trait WeightInfo {
fn add_pool() -> Weight;
fn add_tranche() -> Weight;
fn update_token_price() -> Weight;
fn update_member() -> Weight;
fn transfer() -> Weight;
fn set_domain_router() -> Weight;
}

// NOTE: We use temporary weights here. `execute_epoch` is by far our heaviest
// extrinsic. N denotes the number of tranches. 4 is quite heavy and
// should be enough.
const N: u64 = 4;

impl WeightInfo for () {
fn set_domain_router() -> Weight {
// TODO: BENCHMARK AND USE REAL WEIGHTS
Weight::from_parts(78_019_565, 19974)
.saturating_add(Weight::from_ref_time(38_884_782).saturating_mul(N))
.saturating_add(RocksDbWeight::get().reads(8))
.saturating_add(RocksDbWeight::get().reads((7_u64).saturating_mul(N)))
.saturating_add(RocksDbWeight::get().writes(8))
.saturating_add(RocksDbWeight::get().writes((6_u64).saturating_mul(N)))
.saturating_add(Weight::from_proof_size(17774).saturating_mul(N))
}

fn add_pool() -> Weight {
// TODO: BENCHMARK AND USE REAL WEIGHTS
Weight::from_parts(78_019_565, 19974)
.saturating_add(Weight::from_ref_time(38_884_782).saturating_mul(N))
.saturating_add(RocksDbWeight::get().reads(8))
.saturating_add(RocksDbWeight::get().reads((7_u64).saturating_mul(N)))
.saturating_add(RocksDbWeight::get().writes(8))
.saturating_add(RocksDbWeight::get().writes((6_u64).saturating_mul(N)))
.saturating_add(Weight::from_proof_size(17774).saturating_mul(N))
}

fn add_tranche() -> Weight {
// TODO: BENCHMARK AND USE REAL WEIGHTS
Weight::from_parts(78_019_565, 19974)
.saturating_add(Weight::from_ref_time(38_884_782).saturating_mul(N))
.saturating_add(RocksDbWeight::get().reads(8))
.saturating_add(RocksDbWeight::get().reads((7_u64).saturating_mul(N)))
.saturating_add(RocksDbWeight::get().writes(8))
.saturating_add(RocksDbWeight::get().writes((6_u64).saturating_mul(N)))
.saturating_add(Weight::from_proof_size(17774).saturating_mul(N))
}

fn update_token_price() -> Weight {
// TODO: BENCHMARK AND USE REAL WEIGHTS
Weight::from_parts(78_019_565, 19974)
.saturating_add(Weight::from_ref_time(38_884_782).saturating_mul(N))
.saturating_add(RocksDbWeight::get().reads(8))
.saturating_add(RocksDbWeight::get().reads((7_u64).saturating_mul(N)))
.saturating_add(RocksDbWeight::get().writes(8))
.saturating_add(RocksDbWeight::get().writes((6_u64).saturating_mul(N)))
.saturating_add(Weight::from_proof_size(17774).saturating_mul(N))
}

fn update_member() -> Weight {
// TODO: BENCHMARK AND USE REAL WEIGHTS
Weight::from_parts(78_019_565, 19974)
.saturating_add(Weight::from_ref_time(38_884_782).saturating_mul(N))
.saturating_add(RocksDbWeight::get().reads(8))
.saturating_add(RocksDbWeight::get().reads((7_u64).saturating_mul(N)))
.saturating_add(RocksDbWeight::get().writes(8))
.saturating_add(RocksDbWeight::get().writes((6_u64).saturating_mul(N)))
.saturating_add(Weight::from_proof_size(17774).saturating_mul(N))
}

fn transfer() -> Weight {
// TODO: BENCHMARK AND USE REAL WEIGHTS
Weight::from_parts(78_019_565, 19974)
.saturating_add(Weight::from_ref_time(38_884_782).saturating_mul(N))
.saturating_add(RocksDbWeight::get().reads(8))
.saturating_add(RocksDbWeight::get().reads((7_u64).saturating_mul(N)))
.saturating_add(RocksDbWeight::get().writes(8))
.saturating_add(RocksDbWeight::get().writes((6_u64).saturating_mul(N)))
.saturating_add(Weight::from_proof_size(17774).saturating_mul(N))
}
}
6 changes: 4 additions & 2 deletions pallets/liquidity-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ use xcm::{
VersionedMultiLocation,
};

pub mod weights;
// NOTE: Should be replaced with generated weights in the future. For now, let's
// be defensive.
pub mod defensive_weights;

mod message;
pub use message::*;
Expand Down Expand Up @@ -129,7 +131,7 @@ pub mod pallet {
use xcm::latest::MultiLocation;

use super::*;
use crate::weights::WeightInfo;
use crate::defensive_weights::WeightInfo;

#[pallet::pallet]
#[pallet::generate_store(pub (super) trait Store)]
Expand Down
Loading
Loading