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: implement orml benchmarks #190

Merged
merged 8 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions runtime/basilisk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ orml-nft = { git = "/~https://github.com/open-web3-stack/open-runtime-module-libra
orml-tokens = { git = "/~https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc96605342de9971c27699561723a6de6e2614c2", default-features = false }
orml-traits = { git = "/~https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc96605342de9971c27699561723a6de6e2614c2", default-features = false }
orml-vesting = { git = "/~https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc96605342de9971c27699561723a6de6e2614c2", default-features = false }
orml-benchmarking = { git = "/~https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc96605342de9971c27699561723a6de6e2614c2", default-features = false, optional = true }

# orml XCM support
orml-xtokens = { git = "/~https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc96605342de9971c27699561723a6de6e2614c2", default-features = false }
Expand Down Expand Up @@ -140,6 +141,7 @@ runtime-benchmarks = [
"pallet-scheduler/runtime-benchmarks",
"pallet-utility/runtime-benchmarks",
"pallet-tips/runtime-benchmarks",
"orml-benchmarking",
]
std = [
"codec/std",
Expand All @@ -156,6 +158,7 @@ std = [
"orml-xtokens/std",
"orml-xcm-support/std",
"orml-unknown-tokens/std",
"orml-benchmarking/std",
"cumulus-pallet-parachain-system/std",
"cumulus-pallet-aura-ext/std",
"cumulus-pallet-xcm/std",
Expand Down
136 changes: 136 additions & 0 deletions runtime/basilisk/src/benchmarking/currencies.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use crate::{AccountId, Amount, AssetId, Balance, Currencies, NativeAssetId, Runtime, NATIVE_EXISTENTIAL_DEPOSIT};

use sp_std::prelude::*;

use frame_benchmarking::{account, whitelisted_caller};
use frame_system::RawOrigin;
use sp_runtime::traits::UniqueSaturatedInto;

use frame_support::assert_ok;

use orml_benchmarking::runtime_benchmarks;
use orml_traits::MultiCurrency;
use orml_traits::MultiCurrencyExtended;

use super::*;

use sp_runtime::traits::{SaturatedConversion, StaticLookup};

const SEED: u32 = 0;

const NATIVE: AssetId = NativeAssetId::get();
const NON_NATIVE: AssetId = 1;
pub fn lookup_of_account(who: AccountId) -> <<Runtime as frame_system::Config>::Lookup as StaticLookup>::Source {
<Runtime as frame_system::Config>::Lookup::unlookup(who)
}

pub fn set_balance(currency_id: AssetId, who: &AccountId, balance: Balance) {
assert_ok!(<Currencies as MultiCurrencyExtended<_>>::update_balance(
currency_id,
who,
balance.saturated_into()
));
}

runtime_benchmarks! {
{ Runtime, orml_currencies }

// `transfer` non-native currency
transfer_non_native_currency {
let amount: Balance = 1_000 * BSX;
let from: AccountId = whitelisted_caller();
register_asset(b"TST".to_vec(), 1u128);
set_balance(NON_NATIVE, &from, amount);

let to: AccountId = account("to", 0, SEED);
let to_lookup = lookup_of_account(to.clone());
}: transfer(RawOrigin::Signed(from), to_lookup, NON_NATIVE, amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::total_balance(NON_NATIVE, &to), amount);
}

// `transfer` native currency and in worst case
#[extra]
transfer_native_currency_worst_case {
let existential_deposit = NATIVE_EXISTENTIAL_DEPOSIT;
let amount: Balance = existential_deposit.saturating_mul(1000);
let from: AccountId = whitelisted_caller();
set_balance(NATIVE, &from, amount);

let to: AccountId = account("to", 0, SEED);
let to_lookup = lookup_of_account(to.clone());
}: transfer(RawOrigin::Signed(from), to_lookup, NATIVE, amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::total_balance(NATIVE, &to), amount);
}

// `transfer_native_currency` in worst case
// * will create the `to` account.
// * will kill the `from` account.
transfer_native_currency {
let existential_deposit = NATIVE_EXISTENTIAL_DEPOSIT;
let amount: Balance = existential_deposit.saturating_mul(1000);
let from: AccountId = whitelisted_caller();
set_balance(NATIVE, &from, amount);

let to: AccountId = account("to", 0, SEED);
let to_lookup = lookup_of_account(to.clone());
}: _(RawOrigin::Signed(from), to_lookup, amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::total_balance(NATIVE, &to), amount);
}

// `update_balance` for non-native currency
update_balance_non_native_currency {
let balance: Balance = 2 * BSX;
let amount: Amount = balance.unique_saturated_into();
let who: AccountId = account("who", 0, SEED);
let who_lookup = lookup_of_account(who.clone());
register_asset(b"TST".to_vec(), 1u128);
}: update_balance(RawOrigin::Root, who_lookup, NON_NATIVE, amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::total_balance(NON_NATIVE, &who), balance);
}

// `update_balance` for native currency
// * will create the `who` account.
update_balance_native_currency_creating {
let existential_deposit = NATIVE_EXISTENTIAL_DEPOSIT;
let balance: Balance = existential_deposit.saturating_mul(1000);
let amount: Amount = balance.unique_saturated_into();
let who: AccountId = account("who", 0, SEED);
let who_lookup = lookup_of_account(who.clone());
}: update_balance(RawOrigin::Root, who_lookup, NATIVE, amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::total_balance(NATIVE, &who), balance);
}

