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
use crate::{
    types::{ConnectionID, Difficulties, Difficulty, DifficultySettings, VarDiffBuffer},
    utils, ConfigManager, SessionID,
};
use parking_lot::Mutex;
use std::sync::Arc;
use tracing::warn;
use uuid::Uuid;

//A miner is essentially an individual worker unit. There can be multiple Miners on a single
//connection which is why we needed to break it into these primitives.
#[derive(Debug, Clone)]
pub struct Miner {
    config_manager: ConfigManager,
    shared: Arc<Shared>,
    inner: Arc<Inner>,
}

#[derive(Debug)]
pub(crate) struct Inner {
    pub(crate) worker_id: Uuid,
    pub(crate) sid: SessionID,
    pub(crate) connection_id: ConnectionID,
    pub(crate) client: Option<String>,
    pub(crate) name: Option<String>,
}

#[derive(Debug)]
pub(crate) struct Shared {
    difficulties: Mutex<Difficulties>,
    needs_ban: Mutex<bool>,
    stats: Mutex<MinerStats>,
    var_diff_stats: Mutex<VarDiffStats>,
    difficulty_settings: Mutex<DifficultySettings>,
}

impl Miner {
    #[must_use]
    pub fn new(
        connection_id: ConnectionID,
        worker_id: Uuid,
        sid: SessionID,
        client: Option<String>,
        name: Option<String>,
        config_manager: ConfigManager,
        difficulty: DifficultySettings,
    ) -> Self {
        let now = utils::now();

        let shared = Shared {
            difficulties: Mutex::new(Difficulties::new_only_current(difficulty.default)),
            needs_ban: Mutex::new(false),
            stats: Mutex::new(MinerStats {
                accepted: 0,
                stale: 0,
                rejected: 0,
                last_active: now,
            }),
            var_diff_stats: Mutex::new(VarDiffStats {
                last_timestamp: now,
                last_retarget: config_manager
                    .difficulty_config()
                    .initial_retarget_time(now),
                vardiff_buf: VarDiffBuffer::new(),
                last_retarget_share: 0,
            }),
            difficulty_settings: Mutex::new(difficulty),
        };

        let inner = Inner {
            worker_id,
            sid,
            connection_id,
            client,
            name,
        };

        Miner {
            config_manager,
            shared: Arc::new(shared),
            inner: Arc::new(inner),
        }
    }

    // pub(crate) fn id(&self) -> Uuid {
    //     self.inner.id
    // }

    pub(crate) fn ban(&self) {
        *self.shared.needs_ban.lock() = true;

        //@todo I think we need to disconnect here as well.
    }

    pub fn consider_ban(&self) {
        let stats = self.shared.stats.lock();

        //@note this could possibly possibly possibly overflow - let's just think about that as we
        //move forward.
        let total = stats.accepted + stats.stale + stats.rejected;

        let config = &self.config_manager.current_config().connection;

        if total >= config.check_threshold {
            let percent_bad: f64 = ((stats.stale + stats.rejected) as f64 / total as f64) * 100.0;

            if percent_bad < config.invalid_percent {
                //@todo do we want to reset though?
                //Although if we don't, then this will trigger on every new share after 500.
                //So we could switch it to modulo 500 == 0
                //@todo make this possible. Reset stats to 0.
                // self.stats.lock().await = MinerStats::default();
            } else {
                warn!(
                    id = ?self.inner.connection_id,
                    worker_id = ?self.inner.worker_id,
                    worker = ?self.inner.name,
                    client = ?self.inner.client,
                    "Miner banned. {} out of the last {} shares were invalid",
                    stats.stale + stats.rejected,
                    total
                );
                self.ban();
            }
        }
    }

    #[must_use]
    pub fn difficulties(&self) -> Difficulties {
        self.shared.difficulties.lock().clone()
    }

    //@todo in the future have this accept difficulty, and then we could calculate hashrate here.
    pub fn valid_share(&self) {
        let mut stats = self.shared.stats.lock();
        stats.accepted += 1;
        stats.last_active = utils::now();

        drop(stats);

        self.consider_ban();

        self.retarget();
    }

    pub fn stale_share(&self) {
        let mut stats = self.shared.stats.lock();

        stats.stale += 1;
        stats.last_active = utils::now();

        drop(stats);

        self.consider_ban();

        //@todo see below
        //I don't think we want to retarget on invalid shares, but let's double check later.
        // self.retarget().await;
    }

    pub fn rejected_share(&self) {
        let mut stats = self.shared.stats.lock();

        stats.rejected += 1;
        stats.last_active = utils::now();

        drop(stats);

        self.consider_ban();

        //@todo see below
        //I don't think we want to retarget on invalid shares, but let's double check later.
        // self.retarget().await;
    }

