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
extern crate serde_json;
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io;
use std::io::prelude::*;
extern crate log;
use dirs::*;
use log::error;
use std::process::exit;
/* use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; */
/// This is the struct that holds the built in , network params that are set by the core devs and the same for everyone
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NetworkConfig {
    pub version_major: u8,
    pub version_breaking: u8,
    pub version_minor: u8,
    pub coin_name: String,
    pub node_drop_off_threshold: u8,
    pub decimal_places: u8,
    pub buffer_bytes: u16,
    pub network_id: Vec<u8>,
    pub min_intrest: f32,
    pub max_intrest: f32,
    pub max_reward: u32,
    pub min_vote: u8,
    pub probatory_epoch_count: u8,
    pub certificateDifficulty: u64,
    pub fullnode_lock_amount: u64,
    pub transactionTimestampMaxOffset: u32,
    pub max_time_to_live: u64,
    pub target_epoch_length: u64,
    pub fullnode_lock_time: u64,
    pub username_burn_amount: u64,
    pub first_block_hash: String,
}
/// This is what is saved in a file, the stuff the user can change and edit to fit their needs
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ConfigSave {
    pub db_path: String,
    pub max_connections: u16,
    pub max_threads: u8,
    pub chain_key: String,
    pub state: u8,
    pub ip_host: String,
    pub seednodes: Vec<String>,
    pub ignore_minor_updates: bool,
    pub p2p_port: u16,
    pub rpc_port: u16,
    pub allow_cors: char,
    pub node_type: char,
    pub identitiy: String,
    pub key_file_path: String,
    pub log_level: u8,
    pub wallet_password: String,
}
/// This is the entire config - this is what is passed arround in software and what you should use in anything your build
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, PartialOrd)]
pub struct Config {
    pub version_major: u8,
    pub version_breaking: u8,
    pub version_minor: u8,
    pub coin_name: String,
    pub db_path: String,
    pub node_drop_off_threshold: u8,
    pub decimal_places: u8,
    pub max_connections: u16,
    pub max_threads: u8,
    pub chain_key: String,
    pub state: u8,
    pub ip_host: String,
    pub seednodes: Vec<String>,
    pub ignore_minor_updates: bool,
    pub p2p_port: u16,
    pub rpc_port: u16,
    pub allow_cors: char,
    pub buffer_bytes: u16,
    pub network_id: Vec<u8>,
    pub node_type: char,
    pub identitiy: String,
    pub key_file_path: String,
    pub log_level: u8,
    pub min_intrest: f32,
    pub max_intrest: f32,
    pub max_reward: u32,
    pub min_vote: u8,
    pub probatory_epoch_count: u8,
    pub certificateDifficulty: u64,
    pub fullnode_lock_amount: u64,
    pub transactionTimestampMaxOffset: u32,
    pub max_time_to_live: u64,
    pub target_epoch_length: u64,
    pub username_burn_amount: u64,
    pub fullnode_lock_time: u64,
    pub first_block_hash: String,
    pub wallet_password: String,

}

pub fn config() -> Config {
    if let Ok(mut file) = File::open("node.conf") {
        let mut data: String = String::from("");
        if let Err(_) = file.read_to_string(&mut data) {
            return Config::default();
        } else {
            let conf: ConfigSave = serde_json::from_str(&data).unwrap_or_default();
            return conf.toConfig();
        }
    } else {
        return Config::default();
    }
}

impl Default for ConfigSave {
    fn default() -> ConfigSave {
        if let Some(dir) = home_dir() {
            if let Some(dir_str) = dir.to_str() {
                return ConfigSave {
                    db_path: dir_str.to_string() + &"/.avrio-datadir".to_string(),
                    max_connections: 50,
                    max_threads: 4,
                    chain_key: "".to_string(),
                    state: 0,
                    ip_host: "127.0.0.1".to_string(),
                    seednodes: vec![
                        "127.0.0.1:12345".to_string(),
                        "127.0.0.1:123456".to_string(),
                    ],
                    ignore_minor_updates: false,
                    p2p_port: 12345,
                    rpc_port: 54321,
                    allow_cors: 'n',
                    node_type: 'n',
                    identitiy: String::from(""),
                    key_file_path: "wallet.keys".to_string(),
                    log_level: 2, // 0,1,2,3,4,5 trace, debug, info, warn, error, fatal respectivly
                    wallet_password: "wallet_password_123".to_string(),
                };
            } else {
                return ConfigSave {
                    db_path: ".avrio-datadir".to_string(),
                    max_connections: 50,
                    max_threads: 4,
                    chain_key: "".to_string(),
                    state: 0,
                    ip_host: "127.0.0.1".to_string(),
                    seednodes: vec![
                        "127.0.0.1:12345".to_string(),
                        "127.0.0.1:123456".to_string(),
                    ],
                    ignore_minor_updates: false,
                    p2p_port: 12345,
                    rpc_port: 54321,
                    allow_cors: 'n',
                    node_type: 'n',
                    identitiy: String::from(""),
                    key_file_path: "wallet.keys".to_string(),
                    log_level: 2, // 0,1,2,3,4,5 trace, debug, info, warn, error, fatal respectivly
                    wallet_password: "wallet_password_123".to_string(),
                };
            }
        } else {
            return ConfigSave {
                db_path: ".avrio-datadir".to_string(),
                max_connections: 50,
                max_threads: 4,
                chain_key: "".to_string(),
                state: 0,
                ip_host: "127.0.0.1".to_string(),
                seednodes: vec![
                    "127.0.0.1:12345".to_string(),
                    "127.0.0.1:123456".to_string(),
                ],
                ignore_minor_updates: false,
                p2p_port: 12345,
                rpc_port: 54321,
                allow_cors: 'n',
                node_type: 'n',
                identitiy: String::from(""),
                key_file_path: "wallet.keys".to_string(),
                log_level: 2, // 0,1,2,3,4,5 trace, debug, info, warn, error, fatal respectivly
                wallet_password: "wallet_password_123".to_string(),
            };
        }
    }
}

