Skip to content
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

Feat/no more weird runtime api encoding #1162

Draft
wants to merge 4 commits into
base: devnet-ready
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

4 changes: 1 addition & 3 deletions pallets/subtensor/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ publish = false
workspace = true

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = [
"derive",
] }
codec = { workspace = true }
jsonrpsee = { workspace = true, features = ["client-core", "server", "macros"] }
serde = { workspace = true, features = ["derive"] }

Expand Down
154 changes: 115 additions & 39 deletions pallets/subtensor/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! 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_runtime::{traits::Block as BlockT, AccountId32};
use std::sync::Arc;

use sp_api::ProvideRuntimeApi;
Expand Down Expand Up @@ -112,9 +113,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) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into())
}
}
}

fn get_delegate(
Expand All @@ -125,9 +129,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[..]) {
Ok(delegate_account) => delegate_account,
Err(e) => {
return Err(
Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into(),
)
}
};
match api.get_delegate(at, delegate_account) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into())
}
}
}

fn get_delegated(
Expand All @@ -138,9 +153,20 @@ 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[..]) {
Ok(delegatee_account) => delegatee_account,
Err(e) => {
return Err(
Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into(),
)
}
};
match api.get_delegated(at, delegatee_account) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get delegates info: {:?}", e)).into())
}
}
}

fn get_neurons_lite(
Expand All @@ -151,9 +177,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) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get neurons lite info: {:?}", e)).into())
}
}
}

fn get_neuron_lite(
Expand All @@ -165,17 +194,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) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get neurons lite info: {:?}", e)).into())
}
}
}

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) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get neurons info: {:?}", e)).into())
}
}
}

fn get_neuron(
Expand All @@ -187,8 +223,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) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get neuron info: {:?}", e)).into())
}
}
}

fn get_subnet_info(
Expand All @@ -199,8 +239,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) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get subnet info: {:?}", e)).into())
}
}
}

fn get_subnet_hyperparams(
Expand All @@ -211,16 +255,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) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get subnet info: {:?}", e)).into())
}
}
}

fn get_all_dynamic_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_all_dynamic_info(at).map_err(|e| {
Error::RuntimeError(format!("Unable to get dynamic subnets info: {:?}", e)).into()
})

match api.get_all_dynamic_info(at) {
Ok(result) => Ok(result.encode()),
Err(e) => Err(Error::RuntimeError(format!(
"Unable to get dynamic subnets info: {:?}",
e
))
.into()),
}
}

fn get_dynamic_info(
Expand All @@ -230,9 +284,15 @@ where
) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);
api.get_dynamic_info(at, netuid).map_err(|e| {
Error::RuntimeError(format!("Unable to get dynamic subnets info: {:?}", e)).into()
})

match api.get_dynamic_info(at, netuid) {
Ok(result) => Ok(result.encode()),
Err(e) => Err(Error::RuntimeError(format!(
"Unable to get dynamic subnets info: {:?}",
e
))
.into()),
}
}

fn get_subnet_state(
Expand All @@ -242,17 +302,25 @@ where
) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);
api.get_subnet_state(at, netuid).map_err(|e| {
Error::RuntimeError(format!("Unable to get subnet state info: {:?}", e)).into()
})

match api.get_subnet_state(at, netuid) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get subnet state info: {:?}", e)).into())
}
}
}

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())
match api.get_subnets_info(at) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into())
}
}
}

fn get_subnet_info_v2(
Expand All @@ -263,16 +331,24 @@ where
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())
match api.get_subnet_info_v2(at, netuid) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(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_v2(at) {
Ok(result) => Ok(result.encode()),
Err(e) => {
Err(Error::RuntimeError(format!("Unable to get subnets info: {:?}", e)).into())
}
}
}

fn get_network_lock_cost(&self, at: Option<<Block as BlockT>::Hash>) -> RpcResult<u64> {
Expand Down
7 changes: 5 additions & 2 deletions pallets/subtensor/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ workspace = true

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

codec = { workspace = true }
# local
pallet-subtensor = { version = "4.0.0-dev", path = "../../subtensor", default-features = false }

[features]
default = ["std"]
std = [
"sp-api/std",
"sp-runtime/std",
"frame-support/std",
"pallet-subtensor/std",
"serde/std"
"serde/std",
"codec/std"
]
pow-faucet = []
44 changes: 27 additions & 17 deletions pallets/subtensor/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,47 @@
#![cfg_attr(not(feature = "std"), no_std)]
extern crate alloc;
use alloc::vec::Vec;
use codec::Compact;
use pallet_subtensor::rpc_info::{
delegate_info::DelegateInfo,
dynamic_info::DynamicInfo,
neuron_info::{NeuronInfo, NeuronInfoLite},
show_subnet::SubnetState,
stake_info::StakeInfo,
subnet_info::{SubnetHyperparams, SubnetInfo, SubnetInfov2},
};
use sp_runtime::AccountId32;

// 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_all_dynamic_info() -> Vec<u8>;
fn get_dynamic_info(netuid: u16) -> Vec<u8>;
fn get_subnet_state(netuid: u16) -> Vec<u8>;
fn get_subnet_info(netuid: u16) -> Option<SubnetInfo<AccountId32>>;
fn get_subnets_info() -> Vec<Option<SubnetInfo<AccountId32>>>;
fn get_subnet_info_v2(netuid: u16) -> Option<SubnetInfov2<AccountId32>>;
fn get_subnets_info_v2() -> Vec<Option<SubnetInfov2<AccountId32>>>;
fn get_subnet_hyperparams(netuid: u16) -> Option<SubnetHyperparams>;
fn get_all_dynamic_info() -> Vec<Option<DynamicInfo<AccountId32>>>;
fn get_dynamic_info(netuid: u16) -> Option<DynamicInfo<AccountId32>>;
fn get_subnet_state(netuid: u16) -> Option<SubnetState<AccountId32>>;
}

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