@@ -62,6 +62,7 @@ export class Transaction extends BaseModel<ITransaction> {
6262 } ) {
6363 const mintOps = await this . getMintOps ( params ) ;
6464 const spendOps = this . getSpendOps ( { ...params , mintOps } ) ;
65+ this . pruneMempool ( { ...params , mintOps, spendOps} ) ;
6566
6667 logger . debug ( 'Minting Coins' , mintOps . length ) ;
6768 if ( mintOps . length ) {
@@ -383,6 +384,46 @@ export class Transaction extends BaseModel<ITransaction> {
383384 return spendOps ;
384385 }
385386
387+ async pruneMempool ( params : {
388+ txs : Array < Bitcoin . Transaction > ;
389+ height : number ;
390+ parentChain ?: string ;
391+ forkHeight ?: number ;
392+ chain : string ;
393+ network : string ;
394+ mintOps : Array < any > ;
395+ spendOps : Array < any > ;
396+ initialSyncComplete : boolean ;
397+ [ rest : string ] : any ;
398+ } ) {
399+ const { chain, network, spendOps, initialSyncComplete } = params ;
400+ if ( ! initialSyncComplete || ! spendOps . length ) {
401+ return ;
402+ }
403+ const spentCoinsQuery = {
404+ chain, network, spentHeight : SpentHeightIndicators . pending , $or : spendOps . map ( spendOp => {
405+ return {
406+ mintTxid : spendOp . updateOne . filter . mintTxid ,
407+ mintIndex : spendOp . updateOne . filter . mintIndex ,
408+ spentTxid : { $ne : spendOp . updateOne . update . $set . spentTxid }
409+ }
410+ } )
411+ } ;
412+ const spendingCoins = await CoinModel . collection . find ( spentCoinsQuery ) . toArray ( ) ;
413+ if ( spendingCoins . length ) {
414+ let prunedTxs = { } ;
415+ for ( const coin of spendingCoins ) {
416+ prunedTxs [ coin . spentTxid ] = true ;
417+ }
418+ prunedTxs = Object . keys ( prunedTxs ) ;
419+ await Promise . all ( [
420+ this . collection . update ( { txid : { $in : prunedTxs } } , { $set : { blockHeight : SpentHeightIndicators . conflicting } } , { w : 0 , j : false , multi : true } ) ,
421+ CoinModel . collection . update ( { mintTxid : { $in : prunedTxs } } , { $set : { mintHeight : SpentHeightIndicators . conflicting } } , { w : 0 , j : false , multi : true } )
422+ ] ) ;
423+ }
424+ return ;
425+ }
426+
386427 getTransactions ( params : { query : any ; options : StreamingFindOptions < ITransaction > } ) {
387428 let originalQuery = params . query ;
388429 const { query, options } = Storage . getFindOptions ( this , params . options ) ;
0 commit comments