Skip to content

Commit 676b295

Browse files
committed
Adjustable collator count
1 parent 34db767 commit 676b295

File tree

4 files changed

+47
-3
lines changed

4 files changed

+47
-3
lines changed

pallet/ecdsa-authority/tests/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ fn submit_new_message_root_signature() {
430430
// Case 3.
431431
let s_3 = sign(&k_3, &message.0);
432432
assert_noop!(
433-
EcdsaAuthority::submit_new_message_root_signature(RuntimeOrigin::signed(a_3), s_3,),
433+
EcdsaAuthority::submit_new_message_root_signature(RuntimeOrigin::signed(a_3), s_3),
434434
<Error<Runtime>>::NotAuthority
435435
);
436436

pallet/staking/src/lib.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ pub mod pallet {
245245
NotStaker,
246246
/// Target is not a collator.
247247
TargetNotCollator,
248+
/// Collator count mustn't be zero.
249+
ZeroCollatorCount,
248250
}
249251

250252
/// All staking ledgers.
@@ -327,6 +329,10 @@ pub mod pallet {
327329
#[pallet::genesis_build]
328330
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
329331
fn build(&self) {
332+
if self.collator_count == 0 {
333+
panic!("[pallet::staking] collator count mustn't be 0");
334+
}
335+
330336
<SessionStartTime<T>>::put(self.now);
331337
<ElapsedTime<T>>::put(self.elapsed_time);
332338
<CollatorCount<T>>::put(self.collator_count);
@@ -570,6 +576,25 @@ pub mod pallet {
570576

571577
Ok(())
572578
}
579+
580+
/// Set collator count.
581+
///
582+
/// This will apply to the incoming session.
583+
///
584+
/// Require root origin.
585+
#[pallet::call_index(7)]
586+
#[pallet::weight(0)]
587+
pub fn set_collator_count(origin: OriginFor<T>, count: u32) -> DispatchResult {
588+
ensure_root(origin)?;
589+
590+
if count == 0 {
591+
return Err(<Error<T>>::ZeroCollatorCount)?;
592+
}
593+
594+
<CollatorCount<T>>::put(count);
595+
596+
Ok(())
597+
}
573598
}
574599
impl<T> Pallet<T>
575600
where

pallet/staking/tests/mock.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,6 @@ fn initialize_block(number: u64) {
290290
<AllPalletsWithSystem as OnInitialize<u64>>::on_initialize(number);
291291
}
292292

293-
#[derive(Default)]
294293
pub struct ExtBuilder {
295294
collator_count: u32,
296295
}
@@ -332,3 +331,8 @@ impl ExtBuilder {
332331
ext
333332
}
334333
}
334+
impl Default for ExtBuilder {
335+
fn default() -> Self {
336+
Self { collator_count: 1 }
337+
}
338+
}

pallet/staking/tests/tests.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use darwinia_staking::*;
2727
use dc_types::{Balance, UNIT};
2828
// substrate
2929
use frame_support::{assert_noop, assert_ok, BoundedVec};
30-
use sp_runtime::{assert_eq_error_rate, Perbill};
30+
use sp_runtime::{assert_eq_error_rate, DispatchError, Perbill};
3131

3232
#[test]
3333
fn stake_should_work() {
@@ -403,6 +403,21 @@ fn chill_should_work() {
403403
});
404404
}
405405

406+
#[test]
407+
fn set_collator_count_should_work() {
408+
ExtBuilder::default().build().execute_with(|| {
409+
assert_noop!(
410+
Staking::set_collator_count(RuntimeOrigin::signed(1), 1),
411+
DispatchError::BadOrigin
412+
);
413+
assert_noop!(
414+
Staking::set_collator_count(RuntimeOrigin::root(), 0),
415+
<Error<Runtime>>::ZeroCollatorCount
416+
);
417+
assert_ok!(Staking::set_collator_count(RuntimeOrigin::root(), 1));
418+
});
419+
}
420+
406421
#[test]
407422
fn power_should_work() {
408423
ExtBuilder::default().build().execute_with(|| {

0 commit comments

Comments
 (0)