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 all 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
29 changes: 29 additions & 0 deletions frame/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,35 @@ benchmarks! {
assert!(!T::SortedListProvider::contains(&stash));
}

force_apply_min_commission {
// Clean up any existing state
clear_validators_and_nominators::<T>();

// Create a validator with a commission of 50%
let (stash, controller) =
create_stash_controller::<T>(1, 1, RewardDestination::Staked)?;
let validator_prefs =
ValidatorPrefs { commission: Perbill::from_percent(50), ..Default::default() };
Staking::<T>::validate(RawOrigin::Signed(controller).into(), validator_prefs)?;

// Sanity check
assert_eq!(
Validators::<T>::get(&stash),
ValidatorPrefs { commission: Perbill::from_percent(50), ..Default::default() }
);

// Set the min commission to 75%
MinCommission::<T>::set(Perbill::from_percent(75));
let caller = whitelisted_caller();
}: _(RawOrigin::Signed(caller), stash.clone())
verify {
// The validators commission has been bumped to 75%
assert_eq!(
Validators::<T>::get(&stash),
ValidatorPrefs { commission: Perbill::from_percent(75), ..Default::default() }
);
}

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 a validator to have at least the minimum commission. This will not affect a
/// validator who already has a commission greater than or equal to the minimum. Any account
/// can call this.
#[pallet::weight(T::WeightInfo::force_apply_min_commission())]
pub fn force_apply_min_commission(
origin: OriginFor<T>,
validator_stash: T::AccountId,
) -> DispatchResult {
ensure_signed(origin)?;
let min_commission = MinCommission::<T>::get();
Validators::<T>::try_mutate_exists(validator_stash, |maybe_prefs| {
maybe_prefs
.as_mut()
.map(|prefs| {
(prefs.commission < min_commission)
.then(|| prefs.commission = min_commission)
})
.ok_or(Error::<T>::NotStash)
})?;
Ok(())
}
}
}

Expand Down
35 changes: 35 additions & 0 deletions frame/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4711,3 +4711,38 @@ 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(|| {
assert_ok!(Staking::validate(Origin::signed(30), prefs(10)));
assert_ok!(Staking::validate(Origin::signed(20), prefs(5)));

// Given
assert_eq!(validators(), vec![(31, prefs(10)), (21, prefs(5)), (11, prefs(0))]);
MinCommission::<Test>::set(Perbill::from_percent(5));

// When applying to a commission greater than min
assert_ok!(Staking::force_apply_min_commission(Origin::signed(1), 31));
// Then the commission is not changed
assert_eq!(validators(), vec![(31, prefs(10)), (21, prefs(5)), (11, prefs(0))]);

// When applying to a commission that is equal to min
assert_ok!(Staking::force_apply_min_commission(Origin::signed(1), 21));
// Then the commission is not changed
assert_eq!(validators(), vec![(31, prefs(10)), (21, prefs(5)), (11, prefs(0))]);

// When applying to a commission that is less than the min
assert_ok!(Staking::force_apply_min_commission(Origin::signed(1), 11));
// Then the commission is bumped to the min
assert_eq!(validators(), vec![(31, prefs(10)), (21, prefs(5)), (11, prefs(5))]);
ggwpez marked this conversation as resolved.
Show resolved Hide resolved

// When applying commission to a validator that doesn't exist then storage is not altered
assert_noop!(
Staking::force_apply_min_commission(Origin::signed(1), 420),
Error::<Test>::NotStash
);
});
}
Loading