diff --git a/pallets/subtensor/src/epoch/run_epoch.rs b/pallets/subtensor/src/epoch/run_epoch.rs index f56b8a89a4..07a4fe8041 100644 --- a/pallets/subtensor/src/epoch/run_epoch.rs +++ b/pallets/subtensor/src/epoch/run_epoch.rs @@ -1061,7 +1061,7 @@ impl Pallet { /// Output unnormalized sparse weights, input weights are assumed to be row max-upscaled in u16. pub fn get_weights_sparse(netuid_index: NetUidStorageIndex) -> Vec> { - let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default(); let n = Self::get_subnetwork_n(netuid) as usize; let mut weights: Vec> = vec![vec![]; n]; for (uid_i, weights_i) in @@ -1080,7 +1080,7 @@ impl Pallet { /// Output unnormalized weights in [n, n] matrix, input weights are assumed to be row max-upscaled in u16. pub fn get_weights(netuid_index: NetUidStorageIndex) -> Vec> { - let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default(); let n = Self::get_subnetwork_n(netuid) as usize; let mut weights: Vec> = vec![vec![I32F32::saturating_from_num(0.0); n]; n]; for (uid_i, weights_vec) in @@ -1103,7 +1103,7 @@ impl Pallet { /// Output unnormalized sparse bonds, input bonds are assumed to be column max-upscaled in u16. pub fn get_bonds_sparse(netuid_index: NetUidStorageIndex) -> Vec> { - let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default(); let n = Self::get_subnetwork_n(netuid) as usize; let mut bonds: Vec> = vec![vec![]; n]; for (uid_i, bonds_vec) in @@ -1126,7 +1126,7 @@ impl Pallet { /// Output unnormalized bonds in [n, n] matrix, input bonds are assumed to be column max-upscaled in u16. pub fn get_bonds(netuid_index: NetUidStorageIndex) -> Vec> { - let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default(); let n: usize = Self::get_subnetwork_n(netuid) as usize; let mut bonds: Vec> = vec![vec![I32F32::saturating_from_num(0.0); n]; n]; for (uid_i, bonds_vec) in @@ -1188,7 +1188,7 @@ impl Pallet { bonds: &[Vec<(u16, I32F32)>], netuid_index: NetUidStorageIndex, ) -> Vec> { - let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default(); // Retrieve the bonds moving average for the given network ID and scale it down. let bonds_moving_average: I64F64 = @@ -1301,7 +1301,7 @@ impl Pallet { bonds: &[Vec<(u16, I32F32)>], consensus: &[I32F32], ) -> Vec> { - let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default(); // Check if Liquid Alpha is enabled, consensus is not empty, and contains non-zero values. if LiquidAlphaOn::::get(netuid) @@ -1537,7 +1537,7 @@ impl Pallet { netuid_index: NetUidStorageIndex, account_id: &T::AccountId, ) -> Result<(), DispatchError> { - let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default(); // check bonds reset enabled for this subnet let bonds_reset_enabled: bool = Self::get_bonds_reset(netuid); diff --git a/pallets/subtensor/src/migrations/migrate_crv3_commits_add_block.rs b/pallets/subtensor/src/migrations/migrate_crv3_commits_add_block.rs index bf5a0bb2b5..6dacfbc3c8 100644 --- a/pallets/subtensor/src/migrations/migrate_crv3_commits_add_block.rs +++ b/pallets/subtensor/src/migrations/migrate_crv3_commits_add_block.rs @@ -25,7 +25,7 @@ pub fn migrate_crv3_commits_add_block() -> Weight { for (netuid_index, epoch, old_q) in CRV3WeightCommits::::drain() { total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); - let (netuid, _) = Pallet::::get_netuid_and_subid(netuid_index).unwrap_or_default(); + let (netuid, _) = Pallet::::get_netuid_and_mecid(netuid_index).unwrap_or_default(); let commit_block = Pallet::::get_first_block_of_epoch(netuid, epoch); // convert VecDeque<(who,cipher,rnd)> → VecDeque<(who,cb,cipher,rnd)> diff --git a/pallets/subtensor/src/subnets/mechanism.rs b/pallets/subtensor/src/subnets/mechanism.rs index 481974ef05..2a2b214f5c 100644 --- a/pallets/subtensor/src/subnets/mechanism.rs +++ b/pallets/subtensor/src/subnets/mechanism.rs @@ -16,12 +16,12 @@ pub type BalanceOf = as fungible::Inspect<::AccountId>>::Balance; /// Theoretical maximum of subnets on bittensor. This value is used in indexed -/// storage of epoch values for sub-subnets as +/// storage of epoch values for mechanisms as /// -/// `storage_index = netuid + sub_id * GLOBAL_MAX_SUBNET_COUNT` +/// `storage_index = netuid + mecid * GLOBAL_MAX_SUBNET_COUNT` /// -/// For sub_id = 0 this index results in netuid and provides backward compatibility -/// for subnets with default sub-subnet count of 1. +/// For mecid = 0 this index results in netuid and provides backward compatibility +/// for subnets with default mechanism count of 1. /// /// Changing this value will require a migration of all epoch maps. /// @@ -32,8 +32,8 @@ pub const GLOBAL_MAX_SUBNET_COUNT: u16 = 4096; pub const MAX_MECHANISM_COUNT_PER_SUBNET: u8 = 16; impl Pallet { - pub fn get_mechanism_storage_index(netuid: NetUid, sub_id: MechId) -> NetUidStorageIndex { - u16::from(sub_id) + pub fn get_mechanism_storage_index(netuid: NetUid, mecid: MechId) -> NetUidStorageIndex { + u16::from(mecid) .saturating_mul(GLOBAL_MAX_SUBNET_COUNT) .saturating_add(u16::from(netuid)) .into() @@ -48,7 +48,7 @@ impl Pallet { } } - pub fn get_netuid_and_subid( + pub fn get_netuid_and_mecid( netuid_index: NetUidStorageIndex, ) -> Result<(NetUid, MechId), Error> { let maybe_netuid = u16::from(netuid_index).checked_rem(GLOBAL_MAX_SUBNET_COUNT); @@ -61,13 +61,13 @@ impl Pallet { Error::::MechanismDoesNotExist ); - // Extract sub_id - let sub_id_u8 = u8::try_from(u16::from(netuid_index).safe_div(GLOBAL_MAX_SUBNET_COUNT)) + // Extract mecid + let mecid_u8 = u8::try_from(u16::from(netuid_index).safe_div(GLOBAL_MAX_SUBNET_COUNT)) .map_err(|_| Error::::MechanismDoesNotExist)?; - let sub_id = MechId::from(sub_id_u8); + let mecid = MechId::from(mecid_u8); - if MechanismCountCurrent::::get(netuid) > sub_id { - Ok((netuid, sub_id)) + if MechanismCountCurrent::::get(netuid) > mecid { + Ok((netuid, mecid)) } else { Err(Error::::MechanismDoesNotExist.into()) } @@ -80,7 +80,7 @@ impl Pallet { MechanismCountCurrent::::get(netuid) } - pub fn ensure_mechanism_exists(netuid: NetUid, sub_id: MechId) -> DispatchResult { + pub fn ensure_mechanism_exists(netuid: NetUid, mecid: MechId) -> DispatchResult { // Make sure the base subnet exists ensure!( Self::if_subnet_exist(netuid), @@ -89,7 +89,7 @@ impl Pallet { // Make sure the mechanism limit is not exceeded ensure!( - MechanismCountCurrent::::get(netuid) > sub_id, + MechanismCountCurrent::::get(netuid) > mecid, Error::::MechanismDoesNotExist ); Ok(()) @@ -260,30 +260,30 @@ impl Pallet { .into_iter() .enumerate() // Run epoch function for each mechanism to distribute its portion of emissions - .flat_map(|(sub_id_usize, sub_emission)| { - let sub_id_u8: u8 = sub_id_usize.try_into().unwrap_or_default(); - let sub_id = MechId::from(sub_id_u8); + .flat_map(|(mecid_usize, mech_emission)| { + let mecid_u8: u8 = mecid_usize.try_into().unwrap_or_default(); + let mecid = MechId::from(mecid_u8); // Run epoch function on the mechanism emission - let epoch_output = Self::epoch_mechanism(netuid, sub_id, sub_emission); - Self::persist_mechanism_epoch_terms(netuid, sub_id, epoch_output.as_map()); + let epoch_output = Self::epoch_mechanism(netuid, mecid, mech_emission); + Self::persist_mechanism_epoch_terms(netuid, mecid, epoch_output.as_map()); // Calculate mechanism weight from the split emission (not the other way because preserving // emission accuracy is the priority) // For zero emission the first mechanism gets full weight - let sub_weight = U64F64::saturating_from_num(sub_emission).safe_div_or( + let mech_weight = U64F64::saturating_from_num(mech_emission).safe_div_or( U64F64::saturating_from_num(rao_emission), - U64F64::saturating_from_num(if sub_id_u8 == 0 { 1 } else { 0 }), + U64F64::saturating_from_num(if mecid_u8 == 0 { 1 } else { 0 }), ); - // Produce an iterator of (hotkey, (terms, sub_weight)) tuples + // Produce an iterator of (hotkey, (terms, mech_weight)) tuples epoch_output .0 .into_iter() - .map(move |(hotkey, terms)| (hotkey, (terms, sub_weight))) + .map(move |(hotkey, terms)| (hotkey, (terms, mech_weight))) }) // Consolidate the hotkey emissions into a single BTreeMap - .fold(BTreeMap::new(), |mut acc, (hotkey, (terms, sub_weight))| { + .fold(BTreeMap::new(), |mut acc, (hotkey, (terms, mech_weight))| { acc.entry(hotkey) .and_modify(|acc_terms| { // Server and validator emission come from mechanism emission and need to be added up diff --git a/pallets/subtensor/src/subnets/weights.rs b/pallets/subtensor/src/subnets/weights.rs index 56acbef9c7..f47cbd9628 100644 --- a/pallets/subtensor/src/subnets/weights.rs +++ b/pallets/subtensor/src/subnets/weights.rs @@ -1108,8 +1108,8 @@ impl Pallet { neuron_uid: u16, current_block: u64, ) -> bool { - let maybe_netuid_and_subid = Self::get_netuid_and_subid(netuid_index); - if let Ok((netuid, _)) = maybe_netuid_and_subid + let maybe_netuid_and_mecid = Self::get_netuid_and_mecid(netuid_index); + if let Ok((netuid, _)) = maybe_netuid_and_mecid && Self::is_uid_exist_on_network(netuid, neuron_uid) { // --- 1. Ensure that the diff between current and last_set weights is greater than limit. diff --git a/pallets/subtensor/src/tests/mechanism.rs b/pallets/subtensor/src/tests/mechanism.rs index 9e6450e09c..8fe1467e14 100644 --- a/pallets/subtensor/src/tests/mechanism.rs +++ b/pallets/subtensor/src/tests/mechanism.rs @@ -116,7 +116,7 @@ fn test_netuid_and_subnet_from_index() { ); let (netuid, mecid) = - SubtensorModule::get_netuid_and_subid(NetUidStorageIndex::from(*netuid_index)) + SubtensorModule::get_netuid_and_mecid(NetUidStorageIndex::from(*netuid_index)) .unwrap(); assert_eq!(netuid, NetUid::from(expected_netuid)); assert_eq!(mecid, MechId::from(expected_subid));