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
use crate::router::Router;
use crate::server::VarDiffConfig;
use crate::BanManager;
use crate::MinerList;
use crate::StratumServer;
use async_std::sync::Arc;

#[derive(Default)]
pub struct StratumServerBuilder<State, CState> {
    pub host: String,
    pub port: u16,
    pub exported_port: Option<u16>,
    pub max_connections: Option<usize>,
    pub proxy: bool,
    pub var_diff_config: VarDiffConfig,
    pub initial_difficulty: f64,
    pub state: State,
    pub connection_state: CState,
}

impl<State: Clone + Send + Sync + 'static, CState: Clone + Send + Sync + 'static>
    StratumServerBuilder<State, CState>
{
    pub fn new(state: State, connection_state: CState) -> Self {
        Self {
            host: String::from(""),
            port: 0,
            exported_port: None,
            max_connections: None,
            proxy: false,
            initial_difficulty: 0.0,
            state,
            connection_state,
            var_diff_config: VarDiffConfig {
                var_diff: false,
                minimum_difficulty: 1.0,
                maximum_difficulty: 100_000.0,
                retarget_time: 90,
                target_time: 15.0,
                variance_percent: 30.0,
            },
        }
    }

    pub fn with_host(mut self, host: &str) -> Self {
        self.host = host.to_owned();
        self
    }

    pub fn with_port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }

    pub fn with_max_connections(mut self, max_connections: usize) -> Self {
        self.max_connections = Some(max_connections);
        self
    }

    pub fn with_proxy(mut self, value: bool) -> Self {
        self.proxy = value;
        self
    }

    pub fn with_var_diff(mut self, value: bool) -> Self {
        self.var_diff_config.var_diff = value;
        self
    }

    pub fn with_minimum_difficulty(mut self, difficulty: f64) -> Self {
        self.var_diff_config.minimum_difficulty = difficulty;
        self
    }

    pub fn with_maximum_difficulty(mut self, difficulty: f64) -> Self {
        self.var_diff_config.maximum_difficulty = difficulty;
        self
    }

    pub fn with_retarget_time(mut self, time: i64) -> Self {
        self.var_diff_config.retarget_time = time;
        self
    }

    pub fn with_target_time(mut self, time: f64) -> Self {
        self.var_diff_config.target_time = time;
        self
    }

    pub fn with_variance_percent(mut self, percent: f64) -> Self {
        self.var_diff_config.variance_percent = percent;
        self
    }

    pub fn with_initial_difficulty(mut self, difficulty: f64) -> Self {
        self.initial_difficulty = difficulty;
        self
    }

    pub fn with_expected_port(mut self, port: u16) -> Self {
        self.exported_port = Some(port);
        self
    }

    pub fn build(self) -> StratumServer<State, CState> {
        let connection_list = Arc::new(MinerList::new());

        let expected_port = match self.exported_port {
            Some(exported_port) => exported_port,
            None => self.port,
        };

        StratumServer {
            host: self.host,
            port: self.port,
            expected_port,
            max_connections: self.max_connections,
            proxy: self.proxy,
            initial_difficulty: self.initial_difficulty,
            connection_list,
            state: self.state,
            ban_manager: Arc::new(BanManager::new()),
            router: Arc::new(Router::new()),
            var_diff_config: self.var_diff_config,
            initial_connection_state: self.connection_state,
        }
    }
}