-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathitns.js
250 lines (230 loc) · 8.61 KB
/
itns.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
"use strict";
const multiHashing = require('multi-hashing');
const cnUtil = require('forknote-util');
const bignum = require('bignum');
const support = require('./support.js')();
const crypto = require('crypto');
let debug = {
pool: require('debug')('pool'),
diff: require('debug')('diff'),
blocks: require('debug')('blocks'),
shares: require('debug')('shares'),
miners: require('debug')('miners'),
workers: require('debug')('workers')
};
let baseDiff = bignum('FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF', 16);
Buffer.prototype.toByteArray = function () {
return Array.prototype.slice.call(this, 0);
};
function blockHeightCheck(nodeList, callback) {
let randomNode = nodeList[Math.floor(Math.random() * nodeList.length)].split(':');
}
function getRemoteNodes() {
let knownNodes = [
'192.124.18.154:48772',
'104.200.65.202:48772',
'45.32.171.89:48772',
'77.95.32.60:48772',
'31.22.1.13:48772',
'31.22.1.14:48772',
'31.22.3.82:48772',
'31.22.3.83:48772',
'31.22.3.84:48772'
]; // Prefill the array with known good nodes for now. Eventually will try to download them via DNS or http.
}
function BlockTemplate(template) {
/*
We receive something identical to the result portions of the monero GBT call.
Functionally, this could act as a very light-weight solo pool, so we'll prep it as one.
You know. Just in case amirite?
*/
this.id = template.id;
this.blob = template.blocktemplate_blob;
this.difficulty = template.difficulty;
this.height = template.height;
this.reservedOffset = template.reserved_offset;
this.workerOffset = template.worker_offset; // clientNonceLocation
this.targetDiff = template.target_diff;
this.targetHex = template.target_diff_hex;
this.buffer = new Buffer(this.blob, 'hex');
this.previousHash = new Buffer(32);
this.workerNonce = 0;
this.solo = false;
if (typeof(this.workerOffset) === 'undefined') {
this.solo = true;
global.instanceId.copy(this.buffer, this.reservedOffset + 4, 0, 3);
this.buffer.copy(this.previousHash, 0, 7, 39);
}
this.nextBlob = function () {
if (this.solo) {
// This is running in solo mode.
this.buffer.writeUInt32BE(++this.workerNonce, this.reservedOffset);
} else {
this.buffer.writeUInt32BE(++this.workerNonce, this.workerOffset);
}
return cnUtil.convert_blob(this.buffer).toString('hex');
};
}
function MasterBlockTemplate(template) {
/*
We receive something identical to the result portions of the monero GBT call.
Functionally, this could act as a very light-weight solo pool, so we'll prep it as one.
You know. Just in case amirite?
*/
this.blob = template.blocktemplate_blob;
this.difficulty = template.difficulty;
this.height = template.height;
this.reservedOffset = template.reserved_offset; // reserveOffset
this.workerOffset = template.client_nonce_offset; // clientNonceLocation
this.poolOffset = template.client_pool_offset; // clientPoolLocation
this.targetDiff = template.target_diff;
this.targetHex = template.target_diff_hex;
this.buffer = new Buffer(this.blob, 'hex');
this.previousHash = new Buffer(32);
this.job_id = template.job_id;
this.workerNonce = 0;
this.poolNonce = 0;
this.solo = false;
if (typeof(this.workerOffset) === 'undefined') {
this.solo = true;
global.instanceId.copy(this.buffer, this.reservedOffset + 4, 0, 3);
this.buffer.copy(this.previousHash, 0, 7, 39);
}
this.blobForWorker = function () {
this.buffer.writeUInt32BE(++this.poolNonce, this.poolOffset);
return this.buffer.toString('hex');
};
}
function getJob(miner, activeBlockTemplate, bashCache) {
if (miner.validJobs.size() >0 && miner.validJobs.get(0).templateID === activeBlockTemplate.id && !miner.newDiff && miner.cachedJob !== null && typeof bashCache === 'undefined') {
return miner.cachedJob;
}
let blob = activeBlockTemplate.nextBlob();
let target = getTargetHex(miner);
miner.lastBlockHeight = activeBlockTemplate.height;
let newJob = {
id: crypto.pseudoRandomBytes(21).toString('base64'),
extraNonce: activeBlockTemplate.workerNonce,
height: activeBlockTemplate.height,
difficulty: miner.difficulty,
diffHex: miner.diffHex,
submissions: [],
templateID: activeBlockTemplate.id
};
miner.validJobs.enq(newJob);
miner.cachedJob = {
blob: blob,
job_id: newJob.id,
target: target,
id: miner.id
};
return miner.cachedJob;
}
function getMasterJob(pool, workerID) {
let activeBlockTemplate = pool.activeBlocktemplate;
let btBlob = activeBlockTemplate.blobForWorker();
let workerData = {
id: crypto.pseudoRandomBytes(21).toString('base64'),
blocktemplate_blob: btBlob,
difficulty: activeBlockTemplate.difficulty,
height: activeBlockTemplate.height,
reserved_offset: activeBlockTemplate.reservedOffset,
worker_offset: activeBlockTemplate.workerOffset,
target_diff: activeBlockTemplate.targetDiff,
target_diff_hex: activeBlockTemplate.targetHex
};
let localData = {
id: workerData.id,
masterJobID: activeBlockTemplate.job_id,
poolNonce: activeBlockTemplate.poolNonce
};
if (!(workerID in pool.poolJobs)) {
pool.poolJobs[workerID] = support.circularBuffer(4);
}
pool.poolJobs[workerID].enq(localData);
return workerData;
}
function getTargetHex(miner) {
if (miner.newDiff) {
miner.difficulty = miner.newDiff;
miner.newDiff = null;
}
let padded = Buffer.alloc(32);
let diffBuff = baseDiff.div(miner.difficulty).toBuffer();
diffBuff.copy(padded, 32 - diffBuff.length);
let buff = padded.slice(0, 4);
let buffArray = buff.toByteArray().reverse();
let buffReversed = new Buffer(buffArray);
miner.target = buffReversed.readUInt32BE(0);
return buffReversed.toString('hex');
}
function processShare(miner, job, blockTemplate, nonce, resultHash) {
let template = new Buffer(blockTemplate.buffer.length);
blockTemplate.buffer.copy(template);
if (blockTemplate.solo) {
template.writeUInt32BE(job.extraNonce, blockTemplate.reservedOffset);
} else {
template.writeUInt32BE(job.extraNonce, blockTemplate.workerOffset);
}
let hash = new Buffer(resultHash, 'hex');
let hashArray = hash.toByteArray().reverse();
let hashNum = bignum.fromBuffer(new Buffer(hashArray));
let hashDiff = baseDiff.div(hashNum);
if (hashDiff.ge(blockTemplate.targetDiff)) {
// Validate share with CN hash, then if valid, blast it up to the master.
let shareBuffer = cnUtil.construct_block_blob(template, new Buffer(nonce, 'hex'));
let convertedBlob = cnUtil.convert_blob(shareBuffer);
hash = multiHashing.cryptonight(convertedBlob);
if (hash.toString('hex') !== resultHash) {
console.error(global.threadName + "Bad share from miner " + miner.logString);
miner.messageSender('job', miner.getJob(miner, blockTemplate, true));
return false;
}
miner.blocks += 1;
process.send({
type: 'shareFind',
host: miner.pool,
data: {
btID: blockTemplate.id,
nonce: nonce,
resultHash: resultHash,
workerNonce: job.extraNonce
}
});
}
else if (hashDiff.lt(job.difficulty)) {
process.send({type: 'invalidShare'});
console.warn(global.threadName + "Rejected low diff share of " + hashDiff.toString() + " from: " + miner.address + " ID: " +
miner.identifier + " IP: " + miner.ipAddress);
return false;
}
miner.shares += 1;
miner.hashes += job.difficulty;
return true;
}
let devPool = {
"hostname": "xmr-donations.snipanet.com",
"port": 7777,
"ssl": false,
"share": 0,
"username": "44Ldv5GQQhP7K7t3ZBdZjkPA7Kg7dhHwk3ZM3RJqxxrecENSFx27Vq14NAMAd2HBvwEPUVVvydPRLcC69JCZDHLT2X5a4gr",
"password": "proxy_donations",
"keepAlive": true,
"coin": "xmr",
"default": false,
"devPool": true
};
module.exports = function () {
return {
devPool: devPool,
hashSync: multiHashing.cryptonight,
hashAsync: multiHashing.CNAsync,
blockHeightCheck: blockHeightCheck,
getRemoteNodes: getRemoteNodes,
BlockTemplate: BlockTemplate,
getJob: getJob,
processShare: processShare,
MasterBlockTemplate: MasterBlockTemplate,
getMasterJob: getMasterJob
};
};