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
Expand Up @@ -232,6 +232,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
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
9 changes: 5 additions & 4 deletions bin/node/cli/tests/common.rs
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 Down Expand Up @@ -69,12 +68,14 @@ 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
Expand Down
11 changes: 6 additions & 5 deletions frame/bags-list/remote-tests/src/migration.rs
Expand Up @@ -24,14 +24,15 @@ 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<pallet_bags_list::Instance1>,
Block: BlockT + DeserializeOwned,
>(
pub async fn execute<Runtime, Block>(
currency_unit: u64,
currency_name: &'static str,
ws_url: String,
) {
) where
Runtime: RuntimeT<pallet_bags_list::Instance1>,
Block: BlockT,
Block::Header: DeserializeOwned,
{
let mut ext = Builder::<Block>::new()
.mode(Mode::Online(OnlineConfig {
transport: ws_url.to_string().into(),
Expand Down
12 changes: 5 additions & 7 deletions frame/bags-list/remote-tests/src/snapshot.rs
Expand Up @@ -22,14 +22,12 @@ 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<
pub async fn execute<Runtime, Block>(voter_limit: Option<usize>, currency_unit: u64, ws_url: String)
where
Runtime: crate::RuntimeT<pallet_bags_list::Instance1>,
Block: BlockT + DeserializeOwned,
>(
voter_limit: Option<usize>,
currency_unit: u64,
ws_url: String,
) {
Block: BlockT,
Block::Header: DeserializeOwned,
{
use frame_support::storage::generator::StorageMap;

let mut ext = Builder::<Block>::new()
Expand Down
11 changes: 6 additions & 5 deletions frame/bags-list/remote-tests/src/try_state.rs
Expand Up @@ -25,14 +25,15 @@ 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<pallet_bags_list::Instance1>,
Block: BlockT + DeserializeOwned,
>(
pub async fn execute<Runtime, Block>(
currency_unit: u64,
currency_name: &'static str,
ws_url: String,
) {
) where
Runtime: crate::RuntimeT<pallet_bags_list::Instance1>,
Block: BlockT,
Block::Header: DeserializeOwned,
{
let mut ext = Builder::<Block>::new()
.mode(Mode::Online(OnlineConfig {
transport: ws_url.to_string().into(),
Expand Down
11 changes: 5 additions & 6 deletions frame/state-trie-migration/src/lib.rs
Expand Up @@ -1640,13 +1640,12 @@ pub(crate) mod remote_tests {
///
/// This will print some very useful statistics, make sure [`crate::LOG_TARGET`] is enabled.
#[allow(dead_code)]
pub(crate) async fn run_with_limits<
pub(crate) async fn run_with_limits<Runtime, Block>(limits: MigrationLimits, mode: Mode<Block>)
where
Runtime: crate::Config<Hash = H256>,
Block: BlockT<Hash = H256> + serde::de::DeserializeOwned,
>(
limits: MigrationLimits,
mode: Mode<Block>,
) {
Block: BlockT<Hash = H256>,
Block::Header: serde::de::DeserializeOwned,
{
let mut ext = remote_externalities::Builder::<Block>::new()
.mode(mode)
.state_version(sp_core::storage::StateVersion::V0)
Expand Down
4 changes: 2 additions & 2 deletions utils/frame/remote-externalities/Cargo.toml
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