Skip to content

Commit

Permalink
benchmark fixes for common
Browse files Browse the repository at this point in the history
  • Loading branch information
gianfra-t committed Aug 5, 2024
1 parent f7083ed commit 2a877c2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
10 changes: 5 additions & 5 deletions Cargo.lock

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

24 changes: 13 additions & 11 deletions pallets/parachain-staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

//! Benchmarking
use crate::{types::RoundInfo, *};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, Zero};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use sp_arithmetic::traits::Zero;
use frame_support::{
assert_ok,
traits::{Currency, Get, OnInitialize},
};
use frame_system::{Pallet as System, RawOrigin};
use frame_system::pallet_prelude::BlockNumberFor;
use pallet_session::Pallet as Session;
use sp_runtime::{
traits::{One, SaturatedConversion, StaticLookup},
Expand Down Expand Up @@ -100,7 +102,7 @@ fn fill_unstaking<T: Config>(
delegator: Option<&T::AccountId>,
unstaked: u64,
) where
u32: Into<<T as frame_system::Config>::BlockNumber>,
u32: Into<BlockNumberFor<T>>,
{
let who = delegator.unwrap_or(collator);
assert_eq!(<Unstaking<T>>::get(who).len(), 0);
Expand All @@ -117,18 +119,18 @@ fn fill_unstaking<T: Config>(
T::CurrencyBalance::one()
));
}
System::<T>::set_block_number(System::<T>::block_number() + T::BlockNumber::one());
System::<T>::set_block_number(System::<T>::block_number() + BlockNumberFor::<T>::one());
}
assert_eq!(<Unstaking<T>>::get(who).len() as u64, unstaked);
assert!(<Unstaking<T>>::get(who).len() <= T::MaxUnstakeRequests::get().try_into().unwrap());
}

