-
Notifications
You must be signed in to change notification settings - Fork 28
/
PoolMiner.java
397 lines (312 loc) · 10.2 KB
/
PoolMiner.java
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package snowblossom.miner;
import com.google.protobuf.ByteString;
import duckutil.Config;
import duckutil.ConfigFile;
import duckutil.TimeRecord;
import duckutil.TimeRecordAuto;
import duckutil.RateReporter;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.stub.StreamObserver;
import snowblossom.lib.*;
import snowblossom.proto.*;
import snowblossom.mining.proto.*;
import snowblossom.mining.proto.MiningPoolServiceGrpc.MiningPoolServiceStub;
import snowblossom.mining.proto.MiningPoolServiceGrpc.MiningPoolServiceBlockingStub;
import snowblossom.lib.trie.HashUtils;
import snowblossom.client.WalletUtil;
import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.Buffer;
import java.security.MessageDigest;
import java.text.DecimalFormat;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import duckutil.MultiAtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
public class PoolMiner implements PoolClientOperator
{
private static final Logger logger = Logger.getLogger("snowblossom.miner");
public static void main(String args[]) throws Exception
{
Globals.addCryptoProvider();
if (args.length != 1)
{
logger.log(Level.SEVERE, "Incorrect syntax. Syntax: PoolMiner <config_file>");
System.exit(-1);
}
ConfigFile config = new ConfigFile(args[0]);
LogSetup.setup(config);
PoolMiner miner = new PoolMiner(config);
while (true)
{
Thread.sleep(15000);
miner.printStats();
}
}
private volatile WorkUnit last_work_unit;
private final FieldScan field_scan;
private final NetworkParams params;
private final MultiAtomicLong op_count = new MultiAtomicLong();
private long last_stats_time = System.currentTimeMillis();
private Config config;
private File snow_path;
private TimeRecord time_record;
private RateReporter rate_report=new RateReporter();
private AtomicLong share_submit_count = new AtomicLong(0L);
private AtomicLong share_reject_count = new AtomicLong(0L);
private AtomicLong share_block_count = new AtomicLong(0L);
private final PoolClientFace pool_client;
public PoolMiner(Config config) throws Exception
{
this.config = config;
logger.info(String.format("Starting PoolMiner version %s", Globals.VERSION));
config.require("snow_path");
params = NetworkParams.loadFromConfig(config);
if (config.isSet("pool_host_list"))
{
pool_client = new PoolClientFailover(config, this);
}
else
{
pool_client = new PoolClient(config, this);
}
snow_path = new File(config.get("snow_path"));
field_scan = new FieldScan(snow_path, params, config);
if (config.getBoolean("display_timerecord"))
{
time_record = new TimeRecord();
TimeRecord.setSharedRecord(time_record);
}
pool_client.subscribe();
int threads = config.getIntWithDefault("threads", 8);
logger.info("Starting " + threads + " threads");
for (int i = 0; i < threads; i++)
{
new MinerThread().start();
}
}
public void stop()
{
terminate = true;
pool_client.stop();
}
private volatile boolean terminate = false;
public void printStats()
{
long now = System.currentTimeMillis();
long count_long = op_count.sumAndReset();
double count = count_long;
rate_report.record(count_long);
double time_ms = now - last_stats_time;
double time_sec = time_ms / 1000.0;
double rate = count / time_sec;
DecimalFormat df = new DecimalFormat("0.000");
String block_time_report = "";
if (last_work_unit != null)
{
BigInteger target = BlockchainUtil.targetBytesToBigInteger(last_work_unit.getReportTarget());
double diff = PowUtil.getDiffForTarget(target);
double block_time_sec = Math.pow(2.0, diff) / rate;
double min = block_time_sec / 60.0;
block_time_report = String.format("- at this rate %s minutes per share (diff %s)", df.format(min), df.format(diff));
}
logger.info(String.format("15 Second mining rate: %s/sec %s", df.format(rate), block_time_report));
logger.info(rate_report.getReportShort(df));
last_stats_time = now;
if (count == 0)
{
if (getWorkUnit() == null)
{
logger.info("Stalled. No valid work unit, reconnecting to pool");
try
{
pool_client.subscribe();
}
catch (Throwable t)
{
logger.info("Exception in subscribe: " + t);
}
}
else
{
logger.info("No hashing, and we have a good work unit from the pool. So probably something else wrong.");
}
}
if (config.getBoolean("display_timerecord"))
{
TimeRecord old = time_record;
time_record = new TimeRecord();
TimeRecord.setSharedRecord(time_record);
old.printReport(System.out);
}
logger.info(String.format("Shares: %d (rejected %d) (blocks %d)", share_submit_count.get(), share_reject_count.get(), share_block_count.get()));
}
public WorkUnit getWorkUnit()
{
return last_work_unit;
}
public FieldScan getFieldScan()
{
return field_scan;
}
public class MinerThread extends Thread
{
Random rnd;
MessageDigest md = DigestUtil.getMD();
byte[] tmp_buff = new byte[32];
byte[] word_buff = new byte[SnowMerkle.HASH_LEN];
ByteBuffer word_bb = ByteBuffer.wrap(word_buff);
SnowMerkleProof merkle_proof;
int proof_field;
byte[] nonce = new byte[Globals.NONCE_LENGTH];
public MinerThread()
{
setName("MinerThread");
setDaemon(true);
rnd = new Random();
}
private void runPass() throws Exception
{
WorkUnit wu = last_work_unit;
if (wu == null)
{
try (TimeRecordAuto tra = TimeRecord.openAuto("MinerThread.nullBlockSleep"))
{
Thread.sleep(100);
return;
}
}
if (wu.getHeader().getTimestamp() + 75000 < System.currentTimeMillis())
{
logger.log(Level.WARNING, "Work Unit is old, not mining it");
last_work_unit = null;
}
try (TimeRecordAuto tra = TimeRecord.openAuto("MinerThread.rndNonce"))
{
rnd.nextBytes(nonce);
wu.getHeader().getNonce().copyTo(nonce, 0);
}
byte[] first_hash = PowUtil.hashHeaderBits(wu.getHeader(), nonce, md);
/**
* This is a windows specific improvement since windows likes separete file descriptors
* per thread.
*/
if ((merkle_proof == null) || (proof_field != wu.getHeader().getSnowField()))
{
merkle_proof = field_scan.getSingleUserFieldProof(wu.getHeader().getSnowField());
proof_field = wu.getHeader().getSnowField();
}
byte[] context = first_hash;
try (TimeRecordAuto tra = null)
{
for (int pass = 0; pass < Globals.POW_LOOK_PASSES; pass++)
{
long word_idx;
((Buffer)word_bb).clear();
word_idx = PowUtil.getNextSnowFieldIndex(context, merkle_proof.getTotalWords(), md, tmp_buff);
if (!merkle_proof.readWord(word_idx, word_bb, pass)) { return;}
PowUtil.getNextContext(context, word_buff, md, context);
}
}
byte[] found_hash = context;
if (PowUtil.lessThanTarget(found_hash, wu.getReportTarget()))
{
String str = HashUtils.getHexString(found_hash);
logger.info("Found passable solution: " + str);
submitWork(wu, nonce, merkle_proof);
}
op_count.add(1L);
}
private void submitWork(WorkUnit wu, byte[] nonce, SnowMerkleProof merkle_proof) throws Exception
{
byte[] first_hash = PowUtil.hashHeaderBits(wu.getHeader(), nonce);
byte[] context = first_hash;
BlockHeader.Builder header = BlockHeader.newBuilder();
header.mergeFrom(wu.getHeader());
header.setNonce(ByteString.copyFrom(nonce));
for (int pass = 0; pass < Globals.POW_LOOK_PASSES; pass++)
{
((Buffer)word_bb).clear();
long word_idx = PowUtil.getNextSnowFieldIndex(context, merkle_proof.getTotalWords());
boolean gotData = merkle_proof.readWord(word_idx, word_bb, pass);
if (!gotData)
{
logger.log(Level.SEVERE, "readWord returned false on pass " + pass);
}
SnowPowProof proof = merkle_proof.getProof(word_idx);
header.addPowProof(proof);
context = PowUtil.getNextContext(context, word_buff);
}
byte[] found_hash = context;
header.setSnowHash(ByteString.copyFrom(found_hash));
SubmitReply reply = pool_client.submitWork(wu, header.build());
if (PowUtil.lessThanTarget(found_hash, header.getTarget()))
{
share_block_count.getAndIncrement();
}
logger.info("Work submit: " + reply);
share_submit_count.getAndIncrement();
if (!reply.getSuccess())
{
share_reject_count.getAndIncrement();
}
}
public void run()
{
while (!terminate)
{
boolean err = false;
try (TimeRecordAuto tra = TimeRecord.openAuto("MinerThread.runPass"))
{
runPass();
}
catch (Throwable t)
{
err = true;
logger.warning("Error: " + t);
}
if (err)
{
try (TimeRecordAuto tra = TimeRecord.openAuto("MinerThread.errorSleep"))
{
Thread.sleep(5000);
}
catch (Throwable t)
{
}
}
}
}
}
@Override
public void notifyNewBlock(int block_id){}
@Override
public void notifyNewWorkUnit(WorkUnit wu)
{
int min_field = wu.getHeader().getSnowField();
int selected_field = -1;
try
{
selected_field = field_scan.selectField(min_field);
BlockHeader.Builder bh = BlockHeader.newBuilder();
bh.mergeFrom(wu.getHeader());
bh.setSnowField(selected_field);
WorkUnit wu_new = WorkUnit.newBuilder()
.mergeFrom(wu)
.setHeader(bh.build())
.build();
last_work_unit = wu_new;
}
catch (Throwable t)
{
logger.info("Work block load error: " + t.toString());
last_work_unit = null;
}
}
}