Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
3a11e89
[Enhancement] Convert fast-unstake to use StakingInterface, decouplin…
ruseinov Oct 5, 2022
4f39589
Update primitives/staking/src/lib.rs
ruseinov Oct 6, 2022
9b8e123
Update primitives/staking/src/lib.rs
ruseinov Oct 6, 2022
25520d6
fix validator comment
ruseinov Oct 6, 2022
ad90903
remove todo
ruseinov Oct 10, 2022
89b32b5
ideas from Kian (#12474)
ruseinov Oct 11, 2022
f51c073
Merge remote-tracking branch 'origin/master' into ru/feature/fast-uns…
Oct 11, 2022
41d04ad
Rename StakingInterface -> Staking for nomination-pools
ruseinov Oct 12, 2022
7fc52f8
Staking fixes
ruseinov Oct 12, 2022
d3958ba
StakingInterface changes
ruseinov Oct 12, 2022
2ff689c
fix fast-unstake
ruseinov Oct 12, 2022
1fc6510
fix nomination-pools
ruseinov Oct 12, 2022
28d15d7
Merge remote-tracking branch 'origin/master' into ru/feature/fast-uns…
Oct 18, 2022
0705029
Fix fast-unstake tests
ruseinov Oct 18, 2022
39cf580
Fix benches for fast-unstake
ruseinov Oct 18, 2022
8093567
fix is_unbonding
ruseinov Oct 18, 2022
30a5882
fix nomination pools
ruseinov Oct 18, 2022
37107ef
fix node code
ruseinov Oct 19, 2022
097a9d1
add mock comments
ruseinov Oct 19, 2022
613a701
fix imports
ruseinov Oct 19, 2022
e785dd5
remove todo
ruseinov Oct 19, 2022
03586d7
more fixes
ruseinov Oct 19, 2022
20a4f29
more fixes
ruseinov Oct 19, 2022
9807cf2
bench fixes
ruseinov Oct 19, 2022
3639af8
more fixes
ruseinov Oct 19, 2022
b7827d5
more fixes
ruseinov Oct 20, 2022
5232b99
import fix
ruseinov Oct 20, 2022
f881456
more fixes
ruseinov Oct 20, 2022
c3cee71
more bench fix
ruseinov Oct 20, 2022
068c565
refix
ruseinov Oct 20, 2022
ff36f90
refix
ruseinov Oct 20, 2022
6f862e4
Update primitives/staking/src/lib.rs
ruseinov Oct 25, 2022
bff58a4
is_unbonding returns a result
ruseinov Oct 20, 2022
bdb525e
fix
ruseinov Oct 25, 2022
4bd57b1
review fixes
ruseinov Oct 25, 2022
f6040ec
more review fixes
ruseinov Oct 25, 2022
8c5604d
more fixes
ruseinov Oct 25, 2022
731fab2
more fixes
ruseinov Oct 25, 2022
b90082a
Update frame/fast-unstake/src/benchmarking.rs
ruseinov Oct 26, 2022
1eee8a5
remove redundant CurrencyBalance from nom-pools
ruseinov Oct 26, 2022
1f4fad2
remove CB
ruseinov Oct 26, 2022
27530dc
Merge remote-tracking branch 'origin/master' into ru/feature/fast-uns…
Oct 26, 2022
90cec9f
rephrase
ruseinov Oct 26, 2022
b1560aa
Apply suggestions from code review
kianenigma Oct 27, 2022
75a36bb
Update frame/nomination-pools/src/tests.rs
kianenigma Oct 27, 2022
b3e843e
finish damn renamed
kianenigma Oct 27, 2022
963837b
clippy fix
ruseinov Oct 27, 2022
cbed2f9
fix
ruseinov Oct 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix nomination pools
  • Loading branch information
ruseinov committed Oct 18, 2022
commit 30a5882ae2c3671fe7435cc895526f34de878d8a
90 changes: 60 additions & 30 deletions frame/nomination-pools/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{self as pools};
use frame_support::{assert_ok, parameter_types, PalletId};
use frame_system::RawOrigin;
use sp_runtime::FixedU128;
use sp_staking::Stake;

pub type BlockNumber = u64;
pub type AccountId = u128;
Expand Down Expand Up @@ -47,10 +48,17 @@ impl sp_staking::StakingInterface for StakingMock {
type Balance = Balance;
type AccountId = AccountId;

fn minimum_bond() -> Self::Balance {
fn minimum_nominator_bond() -> Self::Balance {
StakingMinBond::get()
}
fn minimum_validator_bond() -> Self::Balance {
StakingMinBond::get()
}

fn desired_validator_count() -> u32 {
todo!()
}

fn current_era() -> EraIndex {
CurrentEra::get()
}
Expand All @@ -59,39 +67,24 @@ impl sp_staking::StakingInterface for StakingMock {
BondingDuration::get()
}

fn active_stake(who: &Self::AccountId) -> Option<Self::Balance> {
BondedBalanceMap::get().get(who).map(|v| *v)
}

fn total_stake(who: &Self::AccountId) -> Option<Self::Balance> {
match (
UnbondingBalanceMap::get().get(who).map(|v| *v),
BondedBalanceMap::get().get(who).map(|v| *v),
) {
(None, None) => None,
(Some(v), None) | (None, Some(v)) => Some(v),
(Some(a), Some(b)) => Some(a + b),
}
}

fn bond_extra(who: Self::AccountId, extra: Self::Balance) -> DispatchResult {
fn bond_extra(who: &Self::AccountId, extra: Self::Balance) -> DispatchResult {
let mut x = BondedBalanceMap::get();
x.get_mut(&who).map(|v| *v += extra);
x.get_mut(who).map(|v| *v += extra);
BondedBalanceMap::set(&x);
Ok(())
}

fn unbond(who: Self::AccountId, amount: Self::Balance) -> DispatchResult {
fn unbond(who: &Self::AccountId, amount: Self::Balance) -> DispatchResult {
let mut x = BondedBalanceMap::get();
*x.get_mut(&who).unwrap() = x.get_mut(&who).unwrap().saturating_sub(amount);
*x.get_mut(who).unwrap() = x.get_mut(who).unwrap().saturating_sub(amount);
BondedBalanceMap::set(&x);
let mut y = UnbondingBalanceMap::get();
*y.entry(who).or_insert(Self::Balance::zero()) += amount;
*y.entry(who.clone()).or_insert(Self::Balance::zero()) += amount;
UnbondingBalanceMap::set(&y);
Ok(())
}

fn chill(_: Self::AccountId) -> sp_runtime::DispatchResult {
fn chill(_: &Self::AccountId) -> sp_runtime::DispatchResult {
Ok(())
}

Expand All @@ -104,17 +97,12 @@ impl sp_staking::StakingInterface for StakingMock {
Ok(UnbondingBalanceMap::get().is_empty() && BondedBalanceMap::get().is_empty())
}

fn bond(
stash: Self::AccountId,
_: Self::AccountId,
value: Self::Balance,
_: Self::AccountId,
) -> DispatchResult {
StakingMock::set_bonded_balance(stash, value);
fn bond(stash: &Self::AccountId, value: Self::Balance, _: &Self::AccountId) -> DispatchResult {
StakingMock::set_bonded_balance(stash.clone(), value);
Ok(())
}

fn nominate(_: Self::AccountId, nominations: Vec<Self::AccountId>) -> DispatchResult {
fn nominate(_: &Self::AccountId, nominations: Vec<Self::AccountId>) -> DispatchResult {
Nominations::set(&Some(nominations));
Ok(())
}
Expand All @@ -123,6 +111,48 @@ impl sp_staking::StakingInterface for StakingMock {
fn nominations(_: Self::AccountId) -> Option<Vec<Self::AccountId>> {
Nominations::get()
}

fn stash_by_ctrl(_controller: &Self::AccountId) -> Result<Self::AccountId, DispatchError> {
todo!()
}

fn stake(who: &Self::AccountId) -> Result<Stake<Self>, DispatchError> {
match (
UnbondingBalanceMap::get().get(who).map(|v| *v),
BondedBalanceMap::get().get(who).map(|v| *v),
) {
(None, None) => Err(DispatchError::Other("balance not found")),
(Some(v), None) => Ok(Stake { total: v, active: 0, stash: who.clone() }),
(None, Some(v)) => Ok(Stake { total: v, active: v, stash: who.clone() }),
(Some(a), Some(b)) => Ok(Stake { total: a + b, active: b, stash: who.clone() }),
}
}

fn election_ongoing() -> bool {
todo!()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
todo!()
unimplemented!("reason?")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, basically we don't need those methods for nom-pools tests, I will make sure this is properly documented via reason.

}

fn force_unstake(_who: Self::AccountId) -> sp_runtime::DispatchResult {
todo!()
}

fn is_exposed_in_era(_who: &Self::AccountId, _era: &EraIndex) -> bool {
todo!()
}

#[cfg(feature = "runtime-benchmarks")]
fn add_era_stakers(
_current_era: &EraIndex,
_stash: &Self::AccountId,
_exposures: Vec<(Self::AccountId, Self::Balance)>,
) {
todo!()
}

#[cfg(feature = "runtime-benchmarks")]
fn set_current_era(_era: EraIndex) {
todo!()
}
}

impl frame_system::Config for Runtime {
Expand Down
62 changes: 39 additions & 23 deletions frame/nomination-pools/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ fn test_setup_works() {
let reward_account = Pools::create_reward_account(last_pool);

// the bonded_account should be bonded by the depositor's funds.
assert_eq!(StakingMock::active_stake(&bonded_account).unwrap(), 10);
assert_eq!(StakingMock::total_stake(&bonded_account).unwrap(), 10);
assert_eq!(StakingMock::stake(&bonded_account).map(|s| s.active).unwrap(), 10);
assert_eq!(StakingMock::stake(&bonded_account).map(|l| l.total).unwrap(), 10);

// but not nominating yet.
assert!(Nominations::get().is_none());
Expand Down Expand Up @@ -2368,7 +2368,7 @@ mod unbond {
}
);

assert_eq!(StakingMock::active_stake(&default_bonded_account()).unwrap(), 0);
assert_eq!(StakingMock::stake(&default_bonded_account()).map(|l| l.active).unwrap(), 0);
});
}

Expand Down Expand Up @@ -2415,7 +2415,10 @@ mod unbond {
]
);

assert_eq!(StakingMock::active_stake(&default_bonded_account()).unwrap(), 94);
assert_eq!(
StakingMock::stake(&default_bonded_account()).map(|l| l.active).unwrap(),
94
);
assert_eq!(
PoolMembers::<Runtime>::get(40).unwrap().unbonding_eras,
member_unbonding_eras!(0 + 3 => 6)
Expand Down Expand Up @@ -2443,7 +2446,10 @@ mod unbond {
}
}
);
assert_eq!(StakingMock::active_stake(&default_bonded_account()).unwrap(), 2);
assert_eq!(
StakingMock::stake(&default_bonded_account()).map(|l| l.active).unwrap(),
2
);
assert_eq!(
PoolMembers::<Runtime>::get(550).unwrap().unbonding_eras,
member_unbonding_eras!(0 + 3 => 92)
Expand Down Expand Up @@ -2486,7 +2492,10 @@ mod unbond {
}
}
);
assert_eq!(StakingMock::active_stake(&default_bonded_account()).unwrap(), 0);
assert_eq!(
StakingMock::stake(&default_bonded_account()).map(|l| l.active).unwrap(),
0
);

assert_eq!(Balances::free_balance(&550), 550 + 550 + 92);
assert_eq!(
Expand Down Expand Up @@ -2614,7 +2623,10 @@ mod unbond {
}
}
);
assert_eq!(StakingMock::active_stake(&default_bonded_account()).unwrap(), 10);
assert_eq!(
StakingMock::stake(&default_bonded_account()).map(|l| l.active).unwrap(),
10
);
assert_eq!(
SubPoolsStorage::<Runtime>::get(1).unwrap(),
SubPools {
Expand Down Expand Up @@ -2724,7 +2736,7 @@ mod unbond {
}
}
);
assert_eq!(StakingMock::active_stake(&default_bonded_account()).unwrap(), 0);
assert_eq!(StakingMock::stake(&default_bonded_account()).map(|l| l.active).unwrap(), 0);
assert_eq!(*UnbondingBalanceMap::get().get(&default_bonded_account()).unwrap(), 10);
});
}
Expand Down Expand Up @@ -3083,18 +3095,18 @@ mod pool_withdraw_unbonded {
fn pool_withdraw_unbonded_works() {
ExtBuilder::default().build_and_execute(|| {
// Given 10 unbond'ed directly against the pool account
assert_ok!(StakingMock::unbond(default_bonded_account(), 5));
assert_ok!(StakingMock::unbond(&default_bonded_account(), 5));
// and the pool account only has 10 balance
assert_eq!(StakingMock::active_stake(&default_bonded_account()), Some(5));
assert_eq!(StakingMock::total_stake(&default_bonded_account()), Some(10));
assert_eq!(StakingMock::stake(&default_bonded_account()).map(|l| l.active), Ok(5));
assert_eq!(StakingMock::stake(&default_bonded_account()).map(|l| l.total), Ok(10));
assert_eq!(Balances::free_balance(&default_bonded_account()), 10);

// When
assert_ok!(Pools::pool_withdraw_unbonded(RuntimeOrigin::signed(10), 1, 0));

// Then there unbonding balance is no longer locked
assert_eq!(StakingMock::active_stake(&default_bonded_account()), Some(5));
assert_eq!(StakingMock::total_stake(&default_bonded_account()), Some(5));
assert_eq!(StakingMock::stake(&default_bonded_account()).map(|l| l.active), Ok(5));
assert_eq!(StakingMock::stake(&default_bonded_account()).map(|l| l.total), Ok(5));
assert_eq!(Balances::free_balance(&default_bonded_account()), 10);
});
}
Expand Down Expand Up @@ -3141,7 +3153,8 @@ mod withdraw_unbonded {
);
StakingMock::set_bonded_balance(
default_bonded_account(),
StakingMock::active_stake(&default_bonded_account()).unwrap() / 2,
StakingMock::stake(&default_bonded_account()).map(|l| l.active).unwrap() /
2,
);
};

