From 5148de8f171dbe0e81da6526acf9bdea5d1fc5b9 Mon Sep 17 00:00:00 2001 From: softsimon Date: Fri, 19 Mar 2021 13:47:37 +0700 Subject: [PATCH] Shuffle mempool transactions before saving disk cache. (#398) fixes #397 --- backend/src/api/common.ts | 8 +++++++- backend/src/api/disk-cache.ts | 3 +++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/backend/src/api/common.ts b/backend/src/api/common.ts index 021990418..130e0205d 100644 --- a/backend/src/api/common.ts +++ b/backend/src/api/common.ts @@ -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; } - } diff --git a/backend/src/api/disk-cache.ts b/backend/src/api/disk-cache.ts index cbdff49f0..909f12681 100644 --- a/backend/src/api/disk-cache.ts +++ b/backend/src/api/disk-cache.ts @@ -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({