Skip to content

Commit dcda87b

Browse files
authored
Merge pull request #1148 from opentensor/sam-clean-up-rao
clean up devnet-ready
2 parents cfd127f + 3d0659e commit dcda87b

File tree

3 files changed

+97
-151
lines changed

3 files changed

+97
-151
lines changed

.github/workflows/e2e-bittensor-tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ jobs:
7070
python3 -m pip install -e .
7171
python3 -m pip install torch
7272
python3 -m pip install pytest
73+
python3 -m pip install -r requirements/dev.txt
7374
7475
- name: Run tests
7576
working-directory: ${{ github.workspace }}/bittensor

pallets/subtensor/src/tests/children.rs

Lines changed: 96 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,135 +2823,99 @@ fn test_set_weights_no_parent() {
28232823
#[test]
28242824
fn test_childkey_take_drain() {
28252825
new_test_ext(1).execute_with(|| {
2826-
assert!(false);
2827-
2828-
// let coldkey = U256::from(1);
2829-
// let parent = U256::from(2);
2830-
// let child = U256::from(3);
2831-
// let nominator = U256::from(4);
2832-
// let netuid: u16 = 1;
2833-
// let root_id: u16 = 0;
2834-
// let subnet_tempo = 10;
2835-
// let hotkey_tempo = 20;
2836-
// let stake = 100_000_000_000;
2837-
// let proportion: u64 = u64::MAX;
2838-
2839-
// // Add network, register hotkeys, and setup network parameters
2840-
// add_network(root_id, subnet_tempo, 0);
2841-
// add_network(netuid, subnet_tempo, 0);
2842-
// register_ok_neuron(netuid, child, coldkey, 0);
2843-
// register_ok_neuron(netuid, parent, coldkey, 1);
2844-
2845-
// // Set children
2846-
// mock_set_children(&coldkey, &parent, netuid, &[(proportion, child)]);
2847-
2848-
// SubtensorModule::add_balance_to_coldkey_account(
2849-
// &coldkey,
2850-
// stake + ExistentialDeposit::get(),
2851-
// );
2852-
// SubtensorModule::add_balance_to_coldkey_account(
2853-
// &nominator,
2854-
// stake + ExistentialDeposit::get(),
2855-
// );
2856-
// SubtensorModule::set_hotkey_emission_tempo(hotkey_tempo);
2857-
// SubtensorModule::set_weights_set_rate_limit(netuid, 0);
2858-
// SubtensorModule::set_max_allowed_validators(netuid, 2);
2859-
// step_block(subnet_tempo);
2860-
// crate::SubnetOwnerCut::<Test>::set(0);
2861-
2862-
// // Set 20% childkey take
2863-
// let max_take: u16 = 0xFFFF / 5;
2864-
// SubtensorModule::set_max_childkey_take(max_take);
2865-
// assert_ok!(SubtensorModule::set_childkey_take(
2866-
// RuntimeOrigin::signed(coldkey),
2867-
// child,
2868-
// netuid,
2869-
// max_take
2870-
// ));
2871-
2872-
// // Set zero hotkey take for childkey
2873-
// SubtensorModule::set_min_delegate_take(0);
2874-
// assert_ok!(SubtensorModule::do_become_delegate(
2875-
// RuntimeOrigin::signed(coldkey),
2876-
// child,
2877-
// 0
2878-
// ));
2879-
2880-
// // Set zero hotkey take for parent
2881-
// assert_ok!(SubtensorModule::do_become_delegate(
2882-
// RuntimeOrigin::signed(coldkey),
2883-
// parent,
2884-
// 0
2885-
// ));
2886-
2887-
// // Setup stakes:
2888-
// // Stake from parent
2889-
// // Stake from nominator to childkey
2890-
// // Give 100% of parent stake to childkey
2891-
// assert_ok!(SubtensorModule::add_stake(
2892-
// RuntimeOrigin::signed(coldkey),
2893-
// parent,
2894-
// stake
2895-
// ));
2896-
// assert_ok!(SubtensorModule::add_stake(
2897-
// RuntimeOrigin::signed(nominator),
2898-
// child,
2899-
// stake
2900-
// ));
2901-
2902-
// // Setup YUMA so that it creates emissions:
2903-
// // Parent and child both set weights
2904-
// // Parent and child register on root and
2905-
// // Set root weights
2906-
// crate::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2907-
// crate::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2908-
// assert_ok!(SubtensorModule::do_root_register(
2909-
// RuntimeOrigin::signed(coldkey),
2910-
// parent,
2911-
// ));
2912-
// assert_ok!(SubtensorModule::do_root_register(
2913-
// RuntimeOrigin::signed(coldkey),
2914-
// child,
2915-
// ));
2916-
// crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2917-
// crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2918-
2919-
// // Run run_coinbase until PendingHotkeyEmission are populated
2920-
// while crate::PendingdHotkeyEmission::<Test>::get(child) == 0 {
2921-
// step_block(1);
2922-
// }
2923-
2924-
// // Prevent further subnet epochs
2925-
// crate::Tempo::<Test>::set(netuid, u16::MAX);
2926-
// crate::Tempo::<Test>::set(root_id, u16::MAX);
2927-
2928-
// // Run run_coinbase until PendingHotkeyEmission is drained for both child and parent
2929-
// step_block((hotkey_tempo * 2) as u16);
2930-
2931-
// // Verify how emission is split between keys
2932-
// // - Child stake increased by its child key take only (20% * 50% = 10% of total emission)
2933-
// // - Parent stake increased by 40% of total emission
2934-
// // - Nominator stake increased by 50% of total emission
2935-
// let child_emission = crate::Stake::<Test>::get(child, coldkey);
2936-
// let parent_emission = crate::Stake::<Test>::get(parent, coldkey) - stake;
2937-
// let nominator_emission = crate::Stake::<Test>::get(child, nominator) - stake;
2938-
// let total_emission = child_emission + parent_emission + nominator_emission;
2939-
2940-
// assert!(is_within_tolerance(
2941-
// child_emission,
2942-
// total_emission / 10,
2943-
// 500
2944-
// ));
2945-
// assert!(is_within_tolerance(
2946-
// parent_emission,
2947-
// total_emission / 10 * 4,
2948-
// 500
2949-
// ));
2950-
// assert!(is_within_tolerance(
2951-
// nominator_emission,
2952-
// total_emission / 2,
2953-
// 500
2954-
// ));
2826+
let subnet_owner_coldkey = U256::from(1001);
2827+
let subnet_owner_hotkey = U256::from(1002);
2828+
let coldkey = U256::from(1);
2829+
let parent = U256::from(2);
2830+
let child = U256::from(3);
2831+
let nominator = U256::from(4);
2832+
let root_id: u16 = 0;
2833+
let subnet_tempo = 10;
2834+
let stake = 100_000_000_000;
2835+
let proportion: u64 = u64::MAX;
2836+
2837+
// Add network, register hotkeys, and setup network parameters
2838+
add_network(root_id, subnet_tempo, 0);
2839+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
2840+
crate::Tempo::<Test>::set(netuid, subnet_tempo);
2841+
register_ok_neuron(netuid, child, coldkey, 0);
2842+
register_ok_neuron(netuid, parent, coldkey, 1);
2843+
2844+
// Set children
2845+
mock_set_children(&coldkey, &parent, netuid, &[(proportion, child)]);
2846+
2847+
SubtensorModule::add_balance_to_coldkey_account(
2848+
&coldkey,
2849+
stake + ExistentialDeposit::get(),
2850+
);
2851+
SubtensorModule::add_balance_to_coldkey_account(
2852+
&nominator,
2853+
stake + ExistentialDeposit::get(),
2854+
);
2855+
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
2856+
SubtensorModule::set_max_allowed_validators(netuid, 2);
2857+
step_block(subnet_tempo);
2858+
crate::SubnetOwnerCut::<Test>::set(0);
2859+
2860+
// Set 20% childkey take
2861+
let max_take: u16 = 0xFFFF / 5;
2862+
SubtensorModule::set_max_childkey_take(max_take);
2863+
assert_ok!(SubtensorModule::set_childkey_take(
2864+
RuntimeOrigin::signed(coldkey),
2865+
child,
2866+
netuid,
2867+
max_take
2868+
));
2869+
2870+
// Set zero hotkey take for childkey
2871+
SubtensorModule::set_min_delegate_take(0);
2872+
2873+
// Setup stakes:
2874+
// Stake from parent
2875+
// Stake from nominator to childkey
2876+
// Give 100% of parent stake to childkey
2877+
assert_ok!(SubtensorModule::add_stake(
2878+
RuntimeOrigin::signed(coldkey),
2879+
parent,
2880+
netuid,
2881+
stake
2882+
));
2883+
assert_ok!(SubtensorModule::add_stake(
2884+
RuntimeOrigin::signed(nominator),
2885+
child,
2886+
netuid,
2887+
stake
2888+
));
2889+
2890+
// Setup YUMA so that it creates emissions:
2891+
// Parent and child both set weights
2892+
// Parent and child register on root and
2893+
// Set root weights
2894+
crate::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2895+
crate::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2896+
assert_ok!(SubtensorModule::do_root_register(
2897+
RuntimeOrigin::signed(coldkey),
2898+
parent,
2899+
));
2900+
assert_ok!(SubtensorModule::do_root_register(
2901+
RuntimeOrigin::signed(coldkey),
2902+
child,
2903+
));
2904+
crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2905+
crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2906+
2907+
// Verify how emission is split between keys
2908+
// - Child stake increased by its child key take only (20% * 50% = 10% of total emission)
2909+
// - Parent stake increased by 40% of total emission
2910+
// - Nominator stake increased by 50% of total emission
2911+
let child_emission = crate::Stake::<Test>::get(child, coldkey);
2912+
let parent_emission = crate::Stake::<Test>::get(parent, coldkey).saturating_sub(stake);
2913+
let nominator_emission = crate::Stake::<Test>::get(child, nominator).saturating_sub(stake);
2914+
let total_emission = child_emission + parent_emission + nominator_emission;
2915+
2916+
assert_abs_diff_eq!(child_emission, total_emission / 10, epsilon = 500);
2917+
assert_abs_diff_eq!(parent_emission, total_emission / 10 * 4, epsilon = 500);
2918+
assert_abs_diff_eq!(nominator_emission, total_emission / 2, epsilon = 500);
29552919
});
29562920
}
29572921

@@ -3055,17 +3019,9 @@ fn test_childkey_take_drain_validator_take() {
30553019
let nominator_emission = crate::Stake::<Test>::get(child, nominator).saturating_sub(stake);
30563020
let total_emission = child_emission + parent_emission + nominator_emission;
30573021

3058-
assert!(is_within_tolerance(child_emission, total_emission / 5, 500));
3059-
assert!(is_within_tolerance(
3060-
parent_emission,
3061-
total_emission / 10 * 4,
3062-
500
3063-
));
3064-
assert!(is_within_tolerance(
3065-
nominator_emission,
3066-
total_emission / 10 * 4,
3067-
500
3068-
));
3022+
assert_abs_diff_eq!(child_emission, total_emission / 5, epsilon = 500);
3023+
assert_abs_diff_eq!(parent_emission, total_emission / 10 * 4, epsilon = 500);
3024+
assert_abs_diff_eq!(nominator_emission, total_emission / 10 * 4, epsilon = 500);
30693025
});
30703026
}
30713027

pallets/subtensor/src/tests/mock.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -684,17 +684,6 @@ pub fn setup_neuron_with_stake(netuid: u16, hotkey: U256, coldkey: U256, stake:
684684
increase_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake, netuid);
685685
}
686686

687-
// Helper function to check if a value is within tolerance of an expected value
688-
#[allow(dead_code)]
689-
pub fn is_within_tolerance(actual: u64, expected: u64, tolerance: u64) -> bool {
690-
let difference = if actual > expected {
691-
actual - expected
692-
} else {
693-
expected - actual
694-
};
695-
difference <= tolerance
696-
}
697-
698687
#[allow(dead_code)]
699688
pub fn wait_and_set_pending_children(netuid: u16) {
700689
let original_block = System::block_number();

0 commit comments

Comments
 (0)