Minimum commission for validators (#10347)

* Minimum commission for validators

* rename

* Fix benchmakrs

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_staking --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/staking/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* Update frame/staking/src/pallet/mod.rs

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_staking --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/staking/src/weights.rs --template=./.maintain/frame-weight-template.hbs

Co-authored-by: Parity Bot <admin@parity.io>
Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Kian Paimani
2021-12-02 09:11:10 +01:00
committed by GitHub
parent 1d19c7dc7f
commit e9a7dc3ca6
5 changed files with 236 additions and 136 deletions
+33 -5
View File
@@ -215,6 +215,12 @@ pub mod pallet {
#[pallet::storage]
pub type MinValidatorBond<T: Config> = StorageValue<_, BalanceOf<T>, ValueQuery>;
/// The minimum amount of commission that validators can set.
///
/// If set to `0`, no limit exists.
#[pallet::storage]
pub type MinCommission<T: Config> = StorageValue<_, Perbill, ValueQuery>;
/// Map from all (unlocked) "controller" accounts to the info regarding the staking.
#[pallet::storage]
#[pallet::getter(fn ledger)]
@@ -486,6 +492,8 @@ pub mod pallet {
Vec<(T::AccountId, T::AccountId, BalanceOf<T>, crate::StakerStatus<T::AccountId>)>,
pub min_nominator_bond: BalanceOf<T>,
pub min_validator_bond: BalanceOf<T>,
pub max_validator_count: Option<u32>,
pub max_nominator_count: Option<u32>,
}
#[cfg(feature = "std")]
@@ -502,6 +510,8 @@ pub mod pallet {
stakers: Default::default(),
min_nominator_bond: Default::default(),
min_validator_bond: Default::default(),
max_validator_count: None,
max_nominator_count: None,
}
}
}
@@ -519,6 +529,12 @@ pub mod pallet {
StorageVersion::<T>::put(Releases::V7_0_0);
MinNominatorBond::<T>::put(self.min_nominator_bond);
MinValidatorBond::<T>::put(self.min_validator_bond);
if let Some(x) = self.max_validator_count {
MaxValidatorsCount::<T>::put(x);
}
if let Some(x) = self.max_nominator_count {
MaxNominatorsCount::<T>::put(x);
}
for &(ref stash, ref controller, balance, ref status) in &self.stakers {
log!(
@@ -650,6 +666,8 @@ pub mod pallet {
/// There are too many validators in the system. Governance needs to adjust the staking
/// settings to keep things safe for the runtime.
TooManyValidators,
/// Commission is too low. Must be at least `MinCommission`.
CommissionTooLow,
}
#[pallet::hooks]
@@ -955,9 +973,13 @@ pub mod pallet {
let controller = ensure_signed(origin)?;
let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
ensure!(ledger.active >= MinValidatorBond::<T>::get(), Error::<T>::InsufficientBond);
let stash = &ledger.stash;
// ensure their commission is correct.
ensure!(prefs.commission >= MinCommission::<T>::get(), Error::<T>::CommissionTooLow);
// Only check limits if they are not already a validator.
if !Validators::<T>::contains_key(stash) {
// If this error is reached, we need to adjust the `MinValidatorBond` and start
@@ -1508,7 +1530,7 @@ pub mod pallet {
Ok(())
}
/// Update the various staking limits this pallet.
/// Update the various staking configurations .
///
/// * `min_nominator_bond`: The minimum active bond needed to be a nominator.
/// * `min_validator_bond`: The minimum active bond needed to be a validator.
@@ -1516,26 +1538,32 @@ pub mod pallet {
/// set to `None`, no limit is enforced.
/// * `max_validator_count`: The max number of users who can be a validator at once. When
/// set to `None`, no limit is enforced.
/// * `chill_threshold`: The ratio of `max_nominator_count` or `max_validator_count` which
/// should be filled in order for the `chill_other` transaction to work.
/// * `min_commission`: The minimum amount of commission that each validators must maintain.
/// This is checked only upon calling `validate`. Existing validators are not affected.
///
/// Origin must be Root to call this function.
///
/// NOTE: Existing nominators and validators will not be affected by this update.
/// to kick people under the new limits, `chill_other` should be called.
#[pallet::weight(T::WeightInfo::set_staking_limits())]
pub fn set_staking_limits(
#[pallet::weight(T::WeightInfo::set_staking_configs())]
pub fn set_staking_configs(
origin: OriginFor<T>,
min_nominator_bond: BalanceOf<T>,
min_validator_bond: BalanceOf<T>,
max_nominator_count: Option<u32>,
max_validator_count: Option<u32>,
threshold: Option<Percent>,
chill_threshold: Option<Percent>,
min_commission: Perbill,
) -> DispatchResult {
ensure_root(origin)?;
MinNominatorBond::<T>::set(min_nominator_bond);
MinValidatorBond::<T>::set(min_validator_bond);
MaxNominatorsCount::<T>::set(max_nominator_count);
MaxValidatorsCount::<T>::set(max_validator_count);
ChillThreshold::<T>::set(threshold);
ChillThreshold::<T>::set(chill_threshold);
MinCommission::<T>::set(min_commission);
Ok(())
}