Skip to content
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
1 change: 1 addition & 0 deletions asset-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ std = [
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"orml-tokens/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"polkadot-runtime-common/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
Expand Down
2 changes: 2 additions & 0 deletions asset-registry/src/mock/para.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ impl orml_tokens::Config for Runtime {
type MaxReserves = ();
type MaxLocks = ConstU32<50>;
type DustRemovalWhitelist = Nothing;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

#[derive(scale_info::TypeInfo, Encode, Decode, Clone, Eq, PartialEq, Debug, MaxEncodedLen, DecodeWithMemTracking)]
Expand Down
8 changes: 7 additions & 1 deletion currencies/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ orml-utilities = { path = "../utilities", version = "1.5.0", default-features =
pallet-balances = { workspace = true, features = ["std"] }
sp-core = { workspace = true, features = ["std"] }

orml_tokens = { package = "orml-tokens", path = "../tokens" }
orml-tokens = { path = "../tokens", features = ["std"] }

[features]
default = [ "std" ]
Expand All @@ -41,6 +41,12 @@ std = [
"sp-runtime/std",
"sp-std/std",
]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"orml-tokens/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
Expand Down
2 changes: 2 additions & 0 deletions currencies/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ impl orml_tokens::Config for Runtime {
type MaxReserves = ConstU32<100_000>;
type ReserveIdentifier = ReserveIdentifier;
type DustRemovalWhitelist = Nothing;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

pub const NATIVE_CURRENCY_ID: CurrencyId = 1;
Expand Down
19 changes: 17 additions & 2 deletions oracle/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,25 @@
pub use crate::*;

use frame_benchmarking::v2::{impl_test_function, instance_benchmarks, whitelisted_caller};

use frame_benchmarking::v2::*;
use frame_support::assert_ok;
use frame_system::{Pallet as System, RawOrigin};

/// Helper trait for benchmarking.
pub trait BenchmarkHelper<OracleKey, OracleValue, L: Get<u32>> {
/// Returns a list of `(oracle_key, oracle_value)` pairs to be used for
/// benchmarking.
///
/// NOTE: User should ensure to at least submit two values, otherwise the
/// benchmark linear analysis might fail.
fn get_currency_id_value_pairs() -> BoundedVec<(OracleKey, OracleValue), L>;
}

impl<OracleKey, OracleValue, L: Get<u32>> BenchmarkHelper<OracleKey, OracleValue, L> for () {
fn get_currency_id_value_pairs() -> BoundedVec<(OracleKey, OracleValue), L> {
BoundedVec::default()
}
}

#[instance_benchmarks]
mod benchmarks {
use super::*;
Expand Down
22 changes: 3 additions & 19 deletions oracle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,18 @@ use sp_std::{prelude::*, vec};
pub use crate::default_combine_data::DefaultCombineData;

#[cfg(feature = "runtime-benchmarks")]
pub mod benchmarking;
mod benchmarking;

mod default_combine_data;
mod mock;
mod tests;
mod weights;

#[cfg(feature = "runtime-benchmarks")]
pub use benchmarking::BenchmarkHelper;
pub use module::*;
pub use weights::WeightInfo;

#[cfg(feature = "runtime-benchmarks")]
/// Helper trait for benchmarking.
pub trait BenchmarkHelper<OracleKey, OracleValue, L: Get<u32>> {
/// Returns a list of `(oracle_key, oracle_value)` pairs to be used for
/// benchmarking.
///
/// NOTE: User should ensure to at least submit two values, otherwise the
/// benchmark linear analysis might fail.
fn get_currency_id_value_pairs() -> BoundedVec<(OracleKey, OracleValue), L>;
}

#[cfg(feature = "runtime-benchmarks")]
impl<OracleKey, OracleValue, L: Get<u32>> BenchmarkHelper<OracleKey, OracleValue, L> for () {
fn get_currency_id_value_pairs() -> BoundedVec<(OracleKey, OracleValue), L> {
BoundedVec::default()
}
}

#[frame_support::pallet]
pub mod module {
use super::*;
Expand Down
8 changes: 7 additions & 1 deletion payments/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ orml-traits = {path = "../traits", version = "1.5.0", default-features = false }
sp-core = { workspace = true }
sp-io = { workspace = true }

orml-tokens = { path = "../tokens" }
orml-tokens = { path = "../tokens", features = ["std"] }

[features]
default = [ 'std' ]
Expand All @@ -41,6 +41,12 @@ std = [
'sp-runtime/std',
'sp-std/std',
]
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"orml-tokens/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
"frame-support/try-runtime",
"frame-system/try-runtime",
Expand Down
2 changes: 2 additions & 0 deletions payments/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ impl orml_tokens::Config for Test {
type DustRemovalWhitelist = MockDustRemovalWhitelist;
type MaxReserves = ConstU32<2>;
type ReserveIdentifier = ReserveIdentifier;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

pub struct MockDisputeResolver;
Expand Down
4 changes: 4 additions & 0 deletions tokens/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ log = { workspace = true }
scale-info = { workspace = true }
serde = { workspace = true, optional = true }

frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
sp-arithmetic = { workspace = true }
Expand All @@ -31,6 +32,7 @@ sp-staking = { workspace = true, features = ["std"] }
[features]
default = [ "std" ]
std = [
"frame-benchmarking?/std",
"frame-support/std",
"frame-system/std",
"log/std",
Expand All @@ -43,8 +45,10 @@ std = [
"sp-std/std",
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"pallet-treasury/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
]
try-runtime = [
Expand Down
139 changes: 139 additions & 0 deletions tokens/src/benchmarking.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
pub use crate::*;

use frame_benchmarking::v2::*;
use frame_support::assert_ok;
use frame_system::RawOrigin;
use sp_runtime::traits::SaturatedConversion;

/// Helper trait for benchmarking.
pub trait BenchmarkHelper<CurrencyId, Balance> {
/// Returns a currency id and amount to be used in benchmarking.
fn get_currency_id_and_amount() -> Option<(CurrencyId, Balance)>;
}

impl<CurrencyId, Balance> BenchmarkHelper<CurrencyId, Balance> for () {
fn get_currency_id_and_amount() -> Option<(CurrencyId, Balance)> {
None
}
}

#[benchmarks]
mod benchmarks {
use super::*;

#[benchmark]
fn transfer() {
let from: T::AccountId = account("from", 0, 0);

let (currency_id, amount) = T::BenchmarkHelper::get_currency_id_and_amount().unwrap();

assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance(
currency_id,
&from,
amount.saturated_into()
));

let to: T::AccountId = account("to", 0, 0);
let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone());

#[extrinsic_call]
_(RawOrigin::Signed(from), to_lookup, currency_id, amount);

assert_eq!(Pallet::<T>::total_balance(currency_id, &to), amount);
}

#[benchmark]
fn transfer_all() {
let from: T::AccountId = account("from", 0, 0);

let (currency_id, amount) = T::BenchmarkHelper::get_currency_id_and_amount().unwrap();

assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance(
currency_id,
&from,
amount.saturated_into()
));

let to: T::AccountId = account("to", 0, 0);
let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone());

#[extrinsic_call]
_(RawOrigin::Signed(from.clone()), to_lookup, currency_id, false);

assert_eq!(
<Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &from),
0u32.into()
);
}

#[benchmark]
fn transfer_keep_alive() {
let from: T::AccountId = account("from", 0, 0);

let (currency_id, amount) = T::BenchmarkHelper::get_currency_id_and_amount().unwrap();

assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance(
currency_id,
&from,
amount.saturating_mul(2u32.into()).saturated_into()
));

let to: T::AccountId = account("to", 0, 0);
let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone());

#[extrinsic_call]
_(RawOrigin::Signed(from), to_lookup, currency_id, amount);

assert_eq!(
<Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &to),
amount
);
}

#[benchmark]
fn force_transfer() {
let from: T::AccountId = account("from", 0, 0);
let from_lookup = <T as frame_system::Config>::Lookup::unlookup(from.clone());

let (currency_id, amount) = T::BenchmarkHelper::get_currency_id_and_amount().unwrap();

assert_ok!(<Pallet::<T> as MultiCurrencyExtended<_>>::update_balance(
currency_id,
&from,
amount.saturated_into()
));

let to: T::AccountId = account("to", 0, 0);
let to_lookup = <T as frame_system::Config>::Lookup::unlookup(to.clone());

#[extrinsic_call]
_(RawOrigin::Root, from_lookup, to_lookup, currency_id, amount);

assert_eq!(
<Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &to),
amount
);
}

#[benchmark]
fn set_balance() {
let who: T::AccountId = account("who", 0, 0);
let who_lookup = <T as frame_system::Config>::Lookup::unlookup(who.clone());

let (currency_id, amount) = T::BenchmarkHelper::get_currency_id_and_amount().unwrap();

#[extrinsic_call]
_(RawOrigin::Root, who_lookup, currency_id, amount, amount);

assert_eq!(
<Pallet::<T> as MultiCurrency<_>>::total_balance(currency_id, &who),
amount.saturating_mul(2u32.into())
);
}

impl_benchmark_test_suite! {
Pallet,
crate::mock::ExtBuilder::default().build(),
crate::mock::Runtime,
}
}
8 changes: 8 additions & 0 deletions tokens/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ use orml_traits::{
MultiReservableCurrency, NamedMultiReservableCurrency,
};

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;
mod imbalances;
mod impls;
mod mock;
Expand All @@ -82,6 +84,8 @@ mod tests_multicurrency;

