2021-03-18 23:47:40 +07:00
|
|
|
import logger from '../logger';
|
2022-05-31 21:36:42 +00:00
|
|
|
import { MempoolBlock, TransactionExtended, TransactionStripped, MempoolBlockWithTransactions, MempoolBlockDelta } from '../mempool.interfaces';
|
2020-05-24 16:29:30 +07:00
|
|
|
import { Common } from './common';
|
2021-03-18 05:52:46 +00:00
|
|
|
import config from '../config';
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
class MempoolBlocks {
|
2020-06-08 02:08:51 +07:00
|
|
|
private mempoolBlocks: MempoolBlockWithTransactions[] = [];
|
2022-05-31 21:36:42 +00:00
|
|
|
private mempoolBlockDeltas: MempoolBlockDelta[] = [];
|
2020-02-16 22:15:07 +07:00
|
|
|
|
|
|
|
constructor() {}
|
|
|
|
|
|
|
|
public getMempoolBlocks(): MempoolBlock[] {
|
2020-06-08 02:08:51 +07:00
|
|
|
return this.mempoolBlocks.map((block) => {
|
|
|
|
return {
|
|
|
|
blockSize: block.blockSize,
|
|
|
|
blockVSize: block.blockVSize,
|
|
|
|
nTx: block.nTx,
|
|
|
|
totalFees: block.totalFees,
|
|
|
|
medianFee: block.medianFee,
|
|
|
|
feeRange: block.feeRange,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public getMempoolBlocksWithTransactions(): MempoolBlockWithTransactions[] {
|
2020-02-16 22:15:07 +07:00
|
|
|
return this.mempoolBlocks;
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:36:42 +00:00
|
|
|
public getMempoolBlockDeltas(): MempoolBlockDelta[] {
|
2022-06-02 01:29:03 +04:00
|
|
|
return this.mempoolBlockDeltas;
|
2022-05-31 21:36:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-23 19:16:50 +07:00
|
|
|
public updateMempoolBlocks(memPool: { [txid: string]: TransactionExtended }): void {
|
2020-02-16 22:15:07 +07:00
|
|
|
const latestMempool = memPool;
|
2020-02-23 19:16:50 +07:00
|
|
|
const memPoolArray: TransactionExtended[] = [];
|
2020-02-16 22:15:07 +07:00
|
|
|
for (const i in latestMempool) {
|
|
|
|
if (latestMempool.hasOwnProperty(i)) {
|
|
|
|
memPoolArray.push(latestMempool[i]);
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 23:47:40 +07:00
|
|
|
const start = new Date().getTime();
|
|
|
|
|
|
|
|
// Clear bestDescendants & ancestors
|
|
|
|
memPoolArray.forEach((tx) => {
|
|
|
|
tx.bestDescendant = null;
|
|
|
|
tx.ancestors = [];
|
|
|
|
tx.cpfpChecked = false;
|
|
|
|
if (!tx.effectiveFeePerVsize) {
|
|
|
|
tx.effectiveFeePerVsize = tx.feePerVsize;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// First sort
|
2020-02-16 22:15:07 +07:00
|
|
|
memPoolArray.sort((a, b) => b.feePerVsize - a.feePerVsize);
|
2021-03-18 23:47:40 +07:00
|
|
|
|
|
|
|
// Loop through and traverse all ancestors and sum up all the sizes + fees
|
|
|
|
// Pass down size + fee to all unconfirmed children
|
|
|
|
let sizes = 0;
|
|
|
|
memPoolArray.forEach((tx, i) => {
|
2021-04-10 21:26:05 +04:00
|
|
|
sizes += tx.weight;
|
2021-03-18 23:47:40 +07:00
|
|
|
if (sizes > 4000000 * 8) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Common.setRelativesAndGetCpfpInfo(tx, memPool);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Final sort, by effective fee
|
|
|
|
memPoolArray.sort((a, b) => b.effectiveFeePerVsize - a.effectiveFeePerVsize);
|
|
|
|
|
|
|
|
const end = new Date().getTime();
|
|
|
|
const time = end - start;
|
|
|
|
logger.debug('Mempool blocks calculated in ' + time / 1000 + ' seconds');
|
|
|
|
|
2022-05-31 21:36:42 +00:00
|
|
|
const { blocks, deltas } = this.calculateMempoolBlocks(memPoolArray, this.mempoolBlocks);
|
2022-06-02 01:29:03 +04:00
|
|
|
this.mempoolBlocks = blocks;
|
|
|
|
this.mempoolBlockDeltas = deltas;
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2022-06-02 01:29:03 +04:00
|
|
|
private calculateMempoolBlocks(transactionsSorted: TransactionExtended[], prevBlocks: MempoolBlockWithTransactions[]):
|
|
|
|
{ blocks: MempoolBlockWithTransactions[], deltas: MempoolBlockDelta[] } {
|
2020-06-08 02:08:51 +07:00
|
|
|
const mempoolBlocks: MempoolBlockWithTransactions[] = [];
|
2022-05-31 21:36:42 +00:00
|
|
|
const mempoolBlockDeltas: MempoolBlockDelta[] = [];
|
2021-08-14 03:24:31 +03:00
|
|
|
let blockWeight = 0;
|
2020-02-16 22:15:07 +07:00
|
|
|
let blockSize = 0;
|
2020-02-23 19:16:50 +07:00
|
|
|
let transactions: TransactionExtended[] = [];
|
2020-02-16 22:15:07 +07:00
|
|
|
transactionsSorted.forEach((tx) => {
|
2021-10-05 04:11:13 +04:00
|
|
|
if (blockWeight + tx.weight <= config.MEMPOOL.BLOCK_WEIGHT_UNITS
|
|
|
|
|| mempoolBlocks.length === config.MEMPOOL.MEMPOOL_BLOCKS_AMOUNT - 1) {
|
2021-08-14 03:24:31 +03:00
|
|
|
blockWeight += tx.weight;
|
2020-02-16 22:15:07 +07:00
|
|
|
blockSize += tx.size;
|
|
|
|
transactions.push(tx);
|
|
|
|
} else {
|
2021-08-14 03:24:31 +03:00
|
|
|
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length));
|
|
|
|
blockWeight = tx.weight;
|
2020-06-08 18:55:53 +07:00
|
|
|
blockSize = tx.size;
|
|
|
|
transactions = [tx];
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
if (transactions.length) {
|
2021-08-14 03:24:31 +03:00
|
|
|
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length));
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
2022-05-31 21:36:42 +00:00
|
|
|
// Calculate change from previous block states
|
|
|
|
for (let i = 0; i < Math.max(mempoolBlocks.length, prevBlocks.length); i++) {
|
2022-06-02 01:29:03 +04:00
|
|
|
let added: TransactionStripped[] = [];
|
|
|
|
let removed: string[] = [];
|
2022-05-31 21:36:42 +00:00
|
|
|
if (mempoolBlocks[i] && !prevBlocks[i]) {
|
2022-06-02 01:29:03 +04:00
|
|
|
added = mempoolBlocks[i].transactions;
|
2022-05-31 21:36:42 +00:00
|
|
|
} else if (!mempoolBlocks[i] && prevBlocks[i]) {
|
2022-06-02 01:29:03 +04:00
|
|
|
removed = prevBlocks[i].transactions.map(tx => tx.txid);
|
2022-05-31 21:36:42 +00:00
|
|
|
} else if (mempoolBlocks[i] && prevBlocks[i]) {
|
2022-06-02 01:29:03 +04:00
|
|
|
const prevIds = {};
|
|
|
|
const newIds = {};
|
2022-05-31 21:36:42 +00:00
|
|
|
prevBlocks[i].transactions.forEach(tx => {
|
2022-06-02 01:29:03 +04:00
|
|
|
prevIds[tx.txid] = true;
|
|
|
|
});
|
2022-05-31 21:36:42 +00:00
|
|
|
mempoolBlocks[i].transactions.forEach(tx => {
|
2022-06-02 01:29:03 +04:00
|
|
|
newIds[tx.txid] = true;
|
|
|
|
});
|
2022-05-31 21:36:42 +00:00
|
|
|
prevBlocks[i].transactions.forEach(tx => {
|
2022-06-02 01:29:03 +04:00
|
|
|
if (!newIds[tx.txid]) {
|
|
|
|
removed.push(tx.txid);
|
|
|
|
}
|
|
|
|
});
|
2022-05-31 21:36:42 +00:00
|
|
|
mempoolBlocks[i].transactions.forEach(tx => {
|
2022-06-02 01:29:03 +04:00
|
|
|
if (!prevIds[tx.txid]) {
|
|
|
|
added.push(tx);
|
|
|
|
}
|
|
|
|
});
|
2022-05-31 21:36:42 +00:00
|
|
|
}
|
|
|
|
mempoolBlockDeltas.push({
|
|
|
|
added,
|
|
|
|
removed
|
2022-06-02 01:29:03 +04:00
|
|
|
});
|
2022-05-31 21:36:42 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
blocks: mempoolBlocks,
|
|
|
|
deltas: mempoolBlockDeltas
|
2022-06-02 01:29:03 +04:00
|
|
|
};
|
2020-02-16 22:15:07 +07:00
|
|
|
}
|
|
|
|
|
2020-05-24 16:29:30 +07:00
|
|
|
private dataToMempoolBlocks(transactions: TransactionExtended[],
|
2021-08-14 03:24:31 +03:00
|
|
|
blockSize: number, blockWeight: number, blocksIndex: number): MempoolBlockWithTransactions {
|
2020-03-20 02:07:12 +07:00
|
|
|
let rangeLength = 4;
|
2020-02-16 22:15:07 +07:00
|
|
|
if (blocksIndex === 0) {
|
|
|
|
rangeLength = 8;
|
|
|
|
}
|
|
|
|
if (transactions.length > 4000) {
|
2020-03-20 02:07:12 +07:00
|
|
|
rangeLength = 6;
|
2020-02-16 22:15:07 +07:00
|
|
|
} else if (transactions.length > 10000) {
|
|
|
|
rangeLength = 8;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
blockSize: blockSize,
|
2021-08-14 03:24:31 +03:00
|
|
|
blockVSize: blockWeight / 4,
|
2020-02-16 22:15:07 +07:00
|
|
|
nTx: transactions.length,
|
2020-03-17 21:53:20 +07:00
|
|
|
totalFees: transactions.reduce((acc, cur) => acc + cur.fee, 0),
|
2021-03-18 23:47:40 +07:00
|
|
|
medianFee: Common.percentile(transactions.map((tx) => tx.effectiveFeePerVsize), config.MEMPOOL.RECOMMENDED_FEE_PERCENTILE),
|
2020-05-24 16:29:30 +07:00
|
|
|
feeRange: Common.getFeesInRange(transactions, rangeLength),
|
2020-06-08 02:08:51 +07:00
|
|
|
transactionIds: transactions.map((tx) => tx.txid),
|
2022-05-30 17:29:30 +00:00
|
|
|
transactions: transactions.map((tx) => Common.stripTransaction(tx)),
|
2020-02-16 22:15:07 +07:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new MempoolBlocks();
|