2019-07-21 16:59:47 +02:00
|
|
|
import * as fs from 'fs';
|
2020-11-02 13:11:04 +01:00
|
|
|
const fsPromises = fs.promises;
|
|
|
|
import * as cluster from 'cluster';
|
2020-02-26 11:49:53 +01:00
|
|
|
import memPool from './mempool';
|
2020-02-29 15:52:04 +01:00
|
|
|
import blocks from './blocks';
|
2020-10-13 10:27:52 +02:00
|
|
|
import logger from '../logger';
|
2021-02-01 15:54:27 +01:00
|
|
|
import config from '../config';
|
2021-02-14 13:50:31 +01:00
|
|
|
import { TransactionExtended } from '../mempool.interfaces';
|
2021-03-19 07:47:37 +01:00
|
|
|
import { Common } from './common';
|
2019-07-21 16:59:47 +02:00
|
|
|
|
|
|
|
class DiskCache {
|
2021-02-14 14:03:45 +01:00
|
|
|
private static FILE_NAME = config.MEMPOOL.CACHE_DIR + '/cache.json';
|
|
|
|
private static FILE_NAMES = config.MEMPOOL.CACHE_DIR + '/cache{number}.json';
|
2021-02-14 13:50:31 +01:00
|
|
|
private static CHUNK_FILES = 25;
|
2021-04-12 11:21:49 +02:00
|
|
|
private isWritingCache = false;
|
2021-02-14 13:50:31 +01:00
|
|
|
|
2021-01-26 19:49:11 +01:00
|
|
|
constructor() { }
|
2020-03-01 17:09:33 +01:00
|
|
|
|
2020-11-02 13:11:04 +01:00
|
|
|
async $saveCacheToDisk(): Promise<void> {
|
|
|
|
if (!cluster.isMaster) {
|
2020-10-26 18:05:06 +01:00
|
|
|
return;
|
|
|
|
}
|
2021-04-12 11:21:49 +02:00
|
|
|
if (this.isWritingCache) {
|
|
|
|
logger.debug('Saving cache already in progress. Skipping.')
|
|
|
|
return;
|
|
|
|
}
|
2020-10-26 18:05:06 +01:00
|
|
|
try {
|
2020-11-02 16:06:51 +01:00
|
|
|
logger.debug('Writing mempool and blocks data to disk cache (async)...');
|
2021-04-12 11:21:49 +02:00
|
|
|
this.isWritingCache = true;
|
2021-02-14 13:50:31 +01:00
|
|
|
|
|
|
|
const mempool = memPool.getMempool();
|
|
|
|
const mempoolArray: TransactionExtended[] = [];
|
|
|
|
for (const tx in mempool) {
|
|
|
|
mempoolArray.push(mempool[tx]);
|
|
|
|
}
|
|
|
|
|
2021-03-19 07:47:37 +01:00
|
|
|
Common.shuffleArray(mempoolArray);
|
|
|
|
|
2021-02-14 13:50:31 +01:00
|
|
|
const chunkSize = Math.floor(mempoolArray.length / DiskCache.CHUNK_FILES);
|
|
|
|
|
2020-11-02 13:11:04 +01:00
|
|
|
await fsPromises.writeFile(DiskCache.FILE_NAME, JSON.stringify({
|
2020-10-26 18:05:06 +01:00
|
|
|
blocks: blocks.getBlocks(),
|
2021-02-14 13:50:31 +01:00
|
|
|
mempool: {},
|
|
|
|
mempoolArray: mempoolArray.splice(0, chunkSize),
|
2020-11-02 13:11:04 +01:00
|
|
|
}), {flag: 'w'});
|
2021-02-14 13:50:31 +01:00
|
|
|
for (let i = 1; i < DiskCache.CHUNK_FILES; i++) {
|
2021-01-15 17:26:32 +01:00
|
|
|
await fsPromises.writeFile(DiskCache.FILE_NAMES.replace('{number}', i.toString()), JSON.stringify({
|
2021-02-14 13:50:31 +01:00
|
|
|
mempool: {},
|
|
|
|
mempoolArray: mempoolArray.splice(0, chunkSize),
|
2021-01-15 17:26:32 +01:00
|
|
|
}), {flag: 'w'});
|
|
|
|
}
|
2020-10-27 10:34:27 +01:00
|
|
|
logger.debug('Mempool and blocks data saved to disk cache');
|
2021-04-12 11:21:49 +02:00
|
|
|
this.isWritingCache = false;
|
2020-10-26 18:05:06 +01:00
|
|
|
} catch (e) {
|
2021-08-31 14:09:33 +02:00
|
|
|
logger.warn('Error writing to cache file: ' + (e instanceof Error ? e.message : e));
|
2021-04-12 11:21:49 +02:00
|
|
|
this.isWritingCache = false;
|
2020-10-26 18:05:06 +01:00
|
|
|
}
|
2020-02-26 11:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
loadMempoolCache() {
|
2020-11-02 13:11:04 +01:00
|
|
|
if (!fs.existsSync(DiskCache.FILE_NAME)) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-26 19:49:11 +01:00
|
|
|
try {
|
|
|
|
let data: any = {};
|
|
|
|
const cacheData = fs.readFileSync(DiskCache.FILE_NAME, 'utf8');
|
|
|
|
if (cacheData) {
|
|
|
|
logger.info('Restoring mempool and blocks data from disk cache');
|
|
|
|
data = JSON.parse(cacheData);
|
2021-02-14 13:50:31 +01:00
|
|
|
if (data.mempoolArray) {
|
|
|
|
for (const tx of data.mempoolArray) {
|
|
|
|
data.mempool[tx.txid] = tx;
|
|
|
|
}
|
|
|
|
}
|
2021-01-26 19:49:11 +01:00
|
|
|
}
|
2020-02-26 11:49:53 +01:00
|
|
|
|
2021-02-14 13:50:31 +01:00
|
|
|
for (let i = 1; i < DiskCache.CHUNK_FILES; i++) {
|
2021-01-26 19:49:11 +01:00
|
|
|
const fileName = DiskCache.FILE_NAMES.replace('{number}', i.toString());
|
2021-04-12 11:21:49 +02:00
|
|
|
try {
|
|
|
|
if (fs.existsSync(fileName)) {
|
|
|
|
const cacheData2 = JSON.parse(fs.readFileSync(fileName, 'utf8'));
|
|
|
|
if (cacheData2.mempoolArray) {
|
|
|
|
for (const tx of cacheData2.mempoolArray) {
|
|
|
|
data.mempool[tx.txid] = tx;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Object.assign(data.mempool, cacheData2.mempool);
|
2021-02-14 13:50:31 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 11:21:49 +02:00
|
|
|
} catch (e) {
|
|
|
|
logger.debug('Error parsing ' + fileName + '. Skipping.');
|
2021-01-26 19:49:11 +01:00
|
|
|
}
|
2021-01-15 17:26:32 +01:00
|
|
|
}
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2021-01-26 19:49:11 +01:00
|
|
|
memPool.setMempool(data.mempool);
|
|
|
|
blocks.setBlocks(data.blocks);
|
|
|
|
} catch (e) {
|
2021-04-12 11:21:49 +02:00
|
|
|
logger.warn('Failed to parse mempoool and blocks cache. Skipping.');
|
2021-01-26 19:49:11 +01:00
|
|
|
}
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new DiskCache();
|