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