Skip to content

Commit

Permalink
Merge pull request #753 from opentensor/spiigot/subnet_info_versioning
Browse files Browse the repository at this point in the history
Spiigot Subnet Info Versioning
  • Loading branch information
JohnReedV authored Aug 26, 2024
2 parents 41b6ae6 + 5af9888 commit 5567133
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 109 deletions.
8 changes: 4 additions & 4 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ benchmarks! {
let amount: u64 = 1;
let amount_to_be_staked = 100_000_000_000_000u64;
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), amount_to_be_staked);
}: register_network(RawOrigin::Signed(coldkey), None)
}: register_network(RawOrigin::Signed(coldkey))

benchmark_dissolve_network {
let seed : u32 = 1;
Expand All @@ -311,8 +311,8 @@ benchmarks! {
let amount: u64 = 1;
let amount_to_be_staked = 100_000_000_000_000u64;
Subtensor::<T>::add_balance_to_coldkey_account(&coldkey.clone(), amount_to_be_staked);
assert_ok!(Subtensor::<T>::register_network(RawOrigin::Signed(coldkey.clone()).into(), None));
}: dissolve_network(RawOrigin::Signed(coldkey), 1)
assert_ok!(Subtensor::<T>::register_network(RawOrigin::Signed(coldkey.clone()).into()));
}: dissolve_network(RawOrigin::Root, coldkey.clone(), 1)


// swap_hotkey {
Expand Down Expand Up @@ -519,6 +519,6 @@ reveal_weights {
Identities::<T>::insert(&old_coldkey, identity);

// Benchmark setup complete, now execute the extrinsic
}: swap_coldkey(RawOrigin::Signed(old_coldkey.clone()), old_coldkey.clone(), new_coldkey.clone())
}: swap_coldkey(RawOrigin::Root, old_coldkey.clone(), new_coldkey.clone())

}
102 changes: 1 addition & 101 deletions pallets/subtensor/src/coinbase/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,100 +891,6 @@ impl<T: Config> Pallet<T> {
.into())
}

/// Facilitates user registration of a new subnetwork.
///
/// # Args:
/// * `origin` (`T::RuntimeOrigin`): The calling origin. Must be signed.
///
/// # Events:
/// * `NetworkAdded(netuid, modality)`: Emitted when a new network is successfully added.
/// * `NetworkRemoved(netuid)`: Emitted when an existing network is removed to make room for the new one.
///
/// # Raises:
/// * 'TxRateLimitExceeded': If the rate limit for network registration is exceeded.
/// * 'NotEnoughBalanceToStake': If there isn't enough balance to stake for network registration.
/// * 'BalanceWithdrawalError': If an error occurs during balance withdrawal for network registration.
///
pub fn user_add_network(origin: T::RuntimeOrigin) -> dispatch::DispatchResult {
// --- 0. Ensure the caller is a signed user.
let coldkey = ensure_signed(origin)?;

// --- 1. Rate limit for network registrations.
let current_block = Self::get_current_block_as_u64();
let last_lock_block = Self::get_network_last_lock_block();
ensure!(
current_block.saturating_sub(last_lock_block) >= NetworkRateLimit::<T>::get(),
Error::<T>::NetworkTxRateLimitExceeded
);

// --- 2. Calculate and lock the required tokens.
let lock_amount: u64 = Self::get_network_lock_cost();
log::debug!("network lock_amount: {:?}", lock_amount);
ensure!(
Self::can_remove_balance_from_coldkey_account(&coldkey, lock_amount),
Error::<T>::NotEnoughBalanceToStake
);

// --- 4. Determine the netuid to register.
let netuid_to_register: u16 = {
log::debug!(
"subnet count: {:?}\nmax subnets: {:?}",
Self::get_num_subnets(),
Self::get_max_subnets()
);
if Self::get_num_subnets().saturating_sub(1) < Self::get_max_subnets() {
// We subtract one because we don't want root subnet to count towards total
let mut next_available_netuid = 0;
loop {
next_available_netuid.saturating_inc();
if !Self::if_subnet_exist(next_available_netuid) {
log::debug!("got subnet id: {:?}", next_available_netuid);
break next_available_netuid;
}
}
} else {
let netuid_to_prune = Self::get_subnet_to_prune();
ensure!(netuid_to_prune > 0, Error::<T>::AllNetworksInImmunity);

Self::remove_network(netuid_to_prune);
log::debug!("remove_network: {:?}", netuid_to_prune,);
Self::deposit_event(Event::NetworkRemoved(netuid_to_prune));

if SubnetIdentities::<T>::take(netuid_to_prune).is_some() {
Self::deposit_event(Event::SubnetIdentityRemoved(netuid_to_prune));
}

netuid_to_prune
}
};

// --- 5. Perform the lock operation.
let actual_lock_amount = Self::remove_balance_from_coldkey_account(&coldkey, lock_amount)?;
Self::set_subnet_locked_balance(netuid_to_register, actual_lock_amount);
Self::set_network_last_lock(actual_lock_amount);

// --- 6. Set initial and custom parameters for the network.
Self::init_new_network(netuid_to_register, 360);
log::debug!("init_new_network: {:?}", netuid_to_register,);

// --- 7. Set netuid storage.
let current_block_number: u64 = Self::get_current_block_as_u64();
NetworkLastRegistered::<T>::set(current_block_number);
NetworkRegisteredAt::<T>::insert(netuid_to_register, current_block_number);
SubnetOwner::<T>::insert(netuid_to_register, coldkey);

// --- 8. Emit the NetworkAdded event.
log::debug!(
"NetworkAdded( netuid:{:?}, modality:{:?} )",
netuid_to_register,
0
);
Self::deposit_event(Event::NetworkAdded(netuid_to_register, 0));

// --- 9. Return success.
Ok(())
}

