From d4196f82eed5971ac4c895352a08fb9e500bf299 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Tue, 29 Oct 2024 14:41:30 -0400 Subject: [PATCH] Fix double emission for childkey take --- .../subtensor/src/coinbase/run_coinbase.rs | 3 ++- pallets/subtensor/tests/emission.rs | 26 ++++++++++++++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/pallets/subtensor/src/coinbase/run_coinbase.rs b/pallets/subtensor/src/coinbase/run_coinbase.rs index 73d905c85..6080b3e3e 100644 --- a/pallets/subtensor/src/coinbase/run_coinbase.rs +++ b/pallets/subtensor/src/coinbase/run_coinbase.rs @@ -333,7 +333,8 @@ impl Pallet { // Calculate the final emission for the hotkey itself let final_hotkey_emission = validating_emission .to_num::() - .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)); diff --git a/pallets/subtensor/tests/emission.rs b/pallets/subtensor/tests/emission.rs index 514094496..766f5031d 100644 --- a/pallets/subtensor/tests/emission.rs +++ b/pallets/subtensor/tests/emission.rs @@ -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, @@ -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::(); - // 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 @@ -1104,7 +1113,7 @@ fn test_hotkey_take_calculation_scenario() { ParentKeys::::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, @@ -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::(); - assert!(hotkey_emission >= expected_take && hotkey_emission <= (expected_take + 1)); + assert!( + hotkey_emission + untouchable >= expected_take + && hotkey_emission <= (expected_take + 1) + ); } }); }