Skip to content

Commit cba54fe

Browse files
committed
Use dynamic network in test_childkey_take_drain, remove is_within_tolerance
1 parent 6a47916 commit cba54fe

File tree

2 files changed

+10
-47
lines changed

2 files changed

+10
-47
lines changed

pallets/subtensor/src/tests/children.rs

Lines changed: 10 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,20 +2823,21 @@ fn test_set_weights_no_parent() {
28232823
#[test]
28242824
fn test_childkey_take_drain() {
28252825
new_test_ext(1).execute_with(|| {
2826+
let subnet_owner_coldkey = U256::from(1001);
2827+
let subnet_owner_hotkey = U256::from(1002);
28262828
let coldkey = U256::from(1);
28272829
let parent = U256::from(2);
28282830
let child = U256::from(3);
28292831
let nominator = U256::from(4);
2830-
let netuid: u16 = 1;
28312832
let root_id: u16 = 0;
28322833
let subnet_tempo = 10;
2833-
let hotkey_tempo = 20;
28342834
let stake = 100_000_000_000;
28352835
let proportion: u64 = u64::MAX;
28362836

28372837
// Add network, register hotkeys, and setup network parameters
28382838
add_network(root_id, subnet_tempo, 0);
2839-
add_network(netuid, subnet_tempo, 0);
2839+
let netuid: u16 = add_dynamic_network(&subnet_owner_hotkey, &subnet_owner_coldkey);
2840+
crate::Tempo::<Test>::set(netuid, subnet_tempo);
28402841
register_ok_neuron(netuid, child, coldkey, 0);
28412842
register_ok_neuron(netuid, parent, coldkey, 1);
28422843

@@ -2903,13 +2904,6 @@ fn test_childkey_take_drain() {
29032904
crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
29042905
crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
29052906

2906-
// Prevent further subnet epochs
2907-
crate::Tempo::<Test>::set(netuid, u16::MAX);
2908-
crate::Tempo::<Test>::set(root_id, u16::MAX);
2909-
2910-
// Run run_coinbase until PendingHotkeyEmission is drained for both child and parent
2911-
step_block((hotkey_tempo * 2) as u16);
2912-
29132907
// Verify how emission is split between keys
29142908
// - Child stake increased by its child key take only (20% * 50% = 10% of total emission)
29152909
// - Parent stake increased by 40% of total emission
@@ -2919,21 +2913,9 @@ fn test_childkey_take_drain() {
29192913
let nominator_emission = crate::Stake::<Test>::get(child, nominator).saturating_sub(stake);
29202914
let total_emission = child_emission + parent_emission + nominator_emission;
29212915

2922-
assert!(is_within_tolerance(
2923-
child_emission,
2924-
total_emission / 10,
2925-
500
2926-
));
2927-
assert!(is_within_tolerance(
2928-
parent_emission,
2929-
total_emission / 10 * 4,
2930-
500
2931-
));
2932-
assert!(is_within_tolerance(
2933-
nominator_emission,
2934-
total_emission / 2,
2935-
500
2936-
));
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);
29372919
});
29382920
}
29392921

@@ -3037,17 +3019,9 @@ fn test_childkey_take_drain_validator_take() {
30373019
let nominator_emission = crate::Stake::<Test>::get(child, nominator).saturating_sub(stake);
30383020
let total_emission = child_emission + parent_emission + nominator_emission;
30393021

3040-
assert!(is_within_tolerance(child_emission, total_emission / 5, 500));
3041-
assert!(is_within_tolerance(
3042-
parent_emission,
3043-
total_emission / 10 * 4,
3044-
500
3045-
));
3046-
assert!(is_within_tolerance(
3047-
nominator_emission,
3048-
total_emission / 10 * 4,
3049-
500
3050-
));
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);
30513025
});
30523026
}
30533027

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)