diff --git a/pallets/governance/src/dao.rs b/pallets/governance/src/dao.rs index 00d82af4e..ea1dcc414 100644 --- a/pallets/governance/src/dao.rs +++ b/pallets/governance/src/dao.rs @@ -36,17 +36,7 @@ impl Pallet { #[must_use] fn can_add_application_status_based(key: &T::AccountId) -> bool { - // check if there's an application with the given key - CuratorApplications::::iter().find(|(_, app)| app.user_id == *key).map_or( - true, - |(_, app)| { - // if the application exists, check its status - !matches!( - app.status, - ApplicationStatus::Pending | ApplicationStatus::Accepted - ) - }, - ) + !CuratorApplications::::iter().any(|(_, app)| app.user_id == *key) } pub fn add_application( @@ -54,28 +44,29 @@ impl Pallet { application_key: T::AccountId, data: Vec, ) -> DispatchResult { + // make sure application isnt already whitelisted ensure!( !Self::is_in_legit_whitelist(&application_key), Error::::AlreadyWhitelisted ); - + // make sure application does not already exist ensure!( Self::can_add_application_status_based(&application_key), Error::::ApplicationKeyAlreadyUsed ); - + // check if the key has enough funds to file the application let application_cost = GeneralSubnetApplicationCost::::get(); ensure!( PalletSubspace::::has_enough_balance(&key, application_cost), Error::::NotEnoughBalanceToApply ); - + // 1. a remove the balance from the account let Some(removed_balance_as_currency) = PalletSubspace::::u64_to_balance(application_cost) else { return Err(Error::::InvalidCurrencyConversionValue.into()); }; - + // add the application let application_id = Self::get_next_application_id(); let current_block = PalletSubspace::::get_current_block_number(); @@ -89,6 +80,7 @@ impl Pallet { block_number: current_block, }; + // 1. b remove the balance from the account PalletSubspace::::remove_balance_from_account(&key, removed_balance_as_currency)?; CuratorApplications::::insert(application_id, application); @@ -163,6 +155,12 @@ impl Pallet { let key = ensure_signed(origin)?; ensure!(Curator::::get() == key, Error::::NotCurator); + // make sure application isnt already whitelisted + ensure!( + !Self::is_in_legit_whitelist(&module_key), + Error::::AlreadyWhitelisted + ); + let application = CuratorApplications::::iter_values() .find(|app| app.user_id == module_key) .ok_or(Error::::ApplicationNotFound)?; @@ -172,11 +170,6 @@ impl Pallet { Error::::ApplicationNotPending ); - ensure!( - !Self::is_in_legit_whitelist(&module_key), - Error::::AlreadyWhitelisted - ); - LegitWhitelist::::insert(&module_key, ()); T::execute_application(&module_key)?; diff --git a/pallets/subnet_emission/src/lib.rs b/pallets/subnet_emission/src/lib.rs index 399242384..68342d9a8 100644 --- a/pallets/subnet_emission/src/lib.rs +++ b/pallets/subnet_emission/src/lib.rs @@ -10,7 +10,6 @@ use sp_std::collections::btree_map::BTreeMap; pub mod distribute_emission; pub mod migrations; -pub mod post_runtime; pub mod subnet_pricing { pub mod demo; pub mod root; @@ -109,11 +108,6 @@ pub mod pallet { } Weight::zero() } - - fn on_idle(_n: BlockNumberFor, remaining: Weight) -> Weight { - log::info!("on_idle: {remaining:?}"); - Self::deregister_excess_modules(remaining) - } } #[pallet::event] diff --git a/pallets/subnet_emission/src/post_runtime.rs b/pallets/subnet_emission/src/post_runtime.rs deleted file mode 100644 index 33f2a0131..000000000 --- a/pallets/subnet_emission/src/post_runtime.rs +++ /dev/null @@ -1,79 +0,0 @@ -use super::*; -use frame_support::{traits::Get, weights::Weight}; -use pallet_subnet_emission_api::SubnetConsensus; -use pallet_subspace::{MaxAllowedUids, Pallet as PalletS, N}; - -impl Pallet { - pub(crate) fn deregister_excess_modules(mut remaining: Weight) -> Weight { - let netuid = Self::get_consensus_netuid(SubnetConsensus::Linear).unwrap_or(2); - const MAX_MODULES_PER_ITERATION: usize = 5; - const MAX_UIDS: u16 = 524; - - log::info!("Deregistering excess modules for netuid: {}", netuid); - let db_weight = T::DbWeight::get(); - let mut weight = db_weight.reads(2); - let find_id_weight = db_weight.reads(1); - let deregister_weight = Weight::from_parts(300_495_000, 21587) - .saturating_add(db_weight.reads(34)) - .saturating_add(db_weight.writes(48)); - - // Calculate the minimum required weight to proceed - let min_required_weight = - weight.saturating_add(find_id_weight).saturating_add(deregister_weight); - - if !remaining.all_gte(min_required_weight) { - log::info!("Not enough weight remaining: {:?}", remaining); - return Weight::zero(); - } - - remaining = remaining.saturating_sub(weight); - - let mut module_count = N::::get(netuid); - - // Return early if no excess modules need to be deregistered - if module_count <= MAX_UIDS { - // Also set the max modules to this number - MaxAllowedUids::::set(netuid, MAX_UIDS); - return weight; - } - - for _ in 0..MAX_MODULES_PER_ITERATION { - if module_count <= MAX_UIDS { - break; - } - - // Check if there's enough weight for finding the next module - if !remaining.all_gte(find_id_weight) { - log::info!( - "Not enough weight remaining for find_id_weight: {:?}", - remaining - ); - break; - } - - weight = weight.saturating_add(find_id_weight); - remaining = remaining.saturating_sub(find_id_weight); - - if let Some(uid) = PalletS::::get_lowest_uid(netuid, true) { - // Check if there's enough weight for deregistration - if !remaining.all_gte(deregister_weight) { - log::info!( - "Not enough weight remaining for deregister_weight: {:?}", - remaining - ); - break; - } - log::info!("Deregistering module with UID: {}", uid); - - let _ = PalletS::::remove_module(netuid, uid, false); - module_count = module_count.saturating_sub(1); - weight = weight.saturating_add(deregister_weight); - remaining = remaining.saturating_sub(deregister_weight); - } else { - // No more modules to deregister - break; - } - } - weight - } -}