From f120d4d68adb9f988a50679fbe7e52576dbc5da0 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 10 Jun 2016 18:26:18 -0700 Subject: [PATCH] check for negatives and zero in testTarget. --- lib/bcoin/miner.js | 2 +- lib/bcoin/utils.js | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/bcoin/miner.js b/lib/bcoin/miner.js index abaf28a1..853f5000 100644 --- a/lib/bcoin/miner.js +++ b/lib/bcoin/miner.js @@ -526,7 +526,7 @@ MinerBlock.prototype.findNonce = function findNonce() { // The heart and soul of the miner: match the target. while (block.nonce <= 0xffffffff) { // Hash and test against the next target - if (rcmp(utils.dsha256(data), target) < 0) + if (rcmp(utils.dsha256(data), target) <= 0) return true; // Increment the nonce to get a different hash diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 73eed85f..32ed2245 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1062,7 +1062,15 @@ utils.testTarget = function testTarget(hash, target) { if (typeof target === 'number') target = utils.fromCompact(target); - return new bn(hash, 'le').cmp(target) < 0; + if (target.isNeg() || target.cmpn(0) === 0) + return false; + + hash = new bn(hash, 'le'); + + if (hash.cmp(target) > 0) + return false; + + return true; }; /**