Skip to content

Commit

Permalink
Fix double emission for childkey take
Browse files Browse the repository at this point in the history
  • Loading branch information
gztensor committed Oct 29, 2024
1 parent b91aaf5 commit d4196f8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
3 changes: 2 additions & 1 deletion pallets/subtensor/src/coinbase/run_coinbase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ impl<T: Config> Pallet<T> {
// Calculate the final emission for the hotkey itself
let final_hotkey_emission = validating_emission
.to_num::<u64>()
.saturating_sub(to_parents);
.saturating_sub(to_parents)
.saturating_sub(total_childkey_take);

// Add the hotkey's own emission to the distribution list
hotkey_emission_tuples.push((hotkey.clone(), netuid, final_hotkey_emission));
Expand Down
26 changes: 19 additions & 7 deletions pallets/subtensor/tests/emission.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ fn test_basic_emission_distribution_scenario() {
);

let mut emission_tuples = Vec::new();
SubtensorModule::source_hotkey_emission(
let untouchable = SubtensorModule::source_hotkey_emission(
&hotkey,
netuid,
validating_emission,
Expand All @@ -1052,16 +1052,25 @@ fn test_basic_emission_distribution_scenario() {

// We are only distributing validating emission among hotkeys, but mining emission stays with the miner
assert_eq!(emission_tuples.len(), 3);
let total_distributed: u64 = emission_tuples.iter().map(|(_, _, amount)| amount).sum();
assert_eq!(total_distributed, validating_emission);
let total_distributed: u64 = emission_tuples
.iter()
.map(|(_, _, amount)| amount)
.sum::<u64>();

// Check hotkey take and mining emission
// untouchable is mining emission (500) + childkey take (25%)
assert_eq!(
total_distributed + untouchable,
validating_emission + mining_emission
);

// Hotkey child take and mining emission are in untouchable
let hotkey_emission = emission_tuples
.iter()
.find(|(h, _, _)| h == &hotkey)
.map(|(_, _, amount)| amount)
.unwrap();
assert!(hotkey_emission > &0);
assert!(hotkey_emission == &0);
assert!(untouchable > 0);

// Check parent distributions
let parent1_emission = emission_tuples
Expand Down Expand Up @@ -1104,7 +1113,7 @@ fn test_hotkey_take_calculation_scenario() {
ParentKeys::<Test>::insert(hotkey, netuid, vec![(u64::MAX, parent)]);

let mut emission_tuples = Vec::new();
SubtensorModule::source_hotkey_emission(
let untouchable = SubtensorModule::source_hotkey_emission(
&hotkey,
netuid,
validating_emission,
Expand All @@ -1122,7 +1131,10 @@ fn test_hotkey_take_calculation_scenario() {
let max_delegation_fixed = I96F32::from_num(65535u16);
let expected_take =
(emission_fixed * delegation_fixed / max_delegation_fixed).to_num::<u64>();
assert!(hotkey_emission >= expected_take && hotkey_emission <= (expected_take + 1));
assert!(
hotkey_emission + untouchable >= expected_take
&& hotkey_emission <= (expected_take + 1)
);
}
});
}
Expand Down

0 comments on commit d4196f8

Please sign in to comment.