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

Add self-bond filter condition when computing new set of collators #1043

Merged
merged 12 commits into from
Mar 16, 2023
10 changes: 9 additions & 1 deletion pallets/parachain-staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1687,8 +1687,16 @@ pub mod pallet {
let mut collators = candidates
.into_iter()
.rev()
.filter(|x| {
// Only consider collators above minimum total stake and self-bond
x.amount >= T::MinCollatorStk::get()
&& Self::is_candidate(&x.owner)
&& Self::candidate_info(x.owner.clone())
.expect("is_candidate => canidateinfo exists. qed")
.bond
>= T::MinCandidateStk::get()
})
.take(top_n)
.filter(|x| x.amount >= T::MinCollatorStk::get())
.map(|x| x.owner)
.collect::<Vec<T::AccountId>>();
collators.sort();
Expand Down
77 changes: 72 additions & 5 deletions runtime/calamari/tests/integrations_mock/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,73 @@ fn collator_cant_join_below_standard_bond() {
});
}

#[test]
fn collator_with_large_stake_but_too_low_self_bond_not_selected_for_block_production() {
ExtBuilder::default()
.with_balances(vec![
(ALICE.clone(), EARLY_COLLATOR_MINIMUM_STAKE + 100),
(BOB.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
(CHARLIE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
(DAVE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
(EVE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
(FERDIE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
(USER.clone(), 400_000_000 * KMA),
])
.with_invulnerables(vec![])
.with_authorities(vec![
(ALICE.clone(), ALICE_SESSION_KEYS.clone()),
(BOB.clone(), BOB_SESSION_KEYS.clone()),
(CHARLIE.clone(), CHARLIE_SESSION_KEYS.clone()),
(DAVE.clone(), DAVE_SESSION_KEYS.clone()),
(EVE.clone(), EVE_SESSION_KEYS.clone()),
(FERDIE.clone(), FERDIE_SESSION_KEYS.clone()),
])
.build()
.execute_with(|| {
initialize_collators_through_whitelist(vec![
ALICE.clone(),
BOB.clone(),
CHARLIE.clone(),
DAVE.clone(),
EVE.clone(),
FERDIE.clone(),
]);
// Increase self-bond for everyone but ALICE
for collator in vec![
BOB.clone(),
CHARLIE.clone(),
DAVE.clone(),
EVE.clone(),
FERDIE.clone(),
] {
assert_ok!(ParachainStaking::candidate_bond_more(
Origin::signed(collator.clone()),
MIN_BOND_TO_BE_CONSIDERED_COLLATOR - EARLY_COLLATOR_MINIMUM_STAKE
));
}

// Delegate a large amount of tokens to EVE and ALICE
for collator in vec![EVE.clone(), ALICE.clone()] {
assert_ok!(ParachainStaking::delegate(
Origin::signed(USER.clone()),
collator,
100_000_000 * KMA,
50,
50
));
}

// Ensure ALICE is not selected despite having a large total stake through delegation
// NOTE: Must use 6 or more collators because 5 is the minimum on calamari
assert!(!ParachainStaking::compute_top_candidates().contains(&ALICE));
assert!(ParachainStaking::compute_top_candidates().contains(&BOB));
assert!(ParachainStaking::compute_top_candidates().contains(&CHARLIE));
assert!(ParachainStaking::compute_top_candidates().contains(&DAVE));
assert!(ParachainStaking::compute_top_candidates().contains(&EVE));
assert!(ParachainStaking::compute_top_candidates().contains(&FERDIE));
});
}

#[test]
fn collator_can_leave_if_below_standard_bond() {
ExtBuilder::default()
Expand Down Expand Up @@ -646,12 +713,12 @@ fn collator_can_leave_if_below_standard_bond() {
fn collator_with_400k_not_selected_for_block_production() {
ExtBuilder::default()
.with_balances(vec![
(ALICE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
(ALICE.clone(), EARLY_COLLATOR_MINIMUM_STAKE + 100),
(BOB.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
(CHARLIE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
(DAVE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
(EVE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
(FERDIE.clone(), EARLY_COLLATOR_MINIMUM_STAKE + 100),
(FERDIE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100),
])
.with_invulnerables(vec![])
.with_authorities(vec![
Expand All @@ -674,11 +741,11 @@ fn collator_with_400k_not_selected_for_block_production() {
]);
// Increase bond for everyone but FERDIE
for collator in vec![
ALICE.clone(),
BOB.clone(),
CHARLIE.clone(),
DAVE.clone(),
EVE.clone(),
FERDIE.clone(),
] {
assert_ok!(ParachainStaking::candidate_bond_more(
Origin::signed(collator.clone()),
Expand All @@ -688,12 +755,12 @@ fn collator_with_400k_not_selected_for_block_production() {

// Ensure CHARLIE and later are not selected
// NOTE: Must use 6 or more collators because 5 is the minimum on calamari
assert!(ParachainStaking::compute_top_candidates().contains(&ALICE));
assert!(!ParachainStaking::compute_top_candidates().contains(&ALICE));
assert!(ParachainStaking::compute_top_candidates().contains(&BOB));
assert!(ParachainStaking::compute_top_candidates().contains(&CHARLIE));
assert!(ParachainStaking::compute_top_candidates().contains(&DAVE));
assert!(ParachainStaking::compute_top_candidates().contains(&EVE));
assert!(!ParachainStaking::compute_top_candidates().contains(&FERDIE));
assert!(ParachainStaking::compute_top_candidates().contains(&FERDIE));
});
}

Expand Down
5 changes: 5 additions & 0 deletions runtime/calamari/tests/integrations_mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ lazy_static! {
pub(crate) static ref DAVE: AccountId = unchecked_account_id::<Public>("Dave");
pub(crate) static ref EVE: AccountId = unchecked_account_id::<Public>("Eve");
pub(crate) static ref FERDIE: AccountId = unchecked_account_id::<Public>("Ferdie");
pub(crate) static ref USER: AccountId = unchecked_account_id::<Public>("User");
pub(crate) static ref ALICE_SESSION_KEYS: SessionKeys =
SessionKeys::from_seed_unchecked("Alice");
pub(crate) static ref BOB_SESSION_KEYS: SessionKeys = SessionKeys::from_seed_unchecked("Bob");
Expand Down Expand Up @@ -96,4 +97,8 @@ pub fn initialize_collators_through_whitelist(collators: Vec<AccountId>) {
ParachainStaking::candidate_pool().len(),
candidate_count as usize
);
assert_ok!(ParachainStaking::set_total_selected(
root_origin(),
candidate_count
));
}