chain: refactor.

This commit is contained in:
Christopher Jeffrey 2016-11-11 17:36:52 -08:00
parent 6380640447
commit 4e4b87b18e
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 44 additions and 40 deletions

View File

@ -1751,14 +1751,15 @@ Chain.prototype.getCurrentTarget = co(function* getCurrentTarget() {
*/ */
Chain.prototype.getTargetAsync = co(function* getTargetAsync(block, prev) { Chain.prototype.getTargetAsync = co(function* getTargetAsync(block, prev) {
var pow = this.network.pow;
var ancestors; var ancestors;
if ((prev.height + 1) % this.network.pow.retargetInterval !== 0) { if ((prev.height + 1) % pow.retargetInterval !== 0) {
if (!this.network.pow.difficultyReset) if (!pow.difficultyReset)
return this.getTarget(block, prev); return this.getTarget(block, prev);
} }
ancestors = yield prev.getAncestors(this.network.pow.retargetInterval); ancestors = yield prev.getAncestors(pow.retargetInterval);
return this.getTarget(block, prev, ancestors); return this.getTarget(block, prev, ancestors);
}); });
@ -1773,24 +1774,25 @@ Chain.prototype.getTargetAsync = co(function* getTargetAsync(block, prev) {
*/ */
Chain.prototype.getTarget = function getTarget(block, prev, ancestors) { Chain.prototype.getTarget = function getTarget(block, prev, ancestors) {
var pow = this.network.pow;
var ts, first, i; var ts, first, i;
// Genesis // Genesis
if (!prev) if (!prev)
return this.network.pow.bits; return pow.bits;
// Do not retarget // Do not retarget
if ((prev.height + 1) % this.network.pow.retargetInterval !== 0) { if ((prev.height + 1) % pow.retargetInterval !== 0) {
if (this.network.pow.difficultyReset) { if (pow.difficultyReset) {
// Special behavior for testnet: // Special behavior for testnet:
ts = block ? (block.ts || block) : time.now(); ts = block ? (block.ts || block) : time.now();
if (ts > prev.ts + this.network.pow.targetSpacing * 2) if (ts > prev.ts + pow.targetSpacing * 2)
return this.network.pow.bits; return pow.bits;
i = 1; i = 1;
while (ancestors[i] while (ancestors[i]
&& prev.height % this.network.pow.retargetInterval !== 0 && prev.height % pow.retargetInterval !== 0
&& prev.bits === this.network.pow.bits) { && prev.bits === pow.bits) {
prev = ancestors[i++]; prev = ancestors[i++];
} }
} }
@ -1798,7 +1800,7 @@ Chain.prototype.getTarget = function getTarget(block, prev, ancestors) {
} }
// Back 2 weeks // Back 2 weeks
first = ancestors[this.network.pow.retargetInterval - 1]; first = ancestors[pow.retargetInterval - 1];
assert(first); assert(first);
@ -1814,17 +1816,18 @@ Chain.prototype.getTarget = function getTarget(block, prev, ancestors) {
*/ */
Chain.prototype.retarget = function retarget(prev, first) { Chain.prototype.retarget = function retarget(prev, first) {
var targetTimespan = this.network.pow.targetTimespan; var pow = this.network.pow;
var targetTimespan = pow.targetTimespan;
var actualTimespan, target; var actualTimespan, target;
if (this.network.pow.noRetargeting) if (pow.noRetargeting)
return prev.bits; return prev.bits;
actualTimespan = prev.ts - first.ts; actualTimespan = prev.ts - first.ts;
target = utils.fromCompact(prev.bits); target = utils.fromCompact(prev.bits);
if (actualTimespan < targetTimespan / 4) if (actualTimespan < targetTimespan / 4 | 0)
actualTimespan = targetTimespan / 4; actualTimespan = targetTimespan / 4 | 0;
if (actualTimespan > targetTimespan * 4) if (actualTimespan > targetTimespan * 4)
actualTimespan = targetTimespan * 4; actualTimespan = targetTimespan * 4;
@ -1832,8 +1835,8 @@ Chain.prototype.retarget = function retarget(prev, first) {
target.imuln(actualTimespan); target.imuln(actualTimespan);
target.idivn(targetTimespan); target.idivn(targetTimespan);
if (target.cmp(this.network.pow.limit) > 0) if (target.cmp(pow.limit) > 0)
return this.network.pow.bits; return pow.bits;
return utils.toCompact(target); return utils.toCompact(target);
}; };
@ -1897,6 +1900,7 @@ Chain.prototype.getState = co(function* getState(prev, id) {
var threshold = this.network.activationThreshold; var threshold = this.network.activationThreshold;
var deployment = this.network.deployments[id]; var deployment = this.network.deployments[id];
var stateCache = this.stateCache[id]; var stateCache = this.stateCache[id];
var thresholdStates = constants.thresholdStates;
var timeStart, timeTimeout, compute, height; var timeStart, timeTimeout, compute, height;
var i, entry, count, state, block, medianTime; var i, entry, count, state, block, medianTime;
@ -1907,7 +1911,7 @@ Chain.prototype.getState = co(function* getState(prev, id) {
compute = []; compute = [];
if (!prev) if (!prev)
return constants.thresholdStates.DEFINED; return thresholdStates.DEFINED;
if (((prev.height + 1) % period) !== 0) { if (((prev.height + 1) % period) !== 0) {
height = prev.height - ((prev.height + 1) % period); height = prev.height - ((prev.height + 1) % period);
@ -1920,7 +1924,7 @@ Chain.prototype.getState = co(function* getState(prev, id) {
} }
entry = prev; entry = prev;
state = constants.thresholdStates.DEFINED; state = thresholdStates.DEFINED;
while (entry) { while (entry) {
if (stateCache[entry.hash] != null) { if (stateCache[entry.hash] != null) {
@ -1931,7 +1935,7 @@ Chain.prototype.getState = co(function* getState(prev, id) {
medianTime = yield entry.getMedianTimeAsync(); medianTime = yield entry.getMedianTimeAsync();
if (medianTime < timeStart) { if (medianTime < timeStart) {
state = constants.thresholdStates.DEFINED; state = thresholdStates.DEFINED;
stateCache[entry.hash] = state; stateCache[entry.hash] = state;
break; break;
} }
@ -1947,25 +1951,25 @@ Chain.prototype.getState = co(function* getState(prev, id) {
entry = compute.pop(); entry = compute.pop();
switch (state) { switch (state) {
case constants.thresholdStates.DEFINED: case thresholdStates.DEFINED:
medianTime = yield entry.getMedianTimeAsync(); medianTime = yield entry.getMedianTimeAsync();
if (medianTime >= timeTimeout) { if (medianTime >= timeTimeout) {
state = constants.thresholdStates.FAILED; state = thresholdStates.FAILED;
break; break;
} }
if (medianTime >= timeStart) { if (medianTime >= timeStart) {
state = constants.thresholdStates.STARTED; state = thresholdStates.STARTED;
break; break;
} }
break; break;
case constants.thresholdStates.STARTED: case thresholdStates.STARTED:
medianTime = yield entry.getMedianTimeAsync(); medianTime = yield entry.getMedianTimeAsync();
if (medianTime >= timeTimeout) { if (medianTime >= timeTimeout) {
state = constants.thresholdStates.FAILED; state = thresholdStates.FAILED;
break; break;
} }
@ -1977,7 +1981,7 @@ Chain.prototype.getState = co(function* getState(prev, id) {
count++; count++;
if (count >= threshold) { if (count >= threshold) {
state = constants.thresholdStates.LOCKED_IN; state = thresholdStates.LOCKED_IN;
break; break;
} }
@ -1986,11 +1990,11 @@ Chain.prototype.getState = co(function* getState(prev, id) {
} }
break; break;
case constants.thresholdStates.LOCKED_IN: case thresholdStates.LOCKED_IN:
state = constants.thresholdStates.ACTIVE; state = thresholdStates.ACTIVE;
break; break;
case constants.thresholdStates.FAILED: case thresholdStates.FAILED:
case constants.thresholdStates.ACTIVE: case thresholdStates.ACTIVE:
break; break;
default: default:
assert(false, 'Bad state.'); assert(false, 'Bad state.');

View File

@ -163,13 +163,13 @@ ChainEntry.prototype.isGenesis = function isGenesis() {
*/ */
ChainEntry.prototype.getRetargetAncestors = function getRetargetAncestors() { ChainEntry.prototype.getRetargetAncestors = function getRetargetAncestors() {
var medianTimespan = constants.block.MEDIAN_TIMESPAN; var timespan = constants.block.MEDIAN_TIMESPAN;
var retargetInterval = this.network.pow.retargetInterval; var interval = this.network.pow.retargetInterval;
var diffReset = this.network.pow.difficultyReset; var reset = this.network.pow.difficultyReset;
var max = medianTimespan; var max = timespan;
if ((this.height + 1) % retargetInterval === 0 || diffReset) if ((this.height + 1) % interval === 0 || reset)
max = Math.max(max, retargetInterval); max = Math.max(max, interval);
return this.getAncestors(max); return this.getAncestors(max);
}; };
@ -323,12 +323,12 @@ ChainEntry.prototype.getNext = co(function* getNext() {
*/ */
ChainEntry.prototype.getMedianTime = function getMedianTime(ancestors) { ChainEntry.prototype.getMedianTime = function getMedianTime(ancestors) {
var timespan = constants.block.MEDIAN_TIMESPAN;
var entry = this; var entry = this;
var median = []; var median = [];
var timeSpan = constants.block.MEDIAN_TIMESPAN;
var i; var i;
for (i = 0; i < timeSpan && entry; i++, entry = ancestors[i]) for (i = 0; i < timespan && entry; i++, entry = ancestors[i])
median.push(entry.ts); median.push(entry.ts);
median = median.sort(); median = median.sort();
@ -342,8 +342,8 @@ ChainEntry.prototype.getMedianTime = function getMedianTime(ancestors) {
*/ */
ChainEntry.prototype.getMedianTimeAsync = co(function* getMedianTimeAsync() { ChainEntry.prototype.getMedianTimeAsync = co(function* getMedianTimeAsync() {
var MEDIAN_TIMESPAN = constants.block.MEDIAN_TIMESPAN; var timespan = constants.block.MEDIAN_TIMESPAN;
var ancestors = yield this.getAncestors(MEDIAN_TIMESPAN); var ancestors = yield this.getAncestors(timespan);
return this.getMedianTime(ancestors); return this.getMedianTime(ancestors);
}); });