Skip to content

Commit

Permalink
refac: useless code cleanup (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
functor-flow authored May 21, 2024
1 parent 23fcdf3 commit db8a19b
Show file tree
Hide file tree
Showing 17 changed files with 403 additions and 524 deletions.
2 changes: 1 addition & 1 deletion pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub mod pallet {
23148148148
}
#[pallet::storage] // --- ITEM ( unit_emission )
pub(super) type UnitEmission<T> = StorageValue<_, u64, ValueQuery, DefaultUnitEmission<T>>;
pub type UnitEmission<T> = StorageValue<_, u64, ValueQuery, DefaultUnitEmission<T>>;

#[pallet::type_value]
pub fn DefaultAdjustmentAlpha<T: Config>() -> u64 {
Expand Down
6 changes: 3 additions & 3 deletions pallets/subspace/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<T: Config> Pallet<T> {
changeset: ModuleChangeset,
) -> Result<u16, sp_runtime::DispatchError> {
// 1. Get the next uid. This is always equal to subnetwork_n.
let uid: u16 = Self::get_subnet_n(netuid);
let uid: u16 = N::<T>::get(netuid);
let block_number = Self::get_current_block_number();

log::debug!("append_module( netuid: {netuid:?} | uid: {key:?} | new_key: {uid:?})");
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<T: Config> Pallet<T> {
/// Replace the module under this uid.
pub fn remove_module(netuid: u16, uid: u16) {
// 1. Get the old key under this position.
let n = Self::get_subnet_n(netuid);
let n = N::<T>::get(netuid);
if n == 0 {
// No modules in the network.
return;
Expand Down Expand Up @@ -326,7 +326,7 @@ impl<T: Config> Pallet<T> {
.collect();
let stake_from: BTreeMap<T::AccountId, u64> = StakeFrom::<T>::get(netuid, key);

let registration_block = Self::get_registration_block_for_uid(netuid, uid);
let registration_block = RegistrationBlock::<T>::get(netuid, uid);

ModuleStats {
stake_from,
Expand Down
12 changes: 6 additions & 6 deletions pallets/subspace/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl<T: Config> Pallet<T> {

// --- 5. Ensure the caller has enough stake to register.
let min_stake: u64 = MinStake::<T>::get(netuid);
let current_burn: u64 = Self::get_burn(netuid);
let current_burn: u64 = Burn::<T>::get(netuid);
// also ensures that in the case current_burn is present, the stake is enough
// as burn, will be decreased from the stake on the module
ensure!(
Expand Down Expand Up @@ -232,7 +232,7 @@ impl<T: Config> Pallet<T> {
let mut min_score: u64 = u64::MAX;
let mut lowest_priority_uid: u16 = 0;
let current_block = Self::get_current_block_number();
let immunity_period: u64 = Self::get_immunity_period(netuid) as u64;
let immunity_period: u64 = ImmunityPeriod::<T>::get(netuid) as u64;

// Get all the UIDs and their registration blocks from the storage
let mut uids: Vec<_> = RegistrationBlock::<T>::iter_prefix(netuid).collect();
Expand Down Expand Up @@ -384,12 +384,12 @@ impl<T: Config> Pallet<T> {
stake: u64,
changeset: SubnetChangeset<T>,
) -> Result<u16, sp_runtime::DispatchError> {
let num_subnets: u16 = Self::num_subnets();
let num_subnets: u16 = TotalSubnets::<T>::get();
let max_subnets: u16 = MaxAllowedSubnets::<T>::get();

// if we have not reached the max number of subnets, then we can start a new one
let target_subnet = if num_subnets >= max_subnets {
let (min_stake_netuid, min_stake) = Self::least_staked_netuid();
let (min_stake_netuid, min_stake) = Self::get_least_staked_netuid();
// if the stake is greater than the least staked network, then we can start a new one
ensure!(stake > min_stake, Error::<T>::NotEnoughStakeToStartNetwork);
Self::remove_subnet(min_stake_netuid);
Expand All @@ -409,13 +409,13 @@ impl<T: Config> Pallet<T> {
/// subnet is filled, deregister the least staked module on it, or if the max allowed modules on
/// the network is reached, deregisters the least staked module on the least staked netuid.
pub fn reserve_module_slot(netuid: u16) {
if Self::get_subnet_n(netuid) >= Self::get_max_allowed_uids(netuid) {
if N::<T>::get(netuid) >= MaxAllowedUids::<T>::get(netuid) {
// If we reach the max allowed modules for this subnet,
// then we replace the lowest priority node in the current subnet
Self::remove_module(netuid, Self::get_lowest_uid(netuid, false));
} else if Self::global_n_modules() >= MaxAllowedModules::<T>::get() {
// Get the least staked network (subnet) and its least staked module.
let (subnet_uid, _) = Self::least_staked_netuid();
let (subnet_uid, _) = Self::get_least_staked_netuid();
let module_uid = Self::get_lowest_uid(subnet_uid, true);

// Deregister the lowest priority node in the least staked network
Expand Down
12 changes: 9 additions & 3 deletions pallets/subspace/src/set_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ impl<T: Config> Pallet<T> {

// --- 7. Ensure that the passed uids are valid for the network.
ensure!(
uids.iter().all(|&uid| Self::is_uid_exist_on_network(netuid, uid)),
uids.iter().all(|&uid| Self::uid_exist_on_network(netuid, uid)),
Error::<T>::InvalidUid
);

// --- 8. Check the allowed length of uids.
let min_allowed_length: usize = Self::get_min_allowed_weights(netuid) as usize;
let max_allowed_length: usize = Self::get_max_allowed_weights(netuid) as usize;
let max_allowed_length: usize =
MaxAllowedWeights::<T>::get(netuid).min(N::<T>::get(netuid)) as usize;
ensure!(
uids.len() >= min_allowed_length && uids.len() <= max_allowed_length,
Error::<T>::InvalidUidsLength
Expand All @@ -71,7 +72,7 @@ impl<T: Config> Pallet<T> {
ensure!(!uids.contains(&uid), Error::<T>::NoSelfWeight);

// --- 10. Get the stake for the key.
let stake: u64 = Self::get_stake_for_key(netuid, &key);
let stake: u64 = Stake::<T>::get(netuid, &key);

// --- 11. Check if the stake per weight is greater than the required minimum stake.
let min_stake_per_weight: u64 = MinWeightStake::<T>::get();
Expand Down Expand Up @@ -107,6 +108,11 @@ impl<T: Config> Pallet<T> {
Ok(())
}

// Returns true if the uid is set on the network.
pub fn uid_exist_on_network(netuid: u16, uid: u16) -> bool {
Keys::<T>::contains_key(netuid, uid)
}

// Implace normalizes the passed positive integer weights so that they sum to u16 max value.
pub fn normalize_weights(weights: Vec<u16>) -> Vec<u16> {
let sum: u64 = weights.iter().map(|&x| x as u64).sum();
Expand Down
8 changes: 4 additions & 4 deletions pallets/subspace/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl<T: Config> Pallet<T> {
// -- 5. Check before values
let stake_before_add: u64 = Self::get_stake_to_module(netuid, &key, &module_key.clone());
let balance_before_add: u64 = Self::get_balance_u64(&key);
let module_stake_before_add: u64 = Self::get_stake_for_key(netuid, &module_key);
let module_stake_before_add: u64 = Stake::<T>::get(netuid, &module_key);

// --- 6. We remove the balance from the key.
Self::remove_balance_from_account(&key, removed_balance_as_currency.unwrap())?;
Expand All @@ -195,7 +195,7 @@ impl<T: Config> Pallet<T> {
// -- 8. Check after values
let stake_after_add: u64 = Self::get_stake_to_module(netuid, &key, &module_key.clone());
let balance_after_add: u64 = Self::get_balance_u64(&key);
let module_stake_after_add = Self::get_stake_for_key(netuid, &module_key);
let module_stake_after_add = Stake::<T>::get(netuid, &module_key);

// -- 9. Make sure everything went as expected.
// Otherwise these ensurers will revert the storage changes.
Expand Down Expand Up @@ -250,7 +250,7 @@ impl<T: Config> Pallet<T> {
// -- 5. Check before values
let stake_before_remove: u64 = Self::get_stake_to_module(netuid, &key, &module_key.clone());
let balance_before_remove: u64 = Self::get_balance_u64(&key);
let module_stake_before_remove: u64 = Self::get_stake_for_key(netuid, &module_key);
let module_stake_before_remove: u64 = Stake::<T>::get(netuid, &module_key);

// --- 6. We remove the balance from the key.
Self::decrease_stake(netuid, &key, &module_key, amount);
Expand All @@ -261,7 +261,7 @@ impl<T: Config> Pallet<T> {
// --- 8. Check after values
let stake_after_remove: u64 = Self::get_stake_to_module(netuid, &key, &module_key.clone());
let balance_after_remove: u64 = Self::get_balance_u64(&key);
let module_stake_after_remove = Self::get_stake_for_key(netuid, &module_key);
let module_stake_after_remove = Stake::<T>::get(netuid, &module_key);

// -- 9. Make sure everything went as expected.
// Otherwise these ensurers will revert the storage changes.
Expand Down
26 changes: 12 additions & 14 deletions pallets/subspace/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \
let subnet_params = Self::subnet_params(netuid);

// get the amount of modules
let n: u16 = Self::get_subnet_n(netuid);
let n: u16 = N::<T>::get(netuid);
let current_block: u64 = Self::get_current_block_number();

// if there are no modules, then return
Expand All @@ -113,18 +113,16 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \
}

// FOUNDER DIVIDENDS
let founder_key = Self::get_founder(netuid);
let founder_key = Founder::<T>::get(netuid);
let (token_emission, founder_emission) =
Self::calculate_founder_emission(netuid, token_emission);

// STAKE
let uid_key_tuples: Vec<(u16, T::AccountId)> = Self::get_uid_key_tuples(netuid);
let total_stake_u64: u64 = Self::get_total_subnet_stake(netuid).max(1);

let stake_u64: Vec<u64> = uid_key_tuples
.iter()
.map(|(_, key)| Self::get_stake_for_key(netuid, key))
.collect();
let stake_u64: Vec<u64> =
uid_key_tuples.iter().map(|(_, key)| Stake::<T>::get(netuid, key)).collect();

let stake_f64: Vec<I64F64> = stake_u64
.iter()
Expand Down Expand Up @@ -154,7 +152,7 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \

// TRUST
// trust that acts as a multiplier for the incentive
let trust_ratio: u16 = Self::get_trust_ratio(netuid);
let trust_ratio: u16 = TrustRatio::<T>::get(netuid);
if trust_ratio > 0 {
let trust_share: I32F32 = I32F32::from_num(trust_ratio) / I32F32::from_num(100);
let incentive_share: I32F32 = I32F32::from_num(1.0).saturating_sub(trust_share);
Expand Down Expand Up @@ -205,7 +203,7 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \
netuid: u16,
) -> (Vec<I64F64>, Vec<I64F64>) {
let incentive_ratio: I64F64 =
I64F64::from_num(Self::get_incentive_ratio(netuid) as u64) / I64F64::from_num(100);
I64F64::from_num(IncentiveRatio::<T>::get(netuid) as u64) / I64F64::from_num(100);
let dividend_ratio: I64F64 = I64F64::from_num(1.0) - incentive_ratio;

let incentive_emission_float: Vec<I64F64> = incentive
Expand Down Expand Up @@ -512,7 +510,7 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \
stake_f64: &[I64F64],
total_stake_u64: u64,
) -> Vec<Vec<(u16, I32F32)>> {
let last_update_vector = Self::get_last_update(netuid);
let last_update_vector = LastUpdate::<T>::get(netuid);
let min_weight_stake_f64 = I64F64::from_num(global_params.min_weight_stake);
let mut weights: Vec<Vec<(u16, u16)>> = vec![vec![]; n as usize];

Expand Down Expand Up @@ -550,7 +548,7 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \
}

fn calculate_founder_emission(netuid: u16, mut token_emission: u64) -> (u64, u64) {
let founder_share: u16 = Self::get_founder_share(netuid);
let founder_share: u16 = FounderShare::<T>::get(netuid).min(100);
if founder_share == 0u16 {
return (token_emission, 0);
}
Expand All @@ -565,14 +563,14 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \
}

pub fn get_block_at_registration(netuid: u16) -> Vec<u64> {
let n = Self::get_subnet_n(netuid) as usize;
let n = N::<T>::get(netuid) as usize;
let mut block_at_registration: Vec<u64> = vec![0; n];

for (module_uid, block) in block_at_registration.iter_mut().enumerate() {
let module_uid = module_uid as u16;

if Keys::<T>::contains_key(netuid, module_uid) {
*block = Self::get_module_registration_block(netuid, module_uid);
*block = RegistrationBlock::<T>::get(netuid, module_uid);
}
}

Expand Down Expand Up @@ -622,15 +620,15 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \
target_registrations_per_interval: u16,
) {
if block_number % u64::from(target_registrations_interval) == 0 {
let current_burn = Self::get_burn(netuid);
let current_burn = Burn::<T>::get(netuid);

let adjusted_burn = Self::adjust_burn(
current_burn,
registrations_this_interval,
target_registrations_per_interval,
);

Self::set_burn(netuid, adjusted_burn);
Burn::<T>::insert(netuid, adjusted_burn);

// reset the registrations
RegistrationsThisInterval::<T>::insert(netuid, 0);
Expand Down
14 changes: 7 additions & 7 deletions pallets/subspace/src/step/yuma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use substrate_fixed::types::{I32F32, I64F64, I96F32};

use crate::{
math::*, vec, Active, Bonds, BondsMovingAverage, Config, Consensus, Dividends, Emission,
Incentive, Kappa, Keys, MaxAllowedValidators, MaxWeightAge, Pallet, PruningScores, Rank, Stake,
Trust, Uids, ValidatorPermits, ValidatorTrust, Weights,
Founder, Incentive, Kappa, Keys, LastUpdate, MaxAllowedValidators, MaxWeightAge, Pallet,
PruningScores, Rank, Stake, Trust, Uids, ValidatorPermits, ValidatorTrust, Weights, N,
};
use frame_support::ensure;
use sp_std::vec::Vec;
Expand Down Expand Up @@ -42,12 +42,12 @@ impl<T: Config> YumaCalc<T> {
let validator_permits = ValidatorPermits::<T>::get(netuid);
let validator_forbids = validator_permits.iter().map(|&b| !b).collect();

let founder_key = Pallet::<T>::get_founder(netuid);
let founder_key = Founder::<T>::get(netuid);
let (to_be_emitted, founder_emission) =
Pallet::<T>::calculate_founder_emission(netuid, to_be_emitted);

Self {
module_count: Pallet::<T>::get_subnet_n(netuid),
module_count: N::<T>::get(netuid),
netuid,
kappa: Pallet::<T>::get_float_kappa(),

Expand All @@ -57,7 +57,7 @@ impl<T: Config> YumaCalc<T> {

current_block: Pallet::<T>::get_current_block_number(),
activity_cutoff: MaxWeightAge::<T>::get(netuid),
last_update: Pallet::<T>::get_last_update(netuid),
last_update: LastUpdate::<T>::get(netuid),
block_at_registration: Pallet::<T>::get_block_at_registration(netuid),

validator_forbids,
Expand Down Expand Up @@ -660,7 +660,7 @@ impl<T: Config> Pallet<T> {
}

fn get_weights_sparse(netuid: u16) -> Vec<Vec<(u16, I32F32)>> {
let n = Self::get_subnet_n(netuid) as usize;
let n = N::<T>::get(netuid) as usize;
let mut weights: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
for (uid_i, weights_i) in Weights::<T>::iter_prefix(netuid) {
if uid_i >= n as u16 {
Expand All @@ -678,7 +678,7 @@ impl<T: Config> Pallet<T> {
}

fn get_bonds_sparse(netuid: u16) -> Vec<Vec<(u16, I32F32)>> {
let n: usize = Self::get_subnet_n(netuid) as usize;
let n: usize = N::<T>::get(netuid) as usize;
let mut bonds: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
for (uid_i, bonds_i) in Bonds::<T>::iter_prefix(netuid) {
for (uid_j, bonds_ij) in bonds_i {
Expand Down
Loading

0 comments on commit db8a19b

Please sign in to comment.