Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent unstake call from allowing zero amount #165

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions pallets/subtensor/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ impl<T: Config> Pallet<T> {
// --- 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::<T>::NonAssociatedColdKey );

// --- Ensure that the stake amount to be removed is above zero.
ensure!( stake_to_be_removed > 0, Error::<T>::NotEnoughStaketoWithdraw );

// --- 4. Ensure that the hotkey has enough stake to withdraw.
ensure!( Self::has_enough_stake( &coldkey, &hotkey, stake_to_be_removed ), Error::<T>::NotEnoughStaketoWithdraw );

Expand Down
34 changes: 33 additions & 1 deletion pallets/subtensor/tests/staking.rs
Original file line number Diff line number Diff line change
@@ -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::*;
Expand Down Expand Up @@ -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(<<Test as Config>::RuntimeOrigin>::signed(coldkey_account_id), hotkey_account_id, 0),
Error::<Test>::NotEnoughStaketoWithdraw
);
});
}

#[test]
fn test_remove_stake_err_signature() {
new_test_ext().execute_with(|| {
Expand Down
Loading