From 1a6c2e79e62033a2cacf60f909ecb8e4d614f08c Mon Sep 17 00:00:00 2001 From: softsimon Date: Tue, 5 Jan 2021 03:06:57 +0700 Subject: [PATCH] Electrum protocol and tx lookup setting. --- backend/mempool-config.sample.json | 8 ++++++-- backend/src/api/bitcoin/electrum-api.ts | 15 +++++++++------ backend/src/config.ts | 14 ++++++++++++-- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/backend/mempool-config.sample.json b/backend/mempool-config.sample.json index 786aaddc8..694bb180b 100644 --- a/backend/mempool-config.sample.json +++ b/backend/mempool-config.sample.json @@ -9,9 +9,13 @@ }, "ELECTRS": { "REST_API_URL": "http://127.0.0.1:3000", - "POLL_RATE_MS": 2000, + "POLL_RATE_MS": 2000 + }, + "ELECTRUM": { "HOST": "127.0.0.1", - "PORT": 50002 + "PORT": 50002, + "PROTOCOL": "tcl", + "TX_LOOKUPS": false }, "BITCOIND": { "HOST": "127.0.0.1", diff --git a/backend/src/api/bitcoin/electrum-api.ts b/backend/src/api/bitcoin/electrum-api.ts index ee7fe2276..f2d498b00 100644 --- a/backend/src/api/bitcoin/electrum-api.ts +++ b/backend/src/api/bitcoin/electrum-api.ts @@ -21,16 +21,16 @@ class BitcoindElectrsApi extends BitcoinApi implements AbstractBitcoinApi { const electrumPersistencePolicy = { retryPeriod: 10000, maxRetry: 1000, callback: null }; const electrumCallbacks = { - onConnect: (client, versionInfo) => { logger.info(`Connected to Electrum Server at ${config.ELECTRS.HOST}:${config.ELECTRS.PORT} (${JSON.stringify(versionInfo)})`); }, - onClose: (client) => { logger.info(`Disconnected from Electrum Server at ${config.ELECTRS.HOST}:${config.ELECTRS.PORT}`); }, + onConnect: (client, versionInfo) => { logger.info(`Connected to Electrum Server at ${config.ELECTRUM.HOST}:${config.ELECTRUM.PORT} (${JSON.stringify(versionInfo)})`); }, + onClose: (client) => { logger.info(`Disconnected from Electrum Server at ${config.ELECTRUM.HOST}:${config.ELECTRUM.PORT}`); }, onError: (err) => { logger.err(`Electrum error: ${JSON.stringify(err)}`); }, onLog: (str) => { logger.debug(str); }, }; this.electrumClient = new ElectrumClient( - config.ELECTRS.PORT, - config.ELECTRS.HOST, - config.ELECTRS.PORT === 50001 ? 'tcp' : 'tls', + config.ELECTRUM.PORT, + config.ELECTRUM.HOST, + config.ELECTRUM.PROTOCOL, null, electrumCallbacks ); @@ -38,11 +38,14 @@ class BitcoindElectrsApi extends BitcoinApi implements AbstractBitcoinApi { this.electrumClient.initElectrum(electrumConfig, electrumPersistencePolicy) .then(() => {}) .catch((err) => { - logger.err(`Error connecting to Electrum Server at ${config.ELECTRS.HOST}:${config.ELECTRS.PORT}`); + logger.err(`Error connecting to Electrum Server at ${config.ELECTRUM.HOST}:${config.ELECTRUM.PORT}`); }); } async $getRawTransaction(txId: string, skipConversion = false, addPrevout = false): Promise { + if (!config.ELECTRUM.TX_LOOKUPS) { + return super.$getRawTransaction(txId, skipConversion, addPrevout); + } const txInMempool = mempool.getMempool()[txId]; if (txInMempool && addPrevout) { return this.$addPrevouts(txInMempool); diff --git a/backend/src/config.ts b/backend/src/config.ts index b8af02efc..20132fdd8 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -12,8 +12,12 @@ interface IConfig { ELECTRS: { REST_API_URL: string; POLL_RATE_MS: number; + }; + ELECTRUM: { HOST: string; PORT: number; + PROTOCOL: 'tls' | 'tcp'; + TX_LOOKUPS: boolean; }; BITCOIND: { HOST: string; @@ -61,9 +65,13 @@ const defaults: IConfig = { }, 'ELECTRS': { 'REST_API_URL': 'http://127.0.0.1:3000', - 'POLL_RATE_MS': 2000, + 'POLL_RATE_MS': 2000 + }, + 'ELECTRUM': { 'HOST': '127.0.0.1', - 'PORT': 3306 + 'PORT': 3306, + 'PROTOCOL': 'tls', + 'TX_LOOKUPS': false }, 'BITCOIND': { 'HOST': '127.0.0.1', @@ -103,6 +111,7 @@ const defaults: IConfig = { class Config implements IConfig { MEMPOOL: IConfig['MEMPOOL']; ELECTRS: IConfig['ELECTRS']; + ELECTRUM: IConfig['ELECTRUM']; BITCOIND: IConfig['BITCOIND']; DATABASE: IConfig['DATABASE']; STATISTICS: IConfig['STATISTICS']; @@ -114,6 +123,7 @@ class Config implements IConfig { const configs = this.merge(configFile, defaults); this.MEMPOOL = configs.MEMPOOL; this.ELECTRS = configs.ELECTRS; + this.ELECTRUM = configs.ELECTRUM; this.BITCOIND = configs.BITCOIND; this.DATABASE = configs.DATABASE; this.STATISTICS = configs.STATISTICS;