Skip to content

Commit

Permalink
mesh-1255: copy-in staking/src
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Sep 23, 2020
1 parent d0aaf31 commit c8d38b8
Show file tree
Hide file tree
Showing 8 changed files with 297 additions and 151 deletions.
1 change: 1 addition & 0 deletions pallets/runtime/develop/src/runtime.rs
Expand Up @@ -396,6 +396,7 @@ impl pallet_staking::Trait for Runtime {
type MinSolutionScoreBump = MinSolutionScoreBump;
type MaxNominatorRewardedPerValidator = MaxNominatorRewardedPerValidator;
type UnsignedPriority = StakingUnsignedPriority;
type WeightInfo = ();
type RequiredAddOrigin = frame_system::EnsureRoot<AccountId>;
type RequiredRemoveOrigin = frame_system::EnsureRoot<AccountId>;
type RequiredComplianceOrigin = frame_system::EnsureRoot<AccountId>;
Expand Down
1 change: 1 addition & 0 deletions pallets/runtime/testnet/src/runtime.rs
Expand Up @@ -400,6 +400,7 @@ impl pallet_staking::Trait for Runtime {
type RequiredComplianceOrigin = frame_system::EnsureRoot<AccountId>;
type RequiredCommissionOrigin = frame_system::EnsureRoot<AccountId>;
type RequiredChangeHistoryDepthOrigin = frame_system::EnsureRoot<AccountId>;
type WeightInfo = ();
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions pallets/runtime/tests/src/staking/mock.rs
Expand Up @@ -597,6 +597,7 @@ impl Trait for Test {
type RequiredComplianceOrigin = frame_system::EnsureRoot<AccountId>;
type RequiredCommissionOrigin = frame_system::EnsureRoot<AccountId>;
type RequiredChangeHistoryDepthOrigin = frame_system::EnsureRoot<AccountId>;
type WeightInfo = ();
}

impl<LocalCall> frame_system::offchain::SendTransactionTypes<LocalCall> for Test
Expand Down
30 changes: 15 additions & 15 deletions pallets/runtime/tests/src/staking/mod.rs
Expand Up @@ -3697,7 +3697,7 @@ mod offchain_phragmen {
current_era(),
ElectionSize::default(),
),
Error::<Test>::PhragmenEarlySubmission,
Error::<Test>::OffchainElectionEarlySubmission,
Some(<Test as frame_system::Trait>::DbWeight::get().reads(1)),
);
})
Expand All @@ -3723,7 +3723,7 @@ mod offchain_phragmen {
let (compact, winners, score) = horrible_phragmen_with_post_processing(false);
assert_err_with_weight!(
submit_solution(Origin::signed(10), winners.clone(), compact.clone(), score,),
Error::<Test>::PhragmenWeakSubmission,
Error::<Test>::OffchainElectionWeakSubmission,
Some(<Test as frame_system::Trait>::DbWeight::get().reads(3))
);
})
Expand Down Expand Up @@ -3894,7 +3894,7 @@ mod offchain_phragmen {

assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusWinnerCount,
Error::<Test>::OffchainElectionBogusWinnerCount,
);
})
}
Expand All @@ -3918,7 +3918,7 @@ mod offchain_phragmen {
current_era(),
ElectionSize::default(),
),
Error::<Test>::PhragmenBogusElectionSize,
Error::<Test>::OffchainElectionBogusElectionSize,
);
})
}
Expand All @@ -3943,7 +3943,7 @@ mod offchain_phragmen {

assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusWinnerCount,
Error::<Test>::OffchainElectionBogusWinnerCount,
);
})
}
Expand Down Expand Up @@ -3991,7 +3991,7 @@ mod offchain_phragmen {
// The error type sadly cannot be more specific now.
assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusCompact,
Error::<Test>::OffchainElectionBogusCompact,
);
})
}
Expand All @@ -4018,7 +4018,7 @@ mod offchain_phragmen {
// The error type sadly cannot be more specific now.
assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusCompact,
Error::<Test>::OffchainElectionBogusCompact,
);
})
}
Expand All @@ -4044,7 +4044,7 @@ mod offchain_phragmen {

assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusWinner,
Error::<Test>::OffchainElectionBogusWinner,
);
})
}
Expand Down Expand Up @@ -4074,7 +4074,7 @@ mod offchain_phragmen {

assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusEdge,
Error::<Test>::OffchainElectionBogusEdge,
);
})
}
Expand Down Expand Up @@ -4104,7 +4104,7 @@ mod offchain_phragmen {

assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusSelfVote,
Error::<Test>::OffchainElectionBogusSelfVote,
);
})
}
Expand Down Expand Up @@ -4134,7 +4134,7 @@ mod offchain_phragmen {
// This raises score issue.
assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusSelfVote,
Error::<Test>::OffchainElectionBogusSelfVote,
);
})
}
Expand Down Expand Up @@ -4163,7 +4163,7 @@ mod offchain_phragmen {

assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusCompact,
Error::<Test>::OffchainElectionBogusCompact,
);
})
}
Expand Down Expand Up @@ -4199,7 +4199,7 @@ mod offchain_phragmen {

assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusNomination,
Error::<Test>::OffchainElectionBogusNomination,
);
})
}
Expand Down Expand Up @@ -4264,7 +4264,7 @@ mod offchain_phragmen {
// is rejected.
assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenSlashedNomination,
Error::<Test>::OffchainElectionSlashedNomination,
);
})
}
Expand All @@ -4286,7 +4286,7 @@ mod offchain_phragmen {

assert_noop!(
submit_solution(Origin::signed(10), winners, compact, score,),
Error::<Test>::PhragmenBogusScore,
Error::<Test>::OffchainElectionBogusScore,
);
})
}
Expand Down
56 changes: 28 additions & 28 deletions pallets/staking/src/benchmarking.rs
Expand Up @@ -21,7 +21,7 @@ use super::*;
use crate::Module as Staking;
use testing_utils::*;

