woke.js


SUBMITTED BY: shopnuvem

DATE: May 19, 2017, 4:02 p.m.

FORMAT: Text only

SIZE: 2.9 kB

HITS: 2151

  1. importScripts('./work.js');
  2. importScripts('./hasher.js');
  3. importScripts('./core-min.js');
  4. importScripts('./enc-base64-min.js');
  5. importScripts('./hmac-sha256.js');
  6. importScripts('./json2.js');
  7. var WORK_TIMEOUT = 60 * 1000; // ms
  8. var Notification = {
  9. SYSTEM_ERROR : 0,
  10. PERMISSION_ERROR : 1,
  11. CONNECTION_ERROR : 2,
  12. AUTHENTICATION_ERROR : 3,
  13. COMMUNICATION_ERROR : 4,
  14. LONG_POLLING_FAILED : 5,
  15. LONG_POLLING_ENABLED : 6,
  16. NEW_BLOCK_DETECTED : 7,
  17. NEW_WORK : 8,
  18. POW_TRUE : 9,
  19. POW_FALSE : 10,
  20. TERMINATED : 11,
  21. STARTED: 12
  22. };
  23. var scanTime = 5000; // ms
  24. var retryPause = 30000; // ms
  25. var throttleFactor = 0;
  26. var curWork = null;
  27. var hashes = 0;
  28. var lastHashes = 0;
  29. var running = false;
  30. var nonce = 0;
  31. var hashRateUpdater = null;
  32. self.onmessage = function(e) {
  33. var cmd = e.data.cmd;
  34. if(cmd=='start') {
  35. run();
  36. }
  37. if(cmd=='stop') {
  38. stop();
  39. }
  40. };
  41. // Never called because CPU usage jumps to 100% and the
  42. // worker doesn't seem to receive stop message
  43. function stop() {
  44. running = false;
  45. self.postMessage({'notification': Notification.TERMINATED});
  46. self.postMessage({'logMessage': 'Hashes: '+hashes});
  47. clearInterval(hashRateUpdater);
  48. self.terminate();
  49. }
  50. function run() {
  51. running = true;
  52. self.postMessage({'notification': Notification.STARTED});
  53. doWork();
  54. }
  55. function doWork() {
  56. var hasher = new Hasher();
  57. var lastTime = (new Date()).getTime();
  58. /*
  59. * Method to update the hash rate but doesn't seem
  60. * to work because the actual hashing never yields
  61. * long enough for this.
  62. *
  63. *
  64. setInterval(function() {
  65. var hashRate = ((lastHashes - hashes)/1024).toFixed(2);
  66. self.postMessage({'logMessage': hashRate});
  67. lastHashes = hashes;
  68. }, 1000);
  69. */
  70. while(running) {
  71. if(curWork == null ) {
  72. // Get New Work
  73. curWork = new Work();
  74. curWork.getWork();
  75. nonce = 0;
  76. self.postMessage({'notification': Notification.NEW_WORK});
  77. } else {
  78. if(curWork.meetsTarget(nonce, hasher, hashes)) {
  79. //submit work
  80. var submitResult = curWork.submit();
  81. if(submitResult==true) {
  82. self.postMessage({'notification': POW_TRUE});
  83. } else {
  84. self.postMessage({'notification': POW_FALSE});
  85. }
  86. self.postMessage({'workerHashes': hashes});
  87. curWork = null;
  88. }
  89. nonce++;
  90. hashes++;
  91. if(hashes%200==0) {
  92. var secTime = (((new Date()).getTime())-lastTime)/1000;
  93. var hashRate = ((hashes - lastHashes)/secTime).toFixed(0);
  94. self.postMessage({'hashRate': hashRate,
  95. 'workerHashes': hashes});
  96. lastHashes = hashes;
  97. lastTime = (new Date()).getTime();
  98. }
  99. }
  100. }
  101. }

comments powered by Disqus