@@ -5,7 +5,10 @@ use frame_support::{
55 traits:: Hooks ,
66} ;
77use frame_system:: Config ;
8- use pallet_subtensor:: { Error as SubtensorError , SubnetOwner , Tempo , WeightsVersionKeyRateLimit } ;
8+ use pallet_subtensor:: {
9+ Error as SubtensorError , MaxRegistrationsPerBlock , Rank , SubnetOwner ,
10+ TargetRegistrationsPerInterval , Tempo , WeightsVersionKeyRateLimit , * ,
11+ } ;
912// use pallet_subtensor::{migrations, Event};
1013use pallet_subtensor:: Event ;
1114use sp_consensus_grandpa:: AuthorityId as GrandpaId ;
@@ -1951,3 +1954,106 @@ fn test_sudo_set_commit_reveal_version() {
19511954 ) ;
19521955 } ) ;
19531956}
1957+
1958+ #[ test]
1959+ fn test_trim_to_max_allowed_uids ( ) {
1960+ new_test_ext ( ) . execute_with ( || {
1961+ let netuid = NetUid :: from ( 1 ) ;
1962+ add_network ( netuid, 10 ) ;
1963+ MaxRegistrationsPerBlock :: < Test > :: insert ( netuid, 256 ) ;
1964+ TargetRegistrationsPerInterval :: < Test > :: insert ( netuid, 256 ) ;
1965+
1966+ // Add some neurons
1967+ let max_n = 32 ;
1968+ for i in 1 ..=max_n {
1969+ let n = i * 1000 ;
1970+ register_ok_neuron ( netuid, U256 :: from ( n) , U256 :: from ( n + i) , 0 ) ;
1971+ }
1972+
1973+ // Run some block to ensure stake weights are set
1974+ run_to_block ( 20 ) ;
1975+
1976+ // Normal case
1977+ let new_max_n = 20 ;
1978+ assert_ok ! ( AdminUtils :: sudo_trim_to_max_allowed_uids(
1979+ <<Test as Config >:: RuntimeOrigin >:: root( ) ,
1980+ netuid,
1981+ new_max_n
1982+ ) ) ;
1983+
1984+ // Ensure storage has been trimmed
1985+ assert_eq ! ( MaxAllowedUids :: <Test >:: get( netuid) , new_max_n) ;
1986+ assert_eq ! ( Rank :: <Test >:: get( netuid) . len( ) , new_max_n as usize ) ;
1987+ assert_eq ! ( Trust :: <Test >:: get( netuid) . len( ) , new_max_n as usize ) ;
1988+ assert_eq ! ( Active :: <Test >:: get( netuid) . len( ) , new_max_n as usize ) ;
1989+ assert_eq ! ( Emission :: <Test >:: get( netuid) . len( ) , new_max_n as usize ) ;
1990+ assert_eq ! ( Consensus :: <Test >:: get( netuid) . len( ) , new_max_n as usize ) ;
1991+ assert_eq ! ( Incentive :: <Test >:: get( netuid) . len( ) , new_max_n as usize ) ;
1992+ assert_eq ! ( Dividends :: <Test >:: get( netuid) . len( ) , new_max_n as usize ) ;
1993+ assert_eq ! ( LastUpdate :: <Test >:: get( netuid) . len( ) , new_max_n as usize ) ;
1994+ assert_eq ! ( PruningScores :: <Test >:: get( netuid) . len( ) , new_max_n as usize ) ;
1995+ assert_eq ! (
1996+ ValidatorTrust :: <Test >:: get( netuid) . len( ) ,
1997+ new_max_n as usize
1998+ ) ;
1999+ assert_eq ! (
2000+ ValidatorPermit :: <Test >:: get( netuid) . len( ) ,
2001+ new_max_n as usize
2002+ ) ;
2003+ assert_eq ! ( StakeWeight :: <Test >:: get( netuid) . len( ) , new_max_n as usize ) ;
2004+
2005+ for uid in max_n..new_max_n {
2006+ assert ! ( !Keys :: <Test >:: contains_key( netuid, uid) ) ;
2007+ assert ! ( !BlockAtRegistration :: <Test >:: contains_key( netuid, uid) ) ;
2008+ assert ! ( !Weights :: <Test >:: contains_key( netuid, uid) ) ;
2009+ assert ! ( !Bonds :: <Test >:: contains_key( netuid, uid) ) ;
2010+ }
2011+
2012+ for uid in 0 ..max_n {
2013+ assert ! (
2014+ Weights :: <Test >:: get( netuid, uid)
2015+ . iter( )
2016+ . all( |( target_uid, _) | * target_uid < new_max_n) ,
2017+ "Found a weight with target_uid >= new_max_n"
2018+ ) ;
2019+ assert ! (
2020+ Bonds :: <Test >:: get( netuid, uid)
2021+ . iter( )
2022+ . all( |( target_uid, _) | * target_uid < new_max_n) ,
2023+ "Found a bond with target_uid >= new_max_n"
2024+ ) ;
2025+ }
2026+
2027+ assert_eq ! ( SubnetworkN :: <Test >:: get( netuid) , new_max_n) ;
2028+
2029+ // Non existent subnet
2030+ assert_err ! (
2031+ AdminUtils :: sudo_trim_to_max_allowed_uids(
2032+ <<Test as Config >:: RuntimeOrigin >:: root( ) ,
2033+ NetUid :: from( 42 ) ,
2034+ new_max_n
2035+ ) ,
2036+ pallet_subtensor:: Error :: <Test >:: SubNetworkDoesNotExist
2037+ ) ;
2038+
2039+ // New max n less than lower bound
2040+ assert_err ! (
2041+ AdminUtils :: sudo_trim_to_max_allowed_uids(
2042+ <<Test as Config >:: RuntimeOrigin >:: root( ) ,
2043+ netuid,
2044+ 15
2045+ ) ,
2046+ pallet_subtensor:: Error :: <Test >:: InvalidValue
2047+ ) ;
2048+
2049+ // New max n greater than upper bound
2050+ assert_err ! (
2051+ AdminUtils :: sudo_trim_to_max_allowed_uids(
2052+ <<Test as Config >:: RuntimeOrigin >:: root( ) ,
2053+ netuid,
2054+ SubtensorModule :: get_max_allowed_uids( netuid) + 1
2055+ ) ,
2056+ pallet_subtensor:: Error :: <Test >:: InvalidValue
2057+ ) ;
2058+ } ) ;
2059+ }
0 commit comments