2019-07-21 17:59:47 +03:00
|
|
|
import * as fs from 'fs';
|
2020-11-02 19:11:04 +07:00
|
|
|
const fsPromises = fs.promises;
|
2022-06-27 21:28:21 -07:00
|
|
|
import cluster from 'cluster';
|
2020-02-26 17:49:53 +07:00
|
|
|
import memPool from './mempool';
|
2020-02-29 21:52:04 +07:00
|
|
|
import blocks from './blocks';
|
2020-10-13 15:27:52 +07:00
|
|
|
import logger from '../logger';
|
2021-02-01 15:54:27 +01:00
|
|
|
import config from '../config';
|
2021-02-14 19:50:31 +07:00
|
|
|
import { TransactionExtended } from '../mempool.interfaces';
|
2021-03-19 13:47:37 +07:00
|
|
|
import { Common } from './common';
|
2023-03-05 03:02:46 -06:00
|
|
|
import rbfCache from './rbf-cache';
|
2019-07-21 17:59:47 +03:00
|
|
|
|
|
|
|
class DiskCache {
|
2023-02-27 19:06:46 +09:00
|
|
|
private cacheSchemaVersion = 3;
|
2023-03-05 03:02:46 -06:00
|
|
|
private rbfCacheSchemaVersion = 1;
|
2022-06-03 13:31:45 +02:00
|
|
|
|
2023-03-09 20:19:22 -06:00
|
|
|
private static TMP_FILE_NAME = config.MEMPOOL.CACHE_DIR + '/tmp-cache.json';
|
|
|
|
private static TMP_FILE_NAMES = config.MEMPOOL.CACHE_DIR + '/tmp-cache{number}.json';
|
2021-02-14 20:03:45 +07:00
|
|
|
private static FILE_NAME = config.MEMPOOL.CACHE_DIR + '/cache.json';
|
|
|
|
private static FILE_NAMES = config.MEMPOOL.CACHE_DIR + '/cache{number}.json';
|
2023-03-30 05:46:11 +09:00
|
|
|
private static TMP_RBF_FILE_NAME = config.MEMPOOL.CACHE_DIR + '/tmp-rbfcache.json';
|
2023-03-05 03:02:46 -06:00
|
|
|
private static RBF_FILE_NAME = config.MEMPOOL.CACHE_DIR + '/rbfcache.json';
|
2021-02-14 19:50:31 +07:00
|
|
|
private static CHUNK_FILES = 25;
|
2021-04-12 13:21:49 +04:00
|
|
|
private isWritingCache = false;
|
2023-05-25 19:05:29 +04:00
|
|
|
private ignoreBlocksCache = false;
|
2021-02-14 19:50:31 +07:00
|
|
|
|
2023-05-01 14:30:30 -06:00
|
|
|
private semaphore: { resume: (() => void)[], locks: number } = {
|
|
|
|
resume: [],
|
|
|
|
locks: 0,
|
|
|
|
};
|
|
|
|
|
2023-03-09 19:47:54 -06:00
|
|
|
constructor() {
|
2023-05-12 16:29:45 -06:00
|
|
|
if (!cluster.isPrimary || !config.MEMPOOL.CACHE_ENABLED) {
|
2023-03-09 19:47:54 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
process.on('SIGINT', (e) => {
|
2023-03-20 12:07:42 +09:00
|
|
|
this.$saveCacheToDisk(true);
|
2023-03-20 15:23:02 +09:00
|
|
|
process.exit(0);
|
2023-03-09 19:47:54 -06:00
|
|
|
});
|
|
|
|
}
|
2020-03-01 23:09:33 +07:00
|
|
|
|
2023-03-20 12:07:42 +09:00
|
|
|
async $saveCacheToDisk(sync: boolean = false): Promise<void> {
|
2023-05-12 16:29:45 -06:00
|
|
|
if (!cluster.isPrimary || !config.MEMPOOL.CACHE_ENABLED) {
|
2020-10-27 00:05:06 +07:00
|
|
|
return;
|
|
|
|
}
|
2021-04-12 13:21:49 +04:00
|
|
|
if (this.isWritingCache) {
|
2023-03-09 19:47:54 -06:00
|
|
|
logger.debug('Saving cache already in progress. Skipping.');
|
2021-04-12 13:21:49 +04:00
|
|
|
return;
|
|
|
|
}
|
2020-10-27 00:05:06 +07:00
|
|
|
try {
|
2023-03-20 12:07:42 +09:00
|
|
|
logger.debug(`Writing mempool and blocks data to disk cache (${ sync ? 'sync' : 'async' })...`);
|
2021-04-12 13:21:49 +04:00
|
|
|
this.isWritingCache = true;
|
2021-02-14 19:50:31 +07:00
|
|
|
|
|
|
|
const mempool = memPool.getMempool();
|
|
|
|
const mempoolArray: TransactionExtended[] = [];
|
|
|
|
for (const tx in mempool) {
|
2023-04-07 09:41:25 +09:00
|
|
|
if (mempool[tx]) {
|
2023-03-24 09:47:08 +09:00
|
|
|
mempoolArray.push(mempool[tx]);
|
|
|
|
}
|
2021-02-14 19:50:31 +07:00
|
|
|
}
|
|
|
|
|
2021-03-19 13:47:37 +07:00
|
|
|
Common.shuffleArray(mempoolArray);
|
|
|
|
|
2021-02-14 19:50:31 +07:00
|
|
|
const chunkSize = Math.floor(mempoolArray.length / DiskCache.CHUNK_FILES);
|
|
|
|
|
2023-03-20 12:07:42 +09:00
|
|
|
if (sync) {
|
|
|
|
fs.writeFileSync(DiskCache.TMP_FILE_NAME, JSON.stringify({
|
|
|
|
network: config.MEMPOOL.NETWORK,
|
|
|
|
cacheSchemaVersion: this.cacheSchemaVersion,
|
|
|
|
blocks: blocks.getBlocks(),
|
|
|
|
blockSummaries: blocks.getBlockSummaries(),
|
2021-02-14 19:50:31 +07:00
|
|
|
mempool: {},
|
|
|
|
mempoolArray: mempoolArray.splice(0, chunkSize),
|
2022-06-27 21:28:21 -07:00
|
|
|
}), { flag: 'w' });
|
2023-03-20 12:07:42 +09:00
|
|
|
for (let i = 1; i < DiskCache.CHUNK_FILES; i++) {
|
|
|
|
fs.writeFileSync(DiskCache.TMP_FILE_NAMES.replace('{number}', i.toString()), JSON.stringify({
|
|
|
|
mempool: {},
|
|
|
|
mempoolArray: mempoolArray.splice(0, chunkSize),
|
|
|
|
}), { flag: 'w' });
|
|
|
|
}
|
2023-03-09 19:47:54 -06:00
|
|
|
|
2023-03-20 12:07:42 +09:00
|
|
|
fs.renameSync(DiskCache.TMP_FILE_NAME, DiskCache.FILE_NAME);
|
|
|
|
for (let i = 1; i < DiskCache.CHUNK_FILES; i++) {
|
|
|
|
fs.renameSync(DiskCache.TMP_FILE_NAMES.replace('{number}', i.toString()), DiskCache.FILE_NAMES.replace('{number}', i.toString()));
|
|
|
|
}
|
|
|
|
} else {
|
2023-05-01 14:30:30 -06:00
|
|
|
await this.$yield();
|
2023-03-20 12:07:42 +09:00
|
|
|
await fsPromises.writeFile(DiskCache.TMP_FILE_NAME, JSON.stringify({
|
|
|
|
network: config.MEMPOOL.NETWORK,
|
|
|
|
cacheSchemaVersion: this.cacheSchemaVersion,
|
|
|
|
blocks: blocks.getBlocks(),
|
|
|
|
blockSummaries: blocks.getBlockSummaries(),
|
2023-03-09 19:47:54 -06:00
|
|
|
mempool: {},
|
|
|
|
mempoolArray: mempoolArray.splice(0, chunkSize),
|
|
|
|
}), { flag: 'w' });
|
2023-03-20 12:07:42 +09:00
|
|
|
for (let i = 1; i < DiskCache.CHUNK_FILES; i++) {
|
2023-05-01 14:30:30 -06:00
|
|
|
await this.$yield();
|
2023-03-20 12:07:42 +09:00
|
|
|
await fsPromises.writeFile(DiskCache.TMP_FILE_NAMES.replace('{number}', i.toString()), JSON.stringify({
|
|
|
|
mempool: {},
|
|
|
|
mempoolArray: mempoolArray.splice(0, chunkSize),
|
|
|
|
}), { flag: 'w' });
|
|
|
|
}
|
2023-03-09 20:19:22 -06:00
|
|
|
|
2023-03-20 12:07:42 +09:00
|
|
|
await fsPromises.rename(DiskCache.TMP_FILE_NAME, DiskCache.FILE_NAME);
|
|
|
|
for (let i = 1; i < DiskCache.CHUNK_FILES; i++) {
|
|
|
|
await fsPromises.rename(DiskCache.TMP_FILE_NAMES.replace('{number}', i.toString()), DiskCache.FILE_NAMES.replace('{number}', i.toString()));
|
|
|
|
}
|
2023-03-09 20:19:22 -06:00
|
|
|
}
|
|
|
|
|
2023-03-09 19:47:54 -06:00
|
|
|
logger.debug('Mempool and blocks data saved to disk cache');
|
|
|
|
this.isWritingCache = false;
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn('Error writing to cache file: ' + (e instanceof Error ? e.message : e));
|
|
|
|
this.isWritingCache = false;
|
|
|
|
}
|
2023-03-30 05:46:11 +09:00
|
|
|
|
2023-03-05 03:02:46 -06:00
|
|
|
try {
|
|
|
|
logger.debug('Writing rbf data to disk cache (async)...');
|
|
|
|
this.isWritingCache = true;
|
2023-03-30 05:46:11 +09:00
|
|
|
const rbfData = rbfCache.dump();
|
|
|
|
if (sync) {
|
|
|
|
fs.writeFileSync(DiskCache.TMP_RBF_FILE_NAME, JSON.stringify({
|
|
|
|
network: config.MEMPOOL.NETWORK,
|
|
|
|
rbfCacheSchemaVersion: this.rbfCacheSchemaVersion,
|
|
|
|
rbf: rbfData,
|
|
|
|
}), { flag: 'w' });
|
|
|
|
fs.renameSync(DiskCache.TMP_RBF_FILE_NAME, DiskCache.RBF_FILE_NAME);
|
|
|
|
} else {
|
|
|
|
await fsPromises.writeFile(DiskCache.TMP_RBF_FILE_NAME, JSON.stringify({
|
|
|
|
network: config.MEMPOOL.NETWORK,
|
|
|
|
rbfCacheSchemaVersion: this.rbfCacheSchemaVersion,
|
|
|
|
rbf: rbfData,
|
|
|
|
}), { flag: 'w' });
|
|
|
|
await fsPromises.rename(DiskCache.TMP_RBF_FILE_NAME, DiskCache.RBF_FILE_NAME);
|
|
|
|
}
|
2023-03-05 03:02:46 -06:00
|
|
|
logger.debug('Rbf data saved to disk cache');
|
|
|
|
this.isWritingCache = false;
|
|
|
|
} catch (e) {
|
|
|
|
logger.warn('Error writing rbf data to cache file: ' + (e instanceof Error ? e.message : e));
|
|
|
|
this.isWritingCache = false;
|
|
|
|
}
|
2023-03-09 19:47:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
wipeCache(): void {
|
2023-03-03 17:52:16 +09:00
|
|
|
logger.notice(`Wiping nodejs backend cache/cache*.json files`);
|
2023-02-26 11:37:57 +09:00
|
|
|
try {
|
|
|
|
fs.unlinkSync(DiskCache.FILE_NAME);
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e?.code !== 'ENOENT') {
|
|
|
|
logger.err(`Cannot wipe cache file ${DiskCache.FILE_NAME}. Exception ${JSON.stringify(e)}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 13:31:45 +02:00
|
|
|
for (let i = 1; i < DiskCache.CHUNK_FILES; i++) {
|
2023-02-26 11:37:57 +09:00
|
|
|
const filename = DiskCache.FILE_NAMES.replace('{number}', i.toString());
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(filename);
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e?.code !== 'ENOENT') {
|
|
|
|
logger.err(`Cannot wipe cache file ${filename}. Exception ${JSON.stringify(e)}`);
|
|
|
|
}
|
|
|
|
}
|
2022-06-03 13:31:45 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-27 21:28:21 -07:00
|
|
|
|
2023-03-05 03:02:46 -06:00
|
|
|
wipeRbfCache() {
|
|
|
|
logger.notice(`Wipping nodejs backend cache/rbfcache.json file`);
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.unlinkSync(DiskCache.RBF_FILE_NAME);
|
|
|
|
} catch (e: any) {
|
|
|
|
if (e?.code !== 'ENOENT') {
|
|
|
|
logger.err(`Cannot wipe cache file ${DiskCache.RBF_FILE_NAME}. Exception ${JSON.stringify(e)}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-30 15:28:34 -06:00
|
|
|
async $loadMempoolCache(): Promise<void> {
|
2023-05-12 16:29:45 -06:00
|
|
|
if (!config.MEMPOOL.CACHE_ENABLED || !fs.existsSync(DiskCache.FILE_NAME)) {
|
2020-11-02 19:11:04 +07:00
|
|
|
return;
|
|
|
|
}
|
2021-01-27 01:49:11 +07:00
|
|
|
try {
|
2023-07-30 16:01:03 +09:00
|
|
|
const start = Date.now();
|
2021-01-27 01:49:11 +07:00
|
|
|
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);
|
2022-06-03 13:31:45 +02:00
|
|
|
if (data.cacheSchemaVersion === undefined || data.cacheSchemaVersion !== this.cacheSchemaVersion) {
|
|
|
|
logger.notice('Disk cache contains an outdated schema version. Clearing it and skipping the cache loading.');
|
|
|
|
return this.wipeCache();
|
|
|
|
}
|
2023-03-12 19:20:29 +09:00
|
|
|
if (data.network && data.network !== config.MEMPOOL.NETWORK) {
|
|
|
|
logger.notice('Disk cache contains data from a different network. Clearing it and skipping the cache loading.');
|
|
|
|
return this.wipeCache();
|
|
|
|
}
|
2022-06-03 13:31:45 +02:00
|
|
|
|
2021-02-14 19:50:31 +07:00
|
|
|
if (data.mempoolArray) {
|
|
|
|
for (const tx of data.mempoolArray) {
|
2023-06-25 20:37:42 -04:00
|
|
|
delete tx.uid;
|
2021-02-14 19:50:31 +07:00
|
|
|
data.mempool[tx.txid] = tx;
|
|
|
|
}
|
|
|
|
}
|
2021-01-27 01:49:11 +07:00
|
|
|
}
|
2020-02-26 17:49:53 +07:00
|
|
|
|
2021-02-14 19:50:31 +07:00
|
|
|
for (let i = 1; i < DiskCache.CHUNK_FILES; i++) {
|
2021-01-27 01:49:11 +07:00
|
|
|
const fileName = DiskCache.FILE_NAMES.replace('{number}', i.toString());
|
2021-04-12 13:21:49 +04:00
|
|
|
try {
|
|
|
|
if (fs.existsSync(fileName)) {
|
|
|
|
const cacheData2 = JSON.parse(fs.readFileSync(fileName, 'utf8'));
|
|
|
|
if (cacheData2.mempoolArray) {
|
|
|
|
for (const tx of cacheData2.mempoolArray) {
|
2023-06-25 20:37:42 -04:00
|
|
|
delete tx.uid;
|
2021-04-12 13:21:49 +04:00
|
|
|
data.mempool[tx.txid] = tx;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Object.assign(data.mempool, cacheData2.mempool);
|
2021-02-14 19:50:31 +07:00
|
|
|
}
|
|
|
|
}
|
2021-04-12 13:21:49 +04:00
|
|
|
} catch (e) {
|
2023-03-14 13:53:52 +09:00
|
|
|
logger.err('Error parsing ' + fileName + '. Skipping. Reason: ' + (e instanceof Error ? e.message : e));
|
2021-01-27 01:49:11 +07:00
|
|
|
}
|
2021-01-15 23:26:32 +07:00
|
|
|
}
|
2019-07-21 17:59:47 +03:00
|
|
|
|
2023-07-30 16:01:03 +09:00
|
|
|
logger.info(`Loaded mempool from disk cache in ${Date.now() - start} ms`);
|
|
|
|
|
2023-04-30 15:28:34 -06:00
|
|
|
await memPool.$setMempool(data.mempool);
|
2023-05-25 19:05:29 +04:00
|
|
|
if (!this.ignoreBlocksCache) {
|
|
|
|
blocks.setBlocks(data.blocks);
|
|
|
|
blocks.setBlockSummaries(data.blockSummaries || []);
|
2023-05-25 19:19:14 +04:00
|
|
|
} else {
|
|
|
|
logger.info('Re-saving cache with empty recent blocks data');
|
|
|
|
await this.$saveCacheToDisk(true);
|
2023-05-25 19:05:29 +04:00
|
|
|
}
|
2021-01-27 01:49:11 +07:00
|
|
|
} catch (e) {
|
2022-06-03 18:00:14 +02:00
|
|
|
logger.warn('Failed to parse mempoool and blocks cache. Skipping. Reason: ' + (e instanceof Error ? e.message : e));
|
2021-01-27 01:49:11 +07:00
|
|
|
}
|
2023-03-05 03:02:46 -06:00
|
|
|
|
|
|
|
try {
|
|
|
|
let rbfData: any = {};
|
|
|
|
const rbfCacheData = fs.readFileSync(DiskCache.RBF_FILE_NAME, 'utf8');
|
|
|
|
if (rbfCacheData) {
|
|
|
|
logger.info('Restoring rbf data from disk cache');
|
|
|
|
rbfData = JSON.parse(rbfCacheData);
|
|
|
|
if (rbfData.rbfCacheSchemaVersion === undefined || rbfData.rbfCacheSchemaVersion !== this.rbfCacheSchemaVersion) {
|
|
|
|
logger.notice('Rbf disk cache contains an outdated schema version. Clearing it and skipping the cache loading.');
|
|
|
|
return this.wipeRbfCache();
|
|
|
|
}
|
2023-03-30 05:46:11 +09:00
|
|
|
if (rbfData.network && rbfData.network !== config.MEMPOOL.NETWORK) {
|
|
|
|
logger.notice('Rbf disk cache contains data from a different network. Clearing it and skipping the cache loading.');
|
|
|
|
return this.wipeRbfCache();
|
|
|
|
}
|
2023-03-05 03:02:46 -06:00
|
|
|
}
|
|
|
|
|
2023-03-30 05:46:11 +09:00
|
|
|
if (rbfData?.rbf) {
|
|
|
|
rbfCache.load(rbfData.rbf);
|
|
|
|
}
|
2023-03-05 03:02:46 -06:00
|
|
|
} catch (e) {
|
|
|
|
logger.warn('Failed to parse rbf cache. Skipping. Reason: ' + (e instanceof Error ? e.message : e));
|
|
|
|
}
|
2019-07-21 17:59:47 +03:00
|
|
|
}
|
2023-05-01 14:30:30 -06:00
|
|
|
|
|
|
|
private $yield(): Promise<void> {
|
|
|
|
if (this.semaphore.locks) {
|
|
|
|
logger.debug('Pause writing mempool and blocks data to disk cache (async)');
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
this.semaphore.resume.push(resolve);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public lock(): void {
|
|
|
|
this.semaphore.locks++;
|
|
|
|
}
|
|
|
|
|
|
|
|
public unlock(): void {
|
|
|
|
this.semaphore.locks = Math.max(0, this.semaphore.locks - 1);
|
|
|
|
if (!this.semaphore.locks && this.semaphore.resume.length) {
|
|
|
|
const nextResume = this.semaphore.resume.shift();
|
|
|
|
if (nextResume) {
|
|
|
|
logger.debug('Resume writing mempool and blocks data to disk cache (async)');
|
|
|
|
nextResume();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-25 19:05:29 +04:00
|
|
|
|
|
|
|
public setIgnoreBlocksCache(): void {
|
|
|
|
this.ignoreBlocksCache = true;
|
|
|
|
}
|
2019-07-21 17:59:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export default new DiskCache();
|