diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index a4674eeb..7f100868 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -2238,7 +2238,7 @@ Chain.prototype.getProofTime = function getProofTime(to, from) { const pow = this.network.pow; let sign, work; - if (to.chainwork.cmp(from.chainwork) > 0) { + if (to.chainwork.gt(from.chainwork)) { work = to.chainwork.sub(from.chainwork); sign = 1; } else { @@ -2343,7 +2343,7 @@ Chain.prototype.retarget = function retarget(prev, first) { target.imuln(actualTimespan); target.idivn(targetTimespan); - if (target.cmp(pow.limit) > 0) + if (target.gt(pow.limit)) return pow.bits; return consensus.toCompact(target); diff --git a/lib/blockchain/chainentry.js b/lib/blockchain/chainentry.js index de9fece0..d621869b 100644 --- a/lib/blockchain/chainentry.js +++ b/lib/blockchain/chainentry.js @@ -117,7 +117,7 @@ ChainEntry.fromOptions = function fromOptions(options, prev) { ChainEntry.prototype.getProof = function getProof() { const target = consensus.fromCompact(this.bits); - if (target.isNeg() || target.cmpn(0) === 0) + if (target.isNeg() || target.isZero()) return new BN(0); return ChainEntry.MAX_CHAINWORK.div(target.iaddn(1)); @@ -144,7 +144,7 @@ ChainEntry.prototype.getChainwork = function getChainwork(prev) { */ ChainEntry.prototype.isGenesis = function isGenesis() { - return this.prevBlock === encoding.NULL_HASH; + return this.height === 0; }; /** @@ -154,10 +154,11 @@ ChainEntry.prototype.isGenesis = function isGenesis() { */ ChainEntry.prototype.hasUnknown = function hasUnknown(network) { - const bits = this.version & consensus.VERSION_TOP_MASK; - const topBits = consensus.VERSION_TOP_BITS; + const TOP_MASK = consensus.VERSION_TOP_MASK; + const TOP_BITS = consensus.VERSION_TOP_BITS; + const bits = (this.version & TOP_MASK) >>> 0; - if ((bits >>> 0) !== topBits) + if (bits !== TOP_BITS) return false; return (this.version & network.unknownBits) !== 0; diff --git a/lib/mining/common.js b/lib/mining/common.js index 2740ace6..3b43c7f6 100644 --- a/lib/mining/common.js +++ b/lib/mining/common.js @@ -132,7 +132,7 @@ common.getTarget = function getTarget(bits) { if (target.isNeg()) throw new Error('Target is negative.'); - if (target.cmpn(0) === 0) + if (target.isZero()) throw new Error('Target is zero.'); return target.toArrayLike(Buffer, 'le', 32); @@ -147,7 +147,7 @@ common.getTarget = function getTarget(bits) { common.getBits = function getBits(data) { const target = new BN(data, 'le'); - if (target.cmpn(0) === 0) + if (target.isZero()) throw new Error('Target is zero.'); return consensus.toCompact(target); diff --git a/lib/protocol/consensus.js b/lib/protocol/consensus.js index 1cf8bc34..74e5132a 100644 --- a/lib/protocol/consensus.js +++ b/lib/protocol/consensus.js @@ -226,14 +226,15 @@ exports.BIP16_TIME = 1333238400; */ exports.fromCompact = function fromCompact(compact) { - const exponent = compact >>> 24; - const negative = (compact >>> 23) & 1; - let mantissa = compact & 0x7fffff; - let num; - if (compact === 0) return new BN(0); + const exponent = compact >>> 24; + const negative = (compact >>> 23) & 1; + + let mantissa = compact & 0x7fffff; + let num; + if (exponent <= 3) { mantissa >>>= 8 * (3 - exponent); num = new BN(mantissa); @@ -256,12 +257,11 @@ exports.fromCompact = function fromCompact(compact) { */ exports.toCompact = function toCompact(num) { - let mantissa, exponent, compact; - - if (num.cmpn(0) === 0) + if (num.isZero()) return 0; - exponent = num.byteLength(); + let exponent = num.byteLength(); + let mantissa; if (exponent <= 3) { mantissa = num.toNumber(); @@ -275,7 +275,7 @@ exports.toCompact = function toCompact(num) { exponent++; } - compact = (exponent << 24) | mantissa; + let compact = (exponent << 24) | mantissa; if (num.isNeg()) compact |= 0x800000; @@ -295,12 +295,12 @@ exports.toCompact = function toCompact(num) { exports.verifyPOW = function verifyPOW(hash, bits) { const target = exports.fromCompact(bits); - if (target.isNeg() || target.cmpn(0) === 0) + if (target.isNeg() || target.isZero()) return false; - hash = new BN(hash, 'le'); + const num = new BN(hash, 'le'); - if (hash.cmp(target) > 0) + if (num.gt(target)) return false; return true; @@ -313,10 +313,10 @@ exports.verifyPOW = function verifyPOW(hash, bits) { */ exports.getReward = function getReward(height, interval) { - const halvings = Math.floor(height / interval); - assert(height >= 0, 'Bad height for reward.'); + const halvings = Math.floor(height / interval); + // BIP 42 (well, our own version of it, // since we can only handle 32 bit shifts). // https://github.com/bitcoin/bips/blob/master/bip-0042.mediawiki @@ -341,8 +341,9 @@ exports.getReward = function getReward(height, interval) { */ exports.hasBit = function hasBit(version, bit) { - const bits = version & exports.VERSION_TOP_MASK; - const topBits = exports.VERSION_TOP_BITS; + const TOP_MASK = exports.VERSION_TOP_MASK; + const TOP_BITS = exports.VERSION_TOP_BITS; + const bits = (version & TOP_MASK) >>> 0; const mask = 1 << bit; - return (bits >>> 0) === topBits && (version & mask) !== 0; + return bits === TOP_BITS && (version & mask) !== 0; }; diff --git a/lib/utils/gcs.js b/lib/utils/gcs.js index a0881190..29501171 100644 --- a/lib/utils/gcs.js +++ b/lib/utils/gcs.js @@ -507,7 +507,7 @@ BitReader.prototype.readBits64 = function readBits64(count) { */ function compare(a, b) { - return a.cmp(b) < 0 ? -1 : 1; + return a.lt(b) ? -1 : 1; } function siphash24(data, key) { diff --git a/test/chain-test.js b/test/chain-test.js index 8cfc2ceb..2b58ae60 100644 --- a/test/chain-test.js +++ b/test/chain-test.js @@ -197,7 +197,7 @@ describe('Chain', function() { assert(forked); assert.strictEqual(chain.tip.hash, block.hash('hex')); - assert(chain.tip.chainwork.cmp(tip1.chainwork) > 0); + assert(chain.tip.chainwork.gt(tip1.chainwork)); }); it('should have correct chain value', () => { diff --git a/test/node-test.js b/test/node-test.js index e400f4d8..73d0021c 100644 --- a/test/node-test.js +++ b/test/node-test.js @@ -163,7 +163,7 @@ describe('Node', function() { assert(forked); assert.strictEqual(chain.tip.hash, block.hash('hex')); - assert(chain.tip.chainwork.cmp(tip1.chainwork) > 0); + assert(chain.tip.chainwork.gt(tip1.chainwork)); }); it('should have correct chain value', () => {