forked from esenminer/js_ltc_miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
166 lines (101 loc) · 2.66 KB
/
worker.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
importScripts('./work.js');
importScripts('./hasher.js');
importScripts('./core-min.js');
importScripts('./enc-base64-min.js');
importScripts('./hmac-sha256.js');
importScripts('./json2.js');
var WORK_TIMEOUT = 60 * 1000; // ms
var Notification = {
SYSTEM_ERROR : 0,
PERMISSION_ERROR : 1,
CONNECTION_ERROR : 2,
AUTHENTICATION_ERROR : 3,
COMMUNICATION_ERROR : 4,
LONG_POLLING_FAILED : 5,
LONG_POLLING_ENABLED : 6,
NEW_BLOCK_DETECTED : 7,
NEW_WORK : 8,
POW_TRUE : 9,
POW_FALSE : 10,
TERMINATED : 11,
STARTED: 12
};
var scanTime = 5000; // ms
var retryPause = 30000; // ms
var throttleFactor = 0;
var curWork = null;
var hashes = 0;
var lastHashes = 0;
var running = false;
var nonce = 0;
var hashRateUpdater = null;
self.onmessage = function(e) {
var cmd = e.data.cmd;
if(cmd=='start') {
run();
}
if(cmd=='stop') {
stop();
}
};
// Never called because CPU usage jumps to 100% and the
// worker doesn't seem to receive stop message
function stop() {
running = false;
self.postMessage({'notification': Notification.TERMINATED});
self.postMessage({'logMessage': 'Hashes: '+hashes});
clearInterval(hashRateUpdater);
self.terminate();
}
function run() {
running = true;
self.postMessage({'notification': Notification.STARTED});
doWork();
}
function doWork() {
var hasher = new Hasher();
var lastTime = (new Date()).getTime();
/*
* Method to update the hash rate but doesn't seem
* to work because the actual hashing never yields
* long enough for this.
*
*
setInterval(function() {
var hashRate = ((lastHashes - hashes)/1024).toFixed(2);
self.postMessage({'logMessage': hashRate});
lastHashes = hashes;
}, 1000);
*/
while(running) {
if(curWork == null ) {
// Get New Work
curWork = new Work();
curWork.getWork();
nonce = 0;
self.postMessage({'notification': Notification.NEW_WORK});
} else {
if(curWork.meetsTarget(nonce, hasher, hashes)) {
//submit work
var submitResult = curWork.submit();
if(submitResult==true) {
self.postMessage({'notification': POW_TRUE});
} else {
self.postMessage({'notification': POW_FALSE});
}
self.postMessage({'workerHashes': hashes});
curWork = null;
}
nonce++;
hashes++;
if(hashes%200==0) {
var secTime = (((new Date()).getTime())-lastTime)/1000;
var hashRate = ((hashes - lastHashes)/secTime).toFixed(0);
self.postMessage({'hashRate': hashRate,
'workerHashes': hashes});
lastHashes = hashes;
lastTime = (new Date()).getTime();
}
}
}
}