From 96a41400f4b915e27fed2e52c436076300672275 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Thu, 9 Mar 2023 03:36:14 -0600 Subject: [PATCH] Add axios support for esplora unix sockets --- backend/mempool-config.sample.json | 3 ++- backend/src/__fixtures__/mempool-config.template.json | 3 ++- backend/src/__tests__/config.test.ts | 2 +- backend/src/api/bitcoin/esplora-api.ts | 7 +++++-- backend/src/config.ts | 2 ++ docker/README.md | 4 +++- docker/backend/mempool-config.json | 3 ++- docker/backend/start.sh | 2 ++ 8 files changed, 19 insertions(+), 7 deletions(-) diff --git a/backend/mempool-config.sample.json b/backend/mempool-config.sample.json index 2369b64b5..cd2afb0bd 100644 --- a/backend/mempool-config.sample.json +++ b/backend/mempool-config.sample.json @@ -41,7 +41,8 @@ "TLS_ENABLED": true }, "ESPLORA": { - "REST_API_URL": "http://127.0.0.1:3000" + "REST_API_URL": "http://127.0.0.1:3000", + "UNIX_SOCKET_PATH": "/tmp/esplora-bitcoin-mainnet" }, "SECOND_CORE_RPC": { "HOST": "127.0.0.1", diff --git a/backend/src/__fixtures__/mempool-config.template.json b/backend/src/__fixtures__/mempool-config.template.json index 2bf52cbcf..ab0e40416 100644 --- a/backend/src/__fixtures__/mempool-config.template.json +++ b/backend/src/__fixtures__/mempool-config.template.json @@ -42,7 +42,8 @@ "TLS_ENABLED": true }, "ESPLORA": { - "REST_API_URL": "__ESPLORA_REST_API_URL__" + "REST_API_URL": "__ESPLORA_REST_API_URL__", + "UNIX_SOCKET_PATH": "__ESPLORA_UNIX_SOCKET_PATH__" }, "SECOND_CORE_RPC": { "HOST": "__SECOND_CORE_RPC_HOST__", diff --git a/backend/src/__tests__/config.test.ts b/backend/src/__tests__/config.test.ts index 5717808dd..d28f144ce 100644 --- a/backend/src/__tests__/config.test.ts +++ b/backend/src/__tests__/config.test.ts @@ -46,7 +46,7 @@ describe('Mempool Backend Config', () => { expect(config.ELECTRUM).toStrictEqual({ HOST: '127.0.0.1', PORT: 3306, TLS_ENABLED: true }); - expect(config.ESPLORA).toStrictEqual({ REST_API_URL: 'http://127.0.0.1:3000' }); + expect(config.ESPLORA).toStrictEqual({ REST_API_URL: 'http://127.0.0.1:3000', UNIX_SOCKET_PATH: null }); expect(config.CORE_RPC).toStrictEqual({ HOST: '127.0.0.1', diff --git a/backend/src/api/bitcoin/esplora-api.ts b/backend/src/api/bitcoin/esplora-api.ts index 0366695d1..ff6219587 100644 --- a/backend/src/api/bitcoin/esplora-api.ts +++ b/backend/src/api/bitcoin/esplora-api.ts @@ -5,11 +5,14 @@ import { AbstractBitcoinApi } from './bitcoin-api-abstract-factory'; import { IEsploraApi } from './esplora-api.interface'; const axiosConnection = axios.create({ - httpAgent: new http.Agent({ keepAlive: true }) + httpAgent: new http.Agent({ keepAlive: true, }) }); class ElectrsApi implements AbstractBitcoinApi { - axiosConfig: AxiosRequestConfig = { + axiosConfig: AxiosRequestConfig = config.ESPLORA.UNIX_SOCKET_PATH ? { + socketPath: config.ESPLORA.UNIX_SOCKET_PATH, + timeout: 10000, + } : { timeout: 10000, }; diff --git a/backend/src/config.ts b/backend/src/config.ts index 8ccd7e2e4..c0e3d297c 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -36,6 +36,7 @@ interface IConfig { }; ESPLORA: { REST_API_URL: string; + UNIX_SOCKET_PATH: string | void | null; }; LIGHTNING: { ENABLED: boolean; @@ -158,6 +159,7 @@ const defaults: IConfig = { }, 'ESPLORA': { 'REST_API_URL': 'http://127.0.0.1:3000', + 'UNIX_SOCKET_PATH': null, }, 'ELECTRUM': { 'HOST': '127.0.0.1', diff --git a/docker/README.md b/docker/README.md index 468d8069b..9389d32c0 100644 --- a/docker/README.md +++ b/docker/README.md @@ -199,7 +199,8 @@ Corresponding `docker-compose.yml` overrides: `mempool-config.json`: ```json "ESPLORA": { - "REST_API_URL": "http://127.0.0.1:3000" + "REST_API_URL": "http://127.0.0.1:3000", + "UNIX_SOCKET_PATH": "/tmp/esplora-socket" }, ``` @@ -208,6 +209,7 @@ Corresponding `docker-compose.yml` overrides: api: environment: ESPLORA_REST_API_URL: "" + ESPLORA_UNIX_SOCKET_PATH: "" ... ``` diff --git a/docker/backend/mempool-config.json b/docker/backend/mempool-config.json index 78a2c116b..e8ab87d92 100644 --- a/docker/backend/mempool-config.json +++ b/docker/backend/mempool-config.json @@ -40,7 +40,8 @@ "TLS_ENABLED": __ELECTRUM_TLS_ENABLED__ }, "ESPLORA": { - "REST_API_URL": "__ESPLORA_REST_API_URL__" + "REST_API_URL": "__ESPLORA_REST_API_URL__", + "UNIX_SOCKET_PATH": "__ESPLORA_UNIX_SOCKET_PATH__" }, "SECOND_CORE_RPC": { "HOST": "__SECOND_CORE_RPC_HOST__", diff --git a/docker/backend/start.sh b/docker/backend/start.sh index ee5069386..76a89610b 100755 --- a/docker/backend/start.sh +++ b/docker/backend/start.sh @@ -45,6 +45,7 @@ __ELECTRUM_TLS_ENABLED__=${ELECTRUM_TLS_ENABLED:=false} # ESPLORA __ESPLORA_REST_API_URL__=${ESPLORA_REST_API_URL:=http://127.0.0.1:3000} +__ESPLORA_UNIX_SOCKET_PATH__=${ESPLORA_UNIX_SOCKET_PATH:=null} # SECOND_CORE_RPC __SECOND_CORE_RPC_HOST__=${SECOND_CORE_RPC_HOST:=127.0.0.1} @@ -155,6 +156,7 @@ sed -i "s/__ELECTRUM_PORT__/${__ELECTRUM_PORT__}/g" mempool-config.json sed -i "s/__ELECTRUM_TLS_ENABLED__/${__ELECTRUM_TLS_ENABLED__}/g" mempool-config.json sed -i "s!__ESPLORA_REST_API_URL__!${__ESPLORA_REST_API_URL__}!g" mempool-config.json +sed -i "s!__ESPLORA_UNIX_SOCKET_PATH__!${__ESPLORA_UNIX_SOCKET_PATH__}!g" mempool-config.json sed -i "s/__SECOND_CORE_RPC_HOST__/${__SECOND_CORE_RPC_HOST__}/g" mempool-config.json sed -i "s/__SECOND_CORE_RPC_PORT__/${__SECOND_CORE_RPC_PORT__}/g" mempool-config.json