/// Facilitates user registration of a new subnetwork with subnet identity.
///
/// # Args:
Expand All @@ -1002,7 +908,7 @@ impl<T: Config> Pallet<T> {
/// * 'NotEnoughBalanceToStake': If there isn't enough balance to stake for network registration.
/// * 'BalanceWithdrawalError': If an error occurs during balance withdrawal for network registration.
///
pub fn user_add_network_with_identity(
pub fn user_add_network(
origin: T::RuntimeOrigin,
identity: Option<SubnetIdentityOf>,
) -> dispatch::DispatchResult {
Expand Down Expand Up @@ -1229,12 +1135,6 @@ impl<T: Config> Pallet<T> {
/// # Note:
/// This function does not emit any events, nor does it raise any errors. It silently
/// returns if any internal checks fail.
///
/// # Example:
/// ```rust
/// let netuid_to_remove: u16 = 5;
/// Pallet::<T>::remove_network(netuid_to_remove);
/// ```
pub fn remove_network(netuid: u16) {
// --- 1. Return balance to subnet owner.
let owner_coldkey: T::AccountId = SubnetOwner::<T>::get(netuid);
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/macros/dispatches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ mod dispatches {
.saturating_add(T::DbWeight::get().reads(16))
.saturating_add(T::DbWeight::get().writes(30)), DispatchClass::Operational, Pays::No))]
pub fn register_network(origin: OriginFor<T>) -> DispatchResult {
Self::user_add_network(origin)
Self::user_add_network(origin, None)
}

/// Facility extrinsic for user to get taken from faucet
Expand Down Expand Up @@ -1208,7 +1208,7 @@ mod dispatches {
origin: OriginFor<T>,
identity: Option<SubnetIdentityOf>,
) -> DispatchResult {
Self::user_add_network_with_identity(origin, identity)
Self::user_add_network(origin, identity)
}
}
}
4 changes: 2 additions & 2 deletions pallets/subtensor/tests/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,15 +1012,15 @@ fn test_user_add_network_with_identity_fields_ok() {

SubtensorModule::add_balance_to_coldkey_account(&coldkey_1, balance_1);

assert_ok!(SubtensorModule::user_add_network_with_identity(
assert_ok!(SubtensorModule::user_add_network(
RuntimeOrigin::signed(coldkey_1),
Some(identity_value_1.clone())
));

let balance_2 = SubtensorModule::get_network_lock_cost() + 10_000;
SubtensorModule::add_balance_to_coldkey_account(&coldkey_2, balance_2);

assert_ok!(SubtensorModule::user_add_network_with_identity(
assert_ok!(SubtensorModule::user_add_network(
RuntimeOrigin::signed(coldkey_2),
Some(identity_value_2.clone())
));
Expand Down

0 comments on commit 5567133

Please sign in to comment.