Don't save disk cache on exit. Handle corrupted mempool disk cache.
fixes #304
This commit is contained in:
		
							parent
							
								
									b08225dab5
								
							
						
					
					
						commit
						5f1f06fecf
					
				@ -1,6 +1,5 @@
 | 
			
		||||
import * as fs from 'fs';
 | 
			
		||||
const fsPromises = fs.promises;
 | 
			
		||||
import * as process from 'process';
 | 
			
		||||
import * as cluster from 'cluster';
 | 
			
		||||
import memPool from './mempool';
 | 
			
		||||
import blocks from './blocks';
 | 
			
		||||
@ -10,19 +9,7 @@ class DiskCache {
 | 
			
		||||
  private static FILE_NAME = './cache.json';
 | 
			
		||||
  private static FILE_NAMES = './cache{number}.json';
 | 
			
		||||
  private static CHUNK_SIZE = 10000;
 | 
			
		||||
  constructor() {
 | 
			
		||||
    if (!cluster.isMaster) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    process.on('SIGINT', () => {
 | 
			
		||||
      this.saveCacheToDiskSync();
 | 
			
		||||
      process.exit(2);
 | 
			
		||||
    });
 | 
			
		||||
    process.on('SIGTERM', () => {
 | 
			
		||||
      this.saveCacheToDiskSync();
 | 
			
		||||
      process.exit(2);
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
  constructor() { }
 | 
			
		||||
 | 
			
		||||
  async $saveCacheToDisk(): Promise<void> {
 | 
			
		||||
    if (!cluster.isMaster) {
 | 
			
		||||
@ -51,35 +38,11 @@ class DiskCache {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  saveCacheToDiskSync(): void {
 | 
			
		||||
    try {
 | 
			
		||||
      logger.debug('Writing mempool and blocks data to disk cache...');
 | 
			
		||||
      const mempoolChunk_1 = Object.fromEntries(Object.entries(memPool.getMempool()).slice(0, DiskCache.CHUNK_SIZE));
 | 
			
		||||
      fs.writeFileSync(DiskCache.FILE_NAME, JSON.stringify({
 | 
			
		||||
        blocks: blocks.getBlocks(),
 | 
			
		||||
        mempool: mempoolChunk_1
 | 
			
		||||
      }), {flag: 'w'});
 | 
			
		||||
      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'});
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      logger.debug('Mempool and blocks data saved to disk cache');
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      logger.warn('Error writing to cache file: ' + e.message || e);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  loadMempoolCache() {
 | 
			
		||||
    if (!fs.existsSync(DiskCache.FILE_NAME)) {
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
    try {
 | 
			
		||||
      let data: any = {};
 | 
			
		||||
      const cacheData = fs.readFileSync(DiskCache.FILE_NAME, 'utf8');
 | 
			
		||||
      if (cacheData) {
 | 
			
		||||
@ -97,6 +60,9 @@ class DiskCache {
 | 
			
		||||
 | 
			
		||||
      memPool.setMempool(data.mempool);
 | 
			
		||||
      blocks.setBlocks(data.blocks);
 | 
			
		||||
    } catch (e) {
 | 
			
		||||
      logger.warn('Failed to parse mempoool and blocks cache. Skipping...');
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user