From 31ecfff2f7eed90f6e272c99ef64050e756d4fce Mon Sep 17 00:00:00 2001 From: Keith Date: Fri, 17 May 2024 01:09:53 +0800 Subject: [PATCH 1/2] Remove more direct indexing --- pallets/subtensor/src/epoch.rs | 46 +++++++++++++++++++-------- pallets/subtensor/src/registration.rs | 12 +++---- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/pallets/subtensor/src/epoch.rs b/pallets/subtensor/src/epoch.rs index 282de2d62..cc146dd59 100644 --- a/pallets/subtensor/src/epoch.rs +++ b/pallets/subtensor/src/epoch.rs @@ -734,7 +734,6 @@ impl Pallet { } /// Output unnormalized sparse weights, input weights are assumed to be row max-upscaled in u16. - #[allow(clippy::indexing_slicing)] pub fn get_weights_sparse(netuid: u16) -> Vec> { let n: usize = Self::get_subnetwork_n(netuid) as usize; let mut weights: Vec> = vec![vec![]; n]; @@ -743,52 +742,71 @@ impl Pallet { .filter(|(uid_i, _)| *uid_i < n as u16) { for (uid_j, weight_ij) in weights_i.iter().filter(|(uid_j, _)| *uid_j < n as u16) { - weights[uid_i as usize].push((*uid_j, I32F32::from_num(*weight_ij))); + weights + .get_mut(uid_i as usize) + .expect("uid_i is filtered to be less than n; qed") + .push((*uid_j, I32F32::from_num(*weight_ij))); } } weights } /// Output unnormalized weights in [n, n] matrix, input weights are assumed to be row max-upscaled in u16. - #[allow(clippy::indexing_slicing)] pub fn get_weights(netuid: u16) -> Vec> { let n: usize = Self::get_subnetwork_n(netuid) as usize; let mut weights: Vec> = vec![vec![I32F32::from_num(0.0); n]; n]; - for (uid_i, weights_i) in + for (uid_i, weights_vec) in as IterableStorageDoubleMap>>::iter_prefix(netuid) + .filter(|(uid_i, _)| *uid_i < n as u16) { - for (uid_j, weight_ij) in weights_i { - weights[uid_i as usize][uid_j as usize] = I32F32::from_num(weight_ij); + for (uid_j, weight_ij) in weights_vec + .into_iter() + .filter(|(uid_j, _)| *uid_j < n as u16) + { + *weights + .get_mut(uid_i as usize) + .expect("uid_i is filtered to be less than n; qed") + .get_mut(uid_j as usize) + .expect("uid_j is filtered to be less than n; qed") = + I32F32::from_num(weight_ij); } } weights } /// Output unnormalized sparse bonds, input bonds are assumed to be column max-upscaled in u16. - #[allow(clippy::indexing_slicing)] pub fn get_bonds_sparse(netuid: u16) -> Vec> { let n: usize = Self::get_subnetwork_n(netuid) as usize; let mut bonds: Vec> = vec![vec![]; n]; - for (uid_i, bonds_i) in + for (uid_i, bonds_vec) in as IterableStorageDoubleMap>>::iter_prefix(netuid) + .filter(|(uid_i, _)| *uid_i < n as u16) { - for (uid_j, bonds_ij) in bonds_i { - bonds[uid_i as usize].push((uid_j, I32F32::from_num(bonds_ij))); + for (uid_j, bonds_ij) in bonds_vec { + bonds + .get_mut(uid_i as usize) + .expect("uid_i is filtered to be less than n; qed") + .push((uid_j, I32F32::from_num(bonds_ij))); } } bonds } /// Output unnormalized bonds in [n, n] matrix, input bonds are assumed to be column max-upscaled in u16. - #[allow(clippy::indexing_slicing)] pub fn get_bonds(netuid: u16) -> Vec> { let n: usize = Self::get_subnetwork_n(netuid) as usize; let mut bonds: Vec> = vec![vec![I32F32::from_num(0.0); n]; n]; - for (uid_i, bonds_i) in + for (uid_i, bonds_vec) in as IterableStorageDoubleMap>>::iter_prefix(netuid) + .filter(|(uid_i, _)| *uid_i < n as u16) { - for (uid_j, bonds_ij) in bonds_i { - bonds[uid_i as usize][uid_j as usize] = I32F32::from_num(bonds_ij); + for (uid_j, bonds_ij) in bonds_vec.into_iter().filter(|(uid_j, _)| *uid_j < n as u16) { + *bonds + .get_mut(uid_i as usize) + .expect("uid_i has been filtered to be less than n; qed") + .get_mut(uid_j as usize) + .expect("uid_j has been filtered to be less than n; qed") = + I32F32::from_num(bonds_ij); } } bonds diff --git a/pallets/subtensor/src/registration.rs b/pallets/subtensor/src/registration.rs index eebf681ea..85590b9db 100644 --- a/pallets/subtensor/src/registration.rs +++ b/pallets/subtensor/src/registration.rs @@ -528,18 +528,14 @@ impl Pallet { hash_as_vec } - #[allow(clippy::indexing_slicing)] pub fn hash_block_and_hotkey(block_hash_bytes: &[u8; 32], hotkey: &T::AccountId) -> H256 { - // Get the public key from the account id. - let hotkey_pubkey: MultiAddress = MultiAddress::Id(hotkey.clone()); - let binding = hotkey_pubkey.encode(); - // Skip extra 0th byte. - let hotkey_bytes: &[u8] = binding[1..].as_ref(); + let binding = hotkey.encode(); + // Safe because Substrate guarantees that all AccountId types are at least 32 bytes + let (hotkey_bytes, _) = binding.split_at(32); let mut full_bytes = [0u8; 64]; let (first_half, second_half) = full_bytes.split_at_mut(32); first_half.copy_from_slice(block_hash_bytes); - // Safe because Substrate guarantees that all AccountId types are at least 32 bytes - second_half.copy_from_slice(&hotkey_bytes[..32]); + second_half.copy_from_slice(hotkey_bytes); let keccak_256_seal_hash_vec: [u8; 32] = keccak_256(&full_bytes[..]); H256::from_slice(&keccak_256_seal_hash_vec) From 7d2c6247cc1645e1a9f4b7de1cafd7c1205f032a Mon Sep 17 00:00:00 2001 From: Keith Date: Fri, 17 May 2024 01:15:12 +0800 Subject: [PATCH 2/2] Remove unused import --- pallets/subtensor/src/registration.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/pallets/subtensor/src/registration.rs b/pallets/subtensor/src/registration.rs index 85590b9db..cc2d5161c 100644 --- a/pallets/subtensor/src/registration.rs +++ b/pallets/subtensor/src/registration.rs @@ -2,7 +2,6 @@ use super::*; use frame_support::storage::IterableStorageDoubleMap; use sp_core::{Get, H256, U256}; use sp_io::hashing::{keccak_256, sha2_256}; -use sp_runtime::MultiAddress; use system::pallet_prelude::BlockNumberFor; const LOG_TARGET: &str = "runtime::subtensor::registration";