diff --git a/lib/bcoin/abstractblock.js b/lib/bcoin/abstractblock.js index fc68ee19..1edd761f 100644 --- a/lib/bcoin/abstractblock.js +++ b/lib/bcoin/abstractblock.js @@ -115,7 +115,7 @@ AbstractBlock.prototype.verifyHeaders = function verifyHeaders(ret) { ret = {}; // Check proof of work - if (!utils.testTarget(this.bits, this.hash())) { + if (!utils.testTarget(this.hash(), this.bits)) { ret.reason = 'high-hash'; ret.score = 50; return false; diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index c0de575c..e29c1204 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -1220,18 +1220,18 @@ utils.toCompact = function toCompact(num) { /** * Test hash against a target. - * @param {BN|Number} target - Compact number or big number. * @param {Buffer|Hash} hash + * @param {BN|Number} target - Compact number or big number. * @returns {Boolean} True if hash is less than target. */ -utils.testTarget = function testTarget(target, hash) { - if (typeof target === 'number') - target = utils.fromCompact(target); - +utils.testTarget = function testTarget(hash, target) { if (typeof hash === 'string') hash = new Buffer(hash, 'hex'); + if (typeof target === 'number') + target = utils.fromCompact(target); + return new bn(hash, 'le').cmp(target) < 0; }; diff --git a/test/utils-test.js b/test/utils-test.js index f3fc1f9e..523c85eb 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -18,7 +18,7 @@ describe('Utils', function() { 'hex' ); var target = utils.fromCompact(bits); - assert(utils.testTarget(target, hash)); + assert(utils.testTarget(hash, target)); }); it('should convert satoshi to btc', function() {