-
Notifications
You must be signed in to change notification settings - Fork 9
/
class.Share.js
424 lines (347 loc) · 11.7 KB
/
class.Share.js
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
'use strict';
const
precon = require('@mintpond/mint-precon'),
mu = require('@mintpond/mint-utils'),
buffers = require('@mintpond/mint-utils').buffers,
bi = require('@mintpond/mint-utils').bi,
algorithm = require('./service.algorithm'),
StratumError = require('./class.StratumError');
const NONCE_SIZE = 8;
const PROGPOW_HASH_SIZE = 32;
const MIX_HASH_SIZE = 32;
const HASH_OUT_BUFFER = Buffer.alloc(32);
const MIX_HASH_BUFFER = Buffer.alloc(32);
class Share {
/**
* Constructor.
*
* @param args
* @param args.client {Client}
* @param args.stratum {Stratum}
* @param args.workerName {string}
* @param args.jobIdHex {string}
* @param args.nonceBuf {Buffer}
* @param args.progpowHashBuf {Buffer}
* @param args.mixHashBuf {Buffer}
*/
constructor(args) {
precon.notNull(args.client, 'client');
precon.notNull(args.stratum, 'stratum');
precon.string(args.workerName, 'workerName');
precon.string(args.jobIdHex, 'jobIdHex');
precon.buffer(args.nonceBuf, 'nonceBuf');
precon.buffer(args.progpowHashBuf, 'progpowHashBuf');
precon.buffer(args.mixHashBuf, 'mixHashBuf');
const _ = this;
_._client = args.client;
_._stratum = args.stratum;
_._workerName = args.workerName;
_._jobIdHex = args.jobIdHex;
_._nonceBuf = args.nonceBuf;
_._progpowHashBuf = args.progpowHashBuf;
_._mixHashBuf = args.mixHashBuf;
_._nonceHex = buffers.leToHex(_._nonceBuf);
_._stratumDiff = _._client.diff;
_._shareDiff = 0;
_._expectedBlocks = 0;
_._submitTime = mu.now();
_._job = null;
_._isValidShare = null;
_._isValidBlock = false;
_._isBlockAccepted = false;
_._error = null;
_._blockId = null;
_._blockHex = null;
_._blockTxId = null;
_._minerAddress = _._workerName.split('.');
}
/**
* Get the client that submitted the share.
* @returns {Client}
*/
get client() { return this._client; }
/**
* Get the job ID of the share.
* @returns {string}
*/
get jobIdHex() { return this._jobIdHex; }
/**
* Get the job height of the share.
* This value is not available until a job has been associated with the share via the #validate function.
* @returns {number}
*/
get jobHeight() { return this._job ? this._job.height : 0; }
/**
* Get the time of the share submission in epoch seconds.
* @returns {number}
*/
get submitTime() { return this._submitTime; }
/**
* Get the subscription ID of the client that submitted the share.
* @returns {string}
*/
get subscriptionIdHex() { return this._client.subscriptionIdHex; }
/**
* Get the mining address of the client that submitted the share.
* @returns {string}
*/
get minerAddress() { return this._client.minerAddress; }
/**
* Get the share difficulty.
* This value is not available until it is calculated in the #validate function.
* @returns {number}
*/
get shareDiff() { return this._shareDiff; }
/**
* Get the stratum difficulty of the share.
* @returns {number}
*/
get stratumDiff() { return this._stratumDiff; }
/**
* Get the expected blocks of the share.
* This value is not available until it is calculated in the #validate function.
* @returns {number}
*/
get expectedBlocks() { return this._expectedBlocks; }
/**
* Determine if the share is a valid block.
* This value is not available until it is calculated in the #validate function.
* @returns {boolean}
*/
get isValidBlock() { return this._isValidBlock; }
set isValidBlock(isValid) {
precon.boolean(isValid, 'isValidBlock');
this._isValidBlock = isValid;
}
/**
* Determine if the share is valid.
* This value is not available until it is calculated in the #validate function.
* @returns {boolean}
*/
get isValidShare() { return this._isValidShare; }
/**
* Determine if the share as a block was accepted by the coin daemon.
* This value is not available until the block transaction id is set via the #blockTxId property.
* @returns {boolean}
*/
get isBlockAccepted() { return this._isBlockAccepted; }
/**
* Get the stratum error of the share.
* This value is not available until it is calculated in the #validate function.
* @returns {null|StratumError}
*/
get error() { return this._error; }
/**
* Get the block hex data to submit to the coin daemon.
* This value is not available until it is calculated in the #validate function and only when the share is a valid
* block.
* @returns {null|string}
*/
get blockHex() { return this._blockHex; }
/**
* Get the block ID of the valid block.
* This value is not available until it is calculated in the #validate function and only when the share is a valid
* block.
* @returns {null|string}
*/
get blockId() { return this._blockId; }
/**
* Get the block coinbase transaction ID.
* This value is only available when it is externally set.
* @returns {null|string}
*/
get blockTxId() { return this._blockTxId; }
set blockTxId(txId) {
precon.opt_string(txId, 'blockTxId');
this._blockTxId = txId;
this._isBlockAccepted = !!txId;
}
/**
* Get share nonce as a Buffer
* @returns {Buffer}
*/
get nonceHex() { return this._nonceHex; }
/**
* Get client extraNonce1 as a Buffer
* @returns {Buffer}
*/
get extraNonce1Hex() { return this._client.extraNonce1Hex; }
/**
* Get share nonce as a Buffer
* @returns {Buffer}
*/
get nonceBuf() { return this._nonceBuf; }
/**
* Get header hash
* @returns {Buffer}
*/
get progpowHashBuf() { return this._progpowHashBuf; }
/**
* Get Mix hash
* @returns {Buffer}
*/
get mixHashBuf() { return this._mixHashBuf; }
/**
* Validate the share.
*
* @returns {boolean} True if validated. False if the share is invalid.
*/
validate() {
const _ = this;
if (mu.isBoolean(_._isValidShare))
return _._isValidShare;
// check worker name mismatch
if (_._workerName !== _._client.workerName)
return _._setError(StratumError.UNAUTHORIZED_WORKER);
_._job = _._stratum.jobManager.validJobsOMap[_._jobIdHex];
// check valid job
if (!_._job)
return _._setError(StratumError.STALE_SHARE);
// check duplicate share
if (_._isDuplicateShare())
return false;
// check nonce size
if (_._isInvalidNonceSize())
return false;
// check nonce prefix
if (_._isInvalidNoncePrefix())
return false;
if (_._isInvalidHashHeaderSize())
return false;
if (_._isInvalidMixHeaderSize())
return false;
const progpowHashBuf = _._job.getProgPowHashBuf(_._client);
if (_._isHeaderMismatched(progpowHashBuf, _._progpowHashBuf))
return false;
const isValid = algorithm.verify(
/* progpow hash */ progpowHashBuf,
/* nonce */ _._nonceBuf,
/* height */ _._job.height,
/* mix hash */ _._mixHashBuf,
/* hash output */ HASH_OUT_BUFFER);
if (!isValid)
return _._setError(StratumError.PROGPOW_VERIFY_FAILED);
// check valid block
const hashBi = bi.fromBufferBE(HASH_OUT_BUFFER);
_._shareDiff = algorithm.diff1 / Number(hashBi) * algorithm.multiplier;
_._isValidBlock = _._job.targetBi >= hashBi;
if (_._isValidBlock) {
_._blockHex = _._serializeBlock().toString('hex');
_._blockId = _._job.getHeaderHashBuf(_).toString('hex');
console.log(`Winning nonce submitted: ${_._blockId}`);
}
// check low difficulty
if (!_._error && _._isLowDifficulty())
return false;
// calculate expected blocks
if (_._isValidShare !== false)
_._expectedBlocks = _._calculateExpectedBlocks();
return mu.isBoolean(_._isValidShare)
? _._isValidShare
: (_._isValidShare = true);
}
toJSON() {
const _ = this;
return {
jobId: _.jobIdHex,
jobHeight: _.jobHeight,
submitTime: _.submitTime,
subscriptionIdHex: _.subscriptionIdHex,
minerAddress: _.minerAddress,
workerName: _._workerName,
shareDiff: _.shareDiff,
stratumDiff: _.stratumDiff,
expectedBlocks: _.expectedBlocks,
isValidBlock: _.isValidBlock,
isValidShare: _.isValidShare,
isBlockAccepted: _.isBlockAccepted,
nonceHex: _.nonceHex,
error: _.error,
blockHex: _.blockHex,
blockId: _.blockId,
blockTxId: _.blockTxId
};
}
_calculateExpectedBlocks() {
const _ = this;
return _._stratumDiff / _._job.pDiff;
}
_isInvalidNonceSize() {
const _ = this;
if (_._nonceBuf.length !== NONCE_SIZE) {
_._setError(StratumError.INCORRECT_NONCE_SIZE);
return true;
}
return false;
}
_isDuplicateShare() {
const _ = this;
if (!_._job.registerShare(_)) {
_._setError(StratumError.DUPLICATE_SHARE);
return true;
}
return false;
}
_isInvalidNoncePrefix() {
const _ = this;
const prefixBuf = buffers.hexToLE(_.client.extraNonce1Hex);
const prefix2Buf = _._nonceBuf.slice(-prefixBuf.length);
if (Buffer.compare(prefixBuf, prefix2Buf) !== 0) {
_._setError(StratumError.INCORRECT_NONCE_PREFIX);
return true;
}
return false;
}
_isInvalidHashHeaderSize() {
const _ = this;
if (_._progpowHashBuf.length !== PROGPOW_HASH_SIZE) {
_._setError(StratumError.PROGPOW_INCORRECT_HEADER_HASH_SIZE);
return true;
}
return false;
}
_isInvalidMixHeaderSize() {
const _ = this;
if (_._mixHashBuf.length !== MIX_HASH_SIZE) {
_._setError(StratumError.PROGPOW_INCORRECT_MIX_HASH_SIZE);
return true;
}
return false;
}
_isHeaderMismatched(headerHashBuf, shareHeaderHashBuf) {
const _ = this;
if (headerHashBuf.compare(shareHeaderHashBuf) !== 0) {
_._setError(StratumError.PROGPOW_HEADER_HASH_MISMATCH);
return true;
}
return false;
}
_isLowDifficulty() {
const _ = this;
const diffFactor = _._shareDiff / _._stratumDiff;
if (diffFactor < 0.999) {
_._setError(StratumError.LOW_DIFFICULTY);
return true;
}
return false;
}
_setError(error) {
precon.notNull(error, 'error');
const _ = this;
_._error = error;
return _._isValidShare = false;
}
_serializeBlock() {
const _ = this;
const header = _._job.serializeHeader(_);
return Buffer.concat([
/* header */ header.buffer,
/* nonce */ _._nonceBuf,
/* mix hash */ buffers.reverseBytes(_._mixHashBuf, MIX_HASH_BUFFER),
/* transaction len */ buffers.packVarInt(_._job.blockTemplate.transactions.length + 1/* +coinbase */),
/* coinbase tx */ header.coinbaseBuf,
/* transactions */ _._job.txDataBuf
]);
}
}
module.exports = Share;