Skip to content

Commit 1e9a0e2

Browse files
committed
WIP: test_parent_child_chain_emission
1 parent deffa1a commit 1e9a0e2

File tree

2 files changed

+229
-130
lines changed

2 files changed

+229
-130
lines changed

pallets/subtensor/src/coinbase/run_coinbase.rs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl<T: Config> Pallet<T> {
310310

311311
// 6.3.2: Get dividend tuples for parents and self based on childkey relationships and child-take.
312312
let dividend_tuples: Vec<(T::AccountId, u64)> =
313-
Self::get_parent_dividends(&hotkey, netuid, dividends);
313+
Self::get_dividends_distribution(&hotkey, netuid, dividends);
314314
log::debug!(
315315
"Dividend tuples for hotkey {:?} on netuid {:?}: {:?}",
316316
hotkey,
@@ -460,9 +460,43 @@ impl<T: Config> Pallet<T> {
460460
}
461461
}
462462

463+
pub fn get_self_contribution(hotkey: &T::AccountId, netuid: u16) -> u64 {
464+
// Get all childkeys for this hotkey.
465+
let childkeys = Self::get_children(hotkey, netuid);
466+
let mut remaining_proportion: I96F32 = I96F32::from_num(1.0);
467+
for (proportion, _) in childkeys {
468+
remaining_proportion = remaining_proportion.saturating_sub(
469+
I96F32::from_num(proportion) // Normalize
470+
.saturating_div(I96F32::from_num(u64::MAX)),
471+
);
472+
}
473+
474+
// Get TAO weight
475+
let tao_weight: I96F32 = Self::get_tao_weight();
476+
477+
// Get the hotkey's stake including weight
478+
let root_stake: I96F32 = I96F32::from_num(Self::get_stake_for_hotkey_on_subnet(
479+
&hotkey,
480+
Self::get_root_netuid(),
481+
));
482+
let alpha_stake: I96F32 =
483+
I96F32::from_num(Self::get_stake_for_hotkey_on_subnet(&hotkey, netuid));
484+
485+
// Calculate the
486+
let alpha_contribution: I96F32 = alpha_stake.saturating_mul(remaining_proportion);
487+
let root_contribution: I96F32 = root_stake
488+
.saturating_mul(remaining_proportion)
489+
.saturating_mul(tao_weight);
490+
let combined_contribution: I96F32 = alpha_contribution.saturating_add(root_contribution);
491+
492+
// Return the combined contribution as a u64
493+
combined_contribution.to_num::<u64>()
494+
}
495+
463496
/// Returns a list of tuples for each parent associated with this hotkey including self
464497
/// Each tuples contains the dividends owed to that hotkey given their parent proportion
465498
/// The hotkey child take proportion is removed from this and added to the tuples for self.
499+
/// The hotkey also gets a portion based on its own stake contribution, this is added to the childkey take.
466500
///
467501
/// # Arguments
468502
/// * `hotkye` - The hotkey to distribute out from.
@@ -472,7 +506,7 @@ impl<T: Config> Pallet<T> {
472506
/// # Returns
473507
/// * dividend_tuples: `Vec<(T::AccountId, u64)>` - Vector of (hotkey, divs) for each parent including self.
474508
///
475-
pub fn get_parent_dividends(
509+
pub fn get_dividends_distribution(
476510
hotkey: &T::AccountId,
477511
netuid: u16,
478512
dividends: u64,
@@ -485,19 +519,29 @@ impl<T: Config> Pallet<T> {
485519
let childkey_take_proportion: I96F32 =
486520
I96F32::from_num(Self::get_childkey_take(hotkey, netuid))
487521
.saturating_div(I96F32::from_num(u16::MAX));
488-
let mut total_childkey_take: u64 = 0;
489522
// NOTE: Only the validation emission should be split amongst parents.
490523

491524
// Initialize variables to track emission distribution
492525
let mut to_parents: u64 = 0;
493526

494527
// Initialize variables to calculate total stakes from parents
495528
let mut total_contribution: I96F32 = I96F32::from_num(0);
496-
let mut contributions: Vec<(T::AccountId, I96F32)> = Vec::new();
529+
let mut parent_contributions: Vec<(T::AccountId, I96F32)> = Vec::new();
497530

498531
// Get the weights for root and alpha stakes in emission distribution
499532
let tao_weight: I96F32 = Self::get_tao_weight();
500533

534+
// Get self contribution, removing any childkey proportions.
535+
let self_contribution = Self::get_self_contribution(hotkey, netuid);
536+
log::debug!(
537+
"Self contribution for hotkey {:?} on netuid {:?}: {:?}",
538+
hotkey,
539+
netuid,
540+
self_contribution
541+
);
542+
// Add self contribution to total contribution but not to the parent contributions.
543+
total_contribution = total_contribution.saturating_add(I96F32::from_num(self_contribution));
544+
501545
// Calculate total root and alpha (subnet-specific) stakes from all parents
502546
for (proportion, parent) in Self::get_parents(hotkey, netuid) {
503547
// Convert the parent's stake proportion to a fractional value
@@ -523,11 +567,12 @@ impl<T: Config> Pallet<T> {
523567
// Add to the total stakes
524568
total_contribution = total_contribution.saturating_add(combined_contribution);
525569
// Store the parent's contributions for later use
526-
contributions.push((parent.clone(), combined_contribution));
570+
parent_contributions.push((parent.clone(), combined_contribution));
527571
}
528572

529-
// Distribute emission to parents based on their contributions
530-
for (parent, contribution) in contributions {
573+
// Distribute emission to parents based on their contributions.
574+
// Deduct childkey take from parent contribution.
575+
for (parent, contribution) in parent_contributions {
531576
// Sum up the total emission for this parent
532577
let emission_factor: I96F32 = contribution
533578
.checked_div(total_contribution)
@@ -539,7 +584,6 @@ impl<T: Config> Pallet<T> {
539584
let child_emission_take: u64 = childkey_take_proportion
540585
.saturating_mul(I96F32::from_num(total_emission))
541586
.to_num::<u64>();
542-
total_childkey_take = total_childkey_take.saturating_add(child_emission_take);
543587
let parent_total_emission = total_emission.saturating_sub(child_emission_take);
544588

545589
// Add the parent's emission to the distribution list
@@ -548,7 +592,8 @@ impl<T: Config> Pallet<T> {
548592
// Keep track of total emission distributed to parents
549593
to_parents = to_parents.saturating_add(parent_total_emission);
550594
}
551-
// Calculate the final emission for the hotkey itself
595+
// Calculate the final emission for the hotkey itself.
596+
// This includes the take left from the parents and the self contribution.
552597
let final_hotkey_emission = validating_emission
553598
.to_num::<u64>()
554599
.saturating_sub(to_parents);

0 commit comments

Comments
 (0)