From f383b162d62ef4e0f3bb0aed57380e9594f62d62 Mon Sep 17 00:00:00 2001 From: Svyatoslav Nikolsky Date: Wed, 14 Oct 2020 08:19:39 +0300 Subject: [PATCH] connect to substrate using ws (#417) --- bridges/relays/ethereum/src/cli.yml | 2 +- bridges/relays/substrate-client/Cargo.toml | 2 +- bridges/relays/substrate-client/src/client.rs | 8 ++++---- bridges/relays/substrate-client/src/error.rs | 14 ++++++++++++-- bridges/relays/substrate-client/src/lib.rs | 8 ++++---- bridges/relays/substrate/src/cli.rs | 2 +- 6 files changed, 23 insertions(+), 13 deletions(-) diff --git a/bridges/relays/ethereum/src/cli.yml b/bridges/relays/ethereum/src/cli.yml index a665e7677696..c6a5b08e1bb0 100644 --- a/bridges/relays/ethereum/src/cli.yml +++ b/bridges/relays/ethereum/src/cli.yml @@ -24,7 +24,7 @@ subcommands: - sub-port: &sub-port long: sub-port value_name: SUB_PORT - help: Connect to Substrate node at given port. + help: Connect to Substrate node websocket server at given port. takes_value: true - sub-tx-mode: long: sub-tx-mode diff --git a/bridges/relays/substrate-client/Cargo.toml b/bridges/relays/substrate-client/Cargo.toml index 3bf7ae9fafab..4655ca3db918 100644 --- a/bridges/relays/substrate-client/Cargo.toml +++ b/bridges/relays/substrate-client/Cargo.toml @@ -8,7 +8,7 @@ license = "GPL-3.0-or-later WITH Classpath-exception-2.0" [dependencies] async-trait = "0.1.40" codec = { package = "parity-scale-codec", version = "1.3.4" } -jsonrpsee = { git = "https://github.com/svyatonik/jsonrpsee.git", branch = "shared-client-in-rpc-api", default-features = false, features = ["http"] } +jsonrpsee = { git = "https://github.com/svyatonik/jsonrpsee.git", branch = "shared-client-in-rpc-api", default-features = false, features = ["ws"] } log = "0.4.11" num-traits = "0.2" diff --git a/bridges/relays/substrate-client/src/client.rs b/bridges/relays/substrate-client/src/client.rs index 5d744a8920a3..ad6dad1a2a47 100644 --- a/bridges/relays/substrate-client/src/client.rs +++ b/bridges/relays/substrate-client/src/client.rs @@ -22,7 +22,7 @@ use crate::{ConnectionParams, Result}; use jsonrpsee::common::DeserializeOwned; use jsonrpsee::raw::RawClient; -use jsonrpsee::transport::http::HttpTransportClient; +use jsonrpsee::transport::ws::WsTransportClient; use jsonrpsee::Client as RpcClient; use num_traits::Zero; use sp_core::Bytes; @@ -49,10 +49,10 @@ impl std::fmt::Debug for Client { } impl Client { - /// Returns client that is able to call RPCs on Substrate node. + /// Returns client that is able to call RPCs on Substrate node over websocket connection. pub async fn new(params: ConnectionParams) -> Result { - let uri = format!("http://{}:{}", params.host, params.port); - let transport = HttpTransportClient::new(&uri); + let uri = format!("ws://{}:{}", params.host, params.port); + let transport = WsTransportClient::new(&uri).await?; let raw_client = RawClient::new(transport); let client: RpcClient = raw_client.into(); diff --git a/bridges/relays/substrate-client/src/error.rs b/bridges/relays/substrate-client/src/error.rs index 6f8c206d7dac..9dff9e02d2f6 100644 --- a/bridges/relays/substrate-client/src/error.rs +++ b/bridges/relays/substrate-client/src/error.rs @@ -17,6 +17,7 @@ //! Substrate node RPC errors. use jsonrpsee::client::RequestError; +use jsonrpsee::transport::ws::WsNewDnsError; use relay_utils::MaybeConnectionError; /// Result type used by Substrate client. @@ -26,13 +27,21 @@ pub type Result = std::result::Result; /// a Substrate node through RPC. #[derive(Debug)] pub enum Error { + /// Web socket connection error. + WsConnectionError(WsNewDnsError), /// An error that can occur when making an HTTP request to - /// an JSON-RPC client. + /// an JSON-RPC server. Request(RequestError), - /// The response from the client could not be SCALE decoded. + /// The response from the server could not be SCALE decoded. ResponseParseFailed(codec::Error), } +impl From for Error { + fn from(error: WsNewDnsError) -> Self { + Error::WsConnectionError(error) + } +} + impl From for Error { fn from(error: RequestError) -> Self { Error::Request(error) @@ -54,6 +63,7 @@ impl From for String { impl ToString for Error { fn to_string(&self) -> String { match self { + Self::WsConnectionError(e) => e.to_string(), Self::Request(e) => e.to_string(), Self::ResponseParseFailed(e) => e.what().to_string(), } diff --git a/bridges/relays/substrate-client/src/lib.rs b/bridges/relays/substrate-client/src/lib.rs index 6c978f465222..9bc1cf164245 100644 --- a/bridges/relays/substrate-client/src/lib.rs +++ b/bridges/relays/substrate-client/src/lib.rs @@ -30,12 +30,12 @@ pub use crate::client::{Client, OpaqueGrandpaAuthoritiesSet}; pub use crate::error::{Error, Result}; pub use bp_runtime::{BlockNumberOf, Chain as ChainBase, HashOf, HeaderOf}; -/// Substrate connection params. +/// Substrate-over-websocket connection params. #[derive(Debug, Clone)] pub struct ConnectionParams { - /// Substrate RPC host. + /// Websocket server hostname. pub host: String, - /// Substrate RPC port. + /// Websocket server TCP port. pub port: u16, } @@ -43,7 +43,7 @@ impl Default for ConnectionParams { fn default() -> Self { ConnectionParams { host: "localhost".into(), - port: 9933, + port: 9944, } } } diff --git a/bridges/relays/substrate/src/cli.rs b/bridges/relays/substrate/src/cli.rs index 3dfda38e8a50..e13eccf6016d 100644 --- a/bridges/relays/substrate/src/cli.rs +++ b/bridges/relays/substrate/src/cli.rs @@ -76,7 +76,7 @@ macro_rules! declare_chain_options { #[doc = "Connect to " $chain " node at given host."] #[structopt(long)] pub [<$chain_prefix _host>]: String, - #[doc = "Connect to " $chain " node at given port."] + #[doc = "Connect to " $chain " node websocket server at given port."] #[structopt(long)] pub [<$chain_prefix _port>]: u16, }