/home/runner/work/creditcoin/creditcoin/test/client/src/lib.rs
Line | Count | Source (jump to first uncovered line) |
1 | 1 | use creditcoin_node_runtime::{use creditcoin_node_runtime::{ |
2 | | self as runtime, Block, GenesisConfig, SystemConfig as SystemGenesisConfig, WASM_BINARY, |
3 | | }; |
4 | | use sc_service::client; |
5 | | use sp_core::twox_128; |
6 | | use sp_runtime::traits::{Block as BlockT, Hash as HashT, Header as HeaderT}; |
7 | | use sp_runtime::Storage; |
8 | | use std::collections::BTreeMap; |
9 | | |
10 | | pub type Backend = sc_client_db::Backend<Block>; |
11 | | |
12 | | /// Test client type with `LocalExecutorDispatch` and generic Backend. |
13 | | pub type Client<B> = client::Client< |
14 | | B, |
15 | | client::LocalCallExecutor<Block, B, sc_executor::NativeElseWasmExecutor<LocalExecutorDispatch>>, |
16 | | Block, |
17 | | runtime::RuntimeApi, |
18 | | >; |
19 | | |
20 | | /// A `TestClient` with `test-runtime` builder. |
21 | | pub type TestClientBuilder<E, B> = |
22 | | substrate_test_client::TestClientBuilder<Block, E, B, GenesisParameters>; |
23 | | |
24 | | type TestClient = Client<Backend>; |
25 | | |
26 | | /// Creates new client instance used for tests. |
27 | 2 | pub fn new() -> TestClient { |
28 | 2 | TestClientBuilder::with_default_backend().build_with_native_executor(None).0 |
29 | 2 | } |
30 | | |
31 | | /// A unit struct which implements `NativeExecutionDispatch` feeding in the |
32 | | /// hard-coded runtime. |
33 | | pub struct LocalExecutorDispatch; |
34 | | |
35 | | impl sc_executor::NativeExecutionDispatch for LocalExecutorDispatch { |
36 | | type ExtendHostFunctions = (); |
37 | | |
38 | 0 | fn dispatch(method: &str, data: &[u8]) -> Option<Vec<u8>> { |
39 | 0 | runtime::api::dispatch(method, data) |
40 | 0 | } |
41 | | |
42 | 6 | fn native_version() -> sc_executor::NativeVersion { |
43 | 6 | runtime::native_version() |
44 | 6 | } |
45 | | } |
46 | | |
47 | | /// Parameters of test-client builder with test-runtime. |
48 | 2 | #[derive(Default)] |
49 | | pub struct GenesisParameters { |
50 | | wasm_code: Option<Vec<u8>>, |
51 | | } |
52 | | |
53 | | impl GenesisParameters { |
54 | 2 | fn genesis_config(&self) -> GenesisConfig { |
55 | 2 | GenesisConfig { |
56 | 2 | system: SystemGenesisConfig { code: WASM_BINARY.expect("WASM_BUILD").to_vec() }, |
57 | 2 | ..Default::default() |
58 | 2 | } |
59 | 2 | } |
60 | | } |
61 | | |
62 | | impl substrate_test_client::GenesisInit for GenesisParameters { |
63 | 2 | fn genesis_storage(&self) -> Storage { |
64 | 2 | use runtime::BuildStorage; |
65 | 2 | use sp_runtime::codec::Encode; |
66 | 2 | |
67 | 2 | let mut storage = self.genesis_config().build_storage().unwrap(); |
68 | | |
69 | 2 | if let Some(ref code0 ) = self.wasm_code { |
70 | 0 | storage |
71 | 0 | .top |
72 | 0 | .insert(sp_core::storage::well_known_keys::CODE.to_vec(), code.clone()); |
73 | 2 | } |
74 | | |
75 | 2 | let child_roots = storage.children_default.iter().map(|(_sk, child_content)| { |
76 | 0 | let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root( |
77 | 0 | child_content.data.clone().into_iter().collect(), |
78 | 0 | sp_runtime::StateVersion::V1, |
79 | 0 | ); |
80 | 0 | let prefixed_storage_key = child_content.child_info.prefixed_storage_key(); |
81 | 0 | (prefixed_storage_key.into_inner(), state_root.encode()) |
82 | 2 | }); |
83 | 2 | let state_root = <<<Block as BlockT>::Header as HeaderT>::Hashing as HashT>::trie_root( |
84 | 2 | storage.top.clone().into_iter().chain(child_roots).collect(), |
85 | 2 | sp_runtime::StateVersion::V1, |
86 | 2 | ); |
87 | 2 | let block: runtime::Block = client::genesis::construct_genesis_block(state_root); |
88 | 2 | storage.top.extend(additional_storage_with_genesis(&block)); |
89 | 2 | |
90 | 2 | storage |
91 | 2 | } |
92 | | } |
93 | | |
94 | 2 | fn additional_storage_with_genesis(genesis_block: &Block) -> BTreeMap<Vec<u8>, Vec<u8>> { |
95 | 2 | sp_core::map![ |
96 | 2 | twox_128(&b"latest"[..]).to_vec() => genesis_block.hash().as_fixed_bytes().to_vec() |
97 | 2 | ] |
98 | 2 | } |