Skip to content

Commit

Permalink
sam fix test_childkey_take_drain_validator_take
Browse files Browse the repository at this point in the history
  • Loading branch information
sam0x17 committed Jan 9, 2025
1 parent c400217 commit cae24f9
Showing 1 changed file with 123 additions and 136 deletions.
259 changes: 123 additions & 136 deletions pallets/subtensor/src/tests/children.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2957,133 +2957,113 @@ fn test_childkey_take_drain() {
#[test]
fn test_childkey_take_drain_validator_take() {
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);
// 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::<Test>::set(0);

// // Set children
// mock_set_children(&coldkey, &parent, netuid, &[(proportion, child)]);

// // 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 20% hotkey take for childkey
// SubtensorModule::set_max_delegate_take(max_take);
// assert_ok!(SubtensorModule::do_become_delegate(
// RuntimeOrigin::signed(coldkey),
// child,
// max_take
// ));

// // Set 20% hotkey take for parent
// assert_ok!(SubtensorModule::do_become_delegate(
// RuntimeOrigin::signed(coldkey),
// parent,
// max_take
// ));

// // 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
// ));
// // Make all stakes viable
// crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(parent, coldkey, -1);
// crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(child, nominator, -1);

// // 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::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
// crate::Weights::<Test>::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::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
// crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
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);
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::<Test>::set(0);

// // Run run_coinbase until PendingHotkeyEmission are populated
// while crate::PendingdHotkeyEmission::<Test>::get(child) == 0 {
// step_block(1);
// }
// Set children
mock_set_children(&coldkey, &parent, netuid, &[(proportion, child)]);

// // Prevent further subnet epochs
// crate::Tempo::<Test>::set(netuid, u16::MAX);
// crate::Tempo::<Test>::set(root_id, u16::MAX);
// 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
));

// // Run run_coinbase until PendingHotkeyEmission is drained for both child and parent
// step_block((hotkey_tempo * 2) as u16);
// Set 20% hotkey take for childkey
// Set 20% hotkey take for parent
SubtensorModule::set_max_delegate_take(max_take);

// // Verify how emission is split between keys
// // - Child stake increased by its child key take (20% * 50% = 10% of total emission) plus childkey's delegate take (10%)
// // - Parent stake increased by 40% of total emission
// // - Nominator stake increased by 40% of total emission
// let child_emission = crate::Stake::<Test>::get(child, coldkey);
// let parent_emission = crate::Stake::<Test>::get(parent, coldkey) - stake;
// let nominator_emission = crate::Stake::<Test>::get(child, nominator) - stake;
// let total_emission = child_emission + parent_emission + nominator_emission;
// 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
));

// 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
// ));
// 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::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
crate::Weights::<Test>::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::<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 (20% * 50% = 10% of total emission) plus childkey's delegate take (10%)
// - Parent stake increased by 40% of total emission
// - Nominator stake increased by 40% of total emission
let child_emission = crate::Stake::<Test>::get(child, coldkey);
let parent_emission = crate::Stake::<Test>::get(parent, coldkey).saturating_sub(stake);
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
));
});
}

Expand Down Expand Up @@ -3149,7 +3129,7 @@ fn test_childkey_multiple_parents_emission() {
// Set parent-child relationships
mock_set_children(&coldkey_parent1, &parent1, netuid, &[(u64::MAX, child)]);
mock_set_children(&coldkey_parent2, &parent2, netuid, &[(u64::MAX / 2, child)]);
ChildkeyTake::<Test>::insert(child, netuid, u16::MAX/5);
ChildkeyTake::<Test>::insert(child, netuid, u16::MAX / 5);

// Set weights (subnet owner is uid 0, ignore him)
let uids: Vec<u16> = vec![1, 2];
Expand All @@ -3174,9 +3154,8 @@ fn test_childkey_multiple_parents_emission() {
}
step_block(1);
}
let total_emission =
SubtensorModule::get_block_emission().unwrap_or(0) *
(SubtensorModule::get_current_block_as_u64() - start_block + 1);
let total_emission = SubtensorModule::get_block_emission().unwrap_or(0)
* (SubtensorModule::get_current_block_as_u64() - start_block + 1);

// Check emission distribution
let stakes: Vec<(U256, U256, &str)> = vec![
Expand All @@ -3187,19 +3166,27 @@ fn test_childkey_multiple_parents_emission() {
];

for (coldkey, hotkey, name) in stakes.iter() {
let stake_on_subnet = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid);
log::debug!(
"{} stake on subnet: {:?}",
name,
stake_on_subnet
let stake_on_subnet = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
hotkey, coldkey, netuid,
);
log::debug!("{} stake on subnet: {:?}", name, stake_on_subnet);
}

let parent1_stake =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&parent1, &coldkey_parent1, netuid);
let parent2_stake =
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&parent2, &coldkey_parent2, netuid);
let child_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&child, &coldkey_child, netuid);
let parent1_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&parent1,
&coldkey_parent1,
netuid,
);
let parent2_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&parent2,
&coldkey_parent2,
netuid,
);
let child_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&child,
&coldkey_child,
netuid,
);
let weight_setter_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
&weight_setter,
&coldkey_weight_setter,
Expand Down

0 comments on commit cae24f9

Please sign in to comment.