From 423adba250c3900213f7d3aab4ebb1996c1dffce Mon Sep 17 00:00:00 2001 From: Adam Reif Date: Wed, 21 Dec 2022 00:19:20 -0600 Subject: [PATCH] Fix collator_with_400k_not_selected_for_block_production which would erroneously pass, address comments Signed-off-by: Adam Reif --- .../integrations_mock/integration_tests.rs | 101 ++++++++++-------- .../calamari/tests/integrations_mock/mock.rs | 2 +- .../calamari/tests/integrations_mock/mod.rs | 39 ++++++- 3 files changed, 93 insertions(+), 49 deletions(-) diff --git a/runtime/calamari/tests/integrations_mock/integration_tests.rs b/runtime/calamari/tests/integrations_mock/integration_tests.rs index 5e00657d3..85100d847 100644 --- a/runtime/calamari/tests/integrations_mock/integration_tests.rs +++ b/runtime/calamari/tests/integrations_mock/integration_tests.rs @@ -551,20 +551,54 @@ fn sanity_check_round_duration() { fn collator_with_400k_not_selected_for_block_production() { ExtBuilder::default() .with_balances(vec![ - (ALICE.clone(), COLLATOR_MIN_BOND + 100), - (BOB.clone(), COLLATOR_MIN_BOND + 100), - (CHARLIE.clone(), WHITELIST_MIN_BOND + 100), + (ALICE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 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), ]) - .with_collators(vec![ - (ALICE.clone(), COLLATOR_MIN_BOND), - (BOB.clone(), COLLATOR_MIN_BOND), - (CHARLIE.clone(), WHITELIST_MIN_BOND), + .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 bond for everyone but FERDIE + for collator in vec![ + ALICE.clone(), + BOB.clone(), + CHARLIE.clone(), + DAVE.clone(), + EVE.clone(), + ] { + assert_ok!(ParachainStaking::candidate_bond_more( + Origin::signed(collator.clone()), + MIN_BOND_TO_BE_CONSIDERED_COLLATOR - EARLY_COLLATOR_MINIMUM_STAKE + )); + } + + // 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(&BOB)); - assert!(!ParachainStaking::compute_top_candidates().contains(&CHARLIE)); + 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)); }); } @@ -572,9 +606,9 @@ fn collator_with_400k_not_selected_for_block_production() { fn collator_cant_join_below_standard_bond() { ExtBuilder::default() .with_balances(vec![ - (ALICE.clone(), COLLATOR_MIN_BOND + 100), - (BOB.clone(), COLLATOR_MIN_BOND + 100), - (CHARLIE.clone(), WHITELIST_MIN_BOND + 100), + (ALICE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100), + (BOB.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR + 100), + (CHARLIE.clone(), EARLY_COLLATOR_MINIMUM_STAKE + 100), ]) .with_collators(vec![(ALICE.clone(), 50)]) .build() @@ -582,7 +616,7 @@ fn collator_cant_join_below_standard_bond() { assert_noop!( ParachainStaking::join_candidates( Origin::signed(BOB.clone()), - COLLATOR_MIN_BOND - 1, + MIN_BOND_TO_BE_CONSIDERED_COLLATOR - 1, 6u32 ), pallet_parachain_staking::Error::::CandidateBondBelowMin @@ -594,12 +628,12 @@ fn collator_cant_join_below_standard_bond() { fn collator_can_leave_if_below_standard_bond() { ExtBuilder::default() .with_balances(vec![ - (ALICE.clone(), WHITELIST_MIN_BOND + 100), - (BOB.clone(), WHITELIST_MIN_BOND + 100), - (CHARLIE.clone(), WHITELIST_MIN_BOND + 100), - (DAVE.clone(), WHITELIST_MIN_BOND + 100), - (EVE.clone(), WHITELIST_MIN_BOND + 100), - (FERDIE.clone(), WHITELIST_MIN_BOND + 100), + (ALICE.clone(), EARLY_COLLATOR_MINIMUM_STAKE + 100), + (BOB.clone(), EARLY_COLLATOR_MINIMUM_STAKE + 100), + (CHARLIE.clone(), EARLY_COLLATOR_MINIMUM_STAKE + 100), + (DAVE.clone(), EARLY_COLLATOR_MINIMUM_STAKE + 100), + (EVE.clone(), EARLY_COLLATOR_MINIMUM_STAKE + 100), + (FERDIE.clone(), EARLY_COLLATOR_MINIMUM_STAKE + 100), ]) .with_invulnerables(vec![]) .with_authorities(vec![ @@ -612,39 +646,18 @@ fn collator_can_leave_if_below_standard_bond() { ]) .build() .execute_with(|| { - // 0. initialize collators as candidates with manta_collator_selection - assert_ok!(CollatorSelection::set_desired_candidates(root_origin(), 6)); - for aid in [ + initialize_collators_through_whitelist(vec![ ALICE.clone(), BOB.clone(), CHARLIE.clone(), DAVE.clone(), EVE.clone(), FERDIE.clone(), - ] { - assert_ok!(CollatorSelection::register_candidate(root_origin(), aid)); - } - - assert_eq!(CollatorSelection::candidates().len(), 6); - // Migrate to staking - reserves (lower) whitelist bond - assert_ok!(ParachainStaking::initialize_pallet( - 1, - vec![ - ALICE.clone(), - BOB.clone(), - CHARLIE.clone(), - DAVE.clone(), - EVE.clone(), - FERDIE.clone() - ], - calamari_runtime::staking::inflation_config::() - )); - assert_eq!(ParachainStaking::candidate_pool().len(), 6); - - // 1. Attempt to leave as whitelist collator + ]); + // Attempt to leave as whitelist collator assert_ok!(ParachainStaking::schedule_leave_candidates( Origin::signed(FERDIE.clone()), - 6u32 + 6 )); }); } @@ -663,7 +676,7 @@ fn collator_can_leave_if_below_standard_bond() { #[test] fn session_and_collator_selection_work() { ExtBuilder::default() - .with_collators(vec![(ALICE.clone(), COLLATOR_MIN_BOND)]) + .with_collators(vec![(ALICE.clone(), MIN_BOND_TO_BE_CONSIDERED_COLLATOR)]) .with_balances(vec![ (ALICE.clone(), INITIAL_BALANCE), (BOB.clone(), INITIAL_BALANCE), diff --git a/runtime/calamari/tests/integrations_mock/mock.rs b/runtime/calamari/tests/integrations_mock/mock.rs index 4c116f372..63181eb3f 100644 --- a/runtime/calamari/tests/integrations_mock/mock.rs +++ b/runtime/calamari/tests/integrations_mock/mock.rs @@ -129,7 +129,7 @@ impl ExtBuilder { .eviction_baseline, eviction_tolerance: manta_collator_selection::GenesisConfig::::default() .eviction_tolerance, - candidacy_bond: WHITELIST_MIN_BOND, + candidacy_bond: EARLY_COLLATOR_MINIMUM_STAKE, desired_candidates: self.desired_candidates, } .assimilate_storage(&mut t) diff --git a/runtime/calamari/tests/integrations_mock/mod.rs b/runtime/calamari/tests/integrations_mock/mod.rs index cb09ce396..283100c5f 100644 --- a/runtime/calamari/tests/integrations_mock/mod.rs +++ b/runtime/calamari/tests/integrations_mock/mod.rs @@ -20,9 +20,16 @@ pub mod integration_tests; pub mod mock; use calamari_runtime::opaque::SessionKeys; -pub use calamari_runtime::{currency::KMA, Event, Origin, Runtime, System}; +pub use calamari_runtime::{ + currency::KMA, + staking::{EARLY_COLLATOR_MINIMUM_STAKE, MIN_BOND_TO_BE_CONSIDERED_COLLATOR}, + CollatorSelection, Event, Origin, ParachainStaking, Runtime, System, +}; -use frame_support::weights::{DispatchInfo, Weight}; +use frame_support::{ + assert_ok, + weights::{DispatchInfo, Weight}, +}; use lazy_static::lazy_static; use manta_primitives::types::{AccountId, Balance}; use session_key_primitives::util::{unchecked_account_id, unchecked_collator_keys}; @@ -30,8 +37,6 @@ use sp_core::sr25519::Public; #[cfg(feature = "std")] pub(crate) use std::clone::Clone; -pub const COLLATOR_MIN_BOND: Balance = 4_000_000 * KMA; -pub const WHITELIST_MIN_BOND: Balance = 400_000 * KMA; pub const INITIAL_BALANCE: Balance = 1_000_000_000_000 * KMA; lazy_static! { @@ -71,3 +76,29 @@ pub fn last_event() -> Event { pub fn root_origin() -> ::Origin { ::Origin::root() } + +pub fn initialize_collators_through_whitelist(collators: Vec) { + // Add collators through the whitelist + let candidate_count = collators.len() as u32; + assert_ok!(CollatorSelection::set_desired_candidates( + root_origin(), + candidate_count + )); + for aid in collators.clone() { + assert_ok!(CollatorSelection::register_candidate(root_origin(), aid)); + } + assert_eq!( + CollatorSelection::candidates().len(), + candidate_count as usize + ); + // Migrate to staking - reserves (lower) whitelist bond + assert_ok!(ParachainStaking::initialize_pallet( + 1, + collators, + calamari_runtime::staking::inflation_config::() + )); + assert_eq!( + ParachainStaking::candidate_pool().len(), + candidate_count as usize + ); +}