mod weights;

#[cfg(feature = "runtime-benchmarks")]
pub use benchmarking::BenchmarkHelper;
pub use impls::*;
pub use weights::WeightInfo;

Expand Down Expand Up @@ -226,6 +230,10 @@ pub mod module {
// The whitelist of accounts that will not be reaped even if its total
// is zero or below ED.
type DustRemovalWhitelist: Contains<Self::AccountId>;

/// The benchmarks need a way to provide currency id.
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper: BenchmarkHelper<Self::CurrencyId, Self::Balance>;
}

#[pallet::error]
Expand Down
11 changes: 11 additions & 0 deletions tokens/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,15 @@ where
type OnKilledTokenAccount = TrackKilledAccounts<T>;
}

#[cfg(feature = "runtime-benchmarks")]
pub struct MockBenchmarkHelper;
#[cfg(feature = "runtime-benchmarks")]
impl BenchmarkHelper<CurrencyId, Balance> for MockBenchmarkHelper {
fn get_currency_id_and_amount() -> Option<(CurrencyId, Balance)> {
Some((DOT, 1000))
}
}

impl Config for Runtime {
type Balance = Balance;
type Amount = i64;
Expand All @@ -386,6 +395,8 @@ impl Config for Runtime {
type MaxReserves = ConstU32<2>;
type ReserveIdentifier = ReserveIdentifier;
type DustRemovalWhitelist = MockDustRemovalWhitelist;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = MockBenchmarkHelper;
}
pub type TreasuryCurrencyAdapter = <Runtime as pallet_treasury::Config>::Currency;

Expand Down
1 change: 1 addition & 0 deletions xtokens/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ std = [
runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"orml-tokens/runtime-benchmarks",
"pallet-xcm/runtime-benchmarks",
"parachains-common/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
Expand Down
2 changes: 2 additions & 0 deletions xtokens/src/mock/para.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ impl orml_tokens::Config for Runtime {
type MaxReserves = ConstU32<50>;
type ReserveIdentifier = [u8; 8];
type DustRemovalWhitelist = Everything;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
Expand Down
2 changes: 2 additions & 0 deletions xtokens/src/mock/para_relative_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ impl orml_tokens::Config for Runtime {
type MaxReserves = ConstU32<50>;
type ReserveIdentifier = [u8; 8];
type DustRemovalWhitelist = Everything;
#[cfg(feature = "runtime-benchmarks")]
type BenchmarkHelper = ();
}

parameter_types! {
Expand Down
Loading