Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

pallet-staking: Add extrinsic force_apply_min_commission #10786

Merged
merged 28 commits into from
Feb 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
79e68ba
pallet-staking: Add extrinsic `force_apply_min_commission`
emostov Feb 3, 2022
62f3578
Add benchmarks
emostov Feb 3, 2022
ada4198
cargo run --quiet --profile=production --features=runtime-benchmarks…
Feb 3, 2022
2cd137d
Bound iteration by max_validator_count
emostov Feb 3, 2022
5c04cd1
Merge branch 'zeke-force-min-stake' of https://github.com/paritytech/…
emostov Feb 3, 2022
5845d58
Merge branch 'master' of https://github.com/paritytech/substrate into…
Feb 3, 2022
89119d2
cargo run --quiet --profile=production --features=runtime-benchmarks…
Feb 3, 2022
4077606
Only apply to 1 validator
emostov Feb 8, 2022
abea07b
Update doc comments
emostov Feb 8, 2022
18c0089
Uncomment tests
emostov Feb 8, 2022
f780da4
Merge branch 'master' of https://github.com/paritytech/substrate into…
Feb 8, 2022
ef68c25
cargo run --quiet --profile=production --features=runtime-benchmarks…
Feb 8, 2022
5ec80f2
Accept signed origins
emostov Feb 9, 2022
86ba7d8
Merge branch 'zeke-force-min-stake' of https://github.com/paritytech/…
emostov Feb 9, 2022
febb602
Merge remote-tracking branch 'origin' into zeke-force-min-stake
emostov Feb 9, 2022
66c740a
cargo run --quiet --profile=production --features=runtime-benchmarks…
Feb 9, 2022
68ed575
Remove contains_key check
emostov Feb 9, 2022
ed5274c
Merge branch 'zeke-force-min-stake' of https://github.com/paritytech/…
emostov Feb 9, 2022
701b745
Add test for try_mutate_exists
emostov Feb 9, 2022
7508b44
Impove try_mutate_exists docs
emostov Feb 9, 2022
b857336
Delete redundant try_mutate_exists tests;
emostov Feb 9, 2022
5ec4422
Delete residual from removed test
emostov Feb 9, 2022
bf25a87
cargo run --quiet --profile=production --features=runtime-benchmarks…
Feb 9, 2022
8318f1b
Return an error when the stash does not exist
emostov Feb 9, 2022
21f9bd1
Merge branch 'zeke-force-min-stake' of https://github.com/paritytech/…
emostov Feb 9, 2022
0d90a92
Update try_mutate_exist doc wording
emostov Feb 9, 2022
9e14263
Update frame/staking/src/pallet/mod.rs
shawntabrizi Feb 9, 2022
48f5aa3
Apply suggestions from code review
shawntabrizi Feb 9, 2022
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
21 changes: 21 additions & 0 deletions frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,27 @@ benchmarks! {
assert!(!T::SortedListProvider::contains(&stash));
}

force_apply_min_commission {
let i in 1..MaxValidators::<T>::get();
// Clean up any existing state
clear_validators_and_nominators::<T>();

// Create `i` validators with a commission of 50%
frame_support::assert_ok!(create_validators::<T>(i, 1));

// Sanity check that all the generated validators have the expected commission
for (_, prefs) in Validators::<T>::iter() {
assert_eq!(prefs.commission, Perbill::from_percent(50));
}
// Set the min commission to 75%
MinCommission::<T>::set(Perbill::from_percent(75));
}: _(RawOrigin::Root, 0)
verify {
for (_, prefs) in Validators::<T>::iter() {
assert_eq!(prefs.commission, Perbill::from_percent(75));
}
}

impl_benchmark_test_suite!(
Staking,
crate::mock::ExtBuilder::default().has_stakers(true),
Expand Down
22 changes: 22 additions & 0 deletions frame/staking/src/pallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1616,6 +1616,28 @@ pub mod pallet {
Self::chill_stash(&stash);
Ok(())
}

/// Force all validators to have at least the minimum commission. This will not affect
/// validators who already have a commission greater than or equal to the minimum.
///
/// Note that parameter `_validator_count` should be the upper bound of the number of
/// validators stored and is only used for weights.
#[pallet::weight(T::WeightInfo::force_apply_min_commission(*_validator_count))]
pub fn force_apply_min_commission(
origin: OriginFor<T>,
_validator_count: u32,
emostov marked this conversation as resolved.
Show resolved Hide resolved
) -> DispatchResult {
ensure_root(origin)?;
emostov marked this conversation as resolved.
Show resolved Hide resolved
let min_commission = MinCommission::<T>::get();
Validators::<T>::translate(|_, mut prefs: ValidatorPrefs| {
if prefs.commission < min_commission {
prefs.commission = min_commission
}
emostov marked this conversation as resolved.
Show resolved Hide resolved

Some(prefs)
});
Ok(())
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4711,3 +4711,39 @@ mod sorted_list_provider {
});
}
}

#[test]
fn force_apply_min_commission_works() {
let prefs = |c| ValidatorPrefs { commission: Perbill::from_percent(c), blocked: false };
let validators = || Validators::<Test>::iter().collect::<Vec<_>>();
ExtBuilder::default().build_and_execute(|| {
// Given all validators have a commission of 0
assert_eq!(validators(), vec![(31, prefs(0)), (21, prefs(0)), (11, prefs(0))]);
// and the min commission is 0
assert_eq!(MinCommission::<Test>::get(), Perbill::from_percent(0));

// When
assert_ok!(Staking::force_apply_min_commission(Origin::root(), 0));

// Then
assert_eq!(validators(), vec![(31, prefs(0)), (21, prefs(0)), (11, prefs(0))]);

// Given
assert_ok!(Staking::validate(Origin::signed(30), prefs(10)));

// When
assert_ok!(Staking::force_apply_min_commission(Origin::root(), 0));

// Then
assert_eq!(validators(), vec![(31, prefs(10)), (21, prefs(0)), (11, prefs(0))]);

// Given
MinCommission::<Test>::set(Perbill::from_percent(5));

// When
assert_ok!(Staking::force_apply_min_commission(Origin::root(), 0));

// Then
assert_eq!(validators(), vec![(31, prefs(10)), (21, prefs(5)), (11, prefs(5))]);
ggwpez marked this conversation as resolved.
Show resolved Hide resolved
});
}
Loading