Newer
Older
assert_ok!(Staking::unbond(Origin::signed(20), 1000 - ed));
start_active_era(3);
assert_ok!(Staking::withdraw_unbonded(Origin::signed(20), 100));
// initial stuff.
assert_eq!(
Staking::ledger(&20).unwrap(),
StakingLedger {
stash: 21,
total: ed,
active: ed,
unlocking: vec![],
claimed_rewards: vec![]
}
);
})
}
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
mod election_data_provider {
use super::*;
use sp_election_providers::ElectionDataProvider;
#[test]
fn voters_include_self_vote() {
ExtBuilder::default().nominate(false).build().execute_with(|| {
assert!(<Validators<Test>>::iter().map(|(x, _)| x).all(|v| Staking::voters()
.into_iter()
.find(|(w, _, t)| { v == *w && t[0] == *w })
.is_some()))
})
}
#[test]
fn voters_exclude_slashed() {
ExtBuilder::default().build().execute_with(|| {
assert_eq!(Staking::nominators(101).unwrap().targets, vec![11, 21]);
assert_eq!(
<Staking as ElectionDataProvider<AccountId, BlockNumber>>::voters()
.iter()
.find(|x| x.0 == 101)
.unwrap()
.2,
vec![11, 21]
);
start_active_era(1);
add_slash(&11);
// 11 is gone.
start_active_era(2);
assert_eq!(
<Staking as ElectionDataProvider<AccountId, BlockNumber>>::voters()
.iter()
.find(|x| x.0 == 101)
.unwrap()
.2,
vec![21]
);
// resubmit and it is back
assert_ok!(Staking::nominate(Origin::signed(100), vec![11, 21]));
assert_eq!(
<Staking as ElectionDataProvider<AccountId, BlockNumber>>::voters()
.iter()
.find(|x| x.0 == 101)
.unwrap()
.2,
vec![11, 21]
);
})
}
#[test]
fn estimate_next_election_works() {
ExtBuilder::default().session_per_era(5).period(5).build().execute_with(|| {
// first session is always length 0.
for b in 1..20 {
run_to_block(b);
assert_eq!(Staking::next_election_prediction(System::block_number()), 20);
}
// election
run_to_block(20);
assert_eq!(Staking::next_election_prediction(System::block_number()), 45);
assert_eq!(staking_events().len(), 1);
assert_eq!(
*staking_events().last().unwrap(),
RawEvent::StakingElection(ElectionCompute::OnChain)
);
for b in 21..45 {
run_to_block(b);
assert_eq!(Staking::next_election_prediction(System::block_number()), 45);
}
// election
run_to_block(45);
assert_eq!(Staking::next_election_prediction(System::block_number()), 70);
assert_eq!(staking_events().len(), 3);
assert_eq!(
*staking_events().last().unwrap(),
RawEvent::StakingElection(ElectionCompute::OnChain)
);
})
}
}