Remove difficulty adjustment calculation lag in the backend
This commit is contained in:
parent
edfbede704
commit
c7cab4c877
@ -24,7 +24,6 @@ export function calcDifficultyAdjustment(
|
|||||||
network: string,
|
network: string,
|
||||||
latestBlockTimestamp: number,
|
latestBlockTimestamp: number,
|
||||||
): DifficultyAdjustment {
|
): DifficultyAdjustment {
|
||||||
const ESTIMATE_LAG_BLOCKS = 146; // For first 7.2% of epoch, don't estimate.
|
|
||||||
const EPOCH_BLOCK_LENGTH = 2016; // Bitcoin mainnet
|
const EPOCH_BLOCK_LENGTH = 2016; // Bitcoin mainnet
|
||||||
const BLOCK_SECONDS_TARGET = 600; // Bitcoin mainnet
|
const BLOCK_SECONDS_TARGET = 600; // Bitcoin mainnet
|
||||||
const TESTNET_MAX_BLOCK_SECONDS = 1200; // Bitcoin testnet
|
const TESTNET_MAX_BLOCK_SECONDS = 1200; // Bitcoin testnet
|
||||||
@ -38,8 +37,7 @@ export function calcDifficultyAdjustment(
|
|||||||
|
|
||||||
let difficultyChange = 0;
|
let difficultyChange = 0;
|
||||||
let timeAvgSecs = blocksInEpoch ? diffSeconds / blocksInEpoch : BLOCK_SECONDS_TARGET;
|
let timeAvgSecs = blocksInEpoch ? diffSeconds / blocksInEpoch : BLOCK_SECONDS_TARGET;
|
||||||
// Only calculate the estimate once we have 7.2% of blocks in current epoch
|
|
||||||
if (blocksInEpoch >= ESTIMATE_LAG_BLOCKS) {
|
|
||||||
difficultyChange = (BLOCK_SECONDS_TARGET / timeAvgSecs - 1) * 100;
|
difficultyChange = (BLOCK_SECONDS_TARGET / timeAvgSecs - 1) * 100;
|
||||||
// Max increase is x4 (+300%)
|
// Max increase is x4 (+300%)
|
||||||
if (difficultyChange > 300) {
|
if (difficultyChange > 300) {
|
||||||
@ -49,7 +47,6 @@ export function calcDifficultyAdjustment(
|
|||||||
if (difficultyChange < -75) {
|
if (difficultyChange < -75) {
|
||||||
difficultyChange = -75;
|
difficultyChange = -75;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// 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).
|
||||||
|
Loading…
x
Reference in New Issue
Block a user