Add mempool position improvements to updateMempoolBlocks

This commit is contained in:
Mononaut 2023-04-21 09:20:56 +09:00
parent 3b8bcc4da5
commit a22703d547
No known key found for this signature in database
GPG Key ID: A3F058E41374C04E

View File

@ -54,7 +54,14 @@ class MempoolBlocks {
}); });
// First sort // First sort
memPoolArray.sort((a, b) => b.feePerVsize - a.feePerVsize); memPoolArray.sort((a, b) => {
if (a.feePerVsize === b.feePerVsize) {
// tie-break by lexicographic txid order for stability
return a.txid < b.txid ? -1 : 1;
} else {
return b.feePerVsize - a.feePerVsize;
}
});
// Loop through and traverse all ancestors and sum up all the sizes + fees // Loop through and traverse all ancestors and sum up all the sizes + fees
// Pass down size + fee to all unconfirmed children // Pass down size + fee to all unconfirmed children
@ -68,7 +75,14 @@ class MempoolBlocks {
}); });
// Final sort, by effective fee // Final sort, by effective fee
memPoolArray.sort((a, b) => b.effectiveFeePerVsize - a.effectiveFeePerVsize); memPoolArray.sort((a, b) => {
if (a.effectiveFeePerVsize === b.effectiveFeePerVsize) {
// tie-break by lexicographic txid order for stability
return a.txid < b.txid ? -1 : 1;
} else {
return b.effectiveFeePerVsize - a.effectiveFeePerVsize;
}
});
const end = new Date().getTime(); const end = new Date().getTime();
const time = end - start; const time = end - start;
@ -88,14 +102,26 @@ class MempoolBlocks {
private calculateMempoolBlocks(transactionsSorted: TransactionExtended[]): MempoolBlockWithTransactions[] { private calculateMempoolBlocks(transactionsSorted: TransactionExtended[]): MempoolBlockWithTransactions[] {
const mempoolBlocks: MempoolBlockWithTransactions[] = []; const mempoolBlocks: MempoolBlockWithTransactions[] = [];
let blockWeight = 0; let blockWeight = 0;
let blockVsize = 0;
let transactions: TransactionExtended[] = []; let transactions: TransactionExtended[] = [];
transactionsSorted.forEach((tx) => { transactionsSorted.forEach((tx) => {
if (blockWeight + tx.weight <= config.MEMPOOL.BLOCK_WEIGHT_UNITS if (blockWeight + tx.weight <= config.MEMPOOL.BLOCK_WEIGHT_UNITS
|| mempoolBlocks.length === config.MEMPOOL.MEMPOOL_BLOCKS_AMOUNT - 1) { || mempoolBlocks.length === config.MEMPOOL.MEMPOOL_BLOCKS_AMOUNT - 1) {
tx.position = {
block: mempoolBlocks.length,
vsize: blockVsize + (tx.vsize / 2),
};
blockWeight += tx.weight; blockWeight += tx.weight;
blockVsize += tx.vsize;
transactions.push(tx); transactions.push(tx);
} else { } else {
mempoolBlocks.push(this.dataToMempoolBlocks(transactions)); mempoolBlocks.push(this.dataToMempoolBlocks(transactions));
blockVsize = 0;
tx.position = {
block: mempoolBlocks.length,
vsize: blockVsize + (tx.vsize / 2),
};
blockVsize += tx.vsize;
blockWeight = tx.weight; blockWeight = tx.weight;
transactions = [tx]; transactions = [tx];
} }