aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
blob: 0584c3193244f427c15e823a71a79df615172513 (plain)
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
//! # Configuration
//!
//! This module holds the data structures for network configuration.
use crate::block::Fingerprint;
use log::{error, info};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;


/// Configuration struct for a single bot
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct BotConfig {
    /// The initial balance of this bot.
    pub starting_balance: u16,
}


/// Configuration for a single network
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct Config {
    /// Name of the network
    pub name: String,

    /// URL prefix for this network, can be empty
    ///
    /// For example, if url_prefix is `example`, register at
    /// `gradecoin.xyz/example/register`
    pub url_prefix: String,

    /// CSV file that contains the list of users who can register
    ///
    /// Format of CSV file:
    /// ```
    /// User ID, Password
    /// e123456,register_password
    /// e123456,register_password
    /// ```
    /// First line is ignored.
    pub preapproved_users: String,

    /// Valid blocks should have this many transactions
    pub block_transaction_count: u8,

    /// How many zero hexadecimal characters should a correct hash start with?
    pub hash_zeros: u8,

    /// Inital registration bonus
    pub register_bonus: u16,

    /// Coinbase reward
    pub block_reward: u16,

    /// Transaction amount upper limit
    pub tx_upper_limit: u16,

    /// Transaction amount lower limit
    pub tx_lower_limit: u16,

    /// Transaction traffic reward
    pub tx_traffic_reward: u16,

    /// The configuration of the bots in this network.
    /// Maps bot fingerprints to their configurations.
    pub bots: HashMap<Fingerprint, BotConfig>,
}

impl Config {
    /// Read the configuration from a given `.yaml` file.
    pub fn read(filename: &str) -> Option<Self> {
        let file = match std::fs::File::open(filename) {
            Ok(f) => f,
            Err(e) => {
                error!("Cannot read config file: {}", filename);
                error!("Error: {:?}", e);
                return None;
            }
        };
        let config: Config = match serde_yaml::from_reader(file) {
            Ok(c) => c,
            Err(e) => {
                error!("Cannot parse config file: {}", filename);
                error!("Error: {:?}", e);
                return None;
            }
        };
        // File closes automatically when it goes out of scope.
        info!("Config file read successfully: {}", filename);
        Some(config)
    }
}