forked from paritytech/polkadot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test-utils: test executor, backend and function to create test client (…
…paritytech#73) * test-utils: basic executor and backend definitions * test-utils: implement function to create a new test client * Fix indentation
- Loading branch information
Showing
4 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "frontier-template-test-client" | ||
version = "0.1.0" | ||
authors = ["Parity Technologies <admin@parity.io>"] | ||
edition = "2018" | ||
description = "Ethereum RPC (web3) compatibility layer for Substrate." | ||
license = "GPL-3.0" | ||
|
||
[dependencies] | ||
codec = { package = "parity-scale-codec", version = "1.3.1" } | ||
sp-runtime = { path = "../../../vendor/substrate/primitives/runtime" } | ||
sp-api = { path = "../../../vendor/substrate/primitives/api" } | ||
sc-executor = { path = "../../../vendor/substrate/client/executor" } | ||
sc-light = { path = "../../../vendor/substrate/client/light" } | ||
sc-consensus = { path = "../../../vendor/substrate/client/consensus/common" } | ||
sc-client-api = { path = "../../../vendor/substrate/client/api" } | ||
substrate-test-client = { path = "../../../vendor/substrate/test-utils/client" } | ||
frontier-template-runtime = { path = "../../runtime" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
use std::sync::Arc; | ||
|
||
pub use substrate_test_client::*; | ||
pub use frontier_template_runtime as runtime; | ||
use sp_runtime::traits::HashFor; | ||
|
||
sc_executor::native_executor_instance! { | ||
pub LocalExecutor, | ||
runtime::api::dispatch, | ||
runtime::native_version, | ||
} | ||
|
||
pub type Backend = substrate_test_client::Backend<runtime::opaque::Block>; | ||
|
||
pub type Executor = client::LocalCallExecutor< | ||
Backend, | ||
NativeExecutor<LocalExecutor>, | ||
>; | ||
|
||
pub type LightBackend = substrate_test_client::LightBackend<runtime::opaque::Block>; | ||
|
||
pub type LightExecutor = sc_light::GenesisCallExecutor< | ||
LightBackend, | ||
client::LocalCallExecutor< | ||
sc_light::Backend< | ||
sc_client_db::light::LightStorage<runtime::opaque::Block>, | ||
HashFor<runtime::opaque::Block> | ||
>, | ||
NativeExecutor<LocalExecutor> | ||
> | ||
>; | ||
|
||
/// Parameters of test-client builder with test-runtime. | ||
#[derive(Default)] | ||
pub struct GenesisParameters; | ||
|
||
impl substrate_test_client::GenesisInit for GenesisParameters { | ||
fn genesis_storage(&self) -> Storage { | ||
Storage::default() | ||
} | ||
} | ||
|
||
/// A `TestClient` with `test-runtime` builder. | ||
pub type TestClientBuilder<E, B> = substrate_test_client::TestClientBuilder< | ||
runtime::opaque::Block, | ||
E, | ||
B, | ||
GenesisParameters, | ||
>; | ||
|
||
/// Test client type with `LocalExecutor` and generic Backend. | ||
pub type Client<B> = client::Client< | ||
B, | ||
client::LocalCallExecutor<B, sc_executor::NativeExecutor<LocalExecutor>>, | ||
runtime::opaque::Block, | ||
runtime::RuntimeApi, | ||
>; | ||
|
||
/// A test client with default backend. | ||
pub type TestClient = Client<Backend>; | ||
|
||
/// A `TestClientBuilder` with default backend and executor. | ||
pub trait DefaultTestClientBuilderExt: Sized { | ||
/// Create new `TestClientBuilder` | ||
fn new() -> Self; | ||
} | ||
|
||
impl DefaultTestClientBuilderExt for TestClientBuilder<Executor, Backend> { | ||
fn new() -> Self { | ||
Self::with_default_backend() | ||
} | ||
} | ||
|
||
/// A `test-runtime` extensions to `TestClientBuilder`. | ||
pub trait TestClientBuilderExt<B>: Sized { | ||
/// Build the test client. | ||
fn build(self) -> Client<B> { | ||
self.build_with_longest_chain().0 | ||
} | ||
|
||
/// Build the test client and longest chain selector. | ||
fn build_with_longest_chain(self) -> (Client<B>, sc_consensus::LongestChain<B, runtime::opaque::Block>); | ||
|
||
/// Build the test client and the backend. | ||
fn build_with_backend(self) -> (Client<B>, Arc<B>); | ||
} | ||
|
||
impl<B> TestClientBuilderExt<B> for TestClientBuilder< | ||
client::LocalCallExecutor<B, sc_executor::NativeExecutor<LocalExecutor>>, | ||
B | ||
> where | ||
B: sc_client_api::backend::Backend<runtime::opaque::Block> + 'static, | ||
// Rust bug: https://github.com/rust-lang/rust/issues/24159 | ||
<B as sc_client_api::backend::Backend<runtime::opaque::Block>>::State: | ||
sp_api::StateBackend<HashFor<runtime::opaque::Block>>, | ||
{ | ||
fn build_with_longest_chain(self) -> (Client<B>, sc_consensus::LongestChain<B, runtime::opaque::Block>) { | ||
self.build_with_native_executor(None) | ||
} | ||
|
||
fn build_with_backend(self) -> (Client<B>, Arc<B>) { | ||
let backend = self.backend(); | ||
(self.build_with_native_executor(None).0, backend) | ||
} | ||
} | ||
|
||
/// Creates new client instance used for tests. | ||
pub fn new() -> Client<Backend> { | ||
TestClientBuilder::new().build() | ||
} |