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

Open
wants to merge 9 commits into
base: devnet-ready
Choose a base branch
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.

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
172 changes: 128 additions & 44 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 @@ -116,9 +117,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 @@ -129,9 +133,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 @@ -142,9 +157,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 @@ -155,9 +181,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 @@ -169,17 +198,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 @@ -191,8 +227,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 @@ -203,8 +243,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 @@ -215,23 +259,36 @@ 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_all_metagraphs(&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_metagraphs(at)
.map_err(|e| Error::RuntimeError(format!("Unable to get metagraps: {:?}", e)).into())

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

fn get_dynamic_info(
Expand All @@ -241,9 +298,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_metagraph(
Expand All @@ -253,9 +316,14 @@ where
) -> RpcResult<Vec<u8>> {
let api = self.client.runtime_api();
let at = at.unwrap_or_else(|| self.client.info().best_hash);
api.get_metagraph(at, netuid).map_err(|e| {
Error::RuntimeError(format!("Unable to get dynamic subnets info: {:?}", e)).into()
})
match api.get_metagraph(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 @@ -265,17 +333,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 @@ -286,16 +362,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 = []
Loading
Loading