Update Difficulty Calculations

This commit is contained in:
Sky Young 2018-05-14 16:38:03 -07:00
parent 4c03efc1ca
commit c9d676efb4
3 changed files with 71 additions and 18 deletions

View File

@ -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);

View File

@ -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;
};
/**

View File

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