Skip to content

Commit 721ee94

Browse files
author
Samuel Dare
committed
fix: run emissions when reg is off
1 parent c6e1079 commit 721ee94

File tree

2 files changed

+79
-36
lines changed

2 files changed

+79
-36
lines changed

pallets/subtensor/src/block_step.rs

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ impl<T: Config> Pallet<T> {
2727
Ok(())
2828
}
2929

30-
/// Helper function which returns the number of blocks remaining before we will run the epoch on this
31-
/// network. Networks run their epoch when (block_number + netuid + 1 ) % (tempo + 1) = 0
32-
///
30+
// Helper function which returns the number of blocks remaining before we will run the epoch on this
31+
// network. Networks run their epoch when (block_number + netuid + 1 ) % (tempo + 1) = 0
32+
//
3333
pub fn blocks_until_next_epoch(netuid: u16, tempo: u16, block_number: u64) -> u64 {
3434
// tempo | netuid | # first epoch block
3535
// 1 0 0
@@ -45,9 +45,9 @@ impl<T: Config> Pallet<T> {
4545
tempo as u64 - (block_number + netuid as u64 + 1) % (tempo as u64 + 1)
4646
}
4747

48-
/// Helper function returns the number of tuples to drain on a particular step based on
49-
/// the remaining tuples to sink and the block number
50-
///
48+
// Helper function returns the number of tuples to drain on a particular step based on
49+
// the remaining tuples to sink and the block number
50+
//
5151
pub fn tuples_to_drain_this_block(
5252
netuid: u16,
5353
tempo: u16,
@@ -78,9 +78,9 @@ impl<T: Config> Pallet<T> {
7878
LoadedEmission::<T>::get(netuid)
7979
}
8080

81-
/// Reads from the loaded emission storage which contains lists of pending emission tuples ( hotkey, amount )
82-
/// and distributes small chunks of them at a time.
83-
///
81+
// Reads from the loaded emission storage which contains lists of pending emission tuples ( hotkey, amount )
82+
// and distributes small chunks of them at a time.
83+
//
8484
pub fn drain_emission(_: u64) {
8585
// --- 1. We iterate across each network.
8686
for (netuid, _) in <Tempo<T> as IterableStorageMap<u16, u16>>::iter() {
@@ -102,21 +102,25 @@ impl<T: Config> Pallet<T> {
102102
}
103103
}
104104

105-
/// Iterates through networks queues more emission onto their pending storage.
106-
/// If a network has no blocks left until tempo, we run the epoch function and generate
107-
/// more token emission tuples for later draining onto accounts.
108-
///
105+
// Iterates through networks queues more emission onto their pending storage.
106+
// If a network has no blocks left until tempo, we run the epoch function and generate
107+
// more token emission tuples for later draining onto accounts.
108+
//
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() {
112112
// Skip the root network or subnets with registrations turned off
113-
if netuid == Self::get_root_netuid() || !Self::is_registration_allowed(netuid) {
113+
if netuid == Self::get_root_netuid() {
114114
// Root emission or subnet emission is burned
115115
continue;
116116
}
117117

118118
// --- 2. Queue the emission due to this network.
119-
let new_queued_emission: u64 = Self::get_subnet_emission_value(netuid);
119+
let mut new_queued_emission: u64 = Self::get_subnet_emission_value(netuid);
120+
if !Self::is_registration_allowed(netuid) {
121+
new_queued_emission = 0; // No emission for this network if registration is off.
122+
}
123+
120124
log::debug!(
121125
"generate_emission for netuid: {:?} with tempo: {:?} and emission: {:?}",
122126
netuid,
@@ -196,10 +200,10 @@ impl<T: Config> Pallet<T> {
196200
Self::set_last_mechanism_step_block(netuid, block_number);
197201
}
198202
}
199-
/// Distributes token inflation through the hotkey based on emission. The call ensures that the inflation
200-
/// is distributed onto the accounts in proportion of the stake delegated minus the take. This function
201-
/// is called after an epoch to distribute the newly minted stake according to delegation.
202-
///
203+
// Distributes token inflation through the hotkey based on emission. The call ensures that the inflation
204+
// is distributed onto the accounts in proportion of the stake delegated minus the take. This function
205+
// is called after an epoch to distribute the newly minted stake according to delegation.
206+
//
203207
pub fn emit_inflation_through_hotkey_account(
204208
hotkey: &T::AccountId,
205209
server_emission: u64,
@@ -260,9 +264,9 @@ impl<T: Config> Pallet<T> {
260264
Self::increase_stake_on_hotkey_account(hotkey, server_emission);
261265
}
262266

263-
/// Increases the stake on the cold - hot pairing by increment while also incrementing other counters.
264-
/// This function should be called rather than set_stake under account.
265-
///
267+
// Increases the stake on the cold - hot pairing by increment while also incrementing other counters.
268+
// This function should be called rather than set_stake under account.
269+
//
266270
pub fn block_step_increase_stake_on_coldkey_hotkey_account(
267271
coldkey: &T::AccountId,
268272
hotkey: &T::AccountId,
@@ -281,8 +285,8 @@ impl<T: Config> Pallet<T> {
281285
TotalStake::<T>::put(TotalStake::<T>::get().saturating_add(increment));
282286
}
283287

284-
/// Decreases the stake on the cold - hot pairing by the decrement while decreasing other counters.
285-
///
288+
// Decreases the stake on the cold - hot pairing by the decrement while decreasing other counters.
289+
//
286290
pub fn block_step_decrease_stake_on_coldkey_hotkey_account(
287291
coldkey: &T::AccountId,
288292
hotkey: &T::AccountId,
@@ -301,8 +305,8 @@ impl<T: Config> Pallet<T> {
301305
TotalStake::<T>::put(TotalStake::<T>::get().saturating_sub(decrement));
302306
}
303307

304-
/// Returns emission awarded to a hotkey as a function of its proportion of the total stake.
305-
///
308+
// Returns emission awarded to a hotkey as a function of its proportion of the total stake.
309+
//
306310
pub fn calculate_stake_proportional_emission(
307311
stake: u64,
308312
total_stake: u64,
@@ -316,8 +320,8 @@ impl<T: Config> Pallet<T> {
316320
proportional_emission.to_num::<u64>()
317321
}
318322

319-
/// Returns the delegated stake 'take' assigned to this key. (If exists, otherwise 0)
320-
///
323+
// Returns the delegated stake 'take' assigned to this key. (If exists, otherwise 0)
324+
//
321325
pub fn calculate_delegate_proportional_take(hotkey: &T::AccountId, emission: u64) -> u64 {
322326
if Self::hotkey_is_delegate(hotkey) {
323327
let take_proportion: I64F64 =
@@ -329,8 +333,8 @@ impl<T: Config> Pallet<T> {
329333
}
330334
}
331335

332-
/// Adjusts the network difficulties/burns of every active network. Resetting state parameters.
333-
///
336+
// Adjusts the network difficulties/burns of every active network. Resetting state parameters.
337+
//
334338
pub fn adjust_registration_terms_for_networks() {
335339
log::debug!("adjust_registration_terms_for_networks");
336340

@@ -486,9 +490,9 @@ impl<T: Config> Pallet<T> {
486490
}
487491
}
488492

489-
/// Calculates the upgraded difficulty by multiplying the current difficulty by the ratio ( reg_actual + reg_target / reg_target + reg_target )
490-
/// We use I110F18 to avoid any overflows on u64. Also min_difficulty and max_difficulty bound the range.
491-
///
493+
// Calculates the upgraded difficulty by multiplying the current difficulty by the ratio ( reg_actual + reg_target / reg_target + reg_target )
494+
// We use I110F18 to avoid any overflows on u64. Also min_difficulty and max_difficulty bound the range.
495+
//
492496
pub fn upgraded_difficulty(
493497
netuid: u16,
494498
current_difficulty: u64,
@@ -513,9 +517,9 @@ impl<T: Config> Pallet<T> {
513517
}
514518
}
515519

516-
/// Calculates the upgraded burn by multiplying the current burn by the ratio ( reg_actual + reg_target / reg_target + reg_target )
517-
/// We use I110F18 to avoid any overflows on u64. Also min_burn and max_burn bound the range.
518-
///
520+
// Calculates the upgraded burn by multiplying the current burn by the ratio ( reg_actual + reg_target / reg_target + reg_target )
521+
// We use I110F18 to avoid any overflows on u64. Also min_burn and max_burn bound the range.
522+
//
519523
pub fn upgraded_burn(
520524
netuid: u16,
521525
current_burn: u64,

pallets/subtensor/tests/block_step.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,3 +895,42 @@ fn test_emission_based_on_registration_status() {
895895
);
896896
});
897897
}
898+
899+
#[test]
900+
fn test_epoch_runs_when_registration_disabled() {
901+
new_test_ext(1).execute_with(|| {
902+
let n: u16 = 100;
903+
let netuid_off: u16 = 1;
904+
let tempo: u16 = 1;
905+
let netuids: Vec<u16> = vec![netuid_off];
906+
let emissions: Vec<u64> = vec![1000000000];
907+
908+
// Add subnets with registration turned off and on
909+
add_network(netuid_off, tempo, 0);
910+
SubtensorModule::set_max_allowed_uids(netuid_off, n);
911+
SubtensorModule::set_emission_values(&netuids, emissions).unwrap();
912+
SubtensorModule::set_network_registration_allowed(netuid_off, false);
913+
914+
// Populate the subnets with neurons
915+
for i in 0..n {
916+
SubtensorModule::append_neuron(netuid_off, &U256::from(i), 0);
917+
}
918+
919+
// Generate emission at block 1
920+
let block: u64 = 1;
921+
SubtensorModule::generate_emission(block);
922+
923+
step_block(1); // Now block 2
924+
925+
// Verify blocks since last step was set
926+
assert_eq!(SubtensorModule::get_blocks_since_last_step(netuid_off), 1);
927+
928+
// Step to the next epoch block
929+
let epoch_block: u16 = tempo;
930+
step_block(epoch_block);
931+
932+
// Verify blocks since last step was set, this indicates we ran the epoch
933+
assert_eq!(SubtensorModule::get_blocks_since_last_step(netuid_off), 0 as u64);
934+
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_off).is_some());
935+
});
936+
}

0 commit comments

Comments
 (0)