consensus: clean up some functions.

This commit is contained in:
Christopher Jeffrey 2017-09-06 22:14:04 -07:00
parent d78df4b79c
commit 5107cfd27a
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
7 changed files with 32 additions and 30 deletions

View File

@ -2238,7 +2238,7 @@ Chain.prototype.getProofTime = function getProofTime(to, from) {
const pow = this.network.pow; const pow = this.network.pow;
let sign, work; let sign, work;
if (to.chainwork.cmp(from.chainwork) > 0) { if (to.chainwork.gt(from.chainwork)) {
work = to.chainwork.sub(from.chainwork); work = to.chainwork.sub(from.chainwork);
sign = 1; sign = 1;
} else { } else {
@ -2343,7 +2343,7 @@ Chain.prototype.retarget = function retarget(prev, first) {
target.imuln(actualTimespan); target.imuln(actualTimespan);
target.idivn(targetTimespan); target.idivn(targetTimespan);
if (target.cmp(pow.limit) > 0) if (target.gt(pow.limit))
return pow.bits; return pow.bits;
return consensus.toCompact(target); return consensus.toCompact(target);

View File

@ -117,7 +117,7 @@ ChainEntry.fromOptions = function fromOptions(options, prev) {
ChainEntry.prototype.getProof = function getProof() { ChainEntry.prototype.getProof = function getProof() {
const target = consensus.fromCompact(this.bits); const target = consensus.fromCompact(this.bits);
if (target.isNeg() || target.cmpn(0) === 0) if (target.isNeg() || target.isZero())
return new BN(0); return new BN(0);
return ChainEntry.MAX_CHAINWORK.div(target.iaddn(1)); return ChainEntry.MAX_CHAINWORK.div(target.iaddn(1));
@ -144,7 +144,7 @@ ChainEntry.prototype.getChainwork = function getChainwork(prev) {
*/ */
ChainEntry.prototype.isGenesis = function isGenesis() { 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) { ChainEntry.prototype.hasUnknown = function hasUnknown(network) {
const bits = this.version & consensus.VERSION_TOP_MASK; const TOP_MASK = consensus.VERSION_TOP_MASK;
const topBits = consensus.VERSION_TOP_BITS; 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 false;
return (this.version & network.unknownBits) !== 0; return (this.version & network.unknownBits) !== 0;

View File

@ -132,7 +132,7 @@ common.getTarget = function getTarget(bits) {
if (target.isNeg()) if (target.isNeg())
throw new Error('Target is negative.'); throw new Error('Target is negative.');
if (target.cmpn(0) === 0) if (target.isZero())
throw new Error('Target is zero.'); throw new Error('Target is zero.');
return target.toArrayLike(Buffer, 'le', 32); return target.toArrayLike(Buffer, 'le', 32);
@ -147,7 +147,7 @@ common.getTarget = function getTarget(bits) {
common.getBits = function getBits(data) { common.getBits = function getBits(data) {
const target = new BN(data, 'le'); const target = new BN(data, 'le');
if (target.cmpn(0) === 0) if (target.isZero())
throw new Error('Target is zero.'); throw new Error('Target is zero.');
return consensus.toCompact(target); return consensus.toCompact(target);

View File

@ -226,14 +226,15 @@ exports.BIP16_TIME = 1333238400;
*/ */
exports.fromCompact = function fromCompact(compact) { exports.fromCompact = function fromCompact(compact) {
const exponent = compact >>> 24;
const negative = (compact >>> 23) & 1;
let mantissa = compact & 0x7fffff;
let num;
if (compact === 0) if (compact === 0)
return new BN(0); return new BN(0);
const exponent = compact >>> 24;
const negative = (compact >>> 23) & 1;
let mantissa = compact & 0x7fffff;
let num;
if (exponent <= 3) { if (exponent <= 3) {
mantissa >>>= 8 * (3 - exponent); mantissa >>>= 8 * (3 - exponent);
num = new BN(mantissa); num = new BN(mantissa);
@ -256,12 +257,11 @@ exports.fromCompact = function fromCompact(compact) {
*/ */
exports.toCompact = function toCompact(num) { exports.toCompact = function toCompact(num) {
let mantissa, exponent, compact; if (num.isZero())
if (num.cmpn(0) === 0)
return 0; return 0;
exponent = num.byteLength(); let exponent = num.byteLength();
let mantissa;
if (exponent <= 3) { if (exponent <= 3) {
mantissa = num.toNumber(); mantissa = num.toNumber();
@ -275,7 +275,7 @@ exports.toCompact = function toCompact(num) {
exponent++; exponent++;
} }
compact = (exponent << 24) | mantissa; let compact = (exponent << 24) | mantissa;
if (num.isNeg()) if (num.isNeg())
compact |= 0x800000; compact |= 0x800000;
@ -295,12 +295,12 @@ exports.toCompact = function toCompact(num) {
exports.verifyPOW = function verifyPOW(hash, bits) { exports.verifyPOW = function verifyPOW(hash, bits) {
const target = exports.fromCompact(bits); const target = exports.fromCompact(bits);
if (target.isNeg() || target.cmpn(0) === 0) if (target.isNeg() || target.isZero())
return false; 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 false;
return true; return true;
@ -313,10 +313,10 @@ exports.verifyPOW = function verifyPOW(hash, bits) {
*/ */
exports.getReward = function getReward(height, interval) { exports.getReward = function getReward(height, interval) {
const halvings = Math.floor(height / interval);
assert(height >= 0, 'Bad height for reward.'); assert(height >= 0, 'Bad height for reward.');
const halvings = Math.floor(height / interval);
// BIP 42 (well, our own version of it, // BIP 42 (well, our own version of it,
// since we can only handle 32 bit shifts). // since we can only handle 32 bit shifts).
// https://github.com/bitcoin/bips/blob/master/bip-0042.mediawiki // 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) { exports.hasBit = function hasBit(version, bit) {
const bits = version & exports.VERSION_TOP_MASK; const TOP_MASK = exports.VERSION_TOP_MASK;
const topBits = exports.VERSION_TOP_BITS; const TOP_BITS = exports.VERSION_TOP_BITS;
const bits = (version & TOP_MASK) >>> 0;
const mask = 1 << bit; const mask = 1 << bit;
return (bits >>> 0) === topBits && (version & mask) !== 0; return bits === TOP_BITS && (version & mask) !== 0;
}; };

View File

@ -507,7 +507,7 @@ BitReader.prototype.readBits64 = function readBits64(count) {
*/ */
function compare(a, b) { function compare(a, b) {
return a.cmp(b) < 0 ? -1 : 1; return a.lt(b) ? -1 : 1;
} }
function siphash24(data, key) { function siphash24(data, key) {

View File

@ -197,7 +197,7 @@ describe('Chain', function() {
assert(forked); assert(forked);
assert.strictEqual(chain.tip.hash, block.hash('hex')); 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', () => { it('should have correct chain value', () => {

View File

@ -163,7 +163,7 @@ describe('Node', function() {
assert(forked); assert(forked);
assert.strictEqual(chain.tip.hash, block.hash('hex')); 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', () => { it('should have correct chain value', () => {