Skip to content

Commit cac9b44

Browse files
author
Samuel Dare
committed
Merge branch 'development' into sudo-calls-commit-reveal
2 parents b59e28c + 8b91ed4 commit cac9b44

File tree

13 files changed

+379
-266
lines changed

13 files changed

+379
-266
lines changed

.github/workflows/publish-tag.yml

Lines changed: 0 additions & 102 deletions
This file was deleted.

pallets/admin-utils/tests/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ parameter_types! {
7676
pub const InitialStakePruningMin: u16 = 0;
7777
pub const InitialFoundationDistribution: u64 = 0;
7878
pub const InitialDefaultTake: u16 = 11_796; // 18% honest number.
79-
pub const InitialMinTake: u16 = 0;
79+
pub const InitialMinTake: u16 = 5_898; // 9%;
8080
pub const InitialWeightsVersionKey: u16 = 0;
8181
pub const InitialServingRateLimit: u64 = 0; // No limit.
8282
pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing

pallets/admin-utils/tests/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -945,7 +945,7 @@ mod sudo_set_nominator_min_required_stake {
945945
assert_ok!(SubtensorModule::do_become_delegate(
946946
<<Test as Config>::RuntimeOrigin>::signed(cold1),
947947
hot1,
948-
0
948+
u16::MAX / 10
949949
));
950950
assert_eq!(SubtensorModule::get_owning_coldkey_for_hotkey(&hot1), cold1);
951951

@@ -954,7 +954,7 @@ mod sudo_set_nominator_min_required_stake {
954954
assert_ok!(SubtensorModule::do_become_delegate(
955955
<<Test as Config>::RuntimeOrigin>::signed(cold2),
956956
hot2,
957-
0
957+
u16::MAX / 10
958958
));
959959
assert_eq!(SubtensorModule::get_owning_coldkey_for_hotkey(&hot2), cold2);
960960

pallets/subtensor/src/block_step.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ impl<T: Config> Pallet<T> {
109109
pub fn generate_emission(block_number: u64) {
110110
// --- 1. Iterate across each network and add pending emission into stash.
111111
for (netuid, tempo) in <Tempo<T> as IterableStorageMap<u16, u16>>::iter() {
112-
// Skip the root network.
113-
if netuid == Self::get_root_netuid() {
114-
// Root emission is burned.
112+
// Skip the root network or subnets with registrations turned off
113+
if netuid == Self::get_root_netuid() || !Self::is_registration_allowed(netuid) {
114+
// Root emission or subnet emission is burned
115115
continue;
116116
}
117117

pallets/subtensor/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2244,7 +2244,7 @@ where
22442244
Pallet::<T>::get_registrations_this_interval(*netuid);
22452245
let max_registrations_per_interval =
22462246
Pallet::<T>::get_target_registrations_per_interval(*netuid);
2247-
if registrations_this_interval >= max_registrations_per_interval {
2247+
if registrations_this_interval >= (max_registrations_per_interval * 3) {
22482248
// If the registration limit for the interval is exceeded, reject the transaction
22492249
return InvalidTransaction::ExhaustsResources.into();
22502250
}

pallets/subtensor/src/root.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,24 @@ impl<T: Config> Pallet<T> {
288288
Self::deposit_event(Event::NetworkRateLimitSet(limit));
289289
}
290290

291+
/// Checks if registrations are allowed for a given subnet.
292+
///
293+
/// This function retrieves the subnet hyperparameters for the specified subnet and checks the `registration_allowed` flag.
294+
/// If the subnet doesn't exist or doesn't have hyperparameters defined, it returns `false`.
295+
///
296+
/// # Arguments
297+
///
298+
/// * `netuid` - The unique identifier of the subnet.
299+
///
300+
/// # Returns
301+
///
302+
/// * `bool` - `true` if registrations are allowed for the subnet, `false` otherwise.
303+
pub fn is_registration_allowed(netuid: u16) -> bool {
304+
Self::get_subnet_hyperparams(netuid)
305+
.map(|params| params.registration_allowed)
306+
.unwrap_or(false)
307+
}
308+
291309
/// Computes and sets emission values for the root network which determine the emission for all subnets.
292310
///
293311
/// This function is responsible for calculating emission based on network weights, stake values,

pallets/subtensor/src/staking.rs

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -432,26 +432,18 @@ impl<T: Config> Pallet<T> {
432432
Error::<T>::UnstakeRateLimitExceeded
433433
);
434434

435-
// If this is a nomination stake, check if total stake after removing will be above
436-
// the minimum required stake.
437-
438-
// If coldkey is not owner of the hotkey, it's a nomination stake.
439-
if !Self::coldkey_owns_hotkey(&coldkey, &hotkey) {
440-
let total_stake_after_remove =
441-
Stake::<T>::get(&hotkey, &coldkey).saturating_sub(stake_to_be_removed);
442-
443-
ensure!(
444-
total_stake_after_remove >= NominatorMinRequiredStake::<T>::get(),
445-
Error::<T>::NomStakeBelowMinimumThreshold
446-
);
447-
}
448-
449435
// We remove the balance from the hotkey.
450436
Self::decrease_stake_on_coldkey_hotkey_account(&coldkey, &hotkey, stake_to_be_removed);
451437

452-
// We add the balancer to the coldkey. If the above fails we will not credit this coldkey.
438+
// We add the balance to the coldkey. If the above fails we will not credit this coldkey.
453439
Self::add_balance_to_coldkey_account(&coldkey, stake_to_be_removed);
454440

441+
// If the stake is below the minimum, we clear the nomination from storage.
442+
// This only applies to nominator stakes.
443+
// If the coldkey does not own the hotkey, it's a nominator stake.
444+
let new_stake = Self::get_stake_for_coldkey_and_hotkey(&coldkey, &hotkey);
445+
Self::clear_small_nomination_if_required(&hotkey, &coldkey, new_stake);
446+
455447
// Set last block for rate limiting
456448
let block: u64 = Self::get_current_block_as_u64();
457449
Self::set_last_tx_block(&coldkey, block);
@@ -675,17 +667,24 @@ impl<T: Config> Pallet<T> {
675667
/// It also removes the stake entry for the hotkey-coldkey pairing and adjusts the TotalStake
676668
/// and TotalIssuance by subtracting the removed stake amount.
677669
///
670+
/// Returns the amount of stake that was removed.
671+
///
678672
/// # Arguments
679673
///
680674
/// * `coldkey` - A reference to the AccountId of the coldkey involved in the staking.
681675
/// * `hotkey` - A reference to the AccountId of the hotkey associated with the coldkey.
682-
pub fn empty_stake_on_coldkey_hotkey_account(coldkey: &T::AccountId, hotkey: &T::AccountId) {
676+
pub fn empty_stake_on_coldkey_hotkey_account(
677+
coldkey: &T::AccountId,
678+
hotkey: &T::AccountId,
679+
) -> u64 {
683680
let current_stake: u64 = Stake::<T>::get(hotkey, coldkey);
684681
TotalColdkeyStake::<T>::mutate(coldkey, |old| *old = old.saturating_sub(current_stake));
685682
TotalHotkeyStake::<T>::mutate(hotkey, |stake| *stake = stake.saturating_sub(current_stake));
686683
Stake::<T>::remove(hotkey, coldkey);
687684
TotalStake::<T>::mutate(|stake| *stake = stake.saturating_sub(current_stake));
688685
TotalIssuance::<T>::mutate(|issuance| *issuance = issuance.saturating_sub(current_stake));
686+
687+
current_stake
689688
}
690689

691690
/// Clears the nomination for an account, if it is a nominator account and the stake is below the minimum required threshold.
@@ -700,9 +699,9 @@ impl<T: Config> Pallet<T> {
700699
if stake < Self::get_nominator_min_required_stake() {
701700
// Remove the stake from the nominator account. (this is a more forceful unstake operation which )
702701
// Actually deletes the staking account.
703-
Self::empty_stake_on_coldkey_hotkey_account(coldkey, hotkey);
702+
let cleared_stake = Self::empty_stake_on_coldkey_hotkey_account(coldkey, hotkey);
704703
// Add the stake to the coldkey account.
705-
Self::add_balance_to_coldkey_account(coldkey, stake);
704+
Self::add_balance_to_coldkey_account(coldkey, cleared_stake);
706705
}
707706
}
708707
}

pallets/subtensor/src/subnet_info.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct SubnetHyperparams {
4040
weights_rate_limit: Compact<u64>,
4141
adjustment_interval: Compact<u16>,
4242
activity_cutoff: Compact<u16>,
43-
registration_allowed: bool,
43+
pub registration_allowed: bool,
4444
target_regs_per_interval: Compact<u16>,
4545
min_burn: Compact<u64>,
4646
max_burn: Compact<u64>,

pallets/subtensor/tests/block_step.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,3 +803,70 @@ fn test_burn_adjustment_case_e_zero_registrations() {
803803
assert_eq!(adjusted_diff, 5_000);
804804
});
805805
}
806+
807+
#[test]
808+
fn test_emission_based_on_registration_status() {
809+
new_test_ext(1).execute_with(|| {
810+
let n: u16 = 100;
811+
let netuid_off: u16 = 1;
812+
let netuid_on: u16 = 2;
813+
let tempo: u16 = 1;
814+
let netuids: Vec<u16> = vec![netuid_off, netuid_on];
815+
let emissions: Vec<u64> = vec![1000000000, 1000000000];
816+
817+
// Add subnets with registration turned off and on
818+
add_network(netuid_off, tempo, 0);
819+
add_network(netuid_on, tempo, 0);
820+
SubtensorModule::set_max_allowed_uids(netuid_off, n);
821+
SubtensorModule::set_max_allowed_uids(netuid_on, n);
822+
SubtensorModule::set_emission_values(&netuids, emissions).unwrap();
823+
SubtensorModule::set_network_registration_allowed(netuid_off, false);
824+
SubtensorModule::set_network_registration_allowed(netuid_on, true);
825+
826+
// Populate the subnets with neurons
827+
for i in 0..n {
828+
SubtensorModule::append_neuron(netuid_off, &U256::from(i), 0);
829+
SubtensorModule::append_neuron(netuid_on, &U256::from(i), 0);
830+
}
831+
832+
// Generate emission at block 0
833+
let block: u64 = 0;
834+
SubtensorModule::generate_emission(block);
835+
836+
// Verify that no emission tuples are loaded for the subnet with registration off
837+
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_off).is_none());
838+
839+
// Verify that emission tuples are loaded for the subnet with registration on
840+
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_on).is_some());
841+
assert_eq!(
842+
SubtensorModule::get_loaded_emission_tuples(netuid_on)
843+
.unwrap()
844+
.len(),
845+
n as usize
846+
);
847+
848+
// Step to the next epoch block
849+
let epoch_block: u16 = tempo;
850+
step_block(epoch_block);
851+
852+
// Verify that no emission tuples are loaded for the subnet with registration off
853+
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_off).is_none());
854+
log::info!(
855+
"Emissions for netuid with registration off: {:?}",
856+
SubtensorModule::get_loaded_emission_tuples(netuid_off)
857+
);
858+
859+
// Verify that emission tuples are loaded for the subnet with registration on
860+
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_on).is_some());
861+
log::info!(
862+
"Emissions for netuid with registration on: {:?}",
863+
SubtensorModule::get_loaded_emission_tuples(netuid_on)
864+
);
865+
assert_eq!(
866+
SubtensorModule::get_loaded_emission_tuples(netuid_on)
867+
.unwrap()
868+
.len(),
869+
n as usize
870+
);
871+
});
872+
}

pallets/subtensor/tests/mock.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ parameter_types! {
126126
pub const InitialStakePruningMin: u16 = 0;
127127
pub const InitialFoundationDistribution: u64 = 0;
128128
pub const InitialDefaultTake: u16 = 11_796; // 18%, same as in production
129-
pub const InitialMinTake: u16 = 0;
129+
pub const InitialMinTake: u16 =5_898; // 9%;
130130
pub const InitialWeightsVersionKey: u16 = 0;
131131
pub const InitialServingRateLimit: u64 = 0; // No limit.
132132
pub const InitialTxRateLimit: u64 = 0; // Disable rate limit for testing

0 commit comments

Comments
 (0)