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
use crate::{Error, Result};
use async_std::sync::{Arc, Mutex};
use extended_primitives::Buffer;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Default)]
pub struct ReadyIndicator(Arc<Mutex<bool>>);
impl ReadyIndicator {
pub fn new(ready: bool) -> Self {
ReadyIndicator(Arc::new(Mutex::new(ready)))
}
pub async fn ready(&self) {
*self.0.lock().await = true;
}
pub async fn not_ready(&self) {
*self.0.lock().await = false;
}
pub async fn inner(&self) -> bool {
*self.0.lock().await
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum ID {
Num(u64),
Str(String),
Null(serde_json::Value),
}
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(Clone, Debug)]
pub enum MessageValue {
StratumV1(serde_json::map::Map<String, serde_json::Value>),
ExMessage(ExMessageGeneric),
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum MessageTypes {
RegisterWorker,
SubmitShare,
SubmitShareWithTime,
SubmitShareWithVersion,
SubmitShareWithTimeAndVersion,
UnregisterWorker,
MiningSetDiff,
Unknown(u8),
}
impl MessageTypes {
pub fn from_u8(cmd: u8) -> Self {
match cmd {
0x01 => MessageTypes::RegisterWorker,
0x02 => MessageTypes::SubmitShare,
0x03 => MessageTypes::SubmitShareWithTime,
0x04 => MessageTypes::UnregisterWorker,
0x05 => MessageTypes::MiningSetDiff,
0x12 => MessageTypes::SubmitShareWithVersion,
0x13 => MessageTypes::SubmitShareWithTimeAndVersion,
_ => MessageTypes::Unknown(cmd),
}
}
pub fn to_string(&self) -> String {
match *self {
MessageTypes::RegisterWorker => String::from("exMessageRegisterWorker"),
MessageTypes::SubmitShare => String::from("exMessageSubmitShare"),
MessageTypes::SubmitShareWithTime => String::from("exMessageSubmitShare"),
MessageTypes::UnregisterWorker => String::from("exMessageUnregisterWorker"),
MessageTypes::MiningSetDiff => String::from("exMessageMiningSetDiff"),
MessageTypes::SubmitShareWithVersion => String::from("exMessageSubmitShare"),
MessageTypes::SubmitShareWithTimeAndVersion => String::from("exMessageSubmitShare"),
_ => String::from(""),
}
}
}
#[derive(Clone, Debug)]
pub struct ExMessageGeneric {
pub magic_number: u8,
pub cmd: MessageTypes,
pub length: u16,
pub body: Buffer,
}
impl ExMessageGeneric {
pub fn from_buffer(buffer: &mut Buffer) -> Result<Self> {
let magic_number = buffer.read_u8().map_err(|_| Error::BrokenExHeader)?;
let cmd = buffer.read_u8().map_err(|_| Error::BrokenExHeader)?;
let length = buffer.read_u16().map_err(|_| Error::BrokenExHeader)?;
let body = buffer.clone();
let cmd = MessageTypes::from_u8(cmd);
Ok(ExMessageGeneric {
magic_number,
cmd,
length,
body,
})
}
}