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
pub mod error;
pub mod miner;
pub mod params;
pub mod stratum_error;
pub mod traits;
use crate::params::{Params, Results};
pub use crate::stratum_error::StratumError;
use crate::traits::{PoolParams, StratumParams};
pub use error::Error;
pub use miner::{MinerAuth, MinerInfo, MinerJobStats};
use serde::de::{self, MapAccess, SeqAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::fmt;
use std::marker::PhantomData;

pub type Result<T> = std::result::Result<T, Error>;

// #[derive(Serialize, Deserialize)]
// #[serde(untagged)]
// pub enum PoolPacket<PP, SP>
// where
//     PP: PoolParams,
//     SP: StratumParams,
// {
//     Request(Request<PoolParam<PP, SP>>),
//     Response(Response<PoolParam<PP, SP>>),
// }

// #[derive(Serialize, Deserialize)]
// #[serde(untagged)]
// pub enum ClientPacket<PP, SP>
// where
//     PP: PoolParams,
//     SP: StratumParams,
// {
//     Request(Request<ClientParam<PP, SP>>),
//     Response(Response<ClientParam<PP, SP>>),
// }

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
pub enum StratumPacket<PP, SP>
where
    PP: PoolParams,
    SP: StratumParams,
{
    Request(Request<PP, SP>),
    Response(Response<PP>),
}

#[derive(Serialize, Deserialize)]
pub struct Response<PP>
where
    PP: PoolParams,
{
    pub id: ID,
    #[serde(skip_serializing_if = "StratumMethod::is_classic")]
    pub method: StratumMethod,
    pub result: Option<Results<PP>>,
    pub error: Option<StratumError>,
}

// #[derive(Serialize, Deserialize)]
#[derive(Serialize)]
pub struct Request<PP, SP>
where
    PP: PoolParams,
    SP: StratumParams,
{
    pub id: ID,
    pub method: StratumMethod,
    pub params: Params<PP, SP>,
}

impl<'de, PP, SP> Deserialize<'de> for Request<PP, SP>
where
    PP: PoolParams + Deserialize<'de>,
    SP: StratumParams + Deserialize<'de>,
{
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        enum Field {
            Id,
            Method,
            Params,
            JsonRPC,
        };

        // This part could also be generated independently by:
        //
        //    #[derive(Deserialize)]
        //    #[serde(field_identifier, rename_all = "lowercase")]
        //    enum Field { Secs, Nanos }
        impl<'de> Deserialize<'de> for Field {
            fn deserialize<D>(deserializer: D) -> std::result::Result<Field, D::Error>
            where
                D: Deserializer<'de>,
            {
                struct FieldVisitor;

                impl<'de> Visitor<'de> for FieldVisitor {
                    type Value = Field;

                    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                        formatter.write_str("`id` or `method` or params")
                    }

                    fn visit_str<E>(self, value: &str) -> std::result::Result<Field, E>
                    where
                        E: de::Error,
                    {
                        match value {
                            "id" => Ok(Field::Id),
                            "method" => Ok(Field::Method),
                            "params" => Ok(Field::Params),
                            "jsonrpc" => Ok(Field::JsonRPC),
                            _ => Err(de::Error::unknown_field(value, FIELDS)),
                        }
                    }
                }

                deserializer.deserialize_identifier(FieldVisitor)
            }
        }

        // struct RequestVisitor;
        struct RequestVisitor<SP, PP>
        where
            PP: PoolParams,
            SP: StratumParams,
        {
            marker: PhantomData<fn() -> Request<PP, SP>>,
        };

        impl<SP, PP> RequestVisitor<SP, PP>
        where
            PP: PoolParams,
            SP: StratumParams,
        {
            fn new() -> Self {
                RequestVisitor {
                    marker: PhantomData,
                }
            }
        }

        impl<'de, SP, PP> Visitor<'de> for RequestVisitor<SP, PP>
        where
            PP: PoolParams + Deserialize<'de>,
            SP: StratumParams + Deserialize<'de>,
        {
            type Value = Request<PP, SP>;

            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
                formatter.write_str("struct Request")
            }

            //     fn visit_seq<V>(self, mut seq: V) -> Result<Request, V::Error>
            //     where
            //         V: SeqAccess<'de>,
            //     {
            //         let secs = seq
            //             .next_element()?
            //             .ok_or_else(|| de::Error::invalid_length(0, &self))?;
            //         let nanos = seq
            //             .next_element()?
            //             .ok_or_else(|| de::Error::invalid_length(1, &self))?;
            //         Ok(Duration::new(secs, nanos))
            //     }

            fn visit_map<V>(self, mut map: V) -> std::result::Result<Request<PP, SP>, V::Error>
            where
                V: MapAccess<'de>,
            {
                let mut id = None;
                let mut method = None;
                // let mut jsonrpc = None;
                let mut params = None;
                while let Some(key) = map.next_key()? {
                    match key {
                        Field::Id => {
                            if id.is_some() {
                                return Err(de::Error::duplicate_field("id"));
                            }
                            id = Some(map.next_value()?);
                        }
                        Field::Method => {
                            if method.is_some() {
                                return Err(de::Error::duplicate_field("method"));
                            }
                            method = Some(map.next_value()?);
                        }
                        Field::JsonRPC => {
                            //Do nothing but don't error out.
                        }
                        Field::Params => {
                            if params.is_some() {
                                return Err(de::Error::duplicate_field("params"));
                            }
                            if let Some(temp_method) = &method {
                                if &StratumMethod::ClassicSubscribe == temp_method
                                    || &StratumMethod::Subscribe == temp_method
                                {
                                    let temp: PP::Subscribe = map.next_value()?;
                                    params = Some(Params::Subscribe(temp));
                                // params: PP::Subscribe = Some(map.next_value()?);
                                } else {
                                    params = Some(map.next_value()?);
                                }
                            } else {
                                params = Some(map.next_value()?);
                            }
                        }
                    }
                }

                let id = id.ok_or_else(|| de::Error::missing_field("id"))?;
                let method = method.ok_or_else(|| de::Error::missing_field("method"))?;
                let params = params.ok_or_else(|| de::Error::missing_field("params"))?;

                Ok(Request { id, method, params })
            }
        }

        const FIELDS: &'static [&'static str] = &["id", "method", "params", "jsonrpc"];
        deserializer.deserialize_struct("Request", FIELDS, RequestVisitor::new())
    }
}

