Skip to content

Commit

Permalink
Kill stash after unbonding all values
Browse files Browse the repository at this point in the history
- check the ledge after unbond and kill stash after unbond all values
  • Loading branch information
yanganto committed Jan 20, 2020
1 parent 6d64ecb commit b0aef5b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
21 changes: 21 additions & 0 deletions srml/staking/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,11 @@ decl_module! {
///
/// The dispatch origin for this call must be _Signed_ by the controller, not the stash.
///
/// After all pledged Ring and Kton are unbonded, the bonded accounts, namely stash and
/// controller, will also be unbended. Once user want to bond again, the `bond` method
/// should be called. If there are still pledged Ring or Kton and user want to bound more
/// values, the `bond_extra` method should be called.
///
/// # <weight>
/// - Independent of the arguments. Limited but potentially exploitable complexity.
/// - Contains a limited number of reads.
Expand Down Expand Up @@ -877,6 +882,22 @@ decl_module! {
}
},
}

let updated_ledger = Self::clear_mature_deposits(
Self::ledger(&controller).ok_or(err::CONTROLLER_INVALID)?);

let stash = ledger.stash.clone();
let StakingLedger {
active_ring,
active_deposit_ring,
active_kton,
..
} = updated_ledger;

// all bonded rings and ktons is withdrawing, then remove Ledger to save storage
if active_ring.is_zero() && active_deposit_ring.is_zero() && active_kton.is_zero() {
Self::kill_stash(&stash);
}
}

// TODO: doc
Expand Down
34 changes: 7 additions & 27 deletions srml/staking/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1993,37 +1993,17 @@ fn bond_with_no_staked_value() {
RewardDestination::Controller,
0,
));
// assert_eq!(Ring::locks(&1)[0].amount, 5);

assert_ok!(Staking::unbond(Origin::signed(2), StakingBalances::RingBalance(5)));
assert_eq!(
Staking::ledger(2),
Some(StakingLedger {
stash: 1,
active_ring: 0,
active_deposit_ring: 0,
active_kton: 0,
deposit_items: vec![],
ring_staking_lock: StakingLock {
staking_amount: 0,
unbondings: vec![NormalLock { amount: 5, until: 60 }],
},
kton_staking_lock: Default::default(),
}),
);

Timestamp::set_timestamp(BondingDuration::get() - 1);

// Not yet removed.
assert!(Staking::ledger(2).is_some());
// assert_eq!(Ring::locks(&1)[0].amount, 5);
match Ring::locks(&1)[0].withdraw_lock.clone() {
WithdrawLock::Normal(_) => panic!("lock type error"),
WithdrawLock::WithStaking(lock) => assert_eq!(lock.staking_amount, 5),
}

Timestamp::set_timestamp(BondingDuration::get());
assert_ok!(Staking::unbond(Origin::signed(2), StakingBalances::RingBalance(5)));

// FIXME
// Poof. Account 1 is removed from the staking system.
// assert!(Staking::ledger(2).is_none());
// assert_eq!(Ring::locks(&1).len(), 0);
// unbond all, auto remove the ledger
assert_eq!(Staking::ledger(2), None);
});
}

Expand Down

0 comments on commit b0aef5b

Please sign in to comment.