From 3efe80b83ec2b673983e339d1fdcd33376ef3466 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 10 Jan 2025 12:22:15 -0500 Subject: [PATCH 1/5] bump CI From d1196addfc593501d11c26968972c9db3b812bc1 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 10 Jan 2025 12:28:09 -0500 Subject: [PATCH 2/5] bump ci again From 6a47916b46e26648708a9cd3fe4336ea54451b1e Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 10 Jan 2025 12:47:40 -0500 Subject: [PATCH 3/5] sam fix test_childkey_take_drain --- pallets/subtensor/src/tests/children.rs | 240 +++++++++++------------- 1 file changed, 111 insertions(+), 129 deletions(-) diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index 747a1bd2d2..2be6bc5a8e 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -2823,135 +2823,117 @@ fn test_set_weights_no_parent() { #[test] fn test_childkey_take_drain() { new_test_ext(1).execute_with(|| { - assert!(false); - - // let coldkey = U256::from(1); - // let parent = U256::from(2); - // let child = U256::from(3); - // let nominator = U256::from(4); - // let netuid: u16 = 1; - // let root_id: u16 = 0; - // let subnet_tempo = 10; - // let hotkey_tempo = 20; - // let stake = 100_000_000_000; - // let proportion: u64 = u64::MAX; - - // // Add network, register hotkeys, and setup network parameters - // add_network(root_id, subnet_tempo, 0); - // add_network(netuid, subnet_tempo, 0); - // register_ok_neuron(netuid, child, coldkey, 0); - // register_ok_neuron(netuid, parent, coldkey, 1); - - // // Set children - // mock_set_children(&coldkey, &parent, netuid, &[(proportion, child)]); - - // SubtensorModule::add_balance_to_coldkey_account( - // &coldkey, - // stake + ExistentialDeposit::get(), - // ); - // SubtensorModule::add_balance_to_coldkey_account( - // &nominator, - // stake + ExistentialDeposit::get(), - // ); - // SubtensorModule::set_hotkey_emission_tempo(hotkey_tempo); - // SubtensorModule::set_weights_set_rate_limit(netuid, 0); - // SubtensorModule::set_max_allowed_validators(netuid, 2); - // step_block(subnet_tempo); - // crate::SubnetOwnerCut::::set(0); - - // // Set 20% childkey take - // let max_take: u16 = 0xFFFF / 5; - // SubtensorModule::set_max_childkey_take(max_take); - // assert_ok!(SubtensorModule::set_childkey_take( - // RuntimeOrigin::signed(coldkey), - // child, - // netuid, - // max_take - // )); - - // // Set zero hotkey take for childkey - // SubtensorModule::set_min_delegate_take(0); - // assert_ok!(SubtensorModule::do_become_delegate( - // RuntimeOrigin::signed(coldkey), - // child, - // 0 - // )); - - // // Set zero hotkey take for parent - // assert_ok!(SubtensorModule::do_become_delegate( - // RuntimeOrigin::signed(coldkey), - // parent, - // 0 - // )); - - // // Setup stakes: - // // Stake from parent - // // Stake from nominator to childkey - // // Give 100% of parent stake to childkey - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(coldkey), - // parent, - // stake - // )); - // assert_ok!(SubtensorModule::add_stake( - // RuntimeOrigin::signed(nominator), - // child, - // stake - // )); - - // // Setup YUMA so that it creates emissions: - // // Parent and child both set weights - // // Parent and child register on root and - // // Set root weights - // crate::Weights::::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]); - // crate::Weights::::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]); - // assert_ok!(SubtensorModule::do_root_register( - // RuntimeOrigin::signed(coldkey), - // parent, - // )); - // assert_ok!(SubtensorModule::do_root_register( - // RuntimeOrigin::signed(coldkey), - // child, - // )); - // crate::Weights::::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]); - // crate::Weights::::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]); - - // // Run run_coinbase until PendingHotkeyEmission are populated - // while crate::PendingdHotkeyEmission::::get(child) == 0 { - // step_block(1); - // } - - // // Prevent further subnet epochs - // crate::Tempo::::set(netuid, u16::MAX); - // crate::Tempo::::set(root_id, u16::MAX); - - // // Run run_coinbase until PendingHotkeyEmission is drained for both child and parent - // step_block((hotkey_tempo * 2) as u16); - - // // Verify how emission is split between keys - // // - Child stake increased by its child key take only (20% * 50% = 10% of total emission) - // // - Parent stake increased by 40% of total emission - // // - Nominator stake increased by 50% of total emission - // let child_emission = crate::Stake::::get(child, coldkey); - // let parent_emission = crate::Stake::::get(parent, coldkey) - stake; - // let nominator_emission = crate::Stake::::get(child, nominator) - stake; - // let total_emission = child_emission + parent_emission + nominator_emission; - - // assert!(is_within_tolerance( - // child_emission, - // total_emission / 10, - // 500 - // )); - // assert!(is_within_tolerance( - // parent_emission, - // total_emission / 10 * 4, - // 500 - // )); - // assert!(is_within_tolerance( - // nominator_emission, - // total_emission / 2, - // 500 - // )); + let coldkey = U256::from(1); + let parent = U256::from(2); + let child = U256::from(3); + let nominator = U256::from(4); + let netuid: u16 = 1; + let root_id: u16 = 0; + let subnet_tempo = 10; + let hotkey_tempo = 20; + let stake = 100_000_000_000; + let proportion: u64 = u64::MAX; + + // Add network, register hotkeys, and setup network parameters + add_network(root_id, subnet_tempo, 0); + add_network(netuid, subnet_tempo, 0); + register_ok_neuron(netuid, child, coldkey, 0); + register_ok_neuron(netuid, parent, coldkey, 1); + + // Set children + mock_set_children(&coldkey, &parent, netuid, &[(proportion, child)]); + + SubtensorModule::add_balance_to_coldkey_account( + &coldkey, + stake + ExistentialDeposit::get(), + ); + SubtensorModule::add_balance_to_coldkey_account( + &nominator, + stake + ExistentialDeposit::get(), + ); + SubtensorModule::set_weights_set_rate_limit(netuid, 0); + SubtensorModule::set_max_allowed_validators(netuid, 2); + step_block(subnet_tempo); + crate::SubnetOwnerCut::::set(0); + + // Set 20% childkey take + let max_take: u16 = 0xFFFF / 5; + SubtensorModule::set_max_childkey_take(max_take); + assert_ok!(SubtensorModule::set_childkey_take( + RuntimeOrigin::signed(coldkey), + child, + netuid, + max_take + )); + + // Set zero hotkey take for childkey + SubtensorModule::set_min_delegate_take(0); + + // Setup stakes: + // Stake from parent + // Stake from nominator to childkey + // Give 100% of parent stake to childkey + assert_ok!(SubtensorModule::add_stake( + RuntimeOrigin::signed(coldkey), + parent, + netuid, + stake + )); + assert_ok!(SubtensorModule::add_stake( + RuntimeOrigin::signed(nominator), + child, + netuid, + stake + )); + + // Setup YUMA so that it creates emissions: + // Parent and child both set weights + // Parent and child register on root and + // Set root weights + crate::Weights::::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]); + crate::Weights::::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]); + assert_ok!(SubtensorModule::do_root_register( + RuntimeOrigin::signed(coldkey), + parent, + )); + assert_ok!(SubtensorModule::do_root_register( + RuntimeOrigin::signed(coldkey), + child, + )); + crate::Weights::::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]); + crate::Weights::::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]); + + // Prevent further subnet epochs + crate::Tempo::::set(netuid, u16::MAX); + crate::Tempo::::set(root_id, u16::MAX); + + // Run run_coinbase until PendingHotkeyEmission is drained for both child and parent + step_block((hotkey_tempo * 2) as u16); + + // Verify how emission is split between keys + // - Child stake increased by its child key take only (20% * 50% = 10% of total emission) + // - Parent stake increased by 40% of total emission + // - Nominator stake increased by 50% of total emission + let child_emission = crate::Stake::::get(child, coldkey); + let parent_emission = crate::Stake::::get(parent, coldkey).saturating_sub(stake); + let nominator_emission = crate::Stake::::get(child, nominator).saturating_sub(stake); + let total_emission = child_emission + parent_emission + nominator_emission; + + assert!(is_within_tolerance( + child_emission, + total_emission / 10, + 500 + )); + assert!(is_within_tolerance( + parent_emission, + total_emission / 10 * 4, + 500 + )); + assert!(is_within_tolerance( + nominator_emission, + total_emission / 2, + 500 + )); }); } From cba54fec55250eb834de712ef9cb487ea9ed42e2 Mon Sep 17 00:00:00 2001 From: Greg Zaitsev Date: Fri, 10 Jan 2025 13:06:25 -0500 Subject: [PATCH 4/5] Use dynamic network in test_childkey_take_drain, remove is_within_tolerance --- pallets/subtensor/src/tests/children.rs | 46 ++++++------------------- pallets/subtensor/src/tests/mock.rs | 11 ------ 2 files changed, 10 insertions(+), 47 deletions(-) diff --git a/pallets/subtensor/src/tests/children.rs b/pallets/subtensor/src/tests/children.rs index 2be6bc5a8e..7342e3c730 100644 --- a/pallets/subtensor/src/tests/children.rs +++ b/pallets/subtensor/src/tests/children.rs @@ -2823,20 +2823,21 @@ fn test_set_weights_no_parent() { #[test] fn test_childkey_take_drain() { new_test_ext(1).execute_with(|| { + let subnet_owner_coldkey = U256::from(1001); + let subnet_owner_hotkey = U256::from(1002); let coldkey = U256::from(1); let parent = U256::from(2); let child = U256::from(3); let nominator = U256::from(4); - let netuid: u16 = 1; let root_id: u16 = 0; let subnet_tempo = 10; - let hotkey_tempo = 20; let stake = 100_000_000_000; let proportion: u64 = u64::MAX; // Add network, register hotkeys, and setup network parameters add_network(root_id, subnet_tempo, 0); - add_network(netuid, subnet_tempo, 0); + let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey); + crate::Tempo::::set(netuid, subnet_tempo); register_ok_neuron(netuid, child, coldkey, 0); register_ok_neuron(netuid, parent, coldkey, 1); @@ -2903,13 +2904,6 @@ fn test_childkey_take_drain() { crate::Weights::::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]); crate::Weights::::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]); - // Prevent further subnet epochs - crate::Tempo::::set(netuid, u16::MAX); - crate::Tempo::::set(root_id, u16::MAX); - - // Run run_coinbase until PendingHotkeyEmission is drained for both child and parent - step_block((hotkey_tempo * 2) as u16); - // Verify how emission is split between keys // - Child stake increased by its child key take only (20% * 50% = 10% of total emission) // - Parent stake increased by 40% of total emission @@ -2919,21 +2913,9 @@ fn test_childkey_take_drain() { let nominator_emission = crate::Stake::::get(child, nominator).saturating_sub(stake); let total_emission = child_emission + parent_emission + nominator_emission; - assert!(is_within_tolerance( - child_emission, - total_emission / 10, - 500 - )); - assert!(is_within_tolerance( - parent_emission, - total_emission / 10 * 4, - 500 - )); - assert!(is_within_tolerance( - nominator_emission, - total_emission / 2, - 500 - )); + assert_abs_diff_eq!(child_emission, total_emission / 10, epsilon = 500); + assert_abs_diff_eq!(parent_emission, total_emission / 10 * 4, epsilon = 500); + assert_abs_diff_eq!(nominator_emission, total_emission / 2, epsilon = 500); }); } @@ -3037,17 +3019,9 @@ fn test_childkey_take_drain_validator_take() { let nominator_emission = crate::Stake::::get(child, nominator).saturating_sub(stake); let total_emission = child_emission + parent_emission + nominator_emission; - assert!(is_within_tolerance(child_emission, total_emission / 5, 500)); - assert!(is_within_tolerance( - parent_emission, - total_emission / 10 * 4, - 500 - )); - assert!(is_within_tolerance( - nominator_emission, - total_emission / 10 * 4, - 500 - )); + assert_abs_diff_eq!(child_emission, total_emission / 5, epsilon = 500); + assert_abs_diff_eq!(parent_emission, total_emission / 10 * 4, epsilon = 500); + assert_abs_diff_eq!(nominator_emission, total_emission / 10 * 4, epsilon = 500); }); } diff --git a/pallets/subtensor/src/tests/mock.rs b/pallets/subtensor/src/tests/mock.rs index eba443a12d..a6f9caed7f 100644 --- a/pallets/subtensor/src/tests/mock.rs +++ b/pallets/subtensor/src/tests/mock.rs @@ -684,17 +684,6 @@ pub fn setup_neuron_with_stake(netuid: u16, hotkey: U256, coldkey: U256, stake: increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid); } -// Helper function to check if a value is within tolerance of an expected value -#[allow(dead_code)] -pub fn is_within_tolerance(actual: u64, expected: u64, tolerance: u64) -> bool { - let difference = if actual > expected { - actual - expected - } else { - expected - actual - }; - difference <= tolerance -} - #[allow(dead_code)] pub fn wait_and_set_pending_children(netuid: u16) { let original_block = System::block_number(); From 3d0659eb137a24380ef9c64364bb93fa758ab584 Mon Sep 17 00:00:00 2001 From: Sam Johnson Date: Fri, 10 Jan 2025 16:47:13 -0500 Subject: [PATCH 5/5] fix e2e tests --- .github/workflows/e2e-bittensor-tests.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/e2e-bittensor-tests.yml b/.github/workflows/e2e-bittensor-tests.yml index d21514909f..5cfeeb2328 100644 --- a/.github/workflows/e2e-bittensor-tests.yml +++ b/.github/workflows/e2e-bittensor-tests.yml @@ -70,6 +70,7 @@ jobs: python3 -m pip install -e . python3 -m pip install torch python3 -m pip install pytest + python3 -m pip install -r requirements/dev.txt - name: Run tests working-directory: ${{ github.workspace }}/bittensor