-
Notifications
You must be signed in to change notification settings - Fork 2.7k
refactor(remote ext): use jsonrpsee #8105
Changes from 26 commits
4c0abc5
7f9b5b8
4883812
be549b4
8e97733
7edef9e
99e7818
89f4c2a
fd9c2b1
d84dad4
bd7f87c
d9c7951
9d0717a
c95c215
8b7c7c3
2f1206a
79c7dee
c893bd7
df8d9fe
a9a4a19
a0e8bae
cc248a0
c8796c7
01688f2
eced71c
195fee3
4e696c4
8c6c859
12ca19a
7f08f50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -101,9 +101,13 @@ | |
| //! } | ||
| //! ``` | ||
|
|
||
| // jsonrpsee_proc_macros generates faulty warnings: https://github.com/paritytech/jsonrpsee/issues/106 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good if you provide follow ups for this.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's fixed on master but I haven't done another release yet :) |
||
| #![allow(dead_code)] | ||
|
|
||
| use std::{ | ||
| fs, | ||
| path::{Path, PathBuf}, | ||
| sync::Arc, | ||
| }; | ||
| use log::*; | ||
| use sp_core::{hashing::twox_128}; | ||
|
|
@@ -112,18 +116,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)] | ||
|
|
@@ -150,6 +160,10 @@ pub struct OfflineConfig { | |
| pub struct OnlineConfig { | ||
| /// The HTTP uri to use. | ||
| pub uri: String, | ||
| /// JSONRPC HTTP Client. | ||
| // | ||
| // NOTE: Arc is used here because `HttpClient` doesn't implement `Clone`. | ||
| pub rpc: Arc<HttpClient>, | ||
| /// The block number at which to connect. Will be latest finalized head if not provided. | ||
| pub at: Option<Hash>, | ||
| /// An optional cache file to WRITE to, not for reading. Not cached if set to `None`. | ||
|
|
@@ -161,7 +175,13 @@ pub struct OnlineConfig { | |
| impl Default for OnlineConfig { | ||
| fn default() -> Self { | ||
| Self { | ||
| uri: "http://localhost:9933".into(), | ||
| uri: TARGET.to_owned(), | ||
| rpc: Arc::new( | ||
| HttpClient::new( | ||
|
||
| TARGET, | ||
| HttpConfig { max_request_body_size: u32::MAX } | ||
| ).expect("valid HTTP url; qed") | ||
| ), | ||
| at: None, | ||
| cache: None, | ||
| modules: Default::default(), | ||
|
|
@@ -202,12 +222,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 +247,10 @@ 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(|_| "rpc finalized_head failed.") | ||
| } | ||
|
|
||
| /// Relay the request to `state_getPairs` rpc endpoint. | ||
|
|
@@ -250,18 +261,10 @@ 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.") | ||
| RpcApi::storage_pairs(&*self.as_online().rpc, prefix, Some(at)) | ||
| .await | ||
| .map_err(|_| "rpc finalized_head failed") | ||
| } | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.