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

#[derive(Default)]
pub struct StratumServerBuilder<State> {
    pub host: String,
    pub port: u16,
    pub max_connections: Option<usize>,
    pub proxy: bool,
    pub var_diff: bool,
    pub initial_difficulty: f64,
    pub state: State,
    pub minimum_difficulty: f64,
    pub maximum_difficulty: f64,
    //Seconds
    pub retarget_time: i64,
    //Seconds
    pub target_time: f64,
    pub variance_percent: f64,
}

impl<State: Clone + Send + Sync + 'static> StratumServerBuilder<State> {
    pub fn new(state: State) -> Self {
        Self {
            host: String::from(""),
            port: 0,
            max_connections: None,
            proxy: false,
            var_diff: false,
            initial_difficulty: 0.0,
            state,
            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) -> Self {
        self.proxy = true;
        self
    }

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

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

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

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

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

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

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

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

        StratumServer {
            host: self.host,
            port: self.port,
            max_connections: self.max_connections,
            proxy: self.proxy,
            var_diff: self.var_diff,
            initial_difficulty: self.initial_difficulty,
            connection_list,
            state: self.state,
            ban_manager: Arc::new(BanManager::new()),
            router: Arc::new(Router::new()),
            minimum_difficulty: self.minimum_difficulty,
            maximum_difficulty: self.maximum_difficulty,
            retarget_time: self.retarget_time,
            target_time: self.target_time,
            variance_percent: self.variance_percent,
        }
    }
}