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

feat: kick collators without sufficient escrow balance #707

Merged
merged 3 commits into from
Sep 13, 2022
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
13 changes: 10 additions & 3 deletions crates/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,17 +428,24 @@ pub mod pallet {
}

/// Kicks out candidates that did not produce a block in the kick threshold
/// and refund their deposits.
/// or whose free balance drops below the minimum and refund their deposits.
pub fn kick_stale_candidates(candidates: Vec<CandidateInfo<T::AccountId, BalanceOf<T>>>) -> Vec<T::AccountId> {
// TODO: also kick candidates when escrow balance drops
let now = frame_system::Pallet::<T>::block_number();
let kick_threshold = T::KickThreshold::get();
let candidacy_bond = Self::candidacy_bond();
candidates
.into_iter()
.filter_map(|c| {
let last_block = <LastAuthoredBlock<T>>::get(c.who.clone());
let since_last = now.saturating_sub(last_block);
if since_last < kick_threshold || Self::candidates().len() as u32 <= T::MinCandidates::get() {
// total balance excludes reserved
let balance = T::StakingCurrency::total_balance(&c.who);

// don't kick if not enough candidates
let surplus_candidates = Self::candidates().len() as u32 > T::MinCandidates::get();
// kick if stale OR balance is not sufficient
let is_good_collator = since_last < kick_threshold && balance >= candidacy_bond;
if is_good_collator || !surplus_candidates {
Some(c.who)
} else {
let outcome = Self::try_remove_candidate(&c.who);
Expand Down
44 changes: 42 additions & 2 deletions crates/collator-selection/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate as collator_selection;
use crate::{mock::*, CandidateInfo, Error};
use crate::{self as collator_selection, mock::*, CandidateInfo, Error};
use frame_support::{
assert_noop, assert_ok,
traits::{Currency, GenesisBuild, OnInitialize},
Expand Down Expand Up @@ -377,6 +376,47 @@ fn should_not_kick_mechanism_too_few() {
});
}

#[test]
fn should_kick_mechanism_below_balance() {
new_test_ext().execute_with(|| {
// add two candidates
assert_ok!(CollatorSelection::register_as_candidate(Origin::signed(3)));
assert_ok!(CollatorSelection::register_as_candidate(Origin::signed(5)));

// first session change
initialize_to_block(Period::get());
assert_eq!(SessionChangeBlock::get(), Period::get());

assert_eq!(CollatorSelection::candidates().len(), 2);
<crate::LastAuthoredBlock<Test>>::insert(3, 17);
<crate::LastAuthoredBlock<Test>>::insert(5, 19);

// need to decrease reference counter (from candidate bond)
frame_system::Pallet::<Test>::dec_consumers(&3);
gregdhill marked this conversation as resolved.
Show resolved Hide resolved
// workaround to emulate escrow total balance decreasing
assert_ok!(Balances::set_balance(Origin::root(), 3, 0, 0));

// second session change
initialize_to_block(Period::get() * 2);
assert_eq!(SessionChangeBlock::get(), Period::get() * 2);

// 3 & 5 authored recently but 3 balance too low
assert_eq!(CollatorSelection::candidates().len(), 1);

// 3 will be kicked after 1 session delay
assert_eq!(SessionHandlerCollators::get(), vec![1, 2, 3, 5]);
let collator = CandidateInfo { who: 5, deposit: 10 };
assert_eq!(CollatorSelection::candidates(), vec![collator]);

// third session change
initialize_to_block(Period::get() * 3);
assert_eq!(SessionChangeBlock::get(), Period::get() * 3);

// 3 gets kicked after 1 session delay
assert_eq!(SessionHandlerCollators::get(), vec![1, 2, 5]);
});
}

#[test]
#[should_panic = "duplicate invulnerables in genesis."]
fn cannot_set_genesis_value_twice() {
Expand Down