From 37df0ee525252fa9a3e7730629ce0de4da9e68fd Mon Sep 17 00:00:00 2001 From: Seun Lanlege Date: Thu, 29 Sep 2022 13:40:02 +0100 Subject: [PATCH 1/4] expose jsonrpc-core client --- subxt/src/rpc/rpc_client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subxt/src/rpc/rpc_client.rs b/subxt/src/rpc/rpc_client.rs index 96560c60b7..b0e929a9d0 100644 --- a/subxt/src/rpc/rpc_client.rs +++ b/subxt/src/rpc/rpc_client.rs @@ -28,7 +28,7 @@ use std::{ /// Wrapping [`RpcClientT`] in this way is simply a way to expose this additional functionality /// without getting into issues with non-object-safe methods or no `async` in traits. #[derive(Clone)] -pub struct RpcClient(Arc); +pub struct RpcClient(pub Arc); impl RpcClient { pub(crate) fn new(client: R) -> Self { From 98caf56e9e41ad457bc1ad5a087a83455ea9729b Mon Sep 17 00:00:00 2001 From: Seun Lanlege Date: Sat, 1 Oct 2022 09:34:04 +0100 Subject: [PATCH 2/4] use shared reference to RpcClienT --- examples/examples/custom_rpc_client.rs | 2 +- subxt/src/client/online_client.rs | 4 ++-- subxt/src/rpc/rpc.rs | 3 ++- subxt/src/rpc/rpc_client.rs | 4 ++-- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/examples/custom_rpc_client.rs b/examples/examples/custom_rpc_client.rs index 9329b67aad..52e0cd8227 100644 --- a/examples/examples/custom_rpc_client.rs +++ b/examples/examples/custom_rpc_client.rs @@ -88,7 +88,7 @@ async fn main() -> Result<(), Box> { // Pass this into our OnlineClient to instantiate it. This will lead to some // RPC calls being made to fetch chain details/metadata, which will immediately // fail.. - let _ = OnlineClient::::from_rpc_client(rpc_client).await; + let _ = OnlineClient::::from_rpc_client(Arc::new(rpc_client)).await; // But, we can see that the calls were made via our custom RPC client: println!("Log of calls made:\n\n{}", log.lock().unwrap().as_str()); diff --git a/subxt/src/client/online_client.rs b/subxt/src/client/online_client.rs index d043edf7d1..3fe6ca1ca2 100644 --- a/subxt/src/client/online_client.rs +++ b/subxt/src/client/online_client.rs @@ -74,7 +74,7 @@ impl OnlineClient { let client = jsonrpsee_helpers::ws_client(url.as_ref()) .await .map_err(|e| crate::error::RpcError(e.to_string()))?; - OnlineClient::from_rpc_client(client).await + OnlineClient::from_rpc_client(Arc::new(client)).await } } @@ -82,7 +82,7 @@ impl OnlineClient { /// Construct a new [`OnlineClient`] by providing an underlying [`RpcClientT`] /// implementation to drive the connection. pub async fn from_rpc_client( - rpc_client: R, + rpc_client: Arc, ) -> Result, Error> { let rpc = Rpc::new(rpc_client); diff --git a/subxt/src/rpc/rpc.rs b/subxt/src/rpc/rpc.rs index cc650bea69..b6149df6f5 100644 --- a/subxt/src/rpc/rpc.rs +++ b/subxt/src/rpc/rpc.rs @@ -77,6 +77,7 @@ use sp_runtime::{ ApplyExtrinsicResult, }; use std::collections::HashMap; +use std::sync::Arc; /// A number type that can be serialized both as a number or a string that encodes a number in a /// string. @@ -342,7 +343,7 @@ impl std::ops::Deref for Rpc { impl Rpc { /// Create a new [`Rpc`] - pub fn new(client: R) -> Self { + pub fn new(client: Arc) -> Self { Self { client: RpcClient::new(client), _marker: PhantomDataSendSync::new(), diff --git a/subxt/src/rpc/rpc_client.rs b/subxt/src/rpc/rpc_client.rs index b0e929a9d0..6519b0530b 100644 --- a/subxt/src/rpc/rpc_client.rs +++ b/subxt/src/rpc/rpc_client.rs @@ -31,8 +31,8 @@ use std::{ pub struct RpcClient(pub Arc); impl RpcClient { - pub(crate) fn new(client: R) -> Self { - RpcClient(Arc::new(client)) + pub(crate) fn new(client: Arc) -> Self { + RpcClient(client) } /// Make an RPC request, given a method name and some parameters. From f688101ebf3bc5ab8e2f9dc4a57a769bd4963e32 Mon Sep 17 00:00:00 2001 From: Seun Lanlege Date: Sat, 1 Oct 2022 09:35:26 +0100 Subject: [PATCH 3/4] don't expose Arc --- subxt/src/rpc/rpc_client.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/subxt/src/rpc/rpc_client.rs b/subxt/src/rpc/rpc_client.rs index 6519b0530b..56592c0363 100644 --- a/subxt/src/rpc/rpc_client.rs +++ b/subxt/src/rpc/rpc_client.rs @@ -28,7 +28,7 @@ use std::{ /// Wrapping [`RpcClientT`] in this way is simply a way to expose this additional functionality /// without getting into issues with non-object-safe methods or no `async` in traits. #[derive(Clone)] -pub struct RpcClient(pub Arc); +pub struct RpcClient(Arc); impl RpcClient { pub(crate) fn new(client: Arc) -> Self { From 48613ba5e564e830063e17ee2cc55fa34a9a7530 Mon Sep 17 00:00:00 2001 From: Seun Lanlege Date: Tue, 4 Oct 2022 06:52:19 +0100 Subject: [PATCH 4/4] cargo fmt --- subxt/src/events/mod.rs | 1 - subxt/src/rpc/rpc.rs | 6 ++++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/subxt/src/events/mod.rs b/subxt/src/events/mod.rs index d0c9ba0f5b..2019253e0b 100644 --- a/subxt/src/events/mod.rs +++ b/subxt/src/events/mod.rs @@ -17,7 +17,6 @@ pub use event_subscription::{ FinalizedEventSub, }; pub use events_client::{ - // Exposed only for testing: subscribe_to_block_headers_filling_in_gaps, EventsClient, }; diff --git a/subxt/src/rpc/rpc.rs b/subxt/src/rpc/rpc.rs index b6149df6f5..f8f54823df 100644 --- a/subxt/src/rpc/rpc.rs +++ b/subxt/src/rpc/rpc.rs @@ -76,8 +76,10 @@ use sp_runtime::{ }, ApplyExtrinsicResult, }; -use std::collections::HashMap; -use std::sync::Arc; +use std::{ + collections::HashMap, + sync::Arc, +}; /// A number type that can be serialized both as a number or a string that encodes a number in a /// string.