Skip to content

Commit

Permalink
Update sanity check & fix create_works
Browse files Browse the repository at this point in the history
  • Loading branch information
emostov committed Mar 17, 2022
1 parent f48cd37 commit cdc2a32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
11 changes: 8 additions & 3 deletions frame/nomination-pools/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,22 +1430,23 @@ pub mod pallet {
&bonded_pool.reward_account(),
T::Currency::minimum_balance(),
ExistenceRequirement::AllowDeath,
)?;
)
.defensive_map_err(|e| e)?;
let mut reward_pool = RewardPool::<T> {
balance: Zero::zero(),
points: U256::zero(),
total_earnings: Zero::zero(),
};
// Make sure the reward pool has correct balance and earnings so the first payout claim
// does not wipe the ED. This must be done after the transfer
// does not wipe the ED. This must be done after transferring to the reward account.
reward_pool.update_total_earnings_and_balance(bonded_pool.id);

Delegators::<T>::insert(
who.clone(),
Delegator::<T> {
pool_id,
points,
reward_pool_total_earnings: Zero::zero(),
reward_pool_total_earnings: reward_pool.total_earnings,
unbonding_era: None,
},
);
Expand Down Expand Up @@ -1795,6 +1796,7 @@ impl<T: Config> Pallet<T> {
let mut all_delegators = 0u32;
Delegators::<T>::iter().for_each(|(_, d)| {
assert!(BondedPools::<T>::contains_key(d.pool_id));
assert!(d.reward_pool_total_earnings >= T::Currency::minimum_balance());
*pools_delegators.entry(d.pool_id).or_default() += 1;
all_delegators += 1;
});
Expand Down Expand Up @@ -1835,6 +1837,9 @@ impl<T: Config> Pallet<T> {
bonded_balance,
sum_unbonding_balance
);

let reward_account = Pallet::<T>::create_reward_account(pool_id);
assert!(T::Currency::free_balance(&reward_account) >= T::Currency::minimum_balance());
}

Ok(())
Expand Down
16 changes: 11 additions & 5 deletions frame/nomination-pools/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2098,7 +2098,8 @@ mod create {
assert!(!Delegators::<Runtime>::contains_key(11));
assert_eq!(StakingMock::active_stake(&next_pool_stash), None);

Balances::make_free_balance_be(&11, StakingMock::minimum_bond());
let min_create_free = StakingMock::minimum_bond() + Balances::minimum_balance();
Balances::make_free_balance_be(&11, min_create_free);
assert_ok!(Pools::create(
Origin::signed(11),
StakingMock::minimum_bond(),
Expand All @@ -2113,12 +2114,13 @@ mod create {
Delegator {
pool_id: 2,
points: StakingMock::minimum_bond(),
reward_pool_total_earnings: Zero::zero(),
reward_pool_total_earnings: Balances::minimum_balance(),
unbonding_era: None
}
);
let bonded_pool = BondedPool::<Runtime>::get(2).unwrap();
assert_eq!(
BondedPool::<Runtime>::get(2).unwrap(),
bonded_pool,
BondedPool {
id: 2,
inner: BondedPoolInner {
Expand All @@ -2141,11 +2143,15 @@ mod create {
assert_eq!(
RewardPools::<Runtime>::get(2).unwrap(),
RewardPool {
balance: Zero::zero(),
balance: Balances::minimum_balance(),
points: U256::zero(),
total_earnings: Zero::zero(),
total_earnings: Balances::minimum_balance(),
}
);
assert_eq!(
Balances::free_balance(bonded_pool.reward_account()),
Balances::minimum_balance()
);
});
}

Expand Down

0 comments on commit cdc2a32

Please sign in to comment.