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
pub mod consensus;
pub mod gossip;
use std::{
fs::{self, File},
io::{self, Error, ErrorKind, Write},
path::Path,
};
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Config {
#[serde(flatten)]
pub gossip_sender_config: gossip::SenderConfig,
#[serde(default)]
pub validator_only: bool,
pub consensus_parameters: consensus::Parameters,
#[serde(default)]
pub proposer_min_block_delay: u64,
}
impl Default for Config {
fn default() -> Self {
Self::default()
}
}
impl Config {
pub fn default() -> Self {
Self {
gossip_sender_config: gossip::SenderConfig::default(),
validator_only: false,
consensus_parameters: consensus::Parameters::default(),
proposer_min_block_delay: 1000 * 1000 * 1000, }
}
pub fn encode_json(&self) -> io::Result<String> {
serde_json::to_string(&self)
.map_err(|e| Error::new(ErrorKind::Other, format!("failed to serialize JSON {}", e)))
}
pub fn sync(&self, file_path: &str) -> io::Result<()> {
log::info!("syncing subnet config to '{}'", file_path);
let path = Path::new(file_path);
let parent_dir = path.parent().expect("unexpected None parent");
fs::create_dir_all(parent_dir)?;
let d = serde_json::to_vec(self)
.map_err(|e| Error::new(ErrorKind::Other, format!("failed to serialize JSON {}", e)))?;
let mut f = File::create(file_path)?;
f.write_all(&d)?;
Ok(())
}
}
#[test]
fn test_config() {
let _ = env_logger::builder().is_test(true).try_init();
let tmp_path = random_manager::tmp_path(10, Some(".json")).unwrap();
let cfg = Config::default();
cfg.sync(&tmp_path).unwrap();
}