Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Make minimum validator count dynamic.
  • Loading branch information
gavofyork committed Aug 22, 2018
commit dc6de6cb49d9df007814a27e5f1e2edeb176fd60
1 change: 1 addition & 0 deletions demo/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub fn run<I, T>(args: I) -> error::Result<()> where
existential_deposit: 500,
balances: vec![(god_key.clone().into(), 1u64 << 63)].into_iter().collect(),
validator_count: 12,
minimum_validator_count: 4,
sessions_per_era: 24, // 24 hours per era.
bonding_duration: 90, // 90 days per bond.
early_era_slash: 10000,
Expand Down
1 change: 1 addition & 0 deletions demo/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ mod tests {
balances: vec![(alice(), 111)],
intentions: vec![alice(), bob(), Charlie.to_raw_public().into()],
validator_count: 3,
minimum_validator_count: 0,
bonding_duration: 0,
transaction_base_fee: 1,
transaction_byte_fee: 0,
Expand Down
1 change: 1 addition & 0 deletions substrate/runtime/contract/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ fn new_test_ext(existential_deposit: u64, gas_price: u64) -> runtime_io::TestExt
balances: vec![],
intentions: vec![],
validator_count: 2,
minimum_validator_count: 0,
bonding_duration: 0,
transaction_base_fee: 0,
transaction_byte_fee: 0,
Expand Down
1 change: 1 addition & 0 deletions substrate/runtime/council/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ mod tests {
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
intentions: vec![],
validator_count: 2,
minimum_validator_count: 0,
bonding_duration: 0,
transaction_base_fee: 0,
transaction_byte_fee: 0,
Expand Down
1 change: 1 addition & 0 deletions substrate/runtime/democracy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ mod tests {
balances: vec![(1, 10), (2, 20), (3, 30), (4, 40), (5, 50), (6, 60)],
intentions: vec![],
validator_count: 2,
minimum_validator_count: 0,
bonding_duration: 3,
transaction_base_fee: 0,
transaction_byte_fee: 0,
Expand Down
1 change: 1 addition & 0 deletions substrate/runtime/executive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ mod tests {
balances: vec![(1, 111)],
intentions: vec![],
validator_count: 0,
minimum_validator_count: 0,
bonding_duration: 0,
transaction_base_fee: 10,
transaction_byte_fee: 0,
Expand Down
7 changes: 6 additions & 1 deletion substrate/runtime/staking/src/genesis_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ pub struct GenesisConfig<T: Trait> {
pub current_era: T::BlockNumber,
pub balances: Vec<(T::AccountId, T::Balance)>,
pub intentions: Vec<T::AccountId>,
pub validator_count: u64,
pub validator_count: u32,
pub minimum_validator_count: u32,
pub bonding_duration: T::BlockNumber,
pub transaction_base_fee: T::Balance,
pub transaction_byte_fee: T::Balance,
Expand All @@ -60,6 +61,7 @@ impl<T: Trait> GenesisConfig<T> where T::AccountId: From<u64> {
balances: vec![(T::AccountId::from(1), T::Balance::sa(111))],
intentions: vec![T::AccountId::from(1), T::AccountId::from(2), T::AccountId::from(3)],
validator_count: 3,
minimum_validator_count: 1,
bonding_duration: T::BlockNumber::sa(0),
transaction_base_fee: T::Balance::sa(0),
transaction_byte_fee: T::Balance::sa(0),
Expand Down Expand Up @@ -88,6 +90,7 @@ impl<T: Trait> GenesisConfig<T> where T::AccountId: From<u64> {
],
intentions: vec![T::AccountId::from(1), T::AccountId::from(2), T::AccountId::from(3)],
validator_count: 3,
minimum_validator_count: 1,
bonding_duration: T::BlockNumber::sa(0),
transaction_base_fee: T::Balance::sa(1),
transaction_byte_fee: T::Balance::sa(0),
Expand All @@ -110,6 +113,7 @@ impl<T: Trait> Default for GenesisConfig<T> {
balances: vec![],
intentions: vec![],
validator_count: 0,
minimum_validator_count: 0,
bonding_duration: T::BlockNumber::sa(1000),
transaction_base_fee: T::Balance::sa(0),
transaction_byte_fee: T::Balance::sa(0),
Expand All @@ -133,6 +137,7 @@ impl<T: Trait> primitives::BuildStorage for GenesisConfig<T> {
Self::hash(<Intentions<T>>::key()).to_vec() => self.intentions.encode(),
Self::hash(<SessionsPerEra<T>>::key()).to_vec() => self.sessions_per_era.encode(),
Self::hash(<ValidatorCount<T>>::key()).to_vec() => self.validator_count.encode(),
Self::hash(<MinimumValidatorCount<T>>::key()).to_vec() => self.minimum_validator_count.encode(),
Self::hash(<BondingDuration<T>>::key()).to_vec() => self.bonding_duration.encode(),
Self::hash(<TransactionBaseFee<T>>::key()).to_vec() => self.transaction_base_fee.encode(),
Self::hash(<TransactionByteFee<T>>::key()).to_vec() => self.transaction_byte_fee.encode(),
Expand Down
16 changes: 11 additions & 5 deletions substrate/runtime/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod genesis_config;
#[cfg(feature = "std")]
pub use genesis_config::GenesisConfig;

const MINIMUM_REQUIRED_VALIDATORS: usize = 4;
const DEFAULT_MINIMUM_VALIDATOR_COUNT: usize = 4;

/// Number of account IDs stored per enum set.
const ENUM_SET_SIZE: usize = 64;
Expand Down Expand Up @@ -175,8 +175,10 @@ decl_storage! {

// The length of the bonding duration in eras.
pub BondingDuration get(bonding_duration): b"sta:loc" => required T::BlockNumber;
// The length of a staking era in sessions.
// The ideal number of staking participants.
pub ValidatorCount get(validator_count): b"sta:vac" => required u32;
// Minimum number of staking participants before emergency conditions are imposed.
pub MinimumValidatorCount: b"sta:minimum_validator_count" => u32;
// The length of a staking era in sessions.
pub SessionsPerEra get(sessions_per_era): b"sta:spe" => required T::BlockNumber;
// The total amount of stake on the system.
Expand Down Expand Up @@ -280,6 +282,10 @@ impl<T: Trait> Module<T> {

// PUBLIC IMMUTABLES

pub fn minimum_validator_count() -> usize {
<MinimumValidatorCount<T>>::get().map(|v| v as usize).unwrap_or(DEFAULT_MINIMUM_VALIDATOR_COUNT)
}

/// The length of a staking era in blocks.
pub fn era_length() -> T::BlockNumber {
Self::sessions_per_era() * <session::Module<T>>::length()
Expand Down Expand Up @@ -403,7 +409,7 @@ impl<T: Trait> Module<T> {
/// Effects will be felt at the beginning of the next era.
fn unstake(aux: &T::PublicAux, intentions_index: u32) -> Result {
// unstake fails in degenerate case of having too few existing staked parties
if Self::intentions().len() <= MINIMUM_REQUIRED_VALIDATORS {
if Self::intentions().len() <= Self::minimum_validator_count() {
return Err("cannot unstake when there are too few staked participants")
}
Self::apply_unstake(aux.ref_into(), intentions_index as usize)
Expand Down Expand Up @@ -742,7 +748,7 @@ impl<T: Trait> Module<T> {
fn slash_validator(v: &T::AccountId, slash: T::Balance) {
// skip the slash in degenerate case of having only 4 staking participants despite having a larger
// desired number of validators (validator_count).
if Self::intentions().len() <= MINIMUM_REQUIRED_VALIDATORS {
if Self::intentions().len() <= Self::minimum_validator_count() {
return
}

Expand Down Expand Up @@ -840,7 +846,7 @@ impl<T: Trait> Module<T> {
.collect::<Vec<_>>();

// Avoid making new era if it would leave us with fewer than the minimum needed validators
if intentions.len() < MINIMUM_REQUIRED_VALIDATORS {
if intentions.len() < Self::minimum_validator_count() {
return
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But isn't it too late for this? We've already changed current era index and applied some pending changes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comment out of date - just mean "reevaluate validator set"


Expand Down
1 change: 1 addition & 0 deletions substrate/runtime/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub fn new_test_ext(ext_deposit: u64, session_length: u64, sessions_per_era: u64
},
intentions: vec![],
validator_count: 2,
minimum_validator_count: 0,
bonding_duration: 3,
transaction_base_fee: 0,
transaction_byte_fee: 0,
Expand Down