// `update_balance` for native currency
// * will kill the `who` account.
update_balance_native_currency_killing {
let existential_deposit = NATIVE_EXISTENTIAL_DEPOSIT;
let balance: Balance = existential_deposit.saturating_mul(1000);
let amount: Amount = balance.unique_saturated_into();
let who: AccountId = account("who", 0, SEED);
let who_lookup = lookup_of_account(who.clone());
set_balance(NATIVE, &who, balance);
}: update_balance(RawOrigin::Root, who_lookup, NATIVE, -amount)
verify {
assert_eq!(<Currencies as MultiCurrency<_>>::free_balance(NATIVE, &who), 0);
}
}

#[cfg(test)]
mod tests {
use super::*;
use orml_benchmarking::impl_benchmark_test_suite;

fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::default()
.build_storage::<crate::Runtime>()
.unwrap()
.into()
}

impl_benchmark_test_suite!(new_test_ext(),);
}
22 changes: 22 additions & 0 deletions runtime/basilisk/src/benchmarking/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![cfg(feature = "runtime-benchmarks")]

pub mod currencies;
pub mod tokens;
pub mod vesting;

use crate::AssetRegistry;

use frame_system::RawOrigin;
use primitives::{AssetId, Balance};
use sp_std::vec::Vec;

pub const BSX: Balance = primitives::constants::currency::UNITS;

pub fn register_asset(name: Vec<u8>, deposit: Balance) {
let _ = AssetRegistry::register(
RawOrigin::Root.into(),
name,
pallet_asset_registry::AssetType::<AssetId>::Token,
deposit,
);
}
116 changes: 116 additions & 0 deletions runtime/basilisk/src/benchmarking/tokens.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
use crate::{AccountId, AssetId, Balance, Runtime, Tokens};

use sp_std::prelude::*;

use frame_benchmarking::{account, whitelisted_caller};
use frame_system::RawOrigin;

use frame_support::assert_ok;
use orml_benchmarking::runtime_benchmarks;
use orml_traits::MultiCurrency;
use orml_traits::MultiCurrencyExtended;

use super::*;

use sp_runtime::traits::{SaturatedConversion, StaticLookup};

const SEED: u32 = 0;

const NON_NATIVE: AssetId = 1;

pub fn lookup_of_account(who: AccountId) -> <<Runtime as frame_system::Config>::Lookup as StaticLookup>::Source {
<Runtime as frame_system::Config>::Lookup::unlookup(who)
}

pub fn update_balance(currency_id: AssetId, who: &AccountId, balance: Balance) {
assert_ok!(<Tokens as MultiCurrencyExtended<_>>::update_balance(
currency_id,
who,
balance.saturated_into()
));
}

runtime_benchmarks! {
{ Runtime, orml_tokens }

transfer {
let amount: Balance = BSX;

register_asset(b"TST".to_vec(), 1u128);

let from: AccountId = whitelisted_caller();
update_balance(NON_NATIVE, &from, amount);

let to: AccountId = account("to", 0, SEED);
let to_lookup = lookup_of_account(to.clone());
}: _(RawOrigin::Signed(from), to_lookup, NON_NATIVE, amount)
verify {
assert_eq!(<Tokens as MultiCurrency<_>>::total_balance(NON_NATIVE, &to), amount);
}

transfer_all {
let amount: Balance = BSX;

register_asset(b"TST".to_vec(), 1u128);

let from: AccountId = whitelisted_caller();
update_balance(NON_NATIVE, &from, amount);

let to: AccountId = account("to", 0, SEED);
let to_lookup = lookup_of_account(to);
}: _(RawOrigin::Signed(from.clone()), to_lookup, NON_NATIVE, false)
verify {
assert_eq!(<Tokens as MultiCurrency<_>>::total_balance(NON_NATIVE, &from), 0);
}

transfer_keep_alive {
let from: AccountId = whitelisted_caller();
register_asset(b"TST".to_vec(), 1u128);
update_balance(NON_NATIVE, &from, 2 * BSX);

let to: AccountId = account("to", 0, SEED);
let to_lookup = lookup_of_account(to.clone());
}: _(RawOrigin::Signed(from), to_lookup, NON_NATIVE, BSX)
verify {
assert_eq!(<Tokens as MultiCurrency<_>>::total_balance(NON_NATIVE, &to), BSX);
}

force_transfer {
let from: AccountId = account("from", 0, SEED);
let from_lookup = lookup_of_account(from.clone());
register_asset(b"TST".to_vec(), 1u128);
update_balance(NON_NATIVE, &from, 2 * BSX);

let to: AccountId = account("to", 0, SEED);
let to_lookup = lookup_of_account(to.clone());
}: _(RawOrigin::Root, from_lookup, to_lookup, NON_NATIVE, BSX)
verify {
assert_eq!(<Tokens as MultiCurrency<_>>::total_balance(NON_NATIVE, &to), BSX);
}

set_balance {
let who: AccountId = account("who", 0, SEED);
let who_lookup = lookup_of_account(who.clone());

register_asset(b"TST".to_vec(), 1u128);

}: _(RawOrigin::Root, who_lookup, NON_NATIVE, BSX, BSX)
verify {
assert_eq!(<Tokens as MultiCurrency<_>>::total_balance(NON_NATIVE, &who), 2 * BSX);
}
}

#[cfg(test)]
mod tests {
use super::*;
use orml_benchmarking::impl_benchmark_test_suite;

fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::default()
.build_storage::<crate::Runtime>()
.unwrap()
.into()
}

impl_benchmark_test_suite!(new_test_ext(),);
}
Loading