@@ -3237,3 +3237,194 @@ fn test_rank_trust_incentive_calculation_with_parent_child() {
3237
3237
3238
3238
} ) ;
3239
3239
}
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
+ }
3348
+
3349
+ // SKIP_WASM_BUILD=1 RUST_LOG=debug cargo test --package pallet-subtensor --test children -- test_set_weights_no_parent --exact --nocapture
3350
+ #[ test]
3351
+ fn test_set_weights_no_parent ( ) {
3352
+ // Verify that a regular key without a parent delegation is effected by the minimum stake requirements
3353
+ new_test_ext ( 1 ) . execute_with ( || {
3354
+ let netuid: u16 = 1 ;
3355
+ add_network ( netuid, 1 , 0 ) ;
3356
+
3357
+ let hotkey: U256 = U256 :: from ( 2 ) ;
3358
+ let spare_hk: U256 = U256 :: from ( 3 ) ;
3359
+
3360
+ let coldkey: U256 = U256 :: from ( 101 ) ;
3361
+ let spare_ck = U256 :: from ( 102 ) ;
3362
+
3363
+ let stake_to_give_child = 109_999 ;
3364
+
3365
+ SubtensorModule :: add_balance_to_coldkey_account ( & coldkey, stake_to_give_child + 10 ) ;
3366
+
3367
+ // Is registered
3368
+ register_ok_neuron ( netuid, hotkey, coldkey, 1 ) ;
3369
+ // Register a spare key
3370
+ register_ok_neuron ( netuid, spare_hk, spare_ck, 1 ) ;
3371
+
3372
+ SubtensorModule :: increase_stake_on_coldkey_hotkey_account (
3373
+ & coldkey,
3374
+ & hotkey,
3375
+ stake_to_give_child,
3376
+ ) ;
3377
+
3378
+ SubtensorModule :: set_weights_set_rate_limit ( netuid, 0 ) ;
3379
+
3380
+ // Has stake and no parent
3381
+ step_block ( 7200 + 1 ) ;
3382
+
3383
+ let uids: Vec < u16 > = vec ! [ 1 ] ; // Set weights on the other hotkey
3384
+ let values: Vec < u16 > = vec ! [ u16 :: MAX ] ; // Use maximum value for u16
3385
+ let version_key = SubtensorModule :: get_weights_version_key ( netuid) ;
3386
+
3387
+ // Set the min stake very high
3388
+ SubtensorModule :: set_weights_min_stake ( stake_to_give_child * 5 ) ;
3389
+
3390
+ // Check the key has less stake than required
3391
+ assert ! (
3392
+ SubtensorModule :: get_stake_for_hotkey_on_subnet( & hotkey, netuid)
3393
+ < SubtensorModule :: get_weights_min_stake( )
3394
+ ) ;
3395
+
3396
+ // Check the hotkey cannot set weights
3397
+ assert_noop ! (
3398
+ SubtensorModule :: set_weights(
3399
+ RuntimeOrigin :: signed( hotkey) ,
3400
+ netuid,
3401
+ uids. clone( ) ,
3402
+ values. clone( ) ,
3403
+ version_key
3404
+ ) ,
3405
+ Error :: <Test >:: NotEnoughStakeToSetWeights
3406
+ ) ;
3407
+
3408
+ assert ! ( !SubtensorModule :: check_weights_min_stake( & hotkey, netuid) ) ;
3409
+
3410
+ // Set a minimum stake to set weights
3411
+ SubtensorModule :: set_weights_min_stake ( stake_to_give_child - 5 ) ;
3412
+
3413
+ // Check if the stake for the hotkey is above
3414
+ assert ! (
3415
+ SubtensorModule :: get_stake_for_hotkey_on_subnet( & hotkey, netuid)
3416
+ >= SubtensorModule :: get_weights_min_stake( )
3417
+ ) ;
3418
+
3419
+ // Check the hotkey can set weights
3420
+ assert_ok ! ( SubtensorModule :: set_weights(
3421
+ RuntimeOrigin :: signed( hotkey) ,
3422
+ netuid,
3423
+ uids,
3424
+ values,
3425
+ version_key
3426
+ ) ) ;
3427
+
3428
+ assert ! ( SubtensorModule :: check_weights_min_stake( & hotkey, netuid) ) ;
3429
+ } ) ;
3430
+ }
0 commit comments