forked from tari-project/tari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminer.rs
255 lines (240 loc) · 9.35 KB
/
miner.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
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
// Copyright 2021. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
use std::{
panic::panic_any,
pin::Pin,
task::{Context, Poll, Waker},
thread,
time::{Duration, Instant},
};
use chrono::Utc;
use crossbeam::channel::{bounded, Select, Sender, TrySendError};
use futures::Stream;
use log::*;
use minotari_app_grpc::tari_rpc::BlockHeader;
use thread::JoinHandle;
use super::difficulty::BlockHeaderSha3;
pub const LOG_TARGET: &str = "minotari::miner::standalone";
// Identify how often mining thread is reporting / checking context
// ~400_000 hashes per second
const REPORTING_FREQUENCY: u64 = 3_000_000;
// Thread's stack size, ideally we would fit all thread's data in the CPU L1 cache
const STACK_SIZE: usize = 320_000;
/// Miner will send regular reports from every mining threads
#[derive(Debug)]
pub struct MiningReport {
pub miner: usize,
pub target_difficulty: u64,
pub difficulty: u64,
pub hashes: u64,
pub elapsed: Duration,
/// Will be set for when mined header is matching required difficulty
pub header: Option<BlockHeader>,
pub height: u64,
pub last_nonce: u64,
}
/// Miner is starting number of mining threads and implements Stream for async reports polling
/// Communication with async world is performed via channel and waker so should be quite efficient
pub struct Miner {
threads: Vec<JoinHandle<()>>,
channels: Vec<crossbeam::channel::Receiver<MiningReport>>,
num_threads: usize,
header: BlockHeader,
target_difficulty: u64,
share_mode: bool,
}
impl Miner {
pub fn init_mining(header: BlockHeader, target_difficulty: u64, num_threads: usize, share_mode: bool) -> Self {
Self {
threads: vec![],
channels: vec![],
header,
num_threads,
target_difficulty,
share_mode,
}
}
// this will kill all mining threads currently active and attached to this miner
pub fn kill_threads(&mut self) {
self.channels.clear();
}
// Start mining threads with async context waker
fn start_threads(&mut self, ctx: &Context<'_>) {
let miners = (0..self.num_threads)
.map(|i| {
(
thread::Builder::new()
.name(format!("cpu-miner-{}", i))
.stack_size(STACK_SIZE),
i,
)
})
.map(|(thread, i)| {
let (tx, rx) = bounded(1);
let header = self.header.clone();
let waker = ctx.waker().clone();
let difficulty = self.target_difficulty;
let share_mode = self.share_mode;
let handle = thread
.spawn(move || mining_task(header, difficulty, tx, waker, i, share_mode))
.expect("Failed to create mining thread");
(handle, rx)
});
let (threads, channels) = miners.unzip();
self.threads = threads;
self.channels = channels;
}
}
impl Stream for Miner {
type Item = MiningReport;
fn poll_next(mut self: Pin<&mut Self>, ctx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
trace!(target: LOG_TARGET, "Polling Miner");
// First poll would start all the threads passing async context waker
if self.threads.is_empty() && self.num_threads > 0 {
debug!(
target: LOG_TARGET,
"Starting {} mining threads for target difficulty {}", self.num_threads, self.target_difficulty
);
self.start_threads(ctx);
return Poll::Pending;
} else if self.num_threads == 0 {
error!(target: LOG_TARGET, "Cannot mine: no mining threads");
return Poll::Ready(None);
} else if self.channels.is_empty() {
debug!(target: LOG_TARGET, "Finished mining");
return Poll::Ready(None);
} else {
// do nothing
}
// Non blocking select from all miner's receiver channels
let mut sel = Select::new();
for rx in &self.channels {
sel.recv(rx);
}
let report = match sel.try_select() {
Ok(oper) => {
let idx = oper.index();
match oper.recv(&self.channels[idx]) {
Ok(report) => report,
Err(_) => {
// Received error would mean thread is disconnected already
trace!("Thread {} disconnected.", idx);
return Poll::Ready(None);
},
}
},
Err(_) => {
// No reports
return Poll::Pending;
},
};
if report.header.is_some() && !self.share_mode {
// Dropping recipients would stop miners next time they try to report
self.channels.clear();
}
Poll::Ready(Some(report))
}
}
/// Miner starts with a random nonce and iterates until it finds a header hash that meets the desired
/// target
pub fn mining_task(
header: BlockHeader,
target_difficulty: u64,
sender: Sender<MiningReport>,
waker: Waker,
miner: usize,
share_mode: bool,
) {
let start = Instant::now();
let mut hasher = match BlockHeaderSha3::new(header) {
Ok(hasher) => hasher,
Err(err) => {
let err = format!("Miner {} failed to create hasher: {:?}", miner, err);
error!(target: LOG_TARGET, "{}", err);
panic_any(err);
},
};
hasher.random_nonce();
// We're mining over here!
trace!(target: LOG_TARGET, "Mining thread {} started", miner);
// Mining work
loop {
let difficulty = match hasher.difficulty() {
Ok(difficulty) => difficulty,
Err(err) => {
let err = format!("Miner {} failed to calculate difficulty: {:?}", miner, err);
error!(target: LOG_TARGET, "{}", err);
panic_any(err);
},
};
if difficulty >= target_difficulty {
debug!(
target: LOG_TARGET,
"Miner {} found nonce {} with matching difficulty {}", miner, hasher.header.nonce, difficulty
);
if let Err(err) = sender.try_send(MiningReport {
miner,
difficulty,
hashes: hasher.hashes,
elapsed: start.elapsed(),
height: hasher.height(),
last_nonce: hasher.header.nonce,
header: Some(hasher.create_header()),
target_difficulty,
}) {
error!(target: LOG_TARGET, "Miner {} failed to send report: {}", miner, err);
}
// If we are mining in share mode, this share might not be a block, so we need to keep mining till we get a
// new job
if share_mode {
waker.wake_by_ref();
} else {
waker.wake();
trace!(target: LOG_TARGET, "Mining thread {} stopped", miner);
return;
}
}
if hasher.header.nonce % REPORTING_FREQUENCY == 0 {
let res = sender.try_send(MiningReport {
miner,
difficulty,
hashes: hasher.hashes,
elapsed: start.elapsed(),
header: None,
last_nonce: hasher.header.nonce,
height: hasher.height(),
target_difficulty,
});
waker.wake_by_ref();
trace!(target: LOG_TARGET, "Reporting from {} result {:?}", miner, res);
if let Err(TrySendError::Disconnected(_)) = res {
info!(target: LOG_TARGET, "Mining thread {} disconnected", miner);
return;
}
if !(share_mode) {
hasher.set_forward_timestamp(Utc::now().timestamp() as u64);
}
}
hasher.inc_nonce();
}
}