Skip to content

Commit

Permalink
add bounds check
Browse files Browse the repository at this point in the history
  • Loading branch information
andreea-popescu-reef committed Jun 28, 2024
1 parent 4f99ee5 commit 232f687
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions pallets/subtensor/src/uids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,23 @@ impl<T: Config> Pallet<T> {
}

/// Resets the trust, emission, consensus, incentive, dividends of the neuron to default
fn clear_element_at<N>(position: u16) -> impl Fn(&mut Vec<N>)
where
N: From<u8>,
{
move |vec: &mut Vec<N>| {
if vec.len() > position as usize {
vec[position as usize] = N::from(0);
};
}
}

pub fn clear_neuron(netuid: u16, neuron_uid: u16) {
Emission::<T>::mutate(netuid, |v| v[neuron_uid as usize] = 0);
Trust::<T>::mutate(netuid, |v| v[neuron_uid as usize] = 0);
Consensus::<T>::mutate(netuid, |v| v[neuron_uid as usize] = 0);
Incentive::<T>::mutate(netuid, |v| v[neuron_uid as usize] = 0);
Dividends::<T>::mutate(netuid, |v| v[neuron_uid as usize] = 0);
Emission::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
Trust::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
Consensus::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
Incentive::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
Dividends::<T>::mutate(netuid, Self::clear_element_at(neuron_uid));
}

/// Replace the neuron under this uid.
Expand Down

0 comments on commit 232f687

Please sign in to comment.