2022-03-12 14:55:42 +01:00
|
|
|
import config from '../config';
|
|
|
|
import { IDifficultyAdjustment } from '../mempool.interfaces';
|
|
|
|
import blocks from './blocks';
|
|
|
|
|
2022-08-19 23:48:59 +09:00
|
|
|
export interface DifficultyAdjustment {
|
|
|
|
progressPercent: number; // Percent: 0 to 100
|
|
|
|
difficultyChange: number; // Percent: -75 to 300
|
|
|
|
estimatedRetargetDate: number; // Unix time in ms
|
|
|
|
remainingBlocks: number; // Block count
|
|
|
|
remainingTime: number; // Duration of time in ms
|
|
|
|
previousRetarget: number; // Percent: -75 to 300
|
2023-03-07 19:19:28 -06:00
|
|
|
previousTime: number; // Unix time in ms
|
2022-08-19 23:48:59 +09:00
|
|
|
nextRetargetHeight: number; // Block Height
|
|
|
|
timeAvg: number; // Duration of time in ms
|
|
|
|
timeOffset: number; // (Testnet) Time since last block (cap @ 20min) in ms
|
2023-03-07 19:19:28 -06:00
|
|
|
expectedBlocks: number; // Block count
|
2022-08-19 23:48:59 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
export function calcDifficultyAdjustment(
|
|
|
|
DATime: number,
|
|
|
|
nowSeconds: number,
|
|
|
|
blockHeight: number,
|
|
|
|
previousRetarget: number,
|
|
|
|
network: string,
|
|
|
|
latestBlockTimestamp: number,
|
|
|
|
): DifficultyAdjustment {
|
2022-08-20 11:24:48 +09:00
|
|
|
const EPOCH_BLOCK_LENGTH = 2016; // Bitcoin mainnet
|
|
|
|
const BLOCK_SECONDS_TARGET = 600; // Bitcoin mainnet
|
2022-08-20 13:02:22 +09:00
|
|
|
const TESTNET_MAX_BLOCK_SECONDS = 1200; // Bitcoin testnet
|
2022-08-19 23:48:59 +09:00
|
|
|
|
2023-03-26 07:27:11 +09:00
|
|
|
const diffSeconds = Math.max(0, nowSeconds - DATime);
|
2022-08-19 23:48:59 +09:00
|
|
|
const blocksInEpoch = (blockHeight >= 0) ? blockHeight % EPOCH_BLOCK_LENGTH : 0;
|
|
|
|
const progressPercent = (blockHeight >= 0) ? blocksInEpoch / EPOCH_BLOCK_LENGTH * 100 : 100;
|
|
|
|
const remainingBlocks = EPOCH_BLOCK_LENGTH - blocksInEpoch;
|
|
|
|
const nextRetargetHeight = (blockHeight >= 0) ? blockHeight + remainingBlocks : 0;
|
2023-03-07 19:19:28 -06:00
|
|
|
const expectedBlocks = diffSeconds / BLOCK_SECONDS_TARGET;
|
2023-05-11 11:18:58 -06:00
|
|
|
const actualTimespan = (blocksInEpoch === 2015 ? latestBlockTimestamp : nowSeconds) - DATime;
|
2022-08-19 23:48:59 +09:00
|
|
|
|
|
|
|
let difficultyChange = 0;
|
2023-03-26 07:27:11 +09:00
|
|
|
let timeAvgSecs = blocksInEpoch ? diffSeconds / blocksInEpoch : BLOCK_SECONDS_TARGET;
|
2023-03-26 17:01:04 +09:00
|
|
|
|
2023-05-11 11:18:58 -06:00
|
|
|
difficultyChange = (BLOCK_SECONDS_TARGET / (actualTimespan / (blocksInEpoch + 1)) - 1) * 100;
|
2023-03-26 17:01:04 +09:00
|
|
|
// Max increase is x4 (+300%)
|
|
|
|
if (difficultyChange > 300) {
|
|
|
|
difficultyChange = 300;
|
|
|
|
}
|
|
|
|
// Max decrease is /4 (-75%)
|
|
|
|
if (difficultyChange < -75) {
|
|
|
|
difficultyChange = -75;
|
2022-08-19 23:48:59 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Testnet difficulty is set to 1 after 20 minutes of no blocks,
|
|
|
|
// therefore the time between blocks will always be below 20 minutes (1200s).
|
|
|
|
let timeOffset = 0;
|
|
|
|
if (network === 'testnet') {
|
2022-08-20 13:02:22 +09:00
|
|
|
if (timeAvgSecs > TESTNET_MAX_BLOCK_SECONDS) {
|
|
|
|
timeAvgSecs = TESTNET_MAX_BLOCK_SECONDS;
|
2022-08-19 23:48:59 +09:00
|
|
|
}
|
|
|
|
|
2022-08-20 13:02:22 +09:00
|
|
|
const secondsSinceLastBlock = nowSeconds - latestBlockTimestamp;
|
|
|
|
if (secondsSinceLastBlock + timeAvgSecs > TESTNET_MAX_BLOCK_SECONDS) {
|
|
|
|
timeOffset = -Math.min(secondsSinceLastBlock, TESTNET_MAX_BLOCK_SECONDS) * 1000;
|
2022-08-19 23:48:59 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-20 13:02:22 +09:00
|
|
|
const timeAvg = Math.floor(timeAvgSecs * 1000);
|
2022-08-19 23:48:59 +09:00
|
|
|
const remainingTime = remainingBlocks * timeAvg;
|
|
|
|
const estimatedRetargetDate = remainingTime + nowSeconds * 1000;
|
|
|
|
|
|
|
|
return {
|
|
|
|
progressPercent,
|
|
|
|
difficultyChange,
|
|
|
|
estimatedRetargetDate,
|
|
|
|
remainingBlocks,
|
|
|
|
remainingTime,
|
|
|
|
previousRetarget,
|
2023-03-07 19:19:28 -06:00
|
|
|
previousTime: DATime,
|
2022-08-19 23:48:59 +09:00
|
|
|
nextRetargetHeight,
|
|
|
|
timeAvg,
|
|
|
|
timeOffset,
|
2023-03-07 19:19:28 -06:00
|
|
|
expectedBlocks,
|
2022-08-19 23:48:59 +09:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-03-12 14:55:42 +01:00
|
|
|
class DifficultyAdjustmentApi {
|
2022-08-27 10:25:24 +02:00
|
|
|
public getDifficultyAdjustment(): IDifficultyAdjustment | null {
|
2022-03-12 14:55:42 +01:00
|
|
|
const DATime = blocks.getLastDifficultyAdjustmentTime();
|
|
|
|
const previousRetarget = blocks.getPreviousDifficultyRetarget();
|
|
|
|
const blockHeight = blocks.getCurrentBlockHeight();
|
|
|
|
const blocksCache = blocks.getBlocks();
|
|
|
|
const latestBlock = blocksCache[blocksCache.length - 1];
|
2022-08-27 10:25:24 +02:00
|
|
|
if (!latestBlock) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-08-19 22:00:47 +09:00
|
|
|
const nowSeconds = Math.floor(new Date().getTime() / 1000);
|
2022-03-12 14:55:42 +01:00
|
|
|
|
2022-08-19 23:48:59 +09:00
|
|
|
return calcDifficultyAdjustment(
|
|
|
|
DATime, nowSeconds, blockHeight, previousRetarget,
|
|
|
|
config.MEMPOOL.NETWORK, latestBlock.timestamp
|
|
|
|
);
|
2022-03-12 14:55:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new DifficultyAdjustmentApi();
|