refactor testTarget.

This commit is contained in:
Christopher Jeffrey 2016-05-16 03:59:42 -07:00
parent 9a962bb73a
commit 0b6f86c9d5
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
3 changed files with 7 additions and 7 deletions

View File

@ -115,7 +115,7 @@ AbstractBlock.prototype.verifyHeaders = function verifyHeaders(ret) {
ret = {}; ret = {};
// Check proof of work // Check proof of work
if (!utils.testTarget(this.bits, this.hash())) { if (!utils.testTarget(this.hash(), this.bits)) {
ret.reason = 'high-hash'; ret.reason = 'high-hash';
ret.score = 50; ret.score = 50;
return false; return false;

View File

@ -1220,18 +1220,18 @@ utils.toCompact = function toCompact(num) {
/** /**
* Test hash against a target. * Test hash against a target.
* @param {BN|Number} target - Compact number or big number.
* @param {Buffer|Hash} hash * @param {Buffer|Hash} hash
* @param {BN|Number} target - Compact number or big number.
* @returns {Boolean} True if hash is less than target. * @returns {Boolean} True if hash is less than target.
*/ */
utils.testTarget = function testTarget(target, hash) { utils.testTarget = function testTarget(hash, target) {
if (typeof target === 'number')
target = utils.fromCompact(target);
if (typeof hash === 'string') if (typeof hash === 'string')
hash = new Buffer(hash, 'hex'); hash = new Buffer(hash, 'hex');
if (typeof target === 'number')
target = utils.fromCompact(target);
return new bn(hash, 'le').cmp(target) < 0; return new bn(hash, 'le').cmp(target) < 0;
}; };

View File

@ -18,7 +18,7 @@ describe('Utils', function() {
'hex' 'hex'
); );
var target = utils.fromCompact(bits); var target = utils.fromCompact(bits);
assert(utils.testTarget(target, hash)); assert(utils.testTarget(hash, target));
}); });
it('should convert satoshi to btc', function() { it('should convert satoshi to btc', function() {