@@ -573,4 +573,105 @@ fn test_network_prune_results() {
573573 SubtensorModule :: set_emission_values ( & vec ! [ 1u16 , 2u16 , 3u16 ] , vec ! [ 4u64 , 5u64 , 5u64 ] ) ;
574574 assert_eq ! ( SubtensorModule :: get_subnet_to_prune( ) , 1u16 ) ;
575575 } ) ;
576+ }
577+
578+
579+ #[ test]
580+ fn test_weights_after_network_pruning ( ) {
581+ new_test_ext ( ) . execute_with ( || {
582+ migration:: migrate_create_root_network :: < Test > ( ) ;
583+
584+ assert_eq ! ( SubtensorModule :: get_total_issuance( ) , 0 ) ;
585+
586+ // Set up N subnets, with max N + 1 allowed UIDs
587+ let n: usize = 2 ;
588+ let root_netuid: u16 = 0 ;
589+ SubtensorModule :: set_network_immunity_period ( 3 ) ;
590+ SubtensorModule :: set_max_registrations_per_block ( root_netuid, n as u16 ) ;
591+ SubtensorModule :: set_max_subnets ( n as u16 ) ;
592+ SubtensorModule :: set_weights_set_rate_limit ( root_netuid, 0 as u64 ) ;
593+
594+ // No validators yet.
595+ assert_eq ! ( SubtensorModule :: get_subnetwork_n( root_netuid) , 0 ) ;
596+
597+ for i in 0 ..n {
598+
599+ // Register a validator
600+ let hot: U256 = U256 :: from ( i) ;
601+ let cold: U256 = U256 :: from ( i) ;
602+
603+ SubtensorModule :: add_balance_to_coldkey_account ( & cold, 1_000_000_000_000 ) ;
604+
605+ // Register a network
606+ assert_ok ! ( SubtensorModule :: register_network(
607+ <<Test as Config >:: RuntimeOrigin >:: signed( cold)
608+ ) ) ;
609+
610+ log:: debug!( "Adding network with netuid: {}" , ( i as u16 ) + 1 ) ;
611+ assert ! ( SubtensorModule :: if_subnet_exist( ( i as u16 ) + 1 ) ) ;
612+ step_block ( 3 ) ;
613+ }
614+
615+ // Register a validator in subnet 0
616+ let hot: U256 = U256 :: from ( ( n as u64 ) - 1 ) ;
617+ let cold: U256 = U256 :: from ( ( n as u64 ) - 1 ) ;
618+
619+ assert_ok ! ( SubtensorModule :: root_register(
620+ <<Test as Config >:: RuntimeOrigin >:: signed( cold) ,
621+ hot
622+ ) ) ;
623+ assert_ok ! ( SubtensorModule :: add_stake(
624+ <<Test as Config >:: RuntimeOrigin >:: signed( cold) ,
625+ hot,
626+ 1_000
627+ ) ) ;
628+
629+ // Let's give these subnets some weights
630+ let uids: Vec < u16 > = ( 0 ..( n as u16 ) +1 ) . collect ( ) ;
631+ let values: Vec < u16 > = vec ! [ 4u16 , 2u16 , 6u16 ] ;
632+ log:: info!( "uids set: {:?}" , uids) ;
633+ log:: info!( "values set: {:?}" , values) ;
634+ log:: info!( "In netuid: {:?}" , root_netuid) ;
635+ assert_ok ! ( SubtensorModule :: set_weights(
636+ <<Test as Config >:: RuntimeOrigin >:: signed( hot) ,
637+ root_netuid,
638+ uids,
639+ values,
640+ 0
641+ ) ) ;
642+
643+ log:: info!( "Root network weights before extra network registration: {:?}" , SubtensorModule :: get_root_weights( ) ) ;
644+ log:: info!( "Max subnets: {:?}" , SubtensorModule :: get_max_subnets( ) ) ;
645+ let i = ( n as u16 ) + 1 ;
646+ let hot: U256 = U256 :: from ( i) ;
647+ let cold: U256 = U256 :: from ( i) ;
648+
649+ SubtensorModule :: add_balance_to_coldkey_account ( & cold, 1_000_000_000_000_000_000 ) ;
650+ let subnet_to_prune = SubtensorModule :: get_subnet_to_prune ( ) ;
651+
652+ // Subnet 1 should be pruned here.
653+ assert_eq ! ( subnet_to_prune, 1 ) ;
654+ log:: info!( "Removing subnet: {:?}" , subnet_to_prune) ;
655+
656+ // Check that the weights have been set appropriately.
657+ let latest_weights = SubtensorModule :: get_root_weights ( ) ;
658+ log:: info!( "Weights before register network: {:?}" , latest_weights) ;
659+ // We expect subnet 1 to be deregistered as it is oldest and has lowest emissions
660+ assert_eq ! ( latest_weights[ 0 ] [ 1 ] , 21845 ) ;
661+
662+ assert_ok ! ( SubtensorModule :: register_network(
663+ <<Test as Config >:: RuntimeOrigin >:: signed( cold)
664+ ) ) ;
665+
666+ // Subnet should not exist, as it would replace a previous subnet.
667+ assert ! ( !SubtensorModule :: if_subnet_exist( ( i as u16 ) + 1 ) ) ;
668+
669+ log:: info!( "Root network weights: {:?}" , SubtensorModule :: get_root_weights( ) ) ;
670+
671+ let latest_weights = SubtensorModule :: get_root_weights ( ) ;
672+ log:: info!( "Weights after register network: {:?}" , SubtensorModule :: get_root_weights( ) ) ;
673+
674+ // Subnet 0 should be kicked, and thus its weight should be 0
675+ assert_eq ! ( latest_weights[ 0 ] [ 1 ] , 0 ) ;
676+ } ) ;
576677}
0 commit comments