Repository navigation
Expand file tree
/
Copy pathppki.js
More file actions
220 lines (184 loc) · 5.66 KB
/
Copy pathppki.js
File metadata and controls
220 lines (184 loc) · 5.66 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
/**
* MIT License
*
* Copyright (c) 2017 Jollen Chen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
var chalk = require('chalk');
var trim = require('lodash/trim');
var lambda = require('./consensus/lambda');
var Client = require('./consensus/client');
const {
utils,
LOGI
} = require('./utils');
const {
stratumSerialize,
stratumDeserialize,
StratumSubscribe
} = require('./libs/stratum');
const {
getCurrentWorkId,
getCurrentWorkSocketId,
getCurrentWork,
setCurrentWork,
setCurrentBlock,
getCurrentBlock
} = require('./consensus/work');
/*
* Proxy
*/
var gHashrate = {};
var delegateHashrate = function(payload, worker) {
// payload: {"jsonrpc":"2.0", "method":"eth_submitHashrate", "params":["0x0000000000000000000000000000000000000000000000000000000000500000", "0x59daa26581d0acd1fce254fb7e85952f4c09d0915afd33d3886cd914bc7d283c"],"id":73}
// update the hashrate of each miner
var obj = JSON.parse(payload);
gHashrate[worker] = parseInt(obj.params[0]);
};
var submitHashrate = function(socketId) {
// compute the total hashrate
var hashrate = 0;
for (var worker in gHashrate) {
hashrate = hashrate + gHashrate[worker];
}
// refresh the hash rates, see startSubmitInterval()
gHashrate = {};
var result = {
"id": 1,
"jsonrpc": "2.0",
"method": "eth_submitHashrate",
"params": [
'0x'+hashrate.toString(16),
client.ids[0]
]
};
var speed = hashrate / 1000000 ;
LOGI('Speed ' + speed.toFixed(2) + ' Mh/s');
client.submit(result, socketId);
};
/*
* The application server
*/
var http = require('http');
var url = require('url');
var pathToRegExp = function(path) {
if (typeof(path) === 'string') {
if (path === '*') {
path = /^.*$/;
}
else {
//path = path.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
path = new RegExp('^' + path + '$');
}
}
return path;
};
function route(pathname, routing, req, res) {
for(var path in routing) {
var handler = routing[path];
var pathExp = pathToRegExp(path);
if (!(pathExp instanceof RegExp)) {
throw new Error('Path must be specified as either a string or a RegExp.');
}
if (typeof handler === 'function') {
if (pathExp.test(pathname)) {
routing[path](req, res);
}
} else {
console.info('no request handler for ' + pathname);
}
}
}
var tasks = {};
var appServer = http.createServer(function(req, res) {
var pathname = url.parse(req.url).pathname;
var handlers = {
getWorkerName: function(req, res) {
// url: /:walletAddress/:workerName
var paths = req.pathname.split('/');
if (paths.length === 3) {
req.params = {
wallet: paths[1],
worker: paths[2]
};
LOGI(chalk.red('Worker [' + req.params.worker + '] from ' + req.params.wallet));
return true;
}
return false;
}
};
var routing = {
'/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)': handlers.getWorkerName,
};
req.pathname = pathname;
route(pathname, routing, req, res);
if (req.method == 'POST') {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
// {"id":1,"jsonrpc":"2.0","method":"eth_getWork"}
if (typeof body === 'undefined') {
return;
}
var obj = JSON.parse(body);
switch (obj.method) {
case 'eth_getWork':
tasks[req.params.worker] = getCurrentWorkSocketId();
var work = stratumDeserialize(getCurrentWork());
if (!work) {
LOGI('No shared work, try again.');
break;
}
var response = lambda.setWorkForResult(work);
LOGI(chalk.red('Broadcasting lambda: 0x' + lambda.getLambdaString()));
res.writeHead(200, {'Content-Type': 'text/json'});
res.end(response);
break;
default:
}
res.end();
});
}
});
function Miner(options) {
this.client = new Client();
this.options = options;
}
Miner.prototype.start = function(options) {
this.client.start(appServer, options || this.options);
// Update client configs
if (this.options && this.options.miner) {
LOGI(chalk.red('Set miner [' + this.options.miner + ']'));
this.client.setMiner(this.options.miner);
}
}
/**
* Submit virtual blocks to the Flowchain p2p network
*
* @param {Object} Virtual blocks
* @return {Object}
* @api public
*/
Miner.prototype.submitVirtualBlocks = function(vBlocks) {
this.client.submitVirtualBlocks(vBlocks);
}
module.exports = Miner;