From f9860e7d30f43faabcbb4a968e86f3643240c125 Mon Sep 17 00:00:00 2001 From: Niklas Adolfsson Date: Wed, 1 Dec 2021 10:48:32 +0100 Subject: [PATCH] fix some nits --- src/rpc.rs | 14 ++++++++------ test-runtime/Cargo.toml | 1 + test-runtime/build.rs | 21 ++++++++++----------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/rpc.rs b/src/rpc.rs index 4d6a743148..983cf4473e 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -35,6 +35,7 @@ use core::{ use frame_metadata::RuntimeMetadataPrefixed; pub use jsonrpsee::{ client_transport::ws::{ + InvalidUri, Receiver as WsReceiver, Sender as WsSender, Uri, @@ -650,23 +651,24 @@ impl ExtrinsicSuccess { /// Example to check that `From<(Sender, Receiver) for RpcClient` works. pub async fn build_ws_client_default(url: &str) -> Result { - let client = ws_transport(url).await.into(); + let client = ws_transport(url).await?.into(); Ok(client) } /// Build WS RPC client from URL pub async fn build_ws_client(url: &str) -> Result { - let (sender, receiver) = ws_transport(url).await; + let (sender, receiver) = ws_transport(url).await?; Ok(RpcClientBuilder::default() .max_notifs_per_subscription(4096) .build(sender, receiver)) } -async fn ws_transport(url: &str) -> (WsSender, WsReceiver) { - // fix unwraps because I'm lazy. - let url: Uri = url.parse().unwrap(); +async fn ws_transport(url: &str) -> Result<(WsSender, WsReceiver), RpcError> { + let url: Uri = url + .parse() + .map_err(|e: InvalidUri| RpcError::Transport(e.into()))?; WsTransportClientBuilder::default() .build(url) .await - .unwrap() + .map_err(|e| RpcError::Transport(e.into())) } diff --git a/test-runtime/Cargo.toml b/test-runtime/Cargo.toml index 14707745ae..d262747ae2 100644 --- a/test-runtime/Cargo.toml +++ b/test-runtime/Cargo.toml @@ -12,3 +12,4 @@ codec = { package = "parity-scale-codec", version = "2", default-features = fals subxt = { path = ".." } async-std = { version = "1.9.0", features = ["attributes"] } sp-core = { package = "sp-core", git = "https://github.com/paritytech/substrate/", branch = "master" } +jsonrpsee-http-client = { git = "https://github.com/paritytech/jsonrpsee/", branch = "extract-async-client" } diff --git a/test-runtime/build.rs b/test-runtime/build.rs index 48fdbf158a..3b39a997d4 100644 --- a/test-runtime/build.rs +++ b/test-runtime/build.rs @@ -14,6 +14,10 @@ // You should have received a copy of the GNU General Public License // along with subxt. If not, see . +use jsonrpsee_http_client::{ + types::traits::Client, + HttpClientBuilder, +}; use std::{ env, fs, @@ -27,11 +31,6 @@ use std::{ thread, time, }; -use subxt::rpc::{ - build_ws_client, - rpc_params, - Client as _, -}; static SUBSTRATE_BIN_ENV_VAR: &str = "SUBSTRATE_NODE_PATH"; @@ -47,7 +46,7 @@ async fn main() { let cmd = Command::new(&substrate_bin) .arg("--dev") .arg("--tmp") - .arg(format!("--ws-port={}", port)) + .arg(format!("--rpc-port={}", port)) .spawn(); let mut cmd = match cmd { Ok(cmd) => cmd, @@ -61,15 +60,15 @@ async fn main() { const MAX_RETRIES: usize = 20; let mut retries = 0; let mut wait_secs = 1; + let rpc_client = HttpClientBuilder::default() + .build(&format!("http://localhost:{}", port)) + .expect("valid URL; qed"); + loop { if retries >= MAX_RETRIES { panic!("Cannot connect to substrate node after {} retries", retries); } - let res = build_ws_client(&format!("ws://localhost:{}", port)) - .await - .expect("should only error if malformed URL for an HTTP connection") - .request("state_getMetadata", rpc_params![]) - .await; + let res = rpc_client.request("state_getMetadata", None).await; match res { Ok(res) => { let _ = cmd.kill();