From 440933c27daf871f43bf2773f3185823659c4071 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 1 Jan 2016 19:58:35 -0800 Subject: [PATCH] move pow target methods. --- lib/bcoin/fullchain.js | 126 ++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 63 deletions(-) diff --git a/lib/bcoin/fullchain.js b/lib/bcoin/fullchain.js index 4b28211d..bdf066d1 100644 --- a/lib/bcoin/fullchain.js +++ b/lib/bcoin/fullchain.js @@ -431,6 +431,69 @@ Chain.prototype.height = function height() { return this.getTip().height; }; +Chain.prototype.target = function target(last) { + var proofOfWorkLimit = utils.toCompact(network.powLimit); + var adjustmentInterval = network.powTargetTimespan / network.powTargetSpacing; + var newBlockTs, heightFirst, first; + + adjustmentInterval |= 0; + + if (!last) + last = this.getTip(); + + // Do not retarget + if ((last.height + 1) % adjustmentInterval) { + if (network.powAllowMinDifficultyBlocks) { + // Special behavior for testnet: + newBlockTs = Date.now() / 1000 | 0; + if (newBlockTs > last.ts + network.powTargetSpacing * 2) + return proofOfWorkLimit; + + while (last.prev + && last.height % adjustmentInterval !== 0 + && last.bits !== proofOfWorkLimit) { + last = last.prev; + } + + return last.bits; + } + return last.bits; + } + + // Back 2 weeks + heightFirst = last.height - (adjustmentInterval - 1); + first = this.byHeight(heightFirst); + + if (!first) + return 0; + + return this.retarget(last, first.ts); +}; + +Chain.prototype.retarget = function retarget(last, firstTs) { + var powTargetTimespan = new bn(network.powTargetTimespan); + var actualTimespan, powLimit, target; + + if (network.powNoRetargeting) + return last.bits; + + actualTimespan = new bn(last.ts).subn(firstTs); + if (actualTimespan.cmp(powTargetTimespan.divn(4)) < 0) + actualTimespan = powTargetTimespan.divn(4); + + if (actualTimespan.cmp(powTargetTimespan.muln(4)) > 0) + actualTimespan = powTargetTimespan.muln(4); + + powLimit = network.powLimit; + target = utils.fromCompact(last.bits); + target.imul(actualTimespan); + target = target.div(powTargetTimespan); + if (target.cmp(powLimit) > 0) + target = powLimit.clone(); + + return utils.toCompact(target); +}; + Chain.prototype._save = function(hash, obj) { var self = this; @@ -520,69 +583,6 @@ ChainBlock.prototype.getChainwork = function() { return (this.prev ? this.prev.chainwork : new bn(0)).add(this.proof); }; -Chain.prototype.target = function target(last) { - var proofOfWorkLimit = utils.toCompact(network.powLimit); - var adjustmentInterval = network.powTargetTimespan / network.powTargetSpacing; - var newBlockTs, heightFirst, first; - - adjustmentInterval |= 0; - - if (!last) - last = this.getTip(); - - // Do not retarget - if ((last.height + 1) % adjustmentInterval) { - if (network.powAllowMinDifficultyBlocks) { - // Special behavior for testnet: - newBlockTs = Date.now() / 1000 | 0; - if (newBlockTs > last.ts + network.powTargetSpacing * 2) - return proofOfWorkLimit; - - while (last.prev - && last.height % adjustmentInterval !== 0 - && last.bits !== proofOfWorkLimit) { - last = last.prev; - } - - return last.bits; - } - return last.bits; - } - - // Back 2 weeks - heightFirst = last.height - (adjustmentInterval - 1); - first = this.byHeight(heightFirst); - - if (!first) - return 0; - - return this.retarget(last, first.ts); -}; - -Chain.prototype.retarget = function retarget(last, firstTs) { - var powTargetTimespan = new bn(network.powTargetTimespan); - var actualTimespan, powLimit, target; - - if (network.powNoRetargeting) - return last.bits; - - actualTimespan = new bn(last.ts).subn(firstTs); - if (actualTimespan.cmp(powTargetTimespan.divn(4)) < 0) - actualTimespan = powTargetTimespan.divn(4); - - if (actualTimespan.cmp(powTargetTimespan.muln(4)) > 0) - actualTimespan = powTargetTimespan.muln(4); - - powLimit = network.powLimit; - target = utils.fromCompact(last.bits); - target.imul(actualTimespan); - target = target.div(powTargetTimespan); - if (target.cmp(powLimit) > 0) - target = powLimit.clone(); - - return utils.toCompact(target); -}; - ChainBlock.prototype.toJSON = function() { // return [ // this.hash,