Skip to content

Commit

Permalink
Use dynamic network in test_childkey_take_drain, remove is_within_tol…
Browse files Browse the repository at this point in the history
…erance
  • Loading branch information
gztensor committed Jan 10, 2025
1 parent 6a47916 commit cba54fe
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 47 deletions.
46 changes: 10 additions & 36 deletions pallets/subtensor/src/tests/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Test>::set(netuid, subnet_tempo);
register_ok_neuron(netuid, child, coldkey, 0);
register_ok_neuron(netuid, parent, coldkey, 1);

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

// Prevent further subnet epochs
crate::Tempo::<Test>::set(netuid, u16::MAX);
crate::Tempo::<Test>::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
Expand All @@ -2919,21 +2913,9 @@ fn test_childkey_take_drain() {
let nominator_emission = crate::Stake::<Test>::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);
});
}

Expand Down Expand Up @@ -3037,17 +3019,9 @@ fn test_childkey_take_drain_validator_take() {
let nominator_emission = crate::Stake::<Test>::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);
});
}

Expand Down
11 changes: 0 additions & 11 deletions pallets/subtensor/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cba54fe

Please sign in to comment.