Merge pull request #3072 from mempool/mononaut/optimize-mempool-block-7

Optimize mempool block 7 data
This commit is contained in:
softsimon 2023-03-04 10:46:18 +09:00 committed by GitHub
commit 32466f4d46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -97,14 +97,14 @@ class MempoolBlocks {
blockSize += tx.size; blockSize += tx.size;
transactions.push(tx); transactions.push(tx);
} else { } else {
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length)); mempoolBlocks.push(this.dataToMempoolBlocks(transactions, mempoolBlocks.length));
blockWeight = tx.weight; blockWeight = tx.weight;
blockSize = tx.size; blockSize = tx.size;
transactions = [tx]; transactions = [tx];
} }
}); });
if (transactions.length) { if (transactions.length) {
mempoolBlocks.push(this.dataToMempoolBlocks(transactions, blockSize, blockWeight, mempoolBlocks.length)); mempoolBlocks.push(this.dataToMempoolBlocks(transactions, mempoolBlocks.length));
} }
return mempoolBlocks; return mempoolBlocks;
@ -281,7 +281,7 @@ class MempoolBlocks {
const mempoolBlocks = blocks.map((transactions, blockIndex) => { const mempoolBlocks = blocks.map((transactions, blockIndex) => {
return this.dataToMempoolBlocks(transactions.map(tx => { return this.dataToMempoolBlocks(transactions.map(tx => {
return mempool[tx.txid] || null; return mempool[tx.txid] || null;
}).filter(tx => !!tx), undefined, undefined, blockIndex); }).filter(tx => !!tx), blockIndex);
}); });
if (saveResults) { if (saveResults) {
@ -293,18 +293,17 @@ class MempoolBlocks {
return mempoolBlocks; return mempoolBlocks;
} }
private dataToMempoolBlocks(transactions: TransactionExtended[], private dataToMempoolBlocks(transactions: TransactionExtended[], blocksIndex: number): MempoolBlockWithTransactions {
blockSize: number | undefined, blockWeight: number | undefined, blocksIndex: number): MempoolBlockWithTransactions { let totalSize = 0;
let totalSize = blockSize || 0; let totalWeight = 0;
let totalWeight = blockWeight || 0; const fitTransactions: TransactionExtended[] = [];
if (blockSize === undefined && blockWeight === undefined) { transactions.forEach(tx => {
totalSize = 0; totalSize += tx.size;
totalWeight = 0; totalWeight += tx.weight;
transactions.forEach(tx => { if ((totalWeight + tx.weight) <= config.MEMPOOL.BLOCK_WEIGHT_UNITS * 1.2) {
totalSize += tx.size; fitTransactions.push(tx);
totalWeight += tx.weight; }
}); });
}
let rangeLength = 4; let rangeLength = 4;
if (blocksIndex === 0) { if (blocksIndex === 0) {
rangeLength = 8; rangeLength = 8;
@ -322,7 +321,7 @@ class MempoolBlocks {
medianFee: Common.percentile(transactions.map((tx) => tx.effectiveFeePerVsize), config.MEMPOOL.RECOMMENDED_FEE_PERCENTILE), medianFee: Common.percentile(transactions.map((tx) => tx.effectiveFeePerVsize), config.MEMPOOL.RECOMMENDED_FEE_PERCENTILE),
feeRange: Common.getFeesInRange(transactions, rangeLength), feeRange: Common.getFeesInRange(transactions, rangeLength),
transactionIds: transactions.map((tx) => tx.txid), transactionIds: transactions.map((tx) => tx.txid),
transactions: transactions.map((tx) => Common.stripTransaction(tx)), transactions: fitTransactions.map((tx) => Common.stripTransaction(tx)),
}; };
} }
} }