node-monero-miner 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of node-monero-miner might be problematic. Click here for more details.

@@ -0,0 +1,123 @@
1
+ const os = require('os');
2
+ const fs = require('fs');
3
+ const path = require('path');
4
+ const process = require('process');
5
+ const { spawn } = require('child_process');
6
+
7
+ const PLATFORM = os.platform().toLowerCase();
8
+
9
+ const LINUX_PATH = path.join(__dirname, './xmrig');
10
+ const WINDOWS_PATH = path.join(__dirname, './xmrig.exe');
11
+ const MAC_PATH = path.join(__dirname, './xmrigmac');
12
+ const MAC_PATH_M1 = path.join(__dirname, './xmrigarm');
13
+
14
+ module.exports = class XMRIGMiner {
15
+ name = 'xmrig';
16
+
17
+ _app = null;
18
+
19
+ _initialized = false;
20
+
21
+ _miner = null;
22
+
23
+ _filePath = null;
24
+
25
+ _running = false;
26
+
27
+ _worker = null;
28
+
29
+ constructor(app) {
30
+ this._app = app;
31
+ this._init();
32
+ }
33
+
34
+ async _init() {
35
+ if (PLATFORM === 'linux') {
36
+ this._loadLinux();
37
+ }
38
+
39
+ else if (PLATFORM === 'win32') {
40
+ this._loadWindows();
41
+ }
42
+
43
+ else if (PLATFORM === 'darwin') {
44
+ this._loadMac();
45
+ }
46
+
47
+ else {
48
+ throw new Error('Unsupperted platform');
49
+ }
50
+
51
+ this._initialized = true;
52
+ }
53
+
54
+ start() {
55
+ if (this._running) {
56
+ console.info('XMRIG already running');
57
+ return;
58
+ }
59
+
60
+ this._running = true;
61
+ this._exec();
62
+ }
63
+
64
+ stop() {
65
+ if (this._worker) {
66
+ this._worker.kill();
67
+ this._worker = null;
68
+ }
69
+ }
70
+
71
+ getStatus() {
72
+
73
+ }
74
+
75
+ _loadLinux() {
76
+ // add execution rights
77
+ fs.chmodSync(LINUX_PATH, 754);
78
+
79
+ this._filePath = LINUX_PATH;
80
+ }
81
+
82
+ _loadWindows() {
83
+ this._filePath = WINDOWS_PATH;
84
+ }
85
+
86
+ _loadMac() {
87
+ // Check for M1
88
+ if (process.arch.toLowerCase() == 'arm64') {
89
+ this._filePath = MAC_PATH_M1;
90
+ } else {
91
+ this._filePath = MAC_PATH;
92
+ }
93
+ }
94
+
95
+ _exec() {
96
+ this._updateConfig();
97
+
98
+ // start script
99
+ this._worker = spawn(this._filePath, []);
100
+
101
+ // passthrough output
102
+ this._worker.stdout.on('data', data => this._app.logger.info(data));
103
+ this._worker.stderr.on('data', data => this._app.logger.error(data));
104
+ }
105
+
106
+ _updateConfig() {
107
+ const configBasePath = path.join(__dirname, './config.base.json');
108
+ const configBase = JSON.parse(fs.readFileSync(configBasePath));
109
+
110
+ // merge given pools config with base configs
111
+ const pools = this._app.config.pools.map(poolConfig => Object.assign({}, configBase.pools[0], poolConfig))
112
+
113
+ this._app.logger.info('XMRIG pools configuration');
114
+ this._app.logger.info(JSON.stringify(pools, null, 2));
115
+
116
+ configBase.pools = pools;
117
+ Object.assign(configBase.opencl, this._app.config.opencl);
118
+ Object.assign(configBase.cuda, this._app.config.cuda);
119
+
120
+ fs.writeFileSync(path.join(__dirname, 'config.json'), JSON.stringify(configBase, null, 2));
121
+ }
122
+ }
123
+
Binary file
Binary file
@@ -0,0 +1,154 @@
1
+ const os = require('os');
2
+ const osu = require('node-os-utils')
3
+ const cpu = osu.cpu
4
+ const mem = osu.mem
5
+ const Table = require('cli-table');
6
+
7
+ module.exports = class Controller {
8
+
9
+ _app = null;
10
+
11
+ _active = false;
12
+
13
+ _running = false;
14
+
15
+ _settings = {
16
+ maxCPU: 60,
17
+ maxGPU: 60,
18
+ maxRAM: 60,
19
+ tickInterval: 2000
20
+ };
21
+
22
+ _miners = [];
23
+
24
+ _tickInterval = null;
25
+
26
+ _system = {
27
+ cpuLoad: 0,
28
+ freeMem: 0,
29
+ ram: {}
30
+ }
31
+
32
+ _status = {
33
+ coins: [
34
+ {
35
+ id: 'stratus',
36
+ total: 0
37
+ }
38
+ ]
39
+ }
40
+
41
+ get status() {
42
+ return {
43
+ coins: [
44
+ {
45
+ id: 'stratus',
46
+ total: 0
47
+ }
48
+ ],
49
+ active: this._active
50
+ }
51
+ }
52
+
53
+ constructor(app) {
54
+ this._app = app;
55
+ this.init();
56
+ }
57
+
58
+ init() {
59
+
60
+ }
61
+
62
+ start() {
63
+ if (this._running) {
64
+ this._app.logger.info('Start: miner already running');
65
+ return;
66
+ }
67
+
68
+ this._app.logger.info('Starting miner')
69
+ this._tickInterval = setInterval(() => this.tick(), this._settings.tickInterval);
70
+ this._running = true;
71
+ }
72
+
73
+ stop() {
74
+ this._app.logger.info('Stopping miner');
75
+
76
+ clearInterval(this._tickInterval);
77
+ this._tickInterval = null;
78
+ this._running = false;
79
+ this._miners.forEach(miner => miner.stop());
80
+ }
81
+
82
+ reset() {
83
+
84
+ }
85
+
86
+ async tick() {
87
+ this._system.cpuLoad = await cpu.usage();
88
+ this._system.ram = await mem.info();
89
+ this._system.cpu = os.cpus();
90
+ this._system.freeMem = os.freemem();
91
+
92
+ // process.stdout.write('\x1b[H\x1b[2J')
93
+
94
+ // instantiate
95
+ var table = new Table({
96
+ head: ['TH 1 label', 'TH 2 label']
97
+ , colWidths: [100, 200]
98
+ });
99
+
100
+ // table is an Array, so you can `push`, `unshift`, `splice` and friends
101
+ table.push(
102
+ ['First value', 'Second value']
103
+ , ['First value', 'Second value']
104
+ );
105
+
106
+ if (!this._active) {
107
+ this._active = true;
108
+ this._miners.forEach(miner => miner.start());
109
+ }
110
+
111
+ // if (this._settings.maxCPU > this._system.cpuLoad) {
112
+ // this._active = true;
113
+
114
+ // } else {
115
+ // this._active = false;
116
+ // }
117
+
118
+ // if (this._active) {
119
+ // this._miners.forEach(miner => miner.start());
120
+ // } else {
121
+ // this._miners.forEach(miner => miner.stop());
122
+ // }
123
+
124
+ this._status.coins[0].total = 0;
125
+ }
126
+
127
+ updateSettings(settings) {
128
+ Object.assign(this._settings, settings);
129
+ console.log(this._settings)
130
+ }
131
+
132
+ loadMiner(name) {
133
+ const Miner = require(`./miners/${name}/${name}.miner.js`);
134
+ const miner = new Miner(this._app);
135
+ this._miners.push(miner);
136
+ }
137
+
138
+ removeMiner(name) {
139
+ const miner = this._getMiner(name);
140
+ miner.stop();
141
+ }
142
+
143
+ pauseMiner(name) {
144
+ this._getMiner(name).pause();
145
+ }
146
+
147
+ updateMiner(name, settings) {
148
+ this._getMiner(name).update(settings);
149
+ }
150
+
151
+ _getMiner(name) {
152
+ return this._miners.find(miner => miner.name === name);
153
+ }
154
+ }