Skip to content

Commit cae24f9

Browse files
committed
sam fix test_childkey_take_drain_validator_take
1 parent c400217 commit cae24f9

File tree

1 file changed

+123
-136
lines changed

1 file changed

+123
-136
lines changed

pallets/subtensor/src/tests/children.rs

Lines changed: 123 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,133 +2957,113 @@ fn test_childkey_take_drain() {
29572957
#[test]
29582958
fn test_childkey_take_drain_validator_take() {
29592959
new_test_ext(1).execute_with(|| {
2960-
assert!(false);
2961-
2962-
// let coldkey = U256::from(1);
2963-
// let parent = U256::from(2);
2964-
// let child = U256::from(3);
2965-
// let nominator = U256::from(4);
2966-
// let netuid: u16 = 1;
2967-
// let root_id: u16 = 0;
2968-
// let subnet_tempo = 10;
2969-
// let hotkey_tempo = 20;
2970-
// let stake = 100_000_000_000;
2971-
// let proportion: u64 = u64::MAX;
2972-
2973-
// // Add network, register hotkeys, and setup network parameters
2974-
// add_network(root_id, subnet_tempo, 0);
2975-
// add_network(netuid, subnet_tempo, 0);
2976-
// register_ok_neuron(netuid, child, coldkey, 0);
2977-
// register_ok_neuron(netuid, parent, coldkey, 1);
2978-
// SubtensorModule::add_balance_to_coldkey_account(
2979-
// &coldkey,
2980-
// stake + ExistentialDeposit::get(),
2981-
// );
2982-
// SubtensorModule::add_balance_to_coldkey_account(
2983-
// &nominator,
2984-
// stake + ExistentialDeposit::get(),
2985-
// );
2986-
// SubtensorModule::set_hotkey_emission_tempo(hotkey_tempo);
2987-
// SubtensorModule::set_weights_set_rate_limit(netuid, 0);
2988-
// SubtensorModule::set_max_allowed_validators(netuid, 2);
2989-
// step_block(subnet_tempo);
2990-
// crate::SubnetOwnerCut::<Test>::set(0);
2991-
2992-
// // Set children
2993-
// mock_set_children(&coldkey, &parent, netuid, &[(proportion, child)]);
2994-
2995-
// // Set 20% childkey take
2996-
// let max_take: u16 = 0xFFFF / 5;
2997-
// SubtensorModule::set_max_childkey_take(max_take);
2998-
// assert_ok!(SubtensorModule::set_childkey_take(
2999-
// RuntimeOrigin::signed(coldkey),
3000-
// child,
3001-
// netuid,
3002-
// max_take
3003-
// ));
3004-
3005-
// // Set 20% hotkey take for childkey
3006-
// SubtensorModule::set_max_delegate_take(max_take);
3007-
// assert_ok!(SubtensorModule::do_become_delegate(
3008-
// RuntimeOrigin::signed(coldkey),
3009-
// child,
3010-
// max_take
3011-
// ));
3012-
3013-
// // Set 20% hotkey take for parent
3014-
// assert_ok!(SubtensorModule::do_become_delegate(
3015-
// RuntimeOrigin::signed(coldkey),
3016-
// parent,
3017-
// max_take
3018-
// ));
3019-
3020-
// // Setup stakes:
3021-
// // Stake from parent
3022-
// // Stake from nominator to childkey
3023-
// // Give 100% of parent stake to childkey
3024-
// assert_ok!(SubtensorModule::add_stake(
3025-
// RuntimeOrigin::signed(coldkey),
3026-
// parent,
3027-
// stake
3028-
// ));
3029-
// assert_ok!(SubtensorModule::add_stake(
3030-
// RuntimeOrigin::signed(nominator),
3031-
// child,
3032-
// stake
3033-
// ));
3034-
// // Make all stakes viable
3035-
// crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(parent, coldkey, -1);
3036-
// crate::StakeDeltaSinceLastEmissionDrain::<Test>::set(child, nominator, -1);
3037-
3038-
// // Setup YUMA so that it creates emissions:
3039-
// // Parent and child both set weights
3040-
// // Parent and child register on root and
3041-
// // Set root weights
3042-
// crate::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3043-
// crate::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3044-
// assert_ok!(SubtensorModule::do_root_register(
3045-
// RuntimeOrigin::signed(coldkey),
3046-
// parent,
3047-
// ));
3048-
// assert_ok!(SubtensorModule::do_root_register(
3049-
// RuntimeOrigin::signed(coldkey),
3050-
// child,
3051-
// ));
3052-
// crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3053-
// crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2960+
let coldkey = U256::from(1);
2961+
let parent = U256::from(2);
2962+
let child = U256::from(3);
2963+
let nominator = U256::from(4);
2964+
let netuid: u16 = 1;
2965+
let root_id: u16 = 0;
2966+
let subnet_tempo = 10;
2967+
let hotkey_tempo = 20;
2968+
let stake = 100_000_000_000;
2969+
let proportion: u64 = u64::MAX;
2970+
2971+
// Add network, register hotkeys, and setup network parameters
2972+
add_network(root_id, subnet_tempo, 0);
2973+
add_network(netuid, subnet_tempo, 0);
2974+
register_ok_neuron(netuid, child, coldkey, 0);
2975+
register_ok_neuron(netuid, parent, coldkey, 1);
2976+
SubtensorModule::add_balance_to_coldkey_account(
2977+
&coldkey,
2978+
stake + ExistentialDeposit::get(),
2979+
);
2980+
SubtensorModule::add_balance_to_coldkey_account(
2981+
&nominator,
2982+
stake + ExistentialDeposit::get(),
2983+
);
2984+
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
2985+
SubtensorModule::set_max_allowed_validators(netuid, 2);
2986+
step_block(subnet_tempo);
2987+
crate::SubnetOwnerCut::<Test>::set(0);
30542988

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

3060-
// // Prevent further subnet epochs
3061-
// crate::Tempo::<Test>::set(netuid, u16::MAX);
3062-
// crate::Tempo::<Test>::set(root_id, u16::MAX);
2992+
// Set 20% childkey take
2993+
let max_take: u16 = 0xFFFF / 5;
2994+
SubtensorModule::set_max_childkey_take(max_take);
2995+
assert_ok!(SubtensorModule::set_childkey_take(
2996+
RuntimeOrigin::signed(coldkey),
2997+
child,
2998+
netuid,
2999+
max_take
3000+
));
30633001

3064-
// // Run run_coinbase until PendingHotkeyEmission is drained for both child and parent
3065-
// step_block((hotkey_tempo * 2) as u16);
3002+
// Set 20% hotkey take for childkey
3003+
// Set 20% hotkey take for parent
3004+
SubtensorModule::set_max_delegate_take(max_take);
30663005

3067-
// // Verify how emission is split between keys
3068-
// // - Child stake increased by its child key take (20% * 50% = 10% of total emission) plus childkey's delegate take (10%)
3069-
// // - Parent stake increased by 40% of total emission
3070-
// // - Nominator stake increased by 40% of total emission
3071-
// let child_emission = crate::Stake::<Test>::get(child, coldkey);
3072-
// let parent_emission = crate::Stake::<Test>::get(parent, coldkey) - stake;
3073-
// let nominator_emission = crate::Stake::<Test>::get(child, nominator) - stake;
3074-
// let total_emission = child_emission + parent_emission + nominator_emission;
3006+
// Setup stakes:
3007+
// Stake from parent
3008+
// Stake from nominator to childkey
3009+
// Give 100% of parent stake to childkey
3010+
assert_ok!(SubtensorModule::add_stake(
3011+
RuntimeOrigin::signed(coldkey),
3012+
parent,
3013+
netuid,
3014+
stake
3015+
));
3016+
assert_ok!(SubtensorModule::add_stake(
3017+
RuntimeOrigin::signed(nominator),
3018+
child,
3019+
netuid,
3020+
stake
3021+
));
30753022

3076-
// assert!(is_within_tolerance(child_emission, total_emission / 5, 500));
3077-
// assert!(is_within_tolerance(
3078-
// parent_emission,
3079-
// total_emission / 10 * 4,
3080-
// 500
3081-
// ));
3082-
// assert!(is_within_tolerance(
3083-
// nominator_emission,
3084-
// total_emission / 10 * 4,
3085-
// 500
3086-
// ));
3023+
// Setup YUMA so that it creates emissions:
3024+
// Parent and child both set weights
3025+
// Parent and child register on root and
3026+
// Set root weights
3027+
crate::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3028+
crate::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3029+
assert_ok!(SubtensorModule::do_root_register(
3030+
RuntimeOrigin::signed(coldkey),
3031+
parent,
3032+
));
3033+
assert_ok!(SubtensorModule::do_root_register(
3034+
RuntimeOrigin::signed(coldkey),
3035+
child,
3036+
));
3037+
crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3038+
crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
3039+
3040+
// Prevent further subnet epochs
3041+
crate::Tempo::<Test>::set(netuid, u16::MAX);
3042+
crate::Tempo::<Test>::set(root_id, u16::MAX);
3043+
3044+
// Run run_coinbase until PendingHotkeyEmission is drained for both child and parent
3045+
step_block((hotkey_tempo * 2) as u16);
3046+
3047+
// Verify how emission is split between keys
3048+
// - Child stake increased by its child key take (20% * 50% = 10% of total emission) plus childkey's delegate take (10%)
3049+
// - Parent stake increased by 40% of total emission
3050+
// - Nominator stake increased by 40% of total emission
3051+
let child_emission = crate::Stake::<Test>::get(child, coldkey);
3052+
let parent_emission = crate::Stake::<Test>::get(parent, coldkey).saturating_sub(stake);
3053+
let nominator_emission = crate::Stake::<Test>::get(child, nominator).saturating_sub(stake);
3054+
let total_emission = child_emission + parent_emission + nominator_emission;
3055+
3056+
assert!(is_within_tolerance(child_emission, total_emission / 5, 500));
3057+
assert!(is_within_tolerance(
3058+
parent_emission,
3059+
total_emission / 10 * 4,
3060+
500
3061+
));
3062+
assert!(is_within_tolerance(
3063+
nominator_emission,
3064+
total_emission / 10 * 4,
3065+
500
3066+
));
30873067
});
30883068
}
30893069

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

31543134
// Set weights (subnet owner is uid 0, ignore him)
31553135
let uids: Vec<u16> = vec![1, 2];
@@ -3174,9 +3154,8 @@ fn test_childkey_multiple_parents_emission() {
31743154
}
31753155
step_block(1);
31763156
}
3177-
let total_emission =
3178-
SubtensorModule::get_block_emission().unwrap_or(0) *
3179-
(SubtensorModule::get_current_block_as_u64() - start_block + 1);
3157+
let total_emission = SubtensorModule::get_block_emission().unwrap_or(0)
3158+
* (SubtensorModule::get_current_block_as_u64() - start_block + 1);
31803159

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

