From c9d676efb40677527c1b6268ada09a4124d0db76 Mon Sep 17 00:00:00 2001 From: Sky Young Date: Mon, 14 May 2018 16:38:03 -0700 Subject: [PATCH] Update Difficulty Calculations --- lib/blockchain/chain.js | 45 ++++++++++++++++++++++++++------------- lib/protocol/consensus.js | 10 +++++++-- lib/protocol/networks.js | 34 ++++++++++++++++++++++++++++- 3 files changed, 71 insertions(+), 18 deletions(-) diff --git a/lib/blockchain/chain.js b/lib/blockchain/chain.js index 4a31dfcf..01122e0c 100644 --- a/lib/blockchain/chain.js +++ b/lib/blockchain/chain.js @@ -2301,16 +2301,20 @@ Chain.prototype.getTarget = async function getTarget(time, prev) { return prev.bits; } - // Back 2 weeks - var back = pow.retargetInterval - 1; - - if (prev.height + 1 !== pow.retargetInterval) - back = pow.retargetInterval; - - const height = prev.height - back; - assert(height >= 0); + // Back 6 block + var back = pow.averagingInterval - 1; + + + if (prev.height + 1 !== pow.averagingInterval) + back = pow.averagingInterval; + + + let first = prev; + for (let i = 0; i < back; i++){ + if (first) + first = await this.getPrevious(first) + } - const first = await this.getAncestor(prev, height); assert(first); return this.retarget(prev, first); @@ -2327,19 +2331,30 @@ Chain.prototype.getTarget = async function getTarget(time, prev) { Chain.prototype.retarget = function retarget(prev, first) { const pow = this.network.pow; const targetTimespan = pow.targetTimespan; + const averagingIntervalTimespan = pow.averagingIntervalTimespan; + const targetSpacing = pow.targetSpacing; + const adjustUp = pow.adjustUp; + const adjustDown = pow.adjustDown; if (pow.noRetargeting) return prev.bits; - const target = consensus.fromCompact(prev.bits); - let actualTimespan = prev.time - first.time; - if (actualTimespan < targetTimespan / 4 | 0) - actualTimespan = targetTimespan / 4 | 0; - if (actualTimespan > targetTimespan * 4) - actualTimespan = targetTimespan * 4; + let minActualTimespan = Math.floor(averagingIntervalTimespan * (100 - adjustUp) / 100) + let maxActualTimespan = Math.floor(averagingIntervalTimespan * (100 + adjustDown) / 100) + + if (actualTimespan < minActualTimespan) + actualTimespan = minActualTimespan; + + + if (actualTimespan > maxActualTimespan) + actualTimespan = maxActualTimespan; + + // Retarget + let target = consensus.fromCompact(prev.bits); + target.imuln(actualTimespan); target.idivn(targetTimespan); diff --git a/lib/protocol/consensus.js b/lib/protocol/consensus.js index 9a7dc9c4..a1f3061c 100644 --- a/lib/protocol/consensus.js +++ b/lib/protocol/consensus.js @@ -24,7 +24,7 @@ exports.COIN = 100000000; /** * Maximum amount of money in satoshis: - * `21million * 1btc` (consensus). + * `160million * 1flo` (consensus). * @const {Amount} * @default */ @@ -330,7 +330,13 @@ exports.getReward = function getReward(height, interval) { if (halvings === 0) return exports.BASE_REWARD; - return exports.HALF_REWARD >>> (halvings - 1); + let rewardValue = exports.BASE_REWARD; + + for (let i = 0; i < halvings; i++){ + rewardValue /= 2; + } + + return rewardValue; }; /** diff --git a/lib/protocol/networks.js b/lib/protocol/networks.js index 312fa461..3e6bb99e 100644 --- a/lib/protocol/networks.js +++ b/lib/protocol/networks.js @@ -217,7 +217,7 @@ main.pow = { * @default */ - targetTimespan: 1 * 24 * 60 * 60, + targetTimespan: 6 * 40, /** * Average block time. @@ -235,6 +235,38 @@ main.pow = { retargetInterval: 1, + /** + * Average retarget interval in blocks. + * @const {Number} + * @default + */ + + averagingInterval: 6, + + /** + * Average retarget interval in blocks. + * @const {Number} + * @default + */ + + averagingIntervalTimespan: 6 * 40, + + /** + * Adjust Target Timespan Max. + * @const {Number} + * @default + */ + + adjustUp: 2, + + /** + * Adjust Target Timespan Min. + * @const {Number} + * @default + */ + + adjustDown: 3, + /** * Whether to reset target if a block * has not been mined recently.