-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmining-result-stratum.js
108 lines (94 loc) · 3.15 KB
/
mining-result-stratum.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
/* streams */
const Stream = require('stream');
/* require stratum submission */
const StratumSubmission = require('./stratum-submission');
/* mining job */
const MiningJob = require('./mining-job');
/* mining result */
const MiningResult = require('./mining-result');
/* double sha used for hashing */
const doubleSHA = require('./double-sha');
/* used for comparison */
const BigInt = require('big-integer');
/* reverse byte array */
const reverse = require('./reverse');
/* stream class that takes MiningResult and passes it further if it
* matches the network requiremenst */
class MiningResultStratum extends Stream.Duplex
{
constructor()
{
/* call event emitter class constructor */
super({objectMode: true});
}
/* stream.duplex write function */
_write(chunk, encoding, callback)
{
/* array of valid nonces */
var validNonces = [];
/* validate type */
if (!(chunk.miningJob instanceof MiningJob))
return callback(new TypeError("input must contain " +
"MiningJob type"));
/* validate type */
if (!(chunk.miningResult instanceof MiningResult))
return callback(new TypeError("input must contain " +
"MiningResult type"));
/* test for proper hashes */
for (var i = 0; i < chunk.miningResult.nonces.length; i++) {
/* invalid result? */
if (!this._validResult(chunk.miningJob.data,
chunk.miningResult.nonces[i],
chunk.miningJob.target))
continue;
/* push valid nonce */
validNonces.push(chunk.miningResult.nonces[i]);
}
/* found anything? */
if (validNonces.length != 0) {
/* store valid nonces */
chunk.stratumSubmission = new StratumSubmission({
nonces : validNonces
});
/* push into readable part */
this.push(chunk);
}
/* we are done processing */
callback();
}
/* stream that was pushing stratum notifications has been closed? */
_final()
{
/* terminate our string */
this.push(null);
}
/* stream.duplex read function */
_read(size)
{
}
/* returns true if given block (with nonce) results in a hahses to
* value less than 'target' */
_validResult(blockHeader, nonce, target)
{
/* convert to byte array */
var bh = blockHeader.slice(0, 76 * 2);
/* append nonce value */
bh += nonce;
/* change endiannes */
bh = Buffer.from(bh, 'hex').swap32();
/* do the hashing */
var hash = doubleSHA(bh);
/* reverse bytes */
hash = reverse(hash).toString('hex');
var h = hash;
/* build the big nums */
hash = BigInt(hash, 16);
target = BigInt(target, 16);
/* check hash against current target */
var validFound = target.compare(hash) > 0;
/* compare */
return validFound;
}
}
/* export class */
module.exports = MiningResultStratum;