impl ConfigSave {
    pub fn toConfig(&self) -> Config {
        let nconf = NetworkConfig::default();
        return Config {
            db_path: self.db_path.to_owned(),
            max_connections: self.max_connections,
            max_threads: self.max_threads,
            chain_key: self.chain_key.to_owned(),
            state: self.state,
            ip_host: self.ip_host.to_owned(),
            seednodes: self.seednodes.to_owned(),
            ignore_minor_updates: self.ignore_minor_updates,
            p2p_port: self.p2p_port,
            rpc_port: self.rpc_port,
            allow_cors: self.allow_cors,
            node_type: self.node_type,
            identitiy: self.identitiy.to_owned(),
            key_file_path: self.key_file_path.to_owned(),
            log_level: self.log_level, // 0,1,2,3,4,5 trace, debug, info, warn, error, fatal respectivly
            version_breaking: nconf.version_breaking,
            version_major: nconf.version_major,
            version_minor: nconf.version_minor,
            coin_name: nconf.coin_name,
            node_drop_off_threshold: nconf.node_drop_off_threshold,
            certificateDifficulty: nconf.certificateDifficulty,
            decimal_places: nconf.decimal_places,
            min_intrest: nconf.min_intrest,
            min_vote: nconf.min_vote,
            max_intrest: nconf.max_intrest,
            buffer_bytes: nconf.buffer_bytes,
            network_id: nconf.network_id,
            max_reward: nconf.max_reward,
            probatory_epoch_count: nconf.probatory_epoch_count,
            fullnode_lock_amount: nconf.fullnode_lock_amount,
            transactionTimestampMaxOffset: nconf.transactionTimestampMaxOffset,
            max_time_to_live: nconf.max_time_to_live,
            target_epoch_length: nconf.target_epoch_length,
            username_burn_amount: nconf.username_burn_amount,
            fullnode_lock_time: nconf.fullnode_lock_time,
            first_block_hash: nconf.first_block_hash,
            wallet_password: self.wallet_password.to_owned(),
        };
    }
}
impl Default for Config {
    fn default() -> Config {
        return ConfigSave::default().toConfig();
    }
}

impl Default for NetworkConfig {
    fn default() -> NetworkConfig {
        // This is where you change the network parameters
        NetworkConfig {
            version_major: 0,
            version_breaking: 1,
            version_minor: 0,
            coin_name: "avrio".to_string(),
            node_drop_off_threshold: 30,
            decimal_places: 4,
            buffer_bytes: 128,
            network_id: vec![
                0x61, 0x76, 0x72, 0x69, 0x6f, 0x20, 0x6e, 0x6f, 0x6f, 0x64, 0x6c, 0x65,
            ],
            min_intrest: 0.5,
            max_intrest: 2.5,
            max_reward: 25000, // 2.5000 AIO
            min_vote: 65,
            probatory_epoch_count: 10,
            certificateDifficulty: 1000, // TODO find this value
            fullnode_lock_amount: 50000,
            transactionTimestampMaxOffset: 600,
            max_time_to_live: 600000,      // millisecconds
            target_epoch_length: 18000000, // 5 Hours
            fullnode_lock_time: 30 * 5,    // epoches (30 days)
            username_burn_amount: 5000,    // 0.5000 AIO
            first_block_hash: "0x...".to_string(),
        }
    }
}

impl Config {
    pub fn toSave(self) -> ConfigSave {
        ConfigSave {
            db_path: self.db_path,
            max_connections: self.max_connections,
            max_threads: self.max_threads,
            chain_key: self.chain_key,
            state: self.state,
            ip_host: self.ip_host,
            seednodes: self.seednodes,
            ignore_minor_updates: self.ignore_minor_updates,
            p2p_port: self.p2p_port,
            rpc_port: self.rpc_port,
            allow_cors: self.allow_cors,
            node_type: self.node_type,
            identitiy: self.identitiy,
            key_file_path: self.key_file_path,
            log_level: self.log_level,
            wallet_password: self.wallet_password,

        }
    }
    /// This creates a config file from the provided struct, if the file exists it does the same thing as save()
    pub fn create(self) -> io::Result<()> {
        // create file
        let mut file = File::create("node.conf")?;
        file.write_all(serde_json::to_string(&self.toSave()).unwrap().as_bytes())?;
        Ok(())
    }
    /// This is how you save the config, it is a expensive function on devices with slow storage as it opens and writes to the file
    pub fn save(self) -> io::Result<()> {
        // save to exisiting/ update
        let mut file = File::open("node.conf")?;
        file.write_all(serde_json::to_string(&self.toSave()).unwrap().as_bytes())?;
        Ok(())
    }
}