Skip to content

Commit

Permalink
fix benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ank4n committed Mar 31, 2024
1 parent 58fdc71 commit 8909755
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
63 changes: 30 additions & 33 deletions substrate/frame/nomination-pools/benchmarking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ use frame_support::{
};
use frame_system::RawOrigin as RuntimeOrigin;
use pallet_nomination_pools::{
BalanceOf, BondExtra, BondedPoolInner, BondedPools, ClaimPermission, ClaimPermissions,
Commission, CommissionChangeRate, CommissionClaimPermission, ConfigOp, GlobalMaxCommission,
MaxPoolMembers, MaxPoolMembersPerPool, MaxPools, Metadata, MinCreateBond, MinJoinBond,
Pallet as Pools, PoolMembers, PoolRoles, PoolState, RewardPools, SubPoolsStorage,
adapter::StakeStrategy, BalanceOf, BondExtra, BondedPoolInner, BondedPools, ClaimPermission,
ClaimPermissions, Commission, CommissionChangeRate, CommissionClaimPermission, ConfigOp,
GlobalMaxCommission, MaxPoolMembers, MaxPoolMembersPerPool, MaxPools, Metadata, MinCreateBond,
MinJoinBond, Pallet as Pools, PoolMembers, PoolRoles, PoolState, RewardPools, SubPoolsStorage,
};
use pallet_staking::MaxNominationsOf;
use sp_runtime::{
traits::{Bounded, StaticLookup, Zero},
Perbill,
};
use sp_staking::{EraIndex, StakingInterface};
use sp_staking::EraIndex;
use sp_std::{vec, vec::Vec};
// `frame_benchmarking::benchmarks!` macro needs this
use pallet_nomination_pools::Call;
Expand Down Expand Up @@ -157,19 +157,16 @@ impl<T: Config> ListScenario<T> {
let (pool_creator1, pool_origin1) =
create_pool_account::<T>(USER_SEED + 1, origin_weight, Some(Perbill::from_percent(50)));

T::Staking::nominate(
&pool_origin1,
T::StakeAdapter::nominate(
1,
// NOTE: these don't really need to be validators.
vec![account("random_validator", 0, USER_SEED)],
)?;

let (_, pool_origin2) =
create_pool_account::<T>(USER_SEED + 2, origin_weight, Some(Perbill::from_percent(50)));

T::Staking::nominate(
&pool_origin2,
vec![account("random_validator", 0, USER_SEED)].clone(),
)?;
T::StakeAdapter::nominate(2, vec![account("random_validator", 0, USER_SEED)].clone())?;

// Find a destination weight that will trigger the worst case scenario
let dest_weight_as_vote = <T as pallet_staking::Config>::VoterList::score_update_worst_case(
Expand All @@ -184,7 +181,7 @@ impl<T: Config> ListScenario<T> {
let (_, pool_dest1) =
create_pool_account::<T>(USER_SEED + 3, dest_weight, Some(Perbill::from_percent(50)));

T::Staking::nominate(&pool_dest1, vec![account("random_validator", 0, USER_SEED)])?;
T::StakeAdapter::nominate(3, vec![account("random_validator", 0, USER_SEED)])?;

let weight_of = pallet_staking::Pallet::<T>::weight_of_fn();
assert_eq!(vote_to_balance::<T>(weight_of(&pool_origin1)).unwrap(), origin_weight);
Expand All @@ -210,11 +207,11 @@ impl<T: Config> ListScenario<T> {
self.origin1_member = Some(joiner.clone());
CurrencyOf::<T>::set_balance(&joiner, amount * 2u32.into());

let original_bonded = T::Staking::active_stake(&self.origin1).unwrap();
let original_bonded = T::StakeAdapter::active_stake(1);

// Unbond `amount` from the underlying pool account so when the member joins
// we will maintain `current_bonded`.
T::Staking::unbond(&self.origin1, amount).expect("the pool was created in `Self::new`.");
T::StakeAdapter::unbond(1, amount).expect("the pool was created in `Self::new`.");

// Account pool points for the unbonded balance.
BondedPools::<T>::mutate(&1, |maybe_pool| {
Expand Down Expand Up @@ -243,7 +240,7 @@ frame_benchmarking::benchmarks! {
// setup the worst case list scenario.
let scenario = ListScenario::<T>::new(origin_weight, true)?;
assert_eq!(
T::Staking::active_stake(&scenario.origin1).unwrap(),
T::StakeAdapter::active_stake(1),
origin_weight
);

Expand All @@ -258,7 +255,7 @@ frame_benchmarking::benchmarks! {
verify {
assert_eq!(CurrencyOf::<T>::balance(&joiner), joiner_free - max_additional);
assert_eq!(
T::Staking::active_stake(&scenario.origin1).unwrap(),
T::StakeAdapter::active_stake(1),
scenario.dest_weight
);
}
Expand All @@ -273,7 +270,7 @@ frame_benchmarking::benchmarks! {
}: bond_extra(RuntimeOrigin::Signed(scenario.creator1.clone()), BondExtra::FreeBalance(extra))
verify {
assert!(
T::Staking::active_stake(&scenario.origin1).unwrap() >=
T::StakeAdapter::active_stake(1) >=
scenario.dest_weight
);
}
Expand All @@ -297,7 +294,7 @@ frame_benchmarking::benchmarks! {
verify {
// commission of 50% deducted here.
assert!(
T::Staking::active_stake(&scenario.origin1).unwrap() >=
T::StakeAdapter::active_stake(1) >=
scenario.dest_weight / 2u32.into()
);
}
Expand Down Expand Up @@ -351,7 +348,7 @@ frame_benchmarking::benchmarks! {
whitelist_account!(member_id);
}: _(RuntimeOrigin::Signed(member_id.clone()), member_id_lookup, all_points)
verify {
let bonded_after = T::Staking::active_stake(&scenario.origin1).unwrap();
let bonded_after = T::StakeAdapter::active_stake(1);
// We at least went down to the destination bag
assert!(bonded_after <= scenario.dest_weight);
let member = PoolMembers::<T>::get(
Expand All @@ -360,7 +357,7 @@ frame_benchmarking::benchmarks! {
.unwrap();
assert_eq!(
member.unbonding_eras.keys().cloned().collect::<Vec<_>>(),
vec![0 + T::Staking::bonding_duration()]
vec![0 + T::StakeAdapter::bonding_duration()]
);
assert_eq!(
member.unbonding_eras.values().cloned().collect::<Vec<_>>(),
Expand All @@ -382,7 +379,7 @@ frame_benchmarking::benchmarks! {

// Sanity check join worked
assert_eq!(
T::Staking::active_stake(&pool_account).unwrap(),
T::StakeAdapter::active_stake(1),
min_create_bond + min_join_bond
);
assert_eq!(CurrencyOf::<T>::balance(&joiner), min_join_bond);
Expand All @@ -392,7 +389,7 @@ frame_benchmarking::benchmarks! {

// Sanity check that unbond worked
assert_eq!(
T::Staking::active_stake(&pool_account).unwrap(),
T::StakeAdapter::active_stake(1),
min_create_bond
);
assert_eq!(pallet_staking::Ledger::<T>::get(&pool_account).unwrap().unlocking.len(), 1);
Expand Down Expand Up @@ -425,7 +422,7 @@ frame_benchmarking::benchmarks! {

// Sanity check join worked
assert_eq!(
T::Staking::active_stake(&pool_account).unwrap(),
T::StakeAdapter::active_stake(1),
min_create_bond + min_join_bond
);
assert_eq!(CurrencyOf::<T>::balance(&joiner), min_join_bond);
Expand All @@ -436,7 +433,7 @@ frame_benchmarking::benchmarks! {

// Sanity check that unbond worked
assert_eq!(
T::Staking::active_stake(&pool_account).unwrap(),
T::StakeAdapter::active_stake(1),
min_create_bond
);
assert_eq!(pallet_staking::Ledger::<T>::get(&pool_account).unwrap().unlocking.len(), 1);
Expand Down Expand Up @@ -482,7 +479,7 @@ frame_benchmarking::benchmarks! {

// Sanity check that unbond worked
assert_eq!(
T::Staking::active_stake(&pool_account).unwrap(),
T::StakeAdapter::active_stake(1),
Zero::zero()
);
assert_eq!(
Expand Down Expand Up @@ -562,8 +559,8 @@ frame_benchmarking::benchmarks! {
}
);
assert_eq!(
T::Staking::active_stake(&Pools::<T>::create_bonded_account(1)),
Ok(min_create_bond)
T::StakeAdapter::active_stake(1),
min_create_bond
);
}

Expand Down Expand Up @@ -602,8 +599,8 @@ frame_benchmarking::benchmarks! {
}
);
assert_eq!(
T::Staking::active_stake(&Pools::<T>::create_bonded_account(1)),
Ok(min_create_bond)
T::StakeAdapter::active_stake(1),
min_create_bond
);
}

Expand Down Expand Up @@ -687,13 +684,13 @@ frame_benchmarking::benchmarks! {
.map(|i| account("stash", USER_SEED, i))
.collect();

assert_ok!(T::Staking::nominate(&pool_account, validators));
assert!(T::Staking::nominations(&Pools::<T>::create_bonded_account(1)).is_some());
assert_ok!(T::StakeAdapter::nominate(1, validators));
assert!(T::StakeAdapter::nominations(1).is_some());

whitelist_account!(depositor);
}:_(RuntimeOrigin::Signed(depositor.clone()), 1)
verify {
assert!(T::Staking::nominations(&Pools::<T>::create_bonded_account(1)).is_none());
assert!(T::StakeAdapter::nominations(1).is_none());
}

set_commission {
Expand Down Expand Up @@ -792,7 +789,7 @@ frame_benchmarking::benchmarks! {

// Sanity check join worked
assert_eq!(
T::Staking::active_stake(&pool_account).unwrap(),
T::StakeAdapter::active_stake(1),
min_create_bond + min_join_bond
);
}:_(RuntimeOrigin::Signed(joiner.clone()), ClaimPermission::PermissionlessAll)
Expand Down
15 changes: 15 additions & 0 deletions substrate/frame/nomination-pools/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ pub trait StakeStrategy {
amount: Self::Balance,
maybe_reporter: Option<Self::AccountId>,
) -> DispatchResult;

#[cfg(feature = "runtime-benchmarks")]
fn nominations(_: PoolId) -> Option<Vec<Self::AccountId>>;
}

/// A staking strategy implementation that supports transfer based staking.
Expand Down Expand Up @@ -186,6 +189,12 @@ impl<T: Config, Staking: StakingInterface<Balance = BalanceOf<T>, AccountId = T:
) -> DispatchResult {
Err(Error::<T>::Defensive(DefensiveError::DelegationUnsupported).into())
}

#[cfg(feature = "runtime-benchmarks")]
fn nominations(pool: PoolId) -> Option<Vec<Self::AccountId>> {
let pool_account = Pallet::<T>::create_bonded_account(pool);
Staking::nominations(&pool_account)
}
}

/// A staking strategy implementation that supports delegation based staking.
Expand Down Expand Up @@ -298,4 +307,10 @@ impl<
let pool_account = Pallet::<T>::create_bonded_account(pool);
Staking::delegator_slash(&pool_account, who, amount, maybe_reporter)
}

#[cfg(feature = "runtime-benchmarks")]
fn nominations(pool: PoolId) -> Option<Vec<Self::AccountId>> {
let pool_account = Pallet::<T>::create_bonded_account(pool);
Staking::nominations(&pool_account)
}
}

0 comments on commit 8909755

Please sign in to comment.