Skip to content

Commit deffa1a

Browse files
committed
sam mostly fix test_dynamic_parent_child_relationships, pending vune's commit
1 parent cae24f9 commit deffa1a

File tree

1 file changed

+138
-157
lines changed

1 file changed

+138
-157
lines changed

pallets/subtensor/src/tests/children.rs

Lines changed: 138 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -3402,186 +3402,167 @@ fn test_parent_child_chain_emission() {
34023402
#[test]
34033403
fn test_dynamic_parent_child_relationships() {
34043404
new_test_ext(1).execute_with(|| {
3405-
assert!(false);
3406-
3407-
// let netuid: u16 = 1;
3408-
// add_network(netuid, 1, 0);
3409-
3410-
// // Define hotkeys and coldkeys
3411-
// let parent: U256 = U256::from(1);
3412-
// let child1: U256 = U256::from(2);
3413-
// let child2: U256 = U256::from(3);
3414-
// let coldkey_parent: U256 = U256::from(100);
3415-
// let coldkey_child1: U256 = U256::from(101);
3416-
// let coldkey_child2: U256 = U256::from(102);
3417-
3418-
// // Register neurons with varying stakes
3419-
// register_ok_neuron(netuid, parent, coldkey_parent, 0);
3420-
// register_ok_neuron(netuid, child1, coldkey_child1, 0);
3421-
// register_ok_neuron(netuid, child2, coldkey_child2, 0);
3422-
3423-
// // Add initial stakes
3424-
// SubtensorModule::add_balance_to_coldkey_account(&coldkey_parent, 500_000);
3425-
// SubtensorModule::add_balance_to_coldkey_account(&coldkey_child1, 50_000);
3426-
// SubtensorModule::add_balance_to_coldkey_account(&coldkey_child2, 30_000);
3405+
let netuid: u16 = 1;
3406+
add_network(netuid, 1, 0);
34273407

3428-
// SubtensorModule::increase_stake_on_coldkey_hotkey_account(&coldkey_parent, &parent, 500_000);
3429-
// SubtensorModule::increase_stake_on_coldkey_hotkey_account(&coldkey_child1, &child1, 50_000);
3430-
// SubtensorModule::increase_stake_on_coldkey_hotkey_account(&coldkey_child2, &child2, 30_000);
3408+
// Define hotkeys and coldkeys
3409+
let parent: U256 = U256::from(1);
3410+
let child1: U256 = U256::from(2);
3411+
let child2: U256 = U256::from(3);
3412+
let coldkey_parent: U256 = U256::from(100);
3413+
let coldkey_child1: U256 = U256::from(101);
3414+
let coldkey_child2: U256 = U256::from(102);
34313415

3432-
// mock_set_children(&coldkey_parent, &parent, netuid, &[(u64::MAX / 2, child1)]);
3416+
// Register neurons with varying stakes
3417+
register_ok_neuron(netuid, parent, coldkey_parent, 0);
3418+
register_ok_neuron(netuid, child1, coldkey_child1, 0);
3419+
register_ok_neuron(netuid, child2, coldkey_child2, 0);
34333420

3434-
// step_block(2);
3421+
// Add initial stakes
3422+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_parent, 500_000);
3423+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_child1, 50_000);
3424+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_child2, 30_000);
34353425

3436-
// // Set weights
3437-
// let origin = RuntimeOrigin::signed(parent);
3438-
// let uids: Vec<u16> = vec![0, 1, 2]; // UIDs for parent, child1, child2
3439-
// let values: Vec<u16> = vec![65535, 65535, 65535]; // Set equal weights for all hotkeys
3440-
// let version_key = SubtensorModule::get_weights_version_key(netuid);
3426+
SubtensorModule::add_stake(RuntimeOrigin::signed(coldkey_parent), parent, netuid, 500_000).unwrap();
3427+
SubtensorModule::add_stake(RuntimeOrigin::signed(coldkey_child1), child1, netuid, 50_000).unwrap();
3428+
SubtensorModule::add_stake(RuntimeOrigin::signed(coldkey_child2), child2, netuid, 30_000).unwrap();
34413429

3442-
// // Ensure we can set weights without rate limiting
3443-
// SubtensorModule::set_weights_set_rate_limit(netuid, 0);
3430+
mock_set_children(&coldkey_parent, &parent, netuid, &[(u64::MAX / 2, child1)]);
34443431

3445-
// assert_ok!(SubtensorModule::set_weights(
3446-
// origin,
3447-
// netuid,
3448-
// uids,
3449-
// values,
3450-
// version_key
3451-
// ));
3452-
3453-
// // Set hotkey emission tempo
3454-
// SubtensorModule::set_hotkey_emission_tempo(10);
3432+
step_block(2);
34553433

3456-
// // Run first epoch
3457-
// let hardcoded_emission: u64 = 1_000_000; // 1 million (adjust as needed)
3458-
// let hotkey_emission: Vec<(U256, u64, u64)> = SubtensorModule::epoch(netuid, hardcoded_emission);
3434+
// Set weights
3435+
let origin = RuntimeOrigin::signed(parent);
3436+
let uids: Vec<u16> = vec![0, 1, 2]; // UIDs for parent, child1, child2
3437+
let values: Vec<u16> = vec![65535, 65535, 65535]; // Set equal weights for all hotkeys
3438+
let version_key = SubtensorModule::get_weights_version_key(netuid);
34593439

3460-
// // Process the hotkey emission results
3461-
// for (hotkey, mining_emission, validator_emission) in hotkey_emission {
3462-
// SubtensorModule::accumulate_hotkey_emission(&hotkey, netuid, validator_emission, mining_emission);
3463-
// }
3440+
// Ensure we can set weights without rate limiting
3441+
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
34643442

3465-
// // Step blocks to allow for emission distribution
3466-
// step_block(11);
3467-
// step_rate_limit(&TransactionType::SetChildren, netuid);
3443+
assert_ok!(SubtensorModule::set_weights(
3444+
origin,
3445+
netuid,
3446+
uids,
3447+
values,
3448+
version_key
3449+
));
34683450

3469-
// // Change parent-child relationships
3470-
// mock_set_children(&coldkey_parent, &parent, netuid, &[(u64::MAX / 4, child1), (u64::MAX / 3, child2)]);
3451+
// Run first epoch
3452+
let hardcoded_emission: u64 = 1_000_000; // 1 million (adjust as needed)
34713453

3472-
// // Run second epoch
3473-
// let hotkey_emission: Vec<(U256, u64, u64)> = SubtensorModule::epoch(netuid, hardcoded_emission);
3454+
// Step blocks to allow for emission distribution
3455+
step_block(11);
3456+
step_rate_limit(&TransactionType::SetChildren, netuid);
34743457

3475-
// // Process the hotkey emission results
3476-
// for (hotkey, mining_emission, validator_emission) in hotkey_emission {
3477-
// SubtensorModule::accumulate_hotkey_emission(&hotkey, netuid, validator_emission, mining_emission);
3478-
// }
3458+
// Change parent-child relationships
3459+
mock_set_children(&coldkey_parent, &parent, netuid, &[(u64::MAX / 4, child1), (u64::MAX / 3, child2)]);
34793460

3480-
// // Step blocks again to allow for emission distribution
3481-
// step_block(11);
3461+
// Step blocks again to allow for emission distribution
3462+
step_block(11);
34823463

3483-
// // Check final emission distribution
3484-
// let parent_stake: u64 = SubtensorModule::get_stake_for_hotkey_on_subnet(&parent, netuid);
3485-
// let child1_stake: u64 = SubtensorModule::get_stake_for_hotkey_on_subnet(&child1, netuid);
3486-
// let child2_stake: u64 = SubtensorModule::get_stake_for_hotkey_on_subnet(&child2, netuid);
3464+
// Check final emission distribution
3465+
let parent_stake: u64 = SubtensorModule::get_stake_for_hotkey_on_subnet(&parent, netuid);
3466+
let child1_stake: u64 = SubtensorModule::get_stake_for_hotkey_on_subnet(&child1, netuid);
3467+
let child2_stake: u64 = SubtensorModule::get_stake_for_hotkey_on_subnet(&child2, netuid);
34873468

3488-
// log::info!("Final stakes:");
3489-
// log::info!("Parent stake: {}", parent_stake);
3490-
// log::info!("Child1 stake: {}", child1_stake);
3491-
// log::info!("Child2 stake: {}", child2_stake);
3469+
log::info!("Final stakes:");
3470+
log::info!("Parent stake: {}", parent_stake);
3471+
log::info!("Child1 stake: {}", child1_stake);
3472+
log::info!("Child2 stake: {}", child2_stake);
34923473

3493-
// const TOLERANCE: u64 = 5; // Allow for a small discrepancy due to potential rounding
3474+
const TOLERANCE: u64 = 5; // Allow for a small discrepancy due to potential rounding
34943475

3495-
// // Precise assertions with tolerance
3496-
// assert!(
3497-
// (parent_stake as i64 - 926725).abs() <= TOLERANCE as i64,
3498-
// "Parent stake should be close to 926,725, but was {}",
3499-
// parent_stake
3500-
// );
3501-
// // Parent stake calculation:
3502-
// // Initial stake: 500,000
3503-
// // First epoch: ~862,500 (500,000 + 725,000 * 1/2)
3504-
// // Second epoch: ~926,725 (862,500 + 725,000 * 5/12)
3476+
// Precise assertions with tolerance
3477+
assert!(
3478+
(parent_stake as i128 - 926725i128).abs() <= TOLERANCE as i128,
3479+
"Parent stake should be close to 926,725, but was {}",
3480+
parent_stake
3481+
);
3482+
// Parent stake calculation:
3483+
// Initial stake: 500,000
3484+
// First epoch: ~862,500 (500,000 + 725,000 * 1/2)
3485+
// Second epoch: ~926,725 (862,500 + 725,000 * 5/12)
35053486

3506-
// assert!(
3507-
// (child1_stake as i64 - 778446).abs() <= TOLERANCE as i64,
3508-
// "Child1 stake should be close to 778,446, but was {}",
3509-
// child1_stake
3510-
// );
3511-
// // Child1 stake calculation:
3512-
// // Initial stake: 50,000
3513-
// // First epoch: ~412,500 (50,000 + 725,000 * 1/2)
3514-
// // Second epoch: ~778,446 (412,500 + 725,000 * 1/2 * 1/4 + 137,500)
3487+
assert!(
3488+
(child1_stake as i64 - 778446).abs() <= TOLERANCE as i64,
3489+
"Child1 stake should be close to 778,446, but was {}",
3490+
child1_stake
3491+
);
3492+
// Child1 stake calculation:
3493+
// Initial stake: 50,000
3494+
// First epoch: ~412,500 (50,000 + 725,000 * 1/2)
3495+
// Second epoch: ~778,446 (412,500 + 725,000 * 1/2 * 1/4 + 137,500)
35153496

3516-
// assert!(
3517-
// (child2_stake as i64 - 874826).abs() <= TOLERANCE as i64,
3518-
// "Child2 stake should be close to 874,826, but was {}",
3519-
// child2_stake
3520-
// );
3521-
// // Child2 stake calculation:
3522-
// // Initial stake: 30,000
3523-
// // First epoch: ~167,500 (30,000 + 137,500)
3524-
// // Second epoch: ~874,826 (167,500 + 725,000 * 1/2 * 1/3 + 137,500)
3525-
3526-
// // Check that the total stake has increased by approximately twice the hardcoded emission amount
3527-
// let total_stake: u64 = parent_stake + child1_stake + child2_stake;
3528-
// let initial_total_stake: u64 = 500_000 + 50_000 + 30_000;
3529-
// let total_emission: u64 = 2 * hardcoded_emission;
3530-
// assert!(
3531-
// (total_stake as i64 - (initial_total_stake + total_emission) as i64).abs() <= TOLERANCE as i64,
3532-
// "Total stake should have increased by approximately twice the hardcoded emission amount"
3533-
// );
3534-
// // Total stake calculation:
3535-
// // Initial total stake: 500,000 + 50,000 + 30,000 = 580,000
3536-
// // Total emission: 2 * 1,000,000 = 2,000,000
3537-
// // Expected total stake: 580,000 + 2,000,000 = 2,580,000
3497+
assert!(
3498+
(child2_stake as i64 - 874826).abs() <= TOLERANCE as i64,
3499+
"Child2 stake should be close to 874,826, but was {}",
3500+
child2_stake
3501+
);
3502+
// Child2 stake calculation:
3503+
// Initial stake: 30,000
3504+
// First epoch: ~167,500 (30,000 + 137,500)
3505+
// Second epoch: ~874,826 (167,500 + 725,000 * 1/2 * 1/3 + 137,500)
3506+
3507+
// Check that the total stake has increased by approximately twice the hardcoded emission amount
3508+
let total_stake: u64 = parent_stake + child1_stake + child2_stake;
3509+
let initial_total_stake: u64 = 500_000 + 50_000 + 30_000;
3510+
let total_emission: u64 = 2 * hardcoded_emission;
3511+
assert!(
3512+
(total_stake as i64 - (initial_total_stake + total_emission) as i64).abs() <= TOLERANCE as i64,
3513+
"Total stake should have increased by approximately twice the hardcoded emission amount"
3514+
);
3515+
// Total stake calculation:
3516+
// Initial total stake: 500,000 + 50,000 + 30,000 = 580,000
3517+
// Total emission: 2 * 1,000,000 = 2,000,000
3518+
// Expected total stake: 580,000 + 2,000,000 = 2,580,000
35383519

3539-
// // Additional checks for parent-child relationships
3540-
// let parent_children: Vec<(u64, U256)> = SubtensorModule::get_children(&parent, netuid);
3541-
// assert_eq!(
3542-
// parent_children,
3543-
// vec![(u64::MAX / 4, child1), (u64::MAX / 3, child2)],
3544-
// "Parent should have both children with correct proportions"
3545-
// );
3546-
// // Parent-child relationship:
3547-
// // child1: 1/4 of parent's stake
3548-
// // child2: 1/3 of parent's stake
3520+
// Additional checks for parent-child relationships
3521+
let parent_children: Vec<(u64, U256)> = SubtensorModule::get_children(&parent, netuid);
3522+
assert_eq!(
3523+
parent_children,
3524+
vec![(u64::MAX / 4, child1), (u64::MAX / 3, child2)],
3525+
"Parent should have both children with correct proportions"
3526+
);
3527+
// Parent-child relationship:
3528+
// child1: 1/4 of parent's stake
3529+
// child2: 1/3 of parent's stake
35493530

3550-
// let child1_parents: Vec<(u64, U256)> = SubtensorModule::get_parents(&child1, netuid);
3551-
// assert_eq!(
3552-
// child1_parents,
3553-
// vec![(u64::MAX / 4, parent)],
3554-
// "Child1 should have parent as its parent with correct proportion"
3555-
// );
3556-
// // Child1-parent relationship:
3557-
// // parent: 1/4 of child1's stake
3531+
let child1_parents: Vec<(u64, U256)> = SubtensorModule::get_parents(&child1, netuid);
3532+
assert_eq!(
3533+
child1_parents,
3534+
vec![(u64::MAX / 4, parent)],
3535+
"Child1 should have parent as its parent with correct proportion"
3536+
);
3537+
// Child1-parent relationship:
3538+
// parent: 1/4 of child1's stake
35583539

3559-
// let child2_parents: Vec<(u64, U256)> = SubtensorModule::get_parents(&child2, netuid);
3560-
// assert_eq!(
3561-
// child2_parents,
3562-
// vec![(u64::MAX / 3, parent)],
3563-
// "Child2 should have parent as its parent with correct proportion"
3564-
// );
3565-
// // Child2-parent relationship:
3566-
// // parent: 1/3 of child2's stake
3540+
let child2_parents: Vec<(u64, U256)> = SubtensorModule::get_parents(&child2, netuid);
3541+
assert_eq!(
3542+
child2_parents,
3543+
vec![(u64::MAX / 3, parent)],
3544+
"Child2 should have parent as its parent with correct proportion"
3545+
);
3546+
// Child2-parent relationship:
3547+
// parent: 1/3 of child2's stake
35673548

3568-
// // Check that child2 has received more stake than child1
3569-
// assert!(
3570-
// child2_stake > child1_stake,
3571-
// "Child2 should have received more emission than Child1 due to higher proportion"
3572-
// );
3573-
// // Child2 stake (874,826) > Child1 stake (778,446)
3549+
// Check that child2 has received more stake than child1
3550+
assert!(
3551+
child2_stake > child1_stake,
3552+
"Child2 should have received more emission than Child1 due to higher proportion"
3553+
);
3554+
// Child2 stake (874,826) > Child1 stake (778,446)
35743555

3575-
// // Check the approximate difference between child2 and child1 stakes
3576-
// let stake_difference: u64 = child2_stake - child1_stake;
3577-
// assert!(
3578-
// (stake_difference as i64 - 96_380).abs() <= TOLERANCE as i64,
3579-
// "The difference between Child2 and Child1 stakes should be close to 96,380, but was {}",
3580-
// stake_difference
3581-
// );
3582-
// // Stake difference calculation:
3583-
// // Child2 stake: 874,826
3584-
// // Child1 stake: 778,446
3585-
// // Difference: 874,826 - 778,446 = 96,380
3556+
// Check the approximate difference between child2 and child1 stakes
3557+
let stake_difference: u64 = child2_stake - child1_stake;
3558+
assert!(
3559+
(stake_difference as i64 - 96_380).abs() <= TOLERANCE as i64,
3560+
"The difference between Child2 and Child1 stakes should be close to 96,380, but was {}",
3561+
stake_difference
3562+
);
3563+
// Stake difference calculation:
3564+
// Child2 stake: 874,826
3565+
// Child1 stake: 778,446
3566+
// Difference: 874,826 - 778,446 = 96,380
35863567
});
35873568
}

0 commit comments

Comments
 (0)