stratum_types/
miner.rs

1use crate::stratum_error::StratumError;
2use std::net::IpAddr;
3use uuid::Uuid;
4
5//@todo review this. I'm not sure if this is the best strategy here, but I'd like for the ability
6//to pass some information to the implementer of traits. I.E. For logging in, the implementer
7//probably wants the IP of the connection that is attempting to log in, so that they can rate limit
8//or ban. Further, for share submitting and other features, the implementer probably wants the same
9//ability. Rather than pass the entire stream into the trait functions, I figured we should build a
10//struct that holds some information about the miner that can be edited and passed into those
11//functions.
12#[derive(Clone, Debug)]
13pub struct MinerInfo {
14    pub ip: IpAddr,
15    pub auth: Option<MinerAuth>,
16    pub id: Option<Uuid>,
17    pub sid: Option<String>,
18    pub job_stats: Option<MinerJobStats>,
19    pub worker_name: Option<String>,
20}
21
22#[derive(Clone, Debug)]
23pub struct MinerAuth {
24    pub id: String,
25    pub username: String,
26    pub client: String,
27}
28
29#[derive(Clone, Debug)]
30pub struct MinerJobStats {
31    pub expected_difficulty: f64,
32}
33
34impl MinerInfo {
35    pub fn get_auth(&self) -> std::result::Result<MinerAuth, StratumError> {
36        match &self.auth {
37            Some(auth) => Ok(auth.clone()),
38            None => Err(StratumError::Unauthorized),
39        }
40    }
41
42    pub fn get_id(&self) -> std::result::Result<Uuid, StratumError> {
43        match &self.id {
44            Some(id) => Ok(*id),
45            None => Err(StratumError::NotSubscribed),
46        }
47    }
48
49    pub fn get_sid(&self) -> std::result::Result<String, StratumError> {
50        match &self.sid {
51            Some(sid) => Ok(sid.clone()),
52            None => Err(StratumError::NotSubscribed),
53        }
54    }
55
56    pub fn get_job_stats(&self) -> std::result::Result<MinerJobStats, StratumError> {
57        match &self.job_stats {
58            Some(job_stats) => Ok(job_stats.clone()),
59            None => Err(StratumError::Internal),
60        }
61    }
62
63    pub fn get_worker_name(&self) -> Option<String> {
64        self.worker_name.clone()
65    }
66}