-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New testing helpers #6555
New testing helpers #6555
Changes from all commits
44f7a4e
9411a1b
25225de
cb371b9
07d5fc9
edeef50
a105889
db77847
fd000ef
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 |
|---|---|---|
|
|
@@ -36,14 +36,17 @@ 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 std::collections::{HashSet, HashMap}; | ||
| use futures::{future::{Future, FutureExt}, stream::StreamExt}; | ||
| 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}; | ||
| use sc_client_api::BlockchainEvents; | ||
|
|
||
| /// Test client light database backend. | ||
| pub type LightBackend<Block> = sc_light::Backend< | ||
|
|
@@ -255,3 +258,81 @@ impl<Block: BlockT, E, Backend, G: GenesisInit> 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<Box<dyn Future< | ||
| Output = ( | ||
| Option<String>, | ||
| RpcSession, | ||
| futures01::sync::mpsc::Receiver<String>, | ||
| ), | ||
| > + Send>>; | ||
| } | ||
|
|
||
| impl RpcHandlersExt for RpcHandlers { | ||
| fn send_transaction( | ||
| &self, | ||
| extrinsic: OpaqueExtrinsic, | ||
| ) -> Pin<Box<dyn Future< | ||
| Output = ( | ||
| Option<String>, | ||
| RpcSession, | ||
| futures01::sync::mpsc::Receiver<String>, | ||
| ), | ||
| > + Send>> { | ||
| let (tx, rx) = futures01::sync::mpsc::channel(0); | ||
| let mem = RpcSession::new(tx.into()); | ||
| Box::pin(self | ||
| .rpc_query( | ||
| &mem, | ||
| &format!( | ||
| r#"{{ | ||
| "jsonrpc": "2.0", | ||
| "method": "author_submitExtrinsic", | ||
| "params": ["0x{}"], | ||
| "id": 0 | ||
| }}"#, | ||
| hex::encode(extrinsic.encode()) | ||
| ), | ||
| ) | ||
| .map(move |res| (res, mem, rx))) | ||
| } | ||
| } | ||
|
|
||
| /// An extension trait for `BlockchainEvents`. | ||
| pub trait BlockchainEventsExt<C, B> | ||
bkchr marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| where | ||
| C: BlockchainEvents<B>, | ||
| 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<Box<dyn Future<Output = ()> + Send>>; | ||
| } | ||
|
|
||
| impl<C, B> BlockchainEventsExt<C, B> for C | ||
| where | ||
| C: BlockchainEvents<B>, | ||
| B: BlockT, | ||
| { | ||
| fn wait_for_blocks(&self, count: usize) -> Pin<Box<dyn Future<Output = ()> + Send>> { | ||
| assert!(count > 0, "'count' argument must be greater than 0"); | ||
|
|
||
| let mut import_notification_stream = self.import_notification_stream(); | ||
|
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. 👏 |
||
| let mut blocks = HashSet::new(); | ||
|
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. wait this is never used 😮 |
||
|
|
||
| Box::pin(async move { | ||
| while let Some(notification) = import_notification_stream.next().await { | ||
| blocks.insert(notification.hash); | ||
|
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. 👀
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. I think
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 not just a take. The blocks are checked to be unique
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. |
||
| if blocks.len() == count { | ||
| break; | ||
| } | ||
| } | ||
| }) | ||
| } | ||
| } | ||

Uh oh!
There was an error while loading. Please reload this page.