diff --git a/pallets/offworker/src/lib.rs b/pallets/offworker/src/lib.rs index 3c1110441..5929a70ec 100644 --- a/pallets/offworker/src/lib.rs +++ b/pallets/offworker/src/lib.rs @@ -23,7 +23,7 @@ use pallet_subnet_emission::{ yuma::YumaEpoch, }, types::{BlockWeights, PublicKey}, - Authorities, SubnetDecryptionData, WeightEncryptionData, + Authorities, SubnetDecryptionData, }; use sp_std::collections::btree_map::BTreeMap; @@ -185,10 +185,11 @@ pub mod pallet { ); #[cfg(feature = "testing-offworker")] - let (valid_subnets, hanging_subnets) = Self::get_valid_subnets(None); + let (valid_subnets, hanging_subnets) = + pallet_subnet_emission::Pallet::::get_valid_subnets(None); #[cfg(not(feature = "testing-offworker"))] - let account_id = match Signer::::any_account() + let acc_id = match Signer::::any_account() .accounts_from_keys() .next() .map(|account| account.id) @@ -204,9 +205,15 @@ pub mod pallet { // offchain worker. The hanging, are potential subnets that have turned off the // encryption in the middle of offchain worker process #[cfg(not(feature = "testing-offworker"))] - let (valid_subnets, hanging_subnets) = Self::get_valid_subnets(Some(acc_id)); + let (valid_subnets, hanging_subnets) = + pallet_subnet_emission::Pallet::::get_valid_subnets(Some(&acc_id)); - Self::discard_hanging_subnets(hanging_subnets); + // The runtime mimics this logic, by deleting all storages related to consenus + // parameters and weights + hanging_subnets.iter().for_each(|subnet_id| { + log::info!("Deleting subnet: {}", subnet_id); + Self::delete_subnet_state(subnet_id); + }); log::info!("Valid subnets: {:?}", valid_subnets); let deregistered_subnets = Self::process_subnets(valid_subnets, block_number); @@ -300,13 +307,6 @@ impl Pallet { .build() } - fn discard_hanging_subnets(hanging_subnets: Vec) { - hanging_subnets.iter().for_each(|subnet_id| { - log::info!("Deleting subnet: {}", subnet_id); - Self::delete_subnet_state(subnet_id); - }); - } - fn do_send_weights( subnet_id: u16, decrypted_weights: Vec, diff --git a/pallets/offworker/src/process.rs b/pallets/offworker/src/process.rs index cf8c53fd4..b5be22b5b 100644 --- a/pallets/offworker/src/process.rs +++ b/pallets/offworker/src/process.rs @@ -1,29 +1,6 @@ use super::*; impl Pallet { - /// Returns a tuple of subnet UIDs (with_encryption, without_encryption) where: - /// - First vector contains subnets that use weight encryption and have matching keys (if acc_id - /// is Some) - /// - Second vector contains subnets that don't use encryption but still have matching keys (if - /// acc_id is Some) - /// Both require the subnet to have existing encrypted weights. - pub fn get_valid_subnets(acc_id: Option<&T::AccountId>) -> (Vec, Vec) { - let (with_encryption, without_encryption): (Vec<_>, Vec<_>) = - SubnetDecryptionData::::iter() - .filter(|(netuid, data)| { - let key_match = acc_id.map_or(true, |id| &data.node_id == id); - let has_encrypted_weights = WeightEncryptionData::::iter_prefix(*netuid) - .any(|(_, value)| !value.encrypted.is_empty()); - - key_match && has_encrypted_weights - }) - .map(|(netuid, _)| netuid) - .partition(|netuid| pallet_subspace::UseWeightsEncryption::::get(*netuid)); - - (with_encryption, without_encryption) - } - - // TODO: cancled everythign when weight encryption was cancled pub fn process_subnets(subnets: Vec, current_block: u64) -> Vec { let mut deregistered_subnets = Vec::new(); diff --git a/pallets/subnet_emission/src/decryption.rs b/pallets/subnet_emission/src/decryption.rs index 8a39797e1..c0892536b 100644 --- a/pallets/subnet_emission/src/decryption.rs +++ b/pallets/subnet_emission/src/decryption.rs @@ -159,7 +159,7 @@ impl Pallet { None => &module_key, }; if key.encode() != received_key { - log::error!("Key mismatch for module {uid}"); + log::warn!("Key mismatch for module {uid}"); return None; } @@ -298,19 +298,37 @@ impl Pallet { encoded } + /// Returns a tuple of subnet UIDs (with_encryption, without_encryption) where: + /// - First vector contains subnets that use weight encryption and have matching keys (if acc_id + /// is Some) + /// - Second vector contains subnets that don't use encryption but still have matching keys (if + /// acc_id is Some) + /// Both require the subnet to have existing encrypted weights. + pub fn get_valid_subnets(acc_id: Option<&T::AccountId>) -> (Vec, Vec) { + let (with_encryption, without_encryption): (Vec<_>, Vec<_>) = + SubnetDecryptionData::::iter() + .filter(|(netuid, data)| { + let key_match = acc_id.map_or(true, |id| &data.node_id == id); + let has_encrypted_weights = WeightEncryptionData::::iter_prefix(*netuid) + .any(|(_, value)| !value.encrypted.is_empty()); + + key_match && has_encrypted_weights + }) + .map(|(netuid, _)| netuid) + .partition(|netuid| pallet_subspace::UseWeightsEncryption::::get(*netuid)); + + (with_encryption, without_encryption) + } + pub fn cancel_expired_offchain_workers(block_number: u64) { let max_inactivity_blocks = T::PingInterval::get().saturating_mul(T::MaxFailedPings::get() as u64); - // TODO: unite the logic with get valid subnets function from offworker - pallet_subspace::N::::iter_keys() - // Check if subnet uses weight encryption - .filter(|subnet_id| pallet_subspace::UseWeightsEncryption::::get(subnet_id)) - // Check if there are any encrypted weights for this subnet - .filter(|subnet_id| { - WeightEncryptionData::::iter_prefix(subnet_id) - .any(|(_, value)| !value.encrypted.is_empty()) - }) + // Get only subnets that use encryption and have encrypted weights + let (with_encryption, _) = Self::get_valid_subnets(None); + + with_encryption + .into_iter() .filter_map(|subnet_id| { SubnetDecryptionData::::get(subnet_id).map(|info| (subnet_id, info)) })