Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
refactor(remote ext): use jsonrpsee (#8105)
Browse files Browse the repository at this point in the history
* A clean new attempt

* Checkpoint to move remote.

* A lot of dependency wiring to make it feature gated.

* bad macro, bad macro.

* refactor(remote ext): use jsonrpsee

* refactor(remote ext): use jsonrpsee

* Undo the DB mess.

* fix(remote ext): use max limit `u32::MAX`

* resolve TODOs

* jsonrpsee switch to `hyper` as backend

* Update utils/frame/try-runtime/remote-externalities/src/lib.rs

Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>

* update jsonrpsee

* remove boiler-plate

* suppress warnings to CI happy

* Unbreak his build

* Use option

* fix nit; make it work again

* fix err message.

* Update utils/frame/remote-externalities/Cargo.toml

* Fix uri stuff

* remove needless clone

Co-authored-by: kianenigma <kian.peymani@gmail.com>
Co-authored-by: Kian Paimani <5588131+kianenigma@users.noreply.github.com>
Co-authored-by: kianenigma <kian@parity.io>
  • Loading branch information
4 people authored Mar 6, 2021
1 parent af998b2 commit a958512
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 48 deletions.
69 changes: 64 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions utils/frame/remote-externalities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
76 changes: 38 additions & 38 deletions utils/frame/remote-externalities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand All @@ -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<Hash>) -> Vec<(StorageKey, StorageData)>;
#[rpc(method = "chain_getFinalizedHead")]
fn finalized_head() -> Hash;
}
}

/// The execution mode.
#[derive(Clone)]
Expand Down Expand Up @@ -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")
}
}

Expand Down Expand Up @@ -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())
}
}
}
Expand All @@ -232,14 +239,11 @@ impl Builder {
// RPC methods
impl Builder {
async fn rpc_get_head(&self) -> Result<Hash, &'static str> {
let uri = self.as_online().uri.clone();
trace!(target: LOG_TARGET, "rpc: finalized_head");
let client: sc_rpc_api::chain::ChainClient<Number, Hash, (), ()> =
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.
Expand All @@ -250,18 +254,11 @@ impl Builder {
prefix: StorageKey,
at: Hash,
) -> Result<Vec<KeyPair>, &'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<Hash> =
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"
})
}
}

Expand Down Expand Up @@ -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(())
}

Expand Down

0 comments on commit a958512

Please sign in to comment.