From 8f9c7f49d1e22b36551f84dd729e8fb2edfefa07 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 16 Mar 2023 16:32:51 -0400 Subject: [PATCH 1/7] . --- pallets/subtensor/src/delegate_info.rs | 34 ++++++++++++------------ pallets/subtensor/src/lib.rs | 22 ++-------------- pallets/subtensor/src/neuron_info.rs | 26 +++++++++---------- pallets/subtensor/src/subnet_info.rs | 4 +-- runtime/src/lib.rs | 36 ++++++++++++-------------- 5 files changed, 48 insertions(+), 74 deletions(-) diff --git a/pallets/subtensor/src/delegate_info.rs b/pallets/subtensor/src/delegate_info.rs index 611d95519f..3d5a444f89 100644 --- a/pallets/subtensor/src/delegate_info.rs +++ b/pallets/subtensor/src/delegate_info.rs @@ -1,6 +1,5 @@ use super::*; use frame_support::IterableStorageDoubleMap; -use serde::{Serialize, Deserialize}; use frame_support::storage::IterableStorageMap; use frame_support::pallet_prelude::{Decode, Encode}; extern crate alloc; @@ -8,54 +7,53 @@ use alloc::vec::Vec; use sp_core::hexdisplay::AsBytesRef; -#[derive(Decode, Encode, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] -#[serde(deny_unknown_fields)] -pub struct DelegateInfo { - delegate_ss58: DeAccountId, +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] +pub struct DelegateInfo { + delegate_ss58: T::AccountId, take: u16, - nominators: Vec<(DeAccountId, u64)>, // map of nominator_ss58 to stake amount - owner_ss58: DeAccountId + nominators: Vec<(T::AccountId, u64)>, // map of nominator_ss58 to stake amount + owner_ss58: T::AccountId } impl Pallet { - pub fn get_delegate( delegate_account_vec: Vec ) -> Option { + pub fn get_delegate( delegate_account_vec: Vec ) -> Option> { if delegate_account_vec.len() != 32 { return None; } let delegate: AccountIdOf = T::AccountId::decode( &mut delegate_account_vec.as_bytes_ref() ).unwrap(); - let mut nominators = Vec::<(DeAccountId, u64)>::new(); + let mut nominators = Vec::<(T::AccountId, u64)>::new(); for ( nominator, stake ) in < Stake as IterableStorageDoubleMap >::iter_prefix( delegate.clone() ) { - nominators.push( ( nominator.clone().encode().into(), stake ) ); + nominators.push( ( nominator.clone(), stake ) ); } let owner = >::get( delegate.clone() ); return Some( DelegateInfo { - delegate_ss58: delegate.clone().encode().into(), + delegate_ss58: delegate.clone(), take: >::get( delegate.clone() ), nominators, - owner_ss58: owner.clone().encode().into() + owner_ss58: owner.clone() }); } - pub fn get_delegates() -> Vec { - let mut delegates = Vec::::new(); + pub fn get_delegates() -> Vec> { + let mut delegates = Vec::>::new(); for ( delegate, take ) in < Delegates as IterableStorageMap >::iter() { - let mut nominators = Vec::<(DeAccountId, u64)>::new(); + let mut nominators = Vec::<(T::AccountId, u64)>::new(); for ( nominator, stake ) in < Stake as IterableStorageDoubleMap >::iter_prefix( delegate.clone() ) { - nominators.push( ( nominator.clone().encode().into(), stake ) ); + nominators.push( ( nominator.clone(), stake ) ); } let owner = >::get( delegate.clone() ); delegates.push( DelegateInfo { - delegate_ss58: delegate.clone().encode().into(), + delegate_ss58: delegate.clone(), take, nominators, - owner_ss58: owner.clone().encode().into() + owner_ss58: owner.clone() }); } diff --git a/pallets/subtensor/src/lib.rs b/pallets/subtensor/src/lib.rs index a84552e4fa..0c17292e1a 100644 --- a/pallets/subtensor/src/lib.rs +++ b/pallets/subtensor/src/lib.rs @@ -169,20 +169,6 @@ pub mod pallet { pub type AccountIdOf = ::AccountId; - #[derive(Decode, Encode, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] - pub struct DeAccountId { // allows us to de/serialize the account id as a u8 vec - #[serde(with = "serde_bytes")] - id: Vec - } - - impl From> for DeAccountId { - fn from(v: Vec) -> Self { - DeAccountId { - id: v.clone() - } - } - } - // ============================ // ==== Staking + Accounts ==== // ============================ @@ -321,12 +307,10 @@ pub mod pallet { // --- Struct for Axon. pub type AxonInfoOf = AxonInfo; - #[serde_as] - #[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] + #[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)] pub struct AxonInfo { pub block: u64, // --- Axon serving block. pub version: u32, // --- Axon version - #[serde_as(as = "DisplayFromStr")] // serialize as string, deserialize from string pub ip: u128, // --- Axon u128 encoded ip address of type v6 or v4. pub port: u16, // --- Axon u16 encoded port. pub ip_type: u8, // --- Axon ip type, 4 for ipv4 and 6 for ipv6. @@ -337,12 +321,10 @@ pub mod pallet { // --- Struct for Prometheus. pub type PrometheusInfoOf = PrometheusInfo; - #[serde_as] - #[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] + #[derive(Encode, Decode, Default, TypeInfo, Clone, PartialEq, Eq, Debug)] pub struct PrometheusInfo { pub block: u64, // --- Prometheus serving block. pub version: u32, // --- Prometheus version. - #[serde_as(as = "DisplayFromStr")] // serialize as string, deserialize from string pub ip: u128, // --- Prometheus u128 encoded ip address of type v6 or v4. pub port: u16, // --- Prometheus u16 encoded port. pub ip_type: u8, // --- Prometheus ip type, 4 for ipv4 and 6 for ipv6. diff --git a/pallets/subtensor/src/neuron_info.rs b/pallets/subtensor/src/neuron_info.rs index db9a6798b9..4f93bc40a6 100644 --- a/pallets/subtensor/src/neuron_info.rs +++ b/pallets/subtensor/src/neuron_info.rs @@ -1,22 +1,20 @@ use super::*; use crate::math::*; -use serde::{Serialize, Deserialize}; use frame_support::storage::IterableStorageDoubleMap; use frame_support::pallet_prelude::{Decode, Encode}; extern crate alloc; use alloc::vec::Vec; -#[derive(Decode, Encode, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] -#[serde(deny_unknown_fields)] -pub struct NeuronInfo { - hotkey: DeAccountId, - coldkey: DeAccountId, +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] +pub struct NeuronInfo { + hotkey: T::AccountId, + coldkey: T::AccountId, uid: u16, netuid: u16, active: bool, axon_info: AxonInfo, prometheus_info: PrometheusInfo, - stake: Vec<(DeAccountId, u64)>, // map of coldkey to stake on this neuron/hotkey (includes delegations) + stake: Vec<(T::AccountId, u64)>, // map of coldkey to stake on this neuron/hotkey (includes delegations) rank: u16, emission: u64, incentive: u16, @@ -32,7 +30,7 @@ pub struct NeuronInfo { } impl Pallet { - pub fn get_neurons(netuid: u16) -> Vec { + pub fn get_neurons(netuid: u16) -> Vec> { if !Self::if_subnet_exist(netuid) { return Vec::new(); } @@ -57,7 +55,7 @@ impl Pallet { neurons } - fn get_neuron_subnet_exists(netuid: u16, uid: u16) -> Option { + fn get_neuron_subnet_exists(netuid: u16, uid: u16) -> Option> { let _hotkey = Self::get_hotkey_for_net_and_uid(netuid, uid); let hotkey; if _hotkey.is_err() { @@ -92,16 +90,16 @@ impl Pallet { let bonds = Self::get_bonds(netuid)[uid as usize].iter() .map(|x| fixed_proportion_to_u16(*x)).collect::>(); - let mut stakes = Vec::<(DeAccountId, u64)>::new(); + let mut stakes = Vec::<(T::AccountId, u64)>::new(); for ( coldkey, stake ) in < Stake as IterableStorageDoubleMap >::iter_prefix( hotkey.clone() ) { - stakes.push( (coldkey.clone().encode().into(), stake) ); + stakes.push( (coldkey.clone(), stake) ); } let stake = stakes; let neuron = NeuronInfo { - hotkey: hotkey.clone().encode().into(), - coldkey: coldkey.clone().encode().into(), + hotkey: hotkey.clone(), + coldkey: coldkey.clone(), uid, netuid, active, @@ -125,7 +123,7 @@ impl Pallet { return Some(neuron); } - pub fn get_neuron(netuid: u16, uid: u16) -> Option { + pub fn get_neuron(netuid: u16, uid: u16) -> Option> { if !Self::if_subnet_exist(netuid) { return None; } diff --git a/pallets/subtensor/src/subnet_info.rs b/pallets/subtensor/src/subnet_info.rs index 3fd7359466..bab75a2dd9 100644 --- a/pallets/subtensor/src/subnet_info.rs +++ b/pallets/subtensor/src/subnet_info.rs @@ -1,13 +1,11 @@ use super::*; use frame_support::IterableStorageDoubleMap; -use serde::{Serialize, Deserialize}; use frame_support::storage::IterableStorageMap; use frame_support::pallet_prelude::{Decode, Encode}; extern crate alloc; use alloc::vec::Vec; -#[derive(Decode, Encode, Serialize, Deserialize, PartialEq, Eq, Clone, Debug)] -#[serde(deny_unknown_fields)] +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] pub struct SubnetInfo { netuid: u16, rho: u16, diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index dbd731d00d..25e9358f45 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -6,6 +6,7 @@ #[cfg(feature = "std")] include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); +use codec::Encode; use pallet_grandpa::{ fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList, }; @@ -110,7 +111,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 104, + spec_version: 105, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, @@ -670,15 +671,14 @@ impl_runtime_apis! { impl subtensor_custom_rpc_runtime_api::DelegateInfoRuntimeApi for Runtime { fn get_delegates() -> Vec { let result = SubtensorModule::get_delegates(); - serde_json::to_string(&result).expect("Could not convert DelegateInfo list to JSON") - .as_bytes().to_vec() + result.encode() } fn get_delegate(delegate_account_vec: Vec) -> Vec { - let result = SubtensorModule::get_delegate(delegate_account_vec); - if result.is_some() { - serde_json::to_string(&result).expect("Could not convert DelegateInfo to JSON") - .as_bytes().to_vec() + let _result = SubtensorModule::get_delegate(delegate_account_vec); + if _result.is_some() { + let result = _result.expect("Could not convert DelegateInfo to JSON"); + result.encode() } else { vec![] } @@ -688,15 +688,14 @@ impl_runtime_apis! { impl subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi for Runtime { fn get_neurons(netuid: u16) -> Vec { let result = SubtensorModule::get_neurons(netuid); - serde_json::to_string(&result).expect("Could not convert NeuronInfo Vec to JSON") - .as_bytes().to_vec() + result.encode() } fn get_neuron(netuid: u16, uid: u16) -> Vec { - let result = SubtensorModule::get_neuron(netuid, uid); - if result.is_some() { - serde_json::to_string(&result).expect("Could not convert NeuronInfo to JSON") - .as_bytes().to_vec() + let _result = SubtensorModule::get_neuron(netuid, uid); + if _result.is_some() { + let result = _result.expect("Could not convert NeuronInfo to JSON"); + result.encode() } else { vec![] } @@ -705,10 +704,10 @@ impl_runtime_apis! { impl subtensor_custom_rpc_runtime_api::SubnetInfoRuntimeApi for Runtime { fn get_subnet_info(netuid: u16) -> Vec { - let result = SubtensorModule::get_subnet_info(netuid); - if result.is_some() { - serde_json::to_string(&result).expect("Could not convert SubnetInfo to JSON") - .as_bytes().to_vec() + let _result = SubtensorModule::get_subnet_info(netuid); + if _result.is_some() { + let result = _result.expect("Could not convert SubnetInfo to JSON"); + result.encode() } else { vec![] } @@ -716,8 +715,7 @@ impl_runtime_apis! { fn get_subnets_info() -> Vec { let result = SubtensorModule::get_subnets_info(); - serde_json::to_string(&result).expect("Could not convert SubnetInfo list to JSON") - .as_bytes().to_vec() + result.encode() } } } From 0f08240face9ecb247b57c896a6fab77aadaae7d Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 16 Mar 2023 18:39:28 -0400 Subject: [PATCH 2/7] only send non-zero weights and bonds --- pallets/subtensor/src/neuron_info.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/pallets/subtensor/src/neuron_info.rs b/pallets/subtensor/src/neuron_info.rs index 4f93bc40a6..1ba908ad8e 100644 --- a/pallets/subtensor/src/neuron_info.rs +++ b/pallets/subtensor/src/neuron_info.rs @@ -24,8 +24,8 @@ pub struct NeuronInfo { dividends: u16, last_update: u64, validator_permit: bool, - weights: Vec, // Vec uid to weight - bonds: Vec, // Vec uid to bond + weights: Vec<(u16, u16)>, // Vec of (uid, weight) + bonds: Vec<(u16, u16)>, // Vec of (uid, bond) pruning_score: u16 } @@ -85,10 +85,16 @@ impl Pallet { let validator_permit = Self::get_validator_permit_for_uid( netuid, uid as u16 ); let weights = Self::get_weights(netuid)[uid as usize].iter() - .map(|x| fixed_proportion_to_u16(*x)).collect::>(); + .enumerate() + .map(|(i, w)| (i as u16, fixed_proportion_to_u16(*w))) + .filter(|(_, b)| *b > 0) + .collect::>(); let bonds = Self::get_bonds(netuid)[uid as usize].iter() - .map(|x| fixed_proportion_to_u16(*x)).collect::>(); + .enumerate() + .map(|(i, b)| (i as u16, fixed_proportion_to_u16(*b))) + .filter(|(_, b)| *b > 0) + .collect::>(); let mut stakes = Vec::<(T::AccountId, u64)>::new(); for ( coldkey, stake ) in < Stake as IterableStorageDoubleMap >::iter_prefix( hotkey.clone() ) { From 3ad985be7112f15394b06bf0ddf63760feae8545 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 16 Mar 2023 19:24:06 -0400 Subject: [PATCH 3/7] use compact ints for custom rpc --- pallets/subtensor/src/delegate_info.rs | 21 ++++--- pallets/subtensor/src/neuron_info.rs | 57 +++++++++-------- pallets/subtensor/src/subnet_info.rs | 87 +++++++++++++------------- runtime/src/lib.rs | 2 +- 4 files changed, 88 insertions(+), 79 deletions(-) diff --git a/pallets/subtensor/src/delegate_info.rs b/pallets/subtensor/src/delegate_info.rs index 3d5a444f89..465e22edcf 100644 --- a/pallets/subtensor/src/delegate_info.rs +++ b/pallets/subtensor/src/delegate_info.rs @@ -5,13 +5,14 @@ use frame_support::pallet_prelude::{Decode, Encode}; extern crate alloc; use alloc::vec::Vec; use sp_core::hexdisplay::AsBytesRef; +use codec::Compact; #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] pub struct DelegateInfo { delegate_ss58: T::AccountId, - take: u16, - nominators: Vec<(T::AccountId, u64)>, // map of nominator_ss58 to stake amount + take: Compact, + nominators: Vec<(T::AccountId, Compact)>, // map of nominator_ss58 to stake amount owner_ss58: T::AccountId } @@ -22,18 +23,22 @@ impl Pallet { } let delegate: AccountIdOf = T::AccountId::decode( &mut delegate_account_vec.as_bytes_ref() ).unwrap(); + // Check delegate exists + if !>::contains_key( delegate.clone() ) { + return None; + } - let mut nominators = Vec::<(T::AccountId, u64)>::new(); + let mut nominators = Vec::<(T::AccountId, Compact)>::new(); for ( nominator, stake ) in < Stake as IterableStorageDoubleMap >::iter_prefix( delegate.clone() ) { - nominators.push( ( nominator.clone(), stake ) ); + nominators.push( ( nominator.clone(), stake.into() ) ); } let owner = >::get( delegate.clone() ); return Some( DelegateInfo { delegate_ss58: delegate.clone(), - take: >::get( delegate.clone() ), + take: >::get( delegate.clone() ).into(), nominators, owner_ss58: owner.clone() }); @@ -42,16 +47,16 @@ impl Pallet { pub fn get_delegates() -> Vec> { let mut delegates = Vec::>::new(); for ( delegate, take ) in < Delegates as IterableStorageMap >::iter() { - let mut nominators = Vec::<(T::AccountId, u64)>::new(); + let mut nominators = Vec::<(T::AccountId, Compact)>::new(); for ( nominator, stake ) in < Stake as IterableStorageDoubleMap >::iter_prefix( delegate.clone() ) { - nominators.push( ( nominator.clone(), stake ) ); + nominators.push( ( nominator.clone(), stake.into() ) ); } let owner = >::get( delegate.clone() ); delegates.push( DelegateInfo { delegate_ss58: delegate.clone(), - take, + take: take.into(), nominators, owner_ss58: owner.clone() }); diff --git a/pallets/subtensor/src/neuron_info.rs b/pallets/subtensor/src/neuron_info.rs index 1ba908ad8e..0a7a8f695a 100644 --- a/pallets/subtensor/src/neuron_info.rs +++ b/pallets/subtensor/src/neuron_info.rs @@ -4,28 +4,29 @@ use frame_support::storage::IterableStorageDoubleMap; use frame_support::pallet_prelude::{Decode, Encode}; extern crate alloc; use alloc::vec::Vec; +use codec::Compact; #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] pub struct NeuronInfo { hotkey: T::AccountId, coldkey: T::AccountId, - uid: u16, - netuid: u16, + uid: Compact, + netuid: Compact, active: bool, axon_info: AxonInfo, prometheus_info: PrometheusInfo, - stake: Vec<(T::AccountId, u64)>, // map of coldkey to stake on this neuron/hotkey (includes delegations) - rank: u16, - emission: u64, - incentive: u16, - consensus: u16, - trust: u16, - validator_trust: u16, - dividends: u16, - last_update: u64, + stake: Vec<(T::AccountId, Compact)>, // map of coldkey to stake on this neuron/hotkey (includes delegations) + rank: Compact, + emission: Compact, + incentive: Compact, + consensus: Compact, + trust: Compact, + validator_trust: Compact, + dividends: Compact, + last_update: Compact, validator_permit: bool, - weights: Vec<(u16, u16)>, // Vec of (uid, weight) - bonds: Vec<(u16, u16)>, // Vec of (uid, bond) + weights: Vec<(Compact, Compact)>, // Vec of (uid, weight) + bonds: Vec<(Compact, Compact)>, // Vec of (uid, bond) pruning_score: u16 } @@ -88,13 +89,15 @@ impl Pallet { .enumerate() .map(|(i, w)| (i as u16, fixed_proportion_to_u16(*w))) .filter(|(_, b)| *b > 0) - .collect::>(); + .map(|(i, b)| (i.into(), b.into())) + .collect::, Compact)>>(); let bonds = Self::get_bonds(netuid)[uid as usize].iter() .enumerate() .map(|(i, b)| (i as u16, fixed_proportion_to_u16(*b))) .filter(|(_, b)| *b > 0) - .collect::>(); + .map(|(i, b)| (i.into(), b.into())) + .collect::, Compact)>>(); let mut stakes = Vec::<(T::AccountId, u64)>::new(); for ( coldkey, stake ) in < Stake as IterableStorageDoubleMap >::iter_prefix( hotkey.clone() ) { @@ -106,24 +109,24 @@ impl Pallet { let neuron = NeuronInfo { hotkey: hotkey.clone(), coldkey: coldkey.clone(), - uid, - netuid, + uid: uid.into(), + netuid: netuid.into(), active, axon_info, prometheus_info, - stake, - rank, - emission, - incentive, - consensus, - trust, - validator_trust, - dividends, - last_update, + stake: stake.into_iter().map(|(k, v)| (k, v.into())).collect(), + rank: rank.into(), + emission: emission.into(), + incentive: incentive.into(), + consensus: consensus.into(), + trust: trust.into(), + validator_trust: validator_trust.into(), + dividends: dividends.into(), + last_update: last_update.into(), validator_permit, weights, bonds, - pruning_score + pruning_score: pruning_score.into() }; return Some(neuron); diff --git a/pallets/subtensor/src/subnet_info.rs b/pallets/subtensor/src/subnet_info.rs index bab75a2dd9..af0c71b653 100644 --- a/pallets/subtensor/src/subnet_info.rs +++ b/pallets/subtensor/src/subnet_info.rs @@ -4,30 +4,31 @@ use frame_support::storage::IterableStorageMap; use frame_support::pallet_prelude::{Decode, Encode}; extern crate alloc; use alloc::vec::Vec; +use codec::Compact; #[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] pub struct SubnetInfo { - netuid: u16, - rho: u16, - kappa: u16, - difficulty: u64, - immunity_period: u16, - validator_batch_size: u16, - validator_sequence_length: u16, - validator_epochs_per_reset: u16, - validator_epoch_length: u16, - max_allowed_validators: u16, - min_allowed_weights: u16, - max_weights_limit: u16, - scaling_law_power: u16, - synergy_scaling_law_power: u16, - subnetwork_n: u16, - max_allowed_uids: u16, - blocks_since_last_step: u64, - tempo: u16, - network_modality: u16, - network_connect: Vec<[u16; 2]>, - emission_values: u64 + netuid: Compact, + rho: Compact, + kappa: Compact, + difficulty: Compact, + immunity_period: Compact, + validator_batch_size: Compact, + validator_sequence_length: Compact, + validator_epochs_per_reset: Compact, + validator_epoch_length: Compact, + max_allowed_validators: Compact, + min_allowed_weights: Compact, + max_weights_limit: Compact, + scaling_law_power: Compact, + synergy_scaling_law_power: Compact, + subnetwork_n: Compact, + max_allowed_uids: Compact, + blocks_since_last_step: Compact, + tempo: Compact, + network_modality: Compact, + network_connect: Vec<[Compact; 2]>, + emission_values: Compact } impl Pallet { @@ -57,34 +58,34 @@ impl Pallet { let emission_values = Self::get_emission_value(netuid); - let mut network_connect: Vec<[u16; 2]> = Vec::<[u16; 2]>::new(); + let mut network_connect: Vec<[Compact; 2]> = Vec::<[Compact; 2]>::new(); for ( _netuid_, con_req) in < NetworkConnect as IterableStorageDoubleMap >::iter_prefix(netuid) { - network_connect.push([_netuid_, con_req]); + network_connect.push([_netuid_.into(), con_req.into()]); } return Some(SubnetInfo { - rho, - kappa, - difficulty, - immunity_period, - netuid, - validator_batch_size, - validator_sequence_length, - validator_epochs_per_reset, - validator_epoch_length, - max_allowed_validators, - min_allowed_weights, - max_weights_limit, - scaling_law_power, - synergy_scaling_law_power, - subnetwork_n, - max_allowed_uids, - blocks_since_last_step, - tempo, - network_modality, + rho: rho.into(), + kappa: kappa.into(), + difficulty: difficulty.into(), + immunity_period: immunity_period.into(), + netuid: netuid.into(), + validator_batch_size: validator_batch_size.into(), + validator_sequence_length: validator_sequence_length.into(), + validator_epochs_per_reset: validator_epochs_per_reset.into(), + validator_epoch_length: validator_epoch_length.into(), + max_allowed_validators: max_allowed_validators.into(), + min_allowed_weights: min_allowed_weights.into(), + max_weights_limit: max_weights_limit.into(), + scaling_law_power: scaling_law_power.into(), + synergy_scaling_law_power: synergy_scaling_law_power.into(), + subnetwork_n: subnetwork_n.into(), + max_allowed_uids: max_allowed_uids.into(), + blocks_since_last_step: blocks_since_last_step.into(), + tempo: tempo.into(), + network_modality: network_modality.into(), network_connect, - emission_values + emission_values: emission_values.into() }) } diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 25e9358f45..0dcd918732 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -111,7 +111,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 105, + spec_version: 107, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From e7ed850ee0bcac11220a4f452caf5182484fd5c7 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 16 Mar 2023 19:31:06 -0400 Subject: [PATCH 4/7] pruning_score can be compact --- pallets/subtensor/src/neuron_info.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/subtensor/src/neuron_info.rs b/pallets/subtensor/src/neuron_info.rs index 0a7a8f695a..9eb2d5d2da 100644 --- a/pallets/subtensor/src/neuron_info.rs +++ b/pallets/subtensor/src/neuron_info.rs @@ -27,7 +27,7 @@ pub struct NeuronInfo { validator_permit: bool, weights: Vec<(Compact, Compact)>, // Vec of (uid, weight) bonds: Vec<(Compact, Compact)>, // Vec of (uid, bond) - pruning_score: u16 + pruning_score: Compact, } impl Pallet { From 70d3d3e84874820e68a48a1e1cc23358d399fa54 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 16 Mar 2023 19:42:31 -0400 Subject: [PATCH 5/7] fix iterate over weights and bonds --- pallets/subtensor/src/neuron_info.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/pallets/subtensor/src/neuron_info.rs b/pallets/subtensor/src/neuron_info.rs index 9eb2d5d2da..9c8628918d 100644 --- a/pallets/subtensor/src/neuron_info.rs +++ b/pallets/subtensor/src/neuron_info.rs @@ -85,16 +85,12 @@ impl Pallet { let last_update = Self::get_last_update_for_uid( netuid, uid as u16 ); let validator_permit = Self::get_validator_permit_for_uid( netuid, uid as u16 ); - let weights = Self::get_weights(netuid)[uid as usize].iter() - .enumerate() - .map(|(i, w)| (i as u16, fixed_proportion_to_u16(*w))) + let weights = >::get(netuid, uid).iter() .filter(|(_, b)| *b > 0) - .map(|(i, b)| (i.into(), b.into())) + .map(|(i, w)| (i.into(), w.into())) .collect::, Compact)>>(); - let bonds = Self::get_bonds(netuid)[uid as usize].iter() - .enumerate() - .map(|(i, b)| (i as u16, fixed_proportion_to_u16(*b))) + let bonds = >::get(netuid, uid).iter() .filter(|(_, b)| *b > 0) .map(|(i, b)| (i.into(), b.into())) .collect::, Compact)>>(); From 403dd95620be30c37c92a4a26a4a64d7ff141ba3 Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 16 Mar 2023 19:48:35 -0400 Subject: [PATCH 6/7] iter differently over stake map --- pallets/subtensor/src/neuron_info.rs | 11 ++++------- runtime/src/lib.rs | 2 +- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/pallets/subtensor/src/neuron_info.rs b/pallets/subtensor/src/neuron_info.rs index 9c8628918d..5e3101f19a 100644 --- a/pallets/subtensor/src/neuron_info.rs +++ b/pallets/subtensor/src/neuron_info.rs @@ -95,12 +95,9 @@ impl Pallet { .map(|(i, b)| (i.into(), b.into())) .collect::, Compact)>>(); - let mut stakes = Vec::<(T::AccountId, u64)>::new(); - for ( coldkey, stake ) in < Stake as IterableStorageDoubleMap >::iter_prefix( hotkey.clone() ) { - stakes.push( (coldkey.clone(), stake) ); - } - - let stake = stakes; + let stake: Vec<(T::AccountId, Compact)> = < Stake as IterableStorageDoubleMap >::iter_prefix( hotkey.clone() ) + .map(|(coldkey, stake)| (coldkey, stake.into())) + .collect(); let neuron = NeuronInfo { hotkey: hotkey.clone(), @@ -110,7 +107,7 @@ impl Pallet { active, axon_info, prometheus_info, - stake: stake.into_iter().map(|(k, v)| (k, v.into())).collect(), + stake, rank: rank.into(), emission: emission.into(), incentive: incentive.into(), diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 0dcd918732..57bf552e9c 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -111,7 +111,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 107, + spec_version: 108, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1, From b17b1ef192c27cb10cb5c3450416581d5ab4f0ea Mon Sep 17 00:00:00 2001 From: Cameron Fairchild Date: Thu, 16 Mar 2023 20:05:26 -0400 Subject: [PATCH 7/7] use filtermap --- pallets/subtensor/src/neuron_info.rs | 7 ++----- runtime/src/lib.rs | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/pallets/subtensor/src/neuron_info.rs b/pallets/subtensor/src/neuron_info.rs index 5e3101f19a..60bb387110 100644 --- a/pallets/subtensor/src/neuron_info.rs +++ b/pallets/subtensor/src/neuron_info.rs @@ -1,5 +1,4 @@ use super::*; -use crate::math::*; use frame_support::storage::IterableStorageDoubleMap; use frame_support::pallet_prelude::{Decode, Encode}; extern crate alloc; @@ -86,13 +85,11 @@ impl Pallet { let validator_permit = Self::get_validator_permit_for_uid( netuid, uid as u16 ); let weights = >::get(netuid, uid).iter() - .filter(|(_, b)| *b > 0) - .map(|(i, w)| (i.into(), w.into())) + .filter_map(|(i, w)| if *w > 0 { Some((i.into(), w.into())) } else { None }) .collect::, Compact)>>(); let bonds = >::get(netuid, uid).iter() - .filter(|(_, b)| *b > 0) - .map(|(i, b)| (i.into(), b.into())) + .filter_map(|(i, b)| if *b > 0 { Some((i.into(), b.into())) } else { None }) .collect::, Compact)>>(); let stake: Vec<(T::AccountId, Compact)> = < Stake as IterableStorageDoubleMap >::iter_prefix( hotkey.clone() ) diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 57bf552e9c..8f05af7d56 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -111,7 +111,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { // `spec_version`, and `authoring_version` are the same between Wasm and native. // This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use // the compatible custom types. - spec_version: 108, + spec_version: 109, impl_version: 1, apis: RUNTIME_API_VERSIONS, transaction_version: 1,