    //@todo note, this only can be sent over ExMessage when it's hit a certain threshold.
    //@todo self.set_difficulty
    //@todo self.set_next_difficulty
    //@todo does this need to return a result? Ideally not, but if we send difficulty, then maybe.
    //@todo see if we can solve a lot of these recasting issues.
    //@todo wrap u64 with a custom difficulty type.
    fn retarget(&self) {
        let now = utils::now();

        let mut difficulties = self.shared.difficulties.lock();
        let mut var_diff_stats = self.shared.var_diff_stats.lock();
        let stats = self.shared.stats.lock();

        let since_last = now - var_diff_stats.last_timestamp;

        var_diff_stats.vardiff_buf.append(since_last);
        var_diff_stats.last_timestamp = now;

        //@todo review this code, see if we can make this easier.
        if !(((stats.accepted - var_diff_stats.last_retarget_share)
            >= self
                .config_manager
                .difficulty_config()
                .retarget_share_amount)
            || (now - var_diff_stats.last_retarget)
                >= self.config_manager.difficulty_config().retarget_time as u128)
        {
            return;
        }

        var_diff_stats.last_retarget = now;
        var_diff_stats.last_retarget_share = stats.accepted;

        // let variance = self.options.target_time * (self.options.variance_percent as f64 / 100.0);
        // let time_min = self.options.target_time as f64 * 0.40;
        // let time_max = self.options.target_time as f64 * 1.40;

        //This average is in milliseconds
        let avg = var_diff_stats.vardiff_buf.avg();

        if avg <= 0.0 {
            return;
        }

        let mut new_diff;

        //@todo figure out what else needs to come from config here, and comment out this function.
        let target_time = self.config_manager.difficulty_config().target_time as f64 * 1000.0;

        if avg > target_time {
            if (avg / self.config_manager.difficulty_config().target_time as f64) <= 1.5 {
                return;
            }
            new_diff = difficulties.current().as_u64() / 2;
        } else if (avg / target_time) >= 0.7 {
            return;
        } else {
            new_diff = difficulties.current().as_u64() * 2;
        }

        new_diff = new_diff.clamp(
            self.shared.difficulty_settings.lock().minimum.as_u64(),
            self.config_manager.difficulty_config().maximum_difficulty,
        );

        if new_diff != difficulties.current().as_u64() {
            difficulties.update_next(Difficulty::from(new_diff));
            var_diff_stats.vardiff_buf.reset();
        }
    }

    #[must_use]
    pub fn update_difficulty(&self) -> Option<Difficulty> {
        let mut difficulties = self.shared.difficulties.lock();

        difficulties.shift()
    }

    pub fn set_difficulty(&self, difficulty: Difficulty) {
        let mut difficulties = self.shared.difficulties.lock();

        difficulties.set_and_shift(difficulty);
    }

    #[must_use]
    pub fn connection_id(&self) -> ConnectionID {
        self.inner.connection_id.clone()
    }

    #[must_use]
    pub fn worker_id(&self) -> Uuid {
        self.inner.worker_id
    }

    #[must_use]
    pub fn session_id(&self) -> SessionID {
        self.inner.sid
    }
}

//@todo either wrap miner in a folder, or move these both to types
#[derive(Debug, Clone)]
pub struct MinerStats {
    accepted: u64,
    stale: u64,
    rejected: u64,
    last_active: u128,
}

#[derive(Debug)]
pub struct VarDiffStats {
    last_timestamp: u128,
    last_retarget_share: u64,
    last_retarget: u128,
    vardiff_buf: VarDiffBuffer,
}

#[cfg(test)]
mod test {
    use std::thread::sleep;

    use super::*;
    use crate::Config;

    #[test]
    fn test_valid_share() {
        let connection_id = ConnectionID::new();
        let worker_id = Uuid::new_v4();
        let session_id = SessionID::from(1);

        let config = Config::default();
        let config_manager = ConfigManager::new(config.clone());

        let diff_settings = DifficultySettings {
            default: Difficulty::from(config.difficulty.initial_difficulty),
            minimum: Difficulty::from(config.difficulty.minimum_difficulty),
        };
        let miner = Miner::new(
            connection_id,
            worker_id,
            session_id,
            None,
            None,
            config_manager,
            diff_settings,
        );

        miner.valid_share();

        for _ in 0..100 {
            miner.valid_share();
            sleep(std::time::Duration::from_millis(50));
        }

        let new_diff = miner.update_difficulty();
        assert!(new_diff.is_some());

        for _ in 0..100 {
            miner.valid_share();
        }

        let new_diff = miner.update_difficulty();
        assert!(new_diff.is_some());

        //@todo we need some actual result here lol
    }
}