Repository navigation
Expand file tree
/
Copy pathpool.js
More file actions
303 lines (262 loc) · 11.6 KB
/
Copy pathpool.js
File metadata and controls
303 lines (262 loc) · 11.6 KB
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
const events = require('events');
const Redis = require('ioredis');
const varDiff = require('./varDiff.js');
const daemon = require('./daemon.js');
const stratum = require('./stratum.js');
const { JobManager } = require('./jobManager.js');
const constants = require('./constants.js');
const ShareProcessor = require('./shareProcessor.js');
const PaymentProcessor = require('./paymentProcessor.js');
const Stats = require('./stats.js');
var pool = module.exports = function pool(config, logger){
var topicNameClient = "workers-events";
var topicNameShares = "shares-events";
this.redisClient = new Redis(config.redis.port, config.redis.host);
this.config = config;
var _this = this;
var jobExpiryPeriod = config.jobExpiryPeriod * 1000; // ms
this.start = function(){
SetupVarDiff();
SetupDaemonInterface(function(){
SetupJobManager();
OnBlockchainSynced(function(){
StartShareProcessor();
StartPaymentProcessor();
StartStatsReport();
StartStratumServer();
});
});
};
function OnBlockchainSynced(syncedCallback){
var checkSynced = function(displayNotSynced){
_this.daemon.isSynced(function(synced){
if (synced){
syncedCallback();
}
else{
if (displayNotSynced) displayNotSynced();
setTimeout(checkSynced, 5000);
}
});
};
checkSynced(function(){
//Only let the first fork show synced status or the log wil look flooded with it
if (!process.env.forkId || process.env.forkId === '0')
logger.info('Daemon is still syncing with network (download blockchain) - server will be started once synced');
});
}
function SetupVarDiff(){
_this.varDiff = new varDiff(config.pool.varDiff);
_this.varDiff.on('newDifficulty', function(client, newDiff) {
/* We request to set the newDiff @ the next difficulty retarget
(which should happen when a new job comes in - AKA BLOCK) */
client.enqueueNextDifficulty(newDiff);
});
}
function SetupJobManager(){
_this.jobManager = new JobManager(jobExpiryPeriod);
_this.jobManager.on('newJobs', function(templates){
//Check if stratumServer has been initialized yet
if (_this.stratumServer) {
_this.stratumServer.broadcastMiningJobs(templates);
}
}).on('share', function(shareData){
if (shareData.error){
// we only emit valid shares
logger.error('Invalid share from ' + shareData.worker +
', error: ' + shareData.error +
', jobId: ' + shareData.job +
', ip: ' + shareData.ip
);
return;
}
var job = shareData.job;
var chainIndex = chainIndexStr(job.fromGroup, job.toGroup);
logger.info('Received share from ' + shareData.worker +
', jobId: ' + job.jobId +
', chainIndex: ' + chainIndex +
', pool difficulty: ' + shareData.difficulty +
', share difficulty: ' + shareData.shareDiff +
', ip: ' + shareData.ip
);
_this.redisClient.publish(topicNameShares,JSON.stringify({
worker: shareData.worker,
workerAddr: shareData.workerAddress,
jobId: job.jobId,
fromGroup: job.fromGroup,
toGroup: job.toGroup,
pool_difficulty: shareData.difficulty,
share_difficulty: shareData.shareDiff,
ip: shareData.ip
}))
_this.shareProcessor.handleShare(shareData);
if (shareData.foundBlock){
logger.info('Found block for chainIndex: ' + chainIndex +
', hash: ' + shareData.blockHash +
', miner: ' + shareData.worker
);
var block = Buffer.concat([shareData.nonce, job.headerBlob, job.txsBlob]);
_this.daemon.submit(block, function(error){
if (error) {
logger.error('Submit block error: ' + error);
}
});
}
})
}
function chainIndexStr(fromGroup, toGroup){
return fromGroup + " -> " + toGroup;
}
function SetupDaemonInterface(finishedCallback){
if (!config.daemon) {
logger.error('No daemons have been configured - pool cannot start');
return;
}
// TODO: support backup daemons
_this.daemon = new daemon.interface(config.daemon, logger);
_this.daemon.once('online', function(){
finishedCallback();
_this.daemon.connectToMiningServer(messageHandler);
}).on('cliqueNotReady', function(){
logger.info('Clique is not ready.');
}).on('error', function(message){
logger.error(message);
});
_this.daemon.init();
}
function messageHandler(message){
switch(message.type) {
case constants.JobsMessageType:
_this.jobManager.processJobs(message.payload);
break;
case constants.SubmitResultMessageType:
var result = message.payload;
handleSubmitResult(result.fromGroup, result.toGroup, result.succeed);
break;
default:
logger.error('Invalid message type: ' + message.type);
}
}
function handleSubmitResult(fromGroup, toGroup, succeed){
var chainIndex = chainIndexStr(fromGroup, toGroup);
if (succeed){
logger.info('Submit block succeed for chainIndex: ' + chainIndex);
}
else {
logger.error('Submit block failed for chainIndex: ' + chainIndex);
}
}
function StartShareProcessor(){
_this.shareProcessor = new ShareProcessor(config, logger);
_this.shareProcessor.start();
}
function StartPaymentProcessor(){
_this.paymentProcessor = new PaymentProcessor(config, logger);
_this.paymentProcessor.start();
}
function StartStatsReport(){
_this.stats = new Stats(config, logger);
_this.stats.reportStatsRegularly();
}
function StartStratumServer(){
_this.stratumServer = new stratum.Server(config);
_this.stratumServer.on('started', function(){
_this.stratumServer.broadcastMiningJobs(_this.jobManager.currentJobs);
}).on('tooManyConnectionsFromSameIP', function(ipAddress){
logger.warn('Too many connections from IP: ' + ipAddress);
}).on('client.connected', function(client){
logger.info('New miner connected: ' + client.getLabel());
_this.redisClient.publish(topicNameClient,JSON.stringify({
state: 'connected',
ip: client.remoteAddress,
port: client.remotePort
}))
_this.varDiff.manageClient(client);
client.on('submit', function(params, resultCallback){
var result =_this.jobManager.processShare(
params,
client.previousDifficulty,
client.difficulty,
client.remoteAddress,
client.socket.localPort
);
resultCallback(result.error, result.result ? true : null);
}).on('malformedMessage', function (message) {
logger.warn('Malformed message from ' + client.getLabel() + ': ' + message);
_this.redisClient.publish(topicNameClient,JSON.stringify({
state: 'malformedMessage',
ip: client.remoteAddress,
port: client.remotePort
}))
}).on('socketError', function(err) {
logger.warn('Socket error from ' + client.getLabel() + ': ' + JSON.stringify(err));
_this.redisClient.publish(topicNameClient,JSON.stringify({
state: 'socketError',
ip: client.remoteAddress,
port: client.remotePort
}))
}).on('socketTimeout', function(reason){
logger.warn('Connected timed out for ' + client.getLabel() + ': ' + reason)
_this.redisClient.publish(topicNameClient,JSON.stringify({
state: 'socketTimeout',
ip: client.remoteAddress,
port: client.remotePort
}))
}).on('socketDisconnect', function() {
logger.warn('Socket disconnected from ' + client.getLabel());
_this.redisClient.publish(topicNameClient,JSON.stringify({
state: 'socketDisconnected',
ip: client.remoteAddress,
port: client.remotePort
}))
}).on('difficultyChanged', function(difficulty){
logger.info('Set new difficulty for ' + client.getLabel() + ' to ' + difficulty);
}).on('kickedBannedIP', function(remainingBanTime){
logger.info('Rejected incoming connection from ' + client.remoteAddress + ' banned for ' + remainingBanTime + ' more seconds');
_this.redisClient.publish(topicNameClient,JSON.stringify({
state: 'kickedBannedIP',
ip: client.remoteAddress,
port: client.remotePort
}))
}).on('forgaveBannedIP', function(){
logger.info('Forgave banned IP ' + client.remoteAddress);
_this.redisClient.publish(topicNameClient,JSON.stringify({
state: 'unbanned',
ip: client.remoteAddress,
port: client.remotePort
}))
}).on('unknownStratumMethod', function(fullMessage) {
logger.error('Unknown stratum method from ' + client.getLabel() + ': ' + fullMessage.method);
_this.redisClient.publish(topicNameClient,JSON.stringify({
state: 'unknownStratumMethod',
ip: client.remoteAddress,
port: client.remotePort
}))
}).on('socketFlooded', function() {
logger.warn('Detected socket flooding from ' + client.getLabel());
const data = {
state: 'socketFlooded',
ip: client.remoteAddress,
port: client.remotePort
}
_this.redisClient.publish(topicNameClient,JSON.stringify(data))
logger.info("send to channel "+topicNameClient+": "+JSON.stringify(data))
}).on('triggerBan', function(reason){
logger.info('Banned triggered for ' + client.getLabel() + ': ' + reason);
_this.redisClient.publish(topicNameClient,JSON.stringify({
state: 'triggerBan',
ip: client.remoteAddress,
port: client.remotePort
}))
});
}).on('client.disconnected', function(client){
logger.info('Client ' + client.getLabel() + ' disconnected');
_this.redisClient.publish(topicNameClient,JSON.stringify({
state: 'disconnected',
ip: client.remoteAddress,
port: client.remotePort
}))
});
}
};
pool.prototype.__proto__ = events.EventEmitter.prototype;