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

Commit 0622d4c

Browse files
authored
Merge branch 'paritytech:master' into ddc-AFNPEV-T3
2 parents 54b3914 + 1b646b2 commit 0622d4c

File tree

7 files changed

+62
-123
lines changed

7 files changed

+62
-123
lines changed

Cargo.lock

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

bin/node/cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ sp-blockchain = { version = "4.0.0-dev", path = "../../../primitives/blockchain"
122122
futures = "0.3.16"
123123
tempfile = "3.1.0"
124124
assert_cmd = "2.0.2"
125-
nix = "0.19"
125+
nix = "0.23"
126126
serde_json = "1.0"
127127
regex = "1"
128128
platforms = "1.1"

frame/staking/src/benchmarking.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use frame_support::{
2828
traits::{Currency, CurrencyToVote, Get, Imbalance},
2929
};
3030
use sp_runtime::{
31-
traits::{StaticLookup, Zero},
31+
traits::{Bounded, One, StaticLookup, Zero},
3232
Perbill, Percent,
3333
};
3434
use sp_staking::SessionIndex;
@@ -38,7 +38,6 @@ pub use frame_benchmarking::{
3838
account, benchmarks, impl_benchmark_test_suite, whitelist_account, whitelisted_caller,
3939
};
4040
use frame_system::RawOrigin;
41-
use sp_runtime::traits::{Bounded, One};
4241

4342
const SEED: u32 = 0;
4443
const MAX_SPANS: u32 = 100;
@@ -695,7 +694,7 @@ benchmarks! {
695694
let stash = scenario.origin_stash1.clone();
696695

697696
add_slashing_spans::<T>(&stash, s);
698-
T::Currency::make_free_balance_be(&stash, T::Currency::minimum_balance());
697+
Ledger::<T>::insert(&controller, StakingLedger { active: T::Currency::minimum_balance() - One::one(), total: T::Currency::minimum_balance() - One::one(), ..Default::default() });
699698

700699
assert!(Bonded::<T>::contains_key(&stash));
701700
assert!(T::SortedListProvider::contains(&stash));

frame/staking/src/pallet/mod.rs

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,33 +1425,37 @@ pub mod pallet {
14251425
Ok(())
14261426
}
14271427

1428-
/// Remove all data structure concerning a staker/stash once its balance is at the minimum.
1429-
/// This is essentially equivalent to `withdraw_unbonded` except it can be called by anyone
1430-
/// and the target `stash` must have no funds left beyond the ED.
1428+
/// Remove all data structures concerning a staker/stash once it is at a state where it can
1429+
/// be considered `dust` in the staking system. The requirements are:
14311430
///
1432-
/// This can be called from any origin.
1431+
/// 1. the `total_balance` of the stash is below existential deposit.
1432+
/// 2. or, the `ledger.total` of the stash is below existential deposit.
14331433
///
1434-
/// - `stash`: The stash account to reap. Its balance must be zero.
1434+
/// The former can happen in cases like a slash; the latter when a fully unbonded account
1435+
/// is still receiving staking rewards in `RewardDestination::Staked`.
14351436
///
1436-
/// # <weight>
1437-
/// Complexity: O(S) where S is the number of slashing spans on the account.
1438-
/// DB Weight:
1439-
/// - Reads: Stash Account, Bonded, Slashing Spans, Locks
1440-
/// - Writes: Bonded, Slashing Spans (if S > 0), Ledger, Payee, Validators, Nominators,
1441-
/// Stash Account, Locks
1442-
/// - Writes Each: SpanSlash * S
1443-
/// # </weight>
1437+
/// It can be called by anyone, as long as `stash` meets the above requirements.
1438+
///
1439+
/// Refunds the transaction fees upon successful execution.
14441440
#[pallet::weight(T::WeightInfo::reap_stash(*num_slashing_spans))]
14451441
pub fn reap_stash(
1446-
_origin: OriginFor<T>,
1442+
origin: OriginFor<T>,
14471443
stash: T::AccountId,
14481444
num_slashing_spans: u32,
1449-
) -> DispatchResult {
1450-
let at_minimum = T::Currency::total_balance(&stash) == T::Currency::minimum_balance();
1451-
ensure!(at_minimum, Error::<T>::FundedTarget);
1445+
) -> DispatchResultWithPostInfo {
1446+
let _ = ensure_signed(origin)?;
1447+
1448+
let ed = T::Currency::minimum_balance();
1449+
let reapable = T::Currency::total_balance(&stash) < ed ||
1450+
Self::ledger(Self::bonded(stash.clone()).ok_or(Error::<T>::NotStash)?)
1451+
.map(|l| l.total)
1452+
.unwrap_or_default() < ed;
1453+
ensure!(reapable, Error::<T>::FundedTarget);
1454+
14521455
Self::kill_stash(&stash, num_slashing_spans)?;
14531456
T::Currency::remove_lock(STAKING_ID, &stash);
1454-
Ok(())
1457+
1458+
Ok(Pays::No.into())
14551459
}
14561460

14571461
/// Remove the given nominations from the calling validator.

frame/staking/src/tests.rs

Lines changed: 26 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1633,115 +1633,49 @@ fn reward_to_stake_works() {
16331633
}
16341634

16351635
#[test]
1636-
fn on_free_balance_zero_stash_removes_validator() {
1637-
// Tests that validator storage items are cleaned up when stash is empty
1638-
// Tests that storage items are untouched when controller is empty
1636+
fn reap_stash_works() {
16391637
ExtBuilder::default()
16401638
.existential_deposit(10)
16411639
.balance_factor(10)
16421640
.build_and_execute(|| {
1643-
// Check the balance of the validator account
1641+
// given
16441642
assert_eq!(Balances::free_balance(10), 10);
1645-
// Check the balance of the stash account
1646-
assert_eq!(Balances::free_balance(11), 10 * 1000);
1647-
// Check these two accounts are bonded
1648-
assert_eq!(Staking::bonded(&11), Some(10));
1649-
1650-
// Set payee information
1651-
assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Stash));
1652-
1653-
// Check storage items that should be cleaned up
1654-
assert!(<Ledger<Test>>::contains_key(&10));
1655-
assert!(<Bonded<Test>>::contains_key(&11));
1656-
assert!(<Validators<Test>>::contains_key(&11));
1657-
assert!(<Payee<Test>>::contains_key(&11));
1658-
1659-
// Reduce free_balance of controller to 0
1660-
let _ = Balances::slash(&10, Balance::max_value());
1661-
1662-
// Check the balance of the stash account has not been touched
16631643
assert_eq!(Balances::free_balance(11), 10 * 1000);
1664-
// Check these two accounts are still bonded
16651644
assert_eq!(Staking::bonded(&11), Some(10));
16661645

1667-
// Check storage items have not changed
16681646
assert!(<Ledger<Test>>::contains_key(&10));
16691647
assert!(<Bonded<Test>>::contains_key(&11));
16701648
assert!(<Validators<Test>>::contains_key(&11));
16711649
assert!(<Payee<Test>>::contains_key(&11));
16721650

1673-
// Reduce free_balance of stash to 0
1674-
let _ = Balances::slash(&11, Balance::max_value());
1675-
// Check total balance of stash
1676-
assert_eq!(Balances::total_balance(&11), 10);
1677-
1678-
// Reap the stash
1679-
assert_ok!(Staking::reap_stash(Origin::none(), 11, 0));
1680-
1681-
// Check storage items do not exist
1682-
assert!(!<Ledger<Test>>::contains_key(&10));
1683-
assert!(!<Bonded<Test>>::contains_key(&11));
1684-
assert!(!<Validators<Test>>::contains_key(&11));
1685-
assert!(!<Nominators<Test>>::contains_key(&11));
1686-
assert!(!<Payee<Test>>::contains_key(&11));
1687-
});
1688-
}
1689-
1690-
#[test]
1691-
fn on_free_balance_zero_stash_removes_nominator() {
1692-
// Tests that nominator storage items are cleaned up when stash is empty
1693-
// Tests that storage items are untouched when controller is empty
1694-
ExtBuilder::default()
1695-
.existential_deposit(10)
1696-
.balance_factor(10)
1697-
.build_and_execute(|| {
1698-
// Make 10 a nominator
1699-
assert_ok!(Staking::nominate(Origin::signed(10), vec![20]));
1700-
// Check that account 10 is a nominator
1701-
assert!(<Nominators<Test>>::contains_key(11));
1702-
// Check the balance of the nominator account
1703-
assert_eq!(Balances::free_balance(10), 10);
1704-
// Check the balance of the stash account
1705-
assert_eq!(Balances::free_balance(11), 10_000);
1706-
1707-
// Set payee information
1708-
assert_ok!(Staking::set_payee(Origin::signed(10), RewardDestination::Stash));
1709-
1710-
// Check storage items that should be cleaned up
1711-
assert!(<Ledger<Test>>::contains_key(&10));
1712-
assert!(<Bonded<Test>>::contains_key(&11));
1713-
assert!(<Nominators<Test>>::contains_key(&11));
1714-
assert!(<Payee<Test>>::contains_key(&11));
1715-
1716-
// Reduce free_balance of controller to 0
1717-
let _ = Balances::slash(&10, Balance::max_value());
1718-
// Check total balance of account 10
1719-
assert_eq!(Balances::total_balance(&10), 0);
1720-
1721-
// Check the balance of the stash account has not been touched
1722-
assert_eq!(Balances::free_balance(11), 10_000);
1723-
// Check these two accounts are still bonded
1724-
assert_eq!(Staking::bonded(&11), Some(10));
1725-
1726-
// Check storage items have not changed
1727-
assert!(<Ledger<Test>>::contains_key(&10));
1728-
assert!(<Bonded<Test>>::contains_key(&11));
1729-
assert!(<Nominators<Test>>::contains_key(&11));
1730-
assert!(<Payee<Test>>::contains_key(&11));
1651+
// stash is not reapable
1652+
assert_noop!(
1653+
Staking::reap_stash(Origin::signed(20), 11, 0),
1654+
Error::<Test>::FundedTarget
1655+
);
1656+
// controller or any other account is not reapable
1657+
assert_noop!(Staking::reap_stash(Origin::signed(20), 10, 0), Error::<Test>::NotStash);
17311658

1732-
// Reduce free_balance of stash to 0
1733-
let _ = Balances::slash(&11, Balance::max_value());
1734-
// Check total balance of stash
1735-
assert_eq!(Balances::total_balance(&11), 10);
1659+
// no easy way to cause an account to go below ED, we tweak their staking ledger
1660+
// instead.
1661+
Ledger::<Test>::insert(
1662+
10,
1663+
StakingLedger {
1664+
stash: 11,
1665+
total: 5,
1666+
active: 5,
1667+
unlocking: vec![],
1668+
claimed_rewards: vec![],
1669+
},
1670+
);
17361671

1737-
// Reap the stash
1738-
assert_ok!(Staking::reap_stash(Origin::none(), 11, 0));
1672+
// reap-able
1673+
assert_ok!(Staking::reap_stash(Origin::signed(20), 11, 0));
17391674

1740-
// Check storage items do not exist
1675+
// then
17411676
assert!(!<Ledger<Test>>::contains_key(&10));
17421677
assert!(!<Bonded<Test>>::contains_key(&11));
17431678
assert!(!<Validators<Test>>::contains_key(&11));
1744-
assert!(!<Nominators<Test>>::contains_key(&11));
17451679
assert!(!<Payee<Test>>::contains_key(&11));
17461680
});
17471681
}
@@ -2556,10 +2490,10 @@ fn garbage_collection_after_slashing() {
25562490

25572491
// reap_stash respects num_slashing_spans so that weight is accurate
25582492
assert_noop!(
2559-
Staking::reap_stash(Origin::none(), 11, 0),
2493+
Staking::reap_stash(Origin::signed(20), 11, 0),
25602494
Error::<Test>::IncorrectSlashingSpans
25612495
);
2562-
assert_ok!(Staking::reap_stash(Origin::none(), 11, 2));
2496+
assert_ok!(Staking::reap_stash(Origin::signed(20), 11, 2));
25632497

25642498
assert!(<Staking as crate::Store>::SlashingSpans::get(&11).is_none());
25652499
assert_eq!(<Staking as crate::Store>::SpanSlash::get(&(11, 0)).amount_slashed(), &0);

utils/frame/remote-externalities/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1616
jsonrpsee = { version = "0.4.1", features = ["ws-client", "macros"] }
1717

1818
env_logger = "0.9"
19-
frame-support = { path = "../../../frame/support", optional = true }
19+
frame-support = { path = "../../../frame/support", optional = true, version = "4.0.0-dev" }
2020
log = "0.4.11"
2121
codec = { package = "parity-scale-codec", version = "2.0.0" }
2222
serde_json = "1.0"

utils/wasm-builder/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]
1414

1515
[dependencies]
1616
build-helper = "0.1.1"
17-
cargo_metadata = "0.13.1"
17+
cargo_metadata = "0.14.1"
1818
tempfile = "3.1.0"
1919
toml = "0.5.4"
2020
walkdir = "2.3.2"

0 commit comments

Comments
 (0)