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
use crate::{
    config::{ConnectionConfig, DifficultyConfig},
    id_manager::IDManager,
    router::Router,
    types::ReadyIndicator,
    BanManager, Config, ConfigManager, Result, SessionList, StratumServer,
};
use extended_primitives::Buffer;
use std::{marker::PhantomData, sync::Arc};
use tokio::{net::TcpListener, task::JoinSet};
use tokio_stream::wrappers::TcpListenerStream;
use tokio_util::sync::CancellationToken;

#[derive(Default)]
pub struct StratumServerBuilder<State, CState> {
    pub server_id: u8,
    pub host: String,
    pub port: u16,
    #[cfg(feature = "api")]
    pub api_host: String,
    #[cfg(feature = "api")]
    pub api_port: u16,
    pub connection_config: ConnectionConfig,
    pub var_diff_config: DifficultyConfig,
    pub state: State,
    pub connection_state: PhantomData<CState>,
    pub ready_indicator: ReadyIndicator,
    pub shutdown_message: Option<Buffer>,
    pub cancel_token: Option<CancellationToken>,
}

impl<State: Clone + Send + Sync + 'static, CState: Default + Clone + Send + Sync + 'static>
    StratumServerBuilder<State, CState>
{
    pub fn new(state: State, server_id: u8) -> Self {
        Self {
            server_id,
            host: String::new(),
            port: 0,
            #[cfg(feature = "api")]
            api_host: String::from("0.0.0.0"),
            #[cfg(feature = "api")]
            api_port: 8888,
            connection_config: ConnectionConfig::default(),
            state,
            connection_state: PhantomData,
            ready_indicator: ReadyIndicator::new(false),
            var_diff_config: DifficultyConfig {
                retarget_share_amount: 30,
                initial_difficulty: 16384,
                var_diff: false,
                minimum_difficulty: 64,
                maximum_difficulty: 4_611_686_018_427_387_904,
                retarget_time: 300,
                target_time: 10,
                variance_percent: 30.0,
            },
            // #[cfg(feature = "upstream")]
            // upstream_config: UpstreamConfig {
            //     enabled: false,
            //     url: String::from(""),
            // },
            shutdown_message: None,
            cancel_token: None,
        }
    }

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

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

    #[cfg(feature = "api")]
    #[must_use]
    pub fn with_api_host(mut self, host: &str) -> Self {
        self.api_host = host.to_owned();
        self
    }

    #[cfg(feature = "api")]
    #[must_use]
    pub fn with_api_port(mut self, port: u16) -> Self {
        self.api_port = port;
        self
    }

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

    #[must_use]
    pub fn with_proxy(mut self, value: bool) -> Self {
        self.connection_config.proxy_protocol = value;
        self
    }

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

    #[must_use]
    pub fn with_minimum_difficulty(mut self, difficulty: u64) -> Self {
        self.var_diff_config.minimum_difficulty = difficulty;
        self
    }

    #[must_use]
    pub fn with_maximum_difficulty(mut self, difficulty: u64) -> Self {
        self.var_diff_config.maximum_difficulty = difficulty;
        self
    }

    #[must_use]
    pub fn with_retarget_time(mut self, time: u64) -> Self {
        self.var_diff_config.retarget_time = time;
        self
    }

    #[must_use]
    pub fn with_target_time(mut self, time: u64) -> Self {
        self.var_diff_config.target_time = time;
        self
    }

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

    #[must_use]
    pub fn with_initial_difficulty(mut self, difficulty: u64) -> Self {
        self.var_diff_config.initial_difficulty = difficulty;
        self
    }

    #[must_use]
    pub fn with_ready_indicator(mut self, ready_indicator: ReadyIndicator) -> Self {
        self.ready_indicator = ready_indicator;
        self
    }

    #[must_use]
    pub fn with_shutdown_message(mut self, msg: Buffer) -> Self {
        self.shutdown_message = Some(msg);
        self
    }

    #[must_use]
    pub fn with_cancel_token(mut self, token: CancellationToken) -> Self {
        self.cancel_token = Some(token);
        self
    }

    pub async fn build(self) -> Result<StratumServer<State, CState>> {
        let config = Config {
            connection: self.connection_config,
            difficulty: self.var_diff_config,
            bans: crate::config::BanManagerConfig::default(),
        };

        let config_manager = ConfigManager::new(config);

        let listener = TcpListener::bind(format!("{}:{}", self.host, self.port)).await?;
        //This will fail if unable to find a local port.
        let listen_address = listener.local_addr()?;
        let listener = TcpListenerStream::new(listener);
        let session_list = SessionList::new(config_manager.clone());

        let cancel_token = if let Some(cancel_token) = self.cancel_token {
            cancel_token
        } else {
            CancellationToken::new()
        };

        let ban_manager = BanManager::new(config_manager.clone(), cancel_token.child_token());

        #[cfg(feature = "api")]
        let api = {
            let state = crate::api::Context {
                ban_manager: ban_manager.clone(),
                ready_indicator: self.ready_indicator.create_new(),
            };

            let api_address = format!("{}:{}", self.api_host, self.api_port).parse()?;

            crate::api::Api::build(api_address, state).await?
        };

        Ok(StratumServer {
            id: self.server_id,
            listener,
            listen_address,
            session_list,
            config_manager,
            state: self.state,
            ban_manager,
            router: Arc::new(Router::new()),
            session_id_manager: IDManager::new(self.server_id),
            cancel_token,
            global_thread_list: JoinSet::new(),
            ready_indicator: self.ready_indicator,
            shutdown_message: self.shutdown_message,
            #[cfg(feature = "api")]
            api,
        })
    }
}