Skip to content

Commit

Permalink
Add benchmarking to lm (#309)
Browse files Browse the repository at this point in the history
* 🚧 ($PALLET) Add benchmarking and weights to lm

* 💡 ($PALLET) Add docs on `PoolType`

* ✨ ($PALLET) Discard the weights of sudo extrinsics

* 🐛 ($PALLET) Fix salp & vsbond-auction benchmarking code

* 🎨 ($PALLET) Rename T to Test

* 🚧 ($PALLET) Upgrade..

* 🚧 ($PALLET) Compl benchmarking

* 🐛 ($PALLT) Fix some errs
  • Loading branch information
AllenPocketGamer authored Sep 23, 2021
1 parent 5bc6047 commit bb89fb6
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 90 deletions.
11 changes: 11 additions & 0 deletions pallets/liquidity-mining/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ frame-system = { git = "https://github.com/paritytech/substrate", branch = "polk
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9", default-features = false }
node-primitives = { path = "../../node/primitives", default-features = false }
orml-traits = { version = "0.4.1-dev", default-features = false }
frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9", default-features = false, optional = true }

[dev-dependencies]
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.9" }
Expand All @@ -28,4 +29,14 @@ std = [
"frame-support/std",
"node-primitives/std",
"orml-traits/std",
]

runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]

local-benchmarks = [
"pallet-collective/runtime-benchmarks",
]
224 changes: 224 additions & 0 deletions pallets/liquidity-mining/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,224 @@
// This file is part of Bifrost.

// Copyright (C) 2019-2021 Liebi Technologies (UK) Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program 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.

// This program 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.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

// Ensure we're `no_std` when compiling for Wasm.
#![cfg(feature = "runtime-benchmarks")]
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_support::{assert_ok, sp_runtime::sp_std::convert::TryInto, sp_std::prelude::*};
use frame_system::RawOrigin;

use crate::{Pallet as LM, *};

const FARMING_DEPOSIT_1: CurrencyId = CurrencyId::VSToken(TokenSymbol::KSM);
const FARMING_DEPOSIT_2: CurrencyId = CurrencyId::VSBond(TokenSymbol::KSM, 2001, 13, 20);
const REWARD_1: CurrencyId = CurrencyId::Native(TokenSymbol::BNC);
const REWARD_2: CurrencyId = CurrencyId::Token(TokenSymbol::KSM);

fn run_to_block<T: Config>(n: BlockNumberFor<T>) {
type System<T> = frame_system::Pallet<T>;

while System::<T>::block_number() < n {
LM::<T>::on_finalize(System::<T>::block_number());
System::<T>::on_finalize(System::<T>::block_number());
System::<T>::set_block_number(System::<T>::block_number() + 1u128.saturated_into());
System::<T>::on_initialize(System::<T>::block_number());
LM::<T>::on_initialize(System::<T>::block_number());
}
}

benchmarks! {
charge {
let caller: T::AccountId = whitelisted_caller();

let duration = T::MinimumDuration::get().saturating_add(1u128.saturated_into());
let min_deposit_to_start = T::MinimumDepositOfUser::get();
let reward_amount: BalanceOf<T> = {
let duration: u128 = duration.saturated_into();
let per_block: u128 = T::MinimumRewardPerBlock::get().saturated_into();
(duration * (per_block + 1)).saturated_into()
};

assert_ok!(<T as Config>::MultiCurrency::deposit(REWARD_1, &caller, reward_amount));
assert_ok!(<T as Config>::MultiCurrency::deposit(REWARD_2, &caller, reward_amount));

assert_ok!(LM::<T>::create_pool(
(FARMING_DEPOSIT_1, FARMING_DEPOSIT_2),
(REWARD_1, reward_amount),
vec![(REWARD_2, reward_amount)].try_into().unwrap(),
PoolType::Farming,
duration,
min_deposit_to_start,
0u128.saturated_into()
));
}: _(RawOrigin::Signed(caller.clone()), 0)

deposit {
let duration = T::MinimumDuration::get().saturating_add(1u128.saturated_into());
let min_deposit_to_start = T::MinimumDepositOfUser::get();
let reward_amount: BalanceOf<T> = {
let duration: u128 = duration.saturated_into();
let per_block: u128 = T::MinimumRewardPerBlock::get().saturated_into();
(duration * (per_block + 1)).saturated_into()
};

let investor: T::AccountId = account("lm", 0, 0);
assert_ok!(<T as Config>::MultiCurrency::deposit(REWARD_1, &investor, reward_amount));
assert_ok!(<T as Config>::MultiCurrency::deposit(REWARD_2, &investor, reward_amount));

let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiCurrency::deposit(FARMING_DEPOSIT_1, &caller, T::MinimumDepositOfUser::get()));
assert_ok!(<T as Config>::MultiCurrency::deposit(FARMING_DEPOSIT_2, &caller, T::MinimumDepositOfUser::get()));

assert_ok!(LM::<T>::create_pool(
(FARMING_DEPOSIT_1, FARMING_DEPOSIT_2),
(REWARD_1, reward_amount),
vec![(REWARD_2, reward_amount)].try_into().unwrap(),
PoolType::Farming,
duration,
min_deposit_to_start,
0u128.saturated_into()
));

assert_ok!(LM::<T>::charge(RawOrigin::Signed(investor).into(), 0));

}: _(RawOrigin::Signed(caller.clone()), 0, T::MinimumDepositOfUser::get())

redeem {
let duration = T::MinimumDuration::get().saturating_add(1u128.saturated_into());
let min_deposit_to_start = T::MinimumDepositOfUser::get();
let reward_amount: BalanceOf<T> = {
let duration: u128 = duration.saturated_into();
let per_block: u128 = T::MinimumRewardPerBlock::get().saturated_into();
(duration * (per_block + 1)).saturated_into()
};

let investor: T::AccountId = account("lm", 0, 0);
assert_ok!(<T as Config>::MultiCurrency::deposit(REWARD_1, &investor, reward_amount));
assert_ok!(<T as Config>::MultiCurrency::deposit(REWARD_2, &investor, reward_amount));

let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiCurrency::deposit(FARMING_DEPOSIT_1, &caller, T::MinimumDepositOfUser::get()));
assert_ok!(<T as Config>::MultiCurrency::deposit(FARMING_DEPOSIT_2, &caller, T::MinimumDepositOfUser::get()));

assert_ok!(LM::<T>::create_pool(
(FARMING_DEPOSIT_1, FARMING_DEPOSIT_2),
(REWARD_1, reward_amount),
vec![(REWARD_2, reward_amount)].try_into().unwrap(),
PoolType::Farming,
duration,
min_deposit_to_start,
0u128.saturated_into()
));

assert_ok!(LM::<T>::charge(RawOrigin::Signed(investor).into(), 0));

assert_ok!(LM::<T>::deposit(RawOrigin::Signed(caller.clone()).into(), 0, T::MinimumDepositOfUser::get()));

// Run to block
run_to_block::<T>(duration);

}: _(RawOrigin::Signed(caller.clone()), 0)
verify {
let pool = LM::<T>::pool(0);
let deposit_data = LM::<T>::user_deposit_data(0, caller.clone());
assert!(pool.is_none());
assert!(deposit_data.is_none());
}

