diff --git a/Cargo.lock b/Cargo.lock index 38e11a7fed1d7..b7228ae47b374 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2834,6 +2834,67 @@ dependencies = [ "slab", ] +[[package]] +name = "jsonrpsee-http-client" +version = "0.2.0-alpha" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "124797a4ea7430d0675db78e065e53316e3f1a3cbf0ee4d6dbdd42db7b08e193" +dependencies = [ + "async-trait", + "futures 0.3.12", + "hyper 0.13.9", + "jsonrpsee-types", + "jsonrpsee-utils", + "log", + "serde", + "serde_json", + "thiserror", + "unicase", + "url 2.2.0", +] + +[[package]] +name = "jsonrpsee-proc-macros" +version = "0.2.0-alpha" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9cd3d41f5b9a1d3e4e4c9ad49a7a34ad8e1134a1a587cd21c72f644f5c053dd" +dependencies = [ + "Inflector", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "jsonrpsee-types" +version = "0.2.0-alpha" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbf718f9a0d09f50621ea35f507679cf3ab66910a6d95844850076c1281a203c" +dependencies = [ + "async-trait", + "futures 0.3.12", + "log", + "serde", + "serde_json", + "smallvec 1.6.1", + "thiserror", +] + +[[package]] +name = "jsonrpsee-utils" +version = "0.2.0-alpha" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0e45394ec3175a767c3c5bac584560e6ad9b56ebd73216c85ec8bab49619244" +dependencies = [ + "futures 0.3.12", + "globset", + "hyper 0.13.9", + "jsonrpsee-types", + "lazy_static", + "log", + "unicase", +] + [[package]] name = "keccak" version = "0.1.0" @@ -6472,16 +6533,14 @@ version = "0.9.0" dependencies = [ "async-std", "env_logger 0.8.2", - "futures 0.3.12", "hex-literal", - "jsonrpc-core-client", + "jsonrpsee-http-client", + "jsonrpsee-proc-macros", + "jsonrpsee-types", "log", "parity-scale-codec", - "sc-rpc", - "sc-rpc-api", "sp-core", "sp-io", - "tokio 0.1.22", ] [[package]] diff --git a/utils/frame/remote-externalities/Cargo.toml b/utils/frame/remote-externalities/Cargo.toml index d4825211d8a66..8f3f40ec484e3 100644 --- a/utils/frame/remote-externalities/Cargo.toml +++ b/utils/frame/remote-externalities/Cargo.toml @@ -13,16 +13,15 @@ readme = "README.md" targets = ["x86_64-unknown-linux-gnu"] [dependencies] -jsonrpc-core-client = { version = "15.1.0", features = ["http"] } -sc-rpc-api = { version = "0.9.0", path = "../../../client/rpc-api" } -sc-rpc = { version = "3.0.0", path = "../../../client/rpc" } -futures = "0.3" +jsonrpsee-http-client = { version = "0.2.0-alpha", default-features = false, features = ["tokio02"] } +# Needed by jsonrpsee-proc-macros: https://github.com/paritytech/jsonrpsee/issues/214 +jsonrpsee-types = "0.2.0-alpha" +jsonrpsee-proc-macros = "0.2.0-alpha" hex-literal = "0.3.1" env_logger = "0.8.2" log = "0.4.11" codec = { package = "parity-scale-codec", version = "2.0.0" } -tokio = "0.1.22" sp-io = { version = "3.0.0", path = "../../../primitives/io" } sp-core = { version = "3.0.0", path = "../../../primitives/core" } diff --git a/utils/frame/remote-externalities/src/lib.rs b/utils/frame/remote-externalities/src/lib.rs index ab26226253858..a8829a18133a8 100644 --- a/utils/frame/remote-externalities/src/lib.rs +++ b/utils/frame/remote-externalities/src/lib.rs @@ -101,6 +101,9 @@ //! } //! ``` +// jsonrpsee_proc_macros generates faulty warnings: https://github.com/paritytech/jsonrpsee/issues/106 +#![allow(dead_code)] + use std::{ fs, path::{Path, PathBuf}, @@ -112,18 +115,24 @@ use sp_core::{ hexdisplay::HexDisplay, storage::{StorageKey, StorageData}, }; -use futures::{ - compat::Future01CompatExt, - TryFutureExt, -}; use codec::{Encode, Decode}; +use jsonrpsee_http_client::{HttpClient, HttpConfig}; type KeyPair = (StorageKey, StorageData); -type Number = u32; type Hash = sp_core::H256; // TODO: make these two generic. -const LOG_TARGET: &'static str = "remote-ext"; +const LOG_TARGET: &str = "remote-ext"; +const TARGET: &str = "http://localhost:9933"; + +jsonrpsee_proc_macros::rpc_client_api! { + RpcApi { + #[rpc(method = "state_getPairs", positional_params)] + fn storage_pairs(prefix: StorageKey, hash: Option) -> Vec<(StorageKey, StorageData)>; + #[rpc(method = "chain_getFinalizedHead")] + fn finalized_head() -> Hash; + } +} /// The execution mode. #[derive(Clone)] @@ -160,12 +169,15 @@ pub struct OnlineConfig { impl Default for OnlineConfig { fn default() -> Self { - Self { - uri: "http://localhost:9933".into(), - at: None, - cache: None, - modules: Default::default(), - } + Self { uri: TARGET.to_owned(), at: None, cache: None, modules: Default::default() } + } +} + +impl OnlineConfig { + /// Return a new http rpc client. + fn rpc(&self) -> HttpClient { + HttpClient::new(&self.uri, HttpConfig { max_request_body_size: u32::MAX }) + .expect("valid HTTP url; qed") } } @@ -202,12 +214,7 @@ impl Default for Builder { fn default() -> Self { Self { inject: Default::default(), - mode: Mode::Online(OnlineConfig { - at: None, - uri: "http://localhost:9933".into(), - cache: None, - modules: Default::default(), - }), + mode: Mode::Online(OnlineConfig::default()) } } } @@ -232,14 +239,11 @@ impl Builder { // RPC methods impl Builder { async fn rpc_get_head(&self) -> Result { - let uri = self.as_online().uri.clone(); trace!(target: LOG_TARGET, "rpc: finalized_head"); - let client: sc_rpc_api::chain::ChainClient = - jsonrpc_core_client::transports::http::connect(&uri) - .compat() - .map_err(|_| "client initialization failed") - .await?; - client.finalized_head().compat().map_err(|_| "rpc finalized_head failed.").await + RpcApi::finalized_head(&self.as_online().rpc()).await.map_err(|e| { + error!("Error = {:?}", e); + "rpc finalized_head failed." + }) } /// Relay the request to `state_getPairs` rpc endpoint. @@ -250,18 +254,11 @@ impl Builder { prefix: StorageKey, at: Hash, ) -> Result, &'static str> { - let uri = self.as_online().uri.clone(); trace!(target: LOG_TARGET, "rpc: storage_pairs: {:?} / {:?}", prefix, at); - let client: sc_rpc_api::state::StateClient = - jsonrpc_core_client::transports::http::connect(&uri) - .compat() - .map_err(|_| "client initialization failed") - .await?; - client - .storage_pairs(prefix, Some(at)) - .compat() - .map_err(|_| "rpc finalized_head failed.") - .await + RpcApi::storage_pairs(&self.as_online().rpc(), prefix, Some(at)).await.map_err(|e| { + error!("Error = {:?}", e); + "rpc storage_pairs failed" + }) } } @@ -315,8 +312,11 @@ impl Builder { } async fn init_remote_client(&mut self) -> Result<(), &'static str> { - let at = self.rpc_get_head().await?; - self.as_online_mut().at = Some(at); + info!(target: LOG_TARGET, "initializing remote client to {:?}", self.as_online().uri); + if self.as_online().at.is_none() { + let at = self.rpc_get_head().await?; + self.as_online_mut().at = Some(at); + } Ok(()) }