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:
Sergej Sakac
2022-10-05 00:16:07 +02:00
committed by GitHub
parent 0e388ac5b7
commit 8f048cf522
4 changed files with 305 additions and 72 deletions
@@ -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();