2020-02-16 16:15:07 +01:00
|
|
|
const config = require('../../mempool-config.json');
|
2020-02-23 13:16:50 +01:00
|
|
|
import { MempoolBlock, TransactionExtended } from '../interfaces';
|
2020-05-24 11:29:30 +02:00
|
|
|
import { Common } from './common';
|
2020-02-16 16:15:07 +01:00
|
|
|
|
|
|
|
class MempoolBlocks {
|
|
|
|
private mempoolBlocks: MempoolBlock[] = [];
|
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
public getMempoolBlocks(): MempoolBlock[] {
|
|
|
|
return this.mempoolBlocks;
|
|
|
|
}
|
|
|
|
|
2020-02-23 13:16:50 +01:00
|
|
|
public updateMempoolBlocks(memPool: { [txid: string]: TransactionExtended }): void {
|
2020-02-16 16:15:07 +01:00
|
|
|
const latestMempool = memPool;
|
2020-02-23 13:16:50 +01:00
|
|
|
const memPoolArray: TransactionExtended[] = [];
|
2020-02-16 16:15:07 +01:00
|
|
|
for (const i in latestMempool) {
|
|
|
|
if (latestMempool.hasOwnProperty(i)) {
|
|
|
|
memPoolArray.push(latestMempool[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memPoolArray.sort((a, b) => b.feePerVsize - a.feePerVsize);
|
|
|
|
const transactionsSorted = memPoolArray.filter((tx) => tx.feePerVsize);
|
|
|
|
this.mempoolBlocks = this.calculateMempoolBlocks(transactionsSorted);
|
|
|
|
}
|
|
|
|
|
2020-02-23 13:16:50 +01:00
|
|
|
private calculateMempoolBlocks(transactionsSorted: TransactionExtended[]): MempoolBlock[] {
|
2020-02-16 16:15:07 +01:00
|
|
|
const mempoolBlocks: MempoolBlock[] = [];
|
|
|
|
let blockWeight = 0;
|
|
|
|
let blockSize = 0;
|
2020-02-23 13:16:50 +01:00
|
|
|
let transactions: TransactionExtended[] = [];
|
2020-02-16 16:15:07 +01:00
|
|
|
transactionsSorted.forEach((tx) => {
|
2020-03-12 15:56:07 +01:00
|
|
|
if (blockWeight + tx.vsize < 1000000 || mempoolBlocks.length === config.DEFAULT_PROJECTED_BLOCKS_AMOUNT - 1) {
|
2020-02-16 16:15:07 +01:00
|
|
|
blockWeight += tx.vsize;
|
|
|
|
blockSize += tx.size;
|
|
|
|
transactions.push(tx);
|
|
|
|
} else {
|
|
|
|
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length));
|
|
|
|
blockWeight = 0;
|
|
|
|
blockSize = 0;
|
|
|
|
transactions = [];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (transactions.length) {
|
|
|
|
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length));
|
|
|
|
}
|
|
|
|
return mempoolBlocks;
|
|
|
|
}
|
|
|
|
|
2020-05-24 11:29:30 +02:00
|
|
|
private dataToMempoolBlocks(transactions: TransactionExtended[],
|
|
|
|
blockSize: number, blockVSize: number, blocksIndex: number): MempoolBlock {
|
2020-03-19 20:07:12 +01:00
|
|
|
let rangeLength = 4;
|
2020-02-16 16:15:07 +01:00
|
|
|
if (blocksIndex === 0) {
|
|
|
|
rangeLength = 8;
|
|
|
|
}
|
|
|
|
if (transactions.length > 4000) {
|
2020-03-19 20:07:12 +01:00
|
|
|
rangeLength = 6;
|
2020-02-16 16:15:07 +01:00
|
|
|
} else if (transactions.length > 10000) {
|
|
|
|
rangeLength = 8;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
blockSize: blockSize,
|
|
|
|
blockVSize: blockVSize,
|
|
|
|
nTx: transactions.length,
|
2020-03-17 15:53:20 +01:00
|
|
|
totalFees: transactions.reduce((acc, cur) => acc + cur.fee, 0),
|
2020-05-24 11:29:30 +02:00
|
|
|
medianFee: Common.median(transactions.map((tx) => tx.feePerVsize)),
|
|
|
|
feeRange: Common.getFeesInRange(transactions, rangeLength),
|
2020-02-16 16:15:07 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new MempoolBlocks();
|