From dc63fd94284d2f8fd5fe1eb01cf9ece446c2b1a4 Mon Sep 17 00:00:00 2001 From: softsimon Date: Wed, 6 Jan 2021 22:49:28 +0700 Subject: [PATCH] Config file updates. electrs -> esplora --- backend/mempool-config.sample.json | 9 ++++----- backend/src/api/bitcoin/electrum-api.ts | 2 +- backend/src/api/bitcoin/esplora-api.ts | 14 +++++++------- backend/src/api/mempool.ts | 3 ++- backend/src/config.ts | 22 ++++++++++------------ backend/src/index.ts | 16 ++++++++-------- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/backend/mempool-config.sample.json b/backend/mempool-config.sample.json index 694bb180b..f51aa3cb9 100644 --- a/backend/mempool-config.sample.json +++ b/backend/mempool-config.sample.json @@ -5,16 +5,15 @@ "HTTP_PORT": 8999, "SPAWN_CLUSTER_PROCS": 0, "API_URL_PREFIX": "/api/v1/", - "WEBSOCKET_REFRESH_RATE_MS": 2000 - }, - "ELECTRS": { - "REST_API_URL": "http://127.0.0.1:3000", "POLL_RATE_MS": 2000 }, + "ESPLORA": { + "REST_API_URL": "http://127.0.0.1:3000" + }, "ELECTRUM": { "HOST": "127.0.0.1", "PORT": 50002, - "PROTOCOL": "tcl", + "TLS_ENABLED": true, "TX_LOOKUPS": false }, "BITCOIND": { diff --git a/backend/src/api/bitcoin/electrum-api.ts b/backend/src/api/bitcoin/electrum-api.ts index 2929c2da4..b8bcc90d7 100644 --- a/backend/src/api/bitcoin/electrum-api.ts +++ b/backend/src/api/bitcoin/electrum-api.ts @@ -30,7 +30,7 @@ class BitcoindElectrsApi extends BitcoinApi implements AbstractBitcoinApi { this.electrumClient = new ElectrumClient( config.ELECTRUM.PORT, config.ELECTRUM.HOST, - config.ELECTRUM.PROTOCOL, + config.ELECTRUM.TLS_ENABLED ? 'tls' : 'tcp', null, electrumCallbacks ); diff --git a/backend/src/api/bitcoin/esplora-api.ts b/backend/src/api/bitcoin/esplora-api.ts index a7bedbbec..df566daac 100644 --- a/backend/src/api/bitcoin/esplora-api.ts +++ b/backend/src/api/bitcoin/esplora-api.ts @@ -8,32 +8,32 @@ class ElectrsApi implements AbstractBitcoinApi { constructor() { } $getRawMempool(): Promise { - return axios.get(config.ELECTRS.REST_API_URL + '/mempool/txids') + return axios.get(config.ESPLORA.REST_API_URL + '/mempool/txids') .then((response) => response.data); } $getRawTransaction(txId: string): Promise { - return axios.get(config.ELECTRS.REST_API_URL + '/tx/' + txId) + return axios.get(config.ESPLORA.REST_API_URL + '/tx/' + txId) .then((response) => response.data); } $getBlockHeightTip(): Promise { - return axios.get(config.ELECTRS.REST_API_URL + '/blocks/tip/height') + return axios.get(config.ESPLORA.REST_API_URL + '/blocks/tip/height') .then((response) => response.data); } $getTxIdsForBlock(hash: string): Promise { - return axios.get(config.ELECTRS.REST_API_URL + '/block/' + hash + '/txids') + return axios.get(config.ESPLORA.REST_API_URL + '/block/' + hash + '/txids') .then((response) => response.data); } $getBlockHash(height: number): Promise { - return axios.get(config.ELECTRS.REST_API_URL + '/block-height/' + height) + return axios.get(config.ESPLORA.REST_API_URL + '/block-height/' + height) .then((response) => response.data); } $getBlock(hash: string): Promise { - return axios.get(config.ELECTRS.REST_API_URL + '/block/' + hash) + return axios.get(config.ESPLORA.REST_API_URL + '/block/' + hash) .then((response) => response.data); } @@ -46,7 +46,7 @@ class ElectrsApi implements AbstractBitcoinApi { } $getRawTransactionBitcoind(txId: string): Promise { - return axios.get(config.ELECTRS.REST_API_URL + '/tx/' + txId) + return axios.get(config.ESPLORA.REST_API_URL + '/tx/' + txId) .then((response) => response.data); } diff --git a/backend/src/api/mempool.ts b/backend/src/api/mempool.ts index 27cd94794..c00d5c10c 100644 --- a/backend/src/api/mempool.ts +++ b/backend/src/api/mempool.ts @@ -9,6 +9,7 @@ import bitcoinBaseApi from './bitcoin/bitcoin-base.api'; import loadingIndicators from './loading-indicators'; class Mempool { + private static WEBSOCKET_REFRESH_RATE_MS = 10000; private inSync: boolean = false; private mempoolCache: { [txId: string]: TransactionExtended } = {}; private mempoolInfo: IBitcoinApi.MempoolInfo = { loaded: false, size: 0, bytes: 0, usage: 0, @@ -120,7 +121,7 @@ class Mempool { } } - if ((new Date().getTime()) - start > config.MEMPOOL.WEBSOCKET_REFRESH_RATE_MS * 10) { + if ((new Date().getTime()) - start > Mempool.WEBSOCKET_REFRESH_RATE_MS) { break; } } diff --git a/backend/src/config.ts b/backend/src/config.ts index 20132fdd8..1886ae880 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -7,16 +7,15 @@ interface IConfig { HTTP_PORT: number; SPAWN_CLUSTER_PROCS: number; API_URL_PREFIX: string; - WEBSOCKET_REFRESH_RATE_MS: number; - }; - ELECTRS: { - REST_API_URL: string; POLL_RATE_MS: number; }; + ESPLORA: { + REST_API_URL: string; + }; ELECTRUM: { HOST: string; PORT: number; - PROTOCOL: 'tls' | 'tcp'; + TLS_ENABLED: boolean; TX_LOOKUPS: boolean; }; BITCOIND: { @@ -61,16 +60,15 @@ const defaults: IConfig = { 'HTTP_PORT': 8999, 'SPAWN_CLUSTER_PROCS': 0, 'API_URL_PREFIX': '/api/v1/', - 'WEBSOCKET_REFRESH_RATE_MS': 2000 - }, - 'ELECTRS': { - 'REST_API_URL': 'http://127.0.0.1:3000', 'POLL_RATE_MS': 2000 }, + 'ESPLORA': { + 'REST_API_URL': 'http://127.0.0.1:3000', + }, 'ELECTRUM': { 'HOST': '127.0.0.1', 'PORT': 3306, - 'PROTOCOL': 'tls', + 'TLS_ENABLED': true, 'TX_LOOKUPS': false }, 'BITCOIND': { @@ -110,7 +108,7 @@ const defaults: IConfig = { class Config implements IConfig { MEMPOOL: IConfig['MEMPOOL']; - ELECTRS: IConfig['ELECTRS']; + ESPLORA: IConfig['ESPLORA']; ELECTRUM: IConfig['ELECTRUM']; BITCOIND: IConfig['BITCOIND']; DATABASE: IConfig['DATABASE']; @@ -122,7 +120,7 @@ class Config implements IConfig { constructor() { const configs = this.merge(configFile, defaults); this.MEMPOOL = configs.MEMPOOL; - this.ELECTRS = configs.ELECTRS; + this.ESPLORA = configs.ESPLORA; this.ELECTRUM = configs.ELECTRUM; this.BITCOIND = configs.BITCOIND; this.DATABASE = configs.DATABASE; diff --git a/backend/src/index.ts b/backend/src/index.ts index d2db74b74..9d88453fb 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -26,7 +26,7 @@ class Server { private wss: WebSocket.Server | undefined; private server: https.Server | http.Server | undefined; private app: Express; - private retryOnElectrsErrorAfterSeconds = 5; + private currentBackendRetryInterval = 5; constructor() { this.app = express(); @@ -111,19 +111,19 @@ class Server { await memPool.$updateMemPoolInfo(); await blocks.$updateBlocks(); await memPool.$updateMempool(); - setTimeout(this.runMainUpdateLoop.bind(this), config.ELECTRS.POLL_RATE_MS); - this.retryOnElectrsErrorAfterSeconds = 5; + setTimeout(this.runMainUpdateLoop.bind(this), config.MEMPOOL.POLL_RATE_MS); + this.currentBackendRetryInterval = 5; } catch (e) { - const loggerMsg = `runMainLoop error: ${(e.message || e)}. Retrying in ${this.retryOnElectrsErrorAfterSeconds} sec.`; - if (this.retryOnElectrsErrorAfterSeconds > 5) { + const loggerMsg = `runMainLoop error: ${(e.message || e)}. Retrying in ${this.currentBackendRetryInterval} sec.`; + if (this.currentBackendRetryInterval > 5) { logger.warn(loggerMsg); } else { logger.debug(loggerMsg); } logger.debug(JSON.stringify(e)); - setTimeout(this.runMainUpdateLoop.bind(this), 1000 * this.retryOnElectrsErrorAfterSeconds); - this.retryOnElectrsErrorAfterSeconds *= 2; - this.retryOnElectrsErrorAfterSeconds = Math.min(this.retryOnElectrsErrorAfterSeconds, 60); + setTimeout(this.runMainUpdateLoop.bind(this), 1000 * this.currentBackendRetryInterval); + this.currentBackendRetryInterval *= 2; + this.currentBackendRetryInterval = Math.min(this.currentBackendRetryInterval, 60); } }