diff --git a/lib/platform.browser.js b/lib/platform.browser.js index 0429522..86d9148 100644 --- a/lib/platform.browser.js +++ b/lib/platform.browser.js @@ -68,19 +68,17 @@ exports.pow = function(opts) { var poolSize = opts.poolSize || navigator.hardwareConcurrency; poolSize = poolSize || FAILBACK_POOL_SIZE; - // Check all input params prematurely to not let promise executor or - // worker to fail because of it. - assert(typeof poolSize === "number", "Bad pool size"); - assert(poolSize >= 1, "Pool size is too low"); - assert(poolSize <= 1024, "Pool size is too high"); - assert(typeof opts.target === "number", "Bad target"); - assert(opts.target >= 0, "Target is too low"); - assert(opts.target <= 9007199254740991, "Target is too high"); - assert(Buffer.isBuffer(opts.initialHash), "Bad initial hash"); - assert(opts.initialHash.length === 64, "Bad initial hash"); - var cancel; var promise = new Promise(function(resolve, reject) { + assert(typeof poolSize === "number", "Bad pool size"); + assert(poolSize >= 1, "Pool size is too low"); + assert(poolSize <= 1024, "Pool size is too high"); + assert(typeof opts.target === "number", "Bad target"); + assert(opts.target >= 0, "Target is too low"); + assert(opts.target <= 9007199254740991, "Target is too high"); + assert(Buffer.isBuffer(opts.initialHash), "Bad initial hash"); + assert(opts.initialHash.length === 64, "Bad initial hash"); + function terminateAll() { while (workers.length) { workers.shift().terminate(); diff --git a/lib/platform.js b/lib/platform.js index c8b47f8..8a5529e 100644 --- a/lib/platform.js +++ b/lib/platform.js @@ -53,18 +53,6 @@ exports.getTarget = function(opts) { exports.pow = function(opts) { var poolSize = opts.poolSize || os.cpus().length; - - // Check all input params prematurely to not let promise executor or - // worker to fail because of it. - assert(typeof poolSize === "number", "Bad pool size"); - assert(poolSize >= 1, "Pool size is too low"); - assert(poolSize <= 1024, "Pool size is too high"); - assert(typeof opts.target === "number", "Bad target"); - assert(opts.target >= 0, "Target is too low"); - assert(opts.target <= 9007199254740991, "Target is too high"); - assert(Buffer.isBuffer(opts.initialHash), "Bad initial hash"); - assert(opts.initialHash.length === 64, "Bad initial hash"); - // TODO(Kagami): Allow to cancel a POW (see `platform.browser.js`). return new promise(function(resolve, reject) { worker.powAsync( diff --git a/src/worker.cc b/src/worker.cc index 7d8c63f..d159f92 100644 --- a/src/worker.cc +++ b/src/worker.cc @@ -78,11 +78,15 @@ NAN_METHOD(PowAsync) { size_t pool_size = args[0]->Uint32Value(); uint64_t target = args[1]->IntegerValue(); size_t length = Buffer::Length(args[2]->ToObject()); - if (pool_size < 1 || pool_size > MAX_POOL_SIZE || length != HASH_SIZE) { + char* buf = Buffer::Data(args[2]->ToObject()); + if ( + pool_size < 1 || + pool_size > MAX_POOL_SIZE || + length != HASH_SIZE || + buf == NULL) { return NanThrowError("Bad input"); } - char* buf = Buffer::Data(args[2]->ToObject()); // TODO(Kagami): Do we need to process `std::bad_alloc`? uint8_t* initial_hash = new uint8_t[length]; memcpy(initial_hash, buf, length); diff --git a/test.js b/test.js index b49d1b7..dbc2d61 100644 --- a/test.js +++ b/test.js @@ -312,10 +312,15 @@ describe("POW", function() { expect(POW.check({nonce: 3122436, target: 4864647698763, initialHash: Buffer("8ff2d685db89a0af2e3dbfd3f700ae96ef4d9a1eac72fd778bbb368c7510cddda349e03207e1c4965bd95c6f7265e8f1a481a08afab3874eaafb9ade09a10880", "hex")})).to.be.false; }); - it("should fail on bad POW arguments", function() { - expect(POW.doAsync.bind(null, {target: 123, initialHash: 0})).to.throw(Error); - expect(POW.doAsync.bind(null, {target: 123, initialHash: Buffer("test")})).to.throw(Error); - expect(POW.doAsync.bind(null, {poolSize: -1, target: 123, initialHash: Buffer(64)})).to.throw(Error); + it("should reject promise on bad POW arguments", function(done) { + POW.doAsync({target: 123, initialHash: {}}).catch(function() { + POW.doAsync({target: 123, initialHash: Buffer("test")}).catch(function() { + POW.doAsync({poolSize: -1, target: 123, initialHash: Buffer(64)}) + .catch(function() { + done(); + }); + }); + }); }); if (allTests) {