Skip to content

Commit eea8d1a

Browse files
committed
add test for set weights checks on child keys
1 parent 16d636a commit eea8d1a

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

pallets/subtensor/tests/children.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3237,3 +3237,111 @@ fn test_rank_trust_incentive_calculation_with_parent_child() {
32373237

32383238
});
32393239
}
3240+
3241+
// SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --test children -- test_childkey_set_weights_single_parent --exact --nocapture
3242+
#[test]
3243+
fn test_childkey_set_weights_single_parent() {
3244+
new_test_ext(1).execute_with(|| {
3245+
let netuid: u16 = 1;
3246+
add_network(netuid, 1, 0);
3247+
3248+
// Define hotkeys
3249+
let parent: U256 = U256::from(1);
3250+
let child: U256 = U256::from(2);
3251+
let weight_setter: U256 = U256::from(3);
3252+
3253+
// Define coldkeys with more readable names
3254+
let coldkey_parent: U256 = U256::from(100);
3255+
let coldkey_child: U256 = U256::from(101);
3256+
let coldkey_weight_setter: U256 = U256::from(102);
3257+
3258+
let stake_to_give_child = 109_999;
3259+
3260+
// Register parent with minimal stake and child with high stake
3261+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_parent, 1);
3262+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_child, stake_to_give_child + 10);
3263+
SubtensorModule::add_balance_to_coldkey_account(&coldkey_weight_setter, 1_000_000);
3264+
3265+
// Add neurons for parent, child and weight_setter
3266+
register_ok_neuron(netuid, parent, coldkey_parent, 1);
3267+
register_ok_neuron(netuid, child, coldkey_child, 1);
3268+
register_ok_neuron(netuid, weight_setter, coldkey_weight_setter, 1);
3269+
3270+
SubtensorModule::increase_stake_on_coldkey_hotkey_account(
3271+
&coldkey_parent,
3272+
&parent,
3273+
stake_to_give_child,
3274+
);
3275+
SubtensorModule::increase_stake_on_coldkey_hotkey_account(
3276+
&coldkey_weight_setter,
3277+
&weight_setter,
3278+
1_000_000,
3279+
);
3280+
3281+
SubtensorModule::set_weights_set_rate_limit(netuid, 0);
3282+
3283+
// Set parent-child relationship
3284+
assert_ok!(SubtensorModule::do_set_children(
3285+
RuntimeOrigin::signed(coldkey_parent),
3286+
parent,
3287+
netuid,
3288+
vec![(u64::MAX, child)]
3289+
));
3290+
step_block(7200 + 1);
3291+
// Set weights on the child using the weight_setter account
3292+
let origin = RuntimeOrigin::signed(weight_setter);
3293+
let uids: Vec<u16> = vec![1]; // Only set weight for the child (UID 1)
3294+
let values: Vec<u16> = vec![u16::MAX]; // Use maximum value for u16
3295+
let version_key = SubtensorModule::get_weights_version_key(netuid);
3296+
assert_ok!(SubtensorModule::set_weights(
3297+
origin,
3298+
netuid,
3299+
uids.clone(),
3300+
values.clone(),
3301+
version_key
3302+
));
3303+
3304+
// Set the min stake very high
3305+
SubtensorModule::set_weights_min_stake(stake_to_give_child * 5);
3306+
3307+
// Check the child has less stake than required
3308+
assert!(
3309+
SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid)
3310+
< SubtensorModule::get_weights_min_stake()
3311+
);
3312+
3313+
// Check the child cannot set weights
3314+
assert_noop!(
3315+
SubtensorModule::set_weights(
3316+
RuntimeOrigin::signed(child),
3317+
netuid,
3318+
uids.clone(),
3319+
values.clone(),
3320+
version_key
3321+
),
3322+
Error::<Test>::NotEnoughStakeToSetWeights
3323+
);
3324+
3325+
assert!(!SubtensorModule::check_weights_min_stake(&child, netuid));
3326+
3327+
// Set a minimum stake to set weights
3328+
SubtensorModule::set_weights_min_stake(stake_to_give_child - 5);
3329+
3330+
// Check if the stake for the child is above
3331+
assert!(
3332+
SubtensorModule::get_stake_for_hotkey_on_subnet(&child, netuid)
3333+
>= SubtensorModule::get_weights_min_stake()
3334+
);
3335+
3336+
// Check the child can set weights
3337+
assert_ok!(SubtensorModule::set_weights(
3338+
RuntimeOrigin::signed(child),
3339+
netuid,
3340+
uids,
3341+
values,
3342+
version_key
3343+
));
3344+
3345+
assert!(SubtensorModule::check_weights_min_stake(&child, netuid));
3346+
});
3347+
}

0 commit comments

Comments
 (0)