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;
|
2020-09-29 19:25:43 +02:00
|
|
|
import * as process from 'process';
|
2020-11-02 13:11:04 +01:00
|
|
|
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';
|
2019-07-21 16:59:47 +02:00
|
|
|
|
|
|
|
class DiskCache {
|
2020-10-26 18:05:06 +01:00
|
|
|
private static FILE_NAME = './cache.json';
|
2020-11-02 13:11:04 +01:00
|
|
|
private static FILE_NAME_2 = './cache2.json';
|
2020-11-02 16:06:51 +01:00
|
|
|
private static CHUNK_SIZE = 50000;
|
2020-02-26 11:49:53 +01:00
|
|
|
constructor() {
|
2020-11-02 13:11:04 +01:00
|
|
|
if (!cluster.isMaster) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-02 16:06:51 +01:00
|
|
|
process.on('SIGINT', () => {
|
|
|
|
this.saveCacheToDiskSync();
|
2020-11-02 13:11:04 +01:00
|
|
|
process.exit(2);
|
|
|
|
});
|
2020-11-02 16:06:51 +01:00
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
this.saveCacheToDiskSync();
|
2020-11-02 13:11:04 +01:00
|
|
|
process.exit(2);
|
|
|
|
});
|
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;
|
|
|
|
}
|
|
|
|
try {
|
2020-11-02 16:06:51 +01:00
|
|
|
logger.debug('Writing mempool and blocks data to disk cache (async)...');
|
|
|
|
const mempoolChunk_1 = Object.fromEntries(Object.entries(memPool.getMempool()).splice(0, DiskCache.CHUNK_SIZE));
|
|
|
|
const mempoolChunk_2 = Object.fromEntries(Object.entries(memPool.getMempool()).splice(DiskCache.CHUNK_SIZE));
|
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(),
|
2020-11-02 13:11:04 +01:00
|
|
|
mempool: mempoolChunk_1
|
|
|
|
}), {flag: 'w'});
|
|
|
|
await fsPromises.writeFile(DiskCache.FILE_NAME_2, JSON.stringify({
|
|
|
|
mempool: mempoolChunk_2
|
|
|
|
}), {flag: 'w'});
|
2020-10-27 10:34:27 +01:00
|
|
|
logger.debug('Mempool and blocks data saved to disk cache');
|
2020-10-26 18:05:06 +01:00
|
|
|
} catch (e) {
|
2020-11-02 13:11:04 +01:00
|
|
|
logger.warn('Error writing to cache file: ' + e.message || e);
|
2020-10-26 18:05:06 +01:00
|
|
|
}
|
2020-02-26 11:49:53 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 16:06:51 +01:00
|
|
|
saveCacheToDiskSync(): void {
|
|
|
|
try {
|
|
|
|
logger.debug('Writing mempool and blocks data to disk cache...');
|
|
|
|
const mempoolChunk_1 = Object.fromEntries(Object.entries(memPool.getMempool()).splice(0, DiskCache.CHUNK_SIZE));
|
|
|
|
const mempoolChunk_2 = Object.fromEntries(Object.entries(memPool.getMempool()).splice(DiskCache.CHUNK_SIZE));
|
|
|
|
fs.writeFileSync(DiskCache.FILE_NAME, JSON.stringify({
|
|
|
|
blocks: blocks.getBlocks(),
|
|
|
|
mempool: mempoolChunk_1
|
|
|
|
}), {flag: 'w'});
|
|
|
|
fs.writeFileSync(DiskCache.FILE_NAME_2, JSON.stringify({
|
|
|
|
mempool: mempoolChunk_2
|
|
|
|
}), {flag: 'w'});
|
|
|
|
logger.debug('Mempool and blocks data saved to disk cache');
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn('Error writing to cache file: ' + e.message || e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-26 11:49:53 +01:00
|
|
|
loadMempoolCache() {
|
2020-11-02 13:11:04 +01:00
|
|
|
if (!fs.existsSync(DiskCache.FILE_NAME)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let data: any = {};
|
|
|
|
const cacheData = fs.readFileSync(DiskCache.FILE_NAME, 'utf8');
|
2020-02-26 11:49:53 +01:00
|
|
|
if (cacheData) {
|
2020-10-13 10:27:52 +02:00
|
|
|
logger.info('Restoring mempool and blocks data from disk cache');
|
2020-11-02 13:11:04 +01:00
|
|
|
data = JSON.parse(cacheData);
|
2020-02-26 11:49:53 +01:00
|
|
|
}
|
|
|
|
|
2020-11-02 13:11:04 +01:00
|
|
|
if (fs.existsSync(DiskCache.FILE_NAME_2)) {
|
|
|
|
const cacheData2 = JSON.parse(fs.readFileSync(DiskCache.FILE_NAME_2, 'utf8'));
|
|
|
|
Object.assign(data.mempool, cacheData2.mempool);
|
|
|
|
}
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2020-11-02 13:11:04 +01:00
|
|
|
memPool.setMempool(data.mempool);
|
|
|
|
blocks.setBlocks(data.blocks);
|
2019-07-21 16:59:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new DiskCache();
|