31893168
for (coldkey, hotkey, name) in stakes.iter() {
3190-
let stake_on_subnet = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(hotkey, coldkey, netuid);
3191-
log::debug!(
3192-
"{} stake on subnet: {:?}",
3193-
name,
3194-
stake_on_subnet
3169+
let stake_on_subnet = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
3170+
hotkey, coldkey, netuid,
31953171
);
3172+
log::debug!("{} stake on subnet: {:?}", name, stake_on_subnet);
31963173
}
31973174

3198-
let parent1_stake =
3199-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&parent1, &coldkey_parent1, netuid);
3200-
let parent2_stake =
3201-
SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&parent2, &coldkey_parent2, netuid);
3202-
let child_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(&child, &coldkey_child, netuid);
3175+
let parent1_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
3176+
&parent1,
3177+
&coldkey_parent1,
3178+
netuid,
3179+
);
3180+
let parent2_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
3181+
&parent2,
3182+
&coldkey_parent2,
3183+
netuid,
3184+
);
3185+
let child_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
3186+
&child,
3187+
&coldkey_child,
3188+
netuid,
3189+
);
32033190
let weight_setter_stake = SubtensorModule::get_stake_for_hotkey_and_coldkey_on_subnet(
32043191
&weight_setter,
32053192
&coldkey_weight_setter,

0 commit comments

Comments
 (0)