subtensor icon indicating copy to clipboard operation
subtensor copied to clipboard

fix remove_network

Open distributedstatemachine opened this issue 1 year ago • 0 comments

Description

Currently, the remove_network function does not properly handle the unstaking of nominators and delegates when a network is removed. This leads to orphaned stakes in neurons. Additionally, there may be existing neurons staked in already dissolved networks that need to be addressed.

We need to update the remove_network function to correctly differentiate between delegates and nominators when returning stakes, and implement a storage migration to handle existing neurons staked in dissolved networks.

Acceptance Criteria

  • Update the remove_network function to properly unstake and return funds to both delegates and nominators.
  • Implement a storage migration to handle existing neurons staked in dissolved networks.
  • Ensure that delegate stakes are returned to the delegate's coldkey.
  • Ensure that nominator stakes are returned to the nominator's coldkey.
  • Write unit tests to verify the correct behavior of the updated remove_network function.
  • Write integration tests to ensure the storage migration works as expected.

Tasks

pub fn remove_network(netuid: u16) {
    // ... existing code ...

    // Unstake nominators and delegates
    for (hotkey, coldkey, stake) in <Stake<T> as IterableStorageDoubleMap<T::AccountId, T::AccountId, u64>>::iter() {
        if let Some(neuron) = Neurons::<T>::get(&hotkey) {
            if neuron.netuid == netuid {
                if Self::hotkey_is_delegate(&hotkey) {
                    let delegate_coldkey = Owner::<T>::get(hotkey.clone());
                    Self::add_balance_to_coldkey_account(&delegate_coldkey, stake);
                } else {
                    Self::add_balance_to_coldkey_account(&coldkey, stake);
                }
                <Stake<T>>::remove(hotkey.clone(), coldkey.clone());
            }
        }
    }

}
  • [ ] Implement a storage migration to handle existing neurons staked in dissolved networks:
  • [ ] Write unit tests for the updated remove_network function.
  • [ ] Write integration tests for the storage migration.

Related Links

distributedstatemachine avatar Jun 24 '24 10:06 distributedstatemachine