Expand Down Expand Up @@ -3270,7 +3283,7 @@ mod withdraw_unbonded {
// current bond is 600, we slash it all to 300.
StakingMock::set_bonded_balance(default_bonded_account(), 300);
Balances::make_free_balance_be(&default_bonded_account(), 300);
assert_eq!(StakingMock::total_stake(&default_bonded_account()), Some(300));
assert_eq!(StakingMock::stake(&default_bonded_account()).map(|l| l.total), Ok(300));

assert_ok!(fully_unbond_permissioned(40));
assert_ok!(fully_unbond_permissioned(550));
Expand Down Expand Up @@ -4074,12 +4087,15 @@ mod create {
assert!(!BondedPools::<Runtime>::contains_key(2));
assert!(!RewardPools::<Runtime>::contains_key(2));
assert!(!PoolMembers::<Runtime>::contains_key(11));
assert_eq!(StakingMock::active_stake(&next_pool_stash), None);
assert_err!(
StakingMock::stake(&next_pool_stash).map(|s| s.active),
DispatchError::Other("balance not found")
);

Balances::make_free_balance_be(&11, StakingMock::minimum_bond() + ed);
Balances::make_free_balance_be(&11, StakingMock::minimum_nominator_bond() + ed);
assert_ok!(Pools::create(
RuntimeOrigin::signed(11),
StakingMock::minimum_bond(),
StakingMock::minimum_nominator_bond(),
123,
456,
789
Expand All @@ -4090,7 +4106,7 @@ mod create {
PoolMembers::<Runtime>::get(11).unwrap(),
PoolMember {
pool_id: 2,
points: StakingMock::minimum_bond(),
points: StakingMock::minimum_nominator_bond(),
..Default::default()
}
);
Expand All @@ -4099,7 +4115,7 @@ mod create {
BondedPool {
id: 2,
inner: BondedPoolInner {
points: StakingMock::minimum_bond(),
points: StakingMock::minimum_nominator_bond(),
member_counter: 1,
state: PoolState::Open,
roles: PoolRoles {
Expand All @@ -4112,8 +4128,8 @@ mod create {
}
);
assert_eq!(
StakingMock::active_stake(&next_pool_stash).unwrap(),
StakingMock::minimum_bond()
StakingMock::stake(&next_pool_stash).map(|s| s.active).unwrap(),
StakingMock::minimum_nominator_bond()
);
assert_eq!(
RewardPools::<Runtime>::get(2).unwrap(),
Expand Down Expand Up @@ -4142,7 +4158,7 @@ mod create {

// Given
assert_eq!(MinCreateBond::<Runtime>::get(), 2);
assert_eq!(StakingMock::minimum_bond(), 10);
assert_eq!(StakingMock::minimum_nominator_bond(), 10);

// Then
assert_noop!(
Expand Down