Skip to content

Commit

Permalink
mutate directly
Browse files Browse the repository at this point in the history
  • Loading branch information
andreea-popescu-reef committed Jun 25, 2024
1 parent 44ce566 commit eb0da5c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 32 deletions.
11 changes: 5 additions & 6 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1001,19 +1001,18 @@ pub mod pallet {
pub(super) type Rank<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> trust
pub(super) type Trust<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
pub type Trust<T: Config> = StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> consensus
pub(super) type Consensus<T: Config> =
pub type Consensus<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> incentive
pub(super) type Incentive<T: Config> =
pub type Incentive<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> dividends
pub(super) type Dividends<T: Config> =
pub type Dividends<T: Config> =
StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, EmptyU16Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> emission
pub(super) type Emission<T: Config> =
pub type Emission<T: Config> =
StorageMap<_, Identity, u16, Vec<u64>, ValueQuery, EmptyU64Vec<T>>;
#[pallet::storage] // --- DMAP ( netuid ) --> last_update
pub(super) type LastUpdate<T: Config> =
Expand Down
26 changes: 5 additions & 21 deletions pallets/subtensor/src/uids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,6 @@ impl<T: Config> Pallet<T> {
SubnetworkN::<T>::get(netuid)
}

pub fn set_emission_for_uid(netuid: u16, neuron_uid: u16, emission: u64) {
Emission::<T>::mutate(netuid, |v| v[neuron_uid as usize] = emission);
}
pub fn set_trust_for_uid(netuid: u16, neuron_uid: u16, trust: u16) {
Trust::<T>::mutate(netuid, |v| v[neuron_uid as usize] = trust);
}
pub fn set_consensus_for_uid(netuid: u16, neuron_uid: u16, consensus: u16) {
Consensus::<T>::mutate(netuid, |v| v[neuron_uid as usize] = consensus);
}
pub fn set_incentive_for_uid(netuid: u16, neuron_uid: u16, incentive: u16) {
Incentive::<T>::mutate(netuid, |v| v[neuron_uid as usize] = incentive);
}
pub fn set_dividends_for_uid(netuid: u16, neuron_uid: u16, dividends: u16) {
Dividends::<T>::mutate(netuid, |v| v[neuron_uid as usize] = dividends);
}

/// Replace the neuron under this uid.
pub fn replace_neuron(
netuid: u16,
Expand Down Expand Up @@ -63,11 +47,11 @@ impl<T: Config> Pallet<T> {
IsNetworkMember::<T>::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::<T>::mutate(netuid, |v| v[uid_to_replace as usize] = 0);
Trust::<T>::mutate(netuid, |v| v[uid_to_replace as usize] = 0);
Consensus::<T>::mutate(netuid, |v| v[uid_to_replace as usize] = 0);
Incentive::<T>::mutate(netuid, |v| v[uid_to_replace as usize] = 0);
Dividends::<T>::mutate(netuid, |v| v[uid_to_replace as usize] = 0);

// 4a. reset axon info for the new uid.
Axons::<T>::remove(netuid, old_hotkey);
Expand Down
11 changes: 6 additions & 5 deletions pallets/subtensor/tests/uids.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);
Emission::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u64);
Consensus::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);
Incentive::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);
Dividends::<Test>::mutate(netuid, |v| v[neuron_uid as usize] = 5u16);

// serve axon mock address
let ip: u128 = 1676056785;
Expand Down

0 comments on commit eb0da5c

Please sign in to comment.