Skip to content

Commit

Permalink
Adjustable collator count
Browse files Browse the repository at this point in the history
  • Loading branch information
AurevoirXavier committed Mar 11, 2023
1 parent 34db767 commit 676b295
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pallet/ecdsa-authority/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ fn submit_new_message_root_signature() {
// Case 3.
let s_3 = sign(&k_3, &message.0);
assert_noop!(
EcdsaAuthority::submit_new_message_root_signature(RuntimeOrigin::signed(a_3), s_3,),
EcdsaAuthority::submit_new_message_root_signature(RuntimeOrigin::signed(a_3), s_3),
<Error<Runtime>>::NotAuthority
);

Expand Down
25 changes: 25 additions & 0 deletions pallet/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ pub mod pallet {
NotStaker,
/// Target is not a collator.
TargetNotCollator,
/// Collator count mustn't be zero.
ZeroCollatorCount,
}

/// All staking ledgers.
Expand Down Expand Up @@ -327,6 +329,10 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
if self.collator_count == 0 {
panic!("[pallet::staking] collator count mustn't be 0");
}

<SessionStartTime<T>>::put(self.now);
<ElapsedTime<T>>::put(self.elapsed_time);
<CollatorCount<T>>::put(self.collator_count);
Expand Down Expand Up @@ -570,6 +576,25 @@ pub mod pallet {

Ok(())
}

/// Set collator count.
///
/// This will apply to the incoming session.
///
/// Require root origin.
#[pallet::call_index(7)]
#[pallet::weight(0)]
pub fn set_collator_count(origin: OriginFor<T>, count: u32) -> DispatchResult {
ensure_root(origin)?;

if count == 0 {
return Err(<Error<T>>::ZeroCollatorCount)?;
}

<CollatorCount<T>>::put(count);

Ok(())
}
}
impl<T> Pallet<T>
where
Expand Down
6 changes: 5 additions & 1 deletion pallet/staking/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,6 @@ fn initialize_block(number: u64) {
<AllPalletsWithSystem as OnInitialize<u64>>::on_initialize(number);
}

#[derive(Default)]
pub struct ExtBuilder {
collator_count: u32,
}
Expand Down Expand Up @@ -332,3 +331,8 @@ impl ExtBuilder {
ext
}
}
impl Default for ExtBuilder {
fn default() -> Self {
Self { collator_count: 1 }
}
}
17 changes: 16 additions & 1 deletion pallet/staking/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use darwinia_staking::*;
use dc_types::{Balance, UNIT};
// substrate
use frame_support::{assert_noop, assert_ok, BoundedVec};
use sp_runtime::{assert_eq_error_rate, Perbill};
use sp_runtime::{assert_eq_error_rate, DispatchError, Perbill};

#[test]
fn stake_should_work() {
Expand Down Expand Up @@ -403,6 +403,21 @@ fn chill_should_work() {
});
}

#[test]
fn set_collator_count_should_work() {
ExtBuilder::default().build().execute_with(|| {
assert_noop!(
Staking::set_collator_count(RuntimeOrigin::signed(1), 1),
DispatchError::BadOrigin
);
assert_noop!(
Staking::set_collator_count(RuntimeOrigin::root(), 0),
<Error<Runtime>>::ZeroCollatorCount
);
assert_ok!(Staking::set_collator_count(RuntimeOrigin::root(), 1));
});
}

#[test]
fn power_should_work() {
ExtBuilder::default().build().execute_with(|| {
Expand Down

0 comments on commit 676b295

Please sign in to comment.