mirror of
https://github.com/mempool/mempool.git
synced 2024-11-20 10:21:52 +01:00
Also save and load blocks to cache.json for speedy restarts.
This commit is contained in:
parent
50b4e1523e
commit
39394e1178
@ -14,6 +14,10 @@ class Blocks {
|
||||
return this.blocks;
|
||||
}
|
||||
|
||||
public setBlocks(blocks: Block[]) {
|
||||
this.blocks = blocks;
|
||||
}
|
||||
|
||||
public setNewBlockCallback(fn: Function) {
|
||||
this.newBlockCallback = fn;
|
||||
}
|
||||
|
@ -1,13 +1,17 @@
|
||||
import * as fs from 'fs';
|
||||
import memPool from './mempool';
|
||||
import blocks from './blocks';
|
||||
|
||||
class DiskCache {
|
||||
static FILE_NAME = './cache.json';
|
||||
|
||||
constructor() {
|
||||
process.on('SIGINT', () => {
|
||||
this.saveData(JSON.stringify(memPool.getMempool()));
|
||||
console.log('Mempool data saved to disk cache');
|
||||
this.saveData(JSON.stringify({
|
||||
mempool: memPool.getMempool(),
|
||||
blocks: blocks.getBlocks(),
|
||||
}));
|
||||
console.log('Mempool and blocks data saved to disk cache');
|
||||
process.exit(2);
|
||||
});
|
||||
}
|
||||
@ -15,8 +19,14 @@ class DiskCache {
|
||||
loadMempoolCache() {
|
||||
const cacheData = this.loadData();
|
||||
if (cacheData) {
|
||||
console.log('Restoring mempool data from disk cache');
|
||||
memPool.setMempool(JSON.parse(cacheData));
|
||||
console.log('Restoring mempool and blocks data from disk cache');
|
||||
const data = JSON.parse(cacheData);
|
||||
if (data.mempool) {
|
||||
memPool.setMempool(data.mempool);
|
||||
blocks.setBlocks(data.blocks);
|
||||
} else {
|
||||
memPool.setMempool(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user