From 4e4b87b18ee4e6d51f3aa08976a1ad57a629041a Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Fri, 11 Nov 2016 17:36:52 -0800 Subject: [PATCH] chain: refactor. --- lib/chain/chain.js | 64 ++++++++++++++++++++++------------------- lib/chain/chainentry.js | 20 ++++++------- 2 files changed, 44 insertions(+), 40 deletions(-) diff --git a/lib/chain/chain.js b/lib/chain/chain.js index c89505f1..3218170f 100644 --- a/lib/chain/chain.js +++ b/lib/chain/chain.js @@ -1751,14 +1751,15 @@ Chain.prototype.getCurrentTarget = co(function* getCurrentTarget() { */ Chain.prototype.getTargetAsync = co(function* getTargetAsync(block, prev) { + var pow = this.network.pow; var ancestors; - if ((prev.height + 1) % this.network.pow.retargetInterval !== 0) { - if (!this.network.pow.difficultyReset) + if ((prev.height + 1) % pow.retargetInterval !== 0) { + if (!pow.difficultyReset) 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); }); @@ -1773,24 +1774,25 @@ Chain.prototype.getTargetAsync = co(function* getTargetAsync(block, prev) { */ Chain.prototype.getTarget = function getTarget(block, prev, ancestors) { + var pow = this.network.pow; var ts, first, i; // Genesis if (!prev) - return this.network.pow.bits; + return pow.bits; // Do not retarget - if ((prev.height + 1) % this.network.pow.retargetInterval !== 0) { - if (this.network.pow.difficultyReset) { + if ((prev.height + 1) % pow.retargetInterval !== 0) { + if (pow.difficultyReset) { // Special behavior for testnet: ts = block ? (block.ts || block) : time.now(); - if (ts > prev.ts + this.network.pow.targetSpacing * 2) - return this.network.pow.bits; + if (ts > prev.ts + pow.targetSpacing * 2) + return pow.bits; i = 1; while (ancestors[i] - && prev.height % this.network.pow.retargetInterval !== 0 - && prev.bits === this.network.pow.bits) { + && prev.height % pow.retargetInterval !== 0 + && prev.bits === pow.bits) { prev = ancestors[i++]; } } @@ -1798,7 +1800,7 @@ Chain.prototype.getTarget = function getTarget(block, prev, ancestors) { } // Back 2 weeks - first = ancestors[this.network.pow.retargetInterval - 1]; + first = ancestors[pow.retargetInterval - 1]; assert(first); @@ -1814,17 +1816,18 @@ Chain.prototype.getTarget = function getTarget(block, prev, ancestors) { */ 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; - if (this.network.pow.noRetargeting) + if (pow.noRetargeting) return prev.bits; actualTimespan = prev.ts - first.ts; target = utils.fromCompact(prev.bits); - if (actualTimespan < targetTimespan / 4) - actualTimespan = targetTimespan / 4; + if (actualTimespan < targetTimespan / 4 | 0) + actualTimespan = targetTimespan / 4 | 0; if (actualTimespan > targetTimespan * 4) actualTimespan = targetTimespan * 4; @@ -1832,8 +1835,8 @@ Chain.prototype.retarget = function retarget(prev, first) { target.imuln(actualTimespan); target.idivn(targetTimespan); - if (target.cmp(this.network.pow.limit) > 0) - return this.network.pow.bits; + if (target.cmp(pow.limit) > 0) + return pow.bits; return utils.toCompact(target); }; @@ -1897,6 +1900,7 @@ Chain.prototype.getState = co(function* getState(prev, id) { var threshold = this.network.activationThreshold; var deployment = this.network.deployments[id]; var stateCache = this.stateCache[id]; + var thresholdStates = constants.thresholdStates; var timeStart, timeTimeout, compute, height; var i, entry, count, state, block, medianTime; @@ -1907,7 +1911,7 @@ Chain.prototype.getState = co(function* getState(prev, id) { compute = []; if (!prev) - return constants.thresholdStates.DEFINED; + return thresholdStates.DEFINED; if (((prev.height + 1) % period) !== 0) { height = prev.height - ((prev.height + 1) % period); @@ -1920,7 +1924,7 @@ Chain.prototype.getState = co(function* getState(prev, id) { } entry = prev; - state = constants.thresholdStates.DEFINED; + state = thresholdStates.DEFINED; while (entry) { if (stateCache[entry.hash] != null) { @@ -1931,7 +1935,7 @@ Chain.prototype.getState = co(function* getState(prev, id) { medianTime = yield entry.getMedianTimeAsync(); if (medianTime < timeStart) { - state = constants.thresholdStates.DEFINED; + state = thresholdStates.DEFINED; stateCache[entry.hash] = state; break; } @@ -1947,25 +1951,25 @@ Chain.prototype.getState = co(function* getState(prev, id) { entry = compute.pop(); switch (state) { - case constants.thresholdStates.DEFINED: + case thresholdStates.DEFINED: medianTime = yield entry.getMedianTimeAsync(); if (medianTime >= timeTimeout) { - state = constants.thresholdStates.FAILED; + state = thresholdStates.FAILED; break; } if (medianTime >= timeStart) { - state = constants.thresholdStates.STARTED; + state = thresholdStates.STARTED; break; } break; - case constants.thresholdStates.STARTED: + case thresholdStates.STARTED: medianTime = yield entry.getMedianTimeAsync(); if (medianTime >= timeTimeout) { - state = constants.thresholdStates.FAILED; + state = thresholdStates.FAILED; break; } @@ -1977,7 +1981,7 @@ Chain.prototype.getState = co(function* getState(prev, id) { count++; if (count >= threshold) { - state = constants.thresholdStates.LOCKED_IN; + state = thresholdStates.LOCKED_IN; break; } @@ -1986,11 +1990,11 @@ Chain.prototype.getState = co(function* getState(prev, id) { } break; - case constants.thresholdStates.LOCKED_IN: - state = constants.thresholdStates.ACTIVE; + case thresholdStates.LOCKED_IN: + state = thresholdStates.ACTIVE; break; - case constants.thresholdStates.FAILED: - case constants.thresholdStates.ACTIVE: + case thresholdStates.FAILED: + case thresholdStates.ACTIVE: break; default: assert(false, 'Bad state.'); diff --git a/lib/chain/chainentry.js b/lib/chain/chainentry.js index 507e2917..864217bc 100644 --- a/lib/chain/chainentry.js +++ b/lib/chain/chainentry.js @@ -163,13 +163,13 @@ ChainEntry.prototype.isGenesis = function isGenesis() { */ ChainEntry.prototype.getRetargetAncestors = function getRetargetAncestors() { - var medianTimespan = constants.block.MEDIAN_TIMESPAN; - var retargetInterval = this.network.pow.retargetInterval; - var diffReset = this.network.pow.difficultyReset; - var max = medianTimespan; + var timespan = constants.block.MEDIAN_TIMESPAN; + var interval = this.network.pow.retargetInterval; + var reset = this.network.pow.difficultyReset; + var max = timespan; - if ((this.height + 1) % retargetInterval === 0 || diffReset) - max = Math.max(max, retargetInterval); + if ((this.height + 1) % interval === 0 || reset) + max = Math.max(max, interval); return this.getAncestors(max); }; @@ -323,12 +323,12 @@ ChainEntry.prototype.getNext = co(function* getNext() { */ ChainEntry.prototype.getMedianTime = function getMedianTime(ancestors) { + var timespan = constants.block.MEDIAN_TIMESPAN; var entry = this; var median = []; - var timeSpan = constants.block.MEDIAN_TIMESPAN; 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 = median.sort(); @@ -342,8 +342,8 @@ ChainEntry.prototype.getMedianTime = function getMedianTime(ancestors) { */ ChainEntry.prototype.getMedianTimeAsync = co(function* getMedianTimeAsync() { - var MEDIAN_TIMESPAN = constants.block.MEDIAN_TIMESPAN; - var ancestors = yield this.getAncestors(MEDIAN_TIMESPAN); + var timespan = constants.block.MEDIAN_TIMESPAN; + var ancestors = yield this.getAncestors(timespan); return this.getMedianTime(ancestors); });