Skip to content

Commit

Permalink
Merge pull request #474 from opentensor/hotfix/no_registration_no_emi…
Browse files Browse the repository at this point in the history
…ssion

Hotfix/no registration no emission
  • Loading branch information
distributedstatemachine authored May 27, 2024
2 parents 571a409 + 848a6ee commit 40a8321
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
# required_version = "1.5.1"
# unstable_features = false
# disable_all_formatting = false
skip_children = true
# skip_children = true
# hide_parse_errors = false
# error_on_line_overflow = false
# error_on_unformatted = false
Expand Down
6 changes: 3 additions & 3 deletions pallets/subtensor/src/block_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ impl<T: Config> Pallet<T> {
pub fn generate_emission(block_number: u64) {
// --- 1. Iterate across each network and add pending emission into stash.
for (netuid, tempo) in <Tempo<T> as IterableStorageMap<u16, u16>>::iter() {
// Skip the root network.
if netuid == Self::get_root_netuid() {
// Root emission is burned.
// Skip the root network or subnets with registrations turned off
if netuid == Self::get_root_netuid() || !Self::is_registration_allowed(netuid) {
// Root emission or subnet emission is burned
continue;
}

Expand Down
18 changes: 18 additions & 0 deletions pallets/subtensor/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,24 @@ impl<T: Config> Pallet<T> {
Self::deposit_event(Event::NetworkRateLimitSet(limit));
}

/// Checks if registrations are allowed for a given subnet.
///
/// This function retrieves the subnet hyperparameters for the specified subnet and checks the `registration_allowed` flag.
/// If the subnet doesn't exist or doesn't have hyperparameters defined, it returns `false`.
///
/// # Arguments
///
/// * `netuid` - The unique identifier of the subnet.
///
/// # Returns
///
/// * `bool` - `true` if registrations are allowed for the subnet, `false` otherwise.
pub fn is_registration_allowed(netuid: u16) -> bool {
Self::get_subnet_hyperparams(netuid)
.map(|params| params.registration_allowed)
.unwrap_or(false)
}

// Computes and sets emission values for the root network which determine the emission for all subnets.
//
// This function is responsible for calculating emission based on network weights, stake values,
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/subnet_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct SubnetHyperparams {
weights_rate_limit: Compact<u64>,
adjustment_interval: Compact<u16>,
activity_cutoff: Compact<u16>,
registration_allowed: bool,
pub registration_allowed: bool,
target_regs_per_interval: Compact<u16>,
min_burn: Compact<u64>,
max_burn: Compact<u64>,
Expand Down
91 changes: 91 additions & 0 deletions pallets/subtensor/tests/block_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -803,3 +803,94 @@ fn test_burn_adjustment_case_e_zero_registrations() {
assert_eq!(adjusted_diff, 5_000);
});
}

#[test]
fn test_emission_based_on_registration_status() {
new_test_ext(1).execute_with(|| {
let n: u16 = 100;
let netuid_off: u16 = 1;
let netuid_on: u16 = 2;
let tempo: u16 = 1;
let netuids: Vec<u16> = vec![netuid_off, netuid_on];
let emissions: Vec<u64> = vec![1000000000, 1000000000];

// Add subnets with registration turned off and on
add_network(netuid_off, tempo, 0);
add_network(netuid_on, tempo, 0);
SubtensorModule::set_max_allowed_uids(netuid_off, n);
SubtensorModule::set_max_allowed_uids(netuid_on, n);
SubtensorModule::set_emission_values(&netuids, emissions).unwrap();
SubtensorModule::set_network_registration_allowed(netuid_off, false);
SubtensorModule::set_network_registration_allowed(netuid_on, true);

// Populate the subnets with neurons
for i in 0..n {
SubtensorModule::append_neuron(netuid_off, &U256::from(i), 0);
SubtensorModule::append_neuron(netuid_on, &U256::from(i), 0);
}

// Generate emission at block 0
let block: u64 = 0;
SubtensorModule::generate_emission(block);

// Verify that no emission tuples are loaded for the subnet with registration off
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_off).is_none());

// Verify that emission tuples are loaded for the subnet with registration on
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_on).is_some());
assert_eq!(
SubtensorModule::get_loaded_emission_tuples(netuid_on)
.unwrap()
.len(),
n as usize
);

// Step to the next epoch block
let epoch_block: u16 = tempo;
step_block(epoch_block);

// Verify that no emission tuples are loaded for the subnet with registration off
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_off).is_none());
log::info!(
"Emissions for netuid with registration off: {:?}",
SubtensorModule::get_loaded_emission_tuples(netuid_off)
);

// Verify that emission tuples are loaded for the subnet with registration on
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_on).is_some());
log::info!(
"Emissions for netuid with registration on: {:?}",
SubtensorModule::get_loaded_emission_tuples(netuid_on)
);
assert_eq!(
SubtensorModule::get_loaded_emission_tuples(netuid_on)
.unwrap()
.len(),
n as usize
);

// drain the emission tuples for the subnet with registration on
SubtensorModule::drain_emission(next_block as u64);
// Turn on registration for the subnet with registration off
SubtensorModule::set_network_registration_allowed(netuid_off, true);
SubtensorModule::set_network_registration_allowed(netuid_on, false);

// Generate emission at the next block
let next_block: u64 = block + 1;
SubtensorModule::generate_emission(next_block);

// Verify that emission tuples are now loaded for the subnet with registration turned on
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_off).is_some());
log::info!(
"Emissions for netuid with registration on: {:?}",
SubtensorModule::get_loaded_emission_tuples(netuid_on)
);
assert!(SubtensorModule::get_loaded_emission_tuples(netuid_on).is_none());
assert_eq!(
SubtensorModule::get_loaded_emission_tuples(netuid_off)
.unwrap()
.len(),
n as usize
);
});
}

0 comments on commit 40a8321

Please sign in to comment.