mempool/backend/src/api/blocks.ts

86 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-07-21 17:59:47 +03:00
const config = require('../../mempool-config.json');
import bitcoinApi from './bitcoin/electrs-api';
import { Block } from '../interfaces';
2019-07-21 17:59:47 +03:00
class Blocks {
private blocks: Block[] = [];
private currentBlockHeight = 0;
private newBlockCallback: Function = () => {};
constructor() { }
2019-07-21 17:59:47 +03:00
public getBlocks(): Block[] {
2019-07-21 17:59:47 +03:00
return this.blocks;
}
public setNewBlockCallback(fn: Function) {
this.newBlockCallback = fn;
2019-07-21 17:59:47 +03:00
}
public async updateBlocks() {
try {
const blockHeightTip = await bitcoinApi.getBlockHeightTip();
2019-07-21 17:59:47 +03:00
if (this.blocks.length === 0) {
this.currentBlockHeight = blockHeightTip - config.INITIAL_BLOCK_AMOUNT;
2019-07-21 17:59:47 +03:00
} else {
this.currentBlockHeight = this.blocks[this.blocks.length - 1].height;
2019-07-21 17:59:47 +03:00
}
while (this.currentBlockHeight < blockHeightTip) {
if (this.currentBlockHeight === 0) {
this.currentBlockHeight = blockHeightTip;
2019-07-21 17:59:47 +03:00
} else {
this.currentBlockHeight++;
console.log(`New block found (#${this.currentBlockHeight})!`);
2019-07-21 17:59:47 +03:00
}
const blockHash = await bitcoinApi.getBlockHash(this.currentBlockHeight);
const block = await bitcoinApi.getBlock(blockHash);
const txIds = await bitcoinApi.getTxIdsForBlock(blockHash);
block.medianFee = 2;
block.feeRange = [1, 3];
2019-07-21 17:59:47 +03:00
this.blocks.push(block);
if (this.blocks.length > config.KEEP_BLOCK_AMOUNT) {
this.blocks.shift();
}
this.newBlockCallback(block, txIds);
2019-07-21 17:59:47 +03:00
}
} catch (err) {
console.log('updateBlocks error', err);
}
}
2019-07-21 17:59:47 +03:00
private median(numbers: number[]) {
let medianNr = 0;
const numsLen = numbers.length;
numbers.sort();
if (numsLen % 2 === 0) {
medianNr = (numbers[numsLen / 2 - 1] + numbers[numsLen / 2]) / 2;
} else {
medianNr = numbers[(numsLen - 1) / 2];
}
return medianNr;
}
private getFeesInRange(transactions: any[], rangeLength: number) {
const arr = [transactions[transactions.length - 1].feePerVsize];
const chunk = 1 / (rangeLength - 1);
let itemsToAdd = rangeLength - 2;
while (itemsToAdd > 0) {
arr.push(transactions[Math.floor(transactions.length * chunk * itemsToAdd)].feePerVsize);
itemsToAdd--;
}
arr.push(transactions[0].feePerVsize);
return arr;
}
2019-07-21 17:59:47 +03:00
}
export default new Blocks();