From 6728c997f5cd20e26e714261db20c251a827b68f Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 7 Dec 2023 10:32:38 +0100 Subject: [PATCH 01/12] simplify example --- examples/examples/benchmark_bulk_xt.rs | 30 +++++++++++++++++--------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/examples/examples/benchmark_bulk_xt.rs b/examples/examples/benchmark_bulk_xt.rs index b42156566..b139e70c7 100644 --- a/examples/examples/benchmark_bulk_xt.rs +++ b/examples/examples/benchmark_bulk_xt.rs @@ -15,12 +15,15 @@ //! This example floods the node with a series of transactions. +use codec::Encode; use kitchensink_runtime::{AccountId, BalancesCall, RuntimeCall}; +use sp_core::{Bytes, H256}; use sp_keyring::AccountKeyring; use substrate_api_client::{ + ac_compose_macros::rpc_params, ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner as GenericExtrinsicSigner, SignExtrinsic}, - rpc::JsonrpseeClient, - Api, SubmitExtrinsic, + rpc::{JsonrpseeClient, Request}, + Api, }; // To test this example with CI we run it against the Substrate kitchensink node, which uses the asset pallet. @@ -55,16 +58,23 @@ async fn main() { // waiting for the response of the node. let mut nonce = api.get_nonce().unwrap(); let first_nonce = nonce; - while nonce < first_nonce + 500 { - // Compose a balance extrinsic. - let call = RuntimeCall::Balances(BalancesCall::transfer_allow_death { - dest: recipient.clone(), - value: 1_000_000, - }); - let xt = api.compose_extrinsic_offline(call, nonce); + // Compose a balance extrinsic. + let call = RuntimeCall::Balances(BalancesCall::transfer_allow_death { + dest: recipient.clone(), + value: 1_000_000, + }); + + while nonce < first_nonce + 60 { + // Create the extrinsic. + let xt = api.compose_extrinsic_offline(call.clone(), nonce); + let xt_bytes: Bytes = xt.encode().into(); + let hex_encoded_xt = rpc_params![xt_bytes]; println!("Sending extrinsic with nonce {}", nonce); - let _tx_hash = api.submit_extrinsic(xt).unwrap(); + + // Send the extrinsic with jsonrpsee + let _xt_hash: H256 = + api.client().request("author_submitExtrinsic", hex_encoded_xt).unwrap(); nonce += 1; } From ab86aa4ace5aedb28e8c78f39a22ad2ffe5004f0 Mon Sep 17 00:00:00 2001 From: haerdib Date: Thu, 7 Dec 2023 10:33:58 +0100 Subject: [PATCH 02/12] update to v0.17 --- Cargo.lock | 37 ++++++++++++++++++--------------- Cargo.toml | 2 +- src/rpc/jsonrpsee_client/mod.rs | 3 ++- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5b1622ae8..9e4c64dda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2416,9 +2416,9 @@ dependencies = [ [[package]] name = "jsonrpsee" -version = "0.16.3" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "367a292944c07385839818bb71c8d76611138e2dedb0677d035b8da21d29c78b" +checksum = "8b971ce0f6cd1521ede485afc564b95b2c8e7079b9da41d4273bd9b55140a55d" dependencies = [ "jsonrpsee-client-transport", "jsonrpsee-core", @@ -2427,14 +2427,13 @@ dependencies = [ [[package]] name = "jsonrpsee-client-transport" -version = "0.16.3" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8b3815d9f5d5de348e5f162b316dc9cdf4548305ebb15b4eb9328e66cf27d7a" +checksum = "7ca00d975eda834826b04ad57d4e690c67439bb51b02eb0f8b7e4c30fcef8ab9" dependencies = [ "futures-util", "http 0.2.9", "jsonrpsee-core", - "jsonrpsee-types", "pin-project", "rustls-native-certs", "soketto", @@ -2443,20 +2442,18 @@ dependencies = [ "tokio-rustls", "tokio-util", "tracing", - "webpki-roots", ] [[package]] name = "jsonrpsee-core" -version = "0.16.3" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2b5dde66c53d6dcdc8caea1874a45632ec0fcf5b437789f1e45766a1512ce803" +checksum = "b83cca7a5a7899eed8b2935d5f755c8c4052ad66ab5b328bd34ac2b3ffd3515f" dependencies = [ "anyhow", "async-lock", "async-trait", "beef", - "futures-channel", "futures-timer", "futures-util", "jsonrpsee-types", @@ -2465,14 +2462,15 @@ dependencies = [ "serde_json", "thiserror", "tokio", + "tokio-stream", "tracing", ] [[package]] name = "jsonrpsee-types" -version = "0.16.3" +version = "0.17.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "245ba8e5aa633dd1c1e4fae72bce06e71f42d34c14a2767c6b4d173b57bee5e5" +checksum = "dd301ccc3e08718393432d1961539d78c4580dcca86014dfe6769c308b2c08b2" dependencies = [ "anyhow", "beef", @@ -6643,6 +6641,17 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-stream" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + [[package]] name = "tokio-util" version = "0.7.10" @@ -7282,12 +7291,6 @@ dependencies = [ "wasmparser", ] -[[package]] -name = "webpki-roots" -version = "0.25.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14247bb57be4f377dfb94c72830b8ce8fc6beac03cf4bf7b9732eadd414123fc" - [[package]] name = "wide" version = "0.7.13" diff --git a/Cargo.toml b/Cargo.toml index a1530d1a4..4b5157ee3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ url = { version = "2.0.0", optional = true } # websocket dependent features futures = { version = "0.3", optional = true } -jsonrpsee = { version = "0.16", optional = true, features = ["async-client", "client-ws-transport", "jsonrpsee-types"] } +jsonrpsee = { version = "0.17", optional = true, features = ["async-client", "client-ws-transport-native-tls", "jsonrpsee-types"] } tungstenite = { version = "0.21", optional = true, features = ["native-tls"] } ws = { version = "0.9.2", optional = true, features = ["ssl"] } diff --git a/src/rpc/jsonrpsee_client/mod.rs b/src/rpc/jsonrpsee_client/mod.rs index 987a5dfb3..d5f125e43 100644 --- a/src/rpc/jsonrpsee_client/mod.rs +++ b/src/rpc/jsonrpsee_client/mod.rs @@ -49,7 +49,7 @@ impl JsonrpseeClient { .await .map_err(|e| Error::Client(Box::new(e)))?; let client = ClientBuilder::default() - .max_notifs_per_subscription(4096) + .max_buffer_capacity_per_subscription(4096) .build_with_tokio(tx, rx); Ok(Self { inner: Arc::new(client) }) } @@ -64,6 +64,7 @@ impl Request for JsonrpseeClient { .map_err(|e| Error::Client(Box::new(e))) } } + #[maybe_async::sync_impl] impl Request for JsonrpseeClient { fn request(&self, method: &str, params: RpcParams) -> Result { From ece3a7a03770fa59094723f84dea4b0a584820bb Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 8 Dec 2023 16:50:41 +0100 Subject: [PATCH 03/12] make it work --- Cargo.lock | 9 +++++---- Cargo.toml | 3 ++- src/rpc/jsonrpsee_client/mod.rs | 34 ++++++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9e4c64dda..8e857d201 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6389,6 +6389,7 @@ dependencies = [ "sp-storage", "sp-version", "test-case", + "tokio", "tungstenite", "url", "ws", @@ -6606,9 +6607,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.33.0" +version = "1.34.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4f38200e3ef7995e5ef13baec2f432a6da0aa9ac495b2c0e8f3b7eec2c92d653" +checksum = "d0c014766411e834f7af5b8f4cf46257aab4036ca95e9d2c144a10f59ad6f5b9" dependencies = [ "backtrace", "libc", @@ -6622,9 +6623,9 @@ dependencies = [ [[package]] name = "tokio-macros" -version = "2.1.0" +version = "2.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" +checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 4b5157ee3..144daf0fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,6 +41,7 @@ url = { version = "2.0.0", optional = true } # websocket dependent features futures = { version = "0.3", optional = true } +tokio = { version = "1.34", optional = true } jsonrpsee = { version = "0.17", optional = true, features = ["async-client", "client-ws-transport-native-tls", "jsonrpsee-types"] } tungstenite = { version = "0.21", optional = true, features = ["native-tls"] } ws = { version = "0.9.2", optional = true, features = ["ssl"] } @@ -80,7 +81,7 @@ sync-api = ["ac-compose-macros/sync-api", "maybe-async/is_sync"] # Use the `jsonrpsee` crate for websocket communication. Does provide sync and async support but needs a tokio runtime. # Provides convenience functions such as subscription callbacks. # Most examples use the `jsonrpsee` feature and can be used for reference. -jsonrpsee-client = ["std", "jsonrpsee", "futures"] +jsonrpsee-client = ["std", "jsonrpsee", "futures", "tokio"] # Use the `tungstenite` crate for websocket communication. No async support but has some reconnection capabilities. # See the example `transfer_with_tungstenite_client` on how to use it. diff --git a/src/rpc/jsonrpsee_client/mod.rs b/src/rpc/jsonrpsee_client/mod.rs index d5f125e43..1a1139900 100644 --- a/src/rpc/jsonrpsee_client/mod.rs +++ b/src/rpc/jsonrpsee_client/mod.rs @@ -12,6 +12,7 @@ */ use crate::rpc::{Error, Request, Result, RpcParams, Subscribe}; +use core::marker::Send; use futures::executor::block_on; use jsonrpsee::{ client_transport::ws::{Uri, WsTransportClientBuilder}, @@ -21,8 +22,9 @@ use jsonrpsee::{ }, }; use serde::de::DeserializeOwned; -use serde_json::value::RawValue; +use serde_json::{value::RawValue, Value}; use std::sync::Arc; +use tokio::{runtime::Handle, sync::oneshot}; pub use subscription::SubscriptionWrapper; @@ -53,11 +55,12 @@ impl JsonrpseeClient { .build_with_tokio(tx, rx); Ok(Self { inner: Arc::new(client) }) } -} -#[maybe_async::async_impl(?Send)] -impl Request for JsonrpseeClient { - async fn request(&self, method: &str, params: RpcParams) -> Result { + async fn inner_request( + &self, + method: &str, + params: RpcParams, + ) -> Result { self.inner .request(method, RpcParamsWrapper(params)) .await @@ -65,11 +68,28 @@ impl Request for JsonrpseeClient { } } +#[maybe_async::async_impl(?Send)] +impl Request for JsonrpseeClient { + async fn request(&self, method: &str, params: RpcParams) -> Result { + self.inner_request(method, params) + } +} + #[maybe_async::sync_impl] impl Request for JsonrpseeClient { fn request(&self, method: &str, params: RpcParams) -> Result { - block_on(self.inner.request(method, RpcParamsWrapper(params))) - .map_err(|e| Error::Client(Box::new(e))) + let handle = Handle::current(); + let client = self.inner.clone(); + let method_string = method.to_string(); + let string_answer: Value = std::thread::spawn(move || { + handle.block_on(client.request(&method_string, RpcParamsWrapper(params))) + }) + .join() + .unwrap() + .map_err(|e| Error::Client(Box::new(e)))?; + + let deserialized_value: R = serde_json::from_value(string_answer)?; + Ok(deserialized_value) } } From 2a5551e45d687ffb51a79881f382de658f5ac1e4 Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 8 Dec 2023 16:56:38 +0100 Subject: [PATCH 04/12] add explanation comment --- src/rpc/jsonrpsee_client/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/rpc/jsonrpsee_client/mod.rs b/src/rpc/jsonrpsee_client/mod.rs index 1a1139900..f945ec69e 100644 --- a/src/rpc/jsonrpsee_client/mod.rs +++ b/src/rpc/jsonrpsee_client/mod.rs @@ -81,6 +81,10 @@ impl Request for JsonrpseeClient { let handle = Handle::current(); let client = self.inner.clone(); let method_string = method.to_string(); + + // The inner jsonrpsee client must not deserialize to the `R` value, because the return value must + // implement `Send`. But we do not want to enforce the `R` value to implment this simply because we need + // to de-async something. Therefore, the deserialization must happen outside of the newly spawned thread. let string_answer: Value = std::thread::spawn(move || { handle.block_on(client.request(&method_string, RpcParamsWrapper(params))) }) From d1e0ebbce123822e713496dbdd65db64aa4f2543 Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 8 Dec 2023 17:06:20 +0100 Subject: [PATCH 05/12] add more commentary --- src/rpc/jsonrpsee_client/mod.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/rpc/jsonrpsee_client/mod.rs b/src/rpc/jsonrpsee_client/mod.rs index f945ec69e..ec92b7970 100644 --- a/src/rpc/jsonrpsee_client/mod.rs +++ b/src/rpc/jsonrpsee_client/mod.rs @@ -85,6 +85,9 @@ impl Request for JsonrpseeClient { // The inner jsonrpsee client must not deserialize to the `R` value, because the return value must // implement `Send`. But we do not want to enforce the `R` value to implment this simply because we need // to de-async something. Therefore, the deserialization must happen outside of the newly spawned thread. + // We need to spawn a new thread because tokio does not allow the blocking of an asynchronous thread is not allowed: + // ERROR: Cannot block the current thread from within a runtime. + // This happens because a function attempted to block the current thread while the thread is being used to drive asynchronous tasks. let string_answer: Value = std::thread::spawn(move || { handle.block_on(client.request(&method_string, RpcParamsWrapper(params))) }) From 3a7b7e341ee7bcdb00dccc5f4bb7e50b903782c5 Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 8 Dec 2023 17:10:30 +0100 Subject: [PATCH 06/12] remove inner request once again --- src/rpc/jsonrpsee_client/mod.rs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/rpc/jsonrpsee_client/mod.rs b/src/rpc/jsonrpsee_client/mod.rs index ec92b7970..dfad445ff 100644 --- a/src/rpc/jsonrpsee_client/mod.rs +++ b/src/rpc/jsonrpsee_client/mod.rs @@ -55,23 +55,15 @@ impl JsonrpseeClient { .build_with_tokio(tx, rx); Ok(Self { inner: Arc::new(client) }) } - - async fn inner_request( - &self, - method: &str, - params: RpcParams, - ) -> Result { - self.inner - .request(method, RpcParamsWrapper(params)) - .await - .map_err(|e| Error::Client(Box::new(e))) - } } #[maybe_async::async_impl(?Send)] impl Request for JsonrpseeClient { async fn request(&self, method: &str, params: RpcParams) -> Result { - self.inner_request(method, params) + self.inner + .request(method, RpcParamsWrapper(params)) + .await + .map_err(|e| Error::Client(Box::new(e))) } } From b28d5ef8cb9c5b485333ced5ea516ea22a9a4f26 Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 8 Dec 2023 17:10:51 +0100 Subject: [PATCH 07/12] fix clippy --- src/rpc/jsonrpsee_client/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/rpc/jsonrpsee_client/mod.rs b/src/rpc/jsonrpsee_client/mod.rs index dfad445ff..48f01c28e 100644 --- a/src/rpc/jsonrpsee_client/mod.rs +++ b/src/rpc/jsonrpsee_client/mod.rs @@ -12,7 +12,6 @@ */ use crate::rpc::{Error, Request, Result, RpcParams, Subscribe}; -use core::marker::Send; use futures::executor::block_on; use jsonrpsee::{ client_transport::ws::{Uri, WsTransportClientBuilder}, @@ -24,7 +23,7 @@ use jsonrpsee::{ use serde::de::DeserializeOwned; use serde_json::{value::RawValue, Value}; use std::sync::Arc; -use tokio::{runtime::Handle, sync::oneshot}; +use tokio::runtime::Handle; pub use subscription::SubscriptionWrapper; From ccbabcb5ccb17ee886e2280b713518bc5126db26 Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 8 Dec 2023 17:13:19 +0100 Subject: [PATCH 08/12] fix comment --- src/rpc/jsonrpsee_client/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/rpc/jsonrpsee_client/mod.rs b/src/rpc/jsonrpsee_client/mod.rs index 48f01c28e..95af36441 100644 --- a/src/rpc/jsonrpsee_client/mod.rs +++ b/src/rpc/jsonrpsee_client/mod.rs @@ -74,9 +74,9 @@ impl Request for JsonrpseeClient { let method_string = method.to_string(); // The inner jsonrpsee client must not deserialize to the `R` value, because the return value must - // implement `Send`. But we do not want to enforce the `R` value to implment this simply because we need + // implement `Send`. But we do not want to enforce the `R` value to implement this solely because we need // to de-async something. Therefore, the deserialization must happen outside of the newly spawned thread. - // We need to spawn a new thread because tokio does not allow the blocking of an asynchronous thread is not allowed: + // We need to spawn a new thread because tokio does not allow the blocking of an asynchronous thread: // ERROR: Cannot block the current thread from within a runtime. // This happens because a function attempted to block the current thread while the thread is being used to drive asynchronous tasks. let string_answer: Value = std::thread::spawn(move || { From 7c693da3e17834fb7d6fdafd22e81428beede585 Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 8 Dec 2023 17:31:01 +0100 Subject: [PATCH 09/12] revert example changes --- examples/examples/benchmark_bulk_xt.rs | 30 +++++++++----------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/examples/examples/benchmark_bulk_xt.rs b/examples/examples/benchmark_bulk_xt.rs index b139e70c7..b42156566 100644 --- a/examples/examples/benchmark_bulk_xt.rs +++ b/examples/examples/benchmark_bulk_xt.rs @@ -15,15 +15,12 @@ //! This example floods the node with a series of transactions. -use codec::Encode; use kitchensink_runtime::{AccountId, BalancesCall, RuntimeCall}; -use sp_core::{Bytes, H256}; use sp_keyring::AccountKeyring; use substrate_api_client::{ - ac_compose_macros::rpc_params, ac_primitives::{AssetRuntimeConfig, ExtrinsicSigner as GenericExtrinsicSigner, SignExtrinsic}, - rpc::{JsonrpseeClient, Request}, - Api, + rpc::JsonrpseeClient, + Api, SubmitExtrinsic, }; // To test this example with CI we run it against the Substrate kitchensink node, which uses the asset pallet. @@ -58,23 +55,16 @@ async fn main() { // waiting for the response of the node. let mut nonce = api.get_nonce().unwrap(); let first_nonce = nonce; + while nonce < first_nonce + 500 { + // Compose a balance extrinsic. + let call = RuntimeCall::Balances(BalancesCall::transfer_allow_death { + dest: recipient.clone(), + value: 1_000_000, + }); + let xt = api.compose_extrinsic_offline(call, nonce); - // Compose a balance extrinsic. - let call = RuntimeCall::Balances(BalancesCall::transfer_allow_death { - dest: recipient.clone(), - value: 1_000_000, - }); - - while nonce < first_nonce + 60 { - // Create the extrinsic. - let xt = api.compose_extrinsic_offline(call.clone(), nonce); - let xt_bytes: Bytes = xt.encode().into(); - let hex_encoded_xt = rpc_params![xt_bytes]; println!("Sending extrinsic with nonce {}", nonce); - - // Send the extrinsic with jsonrpsee - let _xt_hash: H256 = - api.client().request("author_submitExtrinsic", hex_encoded_xt).unwrap(); + let _tx_hash = api.submit_extrinsic(xt).unwrap(); nonce += 1; } From 90f55b7a5fc4bbf0cd4f40937ab48adf44365ca8 Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 8 Dec 2023 17:32:41 +0100 Subject: [PATCH 10/12] update to v20.03 --- Cargo.lock | 29 +++++++++-------------------- Cargo.toml | 2 +- src/rpc/jsonrpsee_client/mod.rs | 4 ++-- 3 files changed, 12 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e857d201..90cd0bbb8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2416,9 +2416,9 @@ dependencies = [ [[package]] name = "jsonrpsee" -version = "0.17.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b971ce0f6cd1521ede485afc564b95b2c8e7079b9da41d4273bd9b55140a55d" +checksum = "affdc52f7596ccb2d7645231fc6163bb314630c989b64998f3699a28b4d5d4dc" dependencies = [ "jsonrpsee-client-transport", "jsonrpsee-core", @@ -2427,9 +2427,9 @@ dependencies = [ [[package]] name = "jsonrpsee-client-transport" -version = "0.17.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ca00d975eda834826b04ad57d4e690c67439bb51b02eb0f8b7e4c30fcef8ab9" +checksum = "b5b005c793122d03217da09af68ba9383363caa950b90d3436106df8cabce935" dependencies = [ "futures-util", "http 0.2.9", @@ -2442,13 +2442,14 @@ dependencies = [ "tokio-rustls", "tokio-util", "tracing", + "url", ] [[package]] name = "jsonrpsee-core" -version = "0.17.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b83cca7a5a7899eed8b2935d5f755c8c4052ad66ab5b328bd34ac2b3ffd3515f" +checksum = "da2327ba8df2fdbd5e897e2b5ed25ce7f299d345b9736b6828814c3dbd1fd47b" dependencies = [ "anyhow", "async-lock", @@ -2462,15 +2463,14 @@ dependencies = [ "serde_json", "thiserror", "tokio", - "tokio-stream", "tracing", ] [[package]] name = "jsonrpsee-types" -version = "0.17.1" +version = "0.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd301ccc3e08718393432d1961539d78c4580dcca86014dfe6769c308b2c08b2" +checksum = "5be0be325642e850ed0bdff426674d2e66b2b7117c9be23a7caef68a2902b7d9" dependencies = [ "anyhow", "beef", @@ -6642,17 +6642,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "tokio-stream" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "397c988d37662c7dda6d2208364a706264bf3d6138b11d436cbac0ad38832842" -dependencies = [ - "futures-core", - "pin-project-lite", - "tokio", -] - [[package]] name = "tokio-util" version = "0.7.10" diff --git a/Cargo.toml b/Cargo.toml index 144daf0fb..0ad4423bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,7 +42,7 @@ url = { version = "2.0.0", optional = true } # websocket dependent features futures = { version = "0.3", optional = true } tokio = { version = "1.34", optional = true } -jsonrpsee = { version = "0.17", optional = true, features = ["async-client", "client-ws-transport-native-tls", "jsonrpsee-types"] } +jsonrpsee = { version = "0.20", optional = true, features = ["async-client", "client-ws-transport-native-tls", "jsonrpsee-types"] } tungstenite = { version = "0.21", optional = true, features = ["native-tls"] } ws = { version = "0.9.2", optional = true, features = ["ssl"] } diff --git a/src/rpc/jsonrpsee_client/mod.rs b/src/rpc/jsonrpsee_client/mod.rs index 95af36441..50a366b3b 100644 --- a/src/rpc/jsonrpsee_client/mod.rs +++ b/src/rpc/jsonrpsee_client/mod.rs @@ -14,7 +14,7 @@ use crate::rpc::{Error, Request, Result, RpcParams, Subscribe}; use futures::executor::block_on; use jsonrpsee::{ - client_transport::ws::{Uri, WsTransportClientBuilder}, + client_transport::ws::{Url, WsTransportClientBuilder}, core::{ client::{Client, ClientBuilder, ClientT, SubscriptionClientT}, traits::ToRpcParams, @@ -44,7 +44,7 @@ impl JsonrpseeClient { } pub async fn async_new(url: &str) -> Result { - let uri: Uri = url.parse().map_err(|e| Error::Client(Box::new(e)))?; + let uri: Url = url.parse().map_err(|e| Error::Client(Box::new(e)))?; let (tx, rx) = WsTransportClientBuilder::default() .build(uri) .await From 6a99b5c27fbc512e6cef3f0da62bb34126fb1baf Mon Sep 17 00:00:00 2001 From: haerdib Date: Fri, 8 Dec 2023 17:33:53 +0100 Subject: [PATCH 11/12] taplo --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 0ad4423bc..8e13e9447 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,8 +41,8 @@ url = { version = "2.0.0", optional = true } # websocket dependent features futures = { version = "0.3", optional = true } -tokio = { version = "1.34", optional = true } jsonrpsee = { version = "0.20", optional = true, features = ["async-client", "client-ws-transport-native-tls", "jsonrpsee-types"] } +tokio = { version = "1.34", optional = true } tungstenite = { version = "0.21", optional = true, features = ["native-tls"] } ws = { version = "0.9.2", optional = true, features = ["ssl"] } From 2c5bda850d1b35b9206bab3e63bc56ed35c26759 Mon Sep 17 00:00:00 2001 From: haerdib Date: Tue, 12 Dec 2023 11:37:40 +0100 Subject: [PATCH 12/12] add try_current with Handle --- src/rpc/jsonrpsee_client/mod.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/rpc/jsonrpsee_client/mod.rs b/src/rpc/jsonrpsee_client/mod.rs index 50a366b3b..a89046dc9 100644 --- a/src/rpc/jsonrpsee_client/mod.rs +++ b/src/rpc/jsonrpsee_client/mod.rs @@ -69,24 +69,33 @@ impl Request for JsonrpseeClient { #[maybe_async::sync_impl] impl Request for JsonrpseeClient { fn request(&self, method: &str, params: RpcParams) -> Result { - let handle = Handle::current(); + let handle = match Handle::try_current() { + Ok(handle) => handle, + Err(_) => { + // We are not inside a tokio runtime, so lets start one. + let rt = + tokio::runtime::Builder::new_current_thread().enable_all().build().unwrap(); + rt.handle().clone() + }, + }; + let client = self.inner.clone(); let method_string = method.to_string(); // The inner jsonrpsee client must not deserialize to the `R` value, because the return value must // implement `Send`. But we do not want to enforce the `R` value to implement this solely because we need // to de-async something. Therefore, the deserialization must happen outside of the newly spawned thread. - // We need to spawn a new thread because tokio does not allow the blocking of an asynchronous thread: + // We need to spawn a new thread because tokio does not allow the blocking of the main thread: // ERROR: Cannot block the current thread from within a runtime. // This happens because a function attempted to block the current thread while the thread is being used to drive asynchronous tasks. - let string_answer: Value = std::thread::spawn(move || { + let answer: Value = std::thread::spawn(move || { handle.block_on(client.request(&method_string, RpcParamsWrapper(params))) }) .join() .unwrap() .map_err(|e| Error::Client(Box::new(e)))?; - let deserialized_value: R = serde_json::from_value(string_answer)?; + let deserialized_value: R = serde_json::from_value(answer)?; Ok(deserialized_value) } }