From cdc2a32d22e6c00609bf496a5854b8f99f031156 Mon Sep 17 00:00:00 2001 From: emostov <32168567+emostov@users.noreply.github.com> Date: Thu, 17 Mar 2022 13:12:27 +0000 Subject: [PATCH] Update sanity check & fix create_works --- frame/nomination-pools/src/lib.rs | 11 ++++++++--- frame/nomination-pools/src/tests.rs | 16 +++++++++++----- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/frame/nomination-pools/src/lib.rs b/frame/nomination-pools/src/lib.rs index e2030b2f1dd38..33ffeffa40503 100644 --- a/frame/nomination-pools/src/lib.rs +++ b/frame/nomination-pools/src/lib.rs @@ -1430,14 +1430,15 @@ pub mod pallet { &bonded_pool.reward_account(), T::Currency::minimum_balance(), ExistenceRequirement::AllowDeath, - )?; + ) + .defensive_map_err(|e| e)?; let mut reward_pool = RewardPool:: { 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::::insert( @@ -1445,7 +1446,7 @@ pub mod pallet { Delegator:: { pool_id, points, - reward_pool_total_earnings: Zero::zero(), + reward_pool_total_earnings: reward_pool.total_earnings, unbonding_era: None, }, ); @@ -1795,6 +1796,7 @@ impl Pallet { let mut all_delegators = 0u32; Delegators::::iter().for_each(|(_, d)| { assert!(BondedPools::::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; }); @@ -1835,6 +1837,9 @@ impl Pallet { bonded_balance, sum_unbonding_balance ); + + let reward_account = Pallet::::create_reward_account(pool_id); + assert!(T::Currency::free_balance(&reward_account) >= T::Currency::minimum_balance()); } Ok(()) diff --git a/frame/nomination-pools/src/tests.rs b/frame/nomination-pools/src/tests.rs index 8b33de7da674c..b5dafe168ee51 100644 --- a/frame/nomination-pools/src/tests.rs +++ b/frame/nomination-pools/src/tests.rs @@ -2098,7 +2098,8 @@ mod create { assert!(!Delegators::::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(), @@ -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::::get(2).unwrap(); assert_eq!( - BondedPool::::get(2).unwrap(), + bonded_pool, BondedPool { id: 2, inner: BondedPoolInner { @@ -2141,11 +2143,15 @@ mod create { assert_eq!( RewardPools::::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() + ); }); }