Skip to content

Commit

Permalink
taking out pending deregistrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name authored and steinerkelvin committed Feb 13, 2024
1 parent f636f89 commit 0f4f4bf
Show file tree
Hide file tree
Showing 7 changed files with 4 additions and 149 deletions.
4 changes: 0 additions & 4 deletions pallets/subspace/src/autogen_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,6 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
/// Proof: `SubspaceModule::TotalSubnets` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `SubspaceModule::BurnRate` (r:0 w:1)
/// Proof: `SubspaceModule::BurnRate` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `SubspaceModule::PendingDeregisterUids` (r:0 w:1)
/// Proof: `SubspaceModule::PendingDeregisterUids` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `SubspaceModule::MinAllowedWeights` (r:0 w:1)
/// Proof: `SubspaceModule::MinAllowedWeights` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `SubspaceModule::LastUpdate` (r:0 w:1)
Expand Down Expand Up @@ -728,8 +726,6 @@ impl WeightInfo for () {
/// Proof: `SubspaceModule::TotalSubnets` (`max_values`: Some(1), `max_size`: None, mode: `Measured`)
/// Storage: `SubspaceModule::BurnRate` (r:0 w:1)
/// Proof: `SubspaceModule::BurnRate` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `SubspaceModule::PendingDeregisterUids` (r:0 w:1)
/// Proof: `SubspaceModule::PendingDeregisterUids` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `SubspaceModule::MinAllowedWeights` (r:0 w:1)
/// Proof: `SubspaceModule::MinAllowedWeights` (`max_values`: None, `max_size`: None, mode: `Measured`)
/// Storage: `SubspaceModule::LastUpdate` (r:0 w:1)
Expand Down
6 changes: 0 additions & 6 deletions pallets/subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,6 @@ pub mod pallet {
pub fn DefaultMaxAllowedWeights<T: Config>() -> u16 {420}
#[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights
pub type MaxAllowedWeights<T> =StorageMap<_, Identity, u16, u16, ValueQuery, DefaultMaxAllowedWeights<T>>;


#[pallet::type_value]
pub fn DefaultPendingDeregisterUids<T: Config>() -> Vec<u16> {vec![]}
#[pallet::storage] // --- MAP ( netuid ) --> min_allowed_weights
pub type PendingDeregisterUids<T> = StorageMap<_, Identity, u16, Vec<u16>, ValueQuery, DefaultPendingDeregisterUids<T>>;


#[pallet::type_value]
Expand Down
15 changes: 3 additions & 12 deletions pallets/subspace/src/registration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ impl<T: Config> Pallet<T> {
}

uid = Self::append_module(netuid, &module_key, name.clone(), address.clone());

// adding the stake amount
Self::do_add_stake(origin.clone(), netuid, module_key.clone(), stake_amount)?;

// CONSTANT INITIAL BURN
if min_burn > 0 {
ensure!(stake_amount >= min_burn, Error::<T>::NotEnoughStakeToRegister);
Expand Down Expand Up @@ -154,18 +157,6 @@ impl<T: Config> Pallet<T> {
pub fn get_lowest_uid(netuid: u16) -> u16 {
let n: u16 = Self::get_subnet_n(netuid);

// If there are pending deregister uids, then return the first one.
let pending_deregister_uids: Vec<u16> = PendingDeregisterUids::<T>::get(netuid);
if pending_deregister_uids.len() > 0 {
let uid: u16 = pending_deregister_uids[0];
if uid < n {
PendingDeregisterUids::<T>::mutate(netuid, |v| v.remove(0));
return uid

}

}

let mut min_score: u64 = u64::MAX;
let mut lowest_priority_uid: u16 = 0;
let mut prune_uids: Vec<u16> = Vec::new();
Expand Down
9 changes: 0 additions & 9 deletions pallets/subspace/src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ impl<T: Config> Pallet<T> {
let new_queued_emission: u64 = Self::calculate_network_emission(netuid);
PendingEmission::<T>::mutate(netuid, |mut queued| *queued += new_queued_emission);
log::debug!("netuid_i: {:?} queued_emission: +{:?} ", netuid, new_queued_emission);
Self::deregister_pending_uid(netuid); // deregister any pending uids
if Self::blocks_until_next_epoch(netuid, tempo, block_number) > 0 {
continue
}
Expand Down Expand Up @@ -398,14 +397,6 @@ impl<T: Config> Pallet<T> {

let mut zero_stake_uids : Vec<u16> = Vec::new();

for (module_uid, module_key) in uid_key_tuples.iter() {
let new_stake = Self::get_stake_for_key(netuid, module_key);
if new_stake < subnet_params.min_stake {
// if the stake is more than the max stake, then deregister the module
Self::add_pending_deregistration_uid(netuid, *module_uid);
}
}


// calculate the total emission
let emission: Vec<u64> = incentive_emission
Expand Down
27 changes: 0 additions & 27 deletions pallets/subspace/src/subnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,29 +208,6 @@ impl<T: Config> Pallet<T> {



pub fn add_pending_deregistration_uid(netuid: u16, uid: u16) {
PendingDeregisterUids::<T>::mutate(netuid, |val| val.push(uid));
}

pub fn add_pending_deregistration_uids(netuid: u16, uids: Vec<u16>) {
for uid in uids {
PendingDeregisterUids::<T>::mutate(netuid, |val| val.push(uid));
}
}

pub fn deregister_pending_uid(netuid: u16) {
let mut pending_deregister_uids: Vec<u16> = PendingDeregisterUids::<T>::get(netuid);
if pending_deregister_uids.len() > 0 {
let n = Self::get_subnet_n(netuid);
let uid: u16 = pending_deregister_uids.remove(0);

if uid < n {
Self::remove_module(netuid, uid);
PendingDeregisterUids::<T>::insert(netuid, pending_deregister_uids);

}
}
}

pub fn set_max_allowed_uids(netuid: u16, mut max_allowed_uids: u16) {
let n: u16 = Self::get_subnet_n(netuid);
Expand Down Expand Up @@ -491,7 +468,6 @@ impl<T: Config> Pallet<T> {
Dividends::<T>::remove(netuid);
Trust::<T>::remove(netuid);
LastUpdate::<T>::remove(netuid);
PendingDeregisterUids::<T>::remove(netuid);
DelegationFee::<T>::clear_prefix(netuid, u32::max_value(), None);
RegistrationBlock::<T>::clear_prefix(netuid, u32::max_value(), None);

Expand Down Expand Up @@ -1003,7 +979,4 @@ impl<T: Config> Pallet<T> {
MaxWeightAge::<T>::insert(netuid, max_weight_age);
}

pub fn get_pending_deregister_uids(netuid: u16) -> Vec<u16> {
return PendingDeregisterUids::<T>::get(netuid)
}
}
18 changes: 1 addition & 17 deletions pallets/subspace/tests/test_staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,23 +341,7 @@ fn test_min_stake() {
// SubspaceModule::set_min_stake( netuid, min_stake - 100);

SubspaceModule::remove_stake(get_origin(keys[0]), netuid, keys[0], 10_000_000_000);
let pending_deregistration_uids = SubspaceModule::get_pending_deregister_uids(netuid);
println!("PENDING DEREGISTRATION UIDS: {:?}", pending_deregistration_uids);

let stakes = SubspaceModule::get_stakes(netuid);
println!("STAKES: {:?}", stakes);
step_epoch(netuid);
let stakes = SubspaceModule::get_stakes(netuid);
println!("STAKES: {:?}", stakes);
let pending_deregistration_uids = SubspaceModule::get_pending_deregister_uids(netuid);
println!("PENDING DEREGISTRATION UIDS: {:?}", pending_deregistration_uids);
assert_eq!(pending_deregistration_uids.len(), 1);
assert_eq!(pending_deregistration_uids[0], 0);
let is_registered = SubspaceModule::is_registered(netuid, &keys[0]);
assert_eq!(is_registered, true);
step_block(1);
let is_registered = SubspaceModule::is_registered(netuid, &keys[0]);
assert_eq!(is_registered, false);


});
}
Expand Down
74 changes: 0 additions & 74 deletions pallets/subspace/tests/test_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -832,80 +832,6 @@ fn test_trust() {
// });
// }

#[test]
fn test_pending_deregistration() {
new_test_ext().execute_with(|| {

let netuid = 0;
let n = 20;
let initial_stake: u64 = 1000;
let keys : Vec<U256> = (0..n).into_iter().map(|x| U256::from(x)).collect();
let stakes : Vec<u64> = (0..n).into_iter().map(|x| initial_stake * 1_000_000_000).collect();


for i in 0..n {
assert_ok!(register_module(netuid, keys[i], stakes[i]));
let stake_from_vector = SubspaceModule::get_stake_to_vector(netuid, &keys[i]);
println!("{:?}", stake_from_vector);
}
// now we set the p. rams
let mut params = SubspaceModule::global_params();
params.burn_rate = 100;
SubspaceModule::set_global_params(params.clone());
let mut params = SubspaceModule::subnet_params(netuid);
params.min_stake = stakes[0];
params.tempo = 10;
SubspaceModule::set_subnet_params(netuid, params.clone());

let subnet_emission = SubspaceModule::get_subnet_emission(netuid);
println!("subnet_emission: {:?}", subnet_emission);


let voter_key = keys[1];


// vote to avoid key[0] as we want to see the key[0] burn
let mut votes : Vec<u16> = vec![];
let mut uids : Vec<u16> = vec![];
for i in 0..n {
if i != 0 {
votes.push(1);
uids.push(i as u16);
}
}
println!("{:?}", SubspaceModule::get_stake_for_key(netuid, &voter_key));
assert_ok!(SubspaceModule::set_weights(get_origin(voter_key),netuid, uids, votes));

let stakes = SubspaceModule::get_stakes(netuid);
let total_stake_before = stakes.iter().sum::<u64>();
println!("total_stake_before: {:?}", total_stake_before);
step_block(params.tempo);

let params = SubspaceModule::subnet_params(netuid);
println!("params: {:?}", params);

let emissions = SubspaceModule::get_emissions(netuid);
let total_emissions = emissions.iter().sum::<u64>();
println!("total_emissions: {:?}", total_emissions);
println!("emissions: {:?}", emissions);
let stakes = SubspaceModule::get_stakes(netuid);
let total_stake_after = stakes.iter().sum::<u64>();
println!("total_stake_after: {:?}", total_stake_after);
println!("staking: {:?}", stakes);
let key_stake = SubspaceModule::get_total_stake_to(netuid,&keys[1]);
let params = SubspaceModule::subnet_params(netuid);
let pending_deregister_uids = SubspaceModule::get_pending_deregister_uids(netuid);
let staking = SubspaceModule::get_stakes(netuid);

println!("key_stake: {:?}", SubspaceModule::get_min_stake(netuid));
println!("pending_deregister_uids: {:?}", pending_deregister_uids);
assert!(!pending_deregister_uids.contains(&1));
assert!( key_stake > params.min_stake , "key_stake: {:?} params.min_stake {:?}", key_stake, params.min_stake);


});
}



#[test]
Expand Down

0 comments on commit 0f4f4bf

Please sign in to comment.