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

Commit e578af7

Browse files
authored
Support Staking Payout to Any Account (#6832)
* Support staking payout to any account * fix offences benchmarks
1 parent f505e67 commit e578af7

File tree

3 files changed

+43
-12
lines changed

3 files changed

+43
-12
lines changed

frame/offences/benchmarking/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ fn create_offender<T: Trait>(n: u32, nominators: u32) -> Result<Offender<T>, &'s
125125
RawOrigin::Signed(nominator_stash.clone()).into(),
126126
nominator_controller_lookup.clone(),
127127
amount.clone(),
128-
reward_destination,
128+
reward_destination.clone(),
129129
)?;
130130

131131
let selected_validators: Vec<LookupSourceOf<T>> = vec![controller_lookup.clone()];

frame/staking/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -425,16 +425,18 @@ pub enum StakerStatus<AccountId> {
425425

426426
/// A destination account for payment.
427427
#[derive(PartialEq, Eq, Copy, Clone, Encode, Decode, RuntimeDebug)]
428-
pub enum RewardDestination {
428+
pub enum RewardDestination<AccountId> {
429429
/// Pay into the stash account, increasing the amount at stake accordingly.
430430
Staked,
431431
/// Pay into the stash account, not increasing the amount at stake.
432432
Stash,
433433
/// Pay into the controller account.
434434
Controller,
435+
/// Pay into a specified account.
436+
Account(AccountId),
435437
}
436438

437-
impl Default for RewardDestination {
439+
impl<AccountId> Default for RewardDestination<AccountId> {
438440
fn default() -> Self {
439441
RewardDestination::Staked
440442
}
@@ -1049,7 +1051,7 @@ decl_storage! {
10491051
=> Option<StakingLedger<T::AccountId, BalanceOf<T>>>;
10501052

10511053
/// Where the reward payment should be made. Keyed by stash.
1052-
pub Payee get(fn payee): map hasher(twox_64_concat) T::AccountId => RewardDestination;
1054+
pub Payee get(fn payee): map hasher(twox_64_concat) T::AccountId => RewardDestination<T::AccountId>;
10531055

10541056
/// The map from (wannabe) validator stash key to the preferences of that validator.
10551057
pub Validators get(fn validators):
@@ -1496,7 +1498,7 @@ decl_module! {
14961498
pub fn bond(origin,
14971499
controller: <T::Lookup as StaticLookup>::Source,
14981500
#[compact] value: BalanceOf<T>,
1499-
payee: RewardDestination,
1501+
payee: RewardDestination<T::AccountId>,
15001502
) {
15011503
let stash = ensure_signed(origin)?;
15021504

@@ -1830,7 +1832,7 @@ decl_module! {
18301832
/// - Write: Payee
18311833
/// # </weight>
18321834
#[weight = 11 * WEIGHT_PER_MICROS + T::DbWeight::get().reads_writes(1, 1)]
1833-
fn set_payee(origin, payee: RewardDestination) {
1835+
fn set_payee(origin, payee: RewardDestination<T::AccountId>) {
18341836
let controller = ensure_signed(origin)?;
18351837
let ledger = Self::ledger(&controller).ok_or(Error::<T>::NotController)?;
18361838
let stash = &ledger.stash;
@@ -2489,6 +2491,9 @@ impl<T: Trait> Module<T> {
24892491
Self::update_ledger(&controller, &l);
24902492
r
24912493
}),
2494+
RewardDestination::Account(dest_account) => {
2495+
Some(T::Currency::deposit_creating(&dest_account, amount))
2496+
}
24922497
}
24932498
}
24942499

frame/staking/src/tests.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4359,7 +4359,7 @@ fn test_payout_stakers() {
43594359
// We also test that `payout_extra_nominators` works.
43604360
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
43614361
let balance = 1000;
4362-
// Create three validators:
4362+
// Create a validator:
43634363
bond_validator(11, 10, balance); // Default(64)
43644364

43654365
// Create nominators, targeting stash of validators
@@ -4597,15 +4597,12 @@ fn on_initialize_weight_is_correct() {
45974597
});
45984598
}
45994599

4600-
46014600
#[test]
46024601
fn payout_creates_controller() {
4603-
// Here we will test validator can set `max_nominators_payout` and it works.
4604-
// We also test that `payout_extra_nominators` works.
46054602
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
46064603
let balance = 1000;
4607-
// Create three validators:
4608-
bond_validator(11, 10, balance); // Default(64)
4604+
// Create a validator:
4605+
bond_validator(11, 10, balance);
46094606

46104607
// Create a stash/controller pair
46114608
bond_nominator(1234, 1337, 100, vec![11]);
@@ -4626,3 +4623,32 @@ fn payout_creates_controller() {
46264623
assert!(Balances::free_balance(1337) > 0);
46274624
})
46284625
}
4626+
4627+
#[test]
4628+
fn payout_to_any_account_works() {
4629+
ExtBuilder::default().has_stakers(false).build_and_execute(|| {
4630+
let balance = 1000;
4631+
// Create a validator:
4632+
bond_validator(11, 10, balance); // Default(64)
4633+
4634+
// Create a stash/controller pair
4635+
bond_nominator(1234, 1337, 100, vec![11]);
4636+
4637+
// Update payout location
4638+
assert_ok!(Staking::set_payee(Origin::signed(1337), RewardDestination::Account(42)));
4639+
4640+
// Reward Destination account doesn't exist
4641+
assert_eq!(Balances::free_balance(42), 0);
4642+
4643+
mock::start_era(1);
4644+
Staking::reward_by_ids(vec![(11, 1)]);
4645+
// Compute total payout now for whole duration as other parameter won't change
4646+
let total_payout_0 = current_total_payout_for_duration(3 * 1000);
4647+
assert!(total_payout_0 > 100); // Test is meaningful if reward something
4648+
mock::start_era(2);
4649+
assert_ok!(Staking::payout_stakers(Origin::signed(1337), 11, 1));
4650+
4651+
// Payment is successful
4652+
assert!(Balances::free_balance(42) > 0);
4653+
})
4654+
}

0 commit comments

Comments
 (0)