Shuffle mempool transactions before saving disk cache. (#398)

fixes #397
This commit is contained in:
softsimon 2021-03-19 13:47:37 +07:00 committed by GitHub
parent 6f1cdd0c8b
commit 5148de8f17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 1 deletions

View File

@ -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;
}
}

View File

@ -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({