Skip to content

Feat/runtime apis return types #832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ manual_inspect = "allow"
[workspace.dependencies]
cargo-husky = { version = "1", default-features = false }
clap = "4.5.4"
codec = { version = "3.2.2", default-features = false }
enumflags2 = "0.7.9"
futures = "0.3.30"
hex = { version = "0.4", default-features = false }
Expand Down Expand Up @@ -80,6 +79,8 @@ walkdir = "2"

subtensor-macros = { path = "support/macros" }

codec = { package = "parity-scale-codec", version = "3.2.2", default-features = false }

frame-benchmarking = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "v1.16.0-rc1", default-features = false }
frame-benchmarking-cli = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "v1.16.0-rc1" }
frame-executive = { git = "https://github.com/paritytech/polkadot-sdk.git", tag = "v1.16.0-rc1", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions pallets/subtensor/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand Down
132 changes: 82 additions & 50 deletions pallets/subtensor/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -45,10 +47,6 @@ pub trait SubtensorCustomApi<BlockHash> {
fn get_subnet_info(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
#[method(name = "subnetInfo_getSubnetsInfo")]
fn get_subnets_info(&self, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
#[method(name = "subnetInfo_getSubnetInfo_v2")]
fn get_subnet_info_v2(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
#[method(name = "subnetInfo_getSubnetsInf_v2")]
fn get_subnets_info_v2(&self, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;
#[method(name = "subnetInfo_getSubnetHyperparams")]
fn get_subnet_hyperparams(&self, netuid: u16, at: Option<BlockHash>) -> RpcResult<Vec<u8>>;

Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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<<Block as BlockT>::Hash>) -> RpcResult<Vec<u8>> {
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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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<<Block as BlockT>::Hash>) -> RpcResult<Vec<u8>> {
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<<Block as BlockT>::Hash>,
) -> RpcResult<Vec<u8>> {
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<<Block as BlockT>::Hash>) -> RpcResult<Vec<u8>> {
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<<Block as BlockT>::Hash>) -> RpcResult<u64> {
Expand Down
1 change: 1 addition & 0 deletions pallets/subtensor/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ workspace = true

[dependencies]
sp-api = { workspace = true }
codec = { workspace = true }
frame-support = { workspace = true }
serde = { workspace = true, features = ["derive"] }

Expand Down
34 changes: 20 additions & 14 deletions pallets/subtensor/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<u8>;
fn get_delegate( delegate_account_vec: Vec<u8> ) -> Vec<u8>;
fn get_delegated( delegatee_account_vec: Vec<u8> ) -> Vec<u8>;
fn get_delegates() -> Vec<DelegateInfo<AccountId32>>;
fn get_delegate( delegate_account: AccountId32 ) -> Option<DelegateInfo<AccountId32>>;
fn get_delegated( delegatee_account: AccountId32 ) -> Vec<(DelegateInfo<AccountId32>, Compact<u64>)>;
}

pub trait NeuronInfoRuntimeApi {
fn get_neurons(netuid: u16) -> Vec<u8>;
fn get_neuron(netuid: u16, uid: u16) -> Vec<u8>;
fn get_neurons_lite(netuid: u16) -> Vec<u8>;
fn get_neuron_lite(netuid: u16, uid: u16) -> Vec<u8>;
fn get_neurons(netuid: u16) -> Vec<NeuronInfo<AccountId32>>;
fn get_neuron(netuid: u16, uid: u16) -> Option<NeuronInfo<AccountId32>>;
fn get_neurons_lite(netuid: u16) -> Vec<NeuronInfoLite<AccountId32>>;
fn get_neuron_lite(netuid: u16, uid: u16) -> Option<NeuronInfoLite<AccountId32>>;
}

pub trait SubnetInfoRuntimeApi {
fn get_subnet_info(netuid: u16) -> Vec<u8>;
fn get_subnets_info() -> Vec<u8>;
fn get_subnet_info_v2(netuid: u16) -> Vec<u8>;
fn get_subnets_info_v2() -> Vec<u8>;
fn get_subnet_hyperparams(netuid: u16) -> Vec<u8>;
fn get_subnet_info(netuid: u16) -> Option<SubnetInfo<AccountId32>>;
fn get_subnets_info() -> Vec<Option<SubnetInfo<AccountId32>>>;
fn get_subnet_hyperparams(netuid: u16) -> Option<SubnetHyperparams>;
}

pub trait StakeInfoRuntimeApi {
fn get_stake_info_for_coldkey( coldkey_account_vec: Vec<u8> ) -> Vec<u8>;
fn get_stake_info_for_coldkeys( coldkey_account_vecs: Vec<Vec<u8>> ) -> Vec<u8>;
fn get_stake_info_for_coldkey( coldkey_account: AccountId32 ) -> Vec<StakeInfo<AccountId32>>;
fn get_stake_info_for_coldkeys( coldkey_accounts: Vec<AccountId32> ) -> Vec<(AccountId32, Vec<StakeInfo<AccountId32>>)>;
}

pub trait SubnetRegistrationRuntimeApi {
Expand Down
Loading
Loading