-
Notifications
You must be signed in to change notification settings - Fork 86
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
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
3189ed3
feat: orderbook bench helper
wischli b7227b4
feat: add bench_fill_order_full
wischli bb563a0
refactor: bench_create_pool
wischli 80f4c2f
refactor: orderbook bench helper
wischli ac3ae0c
feat: add InvestmentIdBenchmarkHelper
wischli 6681346
Merge branch 'main' into feat/orderbook-bench-helper
wischli 32e28bd
feat: support single pallet check benchmark
wischli d1b80a5
fix: use fixed weight for fn process_msg()
mustermeiszer 2131eda
Merge branch 'main' into feat/orderbook-bench-helper
wischli ff04c51
fix: mock_bench_default_investment_id
wischli 6adb198
fix: order book bench
wischli daddfb6
refactor: use Ratio in FI
wischli 0af85e5
feat: ForeignInvestmentBenchmarkHelper
wischli 1d6a5b1
bench: fund pool account in create setup
wischli 9cd2f50
wip: bench lp inbound msg
wischli 67aef5b
Merge remote-tracking branch 'origin/feat/orderbook-bench-helper' int…
wischli 4fd7682
feat: bench inbound collect redeem
wischli 99e7087
tmp: present generated weights
wischli a9f5b3f
Merge remote-tracking branch 'origin/main' into bench/foreign-investm…
wischli d34c7ce
fix: Quantity remnant
wischli a22054e
refactor: defensive weights
wischli 239fa89
refactor: add FI bench setup return type
wischli 0d1c6f3
Merge remote-tracking branch 'origin/fix/unweighted-process-msg' into…
wischli 030c3a0
refactor: explicit return type
wischli 1936e32
Merge remote-tracking branch 'origin/main' into bench/foreign-investm…
wischli 04ea57a
Merge branch 'main' into bench/foreign-investments
wischli 90eb02e
Merge branch 'main' into bench/foreign-investments
wischli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
159 changes: 159 additions & 0 deletions
159
pallets/foreign-investments/src/impls/benchmark_utils.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
// 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 cfg_traits::{ | ||
benchmarking::{ | ||
BenchForeignInvestmentSetupInfo, ForeignInvestmentBenchmarkHelper, | ||
InvestmentIdBenchmarkHelper, OrderBookBenchmarkHelper, PoolBenchmarkHelper, | ||
}, | ||
investments::{ForeignInvestment, OrderManager}, | ||
}; | ||
use cfg_types::{ | ||
fixed_point::Ratio, | ||
orders::{FulfillmentWithPrice, TotalOrder}, | ||
tokens::CurrencyId, | ||
}; | ||
use frame_benchmarking::Zero; | ||
use frame_support::assert_ok; | ||
use sp_runtime::{DispatchError, FixedPointNumber, Perquintill}; | ||
|
||
use crate::{Config, Pallet}; | ||
|
||
pub const CURRENCY_POOL: CurrencyId = CurrencyId::ForeignAsset(1); | ||
pub const CURRENCY_FOREIGN: CurrencyId = CurrencyId::ForeignAsset(2); | ||
pub const DECIMALS_POOL: u32 = 12; | ||
pub const DECIMALS_FOREIGN: u32 = 6; | ||
pub const INVEST_AMOUNT_POOL_DENOMINATED: u128 = 1_000_000_000_000; | ||
pub const INVEST_AMOUNT_FOREIGN_DENOMINATED: u128 = INVEST_AMOUNT_POOL_DENOMINATED / 1_000_000; | ||
|
||
impl<T: Config> ForeignInvestmentBenchmarkHelper for Pallet<T> | ||
where | ||
T::Balance: From<u128>, | ||
T::CurrencyId: From<CurrencyId>, | ||
T::PoolInspect: PoolBenchmarkHelper<PoolId = T::PoolId, AccountId = T::AccountId, Balance = T::Balance> | ||
+ InvestmentIdBenchmarkHelper<PoolId = T::PoolId, InvestmentId = T::InvestmentId>, | ||
T::TokenSwaps: OrderBookBenchmarkHelper< | ||
AccountId = T::AccountId, | ||
Balance = T::Balance, | ||
CurrencyId = T::CurrencyId, | ||
OrderIdNonce = T::TokenSwapOrderId, | ||
>, | ||
T::Investment: OrderManager< | ||
Error = DispatchError, | ||
InvestmentId = T::InvestmentId, | ||
Orders = TotalOrder<T::Balance>, | ||
Fulfillment = FulfillmentWithPrice<T::BalanceRatio>, | ||
>, | ||
T::BalanceRatio: From<Ratio>, | ||
{ | ||
type AccountId = T::AccountId; | ||
type Balance = T::Balance; | ||
type CurrencyId = T::CurrencyId; | ||
type InvestmentId = T::InvestmentId; | ||
|
||
fn bench_prepare_foreign_investments_setup( | ||
) -> BenchForeignInvestmentSetupInfo<Self::AccountId, Self::InvestmentId, Self::CurrencyId> { | ||
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); | ||
|
||
// Add bidirectional trading pair and fund both accounts | ||
let (investor, funded_trader) = | ||
<T::TokenSwaps as OrderBookBenchmarkHelper>::bench_setup_trading_pair( | ||
CURRENCY_POOL.into(), | ||
CURRENCY_FOREIGN.into(), | ||
INVEST_AMOUNT_POOL_DENOMINATED.into(), | ||
INVEST_AMOUNT_FOREIGN_DENOMINATED.into(), | ||
DECIMALS_POOL.into(), | ||
DECIMALS_FOREIGN.into(), | ||
); | ||
<T::TokenSwaps as OrderBookBenchmarkHelper>::bench_setup_trading_pair( | ||
CURRENCY_FOREIGN.into(), | ||
CURRENCY_POOL.into(), | ||
INVEST_AMOUNT_FOREIGN_DENOMINATED.into(), | ||
INVEST_AMOUNT_POOL_DENOMINATED.into(), | ||
DECIMALS_FOREIGN.into(), | ||
DECIMALS_POOL.into(), | ||
); | ||
|
||
// Grant investor permissions | ||
<T::PoolInspect as PoolBenchmarkHelper>::bench_investor_setup( | ||
pool_id, | ||
investor.clone(), | ||
T::Balance::zero(), | ||
); | ||
let investment_id = | ||
<T::PoolInspect as InvestmentIdBenchmarkHelper>::bench_default_investment_id(pool_id); | ||
|
||
BenchForeignInvestmentSetupInfo { | ||
investor, | ||
investment_id, | ||
pool_currency: CURRENCY_POOL.into(), | ||
foreign_currency: CURRENCY_FOREIGN.into(), | ||
funded_trader, | ||
} | ||
} | ||
|
||
fn bench_prep_foreign_investments_worst_case( | ||
investor: Self::AccountId, | ||
investment_id: Self::InvestmentId, | ||
pool_currency: Self::CurrencyId, | ||
foreign_currency: Self::CurrencyId, | ||
) { | ||
log::debug!( | ||
"Preparing worst case foreign investment benchmark setup with pool currency {:?} and foreign currency: {:?}", | ||
pool_currency, | ||
foreign_currency | ||
); | ||
|
||
// Create `InvestState::ActiveSwapIntoPoolCurrency` and prepare redemption for | ||
// collection by redeeming | ||
assert_ok!(Pallet::<T>::increase_foreign_investment( | ||
&investor, | ||
investment_id, | ||
INVEST_AMOUNT_FOREIGN_DENOMINATED.into(), | ||
foreign_currency, | ||
pool_currency, | ||
)); | ||
assert_eq!( | ||
crate::InvestmentPaymentCurrency::<T>::get(&investor, investment_id).unwrap(), | ||
foreign_currency | ||
); | ||
|
||
log::debug!("Increasing foreign redemption"); | ||
assert_ok!(Pallet::<T>::increase_foreign_redemption( | ||
&investor, | ||
investment_id, | ||
INVEST_AMOUNT_FOREIGN_DENOMINATED.into(), | ||
foreign_currency, | ||
)); | ||
assert_eq!( | ||
crate::RedemptionPayoutCurrency::<T>::get(&investor, investment_id).unwrap(), | ||
foreign_currency | ||
); | ||
|
||
// Process redemption such that collecting will trigger worst case | ||
let fulfillment: FulfillmentWithPrice<T::BalanceRatio> = FulfillmentWithPrice { | ||
of_amount: Perquintill::from_percent(50), | ||
price: Ratio::checked_from_rational(1, 4).unwrap().into(), | ||
}; | ||
assert_ok!(<T::Investment as OrderManager>::process_redeem_orders( | ||
investment_id | ||
)); | ||
assert_ok!(<T::Investment as OrderManager>::redeem_fulfillment( | ||
investment_id, | ||
fulfillment | ||
)); | ||
log::debug!("Worst case benchmark foreign investment setup done!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -152,7 +152,7 @@ pub mod pallet { | |
|
||
/// Type for price ratio for cost of incoming currency relative to | ||
/// outgoing | ||
type Rate: Parameter | ||
type BalanceRatio: Parameter | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for feature creeping. This change does not have any functional impact as it's just renaming. |
||
+ Member | ||
+ sp_runtime::FixedPointNumber | ||
+ sp_runtime::traits::EnsureMul | ||
|
@@ -171,7 +171,7 @@ pub mod pallet { | |
/// more sophisticated swap price discovery. For now, this should be set | ||
/// to one. | ||
#[pallet::constant] | ||
type DefaultTokenSellRate: Get<Self::Rate>; | ||
type DefaultTokenSellRatio: Get<Self::BalanceRatio>; | ||
|
||
/// The token swap order identifying type | ||
type TokenSwapOrderId: Parameter | ||
|
@@ -190,7 +190,7 @@ pub mod pallet { | |
Balance = Self::Balance, | ||
OrderId = Self::TokenSwapOrderId, | ||
OrderDetails = Swap<Self::Balance, Self::CurrencyId>, | ||
SellRatio = Self::Rate, | ||
SellRatio = Self::BalanceRatio, | ||
>; | ||
|
||
/// The hook type which acts upon a finalized investment decrement. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2021 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 cfg_traits::{ | ||
benchmarking::{BenchForeignInvestmentSetupInfo, ForeignInvestmentBenchmarkHelper}, | ||
investments::{ForeignInvestment, TrancheCurrency}, | ||
}; | ||
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::Balance: From<u128>, | ||
T::AccountId: From<[u8; 32]> + Into<[u8; 32]>, | ||
)] | ||
mod benchmarks { | ||
use super::*; | ||
|
||
#[benchmark] | ||
fn inbound_collect_redeem() { | ||
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( | ||
investment_id.clone().into(), | ||
&investor, | ||
(u128::max_value() / 10).into(), | ||
)?; | ||
T::Tokens::mint_into(foreign_currency, &investor, (u128::max_value() / 10).into())?; | ||
|
||
// Increase investment and redemption | ||
<T::ForeignInvestment as ForeignInvestmentBenchmarkHelper>::bench_prep_foreign_investments_worst_case(investor.clone(), investment_id.clone(), pool_currency, foreign_currency); | ||
|
||
let investor_pointer = investor.clone(); | ||
let redeeming_amount = | ||
T::ForeignInvestment::redemption(&investor_pointer, investment_id.clone())?; | ||
let pool_id = investment_id.of_pool(); | ||
let tranche_id = investment_id.of_tranche(); | ||
let foreign_currency_index = Pallet::<T>::try_get_general_index(foreign_currency)?.into(); | ||
|
||
#[block] | ||
{ | ||
Pallet::<T>::handle_collect_redemption( | ||
pool_id, | ||
tranche_id, | ||
investor, | ||
foreign_currency_index, | ||
)?; | ||
} | ||
|
||
assert!( | ||
T::ForeignInvestment::redemption(&investor_pointer, investment_id)? < redeeming_amount | ||
); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 insidebench_*
methods, leaving a clean API to use from the benchmark side with 0 knowledge about what's happening under the hood. i.e. thepool_id
used is totally transparent here ❤️