forked from svenstaro/genact
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cryptomining.rs
105 lines (90 loc) · 4.27 KB
/
cryptomining.rs
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
/// Module that pretends to mine a cryptocurrency.
use rand::{thread_rng, Rng};
use rand::distributions::{Normal, Distribution};
use std::time::Instant;
use yansi::Paint;
use chrono::prelude::*;
use chrono::Duration;
use crate::utils::{gen_hex_string, csleep};
use crate::parse_args::AppConfig;
pub fn run(appconfig: &AppConfig) {
let mut rng = thread_rng();
let num_lines = rng.gen_range(300, 1000);
// How often to receive a new job.
let new_job_every_n_lines = rng.gen_range(20, 50);
let mut remaining_until_new_job = new_job_every_n_lines;
// Base value for how many MH/s a GPU gets.
let approximate_mhs_per_gpu = rng.gen_range(1.0, 99.0);
let num_gpus = rng.gen_range(1, 8);
// How often a solution will be found.
let solution_found_every_n_lines = rng.gen_range(80, 200);
let mut remaining_until_next_solution = solution_found_every_n_lines;
// How many solutions have already been found.
let mut num_solutions_found = 0;
let now = Instant::now();
for _ in 1..num_lines {
let sleep_length = 300;
let time = Paint::magenta(Local::now().format("%H:%M:%S"));
if remaining_until_new_job == 0 {
remaining_until_new_job = new_job_every_n_lines;
let info = Paint::cyan("ℹ").bold();
println!("{info:>3} {time}{separator}{source:<13}Received new job #{jobhex} seed: #{seedhex} target: #{targethex}",
info=info,
time=time,
separator=Paint::black("|"),
source=Paint::blue("stratum"),
jobhex=gen_hex_string(&mut rng, 8),
seedhex=gen_hex_string(&mut rng, 32),
targethex=gen_hex_string(&mut rng, 24));
} else if remaining_until_next_solution == 0 {
remaining_until_next_solution = solution_found_every_n_lines;
num_solutions_found += 1;
let info = Paint::cyan("ℹ").bold();
println!("{info:>3} {time}{separator}{source:<13}Solution found; Submitted to stratum.buttcoin.org",
info=info,
time=time,
separator=Paint::black("|"),
source=Paint::blue("CUDA0"));
println!("{info:>3} {time}{separator}{source:<13}Nonce: 0x{noncehex}",
info=info,
time=time,
separator=Paint::black("|"),
source=Paint::blue("CUDA0"),
noncehex=gen_hex_string(&mut rng, 16));
println!("{info:>3} {time}{separator}{source:<13}{accepted}",
info=info,
time=time,
separator=Paint::black("|"),
source=Paint::blue("stratum"),
accepted=Paint::green("Accepted."));
} else {
remaining_until_new_job -= 1;
remaining_until_next_solution -= 1;
let info = Paint::green("m");
let normal = Normal::new(0.0, 0.2);
let mut total_mhs = 0.0;
let mut gpus = String::from("");
for gpu in 0..num_gpus {
let actual_mhs_per_gpu = approximate_mhs_per_gpu + normal.sample(&mut rng);
gpus.push_str(&format!("gpu/{gpu} {mhs:.2} ", gpu=gpu, mhs=Paint::cyan(actual_mhs_per_gpu)));
total_mhs += actual_mhs_per_gpu;
}
let speed = format!("Speed {mhs:>6.2} Mh/s", mhs=Paint::cyan(total_mhs).bold());
let duration = Duration::from_std(now.elapsed()).expect("Couldn't make chrono::Duration from std::time::Duration!");
let elapsed = format!("{hours:02}:{minutes:02}", hours=duration.num_hours(), minutes=duration.num_minutes());
println!("{info:>3} {time}{separator}{source:<13}{speed} {gpus} [A{solutions}+0:R0+0:F0] Time: {elapsed}",
info=info,
time=time,
separator=Paint::black("|"),
source=Paint::blue("cryptominer"),
speed=speed,
gpus=gpus,
solutions=num_solutions_found,
elapsed=elapsed);
}
csleep(sleep_length);
if appconfig.should_exit() {
return
}
}
}