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

frame/utils: introduce substrate-rpc-client crate for RPC utils #12212

Merged
merged 15 commits into from
Oct 18, 2022
Merged
20 changes: 17 additions & 3 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ members = [
"utils/frame/rpc/system",
"utils/frame/generate-bags",
"utils/frame/generate-bags/node-runtime",
"utils/frame/rpc/client",
"utils/prometheus",
"utils/wasm-builder",
]
Expand Down
2 changes: 1 addition & 1 deletion bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ soketto = "0.7.1"
criterion = { version = "0.3.5", features = ["async_tokio"] }
tokio = { version = "1.17.0", features = ["macros", "time", "parking_lot"] }
wait-timeout = "0.2"
remote-externalities = { path = "../../../utils/frame/remote-externalities" }
substrate-rpc-client = { path = "../../../utils/frame/rpc/client" }
pallet-timestamp = { version = "4.0.0-dev", path = "../../../frame/timestamp" }

[build-dependencies]
Expand Down
13 changes: 7 additions & 6 deletions bin/node/cli/tests/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ use nix::{
sys::signal::{kill, Signal::SIGINT},
unistd::Pid,
};
use node_primitives::Block;
use remote_externalities::rpc_api::RpcService;
use node_primitives::{Hash, Header};
use std::{
io::{BufRead, BufReader, Read},
ops::{Deref, DerefMut},
Expand All @@ -48,7 +47,7 @@ pub fn wait_for(child: &mut Child, secs: u64) -> Result<ExitStatus, ()> {
let result = wait_timeout::ChildExt::wait_timeout(child, Duration::from_secs(secs - 5))
.map_err(|_| ())?;
if let Some(exit_status) = result {
return Ok(exit_status)
return Ok(exit_status);
}
}
eprintln!("Took too long to exit (> {} seconds). Killing...", secs);
Expand All @@ -69,15 +68,17 @@ pub async fn wait_n_finalized_blocks(

/// Wait for at least n blocks to be finalized from a specified node
pub async fn wait_n_finalized_blocks_from(n: usize, url: &str) {
use substrate_rpc_client::{ws_client, ChainApi};

let mut built_blocks = std::collections::HashSet::new();
let mut interval = tokio::time::interval(Duration::from_secs(2));
let rpc_service = RpcService::new(url, false).await.unwrap();
let rpc = ws_client(url).await.unwrap();

loop {
if let Ok(block) = rpc_service.get_finalized_head::<Block>().await {
if let Ok(block) = ChainApi::<(), Hash, Header, ()>::finalized_head(&rpc).await {
built_blocks.insert(block);
if built_blocks.len() > n {
break
break;
}
};
interval.tick().await;
Expand Down
7 changes: 5 additions & 2 deletions frame/bags-list/remote-tests/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ use sp_runtime::{traits::Block as BlockT, DeserializeOwned};

/// Test voter bags migration. `currency_unit` is the number of planks per the the runtimes `UNITS`
/// (i.e. number of decimal places per DOT, KSM etc)
pub async fn execute<Runtime: RuntimeT, Block: BlockT + DeserializeOwned>(
pub async fn execute<Runtime: RuntimeT, Block>(
currency_unit: u64,
currency_name: &'static str,
ws_url: String,
) {
) where
Block: BlockT,
Block::Header: DeserializeOwned,
{
let mut ext = Builder::<Block>::new()
.mode(Mode::Online(OnlineConfig {
transport: ws_url.to_string().into(),
Expand Down
7 changes: 5 additions & 2 deletions frame/bags-list/remote-tests/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ use remote_externalities::{Builder, Mode, OnlineConfig};
use sp_runtime::{traits::Block as BlockT, DeserializeOwned};

/// Execute create a snapshot from pallet-staking.
pub async fn execute<Runtime: crate::RuntimeT, Block: BlockT + DeserializeOwned>(
pub async fn execute<Runtime: crate::RuntimeT, Block>(
voter_limit: Option<usize>,
currency_unit: u64,
ws_url: String,
) {
) where
Block: BlockT,
Block::Header: DeserializeOwned,
{
use frame_support::storage::generator::StorageMap;

let mut ext = Builder::<Block>::new()
Expand Down
7 changes: 5 additions & 2 deletions frame/bags-list/remote-tests/src/try_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ use remote_externalities::{Builder, Mode, OnlineConfig};
use sp_runtime::{traits::Block as BlockT, DeserializeOwned};

/// Execute the sanity check of the bags-list.
pub async fn execute<Runtime: crate::RuntimeT, Block: BlockT + DeserializeOwned>(
pub async fn execute<Runtime: crate::RuntimeT, Block>(
currency_unit: u64,
currency_name: &'static str,
ws_url: String,
) {
) where
Block: BlockT,
Block::Header: DeserializeOwned,
{
let mut ext = Builder::<Block>::new()
.mode(Mode::Online(OnlineConfig {
transport: ws_url.to_string().into(),
Expand Down
4 changes: 2 additions & 2 deletions utils/frame/remote-externalities/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2021"
license = "Apache-2.0"
homepage = "https://substrate.io"
repository = "https://github.com/paritytech/substrate/"
description = "An externalities provided environemnt that can load itself from remote nodes or cache files"
description = "An externalities provided environment that can load itself from remote nodes or cached files"
readme = "README.md"

[package.metadata.docs.rs]
Expand All @@ -15,7 +15,6 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0" }
env_logger = "0.9"
jsonrpsee = { version = "0.15.1", features = ["ws-client", "macros"] }
log = "0.4.17"
serde = "1.0.136"
serde_json = "1.0"
Expand All @@ -24,6 +23,7 @@ sp-core = { version = "6.0.0", path = "../../../primitives/core" }
sp-io = { version = "6.0.0", path = "../../../primitives/io" }
sp-runtime = { version = "6.0.0", path = "../../../primitives/runtime" }
sp-version = { version = "5.0.0", path = "../../../primitives/version" }
substrate-rpc-client = { path = "../rpc/client" }

[dev-dependencies]
tokio = { version = "1.17.0", features = ["macros", "rt-multi-thread"] }
Expand Down
Loading