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

Commit

Permalink
pallet-staking: Add extrinsic force_apply_min_commission (#10786)
Browse files Browse the repository at this point in the history
* pallet-staking: Add extrinsic `force_apply_min_commission`

* Add benchmarks

* cargo run --quiet --profile=production  --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

* Bound iteration by  max_validator_count

* cargo run --quiet --profile=production  --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

* Only apply to 1 validator

* Update doc comments

* Uncomment tests

* cargo run --quiet --profile=production  --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

* Accept signed origins

* cargo run --quiet --profile=production  --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

* Remove contains_key check

* Add test for try_mutate_exists

* Impove try_mutate_exists docs

* Delete redundant try_mutate_exists tests;

* Delete residual from removed test

* cargo run --quiet --profile=production  --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

* Return an error when the stash does not exist

* Update try_mutate_exist doc wording

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

* Apply suggestions from code review

Co-authored-by: Parity Bot <[email protected]>
Co-authored-by: Shawn Tabrizi <[email protected]>
  • Loading branch information
3 people authored Feb 10, 2022
1 parent 9a6d706 commit 27b8806
Show file tree
Hide file tree
Showing 10 changed files with 225 additions and 110 deletions.
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))]);

// 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

0 comments on commit 27b8806

Please sign in to comment.