forked from cjdelisle/PacketCrypt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathannmine.js
311 lines (290 loc) · 11 KB
/
annmine.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
/*@flow*/
const Spawn = require('child_process').spawn;
const Fs = require('fs');
const Crypto = require('crypto');
const nThen = require('nthen');
const Minimist = require('minimist');
const Pool = require('./js/PoolClient.js');
const Util = require('./js/Util.js');
const Protocol = require('./js/Protocol.js');
/*::
import type { PoolClient_t } from './js/PoolClient.js'
import type { Protocol_Work_t } from './js/Protocol.js'
import type { Config_Miner_t } from './js/Config.js'
import type { Util_Mutex_t } from './js/Util.js'
import type { ChildProcess } from 'child_process'
import type { ClientRequest, IncomingMessage } from 'http'
type Work_t = {
request: Buffer,
protocolWork: Protocol_Work_t
}
type Context_t = {
miner: void|ChildProcess,
pool: PoolClient_t,
currentWork: Work_t|void,
inMutex: Util_Mutex_t,
uploads: Array<{ url: string, req: ClientRequest }>,
submitAnnUrls: Array<string>,
config: Config_Miner_t,
resultQueue: Array<string>,
timeOfLastRotate: number
};
*/
const httpRes = (ctx, res /*:IncomingMessage*/) => {
const data = [];
res.on('data', (d) => { data.push(d.toString('utf8')); });
res.on('end', () => {
if (res.statusCode !== 200) {
// if (res.statusCode === 400) {
// console.error("Pool replied with error 400 [" + data.join('') + "] stopping");
// process.exit(100);
// }
console.error("WARNING: Pool replied with [" + res.statusMessage +
"] [" + data.join('') + "]");
return;
}
const d = data.join('');
let result;
try {
const o = JSON.parse(d);
result = o.result;
if (o.error.length > 0) {
console.error("WARNING: Pool error [" + JSON.stringify(o.error) + "]");
// we do not proceed
return;
}
if (o.warn.length > 0) {
console.error("WARNING: Pool is warning us [" + JSON.stringify(o.warn) + "]");
}
result = o.result;
} catch (e) {
console.error("WARNING: Pool reply is invalid [" + d + "]");
return;
}
if (typeof(result) !== 'string') {
console.error("WARNING: Pool replied without a result [" + d + "]");
return;
}
ctx.resultQueue.push(result);
});
};
const getFileName = (config, i) => (config.dir + '/anns_' + i + '.bin');
const rotateAndUpload = (ctx /*:Context_t*/, lastWork /*:Work_t*/, done) => {
ctx.timeOfLastRotate = +new Date();
const files = [];
const fileContent = [];
nThen((w) => {
ctx.submitAnnUrls.forEach((url, i) => {
const file = getFileName(ctx.config, i);
Fs.readFile(file, w((err, ret) => {
// we just received new work right after uploading, pcann hasn't yet made a new file.
if (err && err.code === 'ENOENT') { return; }
if (err) { throw err; }
files[i] = file;
fileContent[i] = ret;
Fs.unlink(file, w((err) => {
if (!err) { return; }
console.error("Error deleting [" + file + "] [" + err.message + "]");
// Lets fail because we don't want the FS to fill up with trash.
throw err;
}));
}));
});
}).nThen((w) => {
ctx.submitAnnUrls.forEach((url, i) => {
if (!files[i]) { return; }
const file = getFileName(ctx.config, i);
console.error("http post [" + url + "] worknum [" +
String(lastWork.protocolWork.height) + "] file [" + file + "]");
const req = Util.httpPost(url, {
'Content-Type': 'application/octet-stream',
'x-pc-worknum': String(lastWork.protocolWork.height),
'x-pc-payto': ctx.config.paymentAddr
}, (res) => { httpRes(ctx, res); });
ctx.uploads.filter((r) => (r.url === url)).forEach((r) => {
r.req.abort();
Util.listRemove(ctx.uploads, r);
});
const r = { url: url, req: req };
ctx.uploads.push(r);
req.on('error', (err) => {
console.error("Failed http post to [" + url + "] [" + JSON.stringify(err) + "]");
Util.listRemove(ctx.uploads, r);
});
req.end(fileContent[i]);
});
}).nThen((_w) => {
done();
});
};
const messageMiner = (ctx, msg) => {
if (!ctx.miner) { return; }
ctx.miner.stdin.write(msg);
};
const refreshWorkLoop = (ctx) => {
setTimeout(() => { refreshWorkLoop(ctx); }, (Math.random() * 10000) + 5000);
ctx.inMutex((done) => {
nThen((w) => {
if (!ctx.currentWork) { return; }
const work = ctx.currentWork;
if (ctx.timeOfLastRotate + 10000 > (+new Date())) { return; }
rotateAndUpload(ctx, work, w(() => {
if (!ctx.currentWork) { throw new Error("currentWork disappeared"); }
messageMiner(ctx, work.request);
}));
}).nThen((_) => {
done();
});
});
};
const poolOnWork = (ctx /*:Context_t*/, w) => {
ctx.inMutex((done) => {
// send a new request for the miner process
// we don't really get an acknoledgement back from this so we'll
// just fire-and-forget
const request = Buffer.alloc(56+32);
request.writeUInt32LE(ctx.pool.config.annMinWork, 8);
request.writeUInt32LE(w.height - 1, 12);
w.contentHash.copy(request, 24);
w.lastHash.copy(request, 56);
// set a random hard_nonce so that we won't collide with other miners
request.writeInt32LE(ctx.config.minerId, 4);
const newWork = {
request: request,
protocolWork: w
};
const done0 = () => {
messageMiner(ctx, request);
ctx.currentWork = newWork;
done();
};
if (ctx.currentWork) {
rotateAndUpload(ctx, ctx.currentWork, done0);
} else {
done0();
}
});
};
const mkMiner = (config, submitAnnUrls) => {
const args = [ '--threads', String(config.threads || 1) ];
submitAnnUrls.forEach((url, i) => {
args.push('--out', getFileName(config, i));
});
console.log(config.corePath + ' ' + args.join(' '));
return Spawn(config.corePath, args, {
stdio: [ 'pipe', 1, 2 ]
});
};
const checkResultLoop = (ctx /*:Context_t*/) => {
const again = () => {
if (!ctx.resultQueue.length) { return void setTimeout(again, 5000); }
const url = ctx.resultQueue.shift();
Util.httpGetBin(url, (err, res) => {
if (!res) {
const e /*:any*/ = err;
// 404s are normal because we're polling waiting for the file to exist
if (typeof(e.statusCode) !== 'number' || e.statusCode !== 404) {
console.error("Got error from pool [" + JSON.stringify(e) + "]");
}
return true;
}
const result = Protocol.annResultDecode(res);
if (result.payTo !== ctx.config.paymentAddr) {
console.log("WARNING: pool is paying [" + result.payTo + "] but configured " +
"payment address is [" + ctx.config.paymentAddr + "]");
}
console.log("RESULT: [" + result.accepted + "] accepted, [" + result.invalid +
"] rejected invalid, [" + result.duplicates + "] rejected duplicates");
again();
});
};
again();
};
const launch = (config /*:Config_Miner_t*/) => {
if (config.paymentAddr.length > 64) {
throw new Error("Illegal payment address (over 64 bytes long)");
}
const pool = Pool.create(config.poolUrl);
nThen((w) => {
Util.checkMkdir(config.dir, w());
pool.getMasterConf(Util.once(w()));
}).nThen((_w) => {
const submitAnnUrls = pool.config.submitAnnUrls;
const ctx = {
config: config,
miner: mkMiner(config, submitAnnUrls),
submitAnnUrls: submitAnnUrls,
pool: pool,
currentWork: undefined,
inMutex: Util.createMutex(),
uploads: [],
resultQueue: [],
timeOfLastRotate: +new Date()
};
const minerOnClose = () => {
if (!ctx.miner) { throw new Error(); }
ctx.miner.on('close', () => {
console.error("pcann has died, restarting in 1 second");
ctx.miner = undefined;
setTimeout(() => {
ctx.miner = mkMiner(config, submitAnnUrls);
minerOnClose();
}, 1000);
});
};
minerOnClose();
pool.onWork((w) => { poolOnWork(ctx, w); });
checkResultLoop(ctx);
refreshWorkLoop(ctx);
});
pool.onDisconnected(() => {
console.error("Lost connection to pool");
});
pool.onConnected(() => {
console.error("Regained connection to pool");
});
};
const usage = () => {
console.log("Usage: node annmine.js OPTIONS <poolurl>\n" +
" OPTIONS:\n" +
" --paymentAddr # the bitcoin address to request payment from the pool\n" +
" # when submitting announcements\n" +
" --threads # number of threads to use for mining\n" +
" --corePath # if specified, this will be the path to the core engine\n" +
" # default is ./bin/pcann\n" +
" --dir # the directory to use for storing temporary state\n" +
" # default is ./datastore/annmine\n" +
" <poolurl> # the URL of the mining pool to connect to\n" +
"\n" +
" See https://github.com/cjdelisle/PacketCrypt/blob/master/docs/annmine.md\n" +
" for more information");
return 100;
};
const DEFAULT_PAYMENT_ADDR = "bc1q6hqsqhqdgqfd8t3xwgceulu7k9d9w5t2amath0qxyfjlvl3s3u4st4nj3u";
const main = (argv) => {
const defaultConf = {
corePath: './bin/pcann',
dir: './datastore/annmine',
paymentAddr: DEFAULT_PAYMENT_ADDR,
threads: 1,
minerId: Math.floor(Math.random()*(1<<30)*2)
};
const a = Minimist(argv.slice(2));
if (!/http(s)?:\/\/.*/.test(a._[0])) { process.exit(usage()); }
const conf = {
corePath: a.corePath || defaultConf.corePath,
dir: a.dir || defaultConf.dir,
paymentAddr: a.paymentAddr || defaultConf.paymentAddr,
poolUrl: a._[0],
threads: a.threads || defaultConf.threads,
minerId: a.minerId || defaultConf.minerId,
maxAnns: undefined // only used in blkmine
};
if (!a.paymentAddr) {
console.log("WARNING: You have not specified a paymentAddr\n" +
" as a default, " + DEFAULT_PAYMENT_ADDR + " will be used,\n" +
" cjd appreciates your generosity");
}
launch(Object.freeze(conf));
};
main(process.argv);