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';
|
2021-01-15 17:26:32 +01:00
|
|
|
private static FILE_NAMES = './cache{number}.json';
|
|
|
|
private static CHUNK_SIZE = 10000;
|
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)...');
|
2021-01-15 17:26:32 +01:00
|
|
|
const mempoolChunk_1 = Object.fromEntries(Object.entries(memPool.getMempool()).slice(0, 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'});
|
2021-01-15 17:26:32 +01:00
|
|
|
for (let i = 1; i < 10; i++) {
|
|
|
|
const mempoolChunk = Object.fromEntries(
|
|
|
|
Object.entries(memPool.getMempool()).slice(
|
|
|
|
DiskCache.CHUNK_SIZE * i, i === 9 ? undefined : DiskCache.CHUNK_SIZE * i + DiskCache.CHUNK_SIZE
|
|
|
|
)
|
|
|
|
);
|
|
|
|
await fsPromises.writeFile(DiskCache.FILE_NAMES.replace('{number}', i.toString()), JSON.stringify({
|
|
|
|
mempool: mempoolChunk
|
|
|
|
}), {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...');
|
2021-01-15 17:26:32 +01:00
|
|
|
const mempoolChunk_1 = Object.fromEntries(Object.entries(memPool.getMempool()).slice(0, DiskCache.CHUNK_SIZE));
|
2020-11-02 16:06:51 +01:00
|
|
|
fs.writeFileSync(DiskCache.FILE_NAME, JSON.stringify({
|
|
|
|
blocks: blocks.getBlocks(),
|
|
|
|
mempool: mempoolChunk_1
|
|
|
|
}), {flag: 'w'});
|
2021-01-15 17:26:32 +01:00
|
|
|
for (let i = 1; i < 10; i++) {
|
|
|
|
const mempoolChunk = Object.fromEntries(
|
|
|
|
Object.entries(memPool.getMempool()).slice(
|
|
|
|
DiskCache.CHUNK_SIZE * i, i === 9 ? undefined : DiskCache.CHUNK_SIZE * i + DiskCache.CHUNK_SIZE
|
|
|
|
)
|
|
|
|
);
|
|
|
|
fs.writeFileSync(DiskCache.FILE_NAMES.replace('{number}', i.toString()), JSON.stringify({
|
|
|
|
mempool: mempoolChunk
|
|
|
|
}), {flag: 'w'});
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:06:51 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-01-15 17:26:32 +01:00
|
|
|
for (let i = 1; i < 10; i++) {
|
|
|
|
const fileName = DiskCache.FILE_NAMES.replace('{number}', i.toString());
|
|
|
|
if (fs.existsSync(fileName)) {
|
|
|
|
const cacheData2 = JSON.parse(fs.readFileSync(fileName, 'utf8'));
|
|
|
|
Object.assign(data.mempool, cacheData2.mempool);
|
|
|
|
}
|
2020-11-02 13:11:04 +01:00
|
|
|
}
|
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();
|