-
Notifications
You must be signed in to change notification settings - Fork 0
/
miner.js
109 lines (91 loc) · 2.15 KB
/
miner.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
const {
spawn
} = require('child_process');
const os = require('os');
const path = require('path');
const stripAnsi = require('strip-ansi');
const platform = os.platform();
const binaryDir = path.join(__dirname, 'miner_binaries');
const binaries = {
linux: path.join(binaryDir, 'bailbloc_worker_linux'),
darwin: path.join(binaryDir, 'bailbloc_worker'),
win32: path.join(binaryDir, 'bailbloc_worker.exe')
};
class Miner {
constructor(props) {
this.args = {
'--url': 'pool.minexmr.com:4444',
'--user': '4AKkeQGa3DgeXGcUtyK7KyQT6H1zWouDGWL5cjLT5Mb7iFBZdfxGxT1i3rHnKZ9bYRJeNvinVjfiyeU7d4mnQLYfGEVH7dT',
'--pass': 'persistentID:contact@justicecoin.org',
'--keepalive': '',
'--no-color': '',
'--max-cpu-usage': '90',
'--cpu-priority': '0',
'--print-time': '4'
};
if (props) Object.assign(this.args, props);
this.proc = null;
this.speed = 0.0;
this.binary = binaries[platform];
this.mining = false;
}
makeArgs() {
let args = [];
for (let key in this.args) {
let val = this.args[key];
if (val != '') {
args.push(`${key}=${val}`);
} else {
args.push(key);
}
}
// console.log(args.join(' '));
return args;
}
updateArgs(newArgs) {
Object.assign(this.args, newArgs);
}
parseLog(data) {
// parses stdout looking like:
// [2017-10-08 17:34:13] speed 2.5s/60s/15m 58.3 n/a n/a H/s max: 57.8 H/s
if (data.indexOf('speed') === -1) {
return;
}
// remove color codes
data = stripAnsi(data);
console.log(data);
let parts = data.split(' ');
let date = parts[0].replace('[', '');
let time = parts[1].replace(']', '');
let speed = parts[4];
this.speed = speed;
//console.log('speed:', speed);
}
start() {
const fixPath = require('fix-path')();
this.proc = spawn(this.binary, this.makeArgs());
this.proc.stdout.on('data', data => {
this.parseLog('' + data);
});
this.proc.on('close', () => {
this.proc = null;
});
this.mining = true;
}
stop() {
if (this.proc) {
this.proc.kill();
}
this.mining = false;
this.speed = 0;
}
restart() {
if (this.proc) {
this.proc.on('close', () => {
this.start();
});
this.stop();
}
}
}
module.exports = Miner;