1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use futures::FutureExt;
use sc_client_api::execution_extensions::ExecutionStrategies;
use sc_executor::WasmExecutionMethod;
use sc_informant::OutputFormat;
use sc_network::{
config::{NetworkConfiguration, Role, TransportConfig},
multiaddr,
};
use sc_service::{
config::KeystoreConfig, BasePath, ChainSpec, Configuration, DatabaseConfig, KeepBlocks,
TaskExecutor, TaskType, TransactionStorageMode,
};
use sp_keyring::sr25519::Keyring::Alice;
use tokio::runtime::Handle;
pub use sc_cli::build_runtime;
pub fn base_path() -> BasePath {
if let Some(base) = std::env::var("DB_BASE_PATH").ok() {
BasePath::new(base)
} else {
BasePath::new_temp_dir().expect("couldn't create a temp dir")
}
}
pub fn default_config(
task_executor: TaskExecutor,
mut chain_spec: Box<dyn ChainSpec>,
) -> Configuration {
let base_path = base_path();
let root_path = base_path.path().to_path_buf().join("chains").join(chain_spec.id());
let storage = chain_spec
.as_storage_builder()
.build_storage()
.expect("could not build storage");
chain_spec.set_storage(storage);
let key_seed = Alice.to_seed();
let mut network_config = NetworkConfiguration::new(
format!("Test Node for: {}", key_seed),
"network/test/0.1",
Default::default(),
None,
);
let informant_output_format = OutputFormat { enable_color: false };
network_config.allow_non_globals_in_dht = true;
network_config.listen_addresses.push(multiaddr::Protocol::Memory(0).into());
network_config.transport = TransportConfig::MemoryOnly;
Configuration {
impl_name: "test-node".to_string(),
impl_version: "0.1".to_string(),
role: Role::Authority,
task_executor: task_executor.into(),
transaction_pool: Default::default(),
network: network_config,
keystore: KeystoreConfig::Path { path: root_path.join("key"), password: None },
database: DatabaseConfig::RocksDb { path: root_path.join("db"), cache_size: 128 },
state_cache_size: 16777216,
state_cache_child_ratio: None,
chain_spec,
wasm_method: WasmExecutionMethod::Interpreted,
execution_strategies: ExecutionStrategies {
syncing: sc_client_api::ExecutionStrategy::AlwaysWasm,
importing: sc_client_api::ExecutionStrategy::AlwaysWasm,
block_construction: sc_client_api::ExecutionStrategy::AlwaysWasm,
offchain_worker: sc_client_api::ExecutionStrategy::AlwaysWasm,
other: sc_client_api::ExecutionStrategy::AlwaysWasm,
},
rpc_http: None,
rpc_ws: None,
rpc_ipc: None,
rpc_ws_max_connections: None,
rpc_http_threads: None,
rpc_cors: None,
rpc_methods: Default::default(),
rpc_max_payload: None,
prometheus_config: None,
telemetry_endpoints: None,
telemetry_external_transport: None,
default_heap_pages: None,
offchain_worker: Default::default(),
force_authoring: false,
disable_grandpa: false,
dev_key_seed: Some(key_seed),
tracing_targets: None,
tracing_receiver: Default::default(),
max_runtime_instances: 8,
announce_block: true,
base_path: Some(base_path),
wasm_runtime_overrides: None,
informant_output_format,
disable_log_reloading: false,
keystore_remote: None,
keep_blocks: KeepBlocks::All,
state_pruning: Default::default(),
transaction_storage: TransactionStorageMode::BlockBody,
}
}
pub fn task_executor(handle: Handle) -> TaskExecutor {
let task_executor = move |fut, task_type| match task_type {
TaskType::Async => handle.spawn(fut).map(drop),
TaskType::Blocking =>
handle.spawn_blocking(move || futures::executor::block_on(fut)).map(drop),
};
task_executor.into()
}