Merge calculateMempoolTxCpfp and calculateLocalTxCpfp
This commit is contained in:
		
							parent
							
								
									6c95cd2149
								
							
						
					
					
						commit
						860bc7d14d
					
				@ -19,7 +19,7 @@ import bitcoinClient from './bitcoin-client';
 | 
				
			|||||||
import difficultyAdjustment from '../difficulty-adjustment';
 | 
					import difficultyAdjustment from '../difficulty-adjustment';
 | 
				
			||||||
import transactionRepository from '../../repositories/TransactionRepository';
 | 
					import transactionRepository from '../../repositories/TransactionRepository';
 | 
				
			||||||
import rbfCache from '../rbf-cache';
 | 
					import rbfCache from '../rbf-cache';
 | 
				
			||||||
import { calculateMempoolTxCpfp, calculateLocalTxCpfp } from '../cpfp';
 | 
					import { calculateMempoolTxCpfp } from '../cpfp';
 | 
				
			||||||
import { handleError } from '../../utils/api';
 | 
					import { handleError } from '../../utils/api';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const TXID_REGEX = /^[a-f0-9]{64}$/i;
 | 
					const TXID_REGEX = /^[a-f0-9]{64}$/i;
 | 
				
			||||||
@ -1011,7 +1011,7 @@ class BitcoinRoutes {
 | 
				
			|||||||
        return;
 | 
					        return;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      const cpfpInfo = calculateLocalTxCpfp(transactions[0], mempool.getMempool());
 | 
					      const cpfpInfo = calculateMempoolTxCpfp(transactions[0], mempool.getMempool(), true);
 | 
				
			||||||
      res.json([cpfpInfo]);
 | 
					      res.json([cpfpInfo]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    } catch (e) {
 | 
					    } catch (e) {
 | 
				
			||||||
 | 
				
			|||||||
@ -167,8 +167,10 @@ export function calculateGoodBlockCpfp(height: number, transactions: MempoolTran
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * Takes a mempool transaction and a copy of the current mempool, and calculates the CPFP data for
 | 
					 * Takes a mempool transaction and a copy of the current mempool, and calculates the CPFP data for
 | 
				
			||||||
 * that transaction (and all others in the same cluster)
 | 
					 * that transaction (and all others in the same cluster)
 | 
				
			||||||
 | 
					 * If the passed transaction is not guaranteed to be in the mempool, set localTx to true: this will
 | 
				
			||||||
 | 
					 * prevent updating the CPFP data of other transactions in the cluster
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
export function calculateMempoolTxCpfp(tx: MempoolTransactionExtended, mempool: { [txid: string]: MempoolTransactionExtended }): CpfpInfo {
 | 
					export function calculateMempoolTxCpfp(tx: MempoolTransactionExtended, mempool: { [txid: string]: MempoolTransactionExtended }, localTx: boolean = false): CpfpInfo {
 | 
				
			||||||
  if (tx.cpfpUpdated && Date.now() < (tx.cpfpUpdated + CPFP_UPDATE_INTERVAL)) {
 | 
					  if (tx.cpfpUpdated && Date.now() < (tx.cpfpUpdated + CPFP_UPDATE_INTERVAL)) {
 | 
				
			||||||
    tx.cpfpDirty = false;
 | 
					    tx.cpfpDirty = false;
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
@ -198,6 +200,13 @@ export function calculateMempoolTxCpfp(tx: MempoolTransactionExtended, mempool:
 | 
				
			|||||||
    totalFee += tx.fees.base;
 | 
					    totalFee += tx.fees.base;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  const effectiveFeePerVsize = totalFee / totalVsize;
 | 
					  const effectiveFeePerVsize = totalFee / totalVsize;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (localTx) {
 | 
				
			||||||
 | 
					    tx.effectiveFeePerVsize = effectiveFeePerVsize;
 | 
				
			||||||
 | 
					    tx.ancestors = Array.from(cluster.get(tx.txid)?.ancestors.values() || []).map(ancestor => ({ txid: ancestor.txid, weight: ancestor.weight, fee: ancestor.fees.base }));
 | 
				
			||||||
 | 
					    tx.descendants = Array.from(cluster.values()).filter(entry => entry.txid !== tx.txid && !cluster.get(tx.txid)?.ancestors.has(entry.txid)).map(tx => ({ txid: tx.txid, weight: tx.weight, fee: tx.fees.base }));
 | 
				
			||||||
 | 
					    tx.bestDescendant = null;
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
    for (const tx of cluster.values()) {
 | 
					    for (const tx of cluster.values()) {
 | 
				
			||||||
      mempool[tx.txid].effectiveFeePerVsize = effectiveFeePerVsize;
 | 
					      mempool[tx.txid].effectiveFeePerVsize = effectiveFeePerVsize;
 | 
				
			||||||
      mempool[tx.txid].ancestors = Array.from(tx.ancestors.values()).map(tx => ({ txid: tx.txid, weight: tx.weight, fee: tx.fees.base }));
 | 
					      mempool[tx.txid].ancestors = Array.from(tx.ancestors.values()).map(tx => ({ txid: tx.txid, weight: tx.weight, fee: tx.fees.base }));
 | 
				
			||||||
@ -210,6 +219,8 @@ export function calculateMempoolTxCpfp(tx: MempoolTransactionExtended, mempool:
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    tx = mempool[tx.txid];
 | 
					    tx = mempool[tx.txid];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return {
 | 
					  return {
 | 
				
			||||||
    ancestors: tx.ancestors || [],
 | 
					    ancestors: tx.ancestors || [],
 | 
				
			||||||
    bestDescendant: tx.bestDescendant || null,
 | 
					    bestDescendant: tx.bestDescendant || null,
 | 
				
			||||||
@ -222,33 +233,6 @@ export function calculateMempoolTxCpfp(tx: MempoolTransactionExtended, mempool:
 | 
				
			|||||||
  };
 | 
					  };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * Takes an unbroadcasted transaction and a copy of the current mempool, and calculates an estimate
 | 
					 | 
				
			||||||
 * of the CPFP data if the transaction were to enter the mempool. This only returns potential ancerstors 
 | 
					 | 
				
			||||||
 * and effective fee rate, and does not update the CPFP data of other transactions in the cluster.
 | 
					 | 
				
			||||||
 */
 | 
					 | 
				
			||||||
export function calculateLocalTxCpfp(tx: MempoolTransactionExtended, mempool: { [txid: string]: MempoolTransactionExtended }): CpfpInfo {
 | 
					 | 
				
			||||||
  const ancestorMap = new Map<string, GraphTx>();
 | 
					 | 
				
			||||||
  const graphTx = convertToGraphTx(tx, memPool.getSpendMap());
 | 
					 | 
				
			||||||
  ancestorMap.set(tx.txid, graphTx);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const allRelatives = expandRelativesGraph(mempool, ancestorMap, memPool.getSpendMap());
 | 
					 | 
				
			||||||
  const relativesMap = initializeRelatives(allRelatives);
 | 
					 | 
				
			||||||
  const cluster = calculateCpfpCluster(tx.txid, relativesMap);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  let totalVsize = 0;
 | 
					 | 
				
			||||||
  let totalFee = 0;
 | 
					 | 
				
			||||||
  for (const tx of cluster.values()) {
 | 
					 | 
				
			||||||
    totalVsize += tx.vsize;
 | 
					 | 
				
			||||||
    totalFee += tx.fees.base;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return {
 | 
					 | 
				
			||||||
    ancestors: Array.from(cluster.get(tx.txid)?.ancestors.values() || []).map(ancestor => ({ txid: ancestor.txid, weight: ancestor.weight, fee: ancestor.fees.base })),
 | 
					 | 
				
			||||||
    effectiveFeePerVsize: totalFee / totalVsize
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
   * Given a root transaction and a list of in-mempool ancestors,
 | 
					   * Given a root transaction and a list of in-mempool ancestors,
 | 
				
			||||||
   * Calculate the CPFP cluster
 | 
					   * Calculate the CPFP cluster
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user