Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MESH-1877/ Staking - off-chain election results fails if validator is no longer compliant #1376

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions pallets/runtime/tests/src/staking/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ use frame_support::{
use mock::*;
use pallet_balances::Error as BalancesError;
use pallet_identity::Error as IdentityError;
use pallet_identity::{Claim1stKey, Claims};
use pallet_staking::*;
use polymesh_primitives::ClaimType;
use sp_npos_elections::ElectionScore;
use sp_runtime::{
assert_eq_error_rate,
Expand Down Expand Up @@ -4341,6 +4343,85 @@ mod offchain_phragmen {
})
}

#[test]
fn off_chain_election_validator_non_compliance() {
ExtBuilder::default()
.offchain_election_ext()
.validator_count(4)
.has_stakers(false)
.session_per_era(5)
.period(10)
.election_lookahead(3)
.build()
.execute_with(|| {
build_offchain_phragmen_test_ext();
run_to_block(12);
ValidatorCount::put(10);

// Add did to user
provide_did_to_user(70);
add_secondary_key(70, 71);
// add a new validator candidate
assert_ok!(Staking::bond(
Origin::signed(70),
71,
1000,
RewardDestination::Controller
));
assert_add_permissioned_validator!(&70);

assert_ok!(Staking::validate(
Origin::signed(71),
ValidatorPrefs::default()
));
assert_ok!(Session::set_keys(
Origin::signed(71),
SessionKeys { other: 71.into() },
vec![]
));

run_to_block(37);
assert!(Staking::snapshot_validators().is_some());
let entity_id = Identity::get_identity(&70).unwrap();

let (compact, winners, score) = prepare_submission_with(true, true, 2, |_| {});

// Ensure submit_solution runs successfully
assert_ok!(submit_solution(
Origin::signed(70),
winners.clone(),
compact.clone(),
score,
));

assert_eq!(winners.len(), 5);

let claim_key = Claim1stKey {
target: entity_id,
claim_type: ClaimType::CustomerDueDiligence,
};
Claims::remove_prefix(claim_key, None); // Remove all CDD claims for the validator.

// Ensure did no longer has valid cdd
assert_eq!(Identity::has_valid_cdd(entity_id), false);

run_to_block(87);
assert!(Staking::snapshot_validators().is_some());
let (compact_2, winners_2, score_2) =
prepare_submission_with(true, true, 2, |_| {});

assert_eq!(winners_2.len(), 4);

// Ensure submit_solution runs successfully
assert_ok!(submit_solution(
Origin::signed(10),
winners_2.clone(),
compact_2.clone(),
score_2,
));
})
}

#[test]
fn offchain_storage_is_set() {
let mut ext = ExtBuilder::default()
Expand Down
12 changes: 11 additions & 1 deletion pallets/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2737,7 +2737,17 @@ impl<T: Config> Module<T> {
let mut add_db_reads_writes = |reads, writes| {
consumed_weight += T::DbWeight::get().reads_writes(reads, writes);
};
let validators = <Validators<T>>::iter().map(|(v, _)| v).collect::<Vec<_>>();
let mut validators = Vec::new();
for (validator, _) in <Validators<T>>::iter()
// Polymesh-Note: Ensure that validator is CDDed + has enough bonded.
// -----------------------------------------------------------------
.filter(|(v, _)| {
Self::is_active_balance_above_min_bond(&v) && Self::is_validator_compliant(&v)
})
// -----------------------------------------------------------------
{
validators.push(validator);
}
let mut nominators = <Nominators<T>>::iter().map(|(n, _)| n).collect::<Vec<_>>();

let num_validators = validators.len();
Expand Down