From 125d8a618a65609c55de2d8bee080eca34216c92 Mon Sep 17 00:00:00 2001 From: unconst Date: Tue, 7 Jan 2025 14:19:47 -0500 Subject: [PATCH] make tao weight global --- .../subtensor/src/coinbase/run_coinbase.rs | 6 ++--- pallets/subtensor/src/lib.rs | 4 ++-- .../subtensor/src/migrations/migrate_rao.rs | 4 ++-- pallets/subtensor/src/staking/stake_utils.rs | 10 ++++----- pallets/subtensor/src/subnets/subnet.rs | 3 --- pallets/subtensor/src/tests/weights.rs | 22 ------------------- 6 files changed, 12 insertions(+), 37 deletions(-) diff --git a/pallets/subtensor/src/coinbase/run_coinbase.rs b/pallets/subtensor/src/coinbase/run_coinbase.rs index c30d00373..ce1b6b8e7 100644 --- a/pallets/subtensor/src/coinbase/run_coinbase.rs +++ b/pallets/subtensor/src/coinbase/run_coinbase.rs @@ -71,7 +71,7 @@ impl Pallet { // Get total ALPHA on subnet. let total_alpha_issuance: I96F32 = I96F32::from_num(Self::get_alpha_issuance( netuid )); // Get tao_weight - let tao_weight: I96F32 = total_root_tao.saturating_mul( Self::get_tao_weight( netuid ) ); + let tao_weight: I96F32 = total_root_tao.saturating_mul( Self::get_tao_weight() ); // Get root proportional dividends. let root_proportion: I96F32 = tao_weight.checked_div( tao_weight.saturating_add( total_alpha_issuance ) ).unwrap_or( I96F32::from_num( 0.0 ) ); // Get root proportion of alpha_out dividends. @@ -293,7 +293,7 @@ impl Pallet { // 7.6.3.3: Get the local alpha and root alpha. let hotkey_tao: I96F32 = I96F32::from_num( Self::get_stake_for_hotkey_on_subnet( &hotkey, Self::get_root_netuid() ) ); - let hotkey_tao_as_alpha: I96F32 = hotkey_tao.saturating_mul( Self::get_tao_weight(netuid) ); + let hotkey_tao_as_alpha: I96F32 = hotkey_tao.saturating_mul( Self::get_tao_weight() ); let hotkey_alpha = I96F32::from_num(Self::get_stake_for_hotkey_on_subnet( &hotkey, netuid )); log::debug!("Hotkey tao for hotkey {:?} on root netuid: {:?}, hotkey tao as alpha: {:?}, hotkey alpha: {:?}", hotkey, hotkey_tao, hotkey_tao_as_alpha, hotkey_alpha); @@ -381,7 +381,7 @@ impl Pallet { let mut contributions: Vec<(T::AccountId, I96F32)> = Vec::new(); // Get the weights for root and alpha stakes in emission distribution - let tao_weight: I96F32 = Self::get_tao_weight(netuid); + let tao_weight: I96F32 = Self::get_tao_weight(); // Calculate total root and alpha (subnet-specific) stakes from all parents for (proportion, parent) in Self::get_parents(hotkey, netuid) { diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index 661b132b6..1cf9f9390 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -737,8 +737,8 @@ pub mod pallet { /// Eventually, Bittensor should migrate to using Holds afterwhich time we will not require this /// separate accounting. #[pallet::storage] - /// --- MAP ( netuid ) --> Global weight - pub type TaoWeight = StorageMap<_, Identity, u16, u64, ValueQuery, DefaultTaoWeight>; + /// --- ITEM --> Global weight + pub type TaoWeight = StorageValue<_, u64, ValueQuery, DefaultTaoWeight>; #[pallet::storage] /// --- ITEM ( default_delegate_take ) pub type MaxDelegateTake = StorageValue<_, u16, ValueQuery, DefaultDelegateTake>; diff --git a/pallets/subtensor/src/migrations/migrate_rao.rs b/pallets/subtensor/src/migrations/migrate_rao.rs index 6b0afaa22..e8f53c324 100644 --- a/pallets/subtensor/src/migrations/migrate_rao.rs +++ b/pallets/subtensor/src/migrations/migrate_rao.rs @@ -55,6 +55,8 @@ pub fn migrate_rao() -> Weight { }); // Convert subnets and give them lock. + // Set global weight to 18% from the start + TaoWeight::::set(320_413_933_267_719_290); for netuid in netuids.iter().clone() { if *netuid == 0 { // Give root a single RAO in pool to avoid any catestrophic division by zero. @@ -73,8 +75,6 @@ pub fn migrate_rao() -> Weight { SubnetAlphaOut::::insert(netuid, 0); // Set zero subnet alpha out. SubnetMechanism::::insert(netuid, 1); // Convert to dynamic immediately with initialization. Tempo::::insert(netuid, DefaultTempo::::get()); - // Set global weight to 18% from the start - TaoWeight::::insert(netuid, 320_413_933_267_719_290); // Set the token symbol for this subnet using Self instead of Pallet:: TokenSymbol::::insert(netuid, Pallet::::get_symbol_for_subnet(*netuid)); diff --git a/pallets/subtensor/src/staking/stake_utils.rs b/pallets/subtensor/src/staking/stake_utils.rs index d1eb43213..88496fefe 100644 --- a/pallets/subtensor/src/staking/stake_utils.rs +++ b/pallets/subtensor/src/staking/stake_utils.rs @@ -54,9 +54,9 @@ impl Pallet { /// /// # Note /// This function uses saturating division to prevent potential overflow errors. - pub fn get_tao_weight(netuid: u16) -> I96F32 { + pub fn get_tao_weight() -> I96F32 { // Step 1: Fetch the global weight from storage - let stored_weight = TaoWeight::::get(netuid); + let stored_weight = TaoWeight::::get(); // Step 2: Convert the u64 weight to I96F32 let weight_fixed = I96F32::from_num(stored_weight); @@ -82,9 +82,9 @@ impl Pallet { /// # Note /// The weight is stored as a raw u64 value. To get the normalized weight between 0 and 1, /// use the `get_tao_weight()` function. - pub fn set_tao_weight(weight: u64, netuid: u16) { + pub fn set_tao_weight(weight: u64) { // Update the TaoWeight storage with the new weight value - TaoWeight::::insert(netuid, weight); + TaoWeight::::set(weight); } /// Calculates the weighted combination of alpha and global tao for hotkeys on a subnet. @@ -113,7 +113,7 @@ impl Pallet { // Step 5: Combine alpha and root tao stakes. // Retrieve the global global weight. - let tao_weight: I64F64 = I64F64::from_num(Self::get_tao_weight(netuid)); + let tao_weight: I64F64 = I64F64::from_num(Self::get_tao_weight()); // Calculate the weighted average of alpha and global tao stakes for each neuron. let total_stake: Vec = alpha_stake .iter() diff --git a/pallets/subtensor/src/subnets/subnet.rs b/pallets/subtensor/src/subnets/subnet.rs index c6095a80c..e08aa825c 100644 --- a/pallets/subtensor/src/subnets/subnet.rs +++ b/pallets/subtensor/src/subnets/subnet.rs @@ -337,9 +337,6 @@ impl Pallet { BurnRegistrationsThisInterval::::get(netuid), ); } - if !TaoWeight::::contains_key(netuid) { - TaoWeight::::insert(netuid, DefaultTaoWeight::::get()); - } } } diff --git a/pallets/subtensor/src/tests/weights.rs b/pallets/subtensor/src/tests/weights.rs index baa71ddd5..9b4adde6b 100644 --- a/pallets/subtensor/src/tests/weights.rs +++ b/pallets/subtensor/src/tests/weights.rs @@ -59,28 +59,6 @@ fn test_set_weights_dispatch_info_ok() { assert_eq!(dispatch_info.pays_fee, Pays::No); }); } -#[test] -fn test_set_rootweights_dispatch_info_ok() { - new_test_ext(0).execute_with(|| { - let dests = vec![1, 1]; - let weights = vec![1, 1]; - let netuid: u16 = 1; - let version_key: u64 = 0; - let hotkey: U256 = U256::from(1); // Add the hotkey field - let call = RuntimeCall::SubtensorModule(SubtensorCall::set_tao_weights { - netuid, - dests, - weights, - version_key, - hotkey, // Include the hotkey field - }); - let dispatch_info = call.get_dispatch_info(); - - assert_eq!(dispatch_info.class, DispatchClass::Normal); - assert_eq!(dispatch_info.pays_fee, Pays::No); - }); -} - #[test] fn test_set_rootweights_validate() { // Testing the signed extension validate function