-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectionPool.js
More file actions
106 lines (90 loc) · 2.71 KB
/
connectionPool.js
File metadata and controls
106 lines (90 loc) · 2.71 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
const net = require('net');
const buffer = require('buffer');
const EventEmitter = require('events');
const uuidv4 = require('uuid/v4');
let BigNumber = require('bignumber.js');
const commands = require('./commands');
class Connection extends EventEmitter {
constructor(options) {
super();
this.host = options.host || null;
this.port = options.port || null;
this.username = options.username || '';
this.socket = null;
this.status = 0;
this.id = 1;
this.secret = null;
this.RPCcounter = 1;
this.currentJob = null;
this.jobCheck = [];
this.nonce = 0;
this.on('found', this.handleSubmitShare.bind(this));
}
handleSubmitShare(job, userId) {
job.id = userId; //use workers UUID to track share submission
job.params.id = this.secret; //use the connections secret ID to submit
this.jobCheck[userId] = true; //record which job has been submitted by who
this.sendToPool(job);
}
get nextNonce() {
this.nonce += 1;
return this.nonce;
}
get myJob() {
return JSON.parse(JSON.stringify(this.currentJob));
}
connect() {
this.socket = net.connect(+this.port, this.host);
this.socket.on('data', this.handlePoolMessage.bind(this));
this.sendToPool(commands.pool.login(this.username, this.id));
}
sendToPool(message) {
if(this.socket && this.socket.write) {
this.socket.write(JSON.stringify(message) + "\n");
}
}
subscribe() {
this.sendToPool(commands.pool.subscribe(this.rpcCount));
}
handlePoolMessage(data) {
let message = data;
try {
message = JSON.parse(data);
} catch (e) {
console.warn(`can't parse message as JSON from miner:`, data, e.message);
return;
}
if(message.id === this.id) {
//Pool just logged in
this.nonce = 0;
this.secret = message.result.id;
this.currentJob = message.result.job;
this.emit("job", this.myJob);
}
if(message.id) {
if(message.error) {
console.error('Error: Probably duplicate share', message);
this.sendToPool(commands.pool.login(this.username, this.id));
this.emit('shareValidated'+message.id, false);
}
if(message.result && message.result.status === 'OK') {
this.emit('shareValidated'+message.id)
this.jobCheck[message.id] = false;
}
}
switch(message.method) {
case "job": {
this.nonce = 0;
this.currentJob = message.params;
this.emit("job", this.myJob);
break;
}
case "keepalived": {
console.log('KEEP ALIVE DEBUG ME', message.params.id, new Error())
this.secret = message.params.id;
break;
}
}
}
}
module.exports = Connection