Skip to content

Commit 0d9be95

Browse files
author
unconst
committed
mechanism weight change
1 parent c542916 commit 0d9be95

File tree

4 files changed

+24
-13
lines changed

4 files changed

+24
-13
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,22 +46,26 @@ impl<T: Config> Pallet<T> {
4646
let subnets: Vec<u16> = Self::get_all_subnet_netuids();
4747
log::debug!("All subnet netuids: {:?}", subnets);
4848

49-
// --- 2. Sum all the SubnetTAO associated with the same mechanism.
50-
// Mechanisms get emission based on the proportion of TAO across all their subnets
51-
let mut total_active_tao: I96F32 = I96F32::from_num(0);
52-
let mut mechanism_tao: BTreeMap<u16, I96F32> = BTreeMap::new();
49+
/// Calculates the total active weight and mechanism weights for emission distribution.
50+
/// For each subnet (except root), sums the SubnetTAO and a portion of the total stake at dynamic registration
51+
/// into mechanism-specific weights. The mechanism weights determine emission proportions.
52+
let mut total_active_weight: I96F32 = I96F32::from_num(0);
53+
let mut mechanism_weight: BTreeMap<u16, I96F32> = BTreeMap::new();
5354
for netuid in subnets.iter() {
5455
if *netuid == 0 {
5556
continue;
5657
} // Skip root network
5758
let mechid = SubnetMechanism::<T>::get(*netuid);
5859
let subnet_tao = I96F32::from_num(SubnetTAO::<T>::get(*netuid));
59-
let new_subnet_tao = subnet_tao
60-
.saturating_add(*mechanism_tao.entry(mechid).or_insert(I96F32::from_num(0)));
61-
*mechanism_tao.entry(mechid).or_insert(I96F32::from_num(0)) = new_subnet_tao;
62-
total_active_tao = total_active_tao.saturating_add(subnet_tao);
60+
let tao_at_dynamic = I96F32::from_num( TotalStakeAtDynamic::<T>::get(*netuid) );
61+
let num_subnets = I96F32::from_num( Self::get_num_subnets() as u64 );
62+
let subnet_weight = subnet_tao.saturating_add( tao_at_dynamic.checked_div( num_subnets ).unwrap_or(I96F32::from_num( 0.0)) );
63+
let new_subnet_weight = subnet_weight
64+
.saturating_add(*mechanism_weight.entry(mechid).or_insert(I96F32::from_num(0)));
65+
*mechanism_weight.entry(mechid).or_insert(I96F32::from_num(0)) = new_subnet_weight;
66+
total_active_weight = total_active_weight.saturating_add(subnet_weight);
6367
}
64-
log::debug!("Mechanism TAO sums: {:?}", mechanism_tao);
68+
log::debug!("Mechanism TAO sums: {:?}", mechanism_weight);
6569

6670
// --- 3. Compute subnet emission values (amount of tao inflation this block).
6771
let mut tao_in_map: BTreeMap<u16, u64> = BTreeMap::new();
@@ -77,15 +81,15 @@ impl<T: Config> Pallet<T> {
7781
let subnet_tao: I96F32 = I96F32::from_num(SubnetTAO::<T>::get(*netuid));
7882
log::debug!("Subnet TAO (T_s) for netuid {:?}: {:?}", netuid, subnet_tao);
7983
// 3.3: Get the denominator as the sum of all TAO associated with a specific mechanism (T_m)
80-
let mech_tao: I96F32 = *mechanism_tao.get(&mechid).unwrap_or(&I96F32::from_num(0));
84+
let mech_tao: I96F32 = *mechanism_weight.get(&mechid).unwrap_or(&I96F32::from_num(0));
8185
log::debug!(
8286
"Mechanism TAO (T_m) for mechanism ID {:?}: {:?}",
8387
mechid,
8488
mech_tao
8589
);
8690
// 3.4: Compute the mechanism emission proportion: P_m = T_m / T_total
8791
let mech_proportion: I96F32 = mech_tao
88-
.checked_div(total_active_tao)
92+
.checked_div(total_active_weight)
8993
.unwrap_or(I96F32::from_num(0));
9094
log::debug!(
9195
"Mechanism proportion (P_m) for mechanism ID {:?}: {:?}",

pallets/subtensor/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,9 @@ pub mod pallet {
903903
#[pallet::storage] // --- DMAP ( netuid ) --> alpha_sell_per_block | Alpha sold per block.
904904
pub type SubnetAlphaEmissionSell<T: Config> =
905905
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultZeroU64<T>>;
906+
#[pallet::storage] // --- DMAP ( netuid ) --> total_stake_at_moment_of_subnet_registration
907+
pub type TotalStakeAtDynamic<T: Config> =
908+
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultZeroU64<T>>;
906909
#[pallet::storage] // --- DMAP ( netuid ) --> alpha_supply_in_pool | Returns the amount of alpha in the subnet.
907910
pub type SubnetAlphaIn<T: Config> =
908911
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultZeroU64<T>>;
@@ -972,7 +975,7 @@ pub mod pallet {
972975
#[pallet::storage] // --- DMAP ( netuid ) --> subnet_name | Returns the name of the subnet.
973976
pub type SubnetName<T: Config> =
974977
StorageMap<_, Identity, u16, Vec<u8>, ValueQuery, DefaultUnicodeVecU8<T>>;
975-
978+
976979
/// ============================
977980
/// ==== Global Parameters =====
978981
/// ============================

pallets/subtensor/src/migrations/migrate_rao.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ pub fn migrate_rao<T: Config>() -> Weight {
8181
Tempo::<T>::insert(netuid, DefaultTempo::<T>::get());
8282
// Set the token symbol for this subnet using Self instead of Pallet::<T>
8383
TokenSymbol::<T>::insert(netuid, Pallet::<T>::get_symbol_for_subnet(*netuid));
84+
SubnetTAO::<T>::insert(netuid, initial_liquidity); // Set TAO to the lock.
85+
TotalStakeAtDynamic::<T>::insert(netuid, 0);
86+
8487

8588
if let Ok(owner_coldkey) = SubnetOwner::<T>::try_get(netuid) {
8689
// Set Owner as the coldkey.

pallets/subtensor/src/subnets/subnet.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ impl<T: Config> Pallet<T> {
247247
SubnetAlphaIn::<T>::insert(netuid_to_register, pool_initial_tao);
248248
SubnetOwner::<T>::insert(netuid_to_register, coldkey.clone());
249249
SubnetOwnerHotkey::<T>::insert(netuid_to_register, hotkey.clone());
250-
250+
TotalStakeAtDynamic::<T>::insert(netuid_to_register, TotalStake::<T>::get() );
251+
251252
if actual_tao_lock_amount_less_pool_tao > 0 {
252253
Self::burn_tokens(actual_tao_lock_amount_less_pool_tao);
253254
}

0 commit comments

Comments
 (0)