Skip to content

Commit 41b6ae6

Browse files
author
Samuel Dare
committed
feat: implement subnetinfov2 , add_network with identities
1 parent 8c17cfd commit 41b6ae6

File tree

10 files changed

+271
-45
lines changed

10 files changed

+271
-45
lines changed

pallets/admin-utils/tests/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1243,7 +1243,7 @@ fn test_sudo_get_set_alpha() {
12431243
DispatchError::BadOrigin
12441244
);
12451245

1246-
assert_ok!(SubtensorModule::register_network(signer.clone(), None));
1246+
assert_ok!(SubtensorModule::register_network(signer.clone()));
12471247

12481248
assert_ok!(AdminUtils::sudo_set_alpha_values(
12491249
signer.clone(),

pallets/subtensor/rpc/src/lib.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ pub trait SubtensorCustomApi<BlockHash> {
4646
fn get_subnet_info(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
4747
#[method(name = "subnetInfo_getSubnetsInfo")]
4848
fn get_subnets_info(&self, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
49+
#[method(name = "subnetInfo_getSubnetInfo_v2")]
50+
fn get_subnet_info_v2(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
51+
#[method(name = "subnetInfo_getSubnetsInf_v2")]
52+
fn get_subnets_info_v2(&self, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
4953
#[method(name = "subnetInfo_getSubnetHyperparams")]
5054
fn get_subnet_hyperparams(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
5155

@@ -215,6 +219,26 @@ where
215219
.map_err(|e| Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into())
216220
}
217221

222+
fn get_subnet_info_v2(
223+
&self,
224+
netuid: u16,
225+
at: Option<<Block as BlockT>::Hash>,
226+
) -> RpcResult<Vec<u8>> {
227+
let api = self.client.runtime_api();
228+
let at = at.unwrap_or_else(|| self.client.info().best_hash);
229+
230+
api.get_subnet_info_v2(at, netuid)
231+
.map_err(|e| Error::RuntimeError(format!("Unable to get subnet info: {:?}", e)).into())
232+
}
233+
234+
fn get_subnets_info_v2(&self, at: Option<<Block as BlockT>::Hash>) -> RpcResult<Vec<u8>> {
235+
let api = self.client.runtime_api();
236+
let at = at.unwrap_or_else(|| self.client.info().best_hash);
237+
238+
api.get_subnets_info_v2(at)
239+
.map_err(|e| Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into())
240+
}
241+
218242
fn get_network_lock_cost(&self, at: Option<<Block as BlockT>::Hash>) -> RpcResult<u64> {
219243
let api = self.client.runtime_api();
220244
let at = at.unwrap_or_else(|| self.client.info().best_hash);

pallets/subtensor/runtime-api/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ sp_api::decl_runtime_apis! {
2121
pub trait SubnetInfoRuntimeApi {
2222
fn get_subnet_info(netuid: u16) -> Vec<u8>;
2323
fn get_subnets_info() -> Vec<u8>;
24+
fn get_subnet_info_v2(netuid: u16) -> Vec<u8>;
25+
fn get_subnets_info_v2() -> Vec<u8>;
2426
fn get_subnet_hyperparams(netuid: u16) -> Vec<u8>;
2527
}
2628

pallets/subtensor/src/coinbase/root.rs

Lines changed: 115 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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: {:?}\nmax 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)]

pallets/subtensor/src/macros/dispatches.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -901,11 +901,8 @@ mod dispatches {
901901
#[pallet::weight((Weight::from_parts(157_000_000, 0)
902902
.saturating_add(T::DbWeight::get().reads(16))
903903
.saturating_add(T::DbWeight::get().writes(30)), DispatchClass::Operational, Pays::No))]
904-
pub fn register_network(
905-
origin: OriginFor<T>,
906-
identity: Option<SubnetIdentityOf>,
907-
) -> DispatchResult {
908-
Self::user_add_network(origin, identity)
904+
pub fn register_network(origin: OriginFor<T>) -> DispatchResult {
905+
Self::user_add_network(origin)
909906
}
910907

911908
/// Facility extrinsic for user to get taken from faucet
@@ -1201,5 +1198,17 @@ mod dispatches {
12011198
) -> DispatchResult {
12021199
Self::do_set_subnet_identity(origin, netuid, subnet_name, github_repo, subnet_contact)
12031200
}
1201+
1202+
/// User register a new subnetwork
1203+
#[pallet::call_index(79)]
1204+
#[pallet::weight((Weight::from_parts(157_000_000, 0)
1205+
.saturating_add(T::DbWeight::get().reads(16))
1206+
.saturating_add(T::DbWeight::get().writes(30)), DispatchClass::Operational, Pays::No))]
1207+
pub fn register_network_with_identity(
1208+
origin: OriginFor<T>,
1209+
identity: Option<SubnetIdentityOf>,
1210+
) -> DispatchResult {
1211+
Self::user_add_network_with_identity(origin, identity)
1212+
}
12041213
}
12051214
}

0 commit comments

Comments
 (0)