##// END OF EJS Templates
Make pow script be a shared worker
neko259 -
r1462:dde38eed default
parent child Browse files
Show More
@@ -86,8 +86,8 b' function selectFileChoice() {'
86 $(document).ready(function() {
86 $(document).ready(function() {
87 var powDifficulty = parseInt($('body').attr('data-pow-difficulty'));
87 var powDifficulty = parseInt($('body').attr('data-pow-difficulty'));
88 if (powDifficulty > 0) {
88 if (powDifficulty > 0) {
89 var worker = new Worker($('#powScript').attr('src'));
89 var worker = new SharedWorker($('#powScript').attr('src'));
90 worker.onmessage = function(e) {
90 worker.port.onmessage = function(e) {
91 var form = $('#form');
91 var form = $('#form');
92 addHiddenInput(form, 'timestamp', e.data.timestamp);
92 addHiddenInput(form, 'timestamp', e.data.timestamp);
93 addHiddenInput(form, 'iteration', e.data.iteration);
93 addHiddenInput(form, 'iteration', e.data.iteration);
@@ -96,6 +96,7 b' function selectFileChoice() {'
96 form.submit();
96 form.submit();
97 $('.post-form-w').unblock();
97 $('.post-form-w').unblock();
98 };
98 };
99 worker.port.start();
99
100
100 var form = $('#form');
101 var form = $('#form');
101 var submitButton = form.find('input[type=submit]');
102 var submitButton = form.find('input[type=submit]');
@@ -110,7 +111,7 b' function selectFileChoice() {'
110 difficulty: parseInt($('body').attr('data-pow-difficulty')),
111 difficulty: parseInt($('body').attr('data-pow-difficulty')),
111 hasher: $('#sha256Script').attr('src')
112 hasher: $('#sha256Script').attr('src')
112 };
113 };
113 worker.postMessage(data);
114 worker.port.postMessage(data);
114
115
115 return false;
116 return false;
116 });
117 });
@@ -1,8 +1,10 b''
1 var POW_COMPUTING_TIMEOUT = 2;
1 var POW_COMPUTING_TIMEOUT = 2;
2 var POW_HASH_LENGTH = 16;
2 var POW_HASH_LENGTH = 16;
3
3
4 var hasher;
4
5
5 function computeHash(iteration, guess, target, payload, timestamp, hasher) {
6
7 function computeHash(iteration, guess, target, payload, timestamp, hasher, port) {
6 iteration += 1;
8 iteration += 1;
7 var hash = hasher(payload + iteration).toString();
9 var hash = hasher(payload + iteration).toString();
8 guess = hash.substring(0, POW_HASH_LENGTH);
10 guess = hash.substring(0, POW_HASH_LENGTH);
@@ -17,19 +19,19 b' function computeHash(iteration, guess, t'
17 timestamp: timestamp,
19 timestamp: timestamp,
18 guess: guess
20 guess: guess
19 };
21 };
20 self.postMessage(data);
22 port.postMessage(data);
21 } else {
23 } else {
22 //console.log("Iteration: ", iteration);
24 //console.log("Iteration: ", iteration);
23 //console.log("Guess: ", guess);
25 //console.log("Guess: ", guess);
24 //console.log("Target: ", target);
26 //console.log("Target: ", target);
25
27
26 setTimeout(function() {
28 setTimeout(function() {
27 computeHash(iteration, guess, target, payload, timestamp, hasher);
29 computeHash(iteration, guess, target, payload, timestamp, hasher, port);
28 }, POW_COMPUTING_TIMEOUT);
30 }, POW_COMPUTING_TIMEOUT);
29 }
31 }
30 }
32 }
31
33
32 function doWork(message, hasher, difficulty) {
34 function doWork(message, hasher, difficulty, port) {
33 var timestamp = Date.now();
35 var timestamp = Date.now();
34 var iteration = 0;
36 var iteration = 0;
35 var payload = timestamp + message;
37 var payload = timestamp + message;
@@ -42,13 +44,20 b' function doWork(message, hasher, difficu'
42 var guess = target + '0';
44 var guess = target + '0';
43
45
44 setTimeout(function() {
46 setTimeout(function() {
45 computeHash(iteration, guess, target, payload, timestamp, hasher);
47 computeHash(iteration, guess, target, payload, timestamp, hasher, port);
46 }, POW_COMPUTING_TIMEOUT);
48 }, POW_COMPUTING_TIMEOUT);
47 }
49 }
48
50
49 self.onmessage = function(e) {
51 onconnect = function(e) {
50 var difficulty = e.data.difficulty;
52 var port = e.ports[0];
51 importScripts(e.data.hasher);
53 port.start();
52 var hasher = CryptoJS.SHA256;
54
53 self.doWork(e.data.msg, hasher, difficulty);
55 port.onmessage = function(e) {
54 };
56 var difficulty = e.data.difficulty;
57 importScripts(e.data.hasher);
58 var hasher = CryptoJS.SHA256;
59
60 self.doWork(e.data.msg, hasher, difficulty, port);
61 };
62 }
63
General Comments 0
You need to be logged in to leave comments. Login now