From 6ef5f8ba22977b5838e0c6275cf87262fa05226f Mon Sep 17 00:00:00 2001 From: Puneet Saraswat Date: Wed, 20 Nov 2024 18:56:34 -0600 Subject: [PATCH 1/5] forbid staking more than stakable amount --- pallets/capacity/src/lib.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pallets/capacity/src/lib.rs b/pallets/capacity/src/lib.rs index 3ef979999e..b197ff1534 100644 --- a/pallets/capacity/src/lib.rs +++ b/pallets/capacity/src/lib.rs @@ -834,9 +834,12 @@ impl Pallet { ) -> BalanceOf { let account_balance = T::Currency::reducible_balance(&staker, Preservation::Preserve, Fortitude::Polite); - account_balance - .saturating_sub(T::MinimumTokenBalance::get()) - .min(proposed_amount) + let stakable_amount = account_balance.saturating_sub(T::MinimumTokenBalance::get()); + if stakable_amount > proposed_amount { + proposed_amount + } else { + Zero::zero() + } } pub(crate) fn do_withdraw_unstaked( From 1abd5e5cac2f630557d44e1cbb68fde7ef81aa64 Mon Sep 17 00:00:00 2001 From: Puneet Saraswat Date: Wed, 20 Nov 2024 19:00:43 -0600 Subject: [PATCH 2/5] fix first test --- pallets/capacity/src/tests/stake_and_deposit_tests.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/capacity/src/tests/stake_and_deposit_tests.rs b/pallets/capacity/src/tests/stake_and_deposit_tests.rs index 9ae495a08f..c2e9462706 100644 --- a/pallets/capacity/src/tests/stake_and_deposit_tests.rs +++ b/pallets/capacity/src/tests/stake_and_deposit_tests.rs @@ -272,10 +272,10 @@ fn stake_when_staking_amount_is_greater_than_free_balance_it_stakes_maximum() { fn get_stakable_amount_for_works() { new_test_ext().execute_with(|| { let account = 200; - // An amount greater than the free balance + // An amount greater than the free balance should not be stakable let amount = 230; let res: u64 = Capacity::get_stakable_amount_for(&account, amount); - assert_eq!(res, 189); + assert_eq!(res, 0); }) } From 01fd5799526f1ac65a558ae06e3ff58f30065f1d Mon Sep 17 00:00:00 2001 From: Puneet Saraswat Date: Thu, 21 Nov 2024 07:40:35 -0600 Subject: [PATCH 3/5] finalize --- pallets/capacity/src/lib.rs | 2 +- pallets/capacity/src/tests/stake_and_deposit_tests.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pallets/capacity/src/lib.rs b/pallets/capacity/src/lib.rs index b197ff1534..5142c424f5 100644 --- a/pallets/capacity/src/lib.rs +++ b/pallets/capacity/src/lib.rs @@ -835,7 +835,7 @@ impl Pallet { let account_balance = T::Currency::reducible_balance(&staker, Preservation::Preserve, Fortitude::Polite); let stakable_amount = account_balance.saturating_sub(T::MinimumTokenBalance::get()); - if stakable_amount > proposed_amount { + if stakable_amount >= proposed_amount { proposed_amount } else { Zero::zero() diff --git a/pallets/capacity/src/tests/stake_and_deposit_tests.rs b/pallets/capacity/src/tests/stake_and_deposit_tests.rs index c2e9462706..0be46db93e 100644 --- a/pallets/capacity/src/tests/stake_and_deposit_tests.rs +++ b/pallets/capacity/src/tests/stake_and_deposit_tests.rs @@ -244,7 +244,7 @@ fn stake_an_account_can_stake_to_multiple_targets() { } #[test] -fn stake_when_staking_amount_is_greater_than_free_balance_it_stakes_maximum() { +fn stake_when_staking_amount_is_greater_than_free_balance_it_stakes_zero() { new_test_ext().execute_with(|| { let target: MessageSourceId = 1; register_provider(target, String::from("Foo")); @@ -252,7 +252,11 @@ fn stake_when_staking_amount_is_greater_than_free_balance_it_stakes_maximum() { // An amount greater than the free balance let amount = 230; - assert_ok!(Capacity::stake(RuntimeOrigin::signed(account), target, amount)); + assert_noop!( + Capacity::stake(RuntimeOrigin::signed(account), target, amount), + Error::::BalanceTooLowtoStake + ); + assert_ok!(Capacity::stake(RuntimeOrigin::signed(account), target, 189)); // Check that StakingAccountLedger is updated. assert_eq!(StakingAccountLedger::::get(account).unwrap().active, 189); From f1af61263730450fbbaf84b945c413be16bb5cb1 Mon Sep 17 00:00:00 2001 From: Puneet Saraswat Date: Thu, 21 Nov 2024 07:42:25 -0600 Subject: [PATCH 4/5] update spec version --- runtime/frequency/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/frequency/src/lib.rs b/runtime/frequency/src/lib.rs index 70be9373f7..ac76dda45d 100644 --- a/runtime/frequency/src/lib.rs +++ b/runtime/frequency/src/lib.rs @@ -402,7 +402,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("frequency"), impl_name: create_runtime_str!("frequency"), authoring_version: 1, - spec_version: 132, + spec_version: 134, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, @@ -416,7 +416,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("frequency-testnet"), impl_name: create_runtime_str!("frequency"), authoring_version: 1, - spec_version: 132, + spec_version: 134, impl_version: 0, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From 57cc16ff563a56bb21053ee8fea21c7bf50c0763 Mon Sep 17 00:00:00 2001 From: Puneet Saraswat Date: Mon, 25 Nov 2024 12:00:44 -0600 Subject: [PATCH 5/5] add e2e test for staking at the edge --- e2e/capacity/staking.test.ts | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/e2e/capacity/staking.test.ts b/e2e/capacity/staking.test.ts index a2b13c5ebf..3b81601f28 100644 --- a/e2e/capacity/staking.test.ts +++ b/e2e/capacity/staking.test.ts @@ -285,5 +285,14 @@ describe('Capacity Staking Tests', function () { const failStakeObj = ExtrinsicHelper.stake(stakingKeys, providerId, stakingAmount); await assert.rejects(failStakeObj.signAndSend(), { name: 'BalanceTooLowtoStake' }); }); + + it('fails to stake when stake is >= than stakable_amount + minimum token balance', async function () { + const stakingKeys = createKeys('stakingKeys'); + const providerId = await createMsaAndProvider(fundingSource, stakingKeys, 'stakingKeys', 1n * DOLLARS); + const stakingAmount = 1n * DOLLARS; + + const failStakeObj = ExtrinsicHelper.stake(stakingKeys, providerId, stakingAmount); + await assert.rejects(failStakeObj.signAndSend(), { name: 'BalanceTooLowtoStake' }); + }); }); });