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
use async_std::sync::{Arc, Mutex};
use extended_primitives::Buffer;
use time::OffsetDateTime;
use tracing::warn;
use uuid::Uuid;
use crate::{connection::MinerOptions, types::VarDiffBuffer};
#[derive(Debug, Clone)]
pub struct Miner {
pub id: Uuid,
pub sid: Buffer,
pub client: Option<String>,
pub name: Option<String>,
pub difficulty: Arc<Mutex<u64>>,
pub previous_difficulty: Arc<Mutex<u64>>,
pub next_difficulty: Arc<Mutex<Option<u64>>>,
pub stats: Arc<Mutex<MinerStats>>,
pub job_stats: Arc<Mutex<JobStats>>,
pub options: Arc<MinerOptions>,
pub needs_ban: Arc<Mutex<bool>>,
}
impl Miner {
pub fn new(
id: Uuid,
client: Option<String>,
name: Option<String>,
sid: Buffer,
options: Arc<MinerOptions>,
difficulty: u64,
) -> Self {
Miner {
id,
sid,
client,
name,
difficulty: Arc::new(Mutex::new(difficulty)),
previous_difficulty: Arc::new(Mutex::new(difficulty)),
next_difficulty: Arc::new(Mutex::new(None)),
stats: Arc::new(Mutex::new(MinerStats {
accepted_shares: 0,
rejected_shares: 0,
last_active: OffsetDateTime::now_utc(),
})),
job_stats: Arc::new(Mutex::new(JobStats {
last_timestamp: OffsetDateTime::now_utc().unix_timestamp(),
last_retarget: OffsetDateTime::now_utc().unix_timestamp()
- options.retarget_time as i64 / 2,
vardiff_buf: VarDiffBuffer::new(),
last_retarget_share: 0,
current_difficulty: difficulty,
})),
options,
needs_ban: Arc::new(Mutex::new(false)),
}
}
pub async fn ban(&self) {
*self.needs_ban.lock().await = true;
}
pub async fn consider_ban(&self) {
let accepted = self.stats.lock().await.accepted_shares;
let rejected = self.stats.lock().await.rejected_shares;
let total = accepted + rejected;
let check_threshold = 500;
let invalid_percent = 50.0;
if total >= check_threshold {
let percent_bad: f64 = (rejected as f64 / total as f64) * 100.0;
if percent_bad < invalid_percent {
} else {
warn!(
"Miner: {} banned. {} out of the last {} shares were invalid",
self.id, rejected, total
);
}
}
}
pub async fn current_difficulty(&self) -> u64 {
*self.difficulty.lock().await
}
pub async fn previous_difficulty(&self) -> u64 {
*self.previous_difficulty.lock().await
}
pub async fn valid_share(&self) {
let mut stats = self.stats.lock().await;
stats.accepted_shares += 1;
stats.last_active = OffsetDateTime::now_utc();
drop(stats);
self.retarget().await;
}
pub async fn invalid_share(&self) {
self.stats.lock().await.rejected_shares += 1;
}
async fn retarget(&self) {
let now = OffsetDateTime::now_utc().unix_timestamp();
let mut job_stats = self.job_stats.lock().await;
let since_last = now - job_stats.last_timestamp;
job_stats.vardiff_buf.append(since_last);
job_stats.last_timestamp = now;
let stats = self.stats.lock().await;
if !(((stats.accepted_shares - job_stats.last_retarget_share as u64) >= 30)
|| (now - job_stats.last_retarget) >= self.options.retarget_time as i64)
{
return;
}
job_stats.last_retarget = now;
job_stats.last_retarget_share = stats.accepted_shares as i64;
let avg = job_stats.vardiff_buf.avg();
if avg <= 0.0 {
return;
}
let mut new_diff;
if avg > self.options.target_time as f64 {
if (avg / self.options.target_time as f64) <= 1.5 {
return;
} else {
new_diff = job_stats.current_difficulty / 2;
}
} else if (avg / self.options.target_time as f64) >= 0.7 {
return;
} else {
new_diff = job_stats.current_difficulty * 2;
}
if new_diff < self.options.min_diff {
new_diff = self.options.min_diff;
}
if new_diff > self.options.max_diff {
new_diff = self.options.max_diff;
}
if new_diff != job_stats.current_difficulty {
*self.next_difficulty.lock().await = Some(new_diff);
job_stats.vardiff_buf.reset();
}
}
pub async fn update_difficulty(&self) -> Option<u64> {
let next_difficulty = *self.next_difficulty.lock().await;
if let Some(next_difficulty) = next_difficulty {
*self.difficulty.lock().await = next_difficulty;
self.job_stats.lock().await.current_difficulty = next_difficulty;
*self.next_difficulty.lock().await = None;
Some(next_difficulty)
} else {
None
}
}
pub async fn set_difficulty(&self, difficulty: u64) {
let old_diff = *self.difficulty.lock().await;
*self.difficulty.lock().await = difficulty;
*self.previous_difficulty.lock().await = old_diff;
self.job_stats.lock().await.current_difficulty = difficulty;
}
}
#[derive(Debug, Clone)]
pub struct MinerStats {
accepted_shares: u64,
rejected_shares: u64,
last_active: OffsetDateTime,
}
#[derive(Debug)]
pub struct JobStats {
last_timestamp: i64,
last_retarget_share: i64,
last_retarget: i64,
vardiff_buf: VarDiffBuffer,
current_difficulty: u64,
}