forked from esenminer/js_ltc_miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminer.js
126 lines (81 loc) · 3.02 KB
/
miner.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
function Miner() {
var self = this;
this.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
};
this.lastWorkTime = 0;
this.lastWorkHashes = 0;
// Setup new worker
worker = new Worker('worker.js');
worker.onmessage = function(e) {
var notification = e.data.notification;
var workerHashes = e.data.workerHashes;
var logMessage = e.data.logMessage;
var hashRate = e.data.hashRate;
if(hashRate!=null) {
document.getElementById('hashRate').value = hashRate;
}
if(workerHashes!=null) {
document.getElementById('workerHashes').value = workerHashes;
}
var message = '';
if(notification!=null) {
switch(notification) {
case self.Notification.SYSTEM_ERROR: message = 'System error.'; break;
case self.Notification.PERMISSION_ERROR: message = 'Permission error.'; break;
case self.Notification.CONNECTION_ERROR: message = 'Connection error, retrying in ' + retryPause/1000 + ' seconds.'; break;
case self.Notification.AUTHENTICATION_ERROR: message = 'Invalid worker username or password.'; break;
case self.Notification.COMMUNICATION_ERROR: message = 'Communication error.'; break;
case self.Notification.LONG_POLLING_FAILED: message = 'Long polling failed.'; break;
case self.Notification.LONG_POLLING_ENABLED: message = 'Long polling activated.'; break;
case self.Notification.NEW_BLOCK_DETECTED: message = 'LONGPOLL detected new block.'; break;
case self.Notification.NEW_WORK:
if (self.lastWorkTime > 0) {
var hashes = workerHashes - self.lastWorkHashes;
var speed = hashes / Math.max(1, (new Date()).getTime() - self.lastWorkTime);
message = hashes + " hashes, " + speed.toFixed(2) + " khash/s";
} else {
message = 'Started new work.';
}
self.lastWorkTime = (new Date()).getTime();
self.lastWorkHashes = workerHashes;
break;
case self.Notification.POW_TRUE: message = 'PROOF OF WORK RESULT: true (yay!!!)'; break;
case self.Notification.POW_FALSE: message = 'PROOF OF WORK RESULT: false (booooo)'; break;
case self.Notification.TERMINATED: message = 'Terminated.'; break;
case self.Notification.STARTED: message = 'Worker started.'; break;
}
}
if(message!=null
&& message!='') {
self.logger(message);
}
if(logMessage!=null && logMessage!='') {
self.logger(logMessage);
}
};
// Start Worker
this.startWorker = function() {
worker.postMessage({'cmd': 'start'});
};
// Stop Worker
this.stopWorker = function() {
worker.terminate();
};
this.logger = function(str) {
var logElement = document.getElementById('log');
logElement.innerHTML =logElement.innerHTML + "<br/>" + str;
};
}