Skip to content

Commit

Permalink
make tao weight global
Browse files Browse the repository at this point in the history
  • Loading branch information
unconst committed Jan 7, 2025
1 parent 32b64f3 commit 125d8a6
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 37 deletions.
6 changes: 3 additions & 3 deletions pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<T: Config> Pallet<T> {
// 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.
Expand Down Expand Up @@ -293,7 +293,7 @@ impl<T: Config> Pallet<T> {

// 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);

Expand Down Expand Up @@ -381,7 +381,7 @@ impl<T: Config> Pallet<T> {
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) {
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = StorageMap<_, Identity, u16, u64, ValueQuery, DefaultTaoWeight<T>>;
/// --- ITEM --> Global weight
pub type TaoWeight<T> = StorageValue<_, u64, ValueQuery, DefaultTaoWeight<T>>;
#[pallet::storage]
/// --- ITEM ( default_delegate_take )
pub type MaxDelegateTake<T> = StorageValue<_, u16, ValueQuery, DefaultDelegateTake<T>>;
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/migrations/migrate_rao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ pub fn migrate_rao<T: Config>() -> Weight {
});

// Convert subnets and give them lock.
// Set global weight to 18% from the start
TaoWeight::<T>::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.
Expand All @@ -73,8 +75,6 @@ pub fn migrate_rao<T: Config>() -> Weight {
SubnetAlphaOut::<T>::insert(netuid, 0); // Set zero subnet alpha out.
SubnetMechanism::<T>::insert(netuid, 1); // Convert to dynamic immediately with initialization.
Tempo::<T>::insert(netuid, DefaultTempo::<T>::get());
// Set global weight to 18% from the start
TaoWeight::<T>::insert(netuid, 320_413_933_267_719_290);
// Set the token symbol for this subnet using Self instead of Pallet::<T>
TokenSymbol::<T>::insert(netuid, Pallet::<T>::get_symbol_for_subnet(*netuid));

Expand Down
10 changes: 5 additions & 5 deletions pallets/subtensor/src/staking/stake_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ impl<T: Config> Pallet<T> {
///
/// # 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::<T>::get(netuid);
let stored_weight = TaoWeight::<T>::get();

// Step 2: Convert the u64 weight to I96F32
let weight_fixed = I96F32::from_num(stored_weight);
Expand All @@ -82,9 +82,9 @@ impl<T: Config> Pallet<T> {
/// # 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::<T>::insert(netuid, weight);
TaoWeight::<T>::set(weight);
}

/// Calculates the weighted combination of alpha and global tao for hotkeys on a subnet.
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<T: Config> Pallet<T> {

// 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<I64F64> = alpha_stake
.iter()
Expand Down
3 changes: 0 additions & 3 deletions pallets/subtensor/src/subnets/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,6 @@ impl<T: Config> Pallet<T> {
BurnRegistrationsThisInterval::<T>::get(netuid),
);
}
if !TaoWeight::<T>::contains_key(netuid) {
TaoWeight::<T>::insert(netuid, DefaultTaoWeight::<T>::get());
}
}
}

Expand Down
22 changes: 0 additions & 22 deletions pallets/subtensor/src/tests/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 125d8a6

Please sign in to comment.