@@ -895,6 +895,100 @@ impl<T: Config> Pallet<T> {
895895 ///
896896 /// # Args:
897897 /// * `origin` (`T::RuntimeOrigin`): The calling origin. Must be signed.
898+ ///
899+ /// # Events:
900+ /// * `NetworkAdded(netuid, modality)`: Emitted when a new network is successfully added.
901+ /// * `NetworkRemoved(netuid)`: Emitted when an existing network is removed to make room for the new one.
902+ ///
903+ /// # Raises:
904+ /// * 'TxRateLimitExceeded': If the rate limit for network registration is exceeded.
905+ /// * 'NotEnoughBalanceToStake': If there isn't enough balance to stake for network registration.
906+ /// * 'BalanceWithdrawalError': If an error occurs during balance withdrawal for network registration.
907+ ///
908+ pub fn user_add_network ( origin : T :: RuntimeOrigin ) -> dispatch:: DispatchResult {
909+ // --- 0. Ensure the caller is a signed user.
910+ let coldkey = ensure_signed ( origin) ?;
911+
912+ // --- 1. Rate limit for network registrations.
913+ let current_block = Self :: get_current_block_as_u64 ( ) ;
914+ let last_lock_block = Self :: get_network_last_lock_block ( ) ;
915+ ensure ! (
916+ current_block. saturating_sub( last_lock_block) >= NetworkRateLimit :: <T >:: get( ) ,
917+ Error :: <T >:: NetworkTxRateLimitExceeded
918+ ) ;
919+
920+ // --- 2. Calculate and lock the required tokens.
921+ let lock_amount: u64 = Self :: get_network_lock_cost ( ) ;
922+ log:: debug!( "network lock_amount: {:?}" , lock_amount) ;
923+ ensure ! (
924+ Self :: can_remove_balance_from_coldkey_account( & coldkey, lock_amount) ,
925+ Error :: <T >:: NotEnoughBalanceToStake
926+ ) ;
927+
928+ // --- 4. Determine the netuid to register.
929+ let netuid_to_register: u16 = {
930+ log:: debug!(
931+ "subnet count: {:?}\n max subnets: {:?}" ,
932+ Self :: get_num_subnets( ) ,
933+ Self :: get_max_subnets( )
934+ ) ;
935+ if Self :: get_num_subnets ( ) . saturating_sub ( 1 ) < Self :: get_max_subnets ( ) {
936+ // We subtract one because we don't want root subnet to count towards total
937+ let mut next_available_netuid = 0 ;
938+ loop {
939+ next_available_netuid. saturating_inc ( ) ;
940+ if !Self :: if_subnet_exist ( next_available_netuid) {
941+ log:: debug!( "got subnet id: {:?}" , next_available_netuid) ;
942+ break next_available_netuid;
943+ }
944+ }
945+ } else {
946+ let netuid_to_prune = Self :: get_subnet_to_prune ( ) ;
947+ ensure ! ( netuid_to_prune > 0 , Error :: <T >:: AllNetworksInImmunity ) ;
948+
949+ Self :: remove_network ( netuid_to_prune) ;
950+ log:: debug!( "remove_network: {:?}" , netuid_to_prune, ) ;
951+ Self :: deposit_event ( Event :: NetworkRemoved ( netuid_to_prune) ) ;
952+
953+ if SubnetIdentities :: < T > :: take ( netuid_to_prune) . is_some ( ) {
954+ Self :: deposit_event ( Event :: SubnetIdentityRemoved ( netuid_to_prune) ) ;
955+ }
956+
957+ netuid_to_prune
958+ }
959+ } ;
960+
961+ // --- 5. Perform the lock operation.
962+ let actual_lock_amount = Self :: remove_balance_from_coldkey_account ( & coldkey, lock_amount) ?;
963+ Self :: set_subnet_locked_balance ( netuid_to_register, actual_lock_amount) ;
964+ Self :: set_network_last_lock ( actual_lock_amount) ;
965+
966+ // --- 6. Set initial and custom parameters for the network.
967+ Self :: init_new_network ( netuid_to_register, 360 ) ;
968+ log:: debug!( "init_new_network: {:?}" , netuid_to_register, ) ;
969+
970+ // --- 7. Set netuid storage.
971+ let current_block_number: u64 = Self :: get_current_block_as_u64 ( ) ;
972+ NetworkLastRegistered :: < T > :: set ( current_block_number) ;
973+ NetworkRegisteredAt :: < T > :: insert ( netuid_to_register, current_block_number) ;
974+ SubnetOwner :: < T > :: insert ( netuid_to_register, coldkey) ;
975+
976+ // --- 8. Emit the NetworkAdded event.
977+ log:: debug!(
978+ "NetworkAdded( netuid:{:?}, modality:{:?} )" ,
979+ netuid_to_register,
980+ 0
981+ ) ;
982+ Self :: deposit_event ( Event :: NetworkAdded ( netuid_to_register, 0 ) ) ;
983+
984+ // --- 9. Return success.
985+ Ok ( ( ) )
986+ }
987+
988+ /// Facilitates user registration of a new subnetwork with subnet identity.
989+ ///
990+ /// # Args:
991+ /// * `origin` (`T::RuntimeOrigin`): The calling origin. Must be signed.
898992 /// * `identity` (`Option<SubnetIdentityOf>`): Optional identity to be associated with the new subnetwork.
899993 ///
900994 /// # Events:
@@ -908,7 +1002,7 @@ impl<T: Config> Pallet<T> {
9081002 /// * 'NotEnoughBalanceToStake': If there isn't enough balance to stake for network registration.
9091003 /// * 'BalanceWithdrawalError': If an error occurs during balance withdrawal for network registration.
9101004 ///
911- pub fn user_add_network (
1005+ pub fn user_add_network_with_identity (
9121006 origin : T :: RuntimeOrigin ,
9131007 identity : Option < SubnetIdentityOf > ,
9141008 ) -> dispatch:: DispatchResult {
@@ -1126,8 +1220,8 @@ impl<T: Config> Pallet<T> {
11261220 /// Removes a network (identified by netuid) and all associated parameters.
11271221 ///
11281222 /// This function is responsible for cleaning up all the data associated with a network.
1129- /// It ensures that all the storage values related to the network are removed, and any
1130- /// reserved balance is returned to the network owner.
1223+ /// It ensures that all the storage values related to the network are removed, any
1224+ /// reserved balance is returned to the network owner, and the subnet identity is removed if it exists .
11311225 ///
11321226 /// # Args:
11331227 /// * 'netuid': ('u16'): The unique identifier of the network to be removed.
@@ -1136,10 +1230,15 @@ impl<T: Config> Pallet<T> {
11361230 /// This function does not emit any events, nor does it raise any errors. It silently
11371231 /// returns if any internal checks fail.
11381232 ///
1233+ /// # Example:
1234+ /// ```rust
1235+ /// let netuid_to_remove: u16 = 5;
1236+ /// Pallet::<T>::remove_network(netuid_to_remove);
1237+ /// ```
11391238 pub fn remove_network ( netuid : u16 ) {
11401239 // --- 1. Return balance to subnet owner.
1141- let owner_coldkey = SubnetOwner :: < T > :: get ( netuid) ;
1142- let reserved_amount = Self :: get_subnet_locked_balance ( netuid) ;
1240+ let owner_coldkey: T :: AccountId = SubnetOwner :: < T > :: get ( netuid) ;
1241+ let reserved_amount: u64 = Self :: get_subnet_locked_balance ( netuid) ;
11431242
11441243 // --- 2. Remove network count.
11451244 SubnetworkN :: < T > :: remove ( netuid) ;
@@ -1150,13 +1249,13 @@ impl<T: Config> Pallet<T> {
11501249 // --- 4. Remove netuid from added networks.
11511250 NetworksAdded :: < T > :: remove ( netuid) ;
11521251
1153- // --- 6 . Decrement the network counter.
1154- TotalNetworks :: < T > :: mutate ( |n| * n = n. saturating_sub ( 1 ) ) ;
1252+ // --- 5 . Decrement the network counter.
1253+ TotalNetworks :: < T > :: mutate ( |n : & mut u16 | * n = n. saturating_sub ( 1 ) ) ;
11551254
1156- // --- 7 . Remove various network-related storages.
1255+ // --- 6 . Remove various network-related storages.
11571256 NetworkRegisteredAt :: < T > :: remove ( netuid) ;
11581257
1159- // --- 8 . Remove incentive mechanism memory.
1258+ // --- 7 . Remove incentive mechanism memory.
11601259 let _ = Uids :: < T > :: clear_prefix ( netuid, u32:: MAX , None ) ;
11611260 let _ = Keys :: < T > :: clear_prefix ( netuid, u32:: MAX , None ) ;
11621261 let _ = Bonds :: < T > :: clear_prefix ( netuid, u32:: MAX , None ) ;
@@ -1171,7 +1270,7 @@ impl<T: Config> Pallet<T> {
11711270 )
11721271 {
11731272 // Create a new vector to hold modified weights.
1174- let mut modified_weights = weights_i. clone ( ) ;
1273+ let mut modified_weights: Vec < ( u16 , u16 ) > = weights_i. clone ( ) ;
11751274 // Iterate over each weight entry to potentially update it.
11761275 for ( subnet_id, weight) in modified_weights. iter_mut ( ) {
11771276 if subnet_id == & netuid {
@@ -1213,6 +1312,12 @@ impl<T: Config> Pallet<T> {
12131312 Self :: add_balance_to_coldkey_account ( & owner_coldkey, reserved_amount) ;
12141313 Self :: set_subnet_locked_balance ( netuid, 0 ) ;
12151314 SubnetOwner :: < T > :: remove ( netuid) ;
1315+
1316+ // --- 13. Remove subnet identity if it exists.
1317+ if SubnetIdentities :: < T > :: contains_key ( netuid) {
1318+ SubnetIdentities :: < T > :: remove ( netuid) ;
1319+ Self :: deposit_event ( Event :: SubnetIdentityRemoved ( netuid) ) ;
1320+ }
12161321 }
12171322
12181323 #[ allow( clippy:: arithmetic_side_effects) ]
0 commit comments