From 7c8780e9173422541827d0659acd172356a28daf Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 12 Apr 2022 17:38:48 +0800 Subject: [PATCH 1/2] add system_health rpc --- subxt/src/rpc.rs | 71 ++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 41 deletions(-) diff --git a/subxt/src/rpc.rs b/subxt/src/rpc.rs index dd95146476..5b8a542134 100644 --- a/subxt/src/rpc.rs +++ b/subxt/src/rpc.rs @@ -21,63 +21,33 @@ // Related: https://github.com/paritytech/subxt/issues/66 #![allow(irrefutable_let_patterns)] -use std::{ - collections::HashMap, - sync::Arc, -}; +use std::{collections::HashMap, sync::Arc}; use crate::{ - error::BasicError, - storage::StorageKeyPrefix, - Config, - Metadata, - PhantomDataSendSync, -}; -use codec::{ - Decode, - Encode, + error::BasicError, storage::StorageKeyPrefix, Config, Metadata, PhantomDataSendSync, }; +use codec::{Decode, Encode}; use frame_metadata::RuntimeMetadataPrefixed; pub use jsonrpsee::{ client_transport::ws::{ - InvalidUri, - Receiver as WsReceiver, - Sender as WsSender, - Uri, + InvalidUri, Receiver as WsReceiver, Sender as WsSender, Uri, WsTransportClientBuilder, }, core::{ client::{ - Client as RpcClient, - ClientBuilder as RpcClientBuilder, - ClientT, - Subscription, - SubscriptionClientT, + Client as RpcClient, ClientBuilder as RpcClientBuilder, ClientT, + Subscription, SubscriptionClientT, }, - to_json_value, - DeserializeOwned, - Error as RpcError, - JsonValue, + to_json_value, DeserializeOwned, Error as RpcError, JsonValue, }, rpc_params, }; -use serde::{ - Deserialize, - Serialize, -}; +use serde::{Deserialize, Serialize}; use sp_core::{ - storage::{ - StorageChangeSet, - StorageData, - StorageKey, - }, - Bytes, - U256, -}; -use sp_runtime::generic::{ - Block, - SignedBlock, + storage::{StorageChangeSet, StorageData, StorageKey}, + Bytes, U256, }; +use sp_runtime::generic::{Block, SignedBlock}; /// A number type that can be serialized both as a number or a string that encodes a number in a /// string. @@ -222,6 +192,20 @@ pub struct BlockStats { pub num_extrinsics: u64, } +/// Health struct returned by the RPC +#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +pub struct Health { + /// Number of connected peers + pub peers: usize, + /// Is the node syncing + pub is_syncing: bool, + /// Should this node have any peers + /// + /// Might be false for local chains or when running without discovery. + pub should_have_peers: bool, +} + /// Client for substrate rpc interfaces pub struct Rpc { /// Rpc client for sending requests. @@ -329,6 +313,11 @@ impl Rpc { .await?) } + /// Fetch system health + pub async fn system_health(&self) -> Result { + Ok(self.client.request("system_health", rpc_params![]).await?) + } + /// Fetch system chain pub async fn system_chain(&self) -> Result { Ok(self.client.request("system_chain", rpc_params![]).await?) From 1f36e4811d1d1c106eef0a48a4a8a705a68bcefe Mon Sep 17 00:00:00 2001 From: yjhmelody Date: Tue, 12 Apr 2022 17:40:27 +0800 Subject: [PATCH 2/2] fmt --- subxt/src/rpc.rs | 52 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/subxt/src/rpc.rs b/subxt/src/rpc.rs index 5b8a542134..b606c1964d 100644 --- a/subxt/src/rpc.rs +++ b/subxt/src/rpc.rs @@ -21,33 +21,63 @@ // Related: https://github.com/paritytech/subxt/issues/66 #![allow(irrefutable_let_patterns)] -use std::{collections::HashMap, sync::Arc}; +use std::{ + collections::HashMap, + sync::Arc, +}; use crate::{ - error::BasicError, storage::StorageKeyPrefix, Config, Metadata, PhantomDataSendSync, + error::BasicError, + storage::StorageKeyPrefix, + Config, + Metadata, + PhantomDataSendSync, +}; +use codec::{ + Decode, + Encode, }; -use codec::{Decode, Encode}; use frame_metadata::RuntimeMetadataPrefixed; pub use jsonrpsee::{ client_transport::ws::{ - InvalidUri, Receiver as WsReceiver, Sender as WsSender, Uri, + InvalidUri, + Receiver as WsReceiver, + Sender as WsSender, + Uri, WsTransportClientBuilder, }, core::{ client::{ - Client as RpcClient, ClientBuilder as RpcClientBuilder, ClientT, - Subscription, SubscriptionClientT, + Client as RpcClient, + ClientBuilder as RpcClientBuilder, + ClientT, + Subscription, + SubscriptionClientT, }, - to_json_value, DeserializeOwned, Error as RpcError, JsonValue, + to_json_value, + DeserializeOwned, + Error as RpcError, + JsonValue, }, rpc_params, }; -use serde::{Deserialize, Serialize}; +use serde::{ + Deserialize, + Serialize, +}; use sp_core::{ - storage::{StorageChangeSet, StorageData, StorageKey}, - Bytes, U256, + storage::{ + StorageChangeSet, + StorageData, + StorageKey, + }, + Bytes, + U256, +}; +use sp_runtime::generic::{ + Block, + SignedBlock, }; -use sp_runtime::generic::{Block, SignedBlock}; /// A number type that can be serialized both as a number or a string that encodes a number in a /// string.