polkadot_test_client/
lib.rs1mod block_builder;
22
23use polkadot_primitives::Block;
24use sp_runtime::BuildStorage;
25use std::sync::Arc;
26
27pub use block_builder::*;
28pub use polkadot_test_runtime as runtime;
29pub use polkadot_test_service::{
30 construct_extrinsic, construct_transfer_extrinsic, Client, FullBackend,
31};
32pub use substrate_test_client::*;
33
34pub type Executor = client::LocalCallExecutor<
36 Block,
37 FullBackend,
38 WasmExecutor<(sp_io::SubstrateHostFunctions, frame_benchmarking::benchmarking::HostFunctions)>,
39>;
40
41pub type TestClientBuilder =
43 substrate_test_client::TestClientBuilder<Block, Executor, FullBackend, GenesisParameters>;
44
45pub type LongestChain = sc_consensus::LongestChain<FullBackend, Block>;
47
48#[derive(Default)]
50pub struct GenesisParameters;
51
52impl substrate_test_client::GenesisInit for GenesisParameters {
53 fn genesis_storage(&self) -> Storage {
54 polkadot_test_service::chain_spec::polkadot_local_testnet_config()
55 .build_storage()
56 .expect("Builds test runtime genesis storage")
57 }
58}
59
60pub trait TestClientBuilderExt: Sized {
62 fn build(self) -> Client {
64 self.build_with_longest_chain().0
65 }
66
67 fn build_with_longest_chain(self) -> (Client, LongestChain);
69}
70
71impl TestClientBuilderExt for TestClientBuilder {
72 fn build_with_longest_chain(self) -> (Client, LongestChain) {
73 let executor = WasmExecutor::builder().build();
74 let executor = client::LocalCallExecutor::new(
75 self.backend().clone(),
76 executor.clone(),
77 Default::default(),
78 ExecutionExtensions::new(Default::default(), Arc::new(executor)),
79 )
80 .unwrap();
81
82 self.build_with_executor(executor)
83 }
84}
85
86pub trait DefaultTestClientBuilderExt: Sized {
88 fn new() -> Self;
90}
91
92impl DefaultTestClientBuilderExt for TestClientBuilder {
93 fn new() -> Self {
94 Self::with_default_backend()
95 }
96}
97
98#[cfg(test)]
99mod tests {
100 use super::*;
101 use sp_consensus::BlockOrigin;
102
103 #[test]
104 fn ensure_test_client_can_build_and_import_block() {
105 let client = TestClientBuilder::new().build();
106
107 let block_builder = client.init_polkadot_block_builder();
108 let block = block_builder.build().expect("Finalizes the block").block;
109
110 futures::executor::block_on(client.import(BlockOrigin::Own, block))
111 .expect("Imports the block");
112 }
113
114 #[test]
115 fn ensure_test_client_can_push_extrinsic() {
116 let client = TestClientBuilder::new().build();
117
118 let transfer = construct_transfer_extrinsic(
119 &client,
120 sp_keyring::Sr25519Keyring::Alice,
121 sp_keyring::Sr25519Keyring::Bob,
122 1000,
123 );
124 let mut block_builder = client.init_polkadot_block_builder();
125 block_builder.push_polkadot_extrinsic(transfer).expect("Pushes extrinsic");
126
127 let block = block_builder.build().expect("Finalizes the block").block;
128
129 futures::executor::block_on(client.import(BlockOrigin::Own, block))
130 .expect("Imports the block");
131 }
132}