More consistent API

This commit is contained in:
Kagami Hiiragi 2015-01-12 21:27:14 +03:00
parent 69ac264e26
commit 667775f015
4 changed files with 24 additions and 29 deletions

View File

@ -68,8 +68,8 @@ 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.
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");
@ -79,8 +79,6 @@ exports.pow = function(opts) {
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) {
function terminateAll() {
while (workers.length) {
workers.shift().terminate();

View File

@ -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(

View File

@ -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);

13
test.js
View File

@ -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) {