2020-10-19 06:57:02 +02:00
|
|
|
import config from '../config';
|
2019-07-21 16:59:47 +02:00
|
|
|
import * as fs from 'fs';
|
2020-09-29 19:25:43 +02:00
|
|
|
import * as process from 'process';
|
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';
|
|
|
|
private enabled = true;
|
2019-07-21 16:59:47 +02:00
|
|
|
|
2020-02-26 11:49:53 +01:00
|
|
|
constructor() {
|
2020-10-19 06:57:02 +02:00
|
|
|
if (process.env.workerId === '0' || !config.MEMPOOL.SPAWN_CLUSTER_PROCS) {
|
2020-10-26 18:05:06 +01:00
|
|
|
if (!fs.existsSync(DiskCache.FILE_NAME)) {
|
|
|
|
fs.closeSync(fs.openSync(DiskCache.FILE_NAME, 'w'));
|
|
|
|
logger.info('Disk cache file created');
|
|
|
|
}
|
|
|
|
|
2020-09-29 19:25:43 +02:00
|
|
|
process.on('SIGINT', () => {
|
|
|
|
this.saveCacheToDisk();
|
|
|
|
process.exit(2);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
this.saveCacheToDisk();
|
|
|
|
process.exit(2);
|
|
|
|
});
|
2020-10-26 18:05:06 +01:00
|
|
|
} else {
|
|
|
|
this.enabled = false;
|
2020-09-29 19:25:43 +02:00
|
|
|
}
|
2020-03-01 17:09:33 +01:00
|
|
|
}
|
|
|
|
|
2020-10-26 18:05:06 +01:00
|
|
|
async $saveCacheToDiskAsync(): Promise<void> {
|
|
|
|
if (!this.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
await this.$saveDataAsync(JSON.stringify({
|
|
|
|
mempool: memPool.getMempool(),
|
|
|
|
blocks: blocks.getBlocks(),
|
|
|
|
}));
|
|
|
|
logger.info('Mempool and blocks data saved to disk cache');
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn('Error writing to cache file asynchronously');
|
|
|
|
}
|
2020-02-26 11:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
loadMempoolCache() {
|
2020-10-26 18:05:06 +01:00
|
|
|
const cacheData = this.loadDataSync();
|
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-02-29 15:52:04 +01:00
|
|
|
const data = JSON.parse(cacheData);
|
2020-09-29 18:13:22 +02:00
|
|
|
memPool.setMempool(data.mempool);
|
|
|
|
blocks.setBlocks(data.blocks);
|
2020-02-26 11:49:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-26 18:05:06 +01:00
|
|
|
private saveCacheToDisk() {
|
|
|
|
this.saveDataSync(JSON.stringify({
|
|
|
|
mempool: memPool.getMempool(),
|
|
|
|
blocks: blocks.getBlocks(),
|
|
|
|
}));
|
|
|
|
logger.info('Mempool and blocks data saved to disk cache');
|
|
|
|
}
|
|
|
|
|
|
|
|
private $saveDataAsync(dataBlob: string): Promise<void> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fs.writeFile(DiskCache.FILE_NAME, dataBlob, (err) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private saveDataSync(dataBlob: string) {
|
2019-07-21 16:59:47 +02:00
|
|
|
fs.writeFileSync(DiskCache.FILE_NAME, dataBlob, 'utf8');
|
|
|
|
}
|
|
|
|
|
2020-10-26 18:05:06 +01:00
|
|
|
private loadDataSync(): string {
|
2019-07-21 16:59:47 +02:00
|
|
|
return fs.readFileSync(DiskCache.FILE_NAME, 'utf8');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new DiskCache();
|