Skip to content

Commit 6a47916

Browse files
committed
sam fix test_childkey_take_drain
1 parent d1196ad commit 6a47916

File tree

1 file changed

+111
-129
lines changed

1 file changed

+111
-129
lines changed

pallets/subtensor/src/tests/children.rs

Lines changed: 111 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,135 +2823,117 @@ fn test_set_weights_no_parent() {
28232823
#[test]
28242824
fn test_childkey_take_drain() {
28252825
new_test_ext(1).execute_with(|| {
2826-
assert!(false);
2827-
2828-
// let coldkey = U256::from(1);
2829-
// let parent = U256::from(2);
2830-
// let child = U256::from(3);
2831-
// let nominator = U256::from(4);
2832-
// let netuid: u16 = 1;
2833-
// let root_id: u16 = 0;
2834-
// let subnet_tempo = 10;
2835-
// let hotkey_tempo = 20;
2836-
// let stake = 100_000_000_000;
2837-
// let proportion: u64 = u64::MAX;
2838-
2839-
// // Add network, register hotkeys, and setup network parameters
2840-
// add_network(root_id, subnet_tempo, 0);
2841-
// add_network(netuid, subnet_tempo, 0);
2842-
// register_ok_neuron(netuid, child, coldkey, 0);
2843-
// register_ok_neuron(netuid, parent, coldkey, 1);
2844-
2845-
// // Set children
2846-
// mock_set_children(&coldkey, &parent, netuid, &[(proportion, child)]);
2847-
2848-
// SubtensorModule::add_balance_to_coldkey_account(
2849-
// &coldkey,
2850-
// stake + ExistentialDeposit::get(),
2851-
// );
2852-
// SubtensorModule::add_balance_to_coldkey_account(
2853-
// &nominator,
2854-
// stake + ExistentialDeposit::get(),
2855-
// );
2856-
// SubtensorModule::set_hotkey_emission_tempo(hotkey_tempo);
2857-
// SubtensorModule::set_weights_set_rate_limit(netuid, 0);
2858-
// SubtensorModule::set_max_allowed_validators(netuid, 2);
2859-
// step_block(subnet_tempo);
2860-
// crate::SubnetOwnerCut::<Test>::set(0);
2861-
2862-
// // Set 20% childkey take
2863-
// let max_take: u16 = 0xFFFF / 5;
2864-
// SubtensorModule::set_max_childkey_take(max_take);
2865-
// assert_ok!(SubtensorModule::set_childkey_take(
2866-
// RuntimeOrigin::signed(coldkey),
2867-
// child,
2868-
// netuid,
2869-
// max_take
2870-
// ));
2871-
2872-
// // Set zero hotkey take for childkey
2873-
// SubtensorModule::set_min_delegate_take(0);
2874-
// assert_ok!(SubtensorModule::do_become_delegate(
2875-
// RuntimeOrigin::signed(coldkey),
2876-
// child,
2877-
// 0
2878-
// ));
2879-
2880-
// // Set zero hotkey take for parent
2881-
// assert_ok!(SubtensorModule::do_become_delegate(
2882-
// RuntimeOrigin::signed(coldkey),
2883-
// parent,
2884-
// 0
2885-
// ));
2886-
2887-
// // Setup stakes:
2888-
// // Stake from parent
2889-
// // Stake from nominator to childkey
2890-
// // Give 100% of parent stake to childkey
2891-
// assert_ok!(SubtensorModule::add_stake(
2892-
// RuntimeOrigin::signed(coldkey),
2893-
// parent,
2894-
// stake
2895-
// ));
2896-
// assert_ok!(SubtensorModule::add_stake(
2897-
// RuntimeOrigin::signed(nominator),
2898-
// child,
2899-
// stake
2900-
// ));
2901-
2902-
// // Setup YUMA so that it creates emissions:
2903-
// // Parent and child both set weights
2904-
// // Parent and child register on root and
2905-
// // Set root weights
2906-
// crate::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2907-
// crate::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2908-
// assert_ok!(SubtensorModule::do_root_register(
2909-
// RuntimeOrigin::signed(coldkey),
2910-
// parent,
2911-
// ));
2912-
// assert_ok!(SubtensorModule::do_root_register(
2913-
// RuntimeOrigin::signed(coldkey),
2914-
// child,
2915-
// ));
2916-
// crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2917-
// crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2918-
2919-
// // Run run_coinbase until PendingHotkeyEmission are populated
2920-
// while crate::PendingdHotkeyEmission::<Test>::get(child) == 0 {
2921-
// step_block(1);
2922-
// }
2923-
2924-
// // Prevent further subnet epochs
2925-
// crate::Tempo::<Test>::set(netuid, u16::MAX);
2926-
// crate::Tempo::<Test>::set(root_id, u16::MAX);
2927-
2928-
// // Run run_coinbase until PendingHotkeyEmission is drained for both child and parent
2929-
// step_block((hotkey_tempo * 2) as u16);
2930-
2931-
// // Verify how emission is split between keys
2932-
// // - Child stake increased by its child key take only (20% * 50% = 10% of total emission)
2933-
// // - Parent stake increased by 40% of total emission
2934-
// // - Nominator stake increased by 50% of total emission
2935-
// let child_emission = crate::Stake::<Test>::get(child, coldkey);
2936-
// let parent_emission = crate::Stake::<Test>::get(parent, coldkey) - stake;
2937-
// let nominator_emission = crate::Stake::<Test>::get(child, nominator) - stake;
2938-
// let total_emission = child_emission + parent_emission + nominator_emission;
2939-
2940-
// assert!(is_within_tolerance(
2941-
// child_emission,
2942-
// total_emission / 10,
2943-
// 500
2944-
// ));
2945-
// assert!(is_within_tolerance(
2946-
// parent_emission,
2947-
// total_emission / 10 * 4,
2948-
// 500
2949-
// ));
2950-
// assert!(is_within_tolerance(
2951-
// nominator_emission,
2952-
// total_emission / 2,
2953-
// 500
2954-
// ));
2826+
let coldkey = U256::from(1);
2827+
let parent = U256::from(2);
2828+
let child = U256::from(3);
2829+
let nominator = U256::from(4);
2830+
let netuid: u16 = 1;
2831+
let root_id: u16 = 0;
2832+
let subnet_tempo = 10;
2833+
let hotkey_tempo = 20;
2834+
let stake = 100_000_000_000;
2835+
let proportion: u64 = u64::MAX;
2836+
2837+
// Add network, register hotkeys, and setup network parameters
2838+
add_network(root_id, subnet_tempo, 0);
2839+
add_network(netuid, subnet_tempo, 0);
2840+
register_ok_neuron(netuid, child, coldkey, 0);
2841+
register_ok_neuron(netuid, parent, coldkey, 1);
2842+
2843+
// Set children
2844+
mock_set_children(&coldkey, &parent, netuid, &[(proportion, child)]);
2845+
2846+
SubtensorModule::add_balance_to_coldkey_account(
2847+
&coldkey,
2848+
stake + ExistentialDeposit::get(),
2849+
);
2850+
SubtensorModule::add_balance_to_coldkey_account(
2851+
&nominator,
2852+
stake + ExistentialDeposit::get(),
2853+
);
2854+
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
2855+
SubtensorModule::set_max_allowed_validators(netuid, 2);
2856+
step_block(subnet_tempo);
2857+
crate::SubnetOwnerCut::<Test>::set(0);
2858+
2859+
// Set 20% childkey take
2860+
let max_take: u16 = 0xFFFF / 5;
2861+
SubtensorModule::set_max_childkey_take(max_take);
2862+
assert_ok!(SubtensorModule::set_childkey_take(
2863+
RuntimeOrigin::signed(coldkey),
2864+
child,
2865+
netuid,
2866+
max_take
2867+
));
2868+
2869+
// Set zero hotkey take for childkey
2870+
SubtensorModule::set_min_delegate_take(0);
2871+
2872+
// Setup stakes:
2873+
// Stake from parent
2874+
// Stake from nominator to childkey
2875+
// Give 100% of parent stake to childkey
2876+
assert_ok!(SubtensorModule::add_stake(
2877+
RuntimeOrigin::signed(coldkey),
2878+
parent,
2879+
netuid,
2880+
stake
2881+
));
2882+
assert_ok!(SubtensorModule::add_stake(
2883+
RuntimeOrigin::signed(nominator),
2884+
child,
2885+
netuid,
2886+
stake
2887+
));
2888+
2889+
// Setup YUMA so that it creates emissions:
2890+
// Parent and child both set weights
2891+
// Parent and child register on root and
2892+
// Set root weights
2893+
crate::Weights::<Test>::insert(netuid, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2894+
crate::Weights::<Test>::insert(netuid, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2895+
assert_ok!(SubtensorModule::do_root_register(
2896+
RuntimeOrigin::signed(coldkey),
2897+
parent,
2898+
));
2899+
assert_ok!(SubtensorModule::do_root_register(
2900+
RuntimeOrigin::signed(coldkey),
2901+
child,
2902+
));
2903+
crate::Weights::<Test>::insert(root_id, 0, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2904+
crate::Weights::<Test>::insert(root_id, 1, vec![(0, 0xFFFF), (1, 0xFFFF)]);
2905+
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+
2913+
// Verify how emission is split between keys
2914+
// - Child stake increased by its child key take only (20% * 50% = 10% of total emission)
2915+
// - Parent stake increased by 40% of total emission
2916+
// - Nominator stake increased by 50% of total emission
2917+
let child_emission = crate::Stake::<Test>::get(child, coldkey);
2918+
let parent_emission = crate::Stake::<Test>::get(parent, coldkey).saturating_sub(stake);
2919+
let nominator_emission = crate::Stake::<Test>::get(child, nominator).saturating_sub(stake);
2920+
let total_emission = child_emission + parent_emission + nominator_emission;
2921+
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+
));
29552937
});
29562938
}
29572939

0 commit comments

Comments
 (0)