pub use frame_benchmarking::{account, benchmarks};
pub use frame_benchmarking::{account, benchmarks, whitelisted_caller};
use frame_system::RawOrigin;
use sp_runtime::traits::One;
const SEED: u32 = 0;
Expand Down Expand Up @@ -57,16 +57,11 @@ pub fn create_validator_with_nominators<T: Trait>(
let mut points_total = 0;
let mut points_individual = Vec::new();

MinimumValidatorCount::put(0);

let (v_stash, v_controller) = create_stash_controller::<T>(0, 100)?;
let validator_prefs = ValidatorPrefs {
commission: Perbill::from_percent(50),
};
Staking::<T>::validate(
RawOrigin::Signed(v_controller.clone()).into(),
validator_prefs,
)?;
Staking::<T>::validate(RawOrigin::Signed(v_controller).into(), validator_prefs)?;
let stash_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(v_stash.clone());

points_total += 10;
Expand Down Expand Up @@ -104,7 +99,7 @@ pub fn create_validator_with_nominators<T: Trait>(
ErasRewardPoints::<T>::insert(current_era, reward);

// Create reward pool
let total_payout = get_minimum_balance::<T>() * 1000.into();
let total_payout = T::Currency::minimum_balance() * 1000.into();
<ErasValidatorReward<T>>::insert(current_era, total_payout);

Ok(v_stash)
Expand All @@ -122,7 +117,7 @@ benchmarks! {
let controller = create_funded_user::<T>("controller", u, 100);
let controller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(controller.clone());
let reward_destination = RewardDestination::Staked;
let amount = get_minimum_balance::<T>() * 10.into();
let amount = T::Currency::minimum_balance() * 10.into();
}: _(RawOrigin::Signed(stash.clone()), controller_lookup, amount, reward_destination)
verify {
assert!(Bonded::<T>::contains_key(stash));
Expand All @@ -132,7 +127,7 @@ benchmarks! {
bond_extra {
let u in ...;
let (stash, controller) = create_stash_controller::<T>(u, 100)?;
let max_additional = get_minimum_balance::<T>() * 10.into();
let max_additional = T::Currency::minimum_balance() * 10.into();
let ledger = Ledger::<T>::get(&controller).ok_or("ledger not created before")?;
let original_bonded: BalanceOf<T> = ledger.active;
}: _(RawOrigin::Signed(stash), max_additional)
Expand All @@ -145,7 +140,7 @@ benchmarks! {
unbond {
let u in ...;
let (_, controller) = create_stash_controller::<T>(u, 100)?;
let amount = get_minimum_balance::<T>() * 10.into();
let amount = T::Currency::minimum_balance() * 10.into();
let ledger = Ledger::<T>::get(&controller).ok_or("ledger not created before")?;
let original_bonded: BalanceOf<T> = ledger.active;
}: _(RawOrigin::Signed(controller.clone()), amount)
Expand All @@ -161,7 +156,7 @@ benchmarks! {
let s in 0 .. MAX_SPANS;
let (stash, controller) = create_stash_controller::<T>(0, 100)?;
add_slashing_spans::<T>(&stash, s);
let amount = get_minimum_balance::<T>() * 5.into(); // Half of total
let amount = T::Currency::minimum_balance() * 5.into(); // Half of total
Staking::<T>::unbond(RawOrigin::Signed(controller.clone()).into(), amount)?;
CurrentEra::put(EraIndex::max_value());
let ledger = Ledger::<T>::get(&controller).ok_or("ledger not created before")?;
Expand All @@ -179,7 +174,7 @@ benchmarks! {
let s in 0 .. MAX_SPANS;
let (stash, controller) = create_stash_controller::<T>(0, 100)?;
add_slashing_spans::<T>(&stash, s);
let amount = get_minimum_balance::<T>() * 10.into();
let amount = T::Currency::minimum_balance() * 10.into();
Staking::<T>::unbond(RawOrigin::Signed(controller.clone()).into(), amount)?;
CurrentEra::put(EraIndex::max_value());
let ledger = Ledger::<T>::get(&controller).ok_or("ledger not created before")?;
Expand Down Expand Up @@ -290,7 +285,7 @@ benchmarks! {
let validator = create_validator_with_nominators::<T>(n, T::MaxNominatorRewardedPerValidator::get() as u32, true)?;

let current_era = CurrentEra::get().unwrap();
let caller = account("caller", 0, SEED);
let caller = whitelisted_caller();
let balance_before = T::Currency::free_balance(&validator);
}: _(RawOrigin::Signed(caller), validator.clone(), current_era)
verify {
Expand All @@ -304,7 +299,7 @@ benchmarks! {
let validator = create_validator_with_nominators::<T>(n, T::MaxNominatorRewardedPerValidator::get() as u32, false)?;

let current_era = CurrentEra::get().unwrap();
let caller = account("caller", 0, SEED);
let caller = whitelisted_caller();
let balance_before = T::Currency::free_balance(&validator);
}: payout_stakers(RawOrigin::Signed(caller), validator.clone(), current_era)
verify {
Expand Down Expand Up @@ -364,7 +359,7 @@ benchmarks! {
new_era {
let v in 1 .. 10;
let n in 1 .. 100;
MinimumValidatorCount::put(0);

create_validators_with_nominators_for_era::<T>(v, n, MAX_NOMINATIONS, false, None)?;
let session_index = SessionIndex::one();
}: {
Expand All @@ -383,8 +378,8 @@ benchmarks! {
for _ in 0 .. l {
staking_ledger.unlocking.push(unlock_chunk.clone())
}
Ledger::<T>::insert(controller.clone(), staking_ledger.clone());
let slash_amount = get_minimum_balance::<T>() * 10.into();
Ledger::<T>::insert(controller, staking_ledger);
let slash_amount = T::Currency::minimum_balance() * 10.into();
let balance_before = T::Currency::free_balance(&stash);
}: {
crate::slashing::do_slash::<T>(
Expand All @@ -401,7 +396,6 @@ benchmarks! {
payout_all {
let v in 1 .. 10;
let n in 1 .. 100;
MinimumValidatorCount::put(0);
create_validators_with_nominators_for_era::<T>(v, n, MAX_NOMINATIONS, false, None)?;
// Start a new Era
let new_validators = Staking::<T>::new_era(SessionIndex::one()).unwrap();
Expand All @@ -427,10 +421,10 @@ benchmarks! {
ErasRewardPoints::<T>::insert(current_era, reward);

// Create reward pool
let total_payout = get_minimum_balance::<T>() * 1000.into();
let total_payout = T::Currency::minimum_balance() * 1000.into();
<ErasValidatorReward<T>>::insert(current_era, total_payout);

let caller: T::AccountId = account("caller", 0, SEED);
let caller: T::AccountId = whitelisted_caller();
}: {
for arg in payout_calls_arg {
<Staking<T>>::payout_stakers(RawOrigin::Signed(caller.clone()).into(), arg.0, arg.1)?;
Expand Down Expand Up @@ -482,6 +476,10 @@ benchmarks! {

let era = <Staking<T>>::current_era().unwrap_or(0);
let caller: T::AccountId = account("caller", n, SEED);

// Whitelist caller account from further DB operations.
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());
}: {
let result = <Staking<T>>::submit_election_solution(
RawOrigin::Signed(caller.clone()).into(),
Expand Down Expand Up @@ -543,6 +541,10 @@ benchmarks! {
let era = <Staking<T>>::current_era().unwrap_or(0);
let caller: T::AccountId = account("caller", n, SEED);

// Whitelist caller account from further DB operations.
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());

// submit a very bad solution on-chain
{
// this is needed to fool the chain to accept this solution.
Expand Down Expand Up @@ -585,7 +587,6 @@ benchmarks! {
// number of nominator intent
let n in 1000 .. 2000;

MinimumValidatorCount::put(0);
create_validators_with_nominators_for_era::<T>(v, n, MAX_NOMINATIONS, false, None)?;

// needed for the solution to be generates.
Expand All @@ -596,6 +597,10 @@ benchmarks! {
let caller: T::AccountId = account("caller", n, SEED);
let era = <Staking<T>>::current_era().unwrap_or(0);

// Whitelist caller account from further DB operations.
let caller_key = frame_system::Account::<T>::hashed_key_for(&caller);
frame_benchmarking::benchmarking::add_to_whitelist(caller_key.into());

// submit a seq-phragmen with all the good stuff on chain.
{
let (winners, compact, score, size) = get_seq_phragmen_solution::<T>(true);
Expand Down Expand Up @@ -637,7 +642,6 @@ mod tests {
use frame_support::assert_ok;

#[test]
#[ignore = "Will fix when benchmarks get fixed"]
fn create_validators_with_nominators_for_era_works() {
ExtBuilder::default()
.has_stakers(false)
Expand All @@ -664,7 +668,6 @@ mod tests {
}

#[test]
#[ignore = "Will fix when benchmarks get fixed"]
fn create_validator_with_nominators_works() {
ExtBuilder::default()
.has_stakers(false)
Expand Down Expand Up @@ -694,7 +697,6 @@ mod tests {
}

#[test]
#[ignore = "Will fix when benchmarks get fixed"]
fn add_slashing_spans_works() {
ExtBuilder::default()
.has_stakers(false)
Expand Down Expand Up @@ -732,7 +734,6 @@ mod tests {
}

#[test]
#[ignore = "Will fix when benchmarks get fixed"]
fn test_payout_all() {
ExtBuilder::default()
.has_stakers(false)
Expand All @@ -758,7 +759,6 @@ mod tests {
}

#[test]
#[ignore = "Will fix when benchmarks get fixed"]
fn test_benchmarks() {
ExtBuilder::default()
.has_stakers(false)
Expand Down Expand Up @@ -795,7 +795,7 @@ mod tests {
}

#[test]
#[ignore = "Will fix when benchmarks get fixed"]
#[ignore]
fn test_benchmarks_offchain() {
ExtBuilder::default()
.has_stakers(false)
Expand Down

0 comments on commit c8d38b8

Please sign in to comment.