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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
use crate::types::{Event, Message, State};
use async_std::net::TcpStream;
use async_std::sync::{Arc, Mutex};
use async_std::task;
use futures::channel::mpsc::{unbounded, UnboundedReceiver, UnboundedSender};
use futures::io::BufReader;
use futures::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt};
use futures::io::{ReadHalf, WriteHalf};
use futures::sink::SinkExt;
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use stratum_types::params::{ClientParam, PoolParam};
use stratum_types::traits::{PoolParams, StratumParams, Subscribe, SubscribeResult};
use stratum_types::Error;
use stratum_types::Result;
use stratum_types::{PoolPacket, Request, StratumError, StratumMethod, ID};

#[derive(Clone, Debug, Deserialize)]
pub struct ClientConfig<PP>
where
    PP: PoolParams,
{
    pub host: String,
    pub credentials: PP::Authorize,
    pub subscriber_info: PP::Subscribe,
}

#[derive(Debug, Clone)]
pub struct StratumClient<PP, SP>
where
    PP: PoolParams,
    SP: StratumParams,
{
    event_rx: Arc<Mutex<UnboundedReceiver<Event>>>,
    event_tx: Arc<Mutex<UnboundedSender<Event>>>,
    message_tx: Arc<Mutex<UnboundedSender<Message<SP>>>>,
    message_rx: Arc<Mutex<UnboundedReceiver<Message<SP>>>>,
    pub write_half: Arc<Mutex<WriteHalf<TcpStream>>>,
    pub read_half: Arc<Mutex<BufReader<ReadHalf<TcpStream>>>>,
    pub config: Arc<ClientConfig<PP>>,
    state: Arc<Mutex<State>>,
    //@todo I think the name of Notify is sloppy. We should just name this "Job", and then have it
    //in the notify packet.
    //@todo maybe don't make this an option since after start it will never be none. Just make sure
    //that we can default it, and then use it.
    current_job: Arc<Mutex<Option<SP::Notify>>>,
    new_work: Arc<Mutex<bool>>,
    difficulty: Arc<Mutex<f64>>,
    subscriber_info: Arc<Mutex<PP::Subscribe>>,
    subscribe_id: Arc<Mutex<String>>,
}

