Skip to content

Commit

Permalink
[GPoS] Forbidden choosing the reward destination (#718)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmyyrroonn authored Jul 27, 2021
1 parent 6ade6d6 commit fbd9a53
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 191 deletions.
21 changes: 4 additions & 17 deletions cstrml/staking/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ pub fn create_stash_controller<T: Config>(n: u32) -> Result<(T::AccountId, T::Ac
let stash = create_funded_user::<T>("stash", n);
let controller = create_funded_user::<T>("controller", n);
let controller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(controller.clone());
let reward_destination = RewardDestination::Staked;
let amount = T::Currency::minimum_balance() * ACCOUNT_BALANCE_RATIO.into();
Staking::<T>::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount, reward_destination)?;
Staking::<T>::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount)?;
return Ok((stash, controller))
}

Expand Down Expand Up @@ -113,9 +112,8 @@ benchmarks! {
let stash = create_funded_user::<T>("stash",100);
let controller = create_funded_user::<T>("controller", 100);
let controller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(controller);
let reward_destination = RewardDestination::Staked;
let amount = T::Currency::minimum_balance() * 10u32.into();
}: _(RawOrigin::Signed(stash), controller_lookup, amount, reward_destination)
}: _(RawOrigin::Signed(stash), controller_lookup, amount)


bond_extra {
Expand All @@ -131,9 +129,8 @@ benchmarks! {
let controller = create_funded_user::<T>("controller", 100);
let controller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(controller.clone());
let max_additional = T::Currency::minimum_balance() * 10u32.into();
let reward_destination = RewardDestination::Staked;
let amount = T::Currency::minimum_balance() * 10u32.into();
Staking::<T>::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount, reward_destination)?;
Staking::<T>::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount)?;
}: _(RawOrigin::Signed(controller), max_additional)


Expand All @@ -143,9 +140,8 @@ benchmarks! {
let controller = create_funded_user::<T>("controller", 100);
let controller_lookup: <T::Lookup as StaticLookup>::Source = T::Lookup::unlookup(controller.clone());
let max_additional = T::Currency::minimum_balance() * 10u32.into();
let reward_destination = RewardDestination::Staked;
let amount = T::Currency::minimum_balance() * 10u32.into();
Staking::<T>::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount, reward_destination)?;
Staking::<T>::bond(RawOrigin::Signed(stash.clone()).into(), controller_lookup, amount)?;
Staking::<T>::unbond(RawOrigin::Signed(controller.clone()).into(), max_additional)?;
}: _(RawOrigin::Signed(controller.clone()))

Expand Down Expand Up @@ -175,14 +171,6 @@ benchmarks! {
let (_, controller) = create_stash_controller::<T>(100)?;
}: _(RawOrigin::Signed(controller))

set_payee {
let (stash, controller) = create_stash_controller::<T>(100)?;
assert_eq!(Payee::<T>::get(&stash), RewardDestination::Staked);
}: _(RawOrigin::Signed(controller), RewardDestination::Controller)
verify {
assert_eq!(Payee::<T>::get(&stash), RewardDestination::Controller);
}

set_controller {
let (stash, _) = create_stash_controller::<T>(100)?;
let new_controller = create_funded_user::<T>("new_controller", 100);
Expand Down Expand Up @@ -252,7 +240,6 @@ mod tests {
assert_ok!(test_benchmark_new_era::<Test>());
assert_ok!(test_benchmark_select_and_update_validators::<Test>());
assert_ok!(test_benchmark_chill::<Test>());
assert_ok!(test_benchmark_set_payee::<Test>());
assert_ok!(test_benchmark_set_controller::<Test>());
assert_ok!(test_benchmark_withdraw_unbonded::<Test>());
});
Expand Down
31 changes: 3 additions & 28 deletions cstrml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,7 @@ decl_storage! {
let _ = <Module<T>>::bond(
T::Origin::from(Some(stash.clone()).into()),
T::Lookup::unlookup(controller.clone()),
balance,
RewardDestination::Staked,
balance
);

gensis_total_stakes += balance;
Expand Down Expand Up @@ -847,8 +846,7 @@ decl_module! {
#[weight = T::WeightInfo::bond()]
fn bond(origin,
controller: <T::Lookup as StaticLookup>::Source,
#[compact] value: BalanceOf<T>,
payee: RewardDestination<T::AccountId>
#[compact] value: BalanceOf<T>
) {
let stash = ensure_signed(origin)?;

Expand All @@ -870,7 +868,7 @@ decl_module! {
// You're auto-bonded forever, here. We might improve this by only bonding when
// you actually validate/guarantee and remove once you unbond __everything__.
<Bonded<T>>::insert(&stash, &controller);
<Payee<T>>::insert(&stash, payee);
<Payee<T>>::insert(&stash, RewardDestination::Staked);

let current_era = CurrentEra::get().unwrap_or(0);
let history_depth = Self::history_depth();
Expand Down Expand Up @@ -1220,29 +1218,6 @@ decl_module! {
Self::deposit_event(RawEvent::ChillSuccess(controller, ledger.stash));
}

/// (Re-)set the payment target for a controller.
///
/// Effects will be felt at the beginning of the next era.
///
/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
///
/// # <weight>
/// - Independent of the arguments. Insignificant complexity.
/// - Contains a limited number of reads.
/// - Writes are limited to the `origin` account key.
/// ---------
/// - DB Weight:
/// - Read: Ledger
/// - Write: Payee
/// # </weight>
#[weight = T::WeightInfo::set_payee()]
fn set_payee(origin, payee: RewardDestination<T::AccountId>) {
let controller = ensure_signed(origin)?;
let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
let stash = &ledger.stash;
<Payee<T>>::insert(stash, payee);
}

/// (Re-)set the controller of a stash.
///
/// Effects will be felt at the beginning of the next era.
Expand Down
15 changes: 11 additions & 4 deletions cstrml/staking/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,9 @@ pub fn bond_validator(acc: u128, val: Balance) {
assert_ok!(Staking::bond(
Origin::signed(acc + 1),
acc,
val,
RewardDestination::Controller
val
));
assert_ok!(set_payee(acc, RewardDestination::Controller));
Staking::upsert_stake_limit(&(acc + 1), u128::max_value());
assert_ok!(Staking::validate(Origin::signed(acc), ValidatorPrefs::default()));
}
Expand All @@ -623,9 +623,9 @@ pub fn bond_guarantor(acc: u128, val: Balance, targets: Vec<(u128, Balance)>) {
assert_ok!(Staking::bond(
Origin::signed(acc + 1),
acc,
val,
RewardDestination::Controller
val
));
assert_ok!(set_payee(acc, RewardDestination::Controller));
for target in targets {
assert_ok!(Staking::guarantee(Origin::signed(acc), target));
}
Expand Down Expand Up @@ -769,4 +769,11 @@ pub fn staking_rewards_in_era(era_index: EraIndex) -> BalanceOf<Test> {
let total_reward = Staking::total_rewards_in_era(era_index);
let authoring_reward = authoring_rewards_in_era(era_index);
total_reward.saturating_sub(authoring_reward)
}

pub fn set_payee(controller: u128, payee: RewardDestination<u128>) -> Result<(), ()> {
let ledger = Staking::ledger(&controller).unwrap();
let stash = &ledger.stash;
<Payee<Test>>::insert(stash, payee);
Ok(())
}
Loading

0 comments on commit fbd9a53

Please sign in to comment.