benchmarks! {
where_clause { where u32: Into<<T as frame_system::Config>::BlockNumber> }
where_clause { where u32: Into<BlockNumberFor<T>> }

on_initialize_no_action {
assert_eq!(<Round<T>>::get().current, 0u32);
let block = T::BlockNumber::one();
let block = BlockNumberFor::<T>::one();
}: { Pallet::<T>::on_initialize(block) }
verify {
assert_eq!(<Round<T>>::get().current, 0u32);
Expand All @@ -145,7 +147,7 @@ benchmarks! {
on_initialize_network_rewards {
let issuance = T::Currency::total_issuance();
// if we only add by one, we also initialize a new year
let block = T::NetworkRewardStart::get() + T::BlockNumber::one() * 2_u32.into();
let block = T::NetworkRewardStart::get() + BlockNumberFor::<T>::one() * 2_u32.into();
}: { Pallet::<T>::on_initialize(block) }
verify {
let new_issuance = T::Currency::total_issuance();
Expand All @@ -169,7 +171,7 @@ benchmarks! {
assert_eq!(Session::<T>::current_index(), 0);

// jump to next block to trigger new round
let now = now + T::BlockNumber::one();
let now = now + BlockNumberFor::<T>::one();
System::<T>::set_block_number(now);
Session::<T>::on_initialize(now);
assert_eq!(Session::<T>::current_index(), 1);
Expand Down Expand Up @@ -221,7 +223,7 @@ benchmarks! {
}

set_blocks_per_round {
let bpr: T::BlockNumber = T::MinBlocksPerRound::get() + T::BlockNumber::one();
let bpr: BlockNumberFor<T> = T::MinBlocksPerRound::get() + BlockNumberFor::<T>::one();
}: _(RawOrigin::Root, bpr)
verify {
assert_eq!(<Round<T>>::get().length, bpr);
Expand Down Expand Up @@ -628,12 +630,12 @@ benchmarks! {
let collator = candidates[0].clone();

let old = InflationConfig::<T>::get();
assert_eq!(LastRewardReduction::<T>::get(), T::BlockNumber::zero());
System::<T>::set_block_number(T::BLOCKS_PER_YEAR + T::BlockNumber::one());
assert_eq!(LastRewardReduction::<T>::get(), BlockNumberFor::<T>::zero());
System::<T>::set_block_number(T::BLOCKS_PER_YEAR + BlockNumberFor::<T>::one());
}: _(RawOrigin::Signed(collator))
verify {
let new = InflationConfig::<T>::get();
assert_eq!(LastRewardReduction::<T>::get(), T::BlockNumber::one());
assert_eq!(LastRewardReduction::<T>::get(), BlockNumberFor::<T>::one());
assert_eq!(new.collator.max_rate, old.collator.max_rate);
assert_eq!(new.delegator.max_rate, old.delegator.max_rate);
assert!(new.collator.reward_rate.annual < old.collator.reward_rate.annual);
Expand Down
2 changes: 1 addition & 1 deletion pallets/treasury-buyout-extension/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use super::{Pallet as TreasuryBuyoutExtension, *};
use crate::types::{AccountIdOf, BalanceOf, CurrencyIdOf};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite, Vec};
use frame_benchmarking::{account, benchmarks, impl_benchmark_test_suite};
use frame_support::assert_ok;
use frame_system::RawOrigin;
use sp_std::prelude::*;
Expand Down
29 changes: 16 additions & 13 deletions runtime/common/src/benchmarking/orml_asset_registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::asset_registry::{CustomMetadata, DiaKeys};
use frame_benchmarking::v2::benchmarks;
use frame_support::assert_ok;
use frame_support::traits::ConstU32;
use frame_system::RawOrigin;
use orml_asset_registry::AssetMetadata;
use sp_runtime::BoundedVec;
Expand All @@ -21,28 +22,30 @@ pub trait Config:
pub mod benchmarks {
use super::{Config, Pallet, *};
use orml_asset_registry::Call;
use crate::asset_registry::StringLimit;

fn longest_vec() -> Vec<u8> {
fn longest_vec<T: sp_core::Get<u32>>() -> BoundedVec<u8, T> {
// there is no fixed upperbound, but all actions are root-only so an assumed upperbound of 128 will do
vec![b'a', 128]
let longest_vec = vec![b'a', 128];
BoundedVec::truncate_from(longest_vec)
}

fn longest_multilocation() -> MultiLocation {
let key = GeneralKey { length: 32, data: [0; 32] };
MultiLocation::new(1, X8(key, key, key, key, key, key, key, key))
}

fn get_asset_metadata() -> AssetMetadata<u128, CustomMetadata> {
AssetMetadata {
fn get_asset_metadata<T :sp_core::Get<u32>>() -> AssetMetadata<u128, CustomMetadata, T> {
AssetMetadata::<u128, CustomMetadata, T> {
decimals: 12,
name: longest_vec(),
symbol: longest_vec(),
name: longest_vec::<T>(),
symbol: longest_vec::<T>(),
existential_deposit: 0,
location: Some(longest_multilocation().into()),
additional: CustomMetadata {
dia_keys: DiaKeys {
blockchain: BoundedVec::truncate_from(longest_vec()),
symbol: BoundedVec::truncate_from(longest_vec()),
blockchain: longest_vec::<StringLimit>(),
symbol: longest_vec::<StringLimit>(),
},
fee_per_second: 123,
},
Expand All @@ -59,7 +62,7 @@ pub mod benchmarks {

#[benchmark]
fn update_asset() {
let metadata = get_asset_metadata();
let metadata = get_asset_metadata::<<T as orml_asset_registry::Config>::StringLimit>();

assert_ok!(orml_asset_registry::Pallet::<T>::register_asset(
RawOrigin::Root.into(),
Expand All @@ -76,14 +79,14 @@ pub mod benchmarks {
RawOrigin::Root,
CurrencyId::Native,
Some(123),
Some(vec![b'b', 128]),
Some(vec![b'b', 128]),
Some(BoundedVec::truncate_from(vec![b'b', 128])),
Some(BoundedVec::truncate_from(vec![b'b', 128])),
Some(1234),
Some(Some(location.into())),
Some(CustomMetadata {
dia_keys: DiaKeys {
blockchain: BoundedVec::truncate_from(longest_vec()),
symbol: BoundedVec::truncate_from(longest_vec()),
blockchain: longest_vec(),
symbol: longest_vec(),
},
fee_per_second: 123,
}),
Expand Down

0 comments on commit 2a877c2

Please sign in to comment.