Also save and load blocks to cache.json for speedy restarts.

This commit is contained in:
softsimon 2020-02-29 21:52:04 +07:00
parent 50b4e1523e
commit 39394e1178
No known key found for this signature in database
GPG Key ID: 488D7DCFB5A430D7
2 changed files with 18 additions and 4 deletions

View File

@ -14,6 +14,10 @@ class Blocks {
return this.blocks; return this.blocks;
} }
public setBlocks(blocks: Block[]) {
this.blocks = blocks;
}
public setNewBlockCallback(fn: Function) { public setNewBlockCallback(fn: Function) {
this.newBlockCallback = fn; this.newBlockCallback = fn;
} }

View File

@ -1,13 +1,17 @@
import * as fs from 'fs'; import * as fs from 'fs';
import memPool from './mempool'; import memPool from './mempool';
import blocks from './blocks';
class DiskCache { class DiskCache {
static FILE_NAME = './cache.json'; static FILE_NAME = './cache.json';
constructor() { constructor() {
process.on('SIGINT', () => { process.on('SIGINT', () => {
this.saveData(JSON.stringify(memPool.getMempool())); this.saveData(JSON.stringify({
console.log('Mempool data saved to disk cache'); mempool: memPool.getMempool(),
blocks: blocks.getBlocks(),
}));
console.log('Mempool and blocks data saved to disk cache');
process.exit(2); process.exit(2);
}); });
} }
@ -15,8 +19,14 @@ class DiskCache {
loadMempoolCache() { loadMempoolCache() {
const cacheData = this.loadData(); const cacheData = this.loadData();
if (cacheData) { if (cacheData) {
console.log('Restoring mempool data from disk cache'); console.log('Restoring mempool and blocks data from disk cache');
memPool.setMempool(JSON.parse(cacheData)); const data = JSON.parse(cacheData);
if (data.mempool) {
memPool.setMempool(data.mempool);
blocks.setBlocks(data.blocks);
} else {
memPool.setMempool(data);
}
} }
} }