-
Notifications
You must be signed in to change notification settings - Fork 1
/
Pool.js
199 lines (152 loc) · 6.18 KB
/
Pool.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
const { logger } = require('./utils.js');
var net = require('net');
const JSONbig = require("json-bigint");
const MessageBuffer = require('./MessageBuffer.js');
let clientReceived = new MessageBuffer("\n");
class Pool {
constructor(workers, config) { // Constructor
this.config = config;
this.socket = new net.Socket();
this.socket.job;
this.socket.lastjob = 0;
this.socket.difficulty = 0;
this.socket.loginResponce;
this.socket.isLoggedIn = false;
this.socket.workerSockets = {};
this.socket.workers = workers;
this.socket.setEncoding("utf8");
let job = "";
this.socket.on('close', function(e ) {
this.isLoggedIn = false;
delete this.job;
delete this.loginResponce;
logger('pool', ['Connection closed']);
process.exit(1);
});
this.socket.on('error', function(e) {
this.isLoggedIn = false;
delete this.job;
delete this.loginResponce;
logger('pool', ['Error connecting to node. Is node running?']);
process.exit(1);
});
this.socket.on('data', function(buffer) {
clientReceived.push(buffer);
while (!clientReceived.isFinished()) {
const message = clientReceived.handleData()
const jsonRPCResponse = JSONbig.parse(message);
switch (jsonRPCResponse.method) {
case 'submit':
logger('debug', ['pool:submit', jsonRPCResponse]);
if(this.isLoggedIn){
let workerId = jsonRPCResponse.id;
jsonRPCResponse.id = this.workerSockets[workerId] ? this.workerSockets[workerId].originRpcId : 0;
if(jsonRPCResponse.result && ((typeof jsonRPCResponse.result == 'string' && jsonRPCResponse.result == 'ok') || (typeof jsonRPCResponse.result == 'object') && jsonRPCResponse.result.status == 'ok' )){
this.workers.getWorker(workerId).countShare();
}else{
this.workers.getWorker(workerId).countReject();
}
//
if(this.workerSockets[workerId]){
this.workerSockets[workerId].write(JSONbig.stringify(jsonRPCResponse).toString('utf8') +"\n");
}
}
break;
case 'status':
logger('debug', ['pool:status', jsonRPCResponse]);
if(this.isLoggedIn){
let workerId = jsonRPCResponse.id;
jsonRPCResponse.id = this.workerSockets[workerId].originRpcId;
this.workerSockets[workerId].write(JSONbig.stringify(jsonRPCResponse).toString('utf8') +"\n");
}
break;
case 'login':
logger('debug', ['pool:login', jsonRPCResponse]);
//xmrig login result
if(jsonRPCResponse.result && ((typeof jsonRPCResponse.result == 'string' && jsonRPCResponse.result == 'ok') || (typeof jsonRPCResponse.result == 'object') && jsonRPCResponse.result.status == 'OK' )){
//if not logged in as soon as possible then epic server is flooded
//if real login fails then we get this in responce later
this.isLoggedIn = true;
let workerId = jsonRPCResponse.id;
if(workerId && this.workerSockets[workerId]){
jsonRPCResponse.id = this.workerSockets[workerId].originRpcId;
this.loginResponce = jsonRPCResponse;
this.workers.getWorker(workerId).isLoggedIn = true;
this.workerSockets[workerId].write(JSONbig.stringify(jsonRPCResponse).toString('utf8') +"\n");
}
}
break;
case 'job':
//xmrig job result
logger('debug', ['pool:job', jsonRPCResponse]);
if(this.isLoggedIn){
if(this.job != jsonRPCResponse){
this.lastjob = Math.round(+new Date()/1000);
this.job = jsonRPCResponse;
}
let newjob = Math.round(+new Date()/1000);
if((newjob - this.lastjob) > 60){
process.exit(1);
}
for(const workerSocket in this.workerSockets){
let worker = this.workerSockets[workerSocket];
this.workers.getWorker(worker.id).countJob();
this.workers.getWorker(worker.id).setJobTemplate(this.job);
worker.write(JSONbig.stringify(this.job).toString('utf8') +"\n");
}
}
break;
case 'getjobtemplate':
logger('debug', ['pool:getjobtemplate', jsonRPCResponse]);
if(this.isLoggedIn){
let workerId = jsonRPCResponse.id;
if(workerId && this.workerSockets[workerId]){
jsonRPCResponse.id = this.workerSockets[workerId].originRpcId;
this.workerSockets[workerId].write(JSONbig.stringify(jsonRPCResponse).toString('utf8') +"\n");
}
}
break;
default:
//console.log('unknown pool method response', jsonRPCResponse);
let workerId = jsonRPCResponse.id;
jsonRPCResponse.id = this.workerSockets[workerId].originRpcId;
this.workerSockets[workerId].write(JSONbig.stringify(jsonRPCResponse).toString('utf8') +"\n");
break
}
}
});
}
async connect(){
this.socket.connect(this.config.port, this.config.host, function() {
});
}
closeConnection(){
this.socket.destroy();
}
write(jsonRequest){
logger('debug', ['pool:write jsonRequest', jsonRequest]);
this.socket.write(JSONbig.stringify(jsonRequest).toString('utf8') +"\n");
}
getLoginResponce(){
return this.socket.loginResponce;
}
setLoggedIn(){
this.socket.isLoggedIn = true;
}
isLoggedIn(){
return this.socket.isLoggedIn;
}
addWorkerSocket(socketId, workerSocket){
this.socket.workerSockets[socketId] = workerSocket;
}
getWorkerSocket(socketId){
return this.socket.workerSockets[socketId];
}
deleteWorkerSocket(socketId){
delete this.socket.workerSockets[socketId];
}
getAlgos(){
return this.config.algos;
}
}
module.exports = Pool;