diff --git a/pallets/subtensor/src/subnets/uids.rs b/pallets/subtensor/src/subnets/uids.rs index 300556647..7fb2e1e25 100644 --- a/pallets/subtensor/src/subnets/uids.rs +++ b/pallets/subtensor/src/subnets/uids.rs @@ -9,26 +9,20 @@ impl Pallet { SubnetworkN::::get(netuid) } - /// Returns a callback that sets the element at the given position to zero, doing nothing if the - /// position is out of bounds - fn clear_element_at(position: u16) -> impl Fn(&mut Vec) - where - N: From, - { - move |vec: &mut Vec| { - if let Some(element) = vec.get_mut(position as usize) { - *element = N::from(0); - } + /// Sets value for the element at the given position if it exists. + pub fn set_element_at(vec: &mut [N], position: usize, value: N) { + if let Some(element) = vec.get_mut(position) { + *element = value; } } /// Resets the trust, emission, consensus, incentive, dividends of the neuron to default pub fn clear_neuron(netuid: u16, neuron_uid: u16) { - Emission::::mutate(netuid, Self::clear_element_at(neuron_uid)); - Trust::::mutate(netuid, Self::clear_element_at(neuron_uid)); - Consensus::::mutate(netuid, Self::clear_element_at(neuron_uid)); - Incentive::::mutate(netuid, Self::clear_element_at(neuron_uid)); - Dividends::::mutate(netuid, Self::clear_element_at(neuron_uid)); + Emission::::mutate(netuid, |v| Self::set_element_at(v, neuron_uid.into(), 0)); + Trust::::mutate(netuid, |v| Self::set_element_at(v, neuron_uid.into(), 0)); + Consensus::::mutate(netuid, |v| Self::set_element_at(v, neuron_uid.into(), 0)); + Incentive::::mutate(netuid, |v| Self::set_element_at(v, neuron_uid.into(), 0)); + Dividends::::mutate(netuid, |v| Self::set_element_at(v, neuron_uid.into(), 0)); } /// Replace the neuron under this uid. diff --git a/pallets/subtensor/tests/uids.rs b/pallets/subtensor/tests/uids.rs index c3453a4e4..9f698d687 100644 --- a/pallets/subtensor/tests/uids.rs +++ b/pallets/subtensor/tests/uids.rs @@ -54,11 +54,21 @@ fn test_replace_neuron() { let neuron_uid = neuron_uid.unwrap(); // set non-default values - Trust::::mutate(netuid, |v| v[neuron_uid as usize] = 5u16); - Emission::::mutate(netuid, |v| v[neuron_uid as usize] = 5u64); - Consensus::::mutate(netuid, |v| v[neuron_uid as usize] = 5u16); - Incentive::::mutate(netuid, |v| v[neuron_uid as usize] = 5u16); - Dividends::::mutate(netuid, |v| v[neuron_uid as usize] = 5u16); + Trust::::mutate(netuid, |v| { + SubtensorModule::set_element_at(v, neuron_uid as usize, 5u16) + }); + Emission::::mutate(netuid, |v| { + SubtensorModule::set_element_at(v, neuron_uid as usize, 5u64) + }); + Consensus::::mutate(netuid, |v| { + SubtensorModule::set_element_at(v, neuron_uid as usize, 5u16) + }); + Incentive::::mutate(netuid, |v| { + SubtensorModule::set_element_at(v, neuron_uid as usize, 5u16) + }); + Dividends::::mutate(netuid, |v| { + SubtensorModule::set_element_at(v, neuron_uid as usize, 5u16) + }); // serve axon mock address let ip: u128 = 1676056785;