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
use dirs;
use rand::distributions::{Alphanumeric, Distribution};
use rand::thread_rng;
use std::env;
use std::fs::{self, File};
use std::io::prelude::*;
use std::io::BufReader;
use std::io::Read;
use std::path::PathBuf;
use toml;
use crate::comments::insert_comments;
use crate::core::global;
use crate::p2p;
use crate::servers::ServerConfig;
use crate::types::{ConfigError, ConfigMembers, GlobalConfig};
use crate::util::LoggingConfig;
pub const SERVER_CONFIG_FILE_NAME: &'static str = "grin-server.toml";
const SERVER_LOG_FILE_NAME: &'static str = "grin-server.log";
const GRIN_HOME: &'static str = ".grin";
const GRIN_CHAIN_DIR: &'static str = "chain_data";
pub const API_SECRET_FILE_NAME: &'static str = ".api_secret";
fn get_grin_path(chain_type: &global::ChainTypes) -> Result<PathBuf, ConfigError> {
let mut grin_path = match dirs::home_dir() {
Some(p) => p,
None => PathBuf::new(),
};
grin_path.push(GRIN_HOME);
grin_path.push(chain_type.shortname());
if !grin_path.exists() {
fs::create_dir_all(grin_path.clone())?;
}
Ok(grin_path)
}
fn check_config_current_dir(path: &str) -> Option<PathBuf> {
let p = env::current_dir();
let mut c = match p {
Ok(c) => c,
Err(_) => {
return None;
}
};
c.push(path);
if c.exists() {
return Some(c);
}
None
}
pub fn init_api_secret(api_secret_path: &PathBuf) -> Result<(), ConfigError> {
let mut api_secret_file = File::create(api_secret_path)?;
let api_secret: String = Alphanumeric
.sample_iter(&mut thread_rng())
.take(20)
.collect();
api_secret_file.write_all(api_secret.as_bytes())?;
Ok(())
}
pub fn check_api_secret(api_secret_path: &PathBuf) -> Result<(), ConfigError> {
let api_secret_file = File::open(api_secret_path)?;
let buf_reader = BufReader::new(api_secret_file);
let mut lines_iter = buf_reader.lines();
let first_line = lines_iter.next();
if first_line.is_none() || first_line.unwrap().is_err() {
fs::remove_file(api_secret_path)?;
init_api_secret(api_secret_path)?;
}
Ok(())
}
fn check_api_secret_file(chain_type: &global::ChainTypes) -> Result<(), ConfigError> {
let grin_path = get_grin_path(chain_type)?;
let mut api_secret_path = grin_path.clone();
api_secret_path.push(API_SECRET_FILE_NAME);
if !api_secret_path.exists() {
init_api_secret(&api_secret_path)
} else {
check_api_secret(&api_secret_path)
}
}
pub fn initial_setup_server(chain_type: &global::ChainTypes) -> Result<GlobalConfig, ConfigError> {
check_api_secret_file(chain_type)?;
if let Some(p) = check_config_current_dir(SERVER_CONFIG_FILE_NAME) {
GlobalConfig::new(p.to_str().unwrap())
} else {
let grin_path = get_grin_path(chain_type)?;
let mut config_path = grin_path.clone();
config_path.push(SERVER_CONFIG_FILE_NAME);
if !config_path.exists() {
let mut default_config = GlobalConfig::for_chain(chain_type);
default_config.update_paths(&grin_path);
default_config.write_to_file(config_path.to_str().unwrap())?;
}
GlobalConfig::new(config_path.to_str().unwrap())
}
}
impl Default for ConfigMembers {
fn default() -> ConfigMembers {
ConfigMembers {
server: ServerConfig::default(),
logging: Some(LoggingConfig::default()),
}
}
}
impl Default for GlobalConfig {
fn default() -> GlobalConfig {
GlobalConfig {
config_file_path: None,
members: Some(ConfigMembers::default()),
}
}
}
impl GlobalConfig {
pub fn for_chain(chain_type: &global::ChainTypes) -> GlobalConfig {
let mut defaults_conf = GlobalConfig::default();
let mut defaults = &mut defaults_conf.members.as_mut().unwrap().server;
defaults.chain_type = chain_type.clone();
match *chain_type {
global::ChainTypes::Mainnet => {}
global::ChainTypes::Floonet => {
defaults.api_http_addr = "127.0.0.1:13413".to_owned();
defaults.p2p_config.port = 13414;
defaults
.stratum_mining_config
.as_mut()
.unwrap()
.stratum_server_addr = Some("127.0.0.1:13416".to_owned());
defaults
.stratum_mining_config
.as_mut()
.unwrap()
.wallet_listener_url = "http://127.0.0.1:13415".to_owned();
}
global::ChainTypes::UserTesting => {
defaults.api_http_addr = "127.0.0.1:23413".to_owned();
defaults.p2p_config.port = 23414;
defaults.p2p_config.seeding_type = p2p::Seeding::None;
defaults
.stratum_mining_config
.as_mut()
.unwrap()
.stratum_server_addr = Some("127.0.0.1:23416".to_owned());
defaults
.stratum_mining_config
.as_mut()
.unwrap()
.wallet_listener_url = "http://127.0.0.1:23415".to_owned();
}
global::ChainTypes::AutomatedTesting => {
panic!("Can't run automated testing directly");
}
}
defaults_conf
}
pub fn new(file_path: &str) -> Result<GlobalConfig, ConfigError> {
let mut return_value = GlobalConfig::default();
return_value.config_file_path = Some(PathBuf::from(&file_path));
let config_file = return_value.config_file_path.clone().unwrap();
if !config_file.exists() {
return Err(ConfigError::FileNotFoundError(String::from(
config_file.to_str().unwrap(),
)));
}
return_value.read_config()
}
fn read_config(mut self) -> Result<GlobalConfig, ConfigError> {
let mut file = File::open(self.config_file_path.as_mut().unwrap())?;
let mut contents = String::new();
file.read_to_string(&mut contents)?;
let decoded: Result<ConfigMembers, toml::de::Error> = toml::from_str(&contents);
match decoded {
Ok(gc) => {
self.members = Some(gc);
return Ok(self);
}
Err(e) => {
return Err(ConfigError::ParseError(
String::from(
self.config_file_path
.as_mut()
.unwrap()
.to_str()
.unwrap()
.clone(),
),
String::from(format!("{}", e)),
));
}
}
}
pub fn update_paths(&mut self, grin_home: &PathBuf) {
let mut chain_path = grin_home.clone();
chain_path.push(GRIN_CHAIN_DIR);
self.members.as_mut().unwrap().server.db_root = chain_path.to_str().unwrap().to_owned();
let mut secret_path = grin_home.clone();
secret_path.push(API_SECRET_FILE_NAME);
self.members.as_mut().unwrap().server.api_secret_path =
Some(secret_path.to_str().unwrap().to_owned());
let mut log_path = grin_home.clone();
log_path.push(SERVER_LOG_FILE_NAME);
self.members
.as_mut()
.unwrap()
.logging
.as_mut()
.unwrap()
.log_file_path = log_path.to_str().unwrap().to_owned();
}
pub fn stratum_enabled(&mut self) -> bool {
return self
.members
.as_mut()
.unwrap()
.server
.stratum_mining_config
.as_mut()
.unwrap()
.enable_stratum_server
.unwrap();
}
pub fn ser_config(&mut self) -> Result<String, ConfigError> {
let encoded: Result<String, toml::ser::Error> =
toml::to_string(self.members.as_mut().unwrap());
match encoded {
Ok(enc) => return Ok(enc),
Err(e) => {
return Err(ConfigError::SerializationError(String::from(format!(
"{}",
e
))));
}
}
}
pub fn write_to_file(&mut self, name: &str) -> Result<(), ConfigError> {
let conf_out = self.ser_config()?;
let conf_out = insert_comments(conf_out);
let mut file = File::create(name)?;
file.write_all(conf_out.as_bytes())?;
Ok(())
}
}