Skip to content

Commit

Permalink
Fix collator_with_400k_not_selected_for_block_production which would …
Browse files Browse the repository at this point in the history
…erroneously pass, address comments

Signed-off-by: Adam Reif <Garandor@manta.network>
  • Loading branch information
Adam Reif authored and Adam Reif committed Dec 21, 2022
1 parent 4a659e9 commit 423adba
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 49 deletions.
101 changes: 57 additions & 44 deletions runtime/calamari/tests/integrations_mock/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,38 +551,72 @@ 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));
});
}

#[test]
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()
.execute_with(|| {
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::<Runtime>::CandidateBondBelowMin
Expand All @@ -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![
Expand All @@ -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::<Runtime>()
));
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
));
});
}
Expand All @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion runtime/calamari/tests/integrations_mock/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl ExtBuilder {
.eviction_baseline,
eviction_tolerance: manta_collator_selection::GenesisConfig::<Runtime>::default()
.eviction_tolerance,
candidacy_bond: WHITELIST_MIN_BOND,
candidacy_bond: EARLY_COLLATOR_MINIMUM_STAKE,
desired_candidates: self.desired_candidates,
}
.assimilate_storage(&mut t)
Expand Down
39 changes: 35 additions & 4 deletions runtime/calamari/tests/integrations_mock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@ 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};
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! {
Expand Down Expand Up @@ -71,3 +76,29 @@ pub fn last_event() -> Event {
pub fn root_origin() -> <Runtime as frame_system::Config>::Origin {
<Runtime as frame_system::Config>::Origin::root()
}

pub fn initialize_collators_through_whitelist(collators: Vec<AccountId>) {
// 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::<Runtime>()
));
assert_eq!(
ParachainStaking::candidate_pool().len(),
candidate_count as usize
);
}

0 comments on commit 423adba

Please sign in to comment.