-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
149 lines (128 loc) · 3.71 KB
/
worker.js
File metadata and controls
149 lines (128 loc) · 3.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
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
const EventEmitter = require('events');
const uuidv4 = require('uuid/v4');
const commands = require('./commands');
class Worker extends EventEmitter {
constructor(options) {
super();
this.id = options.id || uuidv4();
this.user = '';
this.diff = options.diff || 5000;
this.agent = '';
this.connection = options.connection || null;
this.job = options.pool.myJob || null;
this.pool = options.pool || null;
this.RPCcounter = 1;
this.pool.on("shareValidated"+this.id, this.handleShareValidated.bind(this))
this.pool.on("job", this.handleNewJob.bind(this));
}
handleShareValidated(isValid) {
if(isValid === false) {
//request new job
this.messageToMiner(commands.miner.newJob(this.uniqueJob));
} else {
this.emit('validated', this.user, this.diff);
}
this.messageToMiner(commands.miner.ok(this.RPCcounter));
}
connect() {
this.buffer = '';
this.connection.setKeepAlive(true);
this.connection.setEncoding("utf8");
this.connection.on("error", error => {
// this.kill();
});
this.connection.on("close", () => {
this.kill();
});
this.connection.on("data", chunk => {
this.buffer += chunk;
while (this.buffer.includes("\n")) {
const newLineIndex = this.buffer.indexOf("\n");
const stratumMessage = this.buffer.slice(0, newLineIndex);
this.buffer = this.buffer.slice(newLineIndex + 1);
this.handleMessageFromMiner(stratumMessage);
}
});
}
handleLogin(data) {
this.user = data.params.login;
if(this.job) {
let startCommand = commands.miner.start(this.id, this.uniqueJob);
this.messageToMiner(startCommand);
}
}
get uniqueJob() {
let nonce_range = 1;
let nonce = (this.pool.nextNonce * nonce_range).toString(16);
let replaceMe = '00000000';
let newJob = replaceMe.slice(0, (replaceMe.length - nonce.length)) + nonce;
this.job.id = this.id;
this.job.blob = this.job.blob.replace(replaceMe, newJob);
console.log(`Nonce is ${nonce}`, newJob, this.job.blob);
return this.job;
}
handleNewJob(job) {
this.job = JSON.parse(JSON.stringify(job));
this.messageToMiner(commands.miner.newJob(this.uniqueJob));
}
messageToMiner(message) {
this.RPCcounter += 1;
if(this.connection && this.connection.write) {
this.connection.write(JSON.stringify(message) + "\n");
} else {
this.kill();
}
}
handleMessageFromMiner(message) {
let data = message;
try {
data = JSON.parse(message);
} catch (e) {
console.warn(`can't parse message as JSON from miner ${this.user}:`, message, e.message);
this.kill();
return;
}
//not all clients update the rpc count. adjust accordingly
if(data.id) {
this.RPCcounter = data.id;
}
switch(data.method) {
case "login": {
this.handleLogin(data);
break;
}
case "submit": {
this.handleSubmit(data);
break;
}
case "keepalived": {
this.messageToMiner(commands.miner.ok(this.RPCcounter));
break;
}
}
}
handleSubmit(foundJob) {
if(!this.shareValidated(foundJob)) {
//@TODO handle service abuse and IP banning
return;
}
let params = foundJob.params;
params.id = this.pool.id;
let template = {
"method": "submit",
params,
"id": params.id
};
this.pool.emit('found', template, this.id);
}
//@TODO validate shares before submitting
shareValidated() {
return true;
}
kill() {
console.log(`killing connection for ${this.user}`)
this.removeAllListeners();
this.emit('kill', this.id);
}
}
module.exports = Worker;