volunteer_to_redeem {
let duration = T::MinimumDuration::get().saturating_add(1u128.saturated_into());
let min_deposit_to_start = T::MinimumDepositOfUser::get();
let reward_amount: BalanceOf<T> = {
let duration: u128 = duration.saturated_into();
let per_block: u128 = T::MinimumRewardPerBlock::get().saturated_into();
(duration * (per_block + 1)).saturated_into()
};

let investor: T::AccountId = account("lm", 0, 0);
assert_ok!(<T as Config>::MultiCurrency::deposit(REWARD_1, &investor, reward_amount));
assert_ok!(<T as Config>::MultiCurrency::deposit(REWARD_2, &investor, reward_amount));

let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiCurrency::deposit(FARMING_DEPOSIT_1, &caller, T::MinimumDepositOfUser::get()));
assert_ok!(<T as Config>::MultiCurrency::deposit(FARMING_DEPOSIT_2, &caller, T::MinimumDepositOfUser::get()));

assert_ok!(LM::<T>::create_pool(
(FARMING_DEPOSIT_1, FARMING_DEPOSIT_2),
(REWARD_1, reward_amount),
vec![(REWARD_2, reward_amount)].try_into().unwrap(),
PoolType::Farming,
duration,
min_deposit_to_start,
0u128.saturated_into()
));

assert_ok!(LM::<T>::charge(RawOrigin::Signed(investor).into(), 0));

assert_ok!(LM::<T>::deposit(RawOrigin::Signed(caller.clone()).into(), 0, T::MinimumDepositOfUser::get()));

// Run to block
run_to_block::<T>(duration);

let volunteer = account("lm", 0, 1);

}: _(RawOrigin::Signed(volunteer), 0, None)
verify {
let pool = LM::<T>::pool(0);
let deposit_data = LM::<T>::user_deposit_data(0, caller.clone());
assert!(pool.is_none());
assert!(deposit_data.is_none());
}

claim {
let duration = T::MinimumDuration::get().saturating_add(1u128.saturated_into());
let min_deposit_to_start = T::MinimumDepositOfUser::get();
let reward_amount: BalanceOf<T> = {
let duration: u128 = duration.saturated_into();
let per_block: u128 = T::MinimumRewardPerBlock::get().saturated_into();
(duration * (per_block + 1)).saturated_into()
};

let investor: T::AccountId = account("lm", 0, 0);
assert_ok!(<T as Config>::MultiCurrency::deposit(REWARD_1, &investor, reward_amount));
assert_ok!(<T as Config>::MultiCurrency::deposit(REWARD_2, &investor, reward_amount));

let caller: T::AccountId = whitelisted_caller();
assert_ok!(<T as Config>::MultiCurrency::deposit(FARMING_DEPOSIT_1, &caller, T::MinimumDepositOfUser::get()));
assert_ok!(<T as Config>::MultiCurrency::deposit(FARMING_DEPOSIT_2, &caller, T::MinimumDepositOfUser::get()));

assert_ok!(LM::<T>::create_pool(
(FARMING_DEPOSIT_1, FARMING_DEPOSIT_2),
(REWARD_1, reward_amount),
vec![(REWARD_2, reward_amount)].try_into().unwrap(),
PoolType::Farming,
duration,
min_deposit_to_start,
0u128.saturated_into()
));

assert_ok!(LM::<T>::charge(RawOrigin::Signed(investor).into(), 0));

assert_ok!(LM::<T>::deposit(RawOrigin::Signed(caller.clone()).into(), 0, T::MinimumDepositOfUser::get()));

// Run to block
run_to_block::<T>(1u128.saturated_into());

}: _(RawOrigin::Signed(caller.clone()), 0)
}

impl_benchmark_test_suite!(LM, crate::mock::new_test_ext(), crate::mock::Test);
Loading

0 comments on commit bb89fb6

Please sign in to comment.