Introduce safe types for handling imbalances (#2048)

* Be a little safer with total issuance.

* PairT instead of _Pair

* Remove rev causing upset

* Remove fees stuff.

* Fix build (including tests)

* Update runtime, bump version

* Fix

* Handle gas refunds properly.

* Rename identifier

ala #2025

* Address grumbles

* New not-quite-linear-typing API

* Slimmer API

* More linear-type test fixes

* Fix tests

* Tidy

* Fix some grumbles

* Keep unchecked functions private

* Remove another less-than-safe currency function and ensure that
contracts module can never create cash.

* Address a few grumbles and fix tests
This commit is contained in:
Gav Wood
2019-03-20 14:07:28 +01:00
committed by Robert Habermeier
parent f9e224e7b8
commit dcd77a147c
49 changed files with 1108 additions and 1100 deletions
+26 -28
View File
@@ -103,7 +103,7 @@ fn invulnerability_should_work() {
// Make account 10 invulnerable
assert_ok!(Staking::set_invulnerables(vec![10]));
// Give account 10 some funds
Balances::set_free_balance(&10, 70);
let _ = Balances::deposit_creating(&10, 69);
// There is no slash grace -- slash immediately.
assert_eq!(Staking::offline_slash_grace(), 0);
// Account 10 has not been slashed
@@ -132,7 +132,7 @@ fn offline_should_slash_and_kick() {
// Test that an offline validator gets slashed and kicked
with_externalities(&mut ExtBuilder::default().build(), || {
// Give account 10 some balance
Balances::set_free_balance(&10, 1000);
let _ = Balances::deposit_creating(&10, 999);
// Confirm account 10 is a validator
assert!(<Validators<Test>>::exists(&10));
// Validators get slashed immediately
@@ -163,7 +163,7 @@ fn offline_grace_should_delay_slashing() {
// Tests that with grace, slashing is delayed
with_externalities(&mut ExtBuilder::default().build(), || {
// Initialize account 10 with balance
Balances::set_free_balance(&10, 70);
let _ = Balances::deposit_creating(&10, 69);
// Verify account 10 has balance
assert_eq!(Balances::free_balance(&10), 70);
@@ -204,8 +204,8 @@ fn max_unstake_threshold_works() {
with_externalities(&mut ExtBuilder::default().build(), || {
const MAX_UNSTAKE_THRESHOLD: u32 = 10;
// Two users with maximum possible balance
Balances::set_free_balance(&10, u64::max_value());
Balances::set_free_balance(&20, u64::max_value());
let _ = Balances::deposit_creating(&10, u64::max_value() - 1);
let _ = Balances::deposit_creating(&20, u64::max_value() - 1);
// Give them full exposer as a staker
<Stakers<Test>>::insert(&10, Exposure { total: u64::max_value(), own: u64::max_value(), others: vec![]});
@@ -253,9 +253,6 @@ fn max_unstake_threshold_works() {
fn slashing_does_not_cause_underflow() {
// Tests that slashing more than a user has does not underflow
with_externalities(&mut ExtBuilder::default().build(), || {
// One user with less than `max_value` will test underflow does not occur
Balances::set_free_balance(&10, 1);
// Verify initial conditions
assert_eq!(Balances::free_balance(&10), 1);
assert_eq!(Staking::offline_slash_grace(), 0);
@@ -449,7 +446,7 @@ fn staking_should_work() {
assert_eq!(Staking::bonding_duration(), 2);
// put some money in account that we'll use.
for i in 1..5 { Balances::set_free_balance(&i, 1000); }
for i in 1..5 { let _ = Balances::deposit_creating(&i, 1000); }
// bond one account pair and state interest in nomination.
// this is needed to keep 10 and 20 in the validator list with phragmen
@@ -628,9 +625,9 @@ fn nominating_and_rewards_should_work() {
// give the man some money
let initial_balance = 1000;
for i in 1..5 { Balances::set_free_balance(&i, initial_balance); }
Balances::set_free_balance(&10, initial_balance);
Balances::set_free_balance(&20, initial_balance);
for i in [1, 2, 3, 4, 5, 10, 20].iter() {
let _ = Balances::deposit_creating(i, initial_balance - Balances::total_balance(i));
}
// record their balances.
for i in 1..5 { assert_eq!(Balances::total_balance(&i), initial_balance); }
@@ -705,8 +702,9 @@ fn nominators_also_get_slashed() {
// give the man some money.
let initial_balance = 1000;
for i in 1..3 { Balances::set_free_balance(&i, initial_balance); }
Balances::set_free_balance(&10, initial_balance);
for i in [1, 2, 3, 10].iter() {
let _ = Balances::deposit_creating(i, initial_balance - Balances::total_balance(i));
}
// 2 will nominate for 10
let nominator_stake = 500;
@@ -843,7 +841,7 @@ fn cannot_transfer_staked_balance() {
assert_noop!(Balances::transfer(Origin::signed(11), 20, 1), "account liquidity restrictions prevent withdrawal");
// Give account 11 extra free balance
Balances::set_free_balance(&11, 10000);
let _ = Balances::deposit_creating(&11, 9999);
// Confirm that account 11 can now transfer some balance
assert_ok!(Balances::transfer(Origin::signed(11), 20, 1));
});
@@ -863,7 +861,7 @@ fn cannot_reserve_staked_balance() {
assert_noop!(Balances::reserve(&11, 1), "account liquidity restrictions prevent withdrawal");
// Give account 11 extra free balance
Balances::set_free_balance(&11, 10000);
let _ = Balances::deposit_creating(&11, 9990);
// Confirm account 11 can now reserve balance
assert_ok!(Balances::reserve(&11, 1));
});
@@ -1041,7 +1039,7 @@ fn bond_extra_works() {
assert_eq!(Staking::ledger(&10), Some(StakingLedger { stash: 11, total: 1000, active: 1000, unlocking: vec![] }));
// Give account 11 some large free balance greater than total
Balances::set_free_balance(&11, 1000000);
let _ = Balances::deposit_creating(&11, 999000);
// Check the balance of the stash account
assert_eq!(Balances::free_balance(&11), 1000000);
@@ -1077,7 +1075,7 @@ fn bond_extra_and_withdraw_unbonded_works() {
assert_ok!(Staking::set_bonding_duration(2));
// Give account 11 some large free balance greater than total
Balances::set_free_balance(&11, 1000000);
let _ = Balances::deposit_creating(&11, 999000);
// Check the balance of the stash account
assert_eq!(Balances::free_balance(&11), 1000000);
@@ -1175,8 +1173,8 @@ fn slot_stake_is_least_staked_validator_and_limits_maximum_punishment() {
assert_eq!(Staking::stakers(&20).total, 2000);
// Give the man some money.
Balances::set_free_balance(&10, 1000);
Balances::set_free_balance(&20, 1000);
let _ = Balances::deposit_creating(&10, 999);
let _ = Balances::deposit_creating(&20, 999);
// Confirm initial free balance.
assert_eq!(Balances::free_balance(&10), 1000);
@@ -1247,7 +1245,7 @@ fn on_free_balance_zero_stash_removes_validator() {
assert!(<Payee<Test>>::exists(&10));
// Reduce free_balance of controller to 0
Balances::set_free_balance(&10, 0);
Balances::slash(&10, u64::max_value());
// Check total balance of account 10
assert_eq!(Balances::total_balance(&10), 0);
@@ -1263,7 +1261,7 @@ fn on_free_balance_zero_stash_removes_validator() {
assert!(<Payee<Test>>::exists(&10));
// Reduce free_balance of stash to 0
Balances::set_free_balance(&11, 0);
Balances::slash(&11, u64::max_value());
// Check total balance of stash
assert_eq!(Balances::total_balance(&11), 0);
@@ -1306,7 +1304,7 @@ fn on_free_balance_zero_stash_removes_nominator() {
assert!(<Payee<Test>>::exists(&10));
// Reduce free_balance of controller to 0
Balances::set_free_balance(&10, 0);
Balances::slash(&10, u64::max_value());
// Check total balance of account 10
assert_eq!(Balances::total_balance(&10), 0);
@@ -1321,7 +1319,7 @@ fn on_free_balance_zero_stash_removes_nominator() {
assert!(<Payee<Test>>::exists(&10));
// Reduce free_balance of stash to 0
Balances::set_free_balance(&11, 0);
Balances::slash(&11, u64::max_value());
// Check total balance of stash
assert_eq!(Balances::total_balance(&11), 0);
@@ -1382,7 +1380,7 @@ fn phragmen_poc_works() {
// bond [2,1](A), [4,3](B), [6,5](C) as the 3 nominators
// Give all of them some balance to be able to bond properly.
for i in &[1, 3, 5] { Balances::set_free_balance(i, 50); }
for i in &[1, 3, 5] { let _ = Balances::deposit_creating(i, 50); }
// Linking names to the above test:
// 10 => X
// 20 => Y
@@ -1439,7 +1437,7 @@ fn phragmen_election_works() {
// bond [2,1](A), [4,3](B), as 2 nominators
// Give all of them some balance to be able to bond properly.
for i in &[1, 3] { Balances::set_free_balance(i, 50); }
for i in &[1, 3] { let _ = Balances::deposit_creating(i, 50); }
assert_ok!(Staking::bond(Origin::signed(1), 2, 5, RewardDestination::default()));
assert_ok!(Staking::nominate(Origin::signed(2), vec![10, 20]));
@@ -1499,7 +1497,7 @@ fn switching_roles() {
assert_eq!(Session::validators(), vec![20, 10]);
// put some money in account that we'll use.
for i in 1..7 { Balances::set_free_balance(&i, 5000); }
for i in 1..7 { let _ = Balances::deposit_creating(&i, 5000); }
// add 2 nominators
assert_ok!(Staking::bond(Origin::signed(1), 2, 2000, RewardDestination::default()));
@@ -1567,7 +1565,7 @@ fn wrong_vote_is_null() {
assert_eq!(Session::validators(), vec![40, 30, 20, 10]);
// put some money in account that we'll use.
for i in 1..3 { Balances::set_free_balance(&i, 5000); }
for i in 1..3 { let _ = Balances::deposit_creating(&i, 5000); }
// add 1 nominators
assert_ok!(Staking::bond(Origin::signed(1), 2, 2000, RewardDestination::default()));