mod block_builder;
use polkadot_primitives::Block;
use sp_runtime::BuildStorage;
use std::sync::Arc;
pub use block_builder::*;
pub use polkadot_test_runtime as runtime;
pub use polkadot_test_service::{
construct_extrinsic, construct_transfer_extrinsic, Client, FullBackend,
};
pub use substrate_test_client::*;
pub type Executor = client::LocalCallExecutor<
Block,
FullBackend,
WasmExecutor<(sp_io::SubstrateHostFunctions, frame_benchmarking::benchmarking::HostFunctions)>,
>;
pub type TestClientBuilder =
substrate_test_client::TestClientBuilder<Block, Executor, FullBackend, GenesisParameters>;
pub type LongestChain = sc_consensus::LongestChain<FullBackend, Block>;
#[derive(Default)]
pub struct GenesisParameters;
impl substrate_test_client::GenesisInit for GenesisParameters {
fn genesis_storage(&self) -> Storage {
polkadot_test_service::chain_spec::polkadot_local_testnet_config()
.build_storage()
.expect("Builds test runtime genesis storage")
}
}
pub trait TestClientBuilderExt: Sized {
fn build(self) -> Client {
self.build_with_longest_chain().0
}
fn build_with_longest_chain(self) -> (Client, LongestChain);
}
impl TestClientBuilderExt for TestClientBuilder {
fn build_with_longest_chain(self) -> (Client, LongestChain) {
let executor = WasmExecutor::builder().build();
let executor = client::LocalCallExecutor::new(
self.backend().clone(),
executor.clone(),
Default::default(),
ExecutionExtensions::new(Default::default(), Arc::new(executor)),
)
.unwrap();
self.build_with_executor(executor)
}
}
pub trait DefaultTestClientBuilderExt: Sized {
fn new() -> Self;
}
impl DefaultTestClientBuilderExt for TestClientBuilder {
fn new() -> Self {
Self::with_default_backend()
}
}
#[cfg(test)]
mod tests {
use super::*;
use sp_consensus::BlockOrigin;
#[test]
fn ensure_test_client_can_build_and_import_block() {
let client = TestClientBuilder::new().build();
let block_builder = client.init_polkadot_block_builder();
let block = block_builder.build().expect("Finalizes the block").block;
futures::executor::block_on(client.import(BlockOrigin::Own, block))
.expect("Imports the block");
}
#[test]
fn ensure_test_client_can_push_extrinsic() {
let client = TestClientBuilder::new().build();
let transfer = construct_transfer_extrinsic(
&client,
sp_keyring::Sr25519Keyring::Alice,
sp_keyring::Sr25519Keyring::Bob,
1000,
);
let mut block_builder = client.init_polkadot_block_builder();
block_builder.push_polkadot_extrinsic(transfer).expect("Pushes extrinsic");
let block = block_builder.build().expect("Finalizes the block").block;
futures::executor::block_on(client.import(BlockOrigin::Own, block))
.expect("Imports the block");
}
}