From 86c1464e687c4770b9f9f84e2024a6f2fe3c5a6e Mon Sep 17 00:00:00 2001 From: DanielDerefaka Date: Thu, 25 Dec 2025 21:56:47 +0100 Subject: [PATCH 1/2] refactor: standardize MechId naming convention Addresses inconsistent naming of mechanism identifiers across the codebase. Changes: - Rename `sub_id` parameter to `mecid` in public API functions - Rename `get_netuid_and_subid` to `get_netuid_and_mecid` - Update variable names from `sub_id_*` to `mecid_*` in mechanism.rs - Update comments to use `mecid` terminology instead of `sub_id` - Rename `sub_weight`/`sub_emission` to `mech_weight`/`mech_emission` This aligns internal implementation with the public-facing API which already uses `mecid` and `MechId` consistently. Fixes #2101 --- pallets/subtensor/src/epoch/run_epoch.rs | 14 +++--- .../migrate_crv3_commits_add_block.rs | 2 +- pallets/subtensor/src/subnets/mechanism.rs | 48 +++++++++---------- pallets/subtensor/src/subnets/weights.rs | 4 +- pallets/subtensor/src/tests/mechanism.rs | 2 +- 5 files changed, 35 insertions(+), 35 deletions(-) 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)); From cb97fc76a9019d457554352b51235a3368d5fd0d Mon Sep 17 00:00:00 2001 From: DanielDerefaka Date: Tue, 30 Dec 2025 00:19:02 +0100 Subject: [PATCH 2/2] Fix naming: mecid -> mechid per review feedback Renamed all occurrences of 'mecid' to 'mechid' (mechanism ID) for clarity and consistency as requested by @sam0x17. --- pallets/admin-utils/src/tests/mod.rs | 20 ++--- pallets/subtensor/rpc/src/lib.rs | 12 +-- pallets/subtensor/runtime-api/src/lib.rs | 2 +- .../subtensor/src/coinbase/reveal_commits.rs | 6 +- pallets/subtensor/src/epoch/run_epoch.rs | 26 +++--- pallets/subtensor/src/macros/dispatches.rs | 30 +++---- .../migrate_crv3_commits_add_block.rs | 2 +- pallets/subtensor/src/rpc_info/metagraph.rs | 20 ++--- pallets/subtensor/src/subnets/mechanism.rs | 40 ++++----- pallets/subtensor/src/subnets/uids.rs | 20 ++--- pallets/subtensor/src/subnets/weights.rs | 52 +++++------ pallets/subtensor/src/swap/swap_hotkey.rs | 4 +- pallets/subtensor/src/tests/epoch_logs.rs | 4 +- pallets/subtensor/src/tests/mechanism.rs | 88 +++++++++---------- runtime/src/lib.rs | 12 +-- 15 files changed, 169 insertions(+), 169 deletions(-) diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index 024871e60f..8d59cb6578 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -2491,9 +2491,9 @@ fn test_trim_to_max_allowed_uids() { ValidatorPermit::::insert(netuid, bool_values.clone()); Active::::insert(netuid, bool_values); - for mecid in 0..mechanism_count.into() { + for mechid in 0..mechanism_count.into() { let netuid_index = - SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mecid)); + SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mechid)); Incentive::::insert(netuid_index, values.clone()); LastUpdate::::insert(netuid_index, u64_values.clone()); } @@ -2548,9 +2548,9 @@ fn test_trim_to_max_allowed_uids() { } } - for mecid in 0..mechanism_count.into() { + for mechid in 0..mechanism_count.into() { let netuid_index = - SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mecid)); + SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mechid)); Weights::::insert(netuid_index, uid, weights.clone()); Bonds::::insert(netuid_index, uid, bonds.clone()); } @@ -2596,9 +2596,9 @@ fn test_trim_to_max_allowed_uids() { assert_eq!(ValidatorPermit::::get(netuid), expected_bools); assert_eq!(StakeWeight::::get(netuid), expected_values); - for mecid in 0..mechanism_count.into() { + for mechid in 0..mechanism_count.into() { let netuid_index = - SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mecid)); + SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mechid)); assert_eq!(Incentive::::get(netuid_index), expected_values); assert_eq!(LastUpdate::::get(netuid_index), expected_u64_values); } @@ -2608,9 +2608,9 @@ fn test_trim_to_max_allowed_uids() { assert!(!Keys::::contains_key(netuid, uid)); assert!(!BlockAtRegistration::::contains_key(netuid, uid)); assert!(!AssociatedEvmAddress::::contains_key(netuid, uid)); - for mecid in 0..mechanism_count.into() { + for mechid in 0..mechanism_count.into() { let netuid_index = - SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mecid)); + SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mechid)); assert!(!Weights::::contains_key(netuid_index, uid)); assert!(!Bonds::::contains_key(netuid_index, uid)); } @@ -2643,9 +2643,9 @@ fn test_trim_to_max_allowed_uids() { // Ensure trimmed uids weights and bonds connections have been trimmed correctly for uid in 0..new_max_n { - for mecid in 0..mechanism_count.into() { + for mechid in 0..mechanism_count.into() { let netuid_index = - SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mecid)); + SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(mechid)); assert!( Weights::::get(netuid_index, uid) .iter() diff --git a/pallets/subtensor/rpc/src/lib.rs b/pallets/subtensor/rpc/src/lib.rs index b5749988ee..b68db2dfcf 100644 --- a/pallets/subtensor/rpc/src/lib.rs +++ b/pallets/subtensor/rpc/src/lib.rs @@ -78,7 +78,7 @@ pub trait SubtensorCustomApi { fn get_mechagraph( &self, netuid: NetUid, - mecid: MechId, + mechid: MechId, at: Option, ) -> RpcResult>; #[method(name = "subnetInfo_getSubnetState")] @@ -103,7 +103,7 @@ pub trait SubtensorCustomApi { fn get_selective_mechagraph( &self, netuid: NetUid, - mecid: MechId, + mechid: MechId, metagraph_index: Vec, at: Option, ) -> RpcResult>; @@ -391,12 +391,12 @@ where fn get_mechagraph( &self, netuid: NetUid, - mecid: MechId, + mechid: MechId, at: Option<::Hash>, ) -> RpcResult> { let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - match api.get_mechagraph(at, netuid, mecid) { + match api.get_mechagraph(at, netuid, mechid) { Ok(result) => Ok(result.encode()), Err(e) => Err(Error::RuntimeError(format!( "Unable to get dynamic subnets info: {e:?}" @@ -502,14 +502,14 @@ where fn get_selective_mechagraph( &self, netuid: NetUid, - mecid: MechId, + mechid: MechId, metagraph_index: Vec, at: Option<::Hash>, ) -> RpcResult> { let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - match api.get_selective_mechagraph(at, netuid, mecid, metagraph_index) { + match api.get_selective_mechagraph(at, netuid, mechid, metagraph_index) { Ok(result) => Ok(result.encode()), Err(e) => { Err(Error::RuntimeError(format!("Unable to get selective metagraph: {e:?}")).into()) diff --git a/pallets/subtensor/runtime-api/src/lib.rs b/pallets/subtensor/runtime-api/src/lib.rs index f1107bad08..fa2464e536 100644 --- a/pallets/subtensor/runtime-api/src/lib.rs +++ b/pallets/subtensor/runtime-api/src/lib.rs @@ -41,7 +41,7 @@ sp_api::decl_runtime_apis! { fn get_all_metagraphs() -> Vec>>; fn get_metagraph(netuid: NetUid) -> Option>; fn get_all_mechagraphs() -> Vec>>; - fn get_mechagraph(netuid: NetUid, mecid: MechId) -> Option>; + fn get_mechagraph(netuid: NetUid, mechid: MechId) -> Option>; fn get_dynamic_info(netuid: NetUid) -> Option>; fn get_subnet_state(netuid: NetUid) -> Option>; fn get_selective_metagraph(netuid: NetUid, metagraph_indexes: Vec) -> Option>; diff --git a/pallets/subtensor/src/coinbase/reveal_commits.rs b/pallets/subtensor/src/coinbase/reveal_commits.rs index 6fdafa76da..7ad63a524c 100644 --- a/pallets/subtensor/src/coinbase/reveal_commits.rs +++ b/pallets/subtensor/src/coinbase/reveal_commits.rs @@ -46,8 +46,8 @@ impl Pallet { // All mechanisms share the same epoch, so the reveal_period/reveal_epoch are also the same // Reveal for all mechanisms - for mecid in 0..MechanismCountCurrent::::get(netuid).into() { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid.into()); + for mechid in 0..MechanismCountCurrent::::get(netuid).into() { + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid.into()); // Clean expired commits for (epoch, _) in TimelockedWeightCommits::::iter_prefix(netuid_index) { @@ -180,7 +180,7 @@ impl Pallet { if let Err(e) = Self::do_set_mechanism_weights( T::RuntimeOrigin::signed(who.clone()), netuid, - MechId::from(mecid), + MechId::from(mechid), uids, values, version_key, diff --git a/pallets/subtensor/src/epoch/run_epoch.rs b/pallets/subtensor/src/epoch/run_epoch.rs index 07a4fe8041..ef11695dd7 100644 --- a/pallets/subtensor/src/epoch/run_epoch.rs +++ b/pallets/subtensor/src/epoch/run_epoch.rs @@ -87,10 +87,10 @@ impl Pallet { /// Persists per-mechanism epoch output in state pub fn persist_mechanism_epoch_terms( netuid: NetUid, - mecid: MechId, + mechid: MechId, output: &BTreeMap, ) { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid); let mut terms_sorted: sp_std::vec::Vec<&EpochTerms> = output.values().collect(); terms_sorted.sort_unstable_by_key(|t| t.uid); @@ -145,11 +145,11 @@ impl Pallet { #[allow(clippy::indexing_slicing)] pub fn epoch_dense_mechanism( netuid: NetUid, - mecid: MechId, + mechid: MechId, rao_emission: AlphaCurrency, ) -> Vec<(T::AccountId, AlphaCurrency, AlphaCurrency)> { // Calculate netuid storage index - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid); // Get subnetwork size. let n: u16 = Self::get_subnetwork_n(netuid); @@ -558,11 +558,11 @@ impl Pallet { /// pub fn epoch_mechanism( netuid: NetUid, - mecid: MechId, + mechid: MechId, rao_emission: AlphaCurrency, ) -> EpochOutput { // Calculate netuid storage index - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid); // Initialize output keys (neuron hotkeys) and UIDs let mut terms_map: BTreeMap = Keys::::iter_prefix(netuid) @@ -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_mecid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mechid(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_mecid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mechid(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_mecid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mechid(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_mecid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mechid(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_mecid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mechid(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_mecid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mechid(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_mecid(netuid_index).unwrap_or_default(); + let (netuid, _) = Self::get_netuid_and_mechid(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/macros/dispatches.rs b/pallets/subtensor/src/macros/dispatches.rs index 8c0b2210ec..deed741cdd 100644 --- a/pallets/subtensor/src/macros/dispatches.rs +++ b/pallets/subtensor/src/macros/dispatches.rs @@ -120,7 +120,7 @@ mod dispatches { /// * `netuid` (u16): /// - The network uid we are setting these weights on. /// - /// * `mecid` (`u8`): + /// * `mechid` (`u8`): /// - The u8 mechnism identifier. /// /// * `dests` (Vec): @@ -168,7 +168,7 @@ mod dispatches { pub fn set_mechanism_weights( origin: OriginFor, netuid: NetUid, - mecid: MechId, + mechid: MechId, dests: Vec, weights: Vec, version_key: u64, @@ -176,7 +176,7 @@ mod dispatches { if Self::get_commit_reveal_weights_enabled(netuid) { Err(Error::::CommitRevealEnabled.into()) } else { - Self::do_set_mechanism_weights(origin, netuid, mecid, dests, weights, version_key) + Self::do_set_mechanism_weights(origin, netuid, mechid, dests, weights, version_key) } } @@ -258,7 +258,7 @@ mod dispatches { /// * `netuid` (`u16`): /// - The u16 network identifier. /// - /// * `mecid` (`u8`): + /// * `mechid` (`u8`): /// - The u8 mechanism identifier. /// /// * `commit_hash` (`H256`): @@ -278,10 +278,10 @@ mod dispatches { pub fn commit_mechanism_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, commit_hash: H256, ) -> DispatchResult { - Self::do_commit_mechanism_weights(origin, netuid, mecid, commit_hash) + Self::do_commit_mechanism_weights(origin, netuid, mechid, commit_hash) } /// --- Allows a hotkey to commit weight hashes for multiple netuids as a batch. @@ -379,7 +379,7 @@ mod dispatches { /// * `netuid` (`u16`): /// - The u16 network identifier. /// - /// * `mecid` (`u8`): + /// * `mechid` (`u8`): /// - The u8 mechanism identifier. /// /// * `uids` (`Vec`): @@ -417,7 +417,7 @@ mod dispatches { pub fn reveal_mechanism_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, uids: Vec, values: Vec, salt: Vec, @@ -426,7 +426,7 @@ mod dispatches { Self::do_reveal_mechanism_weights( origin, netuid, - mecid, + mechid, uids, values, salt, @@ -485,7 +485,7 @@ mod dispatches { /// * `netuid` (`u16`): /// - The u16 network identifier. /// - /// * `mecid` (`u8`): + /// * `mechid` (`u8`): /// - The u8 mechanism identifier. /// /// * `commit` (`Vec`): @@ -515,14 +515,14 @@ mod dispatches { pub fn commit_crv3_mechanism_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, commit: BoundedVec>, reveal_round: u64, ) -> DispatchResult { Self::do_commit_timelocked_mechanism_weights( origin, netuid, - mecid, + mechid, commit, reveal_round, 4, @@ -2264,7 +2264,7 @@ mod dispatches { /// * `netuid` (`u16`): /// - The u16 network identifier. /// - /// * `mecid` (`u8`): + /// * `mechid` (`u8`): /// - The u8 mechanism identifier. /// /// * `commit` (`Vec`): @@ -2289,7 +2289,7 @@ mod dispatches { pub fn commit_timelocked_mechanism_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, commit: BoundedVec>, reveal_round: u64, commit_reveal_version: u16, @@ -2297,7 +2297,7 @@ mod dispatches { Self::do_commit_timelocked_mechanism_weights( origin, netuid, - mecid, + mechid, commit, reveal_round, commit_reveal_version, 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 6dacfbc3c8..6dd072d543 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_mecid(netuid_index).unwrap_or_default(); + let (netuid, _) = Pallet::::get_netuid_and_mechid(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/rpc_info/metagraph.rs b/pallets/subtensor/src/rpc_info/metagraph.rs index ea24657aeb..25e2227633 100644 --- a/pallets/subtensor/src/rpc_info/metagraph.rs +++ b/pallets/subtensor/src/rpc_info/metagraph.rs @@ -808,15 +808,15 @@ impl Pallet { metagraphs } - pub fn get_mechagraph(netuid: NetUid, mecid: MechId) -> Option> { - if Self::ensure_mechanism_exists(netuid, mecid).is_err() { + pub fn get_mechagraph(netuid: NetUid, mechid: MechId) -> Option> { + if Self::ensure_mechanism_exists(netuid, mechid).is_err() { return None; } // Get netuid metagraph let maybe_meta = Self::get_metagraph(netuid); if let Some(mut meta) = maybe_meta { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid); // Update with mechanism information meta.netuid = NetUid::from(u16::from(netuid_index)).into(); @@ -840,8 +840,8 @@ impl Pallet { let mut metagraphs = Vec::>>::new(); for netuid in netuids.clone().iter() { let mechanism_count = u8::from(MechanismCountCurrent::::get(netuid)); - for mecid in 0..mechanism_count { - metagraphs.push(Self::get_mechagraph(*netuid, MechId::from(mecid))); + for mechid in 0..mechanism_count { + metagraphs.push(Self::get_mechagraph(*netuid, MechId::from(mechid))); } } metagraphs @@ -865,7 +865,7 @@ impl Pallet { pub fn get_selective_mechagraph( netuid: NetUid, - mecid: MechId, + mechid: MechId, metagraph_indexes: Vec, ) -> Option> { if !Self::if_subnet_exist(netuid) { @@ -874,7 +874,7 @@ impl Pallet { let mut result = SelectiveMetagraph::default(); for index in metagraph_indexes.iter() { - let value = Self::get_single_selective_mechagraph(netuid, mecid, *index); + let value = Self::get_single_selective_mechagraph(netuid, mechid, *index); result.merge_value(&value, *index as usize); } // always include netuid even the metagraph_indexes doesn't contain it @@ -1455,12 +1455,12 @@ impl Pallet { fn get_single_selective_mechagraph( netuid: NetUid, - mecid: MechId, + mechid: MechId, metagraph_index: u16, ) -> SelectiveMetagraph { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid); - // Default to netuid, replace as needed for mecid + // Default to netuid, replace as needed for mechid match SelectiveMetagraphIndex::from_index(metagraph_index as usize) { Some(SelectiveMetagraphIndex::Incentives) => SelectiveMetagraph { netuid: netuid.into(), diff --git a/pallets/subtensor/src/subnets/mechanism.rs b/pallets/subtensor/src/subnets/mechanism.rs index 2a2b214f5c..9d6a974f96 100644 --- a/pallets/subtensor/src/subnets/mechanism.rs +++ b/pallets/subtensor/src/subnets/mechanism.rs @@ -18,9 +18,9 @@ pub type BalanceOf = /// Theoretical maximum of subnets on bittensor. This value is used in indexed /// storage of epoch values for mechanisms as /// -/// `storage_index = netuid + mecid * GLOBAL_MAX_SUBNET_COUNT` +/// `storage_index = netuid + mechid * GLOBAL_MAX_SUBNET_COUNT` /// -/// For mecid = 0 this index results in netuid and provides backward compatibility +/// For mechid = 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, mecid: MechId) -> NetUidStorageIndex { - u16::from(mecid) + pub fn get_mechanism_storage_index(netuid: NetUid, mechid: MechId) -> NetUidStorageIndex { + u16::from(mechid) .saturating_mul(GLOBAL_MAX_SUBNET_COUNT) .saturating_add(u16::from(netuid)) .into() @@ -48,7 +48,7 @@ impl Pallet { } } - pub fn get_netuid_and_mecid( + pub fn get_netuid_and_mechid( 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 mecid - let mecid_u8 = u8::try_from(u16::from(netuid_index).safe_div(GLOBAL_MAX_SUBNET_COUNT)) + // Extract mechid + let mechid_u8 = u8::try_from(u16::from(netuid_index).safe_div(GLOBAL_MAX_SUBNET_COUNT)) .map_err(|_| Error::::MechanismDoesNotExist)?; - let mecid = MechId::from(mecid_u8); + let mechid = MechId::from(mechid_u8); - if MechanismCountCurrent::::get(netuid) > mecid { - Ok((netuid, mecid)) + if MechanismCountCurrent::::get(netuid) > mechid { + Ok((netuid, mechid)) } else { Err(Error::::MechanismDoesNotExist.into()) } @@ -80,7 +80,7 @@ impl Pallet { MechanismCountCurrent::::get(netuid) } - pub fn ensure_mechanism_exists(netuid: NetUid, mecid: MechId) -> DispatchResult { + pub fn ensure_mechanism_exists(netuid: NetUid, mechid: 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) > mecid, + MechanismCountCurrent::::get(netuid) > mechid, Error::::MechanismDoesNotExist ); Ok(()) @@ -132,9 +132,9 @@ impl Pallet { let new_count_u8 = u8::from(new_count); if old_count != new_count_u8 { if old_count > new_count_u8 { - for mecid in new_count_u8..old_count { + for mechid in new_count_u8..old_count { let netuid_index = - Self::get_mechanism_storage_index(netuid, MechId::from(mecid)); + Self::get_mechanism_storage_index(netuid, MechId::from(mechid)); // Cleanup Weights let _ = Weights::::clear_prefix(netuid_index, u32::MAX, None); @@ -260,20 +260,20 @@ impl Pallet { .into_iter() .enumerate() // Run epoch function for each mechanism to distribute its portion of emissions - .flat_map(|(mecid_usize, mech_emission)| { - let mecid_u8: u8 = mecid_usize.try_into().unwrap_or_default(); - let mecid = MechId::from(mecid_u8); + .flat_map(|(mechid_usize, mech_emission)| { + let mechid_u8: u8 = mechid_usize.try_into().unwrap_or_default(); + let mechid = MechId::from(mechid_u8); // Run epoch function on the mechanism emission - let epoch_output = Self::epoch_mechanism(netuid, mecid, mech_emission); - Self::persist_mechanism_epoch_terms(netuid, mecid, epoch_output.as_map()); + let epoch_output = Self::epoch_mechanism(netuid, mechid, mech_emission); + Self::persist_mechanism_epoch_terms(netuid, mechid, 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 mech_weight = U64F64::saturating_from_num(mech_emission).safe_div_or( U64F64::saturating_from_num(rao_emission), - U64F64::saturating_from_num(if mecid_u8 == 0 { 1 } else { 0 }), + U64F64::saturating_from_num(if mechid_u8 == 0 { 1 } else { 0 }), ); // Produce an iterator of (hotkey, (terms, mech_weight)) tuples diff --git a/pallets/subtensor/src/subnets/uids.rs b/pallets/subtensor/src/subnets/uids.rs index 0a09017e64..1a4dcbfd9c 100644 --- a/pallets/subtensor/src/subnets/uids.rs +++ b/pallets/subtensor/src/subnets/uids.rs @@ -24,8 +24,8 @@ impl Pallet { let neuron_index: usize = neuron_uid.into(); Emission::::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0.into())); Consensus::::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0)); - for mecid in 0..MechanismCountCurrent::::get(netuid).into() { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid.into()); + for mechid in 0..MechanismCountCurrent::::get(netuid).into() { + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid.into()); Incentive::::mutate(netuid_index, |v| Self::set_element_at(v, neuron_index, 0)); Bonds::::remove(netuid_index, neuron_uid); // Remove bonds for Validator. @@ -111,8 +111,8 @@ impl Pallet { Active::::mutate(netuid, |v| v.push(true)); Emission::::mutate(netuid, |v| v.push(0.into())); Consensus::::mutate(netuid, |v| v.push(0)); - for mecid in 0..MechanismCountCurrent::::get(netuid).into() { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid.into()); + for mechid in 0..MechanismCountCurrent::::get(netuid).into() { + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid.into()); Incentive::::mutate(netuid_index, |v| v.push(0)); Self::set_last_update_for_uid(netuid_index, next_uid, block_number); } @@ -205,8 +205,8 @@ impl Pallet { Keys::::remove(netuid, neuron_uid); BlockAtRegistration::::remove(netuid, neuron_uid); AssociatedEvmAddress::::remove(netuid, neuron_uid); - for mecid in 0..mechanisms_count { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid.into()); + for mechid in 0..mechanisms_count { + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid.into()); Weights::::remove(netuid_index, neuron_uid); Bonds::::remove(netuid_index, neuron_uid); } @@ -264,8 +264,8 @@ impl Pallet { StakeWeight::::insert(netuid, trimmed_stake_weight); // Update incentives/lastupdates for mechanisms - for mecid in 0..mechanisms_count { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid.into()); + for mechid in 0..mechanisms_count { + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid.into()); let incentive = Incentive::::get(netuid_index); let lastupdate = LastUpdate::::get(netuid_index); let mut trimmed_incentive = Vec::with_capacity(trimmed_uids.len()); @@ -302,8 +302,8 @@ impl Pallet { AssociatedEvmAddress::::swap(netuid, old_neuron_uid, netuid, new_neuron_uid); BlockAtRegistration::::swap(netuid, old_neuron_uid, netuid, new_neuron_uid); - for mecid in 0..mechanisms_count { - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid.into()); + for mechid in 0..mechanisms_count { + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid.into()); // Swap to new position and remap all target uids Weights::::swap(netuid_index, old_neuron_uid, netuid_index, new_neuron_uid); diff --git a/pallets/subtensor/src/subnets/weights.rs b/pallets/subtensor/src/subnets/weights.rs index f47cbd9628..70b526ba32 100644 --- a/pallets/subtensor/src/subnets/weights.rs +++ b/pallets/subtensor/src/subnets/weights.rs @@ -52,23 +52,23 @@ impl Pallet { pub fn do_commit_mechanism_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, commit_hash: H256, ) -> DispatchResult { - Self::internal_commit_weights(origin, netuid, mecid, commit_hash) + Self::internal_commit_weights(origin, netuid, mechid, commit_hash) } fn internal_commit_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, commit_hash: H256, ) -> DispatchResult { - // Ensure netuid and mecid exist - Self::ensure_mechanism_exists(netuid, mecid)?; + // Ensure netuid and mechid exist + Self::ensure_mechanism_exists(netuid, mechid)?; // Calculate subnet storage index - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid); // 1. Verify the caller's signature (hotkey). let who = ensure_signed(origin)?; @@ -277,7 +277,7 @@ impl Pallet { pub fn do_commit_timelocked_mechanism_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, commit: BoundedVec>, reveal_round: u64, commit_reveal_version: u16, @@ -285,7 +285,7 @@ impl Pallet { Self::internal_commit_timelocked_weights( origin, netuid, - mecid, + mechid, commit, reveal_round, commit_reveal_version, @@ -295,16 +295,16 @@ impl Pallet { pub fn internal_commit_timelocked_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, commit: BoundedVec>, reveal_round: u64, commit_reveal_version: u16, ) -> DispatchResult { - // Ensure netuid and mecid exist - Self::ensure_mechanism_exists(netuid, mecid)?; + // Ensure netuid and mechid exist + Self::ensure_mechanism_exists(netuid, mechid)?; // Calculate netuid storage index - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid); // 1. Verify the caller's signature (hotkey). let who = ensure_signed(origin)?; @@ -439,26 +439,26 @@ impl Pallet { pub fn do_reveal_mechanism_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, uids: Vec, values: Vec, salt: Vec, version_key: u64, ) -> DispatchResult { - Self::internal_reveal_weights(origin, netuid, mecid, uids, values, salt, version_key) + Self::internal_reveal_weights(origin, netuid, mechid, uids, values, salt, version_key) } fn internal_reveal_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, uids: Vec, values: Vec, salt: Vec, version_key: u64, ) -> DispatchResult { // Calculate netuid storage index - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid); // --- 1. Check the caller's signature (hotkey). let who = ensure_signed(origin.clone())?; @@ -536,7 +536,7 @@ impl Pallet { Self::do_set_mechanism_weights( origin, netuid, - mecid, + mechid, uids.clone(), values.clone(), version_key, @@ -748,13 +748,13 @@ impl Pallet { fn internal_set_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, uids: Vec, values: Vec, version_key: u64, ) -> dispatch::DispatchResult { // Calculate subnet storage index - let netuid_index = Self::get_mechanism_storage_index(netuid, mecid); + let netuid_index = Self::get_mechanism_storage_index(netuid, mechid); // --- 1. Check the caller's signature. This is the hotkey of a registered account. let hotkey = ensure_signed(origin)?; @@ -772,7 +772,7 @@ impl Pallet { ); // --- 3. Check to see if this is a valid network and sub-subnet. - Self::ensure_mechanism_exists(netuid, mecid)?; + Self::ensure_mechanism_exists(netuid, mechid)?; // --- 4. Check to see if the number of uids is within the max allowed uids for this network. ensure!( @@ -935,7 +935,7 @@ impl Pallet { /// * 'netuid' (u16): /// - The u16 network identifier. /// - /// * 'mecid' (u8): + /// * 'mechid' (u8): /// - The u8 identifier of sub-subnet. /// /// * 'uids' ( Vec ): @@ -988,12 +988,12 @@ impl Pallet { pub fn do_set_mechanism_weights( origin: T::RuntimeOrigin, netuid: NetUid, - mecid: MechId, + mechid: MechId, uids: Vec, values: Vec, version_key: u64, ) -> dispatch::DispatchResult { - Self::internal_set_weights(origin, netuid, mecid, uids, values, version_key) + Self::internal_set_weights(origin, netuid, mechid, uids, values, version_key) } /// ---- The implementation for the extrinsic batch_set_weights. @@ -1108,8 +1108,8 @@ impl Pallet { neuron_uid: u16, current_block: u64, ) -> bool { - let maybe_netuid_and_mecid = Self::get_netuid_and_mecid(netuid_index); - if let Ok((netuid, _)) = maybe_netuid_and_mecid + let maybe_netuid_and_mechid = Self::get_netuid_and_mechid(netuid_index); + if let Ok((netuid, _)) = maybe_netuid_and_mechid && Self::is_uid_exist_on_network(netuid, neuron_uid) { // --- 1. Ensure that the diff between current and last_set weights is greater than limit. @@ -1121,7 +1121,7 @@ impl Pallet { >= Self::get_weights_set_rate_limit(netuid); } - // --- 3. Non registered peers cant pass. Neither can non-existing mecid + // --- 3. Non registered peers cant pass. Neither can non-existing mechid false } diff --git a/pallets/subtensor/src/swap/swap_hotkey.rs b/pallets/subtensor/src/swap/swap_hotkey.rs index 4fdf87fb7b..8da0741400 100644 --- a/pallets/subtensor/src/swap/swap_hotkey.rs +++ b/pallets/subtensor/src/swap/swap_hotkey.rs @@ -388,8 +388,8 @@ impl Pallet { // 3.5 Swap WeightCommits // WeightCommits( hotkey ) --> Vec -- the weight commits for the hotkey. if is_network_member { - for mecid in 0..MechanismCountCurrent::::get(netuid).into() { - let netuid_index = Self::get_mechanism_storage_index(netuid, MechId::from(mecid)); + for mechid in 0..MechanismCountCurrent::::get(netuid).into() { + let netuid_index = Self::get_mechanism_storage_index(netuid, MechId::from(mechid)); if let Ok(old_weight_commits) = WeightCommits::::try_get(netuid_index, old_hotkey) { diff --git a/pallets/subtensor/src/tests/epoch_logs.rs b/pallets/subtensor/src/tests/epoch_logs.rs index 38e3a1b734..165a5d22b8 100644 --- a/pallets/subtensor/src/tests/epoch_logs.rs +++ b/pallets/subtensor/src/tests/epoch_logs.rs @@ -108,8 +108,8 @@ fn set_weights(netuid: NetUid, weights: Vec>, indices: Vec) { /// Write sparse weight rows **for a specific mechanism**. /// `rows` is a list of `(validator_uid, row)` where `row` is `[(dest_uid, weight_u16)]`. -fn set_weights_for_mech(netuid: NetUid, mecid: MechId, rows: Vec<(u16, Vec<(u16, u16)>)>) { - let netuid_index = SubtensorModule::get_mechanism_storage_index(netuid, mecid); +fn set_weights_for_mech(netuid: NetUid, mechid: MechId, rows: Vec<(u16, Vec<(u16, u16)>)>) { + let netuid_index = SubtensorModule::get_mechanism_storage_index(netuid, mechid); for (uid, sparse_row) in rows { Weights::::insert(netuid_index, uid, sparse_row); } diff --git a/pallets/subtensor/src/tests/mechanism.rs b/pallets/subtensor/src/tests/mechanism.rs index 8fe1467e14..db76eff147 100644 --- a/pallets/subtensor/src/tests/mechanism.rs +++ b/pallets/subtensor/src/tests/mechanism.rs @@ -115,11 +115,11 @@ fn test_netuid_and_subnet_from_index() { MechId::from(expected_subid + 1), ); - let (netuid, mecid) = - SubtensorModule::get_netuid_and_mecid(NetUidStorageIndex::from(*netuid_index)) + let (netuid, mechid) = + SubtensorModule::get_netuid_and_mechid(NetUidStorageIndex::from(*netuid_index)) .unwrap(); assert_eq!(netuid, NetUid::from(expected_netuid)); - assert_eq!(mecid, MechId::from(expected_subid)); + assert_eq!(mechid, MechId::from(expected_subid)); }); }); } @@ -296,7 +296,7 @@ fn update_mechanism_counts_decreases_and_cleans() { // Set non-default subnet emission split MechanismEmissionSplit::::insert(netuid, vec![123u16, 234u16, 345u16]); - // Seed data at a kept mecid (1) and a removed mecid (2) + // Seed data at a kept mechid (1) and a removed mechid (2) let idx_keep = SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(1u8)); let idx_rm3 = SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(2u8)); @@ -346,7 +346,7 @@ fn update_mechanism_counts_decreases_and_cleans() { idx_keep, 1u64 )); - // Removed prefix (mecid 3) cleared + // Removed prefix (mechid 3) cleared assert!(Weights::::iter_prefix(idx_rm3).next().is_none()); assert_eq!(Incentive::::get(idx_rm3), Vec::::new()); assert!(!LastUpdate::::contains_key(idx_rm3)); @@ -848,7 +848,7 @@ fn neuron_dereg_cleans_weights_across_subids() { Consensus::::insert(netuid, vec![21u16, 88u16, 44u16]); Dividends::::insert(netuid, vec![7u16, 77u16, 17u16]); - // Clearing per-mecid maps + // Clearing per-mechid maps for sub in [0u8, 1u8] { let idx = SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(sub)); @@ -879,7 +879,7 @@ fn neuron_dereg_cleans_weights_across_subids() { let d = Dividends::::get(netuid); assert_eq!(d, vec![7, 0, 17]); - // Per-mecid cleanup + // Per-mechid cleanup for sub in [0u8, 1u8] { let idx = SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(sub)); @@ -958,9 +958,9 @@ fn test_set_mechanism_weights_happy_path_sets_row_under_subid() { 1.into(), ); - // Have at least two sub-subnets; write under mecid = 1 + // Have at least two sub-subnets; write under mechid = 1 MechanismCountCurrent::::insert(netuid, MechId::from(2u8)); - let mecid = MechId::from(1u8); + let mechid = MechId::from(1u8); // Call extrinsic let dests = vec![uid2, uid3]; @@ -968,14 +968,14 @@ fn test_set_mechanism_weights_happy_path_sets_row_under_subid() { assert_ok!(SubtensorModule::set_mechanism_weights( RawOrigin::Signed(hk1).into(), netuid, - mecid, + mechid, dests.clone(), weights.clone(), 0, // version_key )); - // Verify row exists under the chosen mecid and not under a different mecid - let idx1 = SubtensorModule::get_mechanism_storage_index(netuid, mecid); + // Verify row exists under the chosen mechid and not under a different mechid + let idx1 = SubtensorModule::get_mechanism_storage_index(netuid, mechid); assert_eq!( Weights::::get(idx1, uid1), vec![(uid2, 88u16), (uid3, 0xFFFF)] @@ -1015,7 +1015,7 @@ fn test_set_mechanism_weights_above_mechanism_count_fails() { 1.into(), ); - // Have exactly two sub-subnets; write under mecid = 1 + // Have exactly two sub-subnets; write under mechid = 1 MechanismCountCurrent::::insert(netuid, MechId::from(2u8)); let subid_above = MechId::from(2u8); @@ -1073,13 +1073,13 @@ fn test_commit_reveal_mechanism_weights_ok() { 1.into(), ); - // Ensure sub-subnet exists; write under mecid = 1 + // Ensure sub-subnet exists; write under mechid = 1 MechanismCountCurrent::::insert(netuid, MechId::from(2u8)); - let mecid = MechId::from(1u8); + let mechid = MechId::from(1u8); let idx0 = SubtensorModule::get_mechanism_storage_index(netuid, MechId::from(0u8)); - let idx1 = SubtensorModule::get_mechanism_storage_index(netuid, mecid); + let idx1 = SubtensorModule::get_mechanism_storage_index(netuid, mechid); - // Prepare payload and commit hash (include mecid!) + // Prepare payload and commit hash (include mechid!) let dests = vec![uid2, uid3]; let weights = vec![88u16, 0xFFFFu16]; let salt: Vec = vec![1, 2, 3, 4, 5, 6, 7, 8]; @@ -1097,7 +1097,7 @@ fn test_commit_reveal_mechanism_weights_ok() { assert_ok!(SubtensorModule::commit_mechanism_weights( RuntimeOrigin::signed(hk1), netuid, - mecid, + mechid, commit_hash )); @@ -1106,20 +1106,20 @@ fn test_commit_reveal_mechanism_weights_ok() { assert_ok!(SubtensorModule::reveal_mechanism_weights( RuntimeOrigin::signed(hk1), netuid, - mecid, + mechid, dests.clone(), weights.clone(), salt, version_key )); - // Verify weights stored under the chosen mecid (normalized keeps max=0xFFFF here) + // Verify weights stored under the chosen mechid (normalized keeps max=0xFFFF here) assert_eq!( Weights::::get(idx1, uid1), vec![(uid2, 88u16), (uid3, 0xFFFFu16)] ); - // And not under a different mecid + // And not under a different mechid assert!(Weights::::get(idx0, uid1).is_empty()); }); } @@ -1214,13 +1214,13 @@ fn test_reveal_crv3_commits_sub_success() { System::set_block_number(0); let netuid = NetUid::from(1); - let mecid = MechId::from(1u8); // write under sub-subnet #1 + let mechid = MechId::from(1u8); // write under sub-subnet #1 let hotkey1: AccountId = U256::from(1); let hotkey2: AccountId = U256::from(2); let reveal_round: u64 = 1000; add_network(netuid, 5, 0); - // ensure we actually have mecid=1 available + // ensure we actually have mechid=1 available MechanismCountCurrent::::insert(netuid, MechId::from(2u8)); // Register neurons and set up configs @@ -1243,7 +1243,7 @@ fn test_reveal_crv3_commits_sub_success() { let version_key = SubtensorModule::get_weights_version_key(netuid); - // Payload (same as legacy; mecid is provided to the extrinsic) + // Payload (same as legacy; mechid is provided to the extrinsic) let payload = WeightsTlockPayload { hotkey: hotkey1.encode(), values: vec![10, 20], @@ -1273,7 +1273,7 @@ fn test_reveal_crv3_commits_sub_success() { assert_ok!(SubtensorModule::commit_timelocked_mechanism_weights( RuntimeOrigin::signed(hotkey1), netuid, - mecid, + mechid, commit_bytes.clone().try_into().expect("bounded"), reveal_round, SubtensorModule::get_commit_reveal_weights_version() @@ -1293,11 +1293,11 @@ fn test_reveal_crv3_commits_sub_success() { // Run epochs so the commit is processed step_epochs(3, netuid); - // Verify weights applied under the selected mecid index - let idx = SubtensorModule::get_mechanism_storage_index(netuid, mecid); + // Verify weights applied under the selected mechid index + let idx = SubtensorModule::get_mechanism_storage_index(netuid, mechid); let weights_sparse = SubtensorModule::get_weights_sparse(idx); let row = weights_sparse.get(uid1 as usize).cloned().unwrap_or_default(); - assert!(!row.is_empty(), "expected weights set for validator uid1 under mecid"); + assert!(!row.is_empty(), "expected weights set for validator uid1 under mechid"); // Compare rounded normalized weights to expected proportions (like legacy test) let expected: Vec<(u16, I32F32)> = payload.uids.iter().zip(payload.values.iter()).map(|(&u,&v)|(u, I32F32::from_num(v))).collect(); @@ -1326,7 +1326,7 @@ fn test_crv3_above_mechanism_count_fails() { let reveal_round: u64 = 1000; add_network(netuid, 5, 0); - // ensure we actually have mecid=1 available + // ensure we actually have mechid=1 available MechanismCountCurrent::::insert(netuid, MechId::from(2u8)); // Register neurons and set up configs @@ -1346,7 +1346,7 @@ fn test_crv3_above_mechanism_count_fails() { let version_key = SubtensorModule::get_weights_version_key(netuid); - // Payload (same as legacy; mecid is provided to the extrinsic) + // Payload (same as legacy; mechid is provided to the extrinsic) let payload = WeightsTlockPayload { hotkey: hotkey1.encode(), values: vec![10, 20], @@ -1391,7 +1391,7 @@ fn test_crv3_above_mechanism_count_fails() { fn test_do_commit_crv3_mechanism_weights_committing_too_fast() { new_test_ext(1).execute_with(|| { let netuid = NetUid::from(1); - let mecid = MechId::from(1u8); + let mechid = MechId::from(1u8); let hotkey: AccountId = U256::from(1); let commit_data_1: Vec = vec![1, 2, 3]; let commit_data_2: Vec = vec![4, 5, 6]; @@ -1405,7 +1405,7 @@ fn test_do_commit_crv3_mechanism_weights_committing_too_fast() { SubtensorModule::set_commit_reveal_weights_enabled(netuid, true); let uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey).expect("uid"); - let idx1 = SubtensorModule::get_mechanism_storage_index(netuid, mecid); + let idx1 = SubtensorModule::get_mechanism_storage_index(netuid, mechid); SubtensorModule::set_last_update_for_uid(idx1, uid, 0); // make validator with stake @@ -1419,22 +1419,22 @@ fn test_do_commit_crv3_mechanism_weights_committing_too_fast() { 1.into(), ); - // first commit OK on mecid=1 + // first commit OK on mechid=1 assert_ok!(SubtensorModule::commit_timelocked_mechanism_weights( RuntimeOrigin::signed(hotkey), netuid, - mecid, + mechid, commit_data_1.clone().try_into().expect("bounded"), reveal_round, SubtensorModule::get_commit_reveal_weights_version() )); - // immediate second commit on SAME mecid blocked + // immediate second commit on SAME mechid blocked assert_noop!( SubtensorModule::commit_timelocked_mechanism_weights( RuntimeOrigin::signed(hotkey), netuid, - mecid, + mechid, commit_data_2.clone().try_into().expect("bounded"), reveal_round, SubtensorModule::get_commit_reveal_weights_version() @@ -1442,7 +1442,7 @@ fn test_do_commit_crv3_mechanism_weights_committing_too_fast() { Error::::CommittingWeightsTooFast ); - // BUT committing too soon on a DIFFERENT mecid is allowed + // BUT committing too soon on a DIFFERENT mechid is allowed let other_subid = MechId::from(0u8); let idx0 = SubtensorModule::get_mechanism_storage_index(netuid, other_subid); SubtensorModule::set_last_update_for_uid(idx0, uid, 0); // baseline like above @@ -1455,13 +1455,13 @@ fn test_do_commit_crv3_mechanism_weights_committing_too_fast() { SubtensorModule::get_commit_reveal_weights_version() )); - // still too fast on original mecid after 2 blocks + // still too fast on original mechid after 2 blocks step_block(2); assert_noop!( SubtensorModule::commit_timelocked_mechanism_weights( RuntimeOrigin::signed(hotkey), netuid, - mecid, + mechid, commit_data_2.clone().try_into().expect("bounded"), reveal_round, SubtensorModule::get_commit_reveal_weights_version() @@ -1469,12 +1469,12 @@ fn test_do_commit_crv3_mechanism_weights_committing_too_fast() { Error::::CommittingWeightsTooFast ); - // after enough blocks, OK again on original mecid + // after enough blocks, OK again on original mechid step_block(3); assert_ok!(SubtensorModule::commit_timelocked_mechanism_weights( RuntimeOrigin::signed(hotkey), netuid, - mecid, + mechid, commit_data_2.try_into().expect("bounded"), reveal_round, SubtensorModule::get_commit_reveal_weights_version() @@ -1487,8 +1487,8 @@ fn epoch_mechanism_emergency_mode_distributes_by_stake() { new_test_ext(1).execute_with(|| { // setup a single sub-subnet where consensus sum becomes 0 let netuid = NetUid::from(1u16); - let mecid = MechId::from(1u8); - let idx = SubtensorModule::get_mechanism_storage_index(netuid, mecid); + let mechid = MechId::from(1u8); + let idx = SubtensorModule::get_mechanism_storage_index(netuid, mechid); let tempo: u16 = 5; add_network(netuid, tempo, 0); MechanismCountCurrent::::insert(netuid, MechId::from(2u8)); // allow subids {0,1} @@ -1547,7 +1547,7 @@ fn epoch_mechanism_emergency_mode_distributes_by_stake() { let emission = AlphaCurrency::from(1_000_000u64); // --- act: run epoch on this sub-subnet only --- - let out = SubtensorModule::epoch_mechanism(netuid, mecid, emission); + let out = SubtensorModule::epoch_mechanism(netuid, mechid, emission); // collect validator emissions per hotkey let t0 = out.0.get(&hk0).unwrap(); diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 52746675f9..edea603751 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -946,8 +946,8 @@ impl OnMetadataCommitment for ResetBondsOnCommit { fn on_metadata_commitment(netuid: NetUid, address: &AccountId) { // Reset bonds for each mechanism of this subnet let mechanism_count = SubtensorModule::get_current_mechanism_count(netuid); - for mecid in 0..u8::from(mechanism_count) { - let netuid_index = SubtensorModule::get_mechanism_storage_index(netuid, mecid.into()); + for mechid in 0..u8::from(mechanism_count) { + let netuid_index = SubtensorModule::get_mechanism_storage_index(netuid, mechid.into()); let _ = SubtensorModule::do_reset_bonds(netuid_index, address); } } @@ -2427,8 +2427,8 @@ impl_runtime_apis! { SubtensorModule::get_metagraph(netuid) } - fn get_mechagraph(netuid: NetUid, mecid: MechId) -> Option> { - SubtensorModule::get_mechagraph(netuid, mecid) + fn get_mechagraph(netuid: NetUid, mechid: MechId) -> Option> { + SubtensorModule::get_mechagraph(netuid, mechid) } fn get_subnet_state(netuid: NetUid) -> Option> { @@ -2458,8 +2458,8 @@ impl_runtime_apis! { SubtensorModule::get_coldkey_auto_stake_hotkey(coldkey, netuid) } - fn get_selective_mechagraph(netuid: NetUid, mecid: MechId, metagraph_indexes: Vec) -> Option> { - SubtensorModule::get_selective_mechagraph(netuid, mecid, metagraph_indexes) + fn get_selective_mechagraph(netuid: NetUid, mechid: MechId, metagraph_indexes: Vec) -> Option> { + SubtensorModule::get_selective_mechagraph(netuid, mechid, metagraph_indexes) } }