parent
							
								
									21e2ce6a85
								
							
						
					
					
						commit
						6688421e39
					
				@ -14,6 +14,7 @@ class Mempool {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  private vBytesPerSecondArray: VbytesPerSecond[] = [];
 | 
					  private vBytesPerSecondArray: VbytesPerSecond[] = [];
 | 
				
			||||||
  private vBytesPerSecond: number = 0;
 | 
					  private vBytesPerSecond: number = 0;
 | 
				
			||||||
 | 
					  private mempoolProtection = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  constructor() {
 | 
					  constructor() {
 | 
				
			||||||
    setInterval(this.updateTxPerSecond.bind(this), 1000);
 | 
					    setInterval(this.updateTxPerSecond.bind(this), 1000);
 | 
				
			||||||
@ -86,10 +87,11 @@ class Mempool {
 | 
				
			|||||||
    console.log('Updating mempool');
 | 
					    console.log('Updating mempool');
 | 
				
			||||||
    const start = new Date().getTime();
 | 
					    const start = new Date().getTime();
 | 
				
			||||||
    let hasChange: boolean = false;
 | 
					    let hasChange: boolean = false;
 | 
				
			||||||
 | 
					    const currentMempoolSize = Object.keys(this.mempoolCache).length;
 | 
				
			||||||
    let txCount = 0;
 | 
					    let txCount = 0;
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      const transactions = await bitcoinApi.getRawMempool();
 | 
					      const transactions = await bitcoinApi.getRawMempool();
 | 
				
			||||||
      const diff = transactions.length - Object.keys(this.mempoolCache).length;
 | 
					      const diff = transactions.length - currentMempoolSize;
 | 
				
			||||||
      const newTransactions: TransactionExtended[] = [];
 | 
					      const newTransactions: TransactionExtended[] = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      for (const txid of transactions) {
 | 
					      for (const txid of transactions) {
 | 
				
			||||||
@ -122,19 +124,35 @@ class Mempool {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Index object for faster search
 | 
					      // Prevent mempool from clear on bitcoind restart by delaying the deletion
 | 
				
			||||||
      const transactionsObject = {};
 | 
					      if (this.mempoolProtection === 0 && transactions.length < currentMempoolSize / 2) {
 | 
				
			||||||
      transactions.forEach((txId) => transactionsObject[txId] = true);
 | 
					        this.mempoolProtection = 1;
 | 
				
			||||||
 | 
					        console.log('Mempool clear protection triggered.');
 | 
				
			||||||
 | 
					        setTimeout(() => {
 | 
				
			||||||
 | 
					          this.mempoolProtection = 2;
 | 
				
			||||||
 | 
					          console.log('Mempool clear protection resumed.');
 | 
				
			||||||
 | 
					        }, 1000 * 60 * 2);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Replace mempool to separate deleted transactions
 | 
					      let newMempool = {};
 | 
				
			||||||
      const newMempool = {};
 | 
					 | 
				
			||||||
      const deletedTransactions: TransactionExtended[] = [];
 | 
					      const deletedTransactions: TransactionExtended[] = [];
 | 
				
			||||||
      for (const tx in this.mempoolCache) {
 | 
					
 | 
				
			||||||
        if (transactionsObject[tx]) {
 | 
					      if (this.mempoolProtection !== 1) {
 | 
				
			||||||
          newMempool[tx] = this.mempoolCache[tx];
 | 
					        this.mempoolProtection = 0;
 | 
				
			||||||
        } else {
 | 
					        // Index object for faster search
 | 
				
			||||||
          deletedTransactions.push(this.mempoolCache[tx]);
 | 
					        const transactionsObject = {};
 | 
				
			||||||
 | 
					        transactions.forEach((txId) => transactionsObject[txId] = true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Replace mempool to separate deleted transactions
 | 
				
			||||||
 | 
					        for (const tx in this.mempoolCache) {
 | 
				
			||||||
 | 
					          if (transactionsObject[tx]) {
 | 
				
			||||||
 | 
					            newMempool[tx] = this.mempoolCache[tx];
 | 
				
			||||||
 | 
					          } else {
 | 
				
			||||||
 | 
					            deletedTransactions.push(this.mempoolCache[tx]);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        newMempool = this.mempoolCache;
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      if (!this.inSync && transactions.length === Object.keys(newMempool).length) {
 | 
					      if (!this.inSync && transactions.length === Object.keys(newMempool).length) {
 | 
				
			||||||
 | 
				
			|||||||
@ -123,7 +123,7 @@ export interface Block {
 | 
				
			|||||||
  medianFee?: number;
 | 
					  medianFee?: number;
 | 
				
			||||||
  feeRange?: number[];
 | 
					  feeRange?: number[];
 | 
				
			||||||
  reward?: number;
 | 
					  reward?: number;
 | 
				
			||||||
  coinbaseTx?: Transaction;
 | 
					  coinbaseTx?: TransactionMinerInfo;
 | 
				
			||||||
  matchRate: number;
 | 
					  matchRate: number;
 | 
				
			||||||
  stage: number;
 | 
					  stage: number;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user