From b0aef5b59db22fd329e70703461d943aa36a54ba Mon Sep 17 00:00:00 2001 From: yanganto Date: Sat, 18 Jan 2020 18:42:39 +0800 Subject: [PATCH] Kill stash after unbonding all values - check the ledge after unbond and kill stash after unbond all values --- srml/staking/src/lib.rs | 21 +++++++++++++++++++++ srml/staking/src/tests.rs | 34 +++++++--------------------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/srml/staking/src/lib.rs b/srml/staking/src/lib.rs index a1ba96474..a098b9604 100644 --- a/srml/staking/src/lib.rs +++ b/srml/staking/src/lib.rs @@ -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. + /// /// # /// - Independent of the arguments. Limited but potentially exploitable complexity. /// - Contains a limited number of reads. @@ -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 diff --git a/srml/staking/src/tests.rs b/srml/staking/src/tests.rs index 423d2b5cb..3dbe8cf7a 100644 --- a/srml/staking/src/tests.rs +++ b/srml/staking/src/tests.rs @@ -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); }); }