move pow target methods.

This commit is contained in:
Christopher Jeffrey 2016-01-01 19:58:35 -08:00
parent 0e5d7c41f2
commit 440933c27d

View File

@ -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,