mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-13 21:01:05 +00:00
integration with jsonrpsee v2 (#214)
* hacky integration with jsonrpsee v2 * stray todos * fmt * add http support * make test build compile * Update src/rpc.rs * bring back set_client * use crates.io version jsonrpsee * WIP: workaround for embedded subxt client (#236) * workaround for embedded subxt client Signed-off-by: Gregory Hill <gregorydhill@outlook.com> * increase default channel size on subxt client Signed-off-by: Gregory Hill <gregorydhill@outlook.com> * remove client tests due to inference problem on From Signed-off-by: Gregory Hill <gregorydhill@outlook.com> * add comments for missing impls * more verbose errors * make subscription notifs buffer bigger * fmt Co-authored-by: Greg Hill <gregorydhill@outlook.com>
This commit is contained in:
+1
-7
@@ -14,10 +14,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use jsonrpsee::{
|
||||
client::RequestError,
|
||||
transport::ws::WsNewDnsError,
|
||||
};
|
||||
use jsonrpsee_types::error::Error as RequestError;
|
||||
use sp_core::crypto::SecretStringError;
|
||||
use sp_runtime::{
|
||||
transaction_validity::TransactionValidityError,
|
||||
@@ -42,9 +39,6 @@ pub enum Error {
|
||||
/// Rpc error.
|
||||
#[error("Rpc error: {0}")]
|
||||
Rpc(#[from] RequestError),
|
||||
/// Error that can happen during the initial websocket handshake
|
||||
#[error("Rpc error: {0}")]
|
||||
WsHandshake(#[from] WsNewDnsError),
|
||||
/// Serde serialization error
|
||||
#[error("Serde json error: {0}")]
|
||||
Serialization(#[from] serde_json::error::Error),
|
||||
|
||||
@@ -60,7 +60,7 @@ pub struct PutCodeCall<'a, T: Contracts> {
|
||||
/// - The contract is initialized.
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
|
||||
pub struct InstantiateCall<'a, T: Contracts> {
|
||||
/// Initial balance transfered to the contract.
|
||||
/// Initial balance transferred to the contract.
|
||||
#[codec(compact)]
|
||||
pub endowment: <T as Balances>::Balance,
|
||||
/// Gas limit.
|
||||
@@ -188,7 +188,7 @@ mod tests {
|
||||
// fund the account
|
||||
let endowment = 200_000_000_000_000;
|
||||
let _ = client
|
||||
.transfer_and_watch(stash, &new_account_id, endowment)
|
||||
.transfer_and_watch(stash, &new_account_id.into(), endowment)
|
||||
.await
|
||||
.expect("New account balance transfer failed");
|
||||
stash.increment_nonce();
|
||||
@@ -291,8 +291,11 @@ mod tests {
|
||||
let ctx = TestContext::init().await;
|
||||
let code_stored = ctx.put_code().await.unwrap();
|
||||
|
||||
let instantiated = ctx.instantiate(&code_stored.code_hash, &[]).await.unwrap();
|
||||
let executed = ctx.call(&instantiated.contract, &[]).await;
|
||||
let instantiated = ctx
|
||||
.instantiate(&code_stored.code_hash.into(), &[])
|
||||
.await
|
||||
.unwrap();
|
||||
let executed = ctx.call(&instantiated.contract.into(), &[]).await;
|
||||
|
||||
assert!(
|
||||
executed.is_ok(),
|
||||
|
||||
+24
-12
@@ -43,9 +43,6 @@
|
||||
#[macro_use]
|
||||
extern crate substrate_subxt_proc_macro;
|
||||
|
||||
#[cfg(feature = "client")]
|
||||
pub use substrate_subxt_client as client;
|
||||
|
||||
pub use sp_core;
|
||||
pub use sp_runtime;
|
||||
|
||||
@@ -54,7 +51,15 @@ use codec::{
|
||||
Decode,
|
||||
};
|
||||
use futures::future;
|
||||
use jsonrpsee::client::Subscription;
|
||||
use jsonrpsee_http_client::{
|
||||
HttpClient,
|
||||
HttpConfig,
|
||||
};
|
||||
use jsonrpsee_ws_client::{
|
||||
WsClient,
|
||||
WsConfig,
|
||||
WsSubscription as Subscription,
|
||||
};
|
||||
use sp_core::{
|
||||
storage::{
|
||||
StorageChangeSet,
|
||||
@@ -65,7 +70,10 @@ use sp_core::{
|
||||
};
|
||||
pub use sp_runtime::traits::SignedExtension;
|
||||
pub use sp_version::RuntimeVersion;
|
||||
use std::marker::PhantomData;
|
||||
use std::{
|
||||
marker::PhantomData,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
mod error;
|
||||
mod events;
|
||||
@@ -98,6 +106,7 @@ pub use crate::{
|
||||
BlockNumber,
|
||||
ExtrinsicSuccess,
|
||||
ReadProof,
|
||||
RpcClient,
|
||||
SystemProperties,
|
||||
},
|
||||
runtimes::*,
|
||||
@@ -120,7 +129,7 @@ use crate::{
|
||||
#[derive(Default)]
|
||||
pub struct ClientBuilder<T: Runtime> {
|
||||
url: Option<String>,
|
||||
client: Option<jsonrpsee::Client>,
|
||||
client: Option<RpcClient>,
|
||||
page_size: Option<u32>,
|
||||
event_type_registry: EventTypeRegistry<T>,
|
||||
skip_type_sizes_check: bool,
|
||||
@@ -139,7 +148,7 @@ impl<T: Runtime> ClientBuilder<T> {
|
||||
}
|
||||
|
||||
/// Sets the jsonrpsee client.
|
||||
pub fn set_client<P: Into<jsonrpsee::Client>>(mut self, client: P) -> Self {
|
||||
pub fn set_client<C: Into<RpcClient>>(mut self, client: C) -> Self {
|
||||
self.client = Some(client.into());
|
||||
self
|
||||
}
|
||||
@@ -185,9 +194,13 @@ impl<T: Runtime> ClientBuilder<T> {
|
||||
} else {
|
||||
let url = self.url.as_deref().unwrap_or("ws://127.0.0.1:9944");
|
||||
if url.starts_with("ws://") || url.starts_with("wss://") {
|
||||
jsonrpsee::ws_client(url).await?
|
||||
let mut config = WsConfig::with_url(&url);
|
||||
// max notifs per subscription capacity.
|
||||
config.max_subscription_capacity = 4096;
|
||||
RpcClient::WebSocket(WsClient::new(WsConfig::with_url(&url)).await?)
|
||||
} else {
|
||||
jsonrpsee::http_client(url)
|
||||
let client = HttpClient::new(url, HttpConfig::default())?;
|
||||
RpcClient::Http(Arc::new(client))
|
||||
}
|
||||
};
|
||||
let rpc = Rpc::new(client);
|
||||
@@ -670,7 +683,6 @@ mod tests {
|
||||
.expect("Error creating client");
|
||||
(client, tmp)
|
||||
}
|
||||
|
||||
pub(crate) async fn test_client() -> (Client<TestRuntime>, TempDir) {
|
||||
test_client_with(AccountKeyring::Alice).await
|
||||
}
|
||||
@@ -681,7 +693,7 @@ mod tests {
|
||||
let (client, _tmp) = test_client_with(AccountKeyring::Bob).await;
|
||||
let mut blocks = client.subscribe_blocks().await.unwrap();
|
||||
// get the genesis block.
|
||||
assert_eq!(blocks.next().await.number, 0);
|
||||
assert_eq!(blocks.next().await.unwrap().number, 0);
|
||||
let public = AccountKeyring::Alice.public().as_array_ref().to_vec();
|
||||
client
|
||||
.insert_key(
|
||||
@@ -696,7 +708,7 @@ mod tests {
|
||||
.await
|
||||
.unwrap());
|
||||
// Alice is an authority, so blocks should be produced.
|
||||
assert_eq!(blocks.next().await.number, 1);
|
||||
assert_eq!(blocks.next().await.unwrap().number, 1);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
|
||||
+101
-8
@@ -19,6 +19,8 @@
|
||||
// Related: https://github.com/paritytech/substrate-subxt/issues/66
|
||||
#![allow(irrefutable_let_patterns)]
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use codec::{
|
||||
Decode,
|
||||
Encode,
|
||||
@@ -29,13 +31,22 @@ use core::{
|
||||
marker::PhantomData,
|
||||
};
|
||||
use frame_metadata::RuntimeMetadataPrefixed;
|
||||
use jsonrpsee::{
|
||||
client::Subscription,
|
||||
common::{
|
||||
use jsonrpsee_http_client::HttpClient;
|
||||
use jsonrpsee_types::{
|
||||
error::Error as RpcError,
|
||||
jsonrpc::{
|
||||
to_value as to_json_value,
|
||||
DeserializeOwned,
|
||||
Params,
|
||||
},
|
||||
Client,
|
||||
traits::{
|
||||
Client,
|
||||
SubscriptionClient,
|
||||
},
|
||||
};
|
||||
use jsonrpsee_ws_client::{
|
||||
WsClient,
|
||||
WsSubscription as Subscription,
|
||||
};
|
||||
use serde::{
|
||||
Deserialize,
|
||||
@@ -142,6 +153,88 @@ pub enum TransactionStatus<Hash, BlockHash> {
|
||||
Invalid,
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "client", test))]
|
||||
use substrate_subxt_client::SubxtClient;
|
||||
|
||||
/// Rpc client wrapper.
|
||||
/// This is workaround because adding generic types causes the macros to fail.
|
||||
#[derive(Clone)]
|
||||
pub enum RpcClient {
|
||||
/// JSONRPC client WebSocket transport.
|
||||
WebSocket(WsClient),
|
||||
/// JSONRPC client HTTP transport.
|
||||
// NOTE: Arc because `HttpClient` is not clone.
|
||||
Http(Arc<HttpClient>),
|
||||
#[cfg(any(feature = "client", test))]
|
||||
/// Embedded substrate node.
|
||||
Subxt(SubxtClient),
|
||||
}
|
||||
|
||||
impl RpcClient {
|
||||
async fn request<T: DeserializeOwned>(
|
||||
&self,
|
||||
method: &str,
|
||||
params: Params,
|
||||
) -> Result<T, Error> {
|
||||
match self {
|
||||
Self::WebSocket(inner) => {
|
||||
inner.request(method, params).await.map_err(Into::into)
|
||||
}
|
||||
Self::Http(inner) => inner.request(method, params).await.map_err(Into::into),
|
||||
#[cfg(any(feature = "client", test))]
|
||||
Self::Subxt(inner) => inner.request(method, params).await.map_err(Into::into),
|
||||
}
|
||||
}
|
||||
|
||||
async fn subscribe<T: DeserializeOwned>(
|
||||
&self,
|
||||
subscribe_method: &str,
|
||||
params: Params,
|
||||
unsubscribe_method: &str,
|
||||
) -> Result<Subscription<T>, Error> {
|
||||
match self {
|
||||
Self::WebSocket(inner) => {
|
||||
inner
|
||||
.subscribe(subscribe_method, params, unsubscribe_method)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
Self::Http(_) => {
|
||||
Err(RpcError::Custom(
|
||||
"Subscriptions not supported on HTTP transport".to_owned(),
|
||||
)
|
||||
.into())
|
||||
}
|
||||
#[cfg(any(feature = "client", test))]
|
||||
Self::Subxt(inner) => {
|
||||
inner
|
||||
.subscribe(subscribe_method, params, unsubscribe_method)
|
||||
.await
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<WsClient> for RpcClient {
|
||||
fn from(client: WsClient) -> Self {
|
||||
RpcClient::WebSocket(client)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<HttpClient> for RpcClient {
|
||||
fn from(client: HttpClient) -> Self {
|
||||
RpcClient::Http(Arc::new(client))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(any(feature = "client", test))]
|
||||
impl From<SubxtClient> for RpcClient {
|
||||
fn from(client: SubxtClient) -> Self {
|
||||
RpcClient::Subxt(client)
|
||||
}
|
||||
}
|
||||
|
||||
/// ReadProof struct returned by the RPC
|
||||
///
|
||||
/// # Note
|
||||
@@ -159,7 +252,7 @@ pub struct ReadProof<Hash> {
|
||||
|
||||
/// Client for substrate rpc interfaces
|
||||
pub struct Rpc<T: Runtime> {
|
||||
client: Client,
|
||||
client: RpcClient,
|
||||
marker: PhantomData<T>,
|
||||
}
|
||||
|
||||
@@ -173,7 +266,7 @@ impl<T: Runtime> Clone for Rpc<T> {
|
||||
}
|
||||
|
||||
impl<T: Runtime> Rpc<T> {
|
||||
pub fn new(client: Client) -> Self {
|
||||
pub fn new(client: RpcClient) -> Self {
|
||||
Self {
|
||||
client,
|
||||
marker: PhantomData,
|
||||
@@ -434,7 +527,7 @@ impl<T: Runtime> Rpc<T> {
|
||||
let events_sub = self.subscribe_events().await?;
|
||||
let mut xt_sub = self.watch_extrinsic(extrinsic).await?;
|
||||
|
||||
while let status = xt_sub.next().await {
|
||||
while let Some(status) = xt_sub.next().await {
|
||||
// log::info!("received status {:?}", status);
|
||||
match status {
|
||||
// ignore in progress extrinsic for now
|
||||
@@ -497,7 +590,7 @@ impl<T: Runtime> Rpc<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
unreachable!()
|
||||
Err(RpcError::Custom("RPC subscription dropped".into()).into())
|
||||
}
|
||||
|
||||
/// Insert a key into the keystore.
|
||||
|
||||
+10
-2
@@ -14,7 +14,8 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
use jsonrpsee::client::Subscription;
|
||||
use jsonrpsee_types::error::Error as RpcError;
|
||||
use jsonrpsee_ws_client::WsSubscription as Subscription;
|
||||
use sp_core::storage::StorageChangeSet;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
@@ -86,7 +87,14 @@ impl<'a, T: Runtime> EventSubscription<'a, T> {
|
||||
if self.finished {
|
||||
return None
|
||||
}
|
||||
let change_set = self.subscription.next().await;
|
||||
let change_set = match self.subscription.next().await {
|
||||
Some(c) => c,
|
||||
None => {
|
||||
return Some(Err(
|
||||
RpcError::Custom("RPC subscription dropped".into()).into()
|
||||
))
|
||||
}
|
||||
};
|
||||
if let Some(hash) = self.block.as_ref() {
|
||||
if &change_set.block == hash {
|
||||
self.finished = true;
|
||||
|
||||
Reference in New Issue
Block a user