Skip to content

Commit 61e74ac

Browse files
authored
Merge pull request #1257 from opentensor/nonclaim
Nonclaim
2 parents bf6896f + f0ee331 commit 61e74ac

File tree

3 files changed

+611
-11
lines changed

3 files changed

+611
-11
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 23 additions & 4 deletions
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)
@@ -358,6 +361,12 @@ impl<T: Config> Pallet<T> {
358361
if let Ok(owner_coldkey) = SubnetOwner::<T>::try_get(netuid) {
359362
if let Ok(owner_hotkey) = SubnetOwnerHotkey::<T>::try_get(netuid) {
360363
// Increase stake for owner hotkey and coldkey.
364+
log::debug!(
365+
"owner_hotkey: {:?} owner_coldkey: {:?}, owner_cut: {:?}",
366+
owner_hotkey,
367+
owner_coldkey,
368+
owner_cut
369+
);
361370
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
362371
&owner_hotkey,
363372
&owner_coldkey,
@@ -370,6 +379,7 @@ impl<T: Config> Pallet<T> {
370379
// Distribute mining incentives.
371380
for (hotkey, incentive) in incentives {
372381
// Increase stake for miner.
382+
log::debug!("incentives: hotkey: {:?}", incentive);
373383
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
374384
&hotkey.clone(),
375385
&Owner::<T>::get(hotkey.clone()),
@@ -381,21 +391,21 @@ impl<T: Config> Pallet<T> {
381391
// Distribute alpha divs.
382392
let _ = AlphaDividendsPerSubnet::<T>::clear_prefix(netuid, u32::MAX, None);
383393
for (hotkey, mut alpha_divs) in alpha_dividends {
384-
log::debug!("hotkey: {:?} alpha_divs: {:?}", hotkey, alpha_divs);
385-
386394
// Get take prop
387395
let alpha_take: I96F32 =
388396
Self::get_hotkey_take_float(&hotkey).saturating_mul(alpha_divs);
389397
// Remove take prop from alpha_divs
390398
alpha_divs = alpha_divs.saturating_sub(alpha_take);
391399
// Give the validator their take.
400+
log::debug!("hotkey: {:?} alpha_take: {:?}", hotkey, alpha_take);
392401
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
393402
&hotkey,
394403
&Owner::<T>::get(hotkey.clone()),
395404
netuid,
396405
tou64!(alpha_take),
397406
);
398407
// Give all other nominators.
408+
log::debug!("hotkey: {:?} alpha_divs: {:?}", hotkey, alpha_divs);
399409
Self::increase_stake_for_hotkey_on_subnet(&hotkey.clone(), netuid, tou64!(alpha_divs));
400410
// Record dividends for this hotkey.
401411
AlphaDividendsPerSubnet::<T>::mutate(netuid, hotkey.clone(), |divs| {
@@ -406,19 +416,20 @@ impl<T: Config> Pallet<T> {
406416
// Distribute root tao divs.
407417
let _ = TaoDividendsPerSubnet::<T>::clear_prefix(netuid, u32::MAX, None);
408418
for (hotkey, mut root_tao) in tao_dividends {
409-
log::debug!("hotkey: {:?} root_tao: {:?}", hotkey, root_tao);
410419
// Get take prop
411420
let tao_take: I96F32 = Self::get_hotkey_take_float(&hotkey).saturating_mul(root_tao);
412421
// Remove take prop from root_tao
413422
root_tao = root_tao.saturating_sub(tao_take);
414423
// Give the validator their take.
424+
log::debug!("hotkey: {:?} tao_take: {:?}", hotkey, tao_take);
415425
Self::increase_stake_for_hotkey_and_coldkey_on_subnet(
416426
&hotkey,
417427
&Owner::<T>::get(hotkey.clone()),
418428
Self::get_root_netuid(),
419429
tou64!(tao_take),
420430
);
421431
// Give rest to nominators.
432+
log::debug!("hotkey: {:?} root_tao: {:?}", hotkey, root_tao);
422433
Self::increase_stake_for_hotkey_on_subnet(
423434
&hotkey,
424435
Self::get_root_netuid(),
@@ -580,10 +591,18 @@ impl<T: Config> Pallet<T> {
580591
(remaining_emission.saturating_mul(emission_factor)).saturating_to_num::<u64>();
581592

582593
// Add the parent's emission to the distribution list
583-
dividend_tuples.push((parent, parent_emission));
594+
dividend_tuples.push((parent.clone(), parent_emission));
584595

585596
// Keep track of total emission distributed to parents
586597
to_parents = to_parents.saturating_add(parent_emission);
598+
log::debug!(
599+
"Parent contribution for parent {:?} with contribution: {:?}, of total: {:?} of emission: {:?} gets: {:?}",
600+
parent,
601+
contribution,
602+
total_contribution,
603+
remaining_emission,
604+
parent_emission
605+
);
587606
}
588607
// Calculate the final emission for the hotkey itself.
589608
// This includes the take left from the parents and the self contribution.

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,17 @@ 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
134-
let tao_stake =
135-
I64F64::saturating_from_num(Self::get_inherited_for_hotkey_on_subnet(hotkey, 0));
136-
log::trace!("tao_stake: {:?}", tao_stake);
134+
let tao_stake = I64F64::saturating_from_num(Self::get_tao_inherited_for_hotkey_on_subnet(
135+
hotkey, netuid,
136+
));
137+
log::debug!("tao_stake: {:?}", tao_stake);
137138

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

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

0 commit comments

Comments
 (0)