diff --git a/pallets/subtensor/src/staking.rs b/pallets/subtensor/src/staking.rs index 6360a1e63..9e9a1c87e 100644 --- a/pallets/subtensor/src/staking.rs +++ b/pallets/subtensor/src/staking.rs @@ -189,6 +189,9 @@ impl Pallet { // --- 3. Ensure that the hotkey allows delegation or that the hotkey is owned by the calling coldkey. ensure!( Self::hotkey_is_delegate( &hotkey ) || Self::coldkey_owns_hotkey( &coldkey, &hotkey ), Error::::NonAssociatedColdKey ); + // --- Ensure that the stake amount to be removed is above zero. + ensure!( stake_to_be_removed > 0, Error::::NotEnoughStaketoWithdraw ); + // --- 4. Ensure that the hotkey has enough stake to withdraw. ensure!( Self::has_enough_stake( &coldkey, &hotkey, stake_to_be_removed ), Error::::NotEnoughStaketoWithdraw ); diff --git a/pallets/subtensor/tests/staking.rs b/pallets/subtensor/tests/staking.rs index fc0a3824e..5bab5fa68 100644 --- a/pallets/subtensor/tests/staking.rs +++ b/pallets/subtensor/tests/staking.rs @@ -1,4 +1,4 @@ -use frame_support::{assert_ok, traits::Currency}; +use frame_support::{assert_ok, assert_noop, traits::Currency}; use frame_system::{Config}; mod mock; use mock::*; @@ -334,6 +334,38 @@ fn test_remove_stake_ok_no_emission() { }); } +#[test] +fn test_remove_stake_amount_zero() { + new_test_ext().execute_with(|| { + let coldkey_account_id = U256::from(4343); + let hotkey_account_id = U256::from(4968585); + let amount = 10000; + let netuid: u16 = 1; + let tempo: u16 = 13; + let start_nonce: u64 = 0; + + //add network + add_network(netuid, tempo, 0); + + // Let's spin up a neuron + register_ok_neuron( netuid, hotkey_account_id, coldkey_account_id, start_nonce); + + // Some basic assertions + assert_eq!(SubtensorModule::get_total_stake(), 0); + assert_eq!(SubtensorModule::get_total_stake_for_hotkey(&hotkey_account_id), 0); + assert_eq!(SubtensorModule::get_coldkey_balance(&coldkey_account_id), 0); + + // Give the neuron some stake to remove + SubtensorModule::increase_stake_on_hotkey_account(&hotkey_account_id, amount); + + // Do the magic + assert_noop!( + SubtensorModule::remove_stake(<::RuntimeOrigin>::signed(coldkey_account_id), hotkey_account_id, 0), + Error::::NotEnoughStaketoWithdraw + ); + }); +} + #[test] fn test_remove_stake_err_signature() { new_test_ext().execute_with(|| {