|
|
|
@ -4,12 +4,20 @@
|
|
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
// `hash.js` is already required by
|
|
|
|
|
// `bitmessage -> eccrypto -> elliptic -> hash.js` so it won't add
|
|
|
|
|
// additional bytes to the bundle.
|
|
|
|
|
var hash = require("hash.js");
|
|
|
|
|
// Use only one submodule from `sha.js` here and in subworker because
|
|
|
|
|
// it's faster. It will add additional bytes to the bundle but not that
|
|
|
|
|
// much (~10-30KB).
|
|
|
|
|
var Sha512 = require("sha.js/sha512");
|
|
|
|
|
var BN = require("bn.js");
|
|
|
|
|
var work = require("webworkify");
|
|
|
|
|
var assert = require("./util").assert;
|
|
|
|
|
|
|
|
|
|
exports.sha512 = function(buf) {
|
|
|
|
|
return new Buffer(hash.sha512().update(buf).digest());
|
|
|
|
|
return new Sha512().update(buf).digest();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
exports.sha256 = function(buf) {
|
|
|
|
@ -63,7 +71,6 @@ exports.doPOW = function(opts) {
|
|
|
|
|
// Check all input params prematurely to not let promise executor or
|
|
|
|
|
// worker to fail because of it.
|
|
|
|
|
assert(poolSize > 0, "Pool size is too low");
|
|
|
|
|
assert(opts.workerUrl, "Bad worker URL");
|
|
|
|
|
assert(typeof opts.target === "number", "Bad target");
|
|
|
|
|
assert(Buffer.isBuffer(opts.initialHash), "Bad initial hash");
|
|
|
|
|
|
|
|
|
@ -99,7 +106,7 @@ exports.doPOW = function(opts) {
|
|
|
|
|
var workers = [];
|
|
|
|
|
var worker;
|
|
|
|
|
for (var i = 0; i < poolSize; i++) {
|
|
|
|
|
worker = new Worker(opts.workerUrl);
|
|
|
|
|
worker = work(require("./worker.browser.js"));
|
|
|
|
|
workers.push(worker);
|
|
|
|
|
// NOTE(Kagami): There is no race condition here. `onmessage` can
|
|
|
|
|
// only be called _after_ this for-loop finishes. See
|
|
|
|
|