Prevent dust in staking by disallowing cheap bond_extra (#7718)

* prevent bond_extra to cause staking actve lower than ed

* prevent bond_extra to cause staking actve lower than ed

* Check in post conditions.

* check rebond as well.

* also change withdraw_unbonded.

* Fix build

* change check format.

* Apply suggestions from code review

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Kian Paimani
2020-12-14 15:45:49 +00:00
committed by GitHub
parent a9ab45beae
commit 6b08b37bc9
3 changed files with 100 additions and 3 deletions
+82
View File
@@ -4696,3 +4696,85 @@ fn payout_to_any_account_works() {
assert!(Balances::free_balance(42) > 0);
})
}
#[test]
fn cannot_bond_extra_to_lower_than_ed() {
ExtBuilder::default()
.existential_deposit(10)
.build_and_execute(|| {
// stash must have more balance than bonded for this to work.
assert_eq!(Balances::free_balance(&21), 512_000);
// initial stuff.
assert_eq!(
Staking::ledger(&20).unwrap(),
StakingLedger {
stash: 21,
total: 1000,
active: 1000,
unlocking: vec![],
claimed_rewards: vec![]
}
);
// unbond all of it.
assert_ok!(Staking::unbond(Origin::signed(20), 1000));
assert_eq!(
Staking::ledger(&20).unwrap(),
StakingLedger {
stash: 21,
total: 1000,
active: 0,
unlocking: vec![UnlockChunk { value: 1000, era: 3 }],
claimed_rewards: vec![]
}
);
// now bond a wee bit more
assert_noop!(
Staking::bond_extra(Origin::signed(21), 5),
Error::<Test>::InsufficientValue,
);
})
}
#[test]
fn cannot_rebond_to_lower_than_ed() {
ExtBuilder::default()
.existential_deposit(10)
.build_and_execute(|| {
// stash must have more balance than bonded for this to work.
assert_eq!(Balances::free_balance(&21), 512_000);
// initial stuff.
assert_eq!(
Staking::ledger(&20).unwrap(),
StakingLedger {
stash: 21,
total: 1000,
active: 1000,
unlocking: vec![],
claimed_rewards: vec![]
}
);
// unbond all of it.
assert_ok!(Staking::unbond(Origin::signed(20), 1000));
assert_eq!(
Staking::ledger(&20).unwrap(),
StakingLedger {
stash: 21,
total: 1000,
active: 0,
unlocking: vec![UnlockChunk { value: 1000, era: 3 }],
claimed_rewards: vec![]
}
);
// now bond a wee bit more
assert_noop!(
Staking::rebond(Origin::signed(20), 5),
Error::<Test>::InsufficientValue,
);
})
}