From eb0da5ca520fe11d17a938dcd68ba2a5363b40ea Mon Sep 17 00:00:00 2001 From: Andreea Popescu Date: Tue, 25 Jun 2024 14:46:48 +0100 Subject: [PATCH] mutate directly --- pallets/subtensor/src/lib.rs | 11 +++++------ pallets/subtensor/src/uids.rs | 26 +++++--------------------- pallets/subtensor/tests/uids.rs | 11 ++++++----- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index b590781f5..351bc39bf 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -1001,19 +1001,18 @@ pub mod pallet { pub(super) type Rank = StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; #[pallet::storage] // --- DMAP ( netuid ) --> trust - pub(super) type Trust = - StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; + pub type Trust = StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; #[pallet::storage] // --- DMAP ( netuid ) --> consensus - pub(super) type Consensus = + pub type Consensus = StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; #[pallet::storage] // --- DMAP ( netuid ) --> incentive - pub(super) type Incentive = + pub type Incentive = StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; #[pallet::storage] // --- DMAP ( netuid ) --> dividends - pub(super) type Dividends = + pub type Dividends = StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU16Vec>; #[pallet::storage] // --- DMAP ( netuid ) --> emission - pub(super) type Emission = + pub type Emission = StorageMap<_, Identity, u16, Vec, ValueQuery, EmptyU64Vec>; #[pallet::storage] // --- DMAP ( netuid ) --> last_update pub(super) type LastUpdate = diff --git a/pallets/subtensor/src/uids.rs b/pallets/subtensor/src/uids.rs index a3970cb10..36b17f9de 100644 --- a/pallets/subtensor/src/uids.rs +++ b/pallets/subtensor/src/uids.rs @@ -9,22 +9,6 @@ impl Pallet { SubnetworkN::::get(netuid) } - pub fn set_emission_for_uid(netuid: u16, neuron_uid: u16, emission: u64) { - Emission::::mutate(netuid, |v| v[neuron_uid as usize] = emission); - } - pub fn set_trust_for_uid(netuid: u16, neuron_uid: u16, trust: u16) { - Trust::::mutate(netuid, |v| v[neuron_uid as usize] = trust); - } - pub fn set_consensus_for_uid(netuid: u16, neuron_uid: u16, consensus: u16) { - Consensus::::mutate(netuid, |v| v[neuron_uid as usize] = consensus); - } - pub fn set_incentive_for_uid(netuid: u16, neuron_uid: u16, incentive: u16) { - Incentive::::mutate(netuid, |v| v[neuron_uid as usize] = incentive); - } - pub fn set_dividends_for_uid(netuid: u16, neuron_uid: u16, dividends: u16) { - Dividends::::mutate(netuid, |v| v[neuron_uid as usize] = dividends); - } - /// Replace the neuron under this uid. pub fn replace_neuron( netuid: u16, @@ -63,11 +47,11 @@ impl Pallet { IsNetworkMember::::insert(new_hotkey.clone(), netuid, true); // Fill network is member. // 4. Reset trust, emission, consensus, incentive, dividends and axon_info for the new uid. - Self::set_trust_for_uid(netuid, uid_to_replace, 0); - Self::set_emission_for_uid(netuid, uid_to_replace, 0); - Self::set_consensus_for_uid(netuid, uid_to_replace, 0); - Self::set_incentive_for_uid(netuid, uid_to_replace, 0); - Self::set_dividends_for_uid(netuid, uid_to_replace, 0); + Emission::::mutate(netuid, |v| v[uid_to_replace as usize] = 0); + Trust::::mutate(netuid, |v| v[uid_to_replace as usize] = 0); + Consensus::::mutate(netuid, |v| v[uid_to_replace as usize] = 0); + Incentive::::mutate(netuid, |v| v[uid_to_replace as usize] = 0); + Dividends::::mutate(netuid, |v| v[uid_to_replace as usize] = 0); // 4a. reset axon info for the new uid. Axons::::remove(netuid, old_hotkey); diff --git a/pallets/subtensor/tests/uids.rs b/pallets/subtensor/tests/uids.rs index 34526a103..b0049216c 100644 --- a/pallets/subtensor/tests/uids.rs +++ b/pallets/subtensor/tests/uids.rs @@ -1,6 +1,7 @@ use crate::mock::*; use frame_support::assert_ok; use frame_system::Config; +use pallet_subtensor::*; use sp_core::U256; mod mock; @@ -51,11 +52,11 @@ fn test_replace_neuron() { let neuron_uid = neuron_uid.unwrap(); // set non-default values - SubtensorModule::set_trust_for_uid(netuid, neuron_uid, 5u16); - SubtensorModule::set_emission_for_uid(netuid, neuron_uid, 5u64); - SubtensorModule::set_consensus_for_uid(netuid, neuron_uid, 5u16); - SubtensorModule::set_incentive_for_uid(netuid, neuron_uid, 5u16); - SubtensorModule::set_dividends_for_uid(netuid, neuron_uid, 5u16); + 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); // serve axon mock address let ip: u128 = 1676056785;