mirror of
https://github.com/mempool/mempool.git
synced 2025-02-23 14:40:38 +01:00
Refactor difficulty API logic
This commit is contained in:
parent
d700b5f145
commit
87c9f881c0
1 changed files with 6 additions and 4 deletions
|
@ -23,7 +23,8 @@ export function calcDifficultyAdjustment(
|
||||||
latestBlockTimestamp: number,
|
latestBlockTimestamp: number,
|
||||||
): DifficultyAdjustment {
|
): DifficultyAdjustment {
|
||||||
const ESTIMATE_LAG_BLOCKS = 146; // For first 7.2% of epoch, don't estimate.
|
const ESTIMATE_LAG_BLOCKS = 146; // For first 7.2% of epoch, don't estimate.
|
||||||
const EPOCH_BLOCK_LENGTH = 2016;
|
const EPOCH_BLOCK_LENGTH = 2016; // Bitcoin mainnet
|
||||||
|
const BLOCK_SECONDS_TARGET = 600; // Bitcoin mainnet
|
||||||
|
|
||||||
const diffSeconds = nowSeconds - DATime;
|
const diffSeconds = nowSeconds - DATime;
|
||||||
const blocksInEpoch = (blockHeight >= 0) ? blockHeight % EPOCH_BLOCK_LENGTH : 0;
|
const blocksInEpoch = (blockHeight >= 0) ? blockHeight % EPOCH_BLOCK_LENGTH : 0;
|
||||||
|
@ -32,9 +33,12 @@ export function calcDifficultyAdjustment(
|
||||||
const nextRetargetHeight = (blockHeight >= 0) ? blockHeight + remainingBlocks : 0;
|
const nextRetargetHeight = (blockHeight >= 0) ? blockHeight + remainingBlocks : 0;
|
||||||
|
|
||||||
let difficultyChange = 0;
|
let difficultyChange = 0;
|
||||||
|
let timeAvgMins = 10;
|
||||||
// Only calculate the estimate once we have 7.2% of blocks in current epoch
|
// Only calculate the estimate once we have 7.2% of blocks in current epoch
|
||||||
if (blocksInEpoch >= ESTIMATE_LAG_BLOCKS) {
|
if (blocksInEpoch >= ESTIMATE_LAG_BLOCKS) {
|
||||||
difficultyChange = (600 / (diffSeconds / blocksInEpoch) - 1) * 100;
|
const secondsPerBlock = diffSeconds / blocksInEpoch;
|
||||||
|
timeAvgMins = secondsPerBlock / 60;
|
||||||
|
difficultyChange = (BLOCK_SECONDS_TARGET / secondsPerBlock - 1) * 100;
|
||||||
// Max increase is x4 (+300%)
|
// Max increase is x4 (+300%)
|
||||||
if (difficultyChange > 300) {
|
if (difficultyChange > 300) {
|
||||||
difficultyChange = 300;
|
difficultyChange = 300;
|
||||||
|
@ -45,8 +49,6 @@ export function calcDifficultyAdjustment(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let timeAvgMins = blocksInEpoch >= ESTIMATE_LAG_BLOCKS ? diffSeconds / blocksInEpoch / 60 : 10;
|
|
||||||
|
|
||||||
// Testnet difficulty is set to 1 after 20 minutes of no blocks,
|
// Testnet difficulty is set to 1 after 20 minutes of no blocks,
|
||||||
// therefore the time between blocks will always be below 20 minutes (1200s).
|
// therefore the time between blocks will always be below 20 minutes (1200s).
|
||||||
let timeOffset = 0;
|
let timeOffset = 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue