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 = {};
// 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;

View File

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

View File

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