diff --git a/Cargo.lock b/Cargo.lock index 7e78684b1..2f328dfd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10459,6 +10459,7 @@ dependencies = [ "serde", "sp-api", "sp-blockchain", + "sp-core", "sp-rpc", "sp-runtime", "subtensor-custom-rpc-runtime-api", @@ -10470,6 +10471,7 @@ version = "0.0.2" dependencies = [ "frame-support", "pallet-subtensor", + "parity-scale-codec", "serde", "sp-api", ] diff --git a/Cargo.toml b/Cargo.toml index 8f3e35e03..100137c92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,7 +49,9 @@ manual_inspect = "allow" async-trait = "0.1" cargo-husky = { version = "1", default-features = false } clap = "4.5.4" -codec = { version = "3.2.2", default-features = false } +codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false, features = [ + "derive", +] } ed25519-dalek = { version = "2.1.0", default-features = false, features = ["alloc"] } enumflags2 = "0.7.9" futures = "0.3.30" diff --git a/pallets/subtensor/rpc/Cargo.toml b/pallets/subtensor/rpc/Cargo.toml index 861c313d8..d49ef1523 100644 --- a/pallets/subtensor/rpc/Cargo.toml +++ b/pallets/subtensor/rpc/Cargo.toml @@ -21,6 +21,7 @@ serde = { workspace = true, features = ["derive"] } # Substrate packages sp-api = { workspace = true } sp-blockchain = { workspace = true } +sp-core = { workspace = true } sp-rpc = { workspace = true } sp-runtime = { workspace = true } diff --git a/pallets/subtensor/rpc/src/lib.rs b/pallets/subtensor/rpc/src/lib.rs index d99388193..b136d1f01 100644 --- a/pallets/subtensor/rpc/src/lib.rs +++ b/pallets/subtensor/rpc/src/lib.rs @@ -1,12 +1,14 @@ //! RPC interface for the custom Subtensor rpc methods +use codec::{Decode, Encode}; use jsonrpsee::{ core::RpcResult, proc_macros::rpc, types::{error::ErrorObject, ErrorObjectOwned}, }; use sp_blockchain::HeaderBackend; -use sp_runtime::traits::Block as BlockT; +use sp_core::hexdisplay::AsBytesRef; +use sp_runtime::{traits::Block as BlockT, AccountId32}; use std::sync::Arc; use sp_api::ProvideRuntimeApi; @@ -45,10 +47,6 @@ pub trait SubtensorCustomApi { fn get_subnet_info(&self, netuid: u16, at: Option) -> RpcResult>; #[method(name = "subnetInfo_getSubnetsInfo")] fn get_subnets_info(&self, at: Option) -> RpcResult>; - #[method(name = "subnetInfo_getSubnetInfo_v2")] - fn get_subnet_info_v2(&self, netuid: u16, at: Option) -> RpcResult>; - #[method(name = "subnetInfo_getSubnetsInf_v2")] - fn get_subnets_info_v2(&self, at: Option) -> RpcResult>; #[method(name = "subnetInfo_getSubnetHyperparams")] fn get_subnet_hyperparams(&self, netuid: u16, at: Option) -> RpcResult>; @@ -107,9 +105,12 @@ where let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_delegates(at).map_err(|e| { - Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into() - }) + match api.get_delegates(at) { + Err(e) => { + Err(Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into()) + } + Ok(result) => Ok(result.encode()), + } } fn get_delegate( @@ -120,9 +121,20 @@ where let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_delegate(at, delegate_account_vec).map_err(|e| { - Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into() - }) + let delegate_account = match AccountId32::decode(&mut delegate_account_vec.as_bytes_ref()) { + Err(e) => { + return Err( + Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into(), + ) + } + Ok(delegate_account) => delegate_account, + }; + match api.get_delegate(at, delegate_account) { + Err(e) => { + Err(Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into()) + } + Ok(result) => Ok(result.encode()), + } } fn get_delegated( @@ -133,9 +145,21 @@ where let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_delegated(at, delegatee_account_vec).map_err(|e| { - Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into() - }) + let delegatee_account = match AccountId32::decode(&mut delegatee_account_vec.as_bytes_ref()) + { + Err(e) => { + return Err( + Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into(), + ) + } + Ok(delegatee_account) => delegatee_account, + }; + match api.get_delegated(at, delegatee_account) { + Err(e) => { + Err(Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into()) + } + Ok(result) => Ok(result.encode()), + } } fn get_neurons_lite( @@ -146,9 +170,12 @@ where let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_neurons_lite(at, netuid).map_err(|e| { - Error::RuntimeError(format!("Unable to get neurons lite info: {:?}", e)).into() - }) + match api.get_neurons_lite(at, netuid) { + Err(e) => { + Err(Error::RuntimeError(format!("Unable to get neurons lite info: {:?}", e)).into()) + } + Ok(neurons) => Ok(neurons.encode()), + } } fn get_neuron_lite( @@ -160,17 +187,24 @@ where let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_neuron_lite(at, netuid, uid).map_err(|e| { - Error::RuntimeError(format!("Unable to get neurons lite info: {:?}", e)).into() - }) + match api.get_neuron_lite(at, netuid, uid) { + Err(e) => { + Err(Error::RuntimeError(format!("Unable to get neuron lite info: {:?}", e)).into()) + } + Ok(neuron) => Ok(neuron.encode()), + } } fn get_neurons(&self, netuid: u16, at: Option<::Hash>) -> RpcResult> { let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_neurons(at, netuid) - .map_err(|e| Error::RuntimeError(format!("Unable to get neurons info: {:?}", e)).into()) + match api.get_neurons(at, netuid) { + Err(e) => { + Err(Error::RuntimeError(format!("Unable to get neurons info: {:?}", e)).into()) + } + Ok(neurons) => Ok(neurons.encode()), + } } fn get_neuron( @@ -182,8 +216,12 @@ where let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_neuron(at, netuid, uid) - .map_err(|e| Error::RuntimeError(format!("Unable to get neuron info: {:?}", e)).into()) + match api.get_neuron(at, netuid, uid) { + Err(e) => { + Err(Error::RuntimeError(format!("Unable to get neuron info: {:?}", e)).into()) + } + Ok(neuron) => Ok(neuron.encode()), + } } fn get_subnet_info( @@ -194,8 +232,12 @@ where let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_subnet_info(at, netuid) - .map_err(|e| Error::RuntimeError(format!("Unable to get subnet info: {:?}", e)).into()) + match api.get_subnet_info(at, netuid) { + Err(e) => { + Err(Error::RuntimeError(format!("Unable to get subnet info: {:?}", e)).into()) + } + Ok(result) => Ok(result.encode()), + } } fn get_subnet_hyperparams( @@ -206,36 +248,26 @@ where let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_subnet_hyperparams(at, netuid) - .map_err(|e| Error::RuntimeError(format!("Unable to get subnet info: {:?}", e)).into()) + match api.get_subnet_hyperparams(at, netuid) { + Err(e) => Err(Error::RuntimeError(format!( + "Unable to get subnet hyperparam info: {:?}", + e + )) + .into()), + Ok(result) => Ok(result.encode()), + } } fn get_subnets_info(&self, at: Option<::Hash>) -> RpcResult> { let api = self.client.runtime_api(); let at = at.unwrap_or_else(|| self.client.info().best_hash); - api.get_subnets_info(at) - .map_err(|e| Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into()) - } - - fn get_subnet_info_v2( - &self, - netuid: u16, - at: Option<::Hash>, - ) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| self.client.info().best_hash); - - api.get_subnet_info_v2(at, netuid) - .map_err(|e| Error::RuntimeError(format!("Unable to get subnet info: {:?}", e)).into()) - } - - fn get_subnets_info_v2(&self, at: Option<::Hash>) -> RpcResult> { - let api = self.client.runtime_api(); - let at = at.unwrap_or_else(|| self.client.info().best_hash); - - api.get_subnets_info_v2(at) - .map_err(|e| Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into()) + match api.get_subnets_info(at) { + Err(e) => { + Err(Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into()) + } + Ok(result) => Ok(result.encode()), + } } fn get_network_lock_cost(&self, at: Option<::Hash>) -> RpcResult { diff --git a/pallets/subtensor/runtime-api/Cargo.toml b/pallets/subtensor/runtime-api/Cargo.toml index ef3e04947..8705e5ba7 100644 --- a/pallets/subtensor/runtime-api/Cargo.toml +++ b/pallets/subtensor/runtime-api/Cargo.toml @@ -13,6 +13,7 @@ workspace = true [dependencies] sp-api = { workspace = true } +codec = { workspace = true } frame-support = { workspace = true } serde = { workspace = true, features = ["derive"] } diff --git a/pallets/subtensor/runtime-api/src/lib.rs b/pallets/subtensor/runtime-api/src/lib.rs index ca43384b8..8cd786079 100644 --- a/pallets/subtensor/runtime-api/src/lib.rs +++ b/pallets/subtensor/runtime-api/src/lib.rs @@ -1,34 +1,40 @@ #![cfg_attr(not(feature = "std"), no_std)] extern crate alloc; use alloc::vec::Vec; +use codec::Compact; +use frame_support::sp_runtime::AccountId32; +use pallet_subtensor::rpc_info::{ + delegate_info::DelegateInfo, + neuron_info::{NeuronInfo, NeuronInfoLite}, + stake_info::StakeInfo, + subnet_info::{SubnetHyperparams, SubnetInfo}, +}; // Here we declare the runtime API. It is implemented it the `impl` block in // src/neuron_info.rs, src/subnet_info.rs, and src/delegate_info.rs sp_api::decl_runtime_apis! { pub trait DelegateInfoRuntimeApi { - fn get_delegates() -> Vec; - fn get_delegate( delegate_account_vec: Vec ) -> Vec; - fn get_delegated( delegatee_account_vec: Vec ) -> Vec; + fn get_delegates() -> Vec>; + fn get_delegate( delegate_account: AccountId32 ) -> Option>; + fn get_delegated( delegatee_account: AccountId32 ) -> Vec<(DelegateInfo, Compact)>; } pub trait NeuronInfoRuntimeApi { - fn get_neurons(netuid: u16) -> Vec; - fn get_neuron(netuid: u16, uid: u16) -> Vec; - fn get_neurons_lite(netuid: u16) -> Vec; - fn get_neuron_lite(netuid: u16, uid: u16) -> Vec; + fn get_neurons(netuid: u16) -> Vec>; + fn get_neuron(netuid: u16, uid: u16) -> Option>; + fn get_neurons_lite(netuid: u16) -> Vec>; + fn get_neuron_lite(netuid: u16, uid: u16) -> Option>; } pub trait SubnetInfoRuntimeApi { - fn get_subnet_info(netuid: u16) -> Vec; - fn get_subnets_info() -> Vec; - fn get_subnet_info_v2(netuid: u16) -> Vec; - fn get_subnets_info_v2() -> Vec; - fn get_subnet_hyperparams(netuid: u16) -> Vec; + fn get_subnet_info(netuid: u16) -> Option>; + fn get_subnets_info() -> Vec>>; + fn get_subnet_hyperparams(netuid: u16) -> Option; } pub trait StakeInfoRuntimeApi { - fn get_stake_info_for_coldkey( coldkey_account_vec: Vec ) -> Vec; - fn get_stake_info_for_coldkeys( coldkey_account_vecs: Vec> ) -> Vec; + fn get_stake_info_for_coldkey( coldkey_account: AccountId32 ) -> Vec>; + fn get_stake_info_for_coldkeys( coldkey_accounts: Vec ) -> Vec<(AccountId32, Vec>)>; } pub trait SubnetRegistrationRuntimeApi { diff --git a/pallets/subtensor/src/rpc_info/delegate_info.rs b/pallets/subtensor/src/rpc_info/delegate_info.rs index a41b6e17e..a46e1de96 100644 --- a/pallets/subtensor/src/rpc_info/delegate_info.rs +++ b/pallets/subtensor/src/rpc_info/delegate_info.rs @@ -2,18 +2,19 @@ use super::*; use frame_support::pallet_prelude::{Decode, Encode}; use frame_support::storage::IterableStorageMap; use frame_support::IterableStorageDoubleMap; +use sp_runtime::AccountId32; use substrate_fixed::types::U64F64; extern crate alloc; use codec::Compact; use sp_core::hexdisplay::AsBytesRef; -#[freeze_struct("5752e4c650a83e0d")] -#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] -pub struct DelegateInfo { - delegate_ss58: T::AccountId, +#[freeze_struct("66105c2cfec0608d")] +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] +pub struct DelegateInfo { + delegate_ss58: AccountId, take: Compact, - nominators: Vec<(T::AccountId, Compact)>, // map of nominator_ss58 to stake amount - owner_ss58: T::AccountId, + nominators: Vec<(AccountId, Compact)>, // map of nominator_ss58 to stake amount + owner_ss58: AccountId, registrations: Vec>, // Vec of netuid this delegate is registered on validator_permits: Vec>, // Vec of netuid this delegate has validator permit on return_per_1000: Compact, // Delegators current daily return per 1000 TAO staked minus take fee @@ -21,7 +22,7 @@ pub struct DelegateInfo { } impl Pallet { - fn get_delegate_by_existing_account(delegate: AccountIdOf) -> DelegateInfo { + fn get_delegate_by_existing_account(delegate: AccountIdOf) -> DelegateInfo { let mut nominators = Vec::<(T::AccountId, Compact)>::new(); for (nominator, stake) in @@ -82,10 +83,8 @@ impl Pallet { } } - pub fn get_delegate(delegate_account_vec: Vec) -> Option> { - if delegate_account_vec.len() != 32 { - return None; - } + pub fn get_delegate(delegate_account: AccountId32) -> Option> { + let delegate_account_vec = delegate_account.encode(); let delegate: AccountIdOf = T::AccountId::decode(&mut delegate_account_vec.as_bytes_ref()).ok()?; @@ -100,8 +99,8 @@ impl Pallet { /// get all delegates info from storage /// - pub fn get_delegates() -> Vec> { - let mut delegates = Vec::>::new(); + pub fn get_delegates() -> Vec> { + let mut delegates = Vec::>::new(); for delegate in as IterableStorageMap>::iter_keys() { let delegate_info = Self::get_delegate_by_existing_account(delegate.clone()); delegates.push(delegate_info); @@ -112,12 +111,16 @@ impl Pallet { /// get all delegate info and staked token amount for a given delegatee account /// - pub fn get_delegated(delegatee_account_vec: Vec) -> Vec<(DelegateInfo, Compact)> { + pub fn get_delegated( + delegatee_account: AccountId32, + ) -> Vec<(DelegateInfo, Compact)> { + let delegatee_account_vec = delegatee_account.encode(); + let Ok(delegatee) = T::AccountId::decode(&mut delegatee_account_vec.as_bytes_ref()) else { return Vec::new(); // No delegates for invalid account }; - let mut delegates: Vec<(DelegateInfo, Compact)> = Vec::new(); + let mut delegates: Vec<(DelegateInfo, Compact)> = Vec::new(); for delegate in as IterableStorageMap>::iter_keys() { let staked_to_this_delegatee = Self::get_stake_for_coldkey_and_hotkey(&delegatee.clone(), &delegate.clone()); diff --git a/pallets/subtensor/src/rpc_info/neuron_info.rs b/pallets/subtensor/src/rpc_info/neuron_info.rs index be367a566..4838b376f 100644 --- a/pallets/subtensor/src/rpc_info/neuron_info.rs +++ b/pallets/subtensor/src/rpc_info/neuron_info.rs @@ -3,17 +3,17 @@ use frame_support::pallet_prelude::{Decode, Encode}; extern crate alloc; use codec::Compact; -#[freeze_struct("45e69321f5c74b4b")] -#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] -pub struct NeuronInfo { - hotkey: T::AccountId, - coldkey: T::AccountId, +#[freeze_struct("d6da7340b3350951")] +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] +pub struct NeuronInfo { + hotkey: AccountId, + coldkey: AccountId, uid: Compact, netuid: Compact, active: bool, axon_info: AxonInfo, prometheus_info: PrometheusInfo, - stake: Vec<(T::AccountId, Compact)>, // map of coldkey to stake on this neuron/hotkey (includes delegations) + stake: Vec<(AccountId, Compact)>, // map of coldkey to stake on this neuron/hotkey (includes delegations) rank: Compact, emission: Compact, incentive: Compact, @@ -28,17 +28,17 @@ pub struct NeuronInfo { pruning_score: Compact, } -#[freeze_struct("c21f0f4f22bcb2a1")] -#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] -pub struct NeuronInfoLite { - hotkey: T::AccountId, - coldkey: T::AccountId, +#[freeze_struct("3e9eed057f379b3b")] +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] +pub struct NeuronInfoLite { + hotkey: AccountId, + coldkey: AccountId, uid: Compact, netuid: Compact, active: bool, axon_info: AxonInfo, prometheus_info: PrometheusInfo, - stake: Vec<(T::AccountId, Compact)>, // map of coldkey to stake on this neuron/hotkey (includes delegations) + stake: Vec<(AccountId, Compact)>, // map of coldkey to stake on this neuron/hotkey (includes delegations) rank: Compact, emission: Compact, incentive: Compact, @@ -53,7 +53,7 @@ pub struct NeuronInfoLite { } 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(); } @@ -71,7 +71,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 = match Self::get_hotkey_for_net_and_uid(netuid, uid) { Ok(h) => h, Err(_) => return None, @@ -146,7 +146,7 @@ impl Pallet { 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; } @@ -154,7 +154,10 @@ impl Pallet { Self::get_neuron_subnet_exists(netuid, uid) } - fn get_neuron_lite_subnet_exists(netuid: u16, uid: u16) -> Option> { + fn get_neuron_lite_subnet_exists( + netuid: u16, + uid: u16, + ) -> Option> { let hotkey = match Self::get_hotkey_for_net_and_uid(netuid, uid) { Ok(h) => h, Err(_) => return None, @@ -207,12 +210,12 @@ impl Pallet { Some(neuron) } - pub fn get_neurons_lite(netuid: u16) -> Vec> { + pub fn get_neurons_lite(netuid: u16) -> Vec> { if !Self::if_subnet_exist(netuid) { return Vec::new(); } - let mut neurons: Vec> = Vec::new(); + let mut neurons: Vec> = Vec::new(); let n = Self::get_subnetwork_n(netuid); for uid in 0..n { let neuron = match Self::get_neuron_lite_subnet_exists(netuid, uid) { @@ -225,7 +228,7 @@ impl Pallet { neurons } - pub fn get_neuron_lite(netuid: u16, uid: u16) -> Option> { + pub fn get_neuron_lite(netuid: u16, uid: u16) -> Option> { if !Self::if_subnet_exist(netuid) { return None; } diff --git a/pallets/subtensor/src/rpc_info/stake_info.rs b/pallets/subtensor/src/rpc_info/stake_info.rs index 1b39e9936..43a1e7070 100644 --- a/pallets/subtensor/src/rpc_info/stake_info.rs +++ b/pallets/subtensor/src/rpc_info/stake_info.rs @@ -2,27 +2,26 @@ use super::*; use frame_support::pallet_prelude::{Decode, Encode}; extern crate alloc; use codec::Compact; -use sp_core::hexdisplay::AsBytesRef; -#[freeze_struct("86d64c14d71d44b9")] -#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] -pub struct StakeInfo { - hotkey: T::AccountId, - coldkey: T::AccountId, +#[freeze_struct("7ba412c8ac3f4677")] +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] +pub struct StakeInfo { + hotkey: AccountId, + coldkey: AccountId, stake: Compact, } impl Pallet { fn _get_stake_info_for_coldkeys( coldkeys: Vec, - ) -> Vec<(T::AccountId, Vec>)> { + ) -> Vec<(T::AccountId, Vec>)> { if coldkeys.is_empty() { return Vec::new(); // No coldkeys to check } - let mut stake_info: Vec<(T::AccountId, Vec>)> = Vec::new(); + let mut stake_info: Vec<(T::AccountId, Vec>)> = Vec::new(); for coldkey_ in coldkeys { - let mut stake_info_for_coldkey: Vec> = Vec::new(); + let mut stake_info_for_coldkey: Vec> = Vec::new(); for (hotkey, coldkey, stake) in >::iter() { if coldkey == coldkey_ { @@ -41,19 +40,8 @@ impl Pallet { } pub fn get_stake_info_for_coldkeys( - coldkey_account_vecs: Vec>, - ) -> Vec<(T::AccountId, Vec>)> { - let mut coldkeys: Vec = Vec::new(); - for coldkey_account_vec in coldkey_account_vecs { - if coldkey_account_vec.len() != 32 { - continue; // Invalid coldkey - } - let Ok(coldkey) = T::AccountId::decode(&mut coldkey_account_vec.as_bytes_ref()) else { - continue; - }; - coldkeys.push(coldkey); - } - + coldkeys: Vec, + ) -> Vec<(T::AccountId, Vec>)> { if coldkeys.is_empty() { return Vec::new(); // Invalid coldkey } @@ -61,14 +49,7 @@ impl Pallet { Self::_get_stake_info_for_coldkeys(coldkeys) } - pub fn get_stake_info_for_coldkey(coldkey_account_vec: Vec) -> Vec> { - if coldkey_account_vec.len() != 32 { - return Vec::new(); // Invalid coldkey - } - - let Ok(coldkey) = T::AccountId::decode(&mut coldkey_account_vec.as_bytes_ref()) else { - return Vec::new(); - }; + pub fn get_stake_info_for_coldkey(coldkey: T::AccountId) -> Vec> { let stake_info = Self::_get_stake_info_for_coldkeys(vec![coldkey]); if stake_info.is_empty() { diff --git a/pallets/subtensor/src/rpc_info/subnet_info.rs b/pallets/subtensor/src/rpc_info/subnet_info.rs index bdd420821..2cc4440bf 100644 --- a/pallets/subtensor/src/rpc_info/subnet_info.rs +++ b/pallets/subtensor/src/rpc_info/subnet_info.rs @@ -4,9 +4,9 @@ use frame_support::storage::IterableStorageMap; extern crate alloc; use codec::Compact; -#[freeze_struct("fe79d58173da662a")] -#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] -pub struct SubnetInfo { +#[freeze_struct("50e2f95a64f9c6f6")] +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] +pub struct SubnetInfo { netuid: Compact, rho: Compact, kappa: Compact, @@ -24,35 +24,12 @@ pub struct SubnetInfo { network_connect: Vec<[u16; 2]>, emission_values: Compact, burn: Compact, - owner: T::AccountId, -} - -#[freeze_struct("65f931972fa13222")] -#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] -pub struct SubnetInfov2 { - netuid: Compact, - rho: Compact, - kappa: Compact, - difficulty: Compact, - immunity_period: Compact, - max_allowed_validators: Compact, - min_allowed_weights: Compact, - max_weights_limit: Compact, - scaling_law_power: Compact, - subnetwork_n: Compact, - max_allowed_uids: Compact, - blocks_since_last_step: Compact, - tempo: Compact, - network_modality: Compact, - network_connect: Vec<[u16; 2]>, - emission_values: Compact, - burn: Compact, - owner: T::AccountId, + owner: AccountId, identity: Option, } -#[freeze_struct("55b472510f10e76a")] -#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug)] +#[freeze_struct("4714b5e2336f7b19")] +#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)] pub struct SubnetHyperparams { rho: Compact, kappa: Compact, @@ -84,77 +61,7 @@ pub struct SubnetHyperparams { } impl Pallet { - pub fn get_subnet_info(netuid: u16) -> Option> { - if !Self::if_subnet_exist(netuid) { - return None; - } - - let rho = Self::get_rho(netuid); - let kappa = Self::get_kappa(netuid); - let difficulty: Compact = Self::get_difficulty_as_u64(netuid).into(); - let immunity_period = Self::get_immunity_period(netuid); - let max_allowed_validators = Self::get_max_allowed_validators(netuid); - let min_allowed_weights = Self::get_min_allowed_weights(netuid); - let max_weights_limit = Self::get_max_weight_limit(netuid); - let scaling_law_power = Self::get_scaling_law_power(netuid); - let subnetwork_n = Self::get_subnetwork_n(netuid); - let max_allowed_uids = Self::get_max_allowed_uids(netuid); - let blocks_since_last_step = Self::get_blocks_since_last_step(netuid); - let tempo = Self::get_tempo(netuid); - let network_modality = >::get(netuid); - let emission_values = Self::get_emission_value(netuid); - let burn: Compact = Self::get_burn_as_u64(netuid).into(); - // DEPRECATED - let network_connect: Vec<[u16; 2]> = Vec::<[u16; 2]>::new(); - // DEPRECATED for ( _netuid_, con_req) in < NetworkConnect as IterableStorageDoubleMap >::iter_prefix(netuid) { - // network_connect.push([_netuid_, con_req]); - // } - - Some(SubnetInfo { - rho: rho.into(), - kappa: kappa.into(), - difficulty, - immunity_period: immunity_period.into(), - netuid: netuid.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(), - 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.into(), - burn, - owner: Self::get_subnet_owner(netuid), - }) - } - - pub fn get_subnets_info() -> Vec>> { - let mut subnet_netuids = Vec::::new(); - let mut max_netuid: u16 = 0; - for (netuid, added) in as IterableStorageMap>::iter() { - if added { - subnet_netuids.push(netuid); - if netuid > max_netuid { - max_netuid = netuid; - } - } - } - - let mut subnets_info = Vec::>>::new(); - for netuid_ in 0..=max_netuid { - if subnet_netuids.contains(&netuid_) { - subnets_info.push(Self::get_subnet_info(netuid_)); - } - } - - subnets_info - } - - pub fn get_subnet_info_v2(netuid: u16) -> Option> { + pub fn get_subnet_info(netuid: u16) -> Option> { if !Self::if_subnet_exist(netuid) { return None; } @@ -182,7 +89,7 @@ impl Pallet { // network_connect.push([_netuid_, con_req]); // } - Some(SubnetInfov2 { + Some(SubnetInfo { rho: rho.into(), kappa: kappa.into(), difficulty, @@ -204,7 +111,8 @@ impl Pallet { identity, }) } - pub fn get_subnets_info_v2() -> Vec>> { + + pub fn get_subnets_info() -> Vec>> { let mut subnet_netuids = Vec::::new(); let mut max_netuid: u16 = 0; for (netuid, added) in as IterableStorageMap>::iter() { @@ -216,7 +124,7 @@ impl Pallet { } } - let mut subnets_info = Vec::>>::new(); + let mut subnets_info = Vec::>>::new(); for netuid_ in 0..=max_netuid { if subnet_netuids.contains(&netuid_) { subnets_info.push(Self::get_subnet_info(netuid_)); @@ -225,6 +133,7 @@ impl Pallet { subnets_info } + pub fn get_subnet_hyperparams(netuid: u16) -> Option { if !Self::if_subnet_exist(netuid) { return None; diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 2455da5d0..c6d57d11f 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -11,7 +11,7 @@ include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); pub mod check_nonce; mod migrations; -use codec::{Decode, Encode, MaxEncodedLen}; +use codec::{Compact, Decode, Encode, MaxEncodedLen}; use frame_support::traits::Imbalance; use frame_support::{ dispatch::DispatchResultWithPostInfo, @@ -30,6 +30,12 @@ use pallet_grandpa::{ fg_primitives, AuthorityId as GrandpaId, AuthorityList as GrandpaAuthorityList, }; use pallet_registry::CanRegisterIdentity; +use pallet_subtensor::rpc_info::{ + delegate_info::DelegateInfo, + neuron_info::{NeuronInfo, NeuronInfoLite}, + stake_info::StakeInfo, + subnet_info::{SubnetHyperparams, SubnetInfo}, +}; use scale_info::TypeInfo; use smallvec::smallvec; use sp_api::impl_runtime_apis; @@ -1889,110 +1895,58 @@ impl_runtime_apis! { } impl subtensor_custom_rpc_runtime_api::DelegateInfoRuntimeApi for Runtime { - fn get_delegates() -> Vec { - let result = SubtensorModule::get_delegates(); - result.encode() + fn get_delegates() -> Vec> { + SubtensorModule::get_delegates() } - fn get_delegate(delegate_account_vec: Vec) -> Vec { - let _result = SubtensorModule::get_delegate(delegate_account_vec); - if _result.is_some() { - let result = _result.expect("Could not get DelegateInfo"); - result.encode() - } else { - vec![] - } + fn get_delegate(delegate_account: AccountId32) -> Option> { + SubtensorModule::get_delegate(delegate_account) } - fn get_delegated(delegatee_account_vec: Vec) -> Vec { - let result = SubtensorModule::get_delegated(delegatee_account_vec); - result.encode() + fn get_delegated(delegatee_account: AccountId32) -> Vec<(DelegateInfo, Compact)> { + SubtensorModule::get_delegated(delegatee_account) } } impl subtensor_custom_rpc_runtime_api::NeuronInfoRuntimeApi for Runtime { - fn get_neurons_lite(netuid: u16) -> Vec { - let result = SubtensorModule::get_neurons_lite(netuid); - result.encode() + fn get_neurons_lite(netuid: u16) -> Vec> { + SubtensorModule::get_neurons_lite(netuid) } - fn get_neuron_lite(netuid: u16, uid: u16) -> Vec { - let _result = SubtensorModule::get_neuron_lite(netuid, uid); - if _result.is_some() { - let result = _result.expect("Could not get NeuronInfoLite"); - result.encode() - } else { - vec![] - } + fn get_neuron_lite(netuid: u16, uid: u16) -> Option> { + SubtensorModule::get_neuron_lite(netuid, uid) } - fn get_neurons(netuid: u16) -> Vec { - let result = SubtensorModule::get_neurons(netuid); - result.encode() + fn get_neurons(netuid: u16) -> Vec> { + SubtensorModule::get_neurons(netuid) } - fn get_neuron(netuid: u16, uid: u16) -> Vec { - let _result = SubtensorModule::get_neuron(netuid, uid); - if _result.is_some() { - let result = _result.expect("Could not get NeuronInfo"); - result.encode() - } else { - vec![] - } + fn get_neuron(netuid: u16, uid: u16) -> Option> { + SubtensorModule::get_neuron(netuid, uid) } } 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() { - let result = _result.expect("Could not get SubnetInfo"); - result.encode() - } else { - vec![] - } + fn get_subnet_info(netuid: u16) -> Option> { + SubtensorModule::get_subnet_info(netuid) } - fn get_subnets_info() -> Vec { - let result = SubtensorModule::get_subnets_info(); - result.encode() + fn get_subnets_info() -> Vec>> { + SubtensorModule::get_subnets_info() } - fn get_subnet_info_v2(netuid: u16) -> Vec { - let _result = SubtensorModule::get_subnet_info_v2(netuid); - if _result.is_some() { - let result = _result.expect("Could not get SubnetInfo"); - result.encode() - } else { - vec![] - } - } - - fn get_subnets_info_v2() -> Vec { - let result = SubtensorModule::get_subnets_info_v2(); - result.encode() - } - - fn get_subnet_hyperparams(netuid: u16) -> Vec { - let _result = SubtensorModule::get_subnet_hyperparams(netuid); - if _result.is_some() { - let result = _result.expect("Could not get SubnetHyperparams"); - result.encode() - } else { - vec![] - } + fn get_subnet_hyperparams(netuid: u16) -> Option { + SubtensorModule::get_subnet_hyperparams(netuid) } } impl subtensor_custom_rpc_runtime_api::StakeInfoRuntimeApi for Runtime { - fn get_stake_info_for_coldkey( coldkey_account_vec: Vec ) -> Vec { - let result = SubtensorModule::get_stake_info_for_coldkey( coldkey_account_vec ); - result.encode() + fn get_stake_info_for_coldkey( coldkey_account: AccountId32 ) -> Vec> { + SubtensorModule::get_stake_info_for_coldkey( coldkey_account ) } - fn get_stake_info_for_coldkeys( coldkey_account_vecs: Vec> ) -> Vec { - let result = SubtensorModule::get_stake_info_for_coldkeys( coldkey_account_vecs ); - result.encode() + fn get_stake_info_for_coldkeys( coldkey_accounts: Vec ) -> Vec<(AccountId32, Vec>)> { + SubtensorModule::get_stake_info_for_coldkeys( coldkey_accounts ) } }