Adding previous difficulty retarget to the difficulty adjustment api. (#652)

refs #640
This commit is contained in:
softsimon 2021-07-23 14:35:04 +03:00 committed by GitHub
parent 775323de3e
commit 037d6a75ea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 7 deletions

View File

@ -11,7 +11,9 @@ class Blocks {
private static INITIAL_BLOCK_AMOUNT = 8; private static INITIAL_BLOCK_AMOUNT = 8;
private blocks: BlockExtended[] = []; private blocks: BlockExtended[] = [];
private currentBlockHeight = 0; private currentBlockHeight = 0;
private currentDifficulty = 0;
private lastDifficultyAdjustmentTime = 0; private lastDifficultyAdjustmentTime = 0;
private previousDifficultyRetarget = 0;
private newBlockCallbacks: ((block: BlockExtended, txIds: string[], transactions: TransactionExtended[]) => void)[] = []; private newBlockCallbacks: ((block: BlockExtended, txIds: string[], transactions: TransactionExtended[]) => void)[] = [];
constructor() { } constructor() { }
@ -47,6 +49,11 @@ class Blocks {
const blockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff); const blockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff);
const block = await bitcoinApi.$getBlock(blockHash); const block = await bitcoinApi.$getBlock(blockHash);
this.lastDifficultyAdjustmentTime = block.timestamp; this.lastDifficultyAdjustmentTime = block.timestamp;
this.currentDifficulty = block.difficulty;
const previousPeriodBlockHash = await bitcoinApi.$getBlockHash(blockHeightTip - heightDiff - 2016);
const previousPeriodBlock = await bitcoinApi.$getBlock(previousPeriodBlockHash);
this.previousDifficultyRetarget = (block.difficulty - previousPeriodBlock.difficulty) / previousPeriodBlock.difficulty * 100;
} }
while (this.currentBlockHeight < blockHeightTip) { while (this.currentBlockHeight < blockHeightTip) {
@ -101,7 +108,9 @@ class Blocks {
blockExtended.feeRange = transactions.length > 1 ? Common.getFeesInRange(transactions, 8) : [0, 0]; blockExtended.feeRange = transactions.length > 1 ? Common.getFeesInRange(transactions, 8) : [0, 0];
if (block.height % 2016 === 0) { if (block.height % 2016 === 0) {
this.previousDifficultyRetarget = (block.difficulty - this.currentDifficulty) / this.currentDifficulty * 100;
this.lastDifficultyAdjustmentTime = block.timestamp; this.lastDifficultyAdjustmentTime = block.timestamp;
this.currentDifficulty = block.difficulty;
} }
this.blocks.push(blockExtended); this.blocks.push(blockExtended);
@ -122,6 +131,10 @@ class Blocks {
return this.lastDifficultyAdjustmentTime; return this.lastDifficultyAdjustmentTime;
} }
public getPreviousDifficultyRetarget(): number {
return this.previousDifficultyRetarget;
}
public getCurrentBlockHeight(): number { public getCurrentBlockHeight(): number {
return this.currentBlockHeight; return this.currentBlockHeight;
} }

View File

@ -172,6 +172,7 @@ class WebsocketHandler {
'mempoolInfo': memPool.getMempoolInfo(), 'mempoolInfo': memPool.getMempoolInfo(),
'vBytesPerSecond': memPool.getVBytesPerSecond(), 'vBytesPerSecond': memPool.getVBytesPerSecond(),
'lastDifficultyAdjustment': blocks.getLastDifficultyAdjustmentTime(), 'lastDifficultyAdjustment': blocks.getLastDifficultyAdjustmentTime(),
'previousRetarget': blocks.getPreviousDifficultyRetarget(),
'blocks': _blocks, 'blocks': _blocks,
'conversions': fiatConversion.getConversionRates(), 'conversions': fiatConversion.getConversionRates(),
'mempool-blocks': mempoolBlocks.getMempoolBlocks(), 'mempool-blocks': mempoolBlocks.getMempoolBlocks(),
@ -384,6 +385,7 @@ class WebsocketHandler {
'block': block, 'block': block,
'mempoolInfo': memPool.getMempoolInfo(), 'mempoolInfo': memPool.getMempoolInfo(),
'lastDifficultyAdjustment': blocks.getLastDifficultyAdjustmentTime(), 'lastDifficultyAdjustment': blocks.getLastDifficultyAdjustmentTime(),
'previousRetarget': blocks.getPreviousDifficultyRetarget(),
}; };
if (mBlocks && client['want-mempool-blocks']) { if (mBlocks && client['want-mempool-blocks']) {

View File

@ -681,10 +681,10 @@ class Routes {
try { try {
const now = new Date().getTime() / 1000; const now = new Date().getTime() / 1000;
const DATime = blocks.getLastDifficultyAdjustmentTime(); const DATime = blocks.getLastDifficultyAdjustmentTime();
const previousRetarget = blocks.getPreviousDifficultyRetarget();
const diff = now - DATime; const diff = now - DATime;
const blockHeight = blocks.getCurrentBlockHeight(); const blockHeight = blocks.getCurrentBlockHeight();
const blocksInEpoch = blockHeight % 2016; const blocksInEpoch = blockHeight % 2016;
const estimatedBlocks = Math.round(diff / 60 / 10);
const difficultyChange = (600 / (diff / blocksInEpoch) - 1) * 100; const difficultyChange = (600 / (diff / blocksInEpoch) - 1) * 100;
const timeAvgDiff = difficultyChange * 0.1; const timeAvgDiff = difficultyChange * 0.1;
@ -709,6 +709,7 @@ class Routes {
estimatedRetargetDate, estimatedRetargetDate,
remainingBlocks, remainingBlocks,
remainingTime, remainingTime,
previousRetarget,
} }
res.json(result); res.json(result);

View File

@ -10,6 +10,7 @@ export interface WebsocketResponse {
mempoolInfo?: MempoolInfo; mempoolInfo?: MempoolInfo;
vBytesPerSecond?: number; vBytesPerSecond?: number;
lastDifficultyAdjustment?: number; lastDifficultyAdjustment?: number;
previousRetarget?: number;
action?: string; action?: string;
data?: string[]; data?: string[];
tx?: Transaction; tx?: Transaction;