Skip to content

Commit f2caf7d

Browse files
author
unconst
committed
fixes to the childkey emission
2 parents 9a7f491 + c28cac9 commit f2caf7d

File tree

4 files changed

+515
-15
lines changed

4 files changed

+515
-15
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,14 +338,17 @@ impl<T: Config> Pallet<T> {
338338
}
339339
log::debug!("alpha_dividends: {:?}", alpha_dividends);
340340
log::debug!("root_dividends: {:?}", root_dividends);
341+
log::debug!("total_root_divs: {:?}", total_root_divs);
341342

342343
// Compute root divs as TAO. Here we take
343344
let mut tao_dividends: BTreeMap<T::AccountId, I96F32> = BTreeMap::new();
344345
for (hotkey, root_divs) in root_dividends {
345346
// Root proportion.
346347
let root_share: I96F32 = root_divs.checked_div(total_root_divs).unwrap_or(zero);
348+
log::debug!("hotkey: {:?}, root_share: {:?}", hotkey, root_share);
347349
// Root proportion in TAO
348350
let root_tao: I96F32 = asfloat!(pending_tao).saturating_mul(root_share);
351+
log::debug!("hotkey: {:?}, root_tao: {:?}", hotkey, root_tao);
349352
// Record root dividends as TAO.
350353
tao_dividends
351354
.entry(hotkey)
@@ -583,10 +586,18 @@ impl<T: Config> Pallet<T> {
583586
(remaining_emission.saturating_mul(emission_factor)).saturating_to_num::<u64>();
584587

585588
// Add the parent's emission to the distribution list
586-
dividend_tuples.push((parent, parent_emission));
589+
dividend_tuples.push((parent.clone(), parent_emission));
587590

588591
// Keep track of total emission distributed to parents
589592
to_parents = to_parents.saturating_add(parent_emission);
593+
log::debug!(
594+
"Parent contribution for parent {:?} with contribution: {:?}, of total: {:?} of emission: {:?} gets: {:?}",
595+
parent,
596+
contribution,
597+
total_contribution,
598+
remaining_emission,
599+
parent_emission
600+
);
590601
}
591602
// Calculate the final emission for the hotkey itself.
592603
// This includes the take left from the parents and the self contribution.

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 102 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,16 @@ impl<T: Config> Pallet<T> {
128128
// Step 1: Get stake of hotkey (neuron)
129129
let alpha_stake =
130130
I64F64::saturating_from_num(Self::get_inherited_for_hotkey_on_subnet(hotkey, netuid));
131-
log::trace!("alpha_stake: {:?}", alpha_stake);
131+
log::debug!("alpha_stake: {:?}", alpha_stake);
132132

133133
// Step 2: Get the global tao stake for the hotkey
134134
let tao_stake =
135-
I64F64::saturating_from_num(Self::get_inherited_for_hotkey_on_subnet(hotkey, 0));
136-
log::trace!("tao_stake: {:?}", tao_stake);
135+
I64F64::saturating_from_num(Self::get_tao_inherited_for_hotkey_on_subnet(hotkey, netuid));
136+
log::debug!("tao_stake: {:?}", tao_stake);
137137

138138
// Step 3: Combine alpha and tao stakes
139139
let total_stake = alpha_stake.saturating_add(tao_stake.saturating_mul(tao_weight));
140-
log::trace!("total_stake: {:?}", total_stake);
140+
log::debug!("total_stake: {:?}", total_stake);
141141

142142
(total_stake, alpha_stake, tao_stake)
143143
}
@@ -173,8 +173,8 @@ impl<T: Config> Pallet<T> {
173173
.map(|uid| {
174174
if Keys::<T>::contains_key(netuid, uid) {
175175
let hotkey: T::AccountId = Keys::<T>::get(netuid, uid);
176-
I64F64::saturating_from_num(Self::get_inherited_for_hotkey_on_subnet(
177-
&hotkey, 0,
176+
I64F64::saturating_from_num(Self::get_tao_inherited_for_hotkey_on_subnet(
177+
&hotkey, netuid,
178178
))
179179
} else {
180180
I64F64::saturating_from_num(0)
@@ -222,6 +222,102 @@ impl<T: Config> Pallet<T> {
222222
///
223223
/// # Note
224224
/// This function uses saturating arithmetic to prevent overflows.
225+
pub fn get_tao_inherited_for_hotkey_on_subnet(hotkey: &T::AccountId, netuid: u16) -> u64 {
226+
227+
let initial_tao: I96F32 =
228+
I96F32::saturating_from_num(Self::get_stake_for_hotkey_on_subnet(hotkey, Self::get_root_netuid()));
229+
230+
// Initialize variables to track alpha allocated to children and inherited from parents.
231+
let mut tao_to_children: I96F32 = I96F32::saturating_from_num(0);
232+
let mut tao_from_parents: I96F32 = I96F32::saturating_from_num(0);
233+
234+
// Step 2: Retrieve the lists of parents and children for the hotkey on the subnet.
235+
let parents: Vec<(u64, T::AccountId)> = Self::get_parents(hotkey, netuid);
236+
let children: Vec<(u64, T::AccountId)> = Self::get_children(hotkey, netuid);
237+
log::trace!(
238+
"Parents for hotkey {:?} on subnet {}: {:?}",
239+
hotkey,
240+
netuid,
241+
parents
242+
);
243+
log::trace!(
244+
"Children for hotkey {:?} on subnet {}: {:?}",
245+
hotkey,
246+
netuid,
247+
children
248+
);
249+
250+
// Step 3: Calculate the total tao allocated to children.
251+
for (proportion, _) in children {
252+
// Convert the proportion to a normalized value between 0 and 1.
253+
let normalized_proportion: I96F32 = I96F32::saturating_from_num(proportion)
254+
.safe_div(I96F32::saturating_from_num(u64::MAX));
255+
log::trace!(
256+
"Normalized proportion for child: {:?}",
257+
normalized_proportion
258+
);
259+
260+
// Calculate the amount of tao to be allocated to this child.
261+
let tao_proportion_to_child: I96F32 =
262+
I96F32::saturating_from_num(initial_tao).saturating_mul(normalized_proportion);
263+
log::trace!("Tao proportion to child: {:?}", tao_proportion_to_child);
264+
265+
// Add this child's allocation to the total tao allocated to children.
266+
tao_to_children = tao_to_children.saturating_add(tao_proportion_to_child);
267+
}
268+
log::trace!("Total tao allocated to children: {:?}", tao_to_children);
269+
270+
// Step 4: Calculate the total tao inherited from parents.
271+
for (proportion, parent) in parents {
272+
// Retrieve the parent's total stake on this subnet.
273+
let parent_tao: I96F32 =
274+
I96F32::saturating_from_num(Self::get_stake_for_hotkey_on_subnet(&parent, Self::get_root_netuid()));
275+
log::trace!(
276+
"Parent tao for parent {:?} on subnet {}: {:?}",
277+
parent,
278+
netuid,
279+
parent_tao
280+
);
281+
282+
// Convert the proportion to a normalized value between 0 and 1.
283+
let normalized_proportion: I96F32 = I96F32::saturating_from_num(proportion)
284+
.safe_div(I96F32::saturating_from_num(u64::MAX));
285+
log::trace!(
286+
"Normalized proportion from parent: {:?}",
287+
normalized_proportion
288+
);
289+
290+
// Calculate the amount of tao to be inherited from this parent.
291+
let tao_proportion_from_parent: I96F32 =
292+
I96F32::saturating_from_num(parent_tao).saturating_mul(normalized_proportion);
293+
log::trace!(
294+
"Tao proportion from parent: {:?}",
295+
tao_proportion_from_parent
296+
);
297+
298+
// Add this parent's contribution to the total tao inherited from parents.
299+
tao_from_parents = tao_from_parents.saturating_add(tao_proportion_from_parent);
300+
}
301+
log::trace!(
302+
"Total tao inherited from parents: {:?}",
303+
tao_from_parents
304+
);
305+
306+
// Step 5: Calculate the final inherited tao for the hotkey.
307+
let finalized_tao: I96F32 = initial_tao
308+
.saturating_sub(tao_to_children) // Subtract tao allocated to children
309+
.saturating_add(tao_from_parents); // Add tao inherited from parents
310+
log::trace!(
311+
"Finalized tao for hotkey {:?} on subnet {}: {:?}",
312+
hotkey,
313+
netuid,
314+
finalized_tao
315+
);
316+
317+
// Step 6: Return the final inherited tao value.
318+
finalized_tao.saturating_to_num::<u64>()
319+
}
320+
225321
pub fn get_inherited_for_hotkey_on_subnet(hotkey: &T::AccountId, netuid: u16) -> u64 {
226322
// Step 1: Retrieve the initial total stake (alpha) for the hotkey on the specified subnet.
227323
let initial_alpha: I96F32 =

0 commit comments

Comments
 (0)