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
use path_clean::PathClean;
use std::fs;
use std::path::{Path, PathBuf};
use serde::{Deserialize, Serialize};
use ckb_chain_spec::ChainSpec;
pub use ckb_logger_config::Config as LogConfig;
pub use ckb_metrics_config::Config as MetricsConfig;
use ckb_resource::Resource;
use super::configs::*;
#[cfg(feature = "with_sentry")]
use super::sentry_config::SentryConfig;
use super::{cli, legacy, ExitCode};
pub enum AppConfig {
CKB(Box<CKBAppConfig>),
Miner(Box<MinerAppConfig>),
}
#[derive(Clone, Debug, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CKBAppConfig {
#[serde(skip)]
pub bin_name: String,
#[serde(skip)]
pub root_dir: PathBuf,
pub data_dir: PathBuf,
#[serde(default)]
pub ancient: PathBuf,
pub tmp_dir: Option<PathBuf>,
pub logger: LogConfig,
#[cfg(feature = "with_sentry")]
#[serde(default)]
pub sentry: SentryConfig,
#[serde(default)]
pub metrics: MetricsConfig,
#[serde(default)]
pub memory_tracker: MemoryTrackerConfig,
pub chain: ChainConfig,
pub block_assembler: Option<BlockAssemblerConfig>,
#[serde(default)]
pub db: DBConfig,
pub network: NetworkConfig,
pub rpc: RpcConfig,
pub tx_pool: TxPoolConfig,
#[serde(default)]
pub store: StoreConfig,
pub alert_signature: Option<NetworkAlertConfig>,
#[serde(default)]
pub notify: NotifyConfig,
}
#[derive(Clone, Debug, Serialize)]
#[serde(deny_unknown_fields)]
pub struct MinerAppConfig {
#[serde(skip)]
pub bin_name: String,
#[serde(skip)]
pub root_dir: PathBuf,
pub data_dir: PathBuf,
pub chain: ChainConfig,
pub logger: LogConfig,
#[cfg(feature = "with_sentry")]
pub sentry: SentryConfig,
#[serde(default)]
pub metrics: MetricsConfig,
#[serde(default)]
pub memory_tracker: MemoryTrackerConfig,
pub miner: MinerConfig,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ChainConfig {
pub spec: Resource,
}
impl AppConfig {
pub fn load_for_subcommand<P: AsRef<Path>>(
root_dir: P,
subcommand_name: &str,
) -> Result<AppConfig, ExitCode> {
match subcommand_name {
cli::CMD_MINER => {
let resource = ensure_ckb_dir(Resource::miner_config(root_dir.as_ref()))?;
let config = MinerAppConfig::load_from_slice(&resource.get()?)?;
Ok(AppConfig::with_miner(
config.derive_options(root_dir.as_ref())?,
))
}
_ => {
let resource = ensure_ckb_dir(Resource::ckb_config(root_dir.as_ref()))?;
let config = CKBAppConfig::load_from_slice(&resource.get()?)?;
Ok(AppConfig::with_ckb(
config.derive_options(root_dir.as_ref(), subcommand_name)?,
))
}
}
}
pub fn logger(&self) -> &LogConfig {
match self {
AppConfig::CKB(config) => &config.logger,
AppConfig::Miner(config) => &config.logger,
}
}
#[cfg(feature = "with_sentry")]
pub fn sentry(&self) -> &SentryConfig {
match self {
AppConfig::CKB(config) => &config.sentry,
AppConfig::Miner(config) => &config.sentry,
}
}
pub fn metrics(&self) -> &MetricsConfig {
match self {
AppConfig::CKB(config) => &config.metrics,
AppConfig::Miner(config) => &config.metrics,
}
}
pub fn memory_tracker(&self) -> &MemoryTrackerConfig {
match self {
AppConfig::CKB(config) => &config.memory_tracker,
AppConfig::Miner(config) => &config.memory_tracker,
}
}
pub fn chain_spec(&self) -> Result<ChainSpec, ExitCode> {
let spec_resource = match self {
AppConfig::CKB(config) => &config.chain.spec,
AppConfig::Miner(config) => &config.chain.spec,
};
ChainSpec::load_from(spec_resource).map_err(|err| {
eprintln!("{}", err);
ExitCode::Config
})
}
pub fn into_ckb(self) -> Result<Box<CKBAppConfig>, ExitCode> {
match self {
AppConfig::CKB(config) => Ok(config),
_ => {
eprintln!("unmatched config file");
Err(ExitCode::Failure)
}
}
}
pub fn into_miner(self) -> Result<Box<MinerAppConfig>, ExitCode> {
match self {
AppConfig::Miner(config) => Ok(config),
_ => {
eprintln!("unmatched config file");
Err(ExitCode::Failure)
}
}
}
pub fn set_bin_name(&mut self, bin_name: String) {
match self {
AppConfig::CKB(config) => config.bin_name = bin_name,
AppConfig::Miner(config) => config.bin_name = bin_name,
}
}
}
impl AppConfig {
fn with_ckb(config: CKBAppConfig) -> AppConfig {
AppConfig::CKB(Box::new(config))
}
fn with_miner(config: MinerAppConfig) -> AppConfig {
AppConfig::Miner(Box::new(config))
}
}
impl CKBAppConfig {
pub fn load_from_slice(slice: &[u8]) -> Result<Self, ExitCode> {
let legacy_config: legacy::CKBAppConfig = toml::from_slice(&slice)?;
for field in legacy_config.deprecated_fields() {
eprintln!(
"WARN: the option \"{}\" in configuration files is deprecated since v{}.",
field.path, field.since
);
}
Ok(legacy_config.into())
}
fn derive_options(mut self, root_dir: &Path, subcommand_name: &str) -> Result<Self, ExitCode> {
self.root_dir = root_dir.to_path_buf();
self.data_dir = canonicalize_data_dir(self.data_dir, root_dir);
self.db.adjust(root_dir, &self.data_dir, "db");
self.ancient = mkdir(path_specified_or_else(&self.ancient, || {
self.data_dir.join("ancient")
}))?;
self.network.path = self.data_dir.join("network");
if self.tmp_dir.is_none() {
self.tmp_dir = Some(self.data_dir.join("tmp"));
}
self.logger.log_dir = self.data_dir.join("logs");
self.logger.file = Path::new(&(subcommand_name.to_string() + ".log")).to_path_buf();
let tx_pool_path = mkdir(self.data_dir.join("tx_pool"))?;
self.tx_pool.adjust(root_dir, tx_pool_path);
if subcommand_name == cli::CMD_RESET_DATA {
return Ok(self);
}
self.data_dir = mkdir(self.data_dir)?;
self.db.path = mkdir(self.db.path)?;
self.network.path = mkdir(self.network.path)?;
if let Some(tmp_dir) = self.tmp_dir {
self.tmp_dir = Some(mkdir(tmp_dir)?);
}
if self.logger.log_to_file {
mkdir(self.logger.log_dir.clone())?;
touch(self.logger.log_dir.join(&self.logger.file))?;
}
self.chain.spec.absolutize(root_dir);
Ok(self)
}
}
impl MinerAppConfig {
pub fn load_from_slice(slice: &[u8]) -> Result<Self, ExitCode> {
let legacy_config: legacy::MinerAppConfig = toml::from_slice(&slice)?;
for field in legacy_config.deprecated_fields() {
eprintln!(
"WARN: the option \"{}\" in configuration files is deprecated since v{}.",
field.path, field.since
);
}
Ok(legacy_config.into())
}
fn derive_options(mut self, root_dir: &Path) -> Result<Self, ExitCode> {
self.root_dir = root_dir.to_path_buf();
self.data_dir = mkdir(canonicalize_data_dir(self.data_dir, root_dir))?;
self.logger.log_dir = self.data_dir.join("logs");
self.logger.file = Path::new("miner.log").to_path_buf();
if self.logger.log_to_file {
mkdir(self.logger.log_dir.clone())?;
touch(self.logger.log_dir.join(&self.logger.file))?;
}
self.chain.spec.absolutize(root_dir);
Ok(self)
}
}
fn canonicalize_data_dir(data_dir: PathBuf, root_dir: &Path) -> PathBuf {
if data_dir.is_absolute() {
data_dir
} else {
root_dir.join(data_dir)
}
}
fn mkdir(dir: PathBuf) -> Result<PathBuf, ExitCode> {
fs::create_dir_all(&dir.clean())?;
Ok(dir)
}
fn touch(path: PathBuf) -> Result<PathBuf, ExitCode> {
fs::OpenOptions::new()
.create(true)
.append(true)
.open(&path)?;
Ok(path)
}
fn ensure_ckb_dir(r: Resource) -> Result<Resource, ExitCode> {
if r.exists() {
Ok(r)
} else {
eprintln!("Not a CKB directory, initialize one with `ckb init`.");
Err(ExitCode::Config)
}
}
fn path_specified_or_else<P: AsRef<Path>, F: FnOnce() -> PathBuf>(
path: P,
default_path: F,
) -> PathBuf {
let path_ref = path.as_ref();
if path_ref.to_str().is_none() || path_ref.to_str() == Some("") {
default_path()
} else {
path_ref.to_path_buf()
}
}