From 44f7a4ef991761ae2fe431aeae067f6df399a9f7 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 2 Jul 2020 11:57:59 +0200 Subject: [PATCH 1/9] Initial commit Forked at: 8ef1ac0ee13d2a72cc1c391d4624dfaaafe641e8 Parent branch: origin/master From 9411a1b0df8f7433402f8c50f5b2138ccc0b2ad9 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 2 Jul 2020 12:04:45 +0200 Subject: [PATCH 2/9] Add send_transaction to RpcHandlers --- Cargo.lock | 1 + client/service/Cargo.toml | 1 + client/service/src/lib.rs | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 94f3f5effea98..b32e56416704b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6822,6 +6822,7 @@ dependencies = [ "futures 0.3.5", "futures-timer 3.0.2", "hash-db", + "hex", "jsonrpc-pubsub", "lazy_static", "log", diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index f63d3f183db1d..621c05adb0bad 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -40,6 +40,7 @@ hash-db = "0.15.2" serde = "1.0.101" serde_json = "1.0.41" sysinfo = "0.13.3" +hex = "0.4" sc-keystore = { version = "2.0.0-rc4", path = "../keystore" } sp-io = { version = "2.0.0-rc4", path = "../../primitives/io" } sp-runtime = { version = "2.0.0-rc4", path = "../../primitives/runtime" } diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index c3c8f60e689ad..880d7761779cc 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -47,8 +47,11 @@ use futures::{Future, FutureExt, Stream, StreamExt, compat::*}; use sc_network::{NetworkStatus, network_state::NetworkState, PeerId}; use log::{log, warn, debug, error, Level}; use codec::{Encode, Decode}; -use sp_runtime::generic::BlockId; -use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; +use sp_runtime::{ + OpaqueExtrinsic, + generic::BlockId, + traits::{Block as BlockT, Header as HeaderT}, +}; use parity_util_mem::MallocSizeOf; use sp_utils::{status_sinks, mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}}; @@ -113,6 +116,36 @@ impl RpcHandlers { .map(|res| res.expect("this should never fail")) .boxed() } + + /// Send a transaction through the RpcHandlers. + pub async fn send_transaction( + &self, + extrinsic: OpaqueExtrinsic, + ) -> ( + Option, + RpcSession, + futures01::sync::mpsc::Receiver, + ) { + let (tx, rx) = futures01::sync::mpsc::channel(0); + let mem = RpcSession::new(tx.into()); + let res = self + .rpc_query( + &mem, + format!( + r#"{{ + "jsonrpc": "2.0", + "method": "author_submitExtrinsic", + "params": ["0x{}"], + "id": 0 + }}"#, + hex::encode(extrinsic.encode()) + ) + .as_str(), + ) + .await; + + (res, mem, rx) + } } /// Sinks to propagate network status updates. From 25225dee21fdbc28fd46bf97be10cfd6896d66b5 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 2 Jul 2020 12:35:09 +0200 Subject: [PATCH 3/9] Extension trait for RpcHandlers --- Cargo.lock | 2 ++ test-utils/client/Cargo.toml | 2 ++ test-utils/client/src/lib.rs | 57 ++++++++++++++++++++++++++++++++++-- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b32e56416704b..62433376c933c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8297,8 +8297,10 @@ dependencies = [ name = "substrate-test-client" version = "2.0.0-rc4" dependencies = [ + "futures 0.1.29", "futures 0.3.5", "hash-db", + "hex", "parity-scale-codec", "sc-client-api", "sc-client-db", diff --git a/test-utils/client/Cargo.toml b/test-utils/client/Cargo.toml index a9d8590f021fa..e9036bc77abb4 100644 --- a/test-utils/client/Cargo.toml +++ b/test-utils/client/Cargo.toml @@ -20,7 +20,9 @@ sc-executor = { version = "0.8.0-rc4", path = "../../client/executor" } sc-consensus = { version = "0.8.0-rc4", path = "../../client/consensus/common" } sc-service = { version = "0.8.0-rc4", default-features = false, features = ["test-helpers"], path = "../../client/service" } futures = "0.3.4" +futures01 = { package = "futures", version = "0.1.29" } hash-db = "0.15.2" +hex = "0.4" sp-keyring = { version = "2.0.0-rc4", path = "../../primitives/keyring" } codec = { package = "parity-scale-codec", version = "1.3.1" } sp-core = { version = "2.0.0-rc4", path = "../../primitives/core" } diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index 2ab9e4066ddd1..85f1e4aa7bec4 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -36,13 +36,15 @@ pub use sp_keyring::{ pub use sp_core::{traits::BareCryptoStorePtr, tasks::executor as tasks_executor}; pub use sp_runtime::{Storage, StorageChild}; pub use sp_state_machine::ExecutionStrategy; -pub use sc_service::client; +pub use sc_service::{RpcHandlers, RpcSession, client}; pub use self::client_ext::{ClientExt, ClientBlockImportExt}; +use std::pin::Pin; use std::sync::Arc; use std::collections::HashMap; +use futures::future::Future; use sp_core::storage::ChildInfo; -use sp_runtime::traits::{Block as BlockT, BlakeTwo256}; +use sp_runtime::{OpaqueExtrinsic, codec::Encode, traits::{Block as BlockT, BlakeTwo256}}; use sc_service::client::{LocalCallExecutor, ClientConfig}; /// Test client light database backend. @@ -255,3 +257,54 @@ impl TestClientBuilder< self.build_with_executor(executor) } } + +/// An extension trait for `RpcHandlers`. +pub trait RpcHandlersExt { + /// Send a transaction through the RpcHandlers. + fn send_transaction( + &self, + extrinsic: OpaqueExtrinsic, + ) -> Pin, + RpcSession, + futures01::sync::mpsc::Receiver, + ), + > + Send>>; +} + +impl RpcHandlersExt for RpcHandlers { + fn send_transaction( + &self, + extrinsic: OpaqueExtrinsic, + ) -> Pin, + RpcSession, + futures01::sync::mpsc::Receiver, + ), + > + Send>> { + let (tx, rx) = futures01::sync::mpsc::channel(0); + let mem = RpcSession::new(tx.into()); + let future = self + .rpc_query( + &mem, + format!( + r#"{{ + "jsonrpc": "2.0", + "method": "author_submitExtrinsic", + "params": ["0x{}"], + "id": 0 + }}"#, + hex::encode(extrinsic.encode()) + ) + .as_str(), + ); + + Box::pin(async move { + let res = future.await; + + (res, mem, rx) + }) + } +} From cb371b9c50dc22921869cd08579caf41016eec89 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 2 Jul 2020 12:35:30 +0200 Subject: [PATCH 4/9] Revert "Add send_transaction to RpcHandlers" This reverts commit 03c89e13d404bae3f3123387dd50f026061bca82. --- Cargo.lock | 1 - client/service/Cargo.toml | 1 - client/service/src/lib.rs | 37 ++----------------------------------- 3 files changed, 2 insertions(+), 37 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 62433376c933c..051a10f8190be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6822,7 +6822,6 @@ dependencies = [ "futures 0.3.5", "futures-timer 3.0.2", "hash-db", - "hex", "jsonrpc-pubsub", "lazy_static", "log", diff --git a/client/service/Cargo.toml b/client/service/Cargo.toml index 621c05adb0bad..f63d3f183db1d 100644 --- a/client/service/Cargo.toml +++ b/client/service/Cargo.toml @@ -40,7 +40,6 @@ hash-db = "0.15.2" serde = "1.0.101" serde_json = "1.0.41" sysinfo = "0.13.3" -hex = "0.4" sc-keystore = { version = "2.0.0-rc4", path = "../keystore" } sp-io = { version = "2.0.0-rc4", path = "../../primitives/io" } sp-runtime = { version = "2.0.0-rc4", path = "../../primitives/runtime" } diff --git a/client/service/src/lib.rs b/client/service/src/lib.rs index 880d7761779cc..c3c8f60e689ad 100644 --- a/client/service/src/lib.rs +++ b/client/service/src/lib.rs @@ -47,11 +47,8 @@ use futures::{Future, FutureExt, Stream, StreamExt, compat::*}; use sc_network::{NetworkStatus, network_state::NetworkState, PeerId}; use log::{log, warn, debug, error, Level}; use codec::{Encode, Decode}; -use sp_runtime::{ - OpaqueExtrinsic, - generic::BlockId, - traits::{Block as BlockT, Header as HeaderT}, -}; +use sp_runtime::generic::BlockId; +use sp_runtime::traits::{Block as BlockT, Header as HeaderT}; use parity_util_mem::MallocSizeOf; use sp_utils::{status_sinks, mpsc::{tracing_unbounded, TracingUnboundedReceiver, TracingUnboundedSender}}; @@ -116,36 +113,6 @@ impl RpcHandlers { .map(|res| res.expect("this should never fail")) .boxed() } - - /// Send a transaction through the RpcHandlers. - pub async fn send_transaction( - &self, - extrinsic: OpaqueExtrinsic, - ) -> ( - Option, - RpcSession, - futures01::sync::mpsc::Receiver, - ) { - let (tx, rx) = futures01::sync::mpsc::channel(0); - let mem = RpcSession::new(tx.into()); - let res = self - .rpc_query( - &mem, - format!( - r#"{{ - "jsonrpc": "2.0", - "method": "author_submitExtrinsic", - "params": ["0x{}"], - "id": 0 - }}"#, - hex::encode(extrinsic.encode()) - ) - .as_str(), - ) - .await; - - (res, mem, rx) - } } /// Sinks to propagate network status updates. From 07d5fc9c984561bc52dfde88bc853e51e46bddf9 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 2 Jul 2020 12:46:49 +0200 Subject: [PATCH 5/9] Add an extension trait for BlockchainEvents --- test-utils/client/src/lib.rs | 38 ++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index 85f1e4aa7bec4..dfa0d0122964b 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -41,11 +41,12 @@ pub use self::client_ext::{ClientExt, ClientBlockImportExt}; use std::pin::Pin; use std::sync::Arc; -use std::collections::HashMap; -use futures::future::Future; +use std::collections::{HashSet, HashMap}; +use futures::{future::Future, stream::StreamExt}; use sp_core::storage::ChildInfo; use sp_runtime::{OpaqueExtrinsic, codec::Encode, traits::{Block as BlockT, BlakeTwo256}}; use sc_service::client::{LocalCallExecutor, ClientConfig}; +use sc_client_api::BlockchainEvents; /// Test client light database backend. pub type LightBackend = sc_light::Backend< @@ -308,3 +309,36 @@ impl RpcHandlersExt for RpcHandlers { }) } } + +/// An extension trait for `BlockchainEvents`. +pub trait BlockchainEventsExt +where + C: BlockchainEvents, + B: BlockT, +{ + /// Wait for `count` blocks to be imported in the node and then exit. This function will not return if no blocks + /// are ever created, thus you should restrict the maximum amount of time of the test execution. + fn wait_for_blocks(&self, count: usize) -> Pin + Send>>; +} + +impl BlockchainEventsExt for C +where + C: BlockchainEvents, + B: BlockT, +{ + fn wait_for_blocks(&self, count: usize) -> Pin + Send>> { + assert!(count > 0, "'count' argument must be greater than 0"); + + let mut import_notification_stream = self.import_notification_stream(); + let mut blocks = HashSet::new(); + + Box::pin(async move { + while let Some(notification) = import_notification_stream.next().await { + blocks.insert(notification.hash); + if blocks.len() == count { + break; + } + } + }) + } +} From edeef505b1418ce450f90254795a807142f35530 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 2 Jul 2020 13:40:42 +0200 Subject: [PATCH 6/9] Update test-utils/client/src/lib.rs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bastian Köcher --- test-utils/client/src/lib.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index dfa0d0122964b..bf245c49ffd80 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -287,10 +287,10 @@ impl RpcHandlersExt for RpcHandlers { > + Send>> { let (tx, rx) = futures01::sync::mpsc::channel(0); let mem = RpcSession::new(tx.into()); - let future = self + self .rpc_query( &mem, - format!( + &format!( r#"{{ "jsonrpc": "2.0", "method": "author_submitExtrinsic", @@ -298,15 +298,9 @@ impl RpcHandlersExt for RpcHandlers { "id": 0 }}"#, hex::encode(extrinsic.encode()) - ) - .as_str(), - ); - - Box::pin(async move { - let res = future.await; - - (res, mem, rx) - }) + ), + ) + .map(move |res| (res, mem, rx)) } } From a105889167820571ff48c8b4fe7ae9002a918832 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 2 Jul 2020 15:11:51 +0200 Subject: [PATCH 7/9] Update test-utils/client/src/lib.rs --- test-utils/client/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index bf245c49ffd80..b7385b377e26c 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -42,7 +42,7 @@ pub use self::client_ext::{ClientExt, ClientBlockImportExt}; use std::pin::Pin; use std::sync::Arc; use std::collections::{HashSet, HashMap}; -use futures::{future::Future, stream::StreamExt}; +use futures::{future::{Future, FutureExt}, stream::StreamExt}; use sp_core::storage::ChildInfo; use sp_runtime::{OpaqueExtrinsic, codec::Encode, traits::{Block as BlockT, BlakeTwo256}}; use sc_service::client::{LocalCallExecutor, ClientConfig}; From db778476b9fc7c895b5e25c7ee120614f0776c46 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Thu, 2 Jul 2020 15:53:06 +0200 Subject: [PATCH 8/9] fix --- test-utils/client/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-utils/client/src/lib.rs b/test-utils/client/src/lib.rs index b7385b377e26c..fef9acd9d2d54 100644 --- a/test-utils/client/src/lib.rs +++ b/test-utils/client/src/lib.rs @@ -287,7 +287,7 @@ impl RpcHandlersExt for RpcHandlers { > + Send>> { let (tx, rx) = futures01::sync::mpsc::channel(0); let mem = RpcSession::new(tx.into()); - self + Box::pin(self .rpc_query( &mem, &format!( @@ -300,7 +300,7 @@ impl RpcHandlersExt for RpcHandlers { hex::encode(extrinsic.encode()) ), ) - .map(move |res| (res, mem, rx)) + .map(move |res| (res, mem, rx))) } } From fd000eff12c869a83170f54d08c69d42939873b3 Mon Sep 17 00:00:00 2001 From: Cecile Tonglet Date: Fri, 3 Jul 2020 11:21:07 +0200 Subject: [PATCH 9/9] deps fix --- Cargo.lock | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 051a10f8190be..81c82e9ac992d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8903,9 +8903,9 @@ dependencies = [ [[package]] name = "tokio-rustls" -version = "0.13.0" +version = "0.13.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4adb8b3e5f86b707f1b54e7c15b6de52617a823608ccda98a15d3a24222f265a" +checksum = "15cb62a0d2770787abc96e99c1cd98fcf17f94959f3af63ca85bdfb203f051b4" dependencies = [ "futures-core", "rustls",