diff --git a/pallets/subspace/src/lib.rs b/pallets/subspace/src/lib.rs index 26f3cd3c6..31b032a9f 100644 --- a/pallets/subspace/src/lib.rs +++ b/pallets/subspace/src/lib.rs @@ -117,7 +117,7 @@ pub mod pallet { 23148148148 } #[pallet::storage] // --- ITEM ( unit_emission ) - pub(super) type UnitEmission = StorageValue<_, u64, ValueQuery, DefaultUnitEmission>; + pub type UnitEmission = StorageValue<_, u64, ValueQuery, DefaultUnitEmission>; #[pallet::type_value] pub fn DefaultAdjustmentAlpha() -> u64 { diff --git a/pallets/subspace/src/module.rs b/pallets/subspace/src/module.rs index e221d5dd4..593821394 100644 --- a/pallets/subspace/src/module.rs +++ b/pallets/subspace/src/module.rs @@ -148,7 +148,7 @@ impl Pallet { changeset: ModuleChangeset, ) -> Result { // 1. Get the next uid. This is always equal to subnetwork_n. - let uid: u16 = Self::get_subnet_n(netuid); + let uid: u16 = N::::get(netuid); let block_number = Self::get_current_block_number(); log::debug!("append_module( netuid: {netuid:?} | uid: {key:?} | new_key: {uid:?})"); @@ -186,7 +186,7 @@ impl Pallet { /// 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::::get(netuid); if n == 0 { // No modules in the network. return; @@ -326,7 +326,7 @@ impl Pallet { .collect(); let stake_from: BTreeMap = StakeFrom::::get(netuid, key); - let registration_block = Self::get_registration_block_for_uid(netuid, uid); + let registration_block = RegistrationBlock::::get(netuid, uid); ModuleStats { stake_from, diff --git a/pallets/subspace/src/registration.rs b/pallets/subspace/src/registration.rs index 939c2f142..78b6a7787 100644 --- a/pallets/subspace/src/registration.rs +++ b/pallets/subspace/src/registration.rs @@ -131,7 +131,7 @@ impl Pallet { // --- 5. Ensure the caller has enough stake to register. let min_stake: u64 = MinStake::::get(netuid); - let current_burn: u64 = Self::get_burn(netuid); + let current_burn: u64 = Burn::::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!( @@ -232,7 +232,7 @@ impl Pallet { 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::::get(netuid) as u64; // Get all the UIDs and their registration blocks from the storage let mut uids: Vec<_> = RegistrationBlock::::iter_prefix(netuid).collect(); @@ -384,12 +384,12 @@ impl Pallet { stake: u64, changeset: SubnetChangeset, ) -> Result { - let num_subnets: u16 = Self::num_subnets(); + let num_subnets: u16 = TotalSubnets::::get(); let max_subnets: u16 = MaxAllowedSubnets::::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::::NotEnoughStakeToStartNetwork); Self::remove_subnet(min_stake_netuid); @@ -409,13 +409,13 @@ impl Pallet { /// 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::::get(netuid) >= MaxAllowedUids::::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::::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 diff --git a/pallets/subspace/src/set_weights.rs b/pallets/subspace/src/set_weights.rs index 8df946bd4..3376ca9c8 100644 --- a/pallets/subspace/src/set_weights.rs +++ b/pallets/subspace/src/set_weights.rs @@ -55,13 +55,14 @@ impl Pallet { // --- 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::::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::::get(netuid).min(N::::get(netuid)) as usize; ensure!( uids.len() >= min_allowed_length && uids.len() <= max_allowed_length, Error::::InvalidUidsLength @@ -71,7 +72,7 @@ impl Pallet { ensure!(!uids.contains(&uid), Error::::NoSelfWeight); // --- 10. Get the stake for the key. - let stake: u64 = Self::get_stake_for_key(netuid, &key); + let stake: u64 = Stake::::get(netuid, &key); // --- 11. Check if the stake per weight is greater than the required minimum stake. let min_stake_per_weight: u64 = MinWeightStake::::get(); @@ -107,6 +108,11 @@ impl Pallet { Ok(()) } + // Returns true if the uid is set on the network. + pub fn uid_exist_on_network(netuid: u16, uid: u16) -> bool { + Keys::::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) -> Vec { let sum: u64 = weights.iter().map(|&x| x as u64).sum(); diff --git a/pallets/subspace/src/staking.rs b/pallets/subspace/src/staking.rs index e25ef5072..82588b7d4 100644 --- a/pallets/subspace/src/staking.rs +++ b/pallets/subspace/src/staking.rs @@ -184,7 +184,7 @@ impl Pallet { // -- 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::::get(netuid, &module_key); // --- 6. We remove the balance from the key. Self::remove_balance_from_account(&key, removed_balance_as_currency.unwrap())?; @@ -195,7 +195,7 @@ impl Pallet { // -- 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::::get(netuid, &module_key); // -- 9. Make sure everything went as expected. // Otherwise these ensurers will revert the storage changes. @@ -250,7 +250,7 @@ impl Pallet { // -- 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::::get(netuid, &module_key); // --- 6. We remove the balance from the key. Self::decrease_stake(netuid, &key, &module_key, amount); @@ -261,7 +261,7 @@ impl Pallet { // --- 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::::get(netuid, &module_key); // -- 9. Make sure everything went as expected. // Otherwise these ensurers will revert the storage changes. diff --git a/pallets/subspace/src/step.rs b/pallets/subspace/src/step.rs index f346d44c8..cf4912ba8 100644 --- a/pallets/subspace/src/step.rs +++ b/pallets/subspace/src/step.rs @@ -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::::get(netuid); let current_block: u64 = Self::get_current_block_number(); // if there are no modules, then return @@ -113,7 +113,7 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \ } // FOUNDER DIVIDENDS - let founder_key = Self::get_founder(netuid); + let founder_key = Founder::::get(netuid); let (token_emission, founder_emission) = Self::calculate_founder_emission(netuid, token_emission); @@ -121,10 +121,8 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \ 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 = uid_key_tuples - .iter() - .map(|(_, key)| Self::get_stake_for_key(netuid, key)) - .collect(); + let stake_u64: Vec = + uid_key_tuples.iter().map(|(_, key)| Stake::::get(netuid, key)).collect(); let stake_f64: Vec = stake_u64 .iter() @@ -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::::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); @@ -205,7 +203,7 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \ netuid: u16, ) -> (Vec, Vec) { let incentive_ratio: I64F64 = - I64F64::from_num(Self::get_incentive_ratio(netuid) as u64) / I64F64::from_num(100); + I64F64::from_num(IncentiveRatio::::get(netuid) as u64) / I64F64::from_num(100); let dividend_ratio: I64F64 = I64F64::from_num(1.0) - incentive_ratio; let incentive_emission_float: Vec = incentive @@ -512,7 +510,7 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \ stake_f64: &[I64F64], total_stake_u64: u64, ) -> Vec> { - let last_update_vector = Self::get_last_update(netuid); + let last_update_vector = LastUpdate::::get(netuid); let min_weight_stake_f64 = I64F64::from_num(global_params.min_weight_stake); let mut weights: Vec> = vec![vec![]; n as usize]; @@ -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::::get(netuid).min(100); if founder_share == 0u16 { return (token_emission, 0); } @@ -565,14 +563,14 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \ } pub fn get_block_at_registration(netuid: u16) -> Vec { - let n = Self::get_subnet_n(netuid) as usize; + let n = N::::get(netuid) as usize; let mut block_at_registration: Vec = vec![0; n]; for (module_uid, block) in block_at_registration.iter_mut().enumerate() { let module_uid = module_uid as u16; if Keys::::contains_key(netuid, module_uid) { - *block = Self::get_module_registration_block(netuid, module_uid); + *block = RegistrationBlock::::get(netuid, module_uid); } } @@ -622,7 +620,7 @@ 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::::get(netuid); let adjusted_burn = Self::adjust_burn( current_burn, @@ -630,7 +628,7 @@ failed to run yuma consensus algorithm: {err:?}, skipping this block. \ target_registrations_per_interval, ); - Self::set_burn(netuid, adjusted_burn); + Burn::::insert(netuid, adjusted_burn); // reset the registrations RegistrationsThisInterval::::insert(netuid, 0); diff --git a/pallets/subspace/src/step/yuma.rs b/pallets/subspace/src/step/yuma.rs index 491281783..08831001b 100644 --- a/pallets/subspace/src/step/yuma.rs +++ b/pallets/subspace/src/step/yuma.rs @@ -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; @@ -42,12 +42,12 @@ impl YumaCalc { let validator_permits = ValidatorPermits::::get(netuid); let validator_forbids = validator_permits.iter().map(|&b| !b).collect(); - let founder_key = Pallet::::get_founder(netuid); + let founder_key = Founder::::get(netuid); let (to_be_emitted, founder_emission) = Pallet::::calculate_founder_emission(netuid, to_be_emitted); Self { - module_count: Pallet::::get_subnet_n(netuid), + module_count: N::::get(netuid), netuid, kappa: Pallet::::get_float_kappa(), @@ -57,7 +57,7 @@ impl YumaCalc { current_block: Pallet::::get_current_block_number(), activity_cutoff: MaxWeightAge::::get(netuid), - last_update: Pallet::::get_last_update(netuid), + last_update: LastUpdate::::get(netuid), block_at_registration: Pallet::::get_block_at_registration(netuid), validator_forbids, @@ -660,7 +660,7 @@ impl Pallet { } fn get_weights_sparse(netuid: u16) -> Vec> { - let n = Self::get_subnet_n(netuid) as usize; + let n = N::::get(netuid) as usize; let mut weights: Vec> = vec![vec![]; n]; for (uid_i, weights_i) in Weights::::iter_prefix(netuid) { if uid_i >= n as u16 { @@ -678,7 +678,7 @@ impl Pallet { } fn get_bonds_sparse(netuid: u16) -> Vec> { - let n: usize = Self::get_subnet_n(netuid) as usize; + let n: usize = N::::get(netuid) as usize; let mut bonds: Vec> = vec![vec![]; n]; for (uid_i, bonds_i) in Bonds::::iter_prefix(netuid) { for (uid_j, bonds_ij) in bonds_i { diff --git a/pallets/subspace/src/subnet.rs b/pallets/subspace/src/subnet.rs index 24f3940c6..201bf4f53 100644 --- a/pallets/subspace/src/subnet.rs +++ b/pallets/subspace/src/subnet.rs @@ -10,6 +10,10 @@ use sp_runtime::{BoundedVec, DispatchError}; use sp_std::vec::Vec; use substrate_fixed::types::I64F64; +// --------------------------------- +// Subnet Parameters +// --------------------------------- + #[derive(Debug)] pub struct SubnetChangeset { params: SubnetParams, @@ -134,38 +138,6 @@ impl SubnetChangeset { } impl Pallet { - pub fn do_update_subnet( - origin: T::RuntimeOrigin, - netuid: u16, - changeset: SubnetChangeset, - ) -> DispatchResult { - let key = ensure_signed(origin)?; - - // only the founder can update the network on authority mode - ensure!( - Self::is_subnet_founder(netuid, &key), - Error::::NotFounder - ); - - // Ensure that the subnet is not in a `Vote` mode. - // Update by founder can be executed only in `Authority` mode. - ensure!( - VoteModeSubnet::::get(netuid) == VoteMode::Authority, - Error::::InvalidVoteMode - ); - - ensure!( - Self::if_subnet_netuid_exists(netuid), - Error::::NetuidDoesNotExist - ); - - // apply the changeset - changeset.apply(netuid)?; - - // --- 16. Ok and done. - Ok(()) - } - pub fn subnet_params(netuid: u16) -> SubnetParams { SubnetParams { founder: Founder::::get(netuid), @@ -186,166 +158,9 @@ impl Pallet { } } - pub fn if_subnet_exist(netuid: u16) -> bool { - N::::contains_key(netuid) - } - - pub fn get_burn(netuid: u16) -> u64 { - Burn::::get(netuid) - } - - pub fn set_burn(netuid: u16, burn: u64) { - Burn::::insert(netuid, burn); - } - - // get the least staked network - pub fn least_staked_netuid() -> (u16, u64) { - TotalStake::::iter().min_by_key(|(_, stake)| *stake).unwrap_or_else(|| { - let stake = u64::MAX; - let netuid = MaxAllowedSubnets::::get() - 1; - (netuid, stake) - }) - } - - fn set_max_allowed_uids(netuid: u16, mut max_allowed_uids: u16) { - let n: u16 = Self::get_subnet_n(netuid); - if max_allowed_uids < n { - // limit it at 256 at a time - let mut remainder_n: u16 = n - max_allowed_uids; - let max_remainder = 256; - if remainder_n > max_remainder { - // remove the modules in small amounts, as this can be a heavy load on the chain - remainder_n = max_remainder; - max_allowed_uids = n - remainder_n; - } - // remove the modules by adding the to the deregister queue - for i in 0..remainder_n { - let next_uid: u16 = n - 1 - i; - Self::remove_module(netuid, next_uid); - } - } - - MaxAllowedUids::::insert(netuid, max_allowed_uids); - } - - pub fn is_subnet_founder(netuid: u16, key: &T::AccountId) -> bool { - Founder::::get(netuid) == *key - } - - pub fn set_unit_emission(unit_emission: u64) { - UnitEmission::::put(unit_emission) - } - - // Returns the total amount of stake in the staking table. - // TODO: refactor the halving logic, it's not correct. - pub fn get_total_emission_per_block() -> u64 { - let total_stake: u64 = Self::total_stake(); - let unit_emission: u64 = UnitEmission::::get(); - let mut emission_per_block: u64 = unit_emission; // assuming 8 second block times - let halving_total_stake_checkpoints: Vec = - [10_000_000, 20_000_000, 30_000_000, 40_000_000] - .iter() - .map(|x| x * unit_emission) - .collect(); - for (i, having_stake) in halving_total_stake_checkpoints.iter().enumerate() { - let halving_factor: u64 = 2u64.pow((i) as u32); - if total_stake < *having_stake { - emission_per_block /= halving_factor; - break; - } - } - - emission_per_block - } - - /// Empties out all: - /// emission, dividends, incentives, trust on the specific netuid. - fn deactivate_subnet(netuid: u16) { - let module_count = Self::get_subnet_n(netuid) as usize; - let zeroed = vec![0; module_count]; - - SubnetEmission::::insert(netuid, 0); - - Active::::insert(netuid, vec![true; module_count]); - Consensus::::insert(netuid, &zeroed); - Dividends::::insert(netuid, &zeroed); - Emission::::insert(netuid, vec![0; module_count]); - Incentive::::insert(netuid, &zeroed); - PruningScores::::insert(netuid, &zeroed); - Rank::::insert(netuid, &zeroed); - Trust::::insert(netuid, &zeroed); - ValidatorPermits::::insert(netuid, vec![false; module_count]); - ValidatorTrust::::insert(netuid, &zeroed); - } - - pub fn calculate_network_emission(netuid: u16, subnet_stake_threshold: Percent) -> u64 { - let subnet_stake: I64F64 = I64F64::from_num(Self::get_total_subnet_stake(netuid)); - let total_stake: I64F64 = Self::adjust_total_stake(subnet_stake_threshold); - - log::trace!( - "calculating subnet emission {netuid} with stake {subnet_stake}, \ -total stake {total_stake}, \ -threshold {subnet_stake_threshold:?}" - ); - - let subnet_ratio = if total_stake > I64F64::from_num(0) { - subnet_stake / total_stake - } else { - I64F64::from_num(0) - }; - - // Convert subnet_stake_threshold from % to I64F64 - let subnet_stake_threshold_i64f64 = - I64F64::from_num(subnet_stake_threshold.deconstruct()) / I64F64::from_num(100); - - // Check if subnet_ratio meets the subnet_stake_threshold, - // or netuid is not the general subnet - if subnet_ratio < subnet_stake_threshold_i64f64 && netuid != 0 { - // Return early if the threshold is not met, - // this prevents emission gapping, of subnets that don't meet emission threshold - Self::deactivate_subnet(netuid); - log::trace!("subnet {netuid} is not eligible for yuma consensus"); - return 0; - } - - let total_emission_per_block: u64 = Self::get_total_emission_per_block(); - - let token_emission: u64 = - (subnet_ratio * I64F64::from_num(total_emission_per_block)).to_num::(); - - SubnetEmission::::insert(netuid, token_emission); - - token_emission - } - - // This is the total stake of the network without subnets that can not get emission - // TODO: could be optimized - pub fn adjust_total_stake(subnet_stake_threshold: Percent) -> I64F64 { - let total_global_stake = I64F64::from_num(Self::total_stake()); - if total_global_stake == 0 { - return I64F64::from_num(0); - } - - let mut total_stake = I64F64::from_num(0); - let subnet_stake_threshold_i64f64 = - I64F64::from_num(subnet_stake_threshold.deconstruct()) / I64F64::from_num(100); - // Iterate over all subnets - for netuid in N::::iter_keys() { - let subnet_stake: I64F64 = I64F64::from_num(Self::get_total_subnet_stake(netuid)); - if subnet_stake == 0 { - continue; - } - let subnet_ratio = subnet_stake / total_global_stake; - // Check if subnet_ratio meets the subnet_stake_threshold, - // or the netuid is the general subnet - if subnet_ratio >= subnet_stake_threshold_i64f64 || netuid == 0 { - // Add subnet_stake to total_stake if it meets the threshold - total_stake += subnet_stake; - } - } - - total_stake - } + // --------------------------------- + // Adding Subnets + // --------------------------------- pub fn add_subnet( changeset: SubnetChangeset, @@ -374,38 +189,10 @@ threshold {subnet_stake_threshold:?}" Ok(netuid) } - // Initializes a new subnetwork under netuid with parameters. - pub fn subnet_name_exists(name: Vec) -> bool { - for (_, _name) in as IterableStorageMap>>::iter() { - if _name == name { - return true; - } - } - false - } - pub fn if_subnet_netuid_exists(netuid: u16) -> bool { - SubnetNames::::contains_key(netuid) - } - - pub fn does_subnet_name_exist(name: &[u8]) -> bool { - SubnetNames::::iter().any(|(_, n)| n == name) - } - - pub fn get_netuid_for_name(name: &[u8]) -> Option { - SubnetNames::::iter().find(|(_, n)| n == name).map(|(id, _)| id) - } - - pub fn remove_netuid_stake_strorage(netuid: u16) { - // --- 1. Erase network stake, and remove network from list of networks. - for key in Stake::::iter_key_prefix(netuid) { - Self::remove_stake_from_storage(netuid, &key); - } - - // --- 4. Remove all stake. - Stake::::remove_prefix(netuid, None); - TotalStake::::remove(netuid); - } + // --------------------------------- + // Removing subnets + // --------------------------------- pub fn remove_subnet(netuid: u16) -> u16 { // TODO: handle errors @@ -416,7 +203,7 @@ threshold {subnet_stake_threshold:?}" return 0; } - Self::remove_netuid_stake_strorage(netuid); + Self::remove_netuid_stake_storage(netuid); SubnetNames::::remove(netuid); MaxWeightAge::::remove(netuid); @@ -469,210 +256,299 @@ threshold {subnet_stake_threshold:?}" netuid } + // --------------------------------- + // Updating Subnets + // --------------------------------- - /// Returns the number of filled slots on a network. - pub fn get_subnet_n(netuid: u16) -> u16 { - N::::get(netuid) - } + pub fn do_update_subnet( + origin: T::RuntimeOrigin, + netuid: u16, + changeset: SubnetChangeset, + ) -> DispatchResult { + let key = ensure_signed(origin)?; - // Returns true if the uid is set on the network. - pub fn is_uid_exist_on_network(netuid: u16, uid: u16) -> bool { - Keys::::contains_key(netuid, uid) - } + // -- 1. Make sure the netuid exists + ensure!( + SubnetNames::::contains_key(netuid), + Error::::NetuidDoesNotExist + ); - pub fn key_registered(netuid: u16, key: &T::AccountId) -> bool { - Uids::::contains_key(netuid, key) - || Keys::::iter_prefix_values(netuid).any(|k| &k == key) + // --2. Ensury Authority - only the founder can update the network on authority mode. + ensure!(Founder::::get(netuid) == key, Error::::NotFounder); + + // --3. Ensure that the subnet is not in a `Vote` mode. + // Update by founder can be executed only in `Authority` mode. + ensure!( + VoteModeSubnet::::get(netuid) == VoteMode::Authority, + Error::::InvalidVoteMode + ); + + // -4. Apply the changeset. + changeset.apply(netuid)?; + + // --- 5. Ok and done. + Ok(()) } - pub fn is_key_registered_on_any_network(key: &T::AccountId) -> bool { - for netuid in Self::netuids() { - if Uids::::contains_key(netuid, key) { - return true; - } + // --------------------------------- + // Subnet Emission + // --------------------------------- + + pub fn calculate_network_emission(netuid: u16, subnet_stake_threshold: Percent) -> u64 { + let subnet_stake: I64F64 = I64F64::from_num(Self::get_total_subnet_stake(netuid)); + let total_stake: I64F64 = Self::adjust_total_stake(subnet_stake_threshold); + + log::trace!( + "calculating subnet emission {netuid} with stake {subnet_stake}, \ + total stake {total_stake}, \ + threshold {subnet_stake_threshold:?}" + ); + + let subnet_ratio = if total_stake > I64F64::from_num(0) { + subnet_stake / total_stake + } else { + I64F64::from_num(0) + }; + + // Convert subnet_stake_threshold from % to I64F64 + let subnet_stake_threshold_i64f64 = + I64F64::from_num(subnet_stake_threshold.deconstruct()) / I64F64::from_num(100); + + // Check if subnet_ratio meets the subnet_stake_threshold, + // or netuid is not the general subnet + if subnet_ratio < subnet_stake_threshold_i64f64 && netuid != 0 { + // Return early if the threshold is not met, + // this prevents emission gapping, of subnets that don't meet emission threshold + Self::deactivate_subnet(netuid); + log::trace!("subnet {netuid} is not eligible for yuma consensus"); + return 0; } - false - } - // Returs the key under the network uid as a Result. Ok if the uid is taken. - pub fn get_key_for_uid(netuid: u16, module_uid: u16) -> Option { - Keys::::try_get(netuid, module_uid).ok() - } + let total_emission_per_block: u64 = Self::get_total_emission_per_block(); - // Returns the uid of the key in the network as a Result. Ok if the key has a slot. - pub fn get_uid_for_key(netuid: u16, key: &T::AccountId) -> u16 { - Uids::::get(netuid, key).unwrap_or(0) - } + let token_emission: u64 = + (subnet_ratio * I64F64::from_num(total_emission_per_block)).to_num::(); - pub fn get_trust_ratio(netuid: u16) -> u16 { - TrustRatio::::get(netuid) - } + SubnetEmission::::insert(netuid, token_emission); - pub fn get_stake_for_key(netuid: u16, key: &T::AccountId) -> u64 { - Stake::::get(netuid, key) + token_emission } - // Return the total number of subnetworks available on the chain. - pub fn num_subnets() -> u16 { - TotalSubnets::::get() - } + // Returns the total amount of stake in the staking table. + // TODO: refactor the halving logic, can be completelly optimized. + pub fn get_total_emission_per_block() -> u64 { + let total_stake: u64 = Self::total_stake(); + let unit_emission: u64 = UnitEmission::::get(); + let mut emission_per_block: u64 = unit_emission; // assuming 8 second block times + let halving_total_stake_checkpoints: Vec = + [10_000_000, 20_000_000, 30_000_000, 40_000_000] + .iter() + .map(|x| x * unit_emission) + .collect(); + for (i, having_stake) in halving_total_stake_checkpoints.iter().enumerate() { + let halving_factor: u64 = 2u64.pow((i) as u32); + if total_stake < *having_stake { + emission_per_block /= halving_factor; + break; + } + } - pub fn netuids() -> Vec { - as IterableStorageMap>::iter() - .map(|(netuid, _)| netuid) - .collect() + emission_per_block } - pub fn get_tempo(netuid: u16) -> u16 { - Tempo::::get(netuid) - } + // This is the total stake of the network without subnets that can not get emission + // TODO: could be optimized + pub fn adjust_total_stake(subnet_stake_threshold: Percent) -> I64F64 { + let total_global_stake = I64F64::from_num(Self::total_stake()); + if total_global_stake == 0 { + return I64F64::from_num(0); + } - // FOUNDER SHARE (MAX IS 100) - pub fn get_founder_share(netuid: u16) -> u16 { - FounderShare::::get(netuid).min(100) - } + let mut total_stake = I64F64::from_num(0); + let subnet_stake_threshold_i64f64 = + I64F64::from_num(subnet_stake_threshold.deconstruct()) / I64F64::from_num(100); + // Iterate over all subnets + for netuid in N::::iter_keys() { + let subnet_stake: I64F64 = I64F64::from_num(Self::get_total_subnet_stake(netuid)); + if subnet_stake == 0 { + continue; + } + let subnet_ratio = subnet_stake / total_global_stake; + // Check if subnet_ratio meets the subnet_stake_threshold, + // or the netuid is the general subnet + if subnet_ratio >= subnet_stake_threshold_i64f64 || netuid == 0 { + // Add subnet_stake to total_stake if it meets the threshold + total_stake += subnet_stake; + } + } - pub fn get_registration_block_for_uid(netuid: u16, uid: u16) -> u64 { - RegistrationBlock::::get(netuid, uid) + total_stake } - pub fn get_incentive_ratio(netuid: u16) -> u16 { - IncentiveRatio::::get(netuid).min(100) - } + /// Empties out all: + /// emission, dividends, incentives, trust on the specific netuid. + fn deactivate_subnet(netuid: u16) { + let module_count = N::::get(netuid) as usize; + let zeroed = vec![0; module_count]; + + SubnetEmission::::insert(netuid, 0); - pub fn get_founder(netuid: u16) -> T::AccountId { - Founder::::get(netuid) + Active::::insert(netuid, vec![true; module_count]); + Consensus::::insert(netuid, &zeroed); + Dividends::::insert(netuid, &zeroed); + Emission::::insert(netuid, vec![0; module_count]); + Incentive::::insert(netuid, &zeroed); + PruningScores::::insert(netuid, &zeroed); + Rank::::insert(netuid, &zeroed); + Trust::::insert(netuid, &zeroed); + ValidatorPermits::::insert(netuid, vec![false; module_count]); + ValidatorTrust::::insert(netuid, &zeroed); } // --------------------------------- - // Global Getters + // Setters // --------------------------------- - pub fn get_current_block_number() -> u64 { - TryInto::try_into(>::block_number()) - .ok() - .expect("blockchain will not exceed 2^64 blocks; QED.") + fn set_max_allowed_uids(netuid: u16, mut max_allowed_uids: u16) { + let n: u16 = N::::get(netuid); + if max_allowed_uids < n { + // limit it at 256 at a time + let mut remainder_n: u16 = n - max_allowed_uids; + let max_remainder = 256; + if remainder_n > max_remainder { + // remove the modules in small amounts, as this can be a heavy load on the chain + remainder_n = max_remainder; + max_allowed_uids = n - remainder_n; + } + // remove the modules by adding the to the deregister queue + for i in 0..remainder_n { + let next_uid: u16 = n - 1 - i; + Self::remove_module(netuid, next_uid); + } + } + + MaxAllowedUids::::insert(netuid, max_allowed_uids); } pub fn set_last_update_for_uid(netuid: u16, uid: u16, last_update: u64) { - let mut updated_last_update_vec = Self::get_last_update(netuid); + let mut updated_last_update_vec = LastUpdate::::get(netuid); if (uid as usize) < updated_last_update_vec.len() { updated_last_update_vec[uid as usize] = last_update; LastUpdate::::insert(netuid, updated_last_update_vec); } } - pub fn get_emission_for_uid(netuid: u16, uid: u16) -> u64 { - Emission::::get(netuid).get(uid as usize).copied().unwrap_or_default() - } - pub fn get_incentive_for_uid(netuid: u16, uid: u16) -> u16 { - Incentive::::get(netuid).get(uid as usize).copied().unwrap_or_default() - } - pub fn get_dividends_for_uid(netuid: u16, uid: u16) -> u16 { - Dividends::::get(netuid).get(uid as usize).copied().unwrap_or_default() - } - pub fn get_last_update_for_uid(netuid: u16, uid: u16) -> u64 { - LastUpdate::::get(netuid).get(uid as usize).copied().unwrap_or_default() + // --------------------------------- + // Getters + // --------------------------------- + pub fn get_least_staked_netuid() -> (u16, u64) { + TotalStake::::iter() + .min_by_key(|(_, stake)| *stake) + .unwrap_or_else(|| (MaxAllowedSubnets::::get() - 1, u64::MAX)) } - pub fn set_global_max_allowed_subnets(max_allowed_subnets: u16) { - MaxAllowedSubnets::::put(max_allowed_subnets) + pub fn get_min_allowed_weights(netuid: u16) -> u16 { + let min_allowed_weights = MinAllowedWeights::::get(netuid); + min_allowed_weights.min(N::::get(netuid)) } - // --------------------------------- - // Subnetwork Getters - // --------------------------------- + pub fn get_uids(netuid: u16) -> Vec { + (0..N::::get(netuid)).collect() + } - pub fn get_module_registration_block(netuid: u16, uid: u16) -> u64 { - RegistrationBlock::::get(netuid, uid) + pub fn get_keys(netuid: u16) -> Vec { + Self::get_uids(netuid) + .into_iter() + .map(|uid| Self::get_key_for_uid(netuid, uid).unwrap()) + .collect() } - pub fn get_immunity_period(netuid: u16) -> u16 { - ImmunityPeriod::::get(netuid) + pub fn get_uid_key_tuples(netuid: u16) -> Vec<(u16, T::AccountId)> { + (0..N::::get(netuid)) + .map(|uid| (uid, Self::get_key_for_uid(netuid, uid).unwrap())) + .collect() } - pub fn get_min_allowed_weights(netuid: u16) -> u16 { - let min_allowed_weights = MinAllowedWeights::::get(netuid); - let n = Self::get_subnet_n(netuid); - // if n < min_allowed_weights, then return n - if n < min_allowed_weights { - n - } else { - min_allowed_weights - } + pub fn get_names(netuid: u16) -> Vec> { + as IterableStorageDoubleMap>>::iter_prefix(netuid) + .map(|(_, name)| name) + .collect() } - pub fn get_max_allowed_weights(netuid: u16) -> u16 { - let max_allowed_weights = MaxAllowedWeights::::get(netuid); - let n = Self::get_subnet_n(netuid); - // if n < min_allowed_weights, then return n - max_allowed_weights.min(n) + pub fn get_addresses(netuid: u16) -> Vec { + as IterableStorageDoubleMap>::iter_prefix(netuid) + .map(|(key, _)| key) + .collect() } - pub fn get_max_allowed_uids(netuid: u16) -> u16 { - MaxAllowedUids::::get(netuid) + pub fn get_netuid_for_name(name: &[u8]) -> Option { + SubnetNames::::iter().find(|(_, n)| n == name).map(|(id, _)| id) + } + // Returs the key under the network uid as a Result. Ok if the uid is taken. + pub fn get_key_for_uid(netuid: u16, module_uid: u16) -> Option { + Keys::::try_get(netuid, module_uid).ok() + } + // Returns the uid of the key in the network as a Result. Ok if the key has a slot. + pub fn get_uid_for_key(netuid: u16, key: &T::AccountId) -> u16 { + Uids::::get(netuid, key).unwrap_or(0) } - pub fn set_max_allowed_modules(max_allowed_modules: u16) { - MaxAllowedModules::::put(max_allowed_modules) + pub fn get_current_block_number() -> u64 { + TryInto::try_into(>::block_number()) + .ok() + .expect("blockchain will not exceed 2^64 blocks; QED.") } - pub fn get_uids(netuid: u16) -> Vec { - let n = Self::get_subnet_n(netuid); - (0..n).collect() + pub fn get_emission_for_uid(netuid: u16, uid: u16) -> u64 { + Emission::::get(netuid).get(uid as usize).copied().unwrap_or_default() } - pub fn get_keys(netuid: u16) -> Vec { - let uids: Vec = Self::get_uids(netuid); - let keys: Vec = - uids.iter().map(|uid| Self::get_key_for_uid(netuid, *uid).unwrap()).collect(); - keys + + pub fn get_incentive_for_uid(netuid: u16, uid: u16) -> u16 { + Incentive::::get(netuid).get(uid as usize).copied().unwrap_or_default() } - pub fn get_uid_key_tuples(netuid: u16) -> Vec<(u16, T::AccountId)> { - let n = Self::get_subnet_n(netuid); - let mut uid_key_tuples = Vec::<(u16, T::AccountId)>::new(); - for uid in 0..n { - let key = Self::get_key_for_uid(netuid, uid).unwrap(); - uid_key_tuples.push((uid, key)); - } - uid_key_tuples + pub fn get_dividends_for_uid(netuid: u16, uid: u16) -> u16 { + Dividends::::get(netuid).get(uid as usize).copied().unwrap_or_default() } - pub fn get_names(netuid: u16) -> Vec> { - let mut names = Vec::>::new(); - for (_uid, name) in - as IterableStorageDoubleMap>>::iter_prefix(netuid) - { - names.push(name); - } - names + pub fn get_last_update_for_uid(netuid: u16, uid: u16) -> u64 { + LastUpdate::::get(netuid).get(uid as usize).copied().unwrap_or_default() } - pub fn get_addresses(netuid: u16) -> Vec { - let mut addresses = Vec::::new(); - for (key, _uid) in - as IterableStorageDoubleMap>::iter_prefix(netuid) - { - addresses.push(key); - } - addresses + // --------------------------------- + // Utility + // --------------------------------- + + pub fn is_key_registered_on_any_network(key: &T::AccountId) -> bool { + Self::netuids().iter().any(|&netuid| Uids::::contains_key(netuid, key)) } - pub fn get_emissions(netuid: u16) -> Vec { - Emission::::get(netuid) + pub fn is_registered(netuid: u16, key: &T::AccountId) -> bool { + Uids::::contains_key(netuid, key) } - pub fn get_incentives(netuid: u16) -> Vec { - Incentive::::get(netuid) + + pub fn if_subnet_exist(netuid: u16) -> bool { + N::::contains_key(netuid) } - pub fn get_dividends(netuid: u16) -> Vec { - Dividends::::get(netuid) + pub fn key_registered(netuid: u16, key: &T::AccountId) -> bool { + Uids::::contains_key(netuid, key) + || Keys::::iter_prefix_values(netuid).any(|k| &k == key) } - pub fn get_last_update(netuid: u16) -> Vec { - LastUpdate::::get(netuid) + + pub fn netuids() -> Vec { + as IterableStorageMap>::iter() + .map(|(netuid, _)| netuid) + .collect() } - pub fn is_registered(netuid: u16, key: &T::AccountId) -> bool { - Uids::::contains_key(netuid, key) + pub fn remove_netuid_stake_storage(netuid: u16) { + // --- 1. Erase network stake, and remove network from list of networks. + Stake::::iter_key_prefix(netuid) + .for_each(|key| Self::remove_stake_from_storage(netuid, &key)); + + // --- 4. Remove all stake. + Stake::::remove_prefix(netuid, None); + TotalStake::::remove(netuid); } } diff --git a/pallets/subspace/tests/burn.rs b/pallets/subspace/tests/burn.rs index 87b84ec8e..03a96c061 100644 --- a/pallets/subspace/tests/burn.rs +++ b/pallets/subspace/tests/burn.rs @@ -2,7 +2,7 @@ mod mock; use frame_support::assert_ok; use mock::*; -use pallet_subspace::{global::BurnConfiguration, MaxRegistrationsPerBlock}; +use pallet_subspace::{global::BurnConfiguration, Burn, MaxRegistrationsPerBlock}; use sp_core::U256; // test subnet specific burn @@ -49,9 +49,9 @@ fn test_local_subnet_burn() { // this means avg. 0.166.. per block // burn has incrased by 90% > up - let subnet_zero_burn = SubspaceModule::get_burn(0); + let subnet_zero_burn = Burn::::get(0); assert_eq!(subnet_zero_burn, min_burn); - let subnet_one_burn = SubspaceModule::get_burn(1); + let subnet_one_burn = Burn::::get(1); assert!(min_burn < subnet_one_burn && subnet_one_burn < max_burn); }); } diff --git a/pallets/subspace/tests/delegate_staking.rs b/pallets/subspace/tests/delegate_staking.rs index 96d4c37c7..ba30ae27d 100644 --- a/pallets/subspace/tests/delegate_staking.rs +++ b/pallets/subspace/tests/delegate_staking.rs @@ -3,7 +3,7 @@ mod mock; use frame_support::assert_ok; use log::info; use mock::*; -use pallet_subspace::Tempo; +use pallet_subspace::{Dividends, Emission, Incentive, Tempo}; use sp_core::U256; use substrate_fixed::types::I64F64; @@ -116,9 +116,9 @@ fn test_ownership_ratio() { step_epoch(netuid); - let dividends = SubspaceModule::get_dividends(netuid); - let incentives = SubspaceModule::get_incentives(netuid); - let emissions = SubspaceModule::get_emissions(netuid); + let dividends = Dividends::::get(netuid); + let incentives = Incentive::::get(netuid); + let emissions = Emission::::get(netuid); info!("dividends: {dividends:?}"); info!("incentives: {incentives:?}"); diff --git a/pallets/subspace/tests/mock.rs b/pallets/subspace/tests/mock.rs index 3b5909feb..956ab9966 100644 --- a/pallets/subspace/tests/mock.rs +++ b/pallets/subspace/tests/mock.rs @@ -6,7 +6,10 @@ use frame_support::{ PalletId, }; use frame_system as system; -use pallet_subspace::{Address, BurnConfig, MaxRegistrationsPerBlock, Name}; +use pallet_subspace::{ + Address, BurnConfig, Dividends, Emission, Incentive, LastUpdate, MaxRegistrationsPerBlock, + Name, Stake, Tempo, N, +}; use sp_core::{H256, U256}; use sp_runtime::{ traits::{BlakeTwo256, IdentityLookup}, @@ -146,7 +149,7 @@ pub(crate) fn step_block(n: u16) { #[allow(dead_code)] pub(crate) fn step_epoch(netuid: u16) { - let tempo: u16 = SubspaceModule::get_tempo(netuid); + let tempo: u16 = Tempo::::get(netuid); step_block(tempo); } @@ -216,15 +219,15 @@ pub fn register_module(netuid: u16, key: U256, stake: u64) -> DispatchResult { #[allow(dead_code)] pub fn check_subnet_storage(netuid: u16) -> bool { - let n = SubspaceModule::get_subnet_n(netuid); + let n = N::::get(netuid); let uids = SubspaceModule::get_uids(netuid); let keys = SubspaceModule::get_keys(netuid); let names = SubspaceModule::get_names(netuid); let addresses = SubspaceModule::get_addresses(netuid); - let emissions = SubspaceModule::get_emissions(netuid); - let incentives = SubspaceModule::get_incentives(netuid); - let dividends = SubspaceModule::get_dividends(netuid); - let last_update = SubspaceModule::get_last_update(netuid); + let emissions = Emission::::get(netuid); + let incentives = Incentive::::get(netuid); + let dividends = Dividends::::get(netuid); + let last_update = LastUpdate::::get(netuid); if (n as usize) != uids.len() { return false; @@ -271,7 +274,7 @@ pub fn get_stake_for_uid(netuid: u16, module_uid: u16) -> u64 { let Some(key) = SubspaceModule::get_key_for_uid(netuid, module_uid) else { return 0; }; - SubspaceModule::get_stake_for_key(netuid, &key) + Stake::::get(netuid, key) } #[allow(dead_code)] diff --git a/pallets/subspace/tests/profit_share.rs b/pallets/subspace/tests/profit_share.rs index 86fc3b1a1..87a34fa5d 100644 --- a/pallets/subspace/tests/profit_share.rs +++ b/pallets/subspace/tests/profit_share.rs @@ -3,7 +3,7 @@ mod mock; use frame_support::assert_ok; use log::info; use mock::*; -use pallet_subspace::{FloorFounderShare, ProfitShares}; +use pallet_subspace::{FloorFounderShare, ProfitShares, Stake}; use sp_core::U256; use sp_std::vec; @@ -48,8 +48,8 @@ fn test_add_profit_share() { assert_eq!(miner_emission, voter_emission, "emission not equal"); assert!(miner_emission == 0, "emission not equal"); assert!(voter_emission == 0, "emission not equal"); - let miner_stake = SubspaceModule::get_stake_for_key(netuid, &miner_key); - let voter_stake = SubspaceModule::get_stake_for_key(netuid, &voter_key); + let miner_stake = Stake::::get(netuid, miner_key); + let voter_stake = Stake::::get(netuid, voter_key); info!("miner stake before: {miner_stake:?}"); info!("voter stake before: {voter_stake:?}"); step_epoch(netuid); @@ -65,8 +65,8 @@ fn test_add_profit_share() { let voter_balance = SubspaceModule::get_balance_u64(&voter_key); info!("miner balance: {miner_balance:?}"); info!("voter balance: {voter_balance:?}"); - let miner_stake = SubspaceModule::get_stake_for_key(netuid, &miner_key); - let voter_stake = SubspaceModule::get_stake_for_key(netuid, &voter_key); + let miner_stake = Stake::::get(netuid, miner_key); + let voter_stake = Stake::::get(netuid, voter_key); info!("miner stake after: {miner_stake:?}"); info!("voter stake after: {voter_stake:?}"); diff --git a/pallets/subspace/tests/registration.rs b/pallets/subspace/tests/registration.rs index 9c70e9f4e..560d11e14 100644 --- a/pallets/subspace/tests/registration.rs +++ b/pallets/subspace/tests/registration.rs @@ -9,9 +9,9 @@ use sp_core::U256; use log::info; use pallet_subspace::{ voting::ApplicationStatus, Curator, CuratorApplications, Emission, Error, FloorDelegationFee, - GeneralSubnetApplicationCost, MaxAllowedModules, MaxAllowedUids, MaxNameLength, - MaxRegistrationsPerBlock, MinNameLength, MinStake, ProposalCost, RegistrationsPerBlock, Stake, - SubnetGaps, SubnetNames, TotalSubnets, N, + GeneralSubnetApplicationCost, MaxAllowedModules, MaxAllowedSubnets, MaxAllowedUids, + MaxNameLength, MaxRegistrationsPerBlock, MinNameLength, MinStake, ProposalCost, + RegistrationsPerBlock, Stake, SubnetGaps, SubnetNames, TotalSubnets, N, }; use sp_runtime::{DispatchResult, Percent}; @@ -121,7 +121,7 @@ fn test_registration_ok() { .unwrap_or_else(|_| panic!("register module failed for key {key:?}")); // Check if module has added to the specified network(netuid) - assert_eq!(SubspaceModule::get_subnet_n(netuid), 1); + assert_eq!(N::::get(netuid), 1); // Check if the module has added to the Keys let module_uid = SubspaceModule::get_uid_for_key(netuid, &key); @@ -149,11 +149,7 @@ fn test_many_registrations() { register_module(netuid, U256::from(i), stake).unwrap_or_else(|_| { panic!("Failed to register module with key: {i:?} and stake: {stake:?}",) }); - assert_eq!( - SubspaceModule::get_subnet_n(netuid), - i + 1, - "Failed at i={i}", - ); + assert_eq!(N::::get(netuid), i + 1, "Failed at i={i}",); } }); } @@ -352,7 +348,7 @@ fn validates_module_on_update() { assert_eq!(params.name, b"test3"); assert_eq!(params.address, b"0.0.0.0:3"); - FloorDelegationFee::::set(Percent::from_percent(10)); + FloorDelegationFee::::put(Percent::from_percent(10)); assert_err!( update_module(b"test3", b"0.0.0.0:3"), Error::::InvalidMinDelegationFee @@ -421,7 +417,7 @@ fn test_register_invalid_name() { zero_min_burn(); // set min name lenght - MinNameLength::::set(2); + MinNameLength::::put(2); // Get the minimum and maximum name lengths from the configuration let min_name_length = MinNameLength::::get(); @@ -520,7 +516,7 @@ fn test_register_invalid_subnet_name() { zero_min_burn(); // Set min name length - MinNameLength::::set(2); + MinNameLength::::put(2); // Get the minimum and maximum name lengths from the configuration let min_name_length = MinNameLength::::get(); @@ -629,7 +625,7 @@ fn test_remove_from_whitelist() { new_test_ext().execute_with(|| { let whitelist_key = U256::from(0); let module_key = U256::from(1); - Curator::::set(whitelist_key); + Curator::::put(whitelist_key); let proposal_cost = ProposalCost::::get(); let data = "test".as_bytes().to_vec(); @@ -666,7 +662,7 @@ fn test_invalid_curator() { let whitelist_key = U256::from(0); let invalid_key = U256::from(1); let module_key = U256::from(2); - Curator::::set(whitelist_key); + Curator::::put(whitelist_key); // Try to add to whitelist with an invalid curator key assert_noop!( @@ -717,7 +713,7 @@ fn new_subnets_on_removed_uids_register_modules_to_the_correct_netuids() { new_test_ext().execute_with(|| { zero_min_burn(); - SubspaceModule::set_global_max_allowed_subnets(3); + MaxAllowedSubnets::::put(3); assert_ok!(register_module(0, 0.into(), to_nano(10))); assert_ok!(register_module(1, 1.into(), to_nano(5))); diff --git a/pallets/subspace/tests/step_linear.rs b/pallets/subspace/tests/step_linear.rs index 6f753aae0..c85ea6f5d 100644 --- a/pallets/subspace/tests/step_linear.rs +++ b/pallets/subspace/tests/step_linear.rs @@ -4,9 +4,9 @@ use frame_support::assert_ok; use log::info; use mock::*; use pallet_subspace::{ - global::BurnConfiguration, BurnConfig, DaoTreasuryAddress, DaoTreasuryDistribution, - MaxAllowedWeights, MaxRegistrationsPerBlock, MinAllowedWeights, SubnetStakeThreshold, Tempo, - Trust, + global::BurnConfiguration, Burn, BurnConfig, DaoTreasuryAddress, DaoTreasuryDistribution, + Dividends, Emission, FounderShare, Incentive, MaxAllowedModules, MaxAllowedWeights, + MaxRegistrationsPerBlock, MinAllowedWeights, Stake, SubnetStakeThreshold, Tempo, Trust, N, }; use sp_core::U256; use sp_runtime::Percent; @@ -21,9 +21,9 @@ fn check_network_stats(netuid: u16) { let emission_buffer: u64 = 1_000; // the numbers arent perfect but we want to make sure they fall within a range (10_000 / 2**64) let threshold = SubnetStakeThreshold::::get(); let subnet_emission: u64 = SubspaceModule::calculate_network_emission(netuid, threshold); - let incentives: Vec = SubspaceModule::get_incentives(netuid); - let dividends: Vec = SubspaceModule::get_dividends(netuid); - let emissions: Vec = SubspaceModule::get_emissions(netuid); + let incentives: Vec = Incentive::::get(netuid); + let dividends: Vec = Dividends::::get(netuid); + let emissions: Vec = Emission::::get(netuid); let total_incentives: u16 = incentives.iter().sum(); let total_dividends: u16 = dividends.iter().sum(); let total_emissions: u64 = emissions.iter().sum(); @@ -69,9 +69,9 @@ fn test_no_weights() { let _keys = SubspaceModule::get_keys(netuid); let _uids = SubspaceModule::get_uids(netuid); - let incentives: Vec = SubspaceModule::get_incentives(netuid); - let dividends: Vec = SubspaceModule::get_dividends(netuid); - let emissions: Vec = SubspaceModule::get_emissions(netuid); + let incentives: Vec = Incentive::::get(netuid); + let dividends: Vec = Dividends::::get(netuid); + let emissions: Vec = Emission::::get(netuid); let _total_incentives: u16 = incentives.iter().sum(); let _total_dividends: u16 = dividends.iter().sum(); let _total_emissions: u64 = emissions.iter().sum(); @@ -107,9 +107,9 @@ fn test_dividends_same_stake() { let stakes_before: Vec = get_stakes(netuid); step_epoch(netuid); - let incentives: Vec = SubspaceModule::get_incentives(netuid); - let dividends: Vec = SubspaceModule::get_dividends(netuid); - let emissions: Vec = SubspaceModule::get_emissions(netuid); + let incentives: Vec = Incentive::::get(netuid); + let dividends: Vec = Dividends::::get(netuid); + let emissions: Vec = Emission::::get(netuid); let stakes: Vec = get_stakes(netuid); // evaluate votees @@ -201,9 +201,9 @@ fn test_dividends_diff_stake() { let stakes_before: Vec = get_stakes(netuid); step_epoch(netuid); - let incentives: Vec = SubspaceModule::get_incentives(netuid); - let dividends: Vec = SubspaceModule::get_dividends(netuid); - let emissions: Vec = SubspaceModule::get_emissions(netuid); + let incentives: Vec = Incentive::::get(netuid); + let dividends: Vec = Dividends::::get(netuid); + let emissions: Vec = Emission::::get(netuid); let stakes: Vec = get_stakes(netuid); // evaluate votees @@ -263,7 +263,7 @@ fn test_pruning() { // SETUP NETWORK register_n_modules(netuid, n, stake_per_module); - SubspaceModule::set_max_allowed_modules(n); + MaxAllowedModules::::put(n); update_params(netuid, 1, n, 0); let voter_idx = 0; @@ -302,9 +302,9 @@ fn test_pruning() { assert!(is_registered); assert!( - SubspaceModule::get_subnet_n(netuid) == n, - "SubspaceModule::get_subnet_n(netuid): {} != n: {}", - SubspaceModule::get_subnet_n(netuid), + N::::get(netuid) == n, + "N::::get(netuid): {} != n: {}", + N::::get(netuid), n ); @@ -360,9 +360,9 @@ fn test_lowest_priority_mechanism() { weight_values.clone(), ); step_block(tempo); - let incentives: Vec = SubspaceModule::get_incentives(netuid); - let dividends: Vec = SubspaceModule::get_dividends(netuid); - let emissions: Vec = SubspaceModule::get_emissions(netuid); + let incentives: Vec = Incentive::::get(netuid); + let dividends: Vec = Dividends::::get(netuid); + let emissions: Vec = Emission::::get(netuid); let _stakes: Vec = get_stakes(netuid); assert!(emissions[prune_uid as usize] == 0); @@ -415,10 +415,10 @@ fn test_lowest_priority_mechanism() { // weight_values[*uid as usize] = 0; // } -// let old_n : u16 = SubspaceModule::get_subnet_n( netuid ); +// let old_n : u16 = N::::get( netuid ); // set_weights(netuid, keys[0], weight_uids.clone() , weight_values.clone() ); // step_block( tempo ); -// let n: u16 = SubspaceModule::get_subnet_n( netuid ); +// let n: u16 = N::::get( netuid ); // assert !( old_n - num_zero_uids == n ); // }); @@ -536,8 +536,8 @@ fn test_incentives() { set_weights(netuid, keys[0], weight_uids.clone(), weight_values.clone()); step_block(params.tempo); - let incentives: Vec = SubspaceModule::get_incentives(netuid); - let emissions: Vec = SubspaceModule::get_emissions(netuid); + let incentives: Vec = Incentive::::get(netuid); + let emissions: Vec = Emission::::get(netuid); // evaluate votees assert!(incentives[1] > 0); @@ -552,8 +552,8 @@ fn test_incentives() { step_block(params.tempo); - let incentives: Vec = SubspaceModule::get_incentives(netuid); - let emissions: Vec = SubspaceModule::get_emissions(netuid); + let incentives: Vec = Incentive::::get(netuid); + let emissions: Vec = Emission::::get(netuid); // evaluate votees let delta: u64 = 100 * params.tempo as u64; @@ -605,7 +605,7 @@ fn test_trust() { step_block(params.tempo); let trust: Vec = Trust::::get(netuid); - let emission: Vec = SubspaceModule::get_emissions(netuid); + let emission: Vec = Emission::::get(netuid); // evaluate votees info!("trust: {:?}", trust); @@ -637,12 +637,12 @@ fn test_founder_share() { info!("{:?}", stake_from_vector); } update_params!(netuid => { founder_share: 12 }); - let founder_share = SubspaceModule::get_founder_share(netuid); + let founder_share = FounderShare::::get(netuid); let founder_ratio: f64 = founder_share as f64 / 100.0; let subnet_params = SubspaceModule::subnet_params(netuid); - let founder_stake_before = SubspaceModule::get_stake_for_key(netuid, &founder_key); + let founder_stake_before = Stake::::get(netuid, founder_key); info!("founder_stake_before: {founder_stake_before:?}"); // vote to avoid key[0] as we want to see the key[0] burn step_epoch(netuid); @@ -651,9 +651,9 @@ fn test_founder_share() { * subnet_params.tempo as u64; let expected_founder_share = (total_emission as f64 * founder_ratio) as u64; let expected_emission = total_emission - expected_founder_share; - let emissions = SubspaceModule::get_emissions(netuid); - let dividends = SubspaceModule::get_dividends(netuid); - let incentives = SubspaceModule::get_incentives(netuid); + let emissions = Emission::::get(netuid); + let dividends = Dividends::::get(netuid); + let incentives = Incentive::::get(netuid); let total_dividends: u64 = dividends.iter().sum::() as u64; let total_incentives: u64 = incentives.iter().sum::() as u64; @@ -665,7 +665,7 @@ fn test_founder_share() { let calcualted_total_emission = emissions.iter().sum::(); - let key_stake = SubspaceModule::get_stake_for_key(netuid, &founder_key); + let key_stake = Stake::::get(netuid, founder_key); let founder_total_stake = founder_stake_before + founder_emission; assert_eq!( key_stake - (key_stake % 1000), @@ -717,9 +717,9 @@ fn test_dynamic_burn() { step_block(200); assert!( - SubspaceModule::get_burn(netuid) == min_burn, + Burn::::get(netuid) == min_burn, "current burn: {:?}", - SubspaceModule::get_burn(netuid) + Burn::::get(netuid) ); // Register the first 1000 modules, this is 10x the registration target @@ -736,9 +736,9 @@ fn test_dynamic_burn() { // Burn is now at 11 instead of 2 assert!( - SubspaceModule::get_burn(netuid) == to_nano(11), + Burn::::get(netuid) == to_nano(11), "current burn {:?}", - SubspaceModule::get_burn(netuid) + Burn::::get(netuid) ); MaxRegistrationsPerBlock::::set(1000); @@ -753,9 +753,9 @@ fn test_dynamic_burn() { // Make sure the burn correctly decreased based on demand assert!( - SubspaceModule::get_burn(netuid) == 8250000000, + Burn::::get(netuid) == 8250000000, "current burn: {:?}", - SubspaceModule::get_burn(netuid) + Burn::::get(netuid) ); }); } diff --git a/pallets/subspace/tests/step_yuma.rs b/pallets/subspace/tests/step_yuma.rs index 3dc184b6e..465656842 100644 --- a/pallets/subspace/tests/step_yuma.rs +++ b/pallets/subspace/tests/step_yuma.rs @@ -2,7 +2,7 @@ use crate::mock::*; use frame_support::assert_ok; use pallet_subspace::{ yuma::{AccountKey, EmissionMap, ModuleKey, YumaCalc}, - FloorFounderShare, MaxRegistrationsPerBlock, + Emission, FloorFounderShare, MaxRegistrationsPerBlock, Stake, UnitEmission, N, }; use sp_core::U256; use std::collections::BTreeMap; @@ -45,7 +45,7 @@ const ONE: u64 = to_nano(1); #[test] fn test_1_graph() { new_test_ext().execute_with(|| { - SubspaceModule::set_unit_emission(23148148148); + UnitEmission::::put(23148148148); zero_min_burn(); FloorFounderShare::::put(0); @@ -64,7 +64,7 @@ fn test_1_graph() { }); assert_ok!(register_module(netuid, key + 1, 1)); - assert_eq!(SubspaceModule::get_subnet_n(netuid), 2); + assert_eq!(N::::get(netuid), 2); run_to_block(1); // run to next block to ensure weights are set on nodes after their registration block @@ -108,15 +108,15 @@ fn test_10_graph() { key, uid, stake_amount, - SubspaceModule::get_subnet_n(netuid), + N::::get(netuid), ); assert_ok!(register_module(netuid, key, stake_amount)); - assert_eq!(SubspaceModule::get_subnet_n(netuid) - 1, uid); + assert_eq!(N::::get(netuid) - 1, uid); } new_test_ext().execute_with(|| { - SubspaceModule::set_unit_emission(23148148148); + UnitEmission::::put(23148148148); zero_min_burn(); FloorFounderShare::::put(0); MaxRegistrationsPerBlock::::set(1000); @@ -140,7 +140,7 @@ fn test_10_graph() { }); assert_ok!(register_module(netuid, U256::from(n + 1), 1)); - assert_eq!(SubspaceModule::get_subnet_n(netuid), 11); + assert_eq!(N::::get(netuid), 11); run_to_block(1); // run to next block to ensure weights are set on nodes after their registration block @@ -264,7 +264,7 @@ fn yuma_weights_older_than_max_age_are_discarded() { // But make sure there are emissions - let subnet_emission_sum = SubspaceModule::get_emissions(yuma_netuid).iter().sum::(); + let subnet_emission_sum = Emission::::get(yuma_netuid).iter().sum::(); assert!(subnet_emission_sum > 0); }); } @@ -303,7 +303,7 @@ fn test_emission_exploit() { // step first 40 blocks from the registration step_block(40); - let stake_accumulated = SubspaceModule::get_stake_for_key(yuma_netuid, &yuma_badactor_key); + let stake_accumulated = Stake::::get(yuma_netuid, yuma_badactor_key); // User will now unstake and register another subnet. assert_ok!(SubspaceModule::do_remove_stake( get_origin(yuma_badactor_key), @@ -340,8 +340,7 @@ fn test_emission_exploit() { step_block(58); // remove the stake again - let stake_accumulated_two = - SubspaceModule::get_stake_for_key(new_netuid, &yuma_badactor_key); + let stake_accumulated_two = Stake::::get(new_netuid, yuma_badactor_key); assert_ok!(SubspaceModule::do_remove_stake( get_origin(yuma_badactor_key), new_netuid, @@ -364,7 +363,7 @@ fn test_emission_exploit() { step_block(101); // get the stake of honest actor - let hones_stake = SubspaceModule::get_stake_for_key(3, &honest_actor_key); + let hones_stake = Stake::::get(3, honest_actor_key); dbg!(hones_stake); dbg!(badactor_balance_after); @@ -407,8 +406,8 @@ fn test_tempo_compound() { // we will now step, SLOW_TEMPO -> 1000 blocks step_block(SLOW_TEMPO); - let fast = SubspaceModule::get_stake_for_key(f_netuid, &f_key); - let slow = SubspaceModule::get_stake_for_key(s_netuid, &s_key); + let fast = Stake::::get(f_netuid, f_key); + let slow = Stake::::get(s_netuid, s_key); // faster tempo should have quicker compound rate assert!(fast > slow); diff --git a/pallets/subspace/tests/subnet.rs b/pallets/subspace/tests/subnet.rs index b2fb4e161..674fbdcc0 100644 --- a/pallets/subspace/tests/subnet.rs +++ b/pallets/subspace/tests/subnet.rs @@ -4,8 +4,9 @@ use frame_support::{assert_err, assert_ok}; use log::info; use mock::*; use pallet_subspace::{ - Dividends, Error, FounderShare, MaxAllowedSubnets, MaxRegistrationsPerBlock, - MaximumSetWeightCallsPerEpoch, SubnetNames, SubnetStakeThreshold, Tempo, N, + Dividends, Error, FounderShare, MaxAllowedModules, MaxAllowedSubnets, MaxAllowedUids, + MaxRegistrationsPerBlock, MaximumSetWeightCallsPerEpoch, SubnetNames, SubnetStakeThreshold, + Tempo, TotalSubnets, UnitEmission, N, }; use sp_core::U256; use sp_runtime::Percent; @@ -29,7 +30,7 @@ fn test_add_subnets() { assert_ok!(register_module(i, U256::from(i), stake_per_module)); for j in 0..n { if j != i { - let n = SubspaceModule::get_subnet_n(i); + let n = N::::get(i); info!("registering module i:{} j:{} n:{}", i, j, n); assert_ok!(register_module(i, U256::from(j), stake_per_module)); } @@ -38,10 +39,10 @@ fn test_add_subnets() { if expected_subnets > max_allowed_subnets { expected_subnets = max_allowed_subnets; } else { - assert_eq!(SubspaceModule::get_subnet_n(i), n); + assert_eq!(N::::get(i), n); } assert_eq!( - SubspaceModule::num_subnets(), + TotalSubnets::::get(), expected_subnets, "number of subnets is not equal to expected subnets" ); @@ -61,7 +62,7 @@ fn test_add_subnets() { assert_eq!(keys.len() as u16, n); assert!(check_subnet_storage(netuid)); SubspaceModule::remove_subnet(netuid); - assert_eq!(SubspaceModule::get_subnet_n(netuid), 0); + assert_eq!(N::::get(netuid), 0); assert!(check_subnet_storage(netuid)); let total_tokens_after: u64 = keys.iter().map(SubspaceModule::get_balance_u64).sum(); @@ -70,7 +71,7 @@ fn test_add_subnets() { assert_eq!(total_tokens_after, total_tokens_before); expected_subnets = expected_subnets.saturating_sub(1); assert_eq!( - SubspaceModule::num_subnets(), + TotalSubnets::::get(), expected_subnets, "number of subnets is not equal to expected subnets" ); @@ -138,18 +139,18 @@ fn test_set_max_allowed_uids_growing() { MaxRegistrationsPerBlock::::set(max_uids + extra_uids * rounds); for i in 1..max_uids { assert_ok!(register_module(netuid, U256::from(i), stake)); - assert_eq!(SubspaceModule::get_subnet_n(netuid), i + 1); + assert_eq!(N::::get(netuid), i + 1); } - let mut n: u16 = SubspaceModule::get_subnet_n(netuid); + let mut n: u16 = N::::get(netuid); let old_n: u16 = n; let mut _uids: Vec; - assert_eq!(SubspaceModule::get_subnet_n(netuid), max_uids); + assert_eq!(N::::get(netuid), max_uids); for r in 1..rounds { // set max allowed uids to max_uids + extra_uids update_params!(netuid => { max_allowed_uids: max_uids + extra_uids * (r - 1) }); - max_uids = SubspaceModule::get_max_allowed_uids(netuid); + max_uids = MaxAllowedUids::::get(netuid); let new_n = old_n + extra_uids * (r - 1); // print the pruned uids for uid in old_n + extra_uids * (r - 1)..old_n + extra_uids * r { @@ -158,7 +159,7 @@ fn test_set_max_allowed_uids_growing() { // set max allowed uids to max_uids - n = SubspaceModule::get_subnet_n(netuid); + n = N::::get(netuid); assert_eq!(n, new_n); let uids = SubspaceModule::get_uids(netuid); @@ -187,7 +188,7 @@ fn test_set_max_allowed_uids_shrinking() { // make sure that the results won´t get affected by burn zero_min_burn(); - let mut n = SubspaceModule::get_subnet_n(netuid); + let mut n = N::::get(netuid); info!("registering module {}", n); assert_ok!(register_module(netuid, U256::from(0), stake)); update_params!(netuid => { @@ -198,7 +199,7 @@ fn test_set_max_allowed_uids_shrinking() { for i in 1..(max_uids + extra_uids) { let result = register_module(netuid, U256::from(i), stake); result.unwrap(); - n = SubspaceModule::get_subnet_n(netuid); + n = N::::get(netuid); } assert_eq!(n, max_uids + extra_uids); @@ -239,7 +240,7 @@ fn test_set_max_allowed_uids_shrinking() { info!("subnet params {:?}", SubspaceModule::subnet_params(netuid)); assert_ok!(result); let params = SubspaceModule::subnet_params(netuid); - let n = SubspaceModule::get_subnet_n(netuid); + let n = N::::get(netuid); assert_eq!( params.max_allowed_uids, max_uids, "max allowed uids is not equal to expected max allowed uids" @@ -274,12 +275,12 @@ fn test_set_max_allowed_modules() { // make sure that the results won´t get affected by burn zero_min_burn(); MaxRegistrationsPerBlock::::set(1000); - SubspaceModule::set_max_allowed_modules(max_allowed_modules); + MaxAllowedModules::::put(max_allowed_modules); // set max_total modules for i in 1..(2 * max_allowed_modules) { assert_ok!(register_module(netuid, U256::from(i), stake)); - let n = SubspaceModule::get_subnet_n(netuid); + let n = N::::get(netuid); assert!( n <= max_allowed_modules, "subnet_n {:?} is not less than max_allowed_modules {:?}", @@ -318,7 +319,7 @@ fn test_deregister_subnet_when_overflows_max_allowed_subnets() { assert_eq!(SubspaceModule::get_total_subnet_stake(1), stakes[1]); assert_eq!(SubspaceModule::get_total_subnet_stake(2), stakes[2]); assert_eq!(SubspaceModule::get_total_subnet_stake(0), stakes[3]); - assert_eq!(SubspaceModule::num_subnets(), 3); + assert_eq!(TotalSubnets::::get(), 3); }); } @@ -336,9 +337,9 @@ fn test_emission_distribution_novote() { let stake_below_threshold: u64 = to_nano(50_000); // making sure the unit emission are set correctly - SubspaceModule::set_unit_emission(23148148148); + UnitEmission::::put(23148148148); zero_min_burn(); - SubnetStakeThreshold::::set(Percent::from_percent(10)); + SubnetStakeThreshold::::put(Percent::from_percent(10)); let blocks_in_day: u16 = 10_800; // this is aprox. the stake we expect at the end of the day with the above unit emission let expected_stake_change = to_nano(250_000); @@ -438,7 +439,7 @@ fn test_yuma_self_vote() { let miner_self_key = U256::from(4); // making sure the unit emission are set correctly - SubspaceModule::set_unit_emission(23148148148); + UnitEmission::::put(23148148148); zero_min_burn(); assert_ok!(register_module( @@ -539,7 +540,7 @@ fn test_emission_activation() { ]; // Set the stake threshold and minimum burn - SubnetStakeThreshold::::set(Percent::from_percent(5)); + SubnetStakeThreshold::::put(Percent::from_percent(5)); zero_min_burn(); // Register the subnets @@ -575,7 +576,7 @@ fn test_emission_activation() { fn test_parasite_subnet_registrations() { new_test_ext().execute_with(|| { let expected_module_amount: u16 = 5; - SubspaceModule::set_max_allowed_modules(expected_module_amount); + MaxAllowedModules::::put(expected_module_amount); MaxRegistrationsPerBlock::::set(1000); let main_subnet_netuid: u16 = 0; @@ -619,11 +620,11 @@ fn test_parasite_subnet_registrations() { } // Check if the honest subnet has 2 modules. - let main_subnet_module_amount = SubspaceModule::get_subnet_n(main_subnet_netuid); + let main_subnet_module_amount = N::::get(main_subnet_netuid); assert_eq!(main_subnet_module_amount, 2); // Check if the parasite subnet has 3 modules - let parasite_subnet_module_amount = SubspaceModule::get_subnet_n(parasite_netuid); + let parasite_subnet_module_amount = N::::get(parasite_netuid); assert_eq!(parasite_subnet_module_amount, 3); }); } @@ -637,7 +638,7 @@ fn test_subnet_replacing() { // Defines the maximum number of modules, that can be registered, // on all subnets at once. let expected_subnet_amount: u16 = 3; - SubspaceModule::set_max_allowed_modules(expected_subnet_amount); + MaxAllowedModules::::put(expected_subnet_amount); let subnets = [ (U256::from(0), to_nano(100_000)), @@ -653,7 +654,7 @@ fn test_subnet_replacing() { assert_ok!(register_module(i as u16, *subnet_key, *subnet_stake)); } - let subnet_amount = SubspaceModule::num_subnets(); + let subnet_amount = TotalSubnets::::get(); assert_eq!(subnet_amount, expected_subnet_amount); // Register module on the subnet one (netuid 0), this means that subnet @@ -661,13 +662,13 @@ fn test_subnet_replacing() { assert_ok!(register_module(0, random_keys[0], to_nano(1_000))); assert_ok!(register_module(4, random_keys[1], to_nano(150_000))); - let subnet_amount = SubspaceModule::num_subnets(); + let subnet_amount = TotalSubnets::::get(); assert_eq!(subnet_amount, expected_subnet_amount - 1); // netuid 1 replaced by subnet four assert_ok!(register_module(3, subnets[3].0, subnets[3].1)); - let subnet_amount = SubspaceModule::num_subnets(); + let subnet_amount = TotalSubnets::::get(); let total_module_amount = SubspaceModule::global_n_modules(); assert_eq!(subnet_amount, expected_subnet_amount); assert_eq!(total_module_amount, expected_subnet_amount); @@ -681,9 +682,9 @@ fn test_subnet_replacing() { #[test] fn test_active_stake() { new_test_ext().execute_with(|| { - SubnetStakeThreshold::::set(Percent::from_percent(5)); + SubnetStakeThreshold::::put(Percent::from_percent(5)); let max_subnets = 10; - SubspaceModule::set_global_max_allowed_subnets(max_subnets); + MaxAllowedSubnets::::put(max_subnets); let general_subnet_stake = to_nano(65_000_000); let general_subnet_key = U256::from(0); diff --git a/pallets/subspace/tests/weights.rs b/pallets/subspace/tests/weights.rs index 99421c376..919b58bd2 100644 --- a/pallets/subspace/tests/weights.rs +++ b/pallets/subspace/tests/weights.rs @@ -1,7 +1,7 @@ mod mock; use frame_support::{assert_err, assert_ok}; -use pallet_subspace::{Error, FloorFounderShare, MaxRegistrationsPerBlock}; +use pallet_subspace::{Error, FloorFounderShare, MaxRegistrationsPerBlock, N}; use sp_core::U256; use sp_runtime::DispatchError; @@ -57,7 +57,7 @@ fn test_weights_err_has_duplicate_ids() { assert_ok!(register_module(netuid, U256::from(3), 10000000)); SubspaceModule::get_uid_for_key(netuid, &U256::from(3)); - assert_eq!(SubspaceModule::get_subnet_n(netuid), 4); + assert_eq!(N::::get(netuid), 4); let weights_keys: Vec = vec![1, 1, 1]; // Contains duplicates let weight_values: Vec = vec![1, 2, 3];