Added mempool inSync property to backend. Display on frontend when not in sync and on't create statistics when not in sync.

fixes #29
This commit is contained in:
softsimon
2020-04-01 20:06:44 +07:00
parent 03316f99d1
commit c6df78815b
3 changed files with 31 additions and 9 deletions

View File

@@ -3,6 +3,7 @@ import bitcoinApi from './bitcoin/electrs-api';
import { MempoolInfo, TransactionExtended, Transaction } from '../interfaces';
class Mempool {
private inSync: boolean = false;
private mempoolCache: any = {};
private mempoolInfo: MempoolInfo | undefined;
private mempoolChangedCallback: Function | undefined;
@@ -17,6 +18,10 @@ class Mempool {
setInterval(this.updateTxPerSecond.bind(this), 1000);
}
public isInSync() {
return this.inSync;
}
public setMempoolChangedCallback(fn: Function) {
this.mempoolChangedCallback = fn;
}
@@ -94,11 +99,13 @@ class Mempool {
if (transaction) {
this.mempoolCache[txid] = transaction;
txCount++;
this.txPerSecondArray.push(new Date().getTime());
this.vBytesPerSecondArray.push({
unixTime: new Date().getTime(),
vSize: transaction.vsize,
});
if (this.inSync) {
this.txPerSecondArray.push(new Date().getTime());
this.vBytesPerSecondArray.push({
unixTime: new Date().getTime(),
vSize: transaction.vsize,
});
}
hasChange = true;
if (diff > 0) {
console.log('Fetched transaction ' + txCount + ' / ' + diff);
@@ -126,6 +133,11 @@ class Mempool {
}
});
if (!this.inSync && transactions.length === Object.keys(newMempool).length) {
this.inSync = true;
console.log('The mempool is now in sync!');
}
console.log(`New mempool size: ${Object.keys(newMempool).length} Change: ${diff}`);
this.mempoolCache = newMempool;

View File

@@ -23,7 +23,12 @@ class Statistics {
setTimeout(() => {
this.runStatistics();
this.intervalTimer = setInterval(() => { this.runStatistics(); }, 1 * 60 * 1000);
this.intervalTimer = setInterval(() => {
if (!memPool.isInSync()) {
return;
}
this.runStatistics();
}, 1 * 60 * 1000);
}, difference);
}