mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-11 13:01:07 +00:00
Improved election pallet testing (#12327)
* Improved election pallet testing * fmt * remove comment * more checks * fixes in logic * roll_to_signed * switch to roll_to_signed * Update frame/election-provider-multi-phase/src/mock.rs Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> * remove useless checks * remove warning * add checks to signed.rs * add some checks to unsigned.rs * fmt * use roll_to_signed and roll_to_unsigned * remove nonsense * remove even more nonsense * fix * fix * remove useless checks Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com> Co-authored-by: parity-processbot <>
This commit is contained in:
@@ -1842,9 +1842,9 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
mock::{
|
||||
multi_phase_events, raw_solution, roll_to, AccountId, ExtBuilder, MockWeightInfo,
|
||||
MockedWeightInfo, MultiPhase, Runtime, RuntimeOrigin, SignedMaxSubmissions, System,
|
||||
TargetIndex, Targets,
|
||||
multi_phase_events, raw_solution, roll_to, roll_to_signed, roll_to_unsigned, AccountId,
|
||||
ExtBuilder, MockWeightInfo, MockedWeightInfo, MultiPhase, Runtime, RuntimeOrigin,
|
||||
SignedMaxSubmissions, System, TargetIndex, Targets,
|
||||
},
|
||||
Phase,
|
||||
};
|
||||
@@ -1868,7 +1868,7 @@ mod tests {
|
||||
assert!(MultiPhase::snapshot().is_none());
|
||||
assert_eq!(MultiPhase::round(), 1);
|
||||
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Signed);
|
||||
assert_eq!(multi_phase_events(), vec![Event::SignedPhaseStarted { round: 1 }]);
|
||||
assert!(MultiPhase::snapshot().is_some());
|
||||
@@ -1879,7 +1879,7 @@ mod tests {
|
||||
assert!(MultiPhase::snapshot().is_some());
|
||||
assert_eq!(MultiPhase::round(), 1);
|
||||
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25)));
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
@@ -1912,11 +1912,29 @@ mod tests {
|
||||
roll_to(44);
|
||||
assert!(MultiPhase::current_phase().is_off());
|
||||
|
||||
roll_to(45);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
roll_to(55);
|
||||
assert!(MultiPhase::current_phase().is_unsigned_open_at(55));
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::UnsignedPhaseStarted { round: 1 },
|
||||
Event::ElectionFinalized {
|
||||
compute: ElectionCompute::Fallback,
|
||||
score: ElectionScore {
|
||||
minimal_stake: 0,
|
||||
sum_stake: 0,
|
||||
sum_stake_squared: 0
|
||||
}
|
||||
},
|
||||
Event::SignedPhaseStarted { round: 2 },
|
||||
Event::UnsignedPhaseStarted { round: 2 }
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1940,6 +1958,21 @@ mod tests {
|
||||
|
||||
assert!(MultiPhase::current_phase().is_off());
|
||||
assert!(MultiPhase::snapshot().is_none());
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::UnsignedPhaseStarted { round: 1 },
|
||||
Event::ElectionFinalized {
|
||||
compute: ElectionCompute::Fallback,
|
||||
score: ElectionScore {
|
||||
minimal_stake: 0,
|
||||
sum_stake: 0,
|
||||
sum_stake_squared: 0
|
||||
}
|
||||
}
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1952,7 +1985,7 @@ mod tests {
|
||||
roll_to(19);
|
||||
assert!(MultiPhase::current_phase().is_off());
|
||||
|
||||
roll_to(20);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
assert!(MultiPhase::snapshot().is_some());
|
||||
|
||||
@@ -1963,6 +1996,21 @@ mod tests {
|
||||
|
||||
assert!(MultiPhase::current_phase().is_off());
|
||||
assert!(MultiPhase::snapshot().is_none());
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::ElectionFinalized {
|
||||
compute: ElectionCompute::Fallback,
|
||||
score: ElectionScore {
|
||||
minimal_stake: 0,
|
||||
sum_stake: 0,
|
||||
sum_stake_squared: 0
|
||||
}
|
||||
}
|
||||
]
|
||||
)
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1985,6 +2033,14 @@ mod tests {
|
||||
assert_ok!(MultiPhase::elect());
|
||||
|
||||
assert!(MultiPhase::current_phase().is_off());
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![Event::ElectionFinalized {
|
||||
compute: ElectionCompute::Fallback,
|
||||
score: ElectionScore { minimal_stake: 0, sum_stake: 0, sum_stake_squared: 0 }
|
||||
}]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1993,16 +2049,13 @@ mod tests {
|
||||
// An early termination in the signed phase, with no queued solution.
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
// Signed phase started at block 15 and will end at 25.
|
||||
roll_to(14);
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Off);
|
||||
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert_eq!(multi_phase_events(), vec![Event::SignedPhaseStarted { round: 1 }]);
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Signed);
|
||||
assert_eq!(MultiPhase::round(), 1);
|
||||
|
||||
// An unexpected call to elect.
|
||||
roll_to(20);
|
||||
assert_ok!(MultiPhase::elect());
|
||||
|
||||
// We surely can't have any feasible solutions. This will cause an on-chain election.
|
||||
@@ -2031,10 +2084,8 @@ mod tests {
|
||||
// an early termination in the signed phase, with no queued solution.
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
// signed phase started at block 15 and will end at 25.
|
||||
roll_to(14);
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Off);
|
||||
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert_eq!(multi_phase_events(), vec![Event::SignedPhaseStarted { round: 1 }]);
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Signed);
|
||||
assert_eq!(MultiPhase::round(), 1);
|
||||
@@ -2052,7 +2103,6 @@ mod tests {
|
||||
}
|
||||
|
||||
// an unexpected call to elect.
|
||||
roll_to(20);
|
||||
assert_ok!(MultiPhase::elect());
|
||||
|
||||
// all storage items must be cleared.
|
||||
@@ -2062,16 +2112,38 @@ mod tests {
|
||||
assert!(MultiPhase::desired_targets().is_none());
|
||||
assert!(MultiPhase::queued_solution().is_none());
|
||||
assert!(MultiPhase::signed_submissions().is_empty());
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::Slashed { account: 99, value: 5 },
|
||||
Event::Slashed { account: 99, value: 5 },
|
||||
Event::Slashed { account: 99, value: 5 },
|
||||
Event::Slashed { account: 99, value: 5 },
|
||||
Event::Slashed { account: 99, value: 5 },
|
||||
Event::ElectionFinalized {
|
||||
compute: ElectionCompute::Fallback,
|
||||
score: ElectionScore {
|
||||
minimal_stake: 0,
|
||||
sum_stake: 0,
|
||||
sum_stake_squared: 0
|
||||
}
|
||||
}
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_events_with_compute_signed() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(14);
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Off);
|
||||
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
let solution = raw_solution();
|
||||
@@ -2106,7 +2178,7 @@ mod tests {
|
||||
#[test]
|
||||
fn check_events_with_compute_unsigned() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
// ensure we have snapshots in place.
|
||||
@@ -2125,7 +2197,6 @@ mod tests {
|
||||
));
|
||||
assert!(MultiPhase::queued_solution().is_some());
|
||||
|
||||
roll_to(30);
|
||||
assert_ok!(MultiPhase::elect());
|
||||
|
||||
assert_eq!(
|
||||
@@ -2153,7 +2224,7 @@ mod tests {
|
||||
#[test]
|
||||
fn fallback_strategy_works() {
|
||||
ExtBuilder::default().onchain_fallback(true).build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25)));
|
||||
|
||||
// Zilch solutions thus far, but we get a result.
|
||||
@@ -2166,11 +2237,27 @@ mod tests {
|
||||
(30, Support { total: 40, voters: vec![(2, 5), (4, 5), (30, 30)] }),
|
||||
(40, Support { total: 60, voters: vec![(2, 5), (3, 10), (4, 5), (40, 40)] })
|
||||
]
|
||||
)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::UnsignedPhaseStarted { round: 1 },
|
||||
Event::ElectionFinalized {
|
||||
compute: ElectionCompute::Fallback,
|
||||
score: ElectionScore {
|
||||
minimal_stake: 0,
|
||||
sum_stake: 0,
|
||||
sum_stake_squared: 0
|
||||
}
|
||||
}
|
||||
]
|
||||
);
|
||||
});
|
||||
|
||||
ExtBuilder::default().onchain_fallback(false).build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25)));
|
||||
|
||||
// Zilch solutions thus far.
|
||||
@@ -2178,13 +2265,22 @@ mod tests {
|
||||
assert_eq!(MultiPhase::elect().unwrap_err(), ElectionError::Fallback("NoFallback."));
|
||||
// phase is now emergency.
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Emergency);
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::UnsignedPhaseStarted { round: 1 },
|
||||
Event::ElectionFailed
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn governance_fallback_works() {
|
||||
ExtBuilder::default().onchain_fallback(false).build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25)));
|
||||
|
||||
// Zilch solutions thus far.
|
||||
@@ -2243,9 +2339,16 @@ mod tests {
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Off);
|
||||
|
||||
// On-chain backup works though.
|
||||
roll_to(29);
|
||||
let supports = MultiPhase::elect().unwrap();
|
||||
assert!(supports.len() > 0);
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![Event::ElectionFinalized {
|
||||
compute: ElectionCompute::Fallback,
|
||||
score: ElectionScore { minimal_stake: 0, sum_stake: 0, sum_stake_squared: 0 }
|
||||
}]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2269,6 +2372,8 @@ mod tests {
|
||||
let err = MultiPhase::elect().unwrap_err();
|
||||
assert_eq!(err, ElectionError::Fallback("NoFallback."));
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Emergency);
|
||||
|
||||
assert_eq!(multi_phase_events(), vec![Event::ElectionFailed]);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2282,7 +2387,7 @@ mod tests {
|
||||
crate::mock::MaxElectingVoters::set(2);
|
||||
|
||||
// Signed phase opens just fine.
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Signed);
|
||||
|
||||
assert_eq!(
|
||||
@@ -2295,7 +2400,7 @@ mod tests {
|
||||
#[test]
|
||||
fn untrusted_score_verification_is_respected() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Signed);
|
||||
|
||||
// set the solution balancing to get the desired score.
|
||||
|
||||
@@ -99,6 +99,17 @@ pub fn roll_to(n: BlockNumber) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn roll_to_unsigned() {
|
||||
while !matches!(MultiPhase::current_phase(), Phase::Unsigned(_)) {
|
||||
roll_to(System::block_number() + 1);
|
||||
}
|
||||
}
|
||||
pub fn roll_to_signed() {
|
||||
while !matches!(MultiPhase::current_phase(), Phase::Signed) {
|
||||
roll_to(System::block_number() + 1);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn roll_to_with_ocw(n: BlockNumber) {
|
||||
let now = System::block_number();
|
||||
for i in now + 1..=n {
|
||||
|
||||
@@ -528,10 +528,11 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
mock::{
|
||||
balances, raw_solution, roll_to, Balances, ExtBuilder, MockedWeightInfo, MultiPhase,
|
||||
Runtime, RuntimeOrigin, SignedMaxRefunds, SignedMaxSubmissions, SignedMaxWeight,
|
||||
balances, multi_phase_events, raw_solution, roll_to, roll_to_signed, Balances,
|
||||
ExtBuilder, MockedWeightInfo, MultiPhase, Runtime, RuntimeOrigin, SignedMaxRefunds,
|
||||
SignedMaxSubmissions, SignedMaxWeight,
|
||||
},
|
||||
Error, Perbill, Phase,
|
||||
Error, Event, Perbill, Phase,
|
||||
};
|
||||
use frame_support::{assert_noop, assert_ok, assert_storage_noop};
|
||||
|
||||
@@ -555,7 +556,7 @@ mod tests {
|
||||
#[test]
|
||||
fn should_pay_deposit() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
let solution = raw_solution();
|
||||
@@ -565,13 +566,21 @@ mod tests {
|
||||
|
||||
assert_eq!(balances(&99), (95, 5));
|
||||
assert_eq!(MultiPhase::signed_submissions().iter().next().unwrap().deposit, 5);
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false }
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn good_solution_is_rewarded() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
let solution = raw_solution();
|
||||
@@ -582,13 +591,22 @@ mod tests {
|
||||
|
||||
assert!(MultiPhase::finalize_signed_phase());
|
||||
assert_eq!(balances(&99), (100 + 7 + 8, 0));
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::Rewarded { account: 99, value: 7 }
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bad_solution_is_slashed() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
let mut solution = raw_solution();
|
||||
@@ -604,13 +622,22 @@ mod tests {
|
||||
assert!(!MultiPhase::finalize_signed_phase());
|
||||
// and the bond is gone.
|
||||
assert_eq!(balances(&99), (95, 0));
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::Slashed { account: 99, value: 5 }
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn suppressed_solution_gets_bond_back() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
let mut solution = raw_solution();
|
||||
@@ -633,13 +660,22 @@ mod tests {
|
||||
assert_eq!(balances(&99), (100 + 7 + 8, 0));
|
||||
// 999 gets everything back, including the call fee.
|
||||
assert_eq!(balances(&999), (100 + 8, 0));
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::Rewarded { account: 99, value: 7 }
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cannot_submit_worse_with_full_queue() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
for s in 0..SignedMaxSubmissions::get() {
|
||||
@@ -667,7 +703,7 @@ mod tests {
|
||||
#[test]
|
||||
fn call_fee_refund_is_limited_by_signed_max_refunds() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
assert_eq!(SignedMaxRefunds::get(), 1);
|
||||
assert!(SignedMaxSubmissions::get() > 2);
|
||||
@@ -683,7 +719,7 @@ mod tests {
|
||||
assert_eq!(balances(&account), (95, 5));
|
||||
}
|
||||
|
||||
assert!(MultiPhase::finalize_signed_phase());
|
||||
assert_ok!(MultiPhase::do_elect());
|
||||
|
||||
for s in 0..SignedMaxSubmissions::get() {
|
||||
let account = 99 + s as u64;
|
||||
@@ -699,6 +735,26 @@ mod tests {
|
||||
assert_eq!(balances(&account), (100, 0));
|
||||
}
|
||||
}
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::Rewarded { account: 99, value: 7 },
|
||||
Event::ElectionFinalized {
|
||||
compute: ElectionCompute::Signed,
|
||||
score: ElectionScore {
|
||||
minimal_stake: 40,
|
||||
sum_stake: 100,
|
||||
sum_stake_squared: 5200
|
||||
}
|
||||
}
|
||||
]
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -708,7 +764,7 @@ mod tests {
|
||||
.signed_max_submission(1)
|
||||
.better_signed_threshold(Perbill::from_percent(20))
|
||||
.build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
let mut solution = RawSolution {
|
||||
@@ -747,13 +803,27 @@ mod tests {
|
||||
};
|
||||
|
||||
assert_ok!(MultiPhase::submit(RuntimeOrigin::signed(99), Box::new(solution)));
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::SolutionStored {
|
||||
compute: ElectionCompute::Signed,
|
||||
prev_ejected: false
|
||||
},
|
||||
Event::SolutionStored {
|
||||
compute: ElectionCompute::Signed,
|
||||
prev_ejected: true
|
||||
}
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn weakest_is_removed_if_better_provided() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
for s in 0..SignedMaxSubmissions::get() {
|
||||
@@ -800,7 +870,7 @@ mod tests {
|
||||
#[test]
|
||||
fn replace_weakest_works() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
for s in 1..SignedMaxSubmissions::get() {
|
||||
@@ -847,7 +917,7 @@ mod tests {
|
||||
#[test]
|
||||
fn early_ejected_solution_gets_bond_back() {
|
||||
ExtBuilder::default().signed_deposit(2, 0, 0).build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
for s in 0..SignedMaxSubmissions::get() {
|
||||
@@ -878,7 +948,7 @@ mod tests {
|
||||
#[test]
|
||||
fn equally_good_solution_is_not_accepted() {
|
||||
ExtBuilder::default().signed_max_submission(3).build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
for i in 0..SignedMaxSubmissions::get() {
|
||||
@@ -915,7 +985,7 @@ mod tests {
|
||||
// - bad_solution_is_slashed
|
||||
// - suppressed_solution_gets_bond_back
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
assert_eq!(balances(&99), (100, 0));
|
||||
@@ -951,6 +1021,17 @@ mod tests {
|
||||
assert_eq!(balances(&999), (95, 0));
|
||||
// 9999 gets everything back, including the call fee.
|
||||
assert_eq!(balances(&9999), (100 + 8, 0));
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::Slashed { account: 999, value: 5 },
|
||||
Event::Rewarded { account: 99, value: 7 }
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
@@ -960,7 +1041,7 @@ mod tests {
|
||||
.signed_weight(Weight::from_ref_time(40).set_proof_size(u64::MAX))
|
||||
.mock_weight_info(MockedWeightInfo::Basic)
|
||||
.build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
let (raw, witness) = MultiPhase::mine_solution().unwrap();
|
||||
@@ -994,7 +1075,7 @@ mod tests {
|
||||
#[test]
|
||||
fn insufficient_deposit_does_not_store_submission() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
let solution = raw_solution();
|
||||
@@ -1014,7 +1095,7 @@ mod tests {
|
||||
#[test]
|
||||
fn insufficient_deposit_with_full_queue_works_properly() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
for s in 0..SignedMaxSubmissions::get() {
|
||||
@@ -1060,7 +1141,7 @@ mod tests {
|
||||
#[test]
|
||||
fn finalize_signed_phase_is_idempotent_given_submissions() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert!(MultiPhase::current_phase().is_signed());
|
||||
|
||||
let solution = raw_solution();
|
||||
@@ -1073,6 +1154,15 @@ mod tests {
|
||||
|
||||
// calling it again doesn't change anything
|
||||
assert_storage_noop!(MultiPhase::finalize_signed_phase());
|
||||
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::SolutionStored { compute: ElectionCompute::Signed, prev_ejected: false },
|
||||
Event::Rewarded { account: 99, value: 7 }
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1050,11 +1050,12 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
mock::{
|
||||
roll_to, roll_to_with_ocw, trim_helpers, witness, BlockNumber, ExtBuilder, Extrinsic,
|
||||
MinerMaxWeight, MultiPhase, Runtime, RuntimeCall, RuntimeOrigin, System,
|
||||
TestNposSolution, TrimHelpers, UnsignedPhase,
|
||||
multi_phase_events, roll_to, roll_to_signed, roll_to_unsigned, roll_to_with_ocw,
|
||||
trim_helpers, witness, BlockNumber, ExtBuilder, Extrinsic, MinerMaxWeight, MultiPhase,
|
||||
Runtime, RuntimeCall, RuntimeOrigin, System, TestNposSolution, TrimHelpers,
|
||||
UnsignedPhase,
|
||||
},
|
||||
CurrentPhase, InvalidTransaction, Phase, QueuedSolution, TransactionSource,
|
||||
CurrentPhase, Event, InvalidTransaction, Phase, QueuedSolution, TransactionSource,
|
||||
TransactionValidityError,
|
||||
};
|
||||
use codec::Decode;
|
||||
@@ -1100,7 +1101,7 @@ mod tests {
|
||||
));
|
||||
|
||||
// signed
|
||||
roll_to(15);
|
||||
roll_to_signed();
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Signed);
|
||||
assert!(matches!(
|
||||
<MultiPhase as ValidateUnsigned>::validate_unsigned(
|
||||
@@ -1116,7 +1117,7 @@ mod tests {
|
||||
));
|
||||
|
||||
// unsigned
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
assert!(<MultiPhase as ValidateUnsigned>::validate_unsigned(
|
||||
@@ -1147,7 +1148,7 @@ mod tests {
|
||||
#[test]
|
||||
fn validate_unsigned_retracts_low_score() {
|
||||
ExtBuilder::default().desired_targets(0).build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
let solution = RawSolution::<TestNposSolution> {
|
||||
@@ -1193,7 +1194,7 @@ mod tests {
|
||||
#[test]
|
||||
fn validate_unsigned_retracts_incorrect_winner_count() {
|
||||
ExtBuilder::default().desired_targets(1).build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
let raw = RawSolution::<TestNposSolution> {
|
||||
@@ -1222,7 +1223,7 @@ mod tests {
|
||||
.miner_tx_priority(20)
|
||||
.desired_targets(0)
|
||||
.build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
let solution = RawSolution::<TestNposSolution> {
|
||||
@@ -1253,7 +1254,7 @@ mod tests {
|
||||
Some(\"PreDispatchWrongWinnerCount\") })")]
|
||||
fn unfeasible_solution_panics() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
// This is in itself an invalid BS solution.
|
||||
@@ -1275,7 +1276,7 @@ mod tests {
|
||||
deprive validator from their authoring reward.")]
|
||||
fn wrong_witness_panics() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
// This solution is unfeasible as well, but we won't even get there.
|
||||
@@ -1299,7 +1300,7 @@ mod tests {
|
||||
#[test]
|
||||
fn miner_works() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
// ensure we have snapshots in place.
|
||||
@@ -1317,6 +1318,17 @@ mod tests {
|
||||
witness
|
||||
));
|
||||
assert!(MultiPhase::queued_solution().is_some());
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::UnsignedPhaseStarted { round: 1 },
|
||||
Event::SolutionStored {
|
||||
compute: ElectionCompute::Unsigned,
|
||||
prev_ejected: false
|
||||
}
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1326,7 +1338,7 @@ mod tests {
|
||||
.miner_weight(Weight::from_ref_time(100).set_proof_size(u64::MAX))
|
||||
.mock_weight_info(crate::mock::MockedWeightInfo::Basic)
|
||||
.build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
let (raw, witness) = MultiPhase::mine_solution().unwrap();
|
||||
@@ -1360,7 +1372,7 @@ mod tests {
|
||||
fn miner_will_not_submit_if_not_enough_winners() {
|
||||
let (mut ext, _) = ExtBuilder::default().desired_targets(8).build_offchainify(0);
|
||||
ext.execute_with(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
// Force the number of winners to be bigger to fail
|
||||
@@ -1386,7 +1398,7 @@ mod tests {
|
||||
.add_voter(8, 5, bounded_vec![10])
|
||||
.better_unsigned_threshold(Perbill::from_percent(50))
|
||||
.build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
assert_eq!(MultiPhase::desired_targets().unwrap(), 1);
|
||||
|
||||
@@ -1488,7 +1500,7 @@ mod tests {
|
||||
ext.execute_with(|| {
|
||||
let offchain_repeat = <Runtime as Config>::OffchainRepeat::get();
|
||||
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
// first execution -- okay.
|
||||
@@ -1529,7 +1541,7 @@ mod tests {
|
||||
let guard = StorageValueRef::persistent(&OFFCHAIN_LOCK);
|
||||
let last_block = StorageValueRef::persistent(OFFCHAIN_LAST_BLOCK);
|
||||
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
// initially, the lock is not set.
|
||||
@@ -1550,7 +1562,7 @@ mod tests {
|
||||
// ensure that if the guard is in hold, a new execution is not allowed.
|
||||
let (mut ext, pool) = ExtBuilder::default().build_offchainify(0);
|
||||
ext.execute_with(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert!(MultiPhase::current_phase().is_unsigned());
|
||||
|
||||
// artificially set the value, as if another thread is mid-way.
|
||||
@@ -1578,7 +1590,7 @@ mod tests {
|
||||
fn ocw_only_runs_when_unsigned_open_now() {
|
||||
let (mut ext, pool) = ExtBuilder::default().build_offchainify(0);
|
||||
ext.execute_with(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
assert_eq!(MultiPhase::current_phase(), Phase::Unsigned((true, 25)));
|
||||
|
||||
// we must clear the offchain storage to ensure the offchain execution check doesn't get
|
||||
@@ -1658,6 +1670,21 @@ mod tests {
|
||||
|
||||
// the submitted solution changes because the cache was cleared.
|
||||
assert_eq!(tx_cache_1, tx_cache_3);
|
||||
assert_eq!(
|
||||
multi_phase_events(),
|
||||
vec![
|
||||
Event::SignedPhaseStarted { round: 1 },
|
||||
Event::UnsignedPhaseStarted { round: 1 },
|
||||
Event::ElectionFinalized {
|
||||
compute: ElectionCompute::Fallback,
|
||||
score: ElectionScore {
|
||||
minimal_stake: 0,
|
||||
sum_stake: 0,
|
||||
sum_stake_squared: 0
|
||||
}
|
||||
}
|
||||
]
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1797,7 +1824,7 @@ mod tests {
|
||||
#[test]
|
||||
fn trim_assignments_length_does_not_modify_when_short_enough() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
|
||||
// given
|
||||
let TrimHelpers { mut assignments, encoded_size_of, .. } = trim_helpers();
|
||||
@@ -1822,7 +1849,7 @@ mod tests {
|
||||
#[test]
|
||||
fn trim_assignments_length_modifies_when_too_long() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
|
||||
// given
|
||||
let TrimHelpers { mut assignments, encoded_size_of, .. } = trim_helpers();
|
||||
@@ -1848,7 +1875,7 @@ mod tests {
|
||||
#[test]
|
||||
fn trim_assignments_length_trims_lowest_stake() {
|
||||
ExtBuilder::default().build().execute_with(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
|
||||
// given
|
||||
let TrimHelpers { voters, mut assignments, encoded_size_of, voter_index } =
|
||||
@@ -1911,7 +1938,7 @@ mod tests {
|
||||
// or when we trim it to zero.
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
// we need snapshot for `trim_helpers` to work.
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
let TrimHelpers { mut assignments, encoded_size_of, .. } = trim_helpers();
|
||||
assert!(assignments.len() > 0);
|
||||
|
||||
@@ -1933,7 +1960,7 @@ mod tests {
|
||||
#[test]
|
||||
fn mine_solution_solutions_always_within_acceptable_length() {
|
||||
ExtBuilder::default().build_and_execute(|| {
|
||||
roll_to(25);
|
||||
roll_to_unsigned();
|
||||
|
||||
// how long would the default solution be?
|
||||
let solution = MultiPhase::mine_solution().unwrap();
|
||||
|
||||
Reference in New Issue
Block a user