stratum_server/
config.rs

1use std::{net::IpAddr, sync::Arc, time::Duration};
2
3//@todo wrap this in a Mutex<Arc. Then when a new config is refreshed, everyone can just get it
4//from a clone.
5#[derive(Default, Clone, Debug)]
6pub struct ConfigManager {
7    config: Arc<Config>,
8}
9
10impl ConfigManager {
11    pub(crate) fn new(config: Config) -> Self {
12        Self {
13            config: Arc::new(config),
14        }
15    }
16
17    pub(crate) fn current_config(&self) -> Arc<Config> {
18        self.config.clone()
19    }
20
21    // Helpers
22    pub(crate) fn proxy_protocol(&self) -> bool {
23        self.config.connection.proxy_protocol
24    }
25
26    pub(crate) fn default_ban_duration(&self) -> Duration {
27        self.config.bans.default_ban_duration
28    }
29
30    pub(crate) fn difficulty_config(&self) -> &DifficultyConfig {
31        &self.config.difficulty
32    }
33
34    pub(crate) fn connection_config(&self) -> &ConnectionConfig {
35        &self.config.connection
36    }
37
38    pub(crate) fn ban_manager_enabled(&self) -> bool {
39        self.config.bans.enabled
40    }
41}
42
43#[derive(Clone, Debug, Default)]
44pub struct Config {
45    pub(crate) connection: ConnectionConfig,
46    pub(crate) difficulty: DifficultyConfig,
47    pub(crate) bans: BanManagerConfig,
48}
49
50#[derive(Clone, Debug)]
51pub struct BanManagerConfig {
52    pub(crate) enabled: bool,
53    pub(crate) default_ban_duration: Duration,
54    pub(crate) _ban_score_allowed: u64,
55    pub(crate) _whitelisted_ips: Vec<IpAddr>,
56    pub(crate) _perma_ban_starting_list: Vec<IpAddr>,
57}
58
59impl Default for BanManagerConfig {
60    fn default() -> Self {
61        BanManagerConfig {
62            enabled: false,
63            default_ban_duration: Duration::from_secs(3600),
64            _ban_score_allowed: 100,
65            _whitelisted_ips: Vec::new(),
66            _perma_ban_starting_list: Vec::new(),
67        }
68    }
69}
70
71#[derive(Clone, Debug)]
72pub struct ConnectionConfig {
73    pub(crate) proxy_protocol: bool,
74    pub(crate) max_connections: Option<usize>,
75    /// Active Timeout is how long with no activity before we disconnect a miner.
76    pub(crate) active_timeout: u64,
77    /// Initial Timeout is how long we wait for an initial message from a miner. Once they are
78    /// active, we switch to the active timeout setting.
79    pub(crate) inital_timeout: u64,
80    //@todo maybe move this to a new struct called MinerConfig, but for now I think it's ok.
81    /// Check Threshold is how many shares until we consider a ban on a miner
82    pub(crate) check_threshold: u64,
83    /// Invalid Percent is the percent of shares that are rejected or stale before we ban a miner.
84    /// In full-interval format e.g. 50.0 = 50%.
85    pub(crate) invalid_percent: f64,
86}
87
88impl Default for ConnectionConfig {
89    fn default() -> Self {
90        ConnectionConfig {
91            proxy_protocol: false,
92            max_connections: None,
93            active_timeout: 600,
94            inital_timeout: 15,
95            check_threshold: 500,
96            invalid_percent: 50.0,
97        }
98    }
99}
100
101#[derive(Clone, Debug)]
102pub struct DifficultyConfig {
103    pub(crate) retarget_share_amount: u64,
104    pub(crate) initial_difficulty: u64,
105    pub(crate) var_diff: bool,
106    pub(crate) minimum_difficulty: u64,
107    pub(crate) maximum_difficulty: u64,
108    //Seconds
109    pub(crate) retarget_time: u64,
110    //Seconds
111    pub(crate) target_time: u64,
112    //@todo see if we use this.
113    pub(crate) variance_percent: f64,
114}
115
116impl Default for DifficultyConfig {
117    fn default() -> Self {
118        DifficultyConfig {
119            retarget_share_amount: 30,
120            initial_difficulty: 16384,
121            var_diff: false,
122            minimum_difficulty: 64,
123            maximum_difficulty: 4_611_686_018_427_387_904,
124            retarget_time: 300,
125            target_time: 10,
126            variance_percent: 30.0,
127        }
128    }
129}
130
131impl DifficultyConfig {
132    pub(crate) fn initial_retarget_time(&self, now: u128) -> u128 {
133        now - ((self.retarget_time as u128 * 1000) / 2)
134    }
135}
136
137// #[cfg(feature = "upstream")]
138// #[derive(Clone, Debug, Default)]
139// pub struct UpstreamConfig {
140//     pub(crate) enabled: bool,
141//     pub(crate) url: String,
142// }