forked from nervosnetwork/ckb
/
app_config.rs
387 lines (351 loc) · 13.4 KB
/
app_config.rs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
use std::fs;
use std::path::{Path, PathBuf};
use serde_derive::Deserialize;
use ckb_chain_spec::ChainSpec;
use ckb_db::DBConfig;
use ckb_miner::BlockAssemblerConfig;
use ckb_miner::MinerConfig;
use ckb_network::NetworkConfig;
use ckb_resource::{Resource, ResourceLocator};
use ckb_rpc::Config as RpcConfig;
use ckb_shared::tx_pool::TxPoolConfig;
use ckb_sync::Config as SyncConfig;
use logger::Config as LogConfig;
use super::sentry_config::SentryConfig;
use super::{cli, ExitCode};
pub struct AppConfig {
resource: Resource,
content: AppConfigContent,
}
pub enum AppConfigContent {
CKB(Box<CKBAppConfig>),
Miner(Box<MinerAppConfig>),
}
#[derive(Clone, Debug, Deserialize)]
pub struct CKBAppConfig {
pub chain: ChainConfig,
pub data_dir: PathBuf,
pub logger: LogConfig,
pub sentry: SentryConfig,
pub block_assembler: BlockAssemblerConfig,
#[serde(default)]
pub db: DBConfig,
pub network: NetworkConfig,
pub rpc: RpcConfig,
pub sync: SyncConfig,
pub tx_pool: TxPoolConfig,
}
#[derive(Clone, Debug, Deserialize)]
pub struct MinerAppConfig {
pub chain: ChainConfig,
pub data_dir: PathBuf,
pub logger: LogConfig,
pub sentry: SentryConfig,
pub miner: MinerConfig,
}
#[derive(Clone, Debug, Deserialize)]
pub struct ChainConfig {
pub spec: PathBuf,
}
impl AppConfig {
pub fn is_bundled(&self) -> bool {
self.resource.is_bundled()
}
pub fn load_for_subcommand(
locator: &ResourceLocator,
subcommand_name: &str,
) -> Result<AppConfig, ExitCode> {
match subcommand_name {
cli::CMD_MINER => {
let resource = locator.miner();
let config: MinerAppConfig = toml::from_slice(&resource.get()?)?;
Ok(AppConfig {
resource,
content: AppConfigContent::with_miner(
config.derive_options(locator.root_dir())?,
),
})
}
_ => {
let resource = locator.ckb();
let config: CKBAppConfig = toml::from_slice(&resource.get()?)?;
Ok(AppConfig {
resource,
content: AppConfigContent::with_ckb(
config.derive_options(locator.root_dir(), subcommand_name)?,
),
})
}
}
}
pub fn logger(&self) -> &LogConfig {
match &self.content {
AppConfigContent::CKB(config) => &config.logger,
AppConfigContent::Miner(config) => &config.logger,
}
}
pub fn sentry(&self) -> &SentryConfig {
match &self.content {
AppConfigContent::CKB(config) => &config.sentry,
AppConfigContent::Miner(config) => &config.sentry,
}
}
pub fn chain_spec(&self, locator: &ResourceLocator) -> Result<ChainSpec, ExitCode> {
let spec_path = PathBuf::from(match &self.content {
AppConfigContent::CKB(config) => &config.chain.spec,
AppConfigContent::Miner(config) => &config.chain.spec,
});
ChainSpec::resolve_relative_to(locator, spec_path, &self.resource).map_err(|err| {
eprintln!("{:?}", err);
ExitCode::Config
})
}
pub fn into_ckb(self) -> Result<Box<CKBAppConfig>, ExitCode> {
match self.content {
AppConfigContent::CKB(config) => Ok(config),
_ => {
eprintln!("unmatched config file");
Err(ExitCode::Failure)
}
}
}
pub fn into_miner(self) -> Result<Box<MinerAppConfig>, ExitCode> {
match self.content {
AppConfigContent::Miner(config) => Ok(config),
_ => {
eprintln!("unmatched config file");
Err(ExitCode::Failure)
}
}
}
}
impl AppConfigContent {
fn with_ckb(config: CKBAppConfig) -> AppConfigContent {
AppConfigContent::CKB(Box::new(config))
}
fn with_miner(config: MinerAppConfig) -> AppConfigContent {
AppConfigContent::Miner(Box::new(config))
}
}
impl CKBAppConfig {
fn derive_options(mut self, root_dir: &Path, subcommand_name: &str) -> Result<Self, ExitCode> {
self.data_dir = canonicalize_data_dir(self.data_dir, root_dir)?;
if self.logger.log_to_file {
self.logger.file = Some(touch(
mkdir(self.data_dir.join("logs"))?.join(subcommand_name.to_string() + ".log"),
)?);
}
self.db.path = mkdir(self.data_dir.join("db"))?;
self.network.path = mkdir(self.data_dir.join("network"))?;
Ok(self)
}
}
impl MinerAppConfig {
fn derive_options(mut self, root_dir: &Path) -> Result<Self, ExitCode> {
self.data_dir = canonicalize_data_dir(self.data_dir, root_dir)?;
if self.logger.log_to_file {
self.logger.file = Some(touch(mkdir(self.data_dir.join("logs"))?.join("miner.log"))?);
}
Ok(self)
}
}
fn canonicalize_data_dir(data_dir: PathBuf, root_dir: &Path) -> Result<PathBuf, ExitCode> {
let path = if data_dir.is_absolute() {
data_dir
} else {
root_dir.join(data_dir)
};
mkdir(path)
}
fn mkdir(dir: PathBuf) -> Result<PathBuf, ExitCode> {
fs::create_dir_all(&dir)?;
dir.canonicalize().map_err(Into::into)
}
fn touch(path: PathBuf) -> Result<PathBuf, ExitCode> {
fs::OpenOptions::new()
.create(true)
.append(true)
.open(&path)?;
Ok(path)
}
#[cfg(test)]
mod tests {
use super::*;
use ckb_resource::TemplateContext;
fn mkdir() -> tempfile::TempDir {
tempfile::Builder::new()
.prefix("app_config_test")
.tempdir()
.unwrap()
}
#[test]
fn test_ckb_toml() {
let dir = mkdir();
let locator = ResourceLocator::with_root_dir(dir.path().to_path_buf()).unwrap();
let app_config = AppConfig::load_for_subcommand(&locator, cli::CMD_RUN)
.unwrap_or_else(|err| panic!(err));
let ckb_config = app_config.into_ckb().unwrap_or_else(|err| panic!(err));
assert_eq!(ckb_config.chain.spec, PathBuf::from("specs/dev.toml"));
assert_eq!(
ckb_config.logger.file,
Some(locator.root_dir().join("data/logs/run.log"))
);
assert_eq!(ckb_config.db.path, locator.root_dir().join("data/db"));
assert_eq!(
ckb_config.network.path,
locator.root_dir().join("data/network")
);
}
#[test]
fn test_miner_toml() {
let dir = mkdir();
let locator = ResourceLocator::with_root_dir(dir.path().to_path_buf()).unwrap();
let app_config = AppConfig::load_for_subcommand(&locator, cli::CMD_MINER)
.unwrap_or_else(|err| panic!(err));
let miner_config = app_config.into_miner().unwrap_or_else(|err| panic!(err));
assert_eq!(miner_config.chain.spec, PathBuf::from("specs/dev.toml"));
assert_eq!(
miner_config.logger.file,
Some(locator.root_dir().join("data/logs/miner.log"))
);
}
#[test]
fn test_export_dev_config_files() {
let dir = mkdir();
let locator = ResourceLocator::with_root_dir(dir.path().to_path_buf()).unwrap();
let context = TemplateContext {
spec: "dev",
rpc_port: "7000",
p2p_port: "8000",
log_to_file: true,
log_to_stdout: true,
};
{
locator.export_ckb(&context).expect("export config files");
let app_config = AppConfig::load_for_subcommand(&locator, cli::CMD_RUN)
.unwrap_or_else(|err| panic!(err));
let ckb_config = app_config.into_ckb().unwrap_or_else(|err| panic!(err));
assert_eq!(ckb_config.logger.filter, Some("info".to_string()));
assert_eq!(ckb_config.chain.spec, PathBuf::from("specs/dev.toml"));
assert_eq!(
ckb_config.network.listen_addresses,
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
);
assert_eq!(ckb_config.network.connect_outbound_interval_secs, 15);
assert_eq!(ckb_config.rpc.listen_address, "0.0.0.0:7000");
}
{
locator.export_miner(&context).expect("export config files");
let app_config = AppConfig::load_for_subcommand(&locator, cli::CMD_MINER)
.unwrap_or_else(|err| panic!(err));
let miner_config = app_config.into_miner().unwrap_or_else(|err| panic!(err));
assert_eq!(miner_config.logger.filter, Some("info".to_string()));
assert_eq!(miner_config.chain.spec, PathBuf::from("specs/dev.toml"));
assert_eq!(miner_config.miner.rpc_url, "http://127.0.0.1:7000/");
}
}
#[test]
fn test_log_to_stdout_only() {
let dir = mkdir();
let locator = ResourceLocator::with_root_dir(dir.path().to_path_buf()).unwrap();
let context = TemplateContext {
spec: "dev",
rpc_port: "7000",
p2p_port: "8000",
log_to_file: false,
log_to_stdout: true,
};
{
locator.export_ckb(&context).expect("export config files");
let app_config = AppConfig::load_for_subcommand(&locator, cli::CMD_RUN)
.unwrap_or_else(|err| panic!(err));
let ckb_config = app_config.into_ckb().unwrap_or_else(|err| panic!(err));
assert_eq!(ckb_config.logger.file, None);
assert_eq!(ckb_config.logger.log_to_file, false);
assert_eq!(ckb_config.logger.log_to_stdout, true);
}
{
locator.export_miner(&context).expect("export config files");
let app_config = AppConfig::load_for_subcommand(&locator, cli::CMD_MINER)
.unwrap_or_else(|err| panic!(err));
let miner_config = app_config.into_miner().unwrap_or_else(|err| panic!(err));
assert_eq!(miner_config.logger.file, None);
assert_eq!(miner_config.logger.log_to_file, false);
assert_eq!(miner_config.logger.log_to_stdout, true);
}
}
#[test]
fn test_export_testnet_config_files() {
let dir = mkdir();
let locator = ResourceLocator::with_root_dir(dir.path().to_path_buf()).unwrap();
let context = TemplateContext {
spec: "testnet",
rpc_port: "7000",
p2p_port: "8000",
log_to_file: true,
log_to_stdout: true,
};
locator.export_ckb(&context).expect("export config files");
{
let app_config = AppConfig::load_for_subcommand(&locator, cli::CMD_RUN)
.unwrap_or_else(|err| panic!(err));
let ckb_config = app_config.into_ckb().unwrap_or_else(|err| panic!(err));
assert_eq!(ckb_config.logger.filter, Some("info".to_string()));
assert_eq!(ckb_config.chain.spec, PathBuf::from("specs/testnet.toml"));
assert_eq!(
ckb_config.network.listen_addresses,
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
);
assert_eq!(ckb_config.network.connect_outbound_interval_secs, 15);
assert_eq!(ckb_config.rpc.listen_address, "0.0.0.0:7000");
}
{
locator.export_miner(&context).expect("export config files");
let app_config = AppConfig::load_for_subcommand(&locator, cli::CMD_MINER)
.unwrap_or_else(|err| panic!(err));
let miner_config = app_config.into_miner().unwrap_or_else(|err| panic!(err));
assert_eq!(miner_config.logger.filter, Some("info".to_string()));
assert_eq!(miner_config.chain.spec, PathBuf::from("specs/testnet.toml"));
assert_eq!(miner_config.miner.rpc_url, "http://127.0.0.1:7000/");
}
}
#[test]
fn test_export_integration_config_files() {
let dir = mkdir();
let locator = ResourceLocator::with_root_dir(dir.path().to_path_buf()).unwrap();
let context = TemplateContext {
spec: "integration",
rpc_port: "7000",
p2p_port: "8000",
log_to_file: true,
log_to_stdout: true,
};
locator.export_ckb(&context).expect("export config files");
{
let app_config = AppConfig::load_for_subcommand(&locator, cli::CMD_RUN)
.unwrap_or_else(|err| panic!(err));
let ckb_config = app_config.into_ckb().unwrap_or_else(|err| panic!(err));
assert_eq!(
ckb_config.chain.spec,
PathBuf::from("specs/integration.toml")
);
assert_eq!(
ckb_config.network.listen_addresses,
vec!["/ip4/0.0.0.0/tcp/8000".parse().unwrap()]
);
assert_eq!(ckb_config.network.connect_outbound_interval_secs, 1);
assert_eq!(ckb_config.rpc.listen_address, "0.0.0.0:7000");
}
{
locator.export_miner(&context).expect("export config files");
let app_config = AppConfig::load_for_subcommand(&locator, cli::CMD_MINER)
.unwrap_or_else(|err| panic!(err));
let miner_config = app_config.into_miner().unwrap_or_else(|err| panic!(err));
assert_eq!(
miner_config.chain.spec,
PathBuf::from("specs/integration.toml")
);
assert_eq!(miner_config.miner.rpc_url, "http://127.0.0.1:7000/");
}
}
}