refactor testTarget.
This commit is contained in:
parent
9a962bb73a
commit
0b6f86c9d5
@ -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;
|
||||||
|
|||||||
@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -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() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user