From c6df78815b9daad654553dbd01d1414a3addb12f Mon Sep 17 00:00:00 2001 From: softsimon Date: Wed, 1 Apr 2020 20:06:44 +0700 Subject: [PATCH] Added mempool inSync property to backend. Display on frontend when not in sync and on't create statistics when not in sync. fixes #29 --- backend/src/api/mempool.ts | 22 ++++++++++++++----- backend/src/api/statistics.ts | 7 +++++- .../components/footer/footer.component.html | 11 +++++++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/backend/src/api/mempool.ts b/backend/src/api/mempool.ts index bba043759..f414c99d8 100644 --- a/backend/src/api/mempool.ts +++ b/backend/src/api/mempool.ts @@ -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; diff --git a/backend/src/api/statistics.ts b/backend/src/api/statistics.ts index c9390b9db..116b48e35 100644 --- a/backend/src/api/statistics.ts +++ b/backend/src/api/statistics.ts @@ -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); } diff --git a/frontend/src/app/components/footer/footer.component.html b/frontend/src/app/components/footer/footer.component.html index 2c1770eed..4e52aecb7 100644 --- a/frontend/src/app/components/footer/footer.component.html +++ b/frontend/src/app/components/footer/footer.component.html @@ -3,9 +3,14 @@
Tx weight per second: -
-
{{ memPoolInfo?.vBytesPerSecond | ceil | number }} vBytes/s
-
+ +  Backend is synchronizing + + +
+
{{ memPoolInfo?.vBytesPerSecond | ceil | number }} vBytes/s
+
+
Unconfirmed transactions: