@@ -999,7 +999,7 @@ fn test_childkey_take_rate_limiting() {
999
999
& hotkey,
1000
1000
netuid,
1001
1001
) ;
1002
- let limit = SubtensorModule :: get_rate_limit ( & TransactionType :: SetChildkeyTake ) ;
1002
+ let limit = SubtensorModule :: get_rate_limit ( & TransactionType :: SetChildkeyTake , 0 ) ;
1003
1003
log:: info!(
1004
1004
"Rate limit info: current_block: {}, last_block: {}, limit: {}, passes: {}, diff: {}" ,
1005
1005
current_block,
@@ -3702,3 +3702,78 @@ fn test_childkey_take_drain_validator_take() {
3702
3702
) ) ;
3703
3703
} ) ;
3704
3704
}
3705
+
3706
+ // 60: Test set_children rate limiting - Fail then succeed
3707
+ // This test ensures that an immediate second `set_children` transaction fails due to rate limiting:
3708
+ // - Sets up a network and registers a hotkey
3709
+ // - Performs a `set_children` transaction
3710
+ // - Attempts a second `set_children` transaction immediately
3711
+ // - Verifies that the second transaction fails with `TxRateLimitExceeded`
3712
+ // Then the rate limit period passes and the second transaction succeeds
3713
+ // - Steps blocks for the rate limit period
3714
+ // - Attempts the second transaction again and verifies it succeeds
3715
+ // SKIP_WASM_BUILD=1 RUST_LOG=info cargo test --test children -- test_set_children_rate_limit_fail_then_succeed --exact --nocapture
3716
+ #[ test]
3717
+ fn test_set_children_rate_limit_fail_then_succeed ( ) {
3718
+ new_test_ext ( 1 ) . execute_with ( || {
3719
+ let coldkey = U256 :: from ( 1 ) ;
3720
+ let hotkey = U256 :: from ( 2 ) ;
3721
+ let child = U256 :: from ( 3 ) ;
3722
+ let child2 = U256 :: from ( 4 ) ;
3723
+ let netuid: u16 = 1 ;
3724
+ let tempo = 13 ;
3725
+
3726
+ // Add network and register hotkey
3727
+ add_network ( netuid, tempo, 0 ) ;
3728
+ register_ok_neuron ( netuid, hotkey, coldkey, 0 ) ;
3729
+
3730
+ // First set_children transaction
3731
+ assert_ok ! ( SubtensorModule :: do_set_children(
3732
+ RuntimeOrigin :: signed( coldkey) ,
3733
+ hotkey,
3734
+ netuid,
3735
+ vec![ ( 100 , child) ]
3736
+ ) ) ;
3737
+
3738
+ // Immediate second transaction should fail due to rate limit
3739
+ assert_noop ! (
3740
+ SubtensorModule :: do_set_children(
3741
+ RuntimeOrigin :: signed( coldkey) ,
3742
+ hotkey,
3743
+ netuid,
3744
+ vec![ ( 100 , child2) ]
3745
+ ) ,
3746
+ Error :: <Test >:: TxRateLimitExceeded
3747
+ ) ;
3748
+
3749
+ // Verify first children assignment remains
3750
+ let children = SubtensorModule :: get_children ( & hotkey, netuid) ;
3751
+ assert_eq ! ( children, vec![ ( 100 , child) ] ) ;
3752
+
3753
+ // Try again after rate limit period has passed
3754
+ // Check rate limit
3755
+ let limit = SubtensorModule :: get_rate_limit ( & TransactionType :: SetChildren , netuid) ;
3756
+
3757
+ // Step that many blocks
3758
+ step_block ( limit as u16 ) ;
3759
+
3760
+ // Verify rate limit passes
3761
+ assert ! ( SubtensorModule :: passes_rate_limit_on_subnet(
3762
+ & TransactionType :: SetChildren ,
3763
+ & hotkey,
3764
+ netuid
3765
+ ) ) ;
3766
+
3767
+ // Try again
3768
+ assert_ok ! ( SubtensorModule :: do_set_children(
3769
+ RuntimeOrigin :: signed( coldkey) ,
3770
+ hotkey,
3771
+ netuid,
3772
+ vec![ ( 100 , child2) ]
3773
+ ) ) ;
3774
+
3775
+ // Verify children assignment has changed
3776
+ let children = SubtensorModule :: get_children ( & hotkey, netuid) ;
3777
+ assert_eq ! ( children, vec![ ( 100 , child2) ] ) ;
3778
+ } ) ;
3779
+ }
0 commit comments