From 232f6875e185e81716acaef2d18522908835204c Mon Sep 17 00:00:00 2001 From: Andreea Popescu Date: Fri, 28 Jun 2024 14:55:33 +0100 Subject: [PATCH] add bounds check --- pallets/subtensor/src/uids.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/pallets/subtensor/src/uids.rs b/pallets/subtensor/src/uids.rs index 5f96c36c2..3bad000af 100644 --- a/pallets/subtensor/src/uids.rs +++ b/pallets/subtensor/src/uids.rs @@ -10,12 +10,23 @@ impl Pallet { } /// Resets the trust, emission, consensus, incentive, dividends of the neuron to default + fn clear_element_at(position: u16) -> impl Fn(&mut Vec) + where + N: From, + { + move |vec: &mut Vec| { + if vec.len() > position as usize { + vec[position as usize] = N::from(0); + }; + } + } + pub fn clear_neuron(netuid: u16, neuron_uid: u16) { - Emission::::mutate(netuid, |v| v[neuron_uid as usize] = 0); - Trust::::mutate(netuid, |v| v[neuron_uid as usize] = 0); - Consensus::::mutate(netuid, |v| v[neuron_uid as usize] = 0); - Incentive::::mutate(netuid, |v| v[neuron_uid as usize] = 0); - Dividends::::mutate(netuid, |v| v[neuron_uid as usize] = 0); + 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)); } /// Replace the neuron under this uid.