#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum ID {
    Num(u64),
    Str(String),
    Null(serde_json::Value),
}

// impl Serialize for ID {
//     fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
//     where
//         S: Serializer,
//     {
//         let id = match *self {
//             ID::Num(num) => num.to_string(),
//             ID::Str(ref string) => string.clone(),
//         };

//         serializer.serialize_str(&id)
//     }
// }

impl std::fmt::Display for ID {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ID::Num(ref e) => write!(f, "{}", e),
            ID::Str(ref e) => write!(f, "{}", e),
            ID::Null(ref _e) => write!(f, "null"),
        }
    }
}

#[derive(Debug, PartialEq)]
pub enum StratumMethod {
    //Sending and receiving
    Authorize,
    ClassicAuthorize,
    Submit,
    ClassicSubmit,
    Subscribe,
    ClassicSubscribe,
    Notify,
    ClassicNotify,
    SetDifficulty,
    ClassicSetDifficulty,

    //Future methods potentially not implemented yet.
    Unknown(String),
}

impl StratumMethod {
    pub fn is_classic(&self) -> bool {
        match self {
            StratumMethod::ClassicAuthorize => true,
            StratumMethod::ClassicSubmit => true,
            StratumMethod::ClassicNotify => true,
            StratumMethod::ClassicSetDifficulty => true,
            _ => false,
        }
    }
}

impl Serialize for StratumMethod {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(match *self {
            StratumMethod::Authorize => "authorize",
            StratumMethod::ClassicAuthorize => "mining.authorize",
            StratumMethod::Submit => "submit",
            StratumMethod::ClassicSubmit => "mining.submit",
            StratumMethod::Subscribe => "subscribe",
            StratumMethod::ClassicSubscribe => "mining.subscribe",
            StratumMethod::Notify => "notify",
            StratumMethod::ClassicNotify => "mining.notify",
            StratumMethod::SetDifficulty => "set_difficulty",
            StratumMethod::ClassicSetDifficulty => "mining.set_difficulty",
            StratumMethod::Unknown(ref s) => s,
        })
    }
}

impl<'de> Deserialize<'de> for StratumMethod {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        Ok(match s.as_str() {
            "authorize" => StratumMethod::Authorize,
            "mining.authorize" => StratumMethod::ClassicAuthorize,
            "submit" => StratumMethod::Submit,
            "mining.submit" => StratumMethod::ClassicSubmit,
            "subscribe" => StratumMethod::Subscribe,
            "mining.subscribe" => StratumMethod::ClassicSubscribe,
            "notify" => StratumMethod::Notify,
            "mining.notify" => StratumMethod::ClassicNotify,
            "set_difficulty" => StratumMethod::SetDifficulty,
            "mining.set_difficulty" => StratumMethod::ClassicSetDifficulty,
            _ => StratumMethod::Unknown(s),
        })
    }
}