Shuffle mempool transactions before saving disk cache. (#398)
fixes #397
This commit is contained in:
		
							parent
							
								
									6f1cdd0c8b
								
							
						
					
					
						commit
						5148de8f17
					
				@ -71,6 +71,13 @@ export class Common {
 | 
			
		||||
       }, ms);
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
  
 | 
			
		||||
  static shuffleArray(array: any[]) {
 | 
			
		||||
    for (let i = array.length - 1; i > 0; i--) {
 | 
			
		||||
        const j = Math.floor(Math.random() * (i + 1));
 | 
			
		||||
        [array[i], array[j]] = [array[j], array[i]];
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static setRelativesAndGetCpfpInfo(tx: TransactionExtended, memPool: { [txid: string]: TransactionExtended }): CpfpInfo {
 | 
			
		||||
    const parents = this.findAllParents(tx, memPool);
 | 
			
		||||
@ -129,5 +136,4 @@ export class Common {
 | 
			
		||||
    });
 | 
			
		||||
    return parents;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -6,6 +6,7 @@ import blocks from './blocks';
 | 
			
		||||
import logger from '../logger';
 | 
			
		||||
import config from '../config';
 | 
			
		||||
import { TransactionExtended } from '../mempool.interfaces';
 | 
			
		||||
import { Common } from './common';
 | 
			
		||||
 | 
			
		||||
class DiskCache {
 | 
			
		||||
  private static FILE_NAME = config.MEMPOOL.CACHE_DIR + '/cache.json';
 | 
			
		||||
@ -27,6 +28,8 @@ class DiskCache {
 | 
			
		||||
        mempoolArray.push(mempool[tx]);
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      Common.shuffleArray(mempoolArray);
 | 
			
		||||
 | 
			
		||||
      const chunkSize = Math.floor(mempoolArray.length / DiskCache.CHUNK_FILES);
 | 
			
		||||
 | 
			
		||||
      await fsPromises.writeFile(DiskCache.FILE_NAME, JSON.stringify({
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user