Skip to content

Commit

Permalink
feat: uniting subnet validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Honza committed Nov 13, 2024
1 parent 88e17ba commit f2148fa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 45 deletions.
24 changes: 12 additions & 12 deletions pallets/offworker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<T>::get_valid_subnets(None);

#[cfg(not(feature = "testing-offworker"))]
let account_id = match Signer::<T, T::AuthorityId>::any_account()
let acc_id = match Signer::<T, T::AuthorityId>::any_account()
.accounts_from_keys()
.next()
.map(|account| account.id)
Expand All @@ -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::<T>::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);
Expand Down Expand Up @@ -300,13 +307,6 @@ impl<T: Config> Pallet<T> {
.build()
}

fn discard_hanging_subnets(hanging_subnets: Vec<u16>) {
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<BlockWeights>,
Expand Down
23 changes: 0 additions & 23 deletions pallets/offworker/src/process.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,6 @@
use super::*;

impl<T: Config> Pallet<T> {
/// 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<u16>, Vec<u16>) {
let (with_encryption, without_encryption): (Vec<_>, Vec<_>) =
SubnetDecryptionData::<T>::iter()
.filter(|(netuid, data)| {
let key_match = acc_id.map_or(true, |id| &data.node_id == id);
let has_encrypted_weights = WeightEncryptionData::<T>::iter_prefix(*netuid)
.any(|(_, value)| !value.encrypted.is_empty());

key_match && has_encrypted_weights
})
.map(|(netuid, _)| netuid)
.partition(|netuid| pallet_subspace::UseWeightsEncryption::<T>::get(*netuid));

(with_encryption, without_encryption)
}

// TODO: cancled everythign when weight encryption was cancled
pub fn process_subnets(subnets: Vec<u16>, current_block: u64) -> Vec<u16> {
let mut deregistered_subnets = Vec::new();

Expand Down
38 changes: 28 additions & 10 deletions pallets/subnet_emission/src/decryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ impl<T: Config> Pallet<T> {
None => &module_key,
};
if key.encode() != received_key {
log::error!("Key mismatch for module {uid}");
log::warn!("Key mismatch for module {uid}");
return None;
}

Expand Down Expand Up @@ -298,19 +298,37 @@ impl<T: Config> Pallet<T> {
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.

Check failure on line 306 in pallets/subnet_emission/src/decryption.rs

View workflow job for this annotation

GitHub Actions / check

doc list item without indentation
pub fn get_valid_subnets(acc_id: Option<&T::AccountId>) -> (Vec<u16>, Vec<u16>) {
let (with_encryption, without_encryption): (Vec<_>, Vec<_>) =
SubnetDecryptionData::<T>::iter()
.filter(|(netuid, data)| {
let key_match = acc_id.map_or(true, |id| &data.node_id == id);
let has_encrypted_weights = WeightEncryptionData::<T>::iter_prefix(*netuid)
.any(|(_, value)| !value.encrypted.is_empty());

key_match && has_encrypted_weights
})
.map(|(netuid, _)| netuid)
.partition(|netuid| pallet_subspace::UseWeightsEncryption::<T>::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::<T>::iter_keys()
// Check if subnet uses weight encryption
.filter(|subnet_id| pallet_subspace::UseWeightsEncryption::<T>::get(subnet_id))
// Check if there are any encrypted weights for this subnet
.filter(|subnet_id| {
WeightEncryptionData::<T>::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::<T>::get(subnet_id).map(|info| (subnet_id, info))
})
Expand Down

0 comments on commit f2148fa

Please sign in to comment.