//@todo build sync versions of all these functions as well. Then come clean it up.
impl<PP, SP> StratumClient<PP, SP>
where
    //@todo smells
    PP: PoolParams
        + Serialize
        + for<'de> Deserialize<'de>
        + Sync
        + Send
        + std::fmt::Debug
        + Clone
        + 'static,
    SP: StratumParams
        + Serialize
        + for<'de> Deserialize<'de>
        + Sync
        + Send
        + std::fmt::Debug
        + Clone
        + 'static,
{
    // ===== Constructor Methods ===== //
    pub async fn new(config: ClientConfig<PP>, sid: Option<String>) -> Result<Self> {
        let stream = TcpStream::connect(&config.host).await?;

        let (rh, wh) = stream.split();

        let (tx, rx) = unbounded();
        let (m_tx, m_rx) = unbounded();

        let subscribe_id = sid.unwrap_or("".to_owned());

        Ok(StratumClient {
            config: Arc::new(config.clone()),
            write_half: Arc::new(Mutex::new(wh)),
            read_half: Arc::new(Mutex::new(BufReader::new(rh))),
            state: Arc::new(Mutex::new(State::Connected)),
            event_tx: Arc::new(Mutex::new(tx)),
            event_rx: Arc::new(Mutex::new(rx)),
            message_rx: Arc::new(Mutex::new(m_rx)),
            message_tx: Arc::new(Mutex::new(m_tx)),
            current_job: Arc::new(Mutex::new(None)),
            new_work: Arc::new(Mutex::new(false)),
            difficulty: Arc::new(Mutex::new(0.0)),
            subscriber_info: Arc::new(Mutex::new(config.subscriber_info)),
            subscribe_id: Arc::new(Mutex::new(subscribe_id)),
        })
    }

    pub async fn init_connection(&self) -> Result<()> {
        *self.state.lock().await = State::Connected;
        if !self.authorize().await? {
            //handle bad auth
            return Ok(());
        }

        self.subscribe().await?;

        Ok(())
    }

    //@todo all the messages in here should be DEBUG.
    pub async fn init_server(&self) -> Result<()> {
        self.init_connection().await?;

        // Init the connection.
        // Start the Event loop for reading new messages (TO SEND).
        // Start the Read loop for incoming messages

        let send_self = self.clone();
        // let self_2 = send_self.clone();
        let sender_handle = task::spawn(async move {
            send_self.send_loop().await;
        });

        self.receive_loop().await;
        self.disconnect().await;
        //@todo not sure how we do this the best, but we want to wait for it to finish before
        //finishing the task here.
        //We whould be smart about which one ends first - so think if we want to stop receiving or
        //sending first and then write this accordingly.
        sender_handle.await;

        Ok(())
    }

    // ===== Start Methods ===== //

    //sync version of Start
    pub fn run(&self) -> Result<()> {
        async_std::task::block_on(self.start())
    }

    //@todo allow for multiple hostnames and a backoff function here.
    //So if the connection doesn't work rright away, we can exponentially backoff, and then switch
    //to a backup if even that doesn't work.
    //@todo this means we need to move the connection code into here.
    //Meat and potatoes function here.
    //You would spawn this function inside of a task.
    pub async fn start(&self) -> Result<()> {
        let mut retry_count = 0;
        let timeout = Duration::from_secs(10);

        //Atttempt to reconnect to stratum.
        loop {
            if retry_count > 0 {
                println!(
                    "Connection broken, attempting reconnect. Current attempts: {}",
                    retry_count
                );
            }
            self.init_server().await;

            println!("Connection broken, attempting to reconnect");

            let result = TcpStream::connect(&self.config.host).await;

            if let Ok(stream) = result {
                println!("Connection Reestablished. Continuing...");

                let (rh, wh) = stream.split();
                *self.write_half.lock().await = wh;
                *self.read_half.lock().await = BufReader::new(rh);
                println!("Initializating the server");
                self.init_server().await;
            } else {
                if retry_count > 12 {
                    break;
                }
                async_std::task::sleep(timeout).await;
                retry_count += 1;
            }
        }

        Ok(())
    }

    // ===== Loops ===== //

    //Loops on new messages coming in from the TcpStream.
    async fn receive_loop(&self) -> Result<()> {
        loop {
            if self.check_disconnect().await {
                break;
            }

            let msg = self.next_message().await?;

            match msg {
                PoolPacket::Request(req) => self.handle_requests(req).await?,
                PoolPacket::Response(res) => {
                    if let Some(result) = res.result {
                        self.handle_responses(result).await?;
                    } else if let Some(error) = res.error {
                        self.handle_errors(error).await?;
                    } else {
                        //Packet is null for both result and error. Not sure how to handle
                        //that.
                    }
                }
            };
        }

        Ok(())
    }

    async fn handle_requests(&self, req: Request<PoolParam<PP, SP>>) -> Result<()> {
        match req.params {
            //@todo I think this needs to be in responses.
            PoolParam::SubmitResult(accepted) => self.handle_submit_result(accepted).await?,
            PoolParam::Notify(job) => self.handle_notify(job).await?,
            PoolParam::SetDifficulty(diff) => self.handle_difficulty(diff).await?,
            _ => {
                // warn!("Invalid Message");
                dbg!("Got an invaid msg");
                //throw error
                // break;
            }
        };
        Ok(())
    }

    async fn handle_responses(&self, result: PoolParam<PP, SP>) -> Result<()> {
        //@todo this can be removed, since we now handle it in the authorize function.
        if let PoolParam::AuthorizeResult(auth) = &result {
            self.handle_authorize_return(auth).await?;
            return Ok(());
        }

        match result {
            PoolParam::SubscribeResult(info) => self.handle_subscribe_result(info).await?,
            PoolParam::SubmitResult(_) => println!("Share Accepted!"),
            _ => {
                println!("Recieved unknown packet");
            }
        }

        Ok(())
    }

    async fn handle_errors(&self, error: StratumError) -> Result<()> {
        println!("Error: {}", error.to_string());
        // match error {
        //     StratumError::StaleShare => println!("Stratum - Error: Sent Stale Share"),
        //     _ => println!("Stratum - Error not handled yet"),
        // }

        Ok(())
    }

    //Loops on messages submitted for sending.
    async fn send_loop(&self) -> Result<()> {
        loop {
            if self.check_disconnect().await {
                break;
            }

            while let Some(msg) = self.message_rx.lock().await.next().await {
                match msg {
                    Message::Submit(share) => self.handle_submit_message(share).await?,
                }
            }
        }

        Ok(())
    }

    // ===== Helper Functions ===== //
    async fn check_disconnect(&self) -> bool {
        *self.state.lock().await == State::Disconnect
    }

    async fn disconnect(&self) {
        *self.state.lock().await = State::Disconnect;
    }

    //Parses in the incoming messages.
    pub async fn next_message(&self) -> Result<PoolPacket<PP, SP>> {
        loop {
            let mut stream = self.read_half.lock().await;

            let mut buf = String::new();

            //If this returns Ok(0), need to try to reconnect.
            let amt = stream.read_line(&mut buf).await?;

            if amt == 0 {
                return Err(Error::StreamClosed);
            }

            buf = buf.trim().to_string();

            if !buf.is_empty() {
                let response: PoolPacket<PP, SP> = serde_json::from_str(&buf)?;
                return Ok(response);
            };
        }
    }

    async fn send<T: Serialize>(&self, message: T) -> Result<()> {
        let msg = serde_json::to_vec(&message)?;

        let mut stream = self.write_half.lock().await;

        stream.write_all(&msg).await?;

        stream.write_all(b"\n").await?;

        Ok(())
    }

    //Helper function to make sending requests more simple.
    async fn send_request(&self, method: StratumMethod, params: ClientParam<PP, SP>) -> Result<()> {
        let msg = Request {
            //@todo
            id: ID::Str("".to_owned()),
            method,
            params,
        };

        Ok(self.send(msg).await?)
    }

    // async fn send_response(&self, _method: StratumMethod, _result: PoolParams<SP>) -> Result<()> {
    //     Ok(())
    // }

    pub async fn authorize(&self) -> Result<bool> {
        let authorize = self.config.credentials.clone();

        self.send_request(StratumMethod::Authorize, ClientParam::Authorize(authorize))
            .await?;

        let msg = self.next_message().await?;
        let mut authorize_success = false;

        if let PoolPacket::Response(res) = msg {
            if let Some(PoolParam::AuthorizeResult(auth)) = &res.result {
                self.handle_authorize_return(auth).await?;
                authorize_success = true;
            }
        }

        Ok(authorize_success)
    }

    pub async fn subscribe(&self) -> Result<()> {
        let subscribe = self.subscriber_info.lock().await;

        self.send_request(
            StratumMethod::Subscribe,
            ClientParam::Subscribe(subscribe.clone()),
        )
        .await?;

        Ok(())
    }

    //TODO this needs to accept some params, and also return a value I believe.
    //@todo rename to result
    pub async fn handle_authorize_return(&self, _auth: &PP::AuthorizeResult) -> Result<()> {
        Ok(())
    }

    //Public Submit function
    pub async fn submit(&self, share: SP::Submit) -> Result<()> {
        let msg = Message::Submit(share);

        //@todo remove the unwrap here, just need to impl the error
        self.message_tx.lock().await.send(msg).await?;

        Ok(())
    }

    //This function handles the internal message of submit. This allows for a miner to submit
    //a share, and not have the function block. We then send that share in the event loop above.
    async fn handle_submit_message(&self, share: SP::Submit) -> Result<()> {
        *self.new_work.lock().await = false;

        Ok(self
            .send_request(StratumMethod::Submit, ClientParam::Submit(share))
            .await?)
    }

    async fn handle_subscribe_result(&self, info: PP::SubscribeResult) -> Result<()> {
        //@todo can probably remove this now.
        *self.subscribe_id.lock().await = info.id();
        self.subscriber_info.lock().await.set_sid(&info.id());
        Ok(())
    }

    async fn handle_submit_result(&self, _accepted: bool) -> Result<()> {
        Ok(())
    }

    async fn handle_notify(&self, job: SP::Notify) -> Result<()> {
        *self.current_job.lock().await = Some(job);

        Ok(())
    }

    async fn handle_difficulty(&self, diff: f64) -> Result<()> {
        println!("Updating difficulty to {}", diff);
        *self.difficulty.lock().await = diff;

        Ok(())
    }

    //@todo this only works one time. We could probably use the thread_local crate here, but let's
    //wait on it for now. Probably revamping how we run the miner is best instead of reworking this
    //for now.
    pub async fn is_new_work(&self) -> bool {
        *self.new_work.lock().await
    }

    pub async fn get_difficulty(&self) -> f64 {
        *self.difficulty.lock().await
    }

    pub async fn get_work(&self) -> Option<SP::Notify> {
        let work = self.current_job.lock().await;

        // if self.is_new_work().await {
        //     *self.new_work.lock().await = false;
        // }

        work.clone()
    }

    //Pauses the miner until the initial work has been received
    pub async fn wait_for_work(&self) -> Result<()> {
        loop {
            if self.current_job.lock().await.is_some() {
                break;
            }
            async_std::task::sleep(Duration::from_secs(2)).await;
        }

        Ok(())
    }

    pub async fn get_subscriber_id(&self) -> String {
        self.subscribe_id.lock().await.clone()
    }
}