From 3126a559a03794cddd0d412b44293e8c25d8a6cb Mon Sep 17 00:00:00 2001 From: Mononaut Date: Sat, 3 Dec 2022 11:17:53 +0900 Subject: [PATCH] Make forensics backend call rate limiting configurable --- backend/mempool-config.sample.json | 3 ++- backend/src/__fixtures__/mempool-config.template.json | 3 ++- backend/src/config.ts | 2 ++ backend/src/tasks/lightning/forensics.service.ts | 7 +++---- 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/backend/mempool-config.sample.json b/backend/mempool-config.sample.json index 6de690ad8..dbbc8412d 100644 --- a/backend/mempool-config.sample.json +++ b/backend/mempool-config.sample.json @@ -85,7 +85,8 @@ "STATS_REFRESH_INTERVAL": 600, "GRAPH_REFRESH_INTERVAL": 600, "LOGGER_UPDATE_INTERVAL": 30, - "FORENSICS_INTERVAL": 43200 + "FORENSICS_INTERVAL": 43200, + "FORENSICS_RATE_LIMIT": 20 }, "LND": { "TLS_CERT_PATH": "tls.cert", diff --git a/backend/src/__fixtures__/mempool-config.template.json b/backend/src/__fixtures__/mempool-config.template.json index 7a988a70d..2e9221c7a 100644 --- a/backend/src/__fixtures__/mempool-config.template.json +++ b/backend/src/__fixtures__/mempool-config.template.json @@ -101,7 +101,8 @@ "STATS_REFRESH_INTERVAL": 600, "GRAPH_REFRESH_INTERVAL": 600, "LOGGER_UPDATE_INTERVAL": 30, - "FORENSICS_INTERVAL": 43200 + "FORENSICS_INTERVAL": 43200, + "FORENSICS_RATE_LIMIT": "__FORENSICS_RATE_LIMIT__" }, "LND": { "TLS_CERT_PATH": "", diff --git a/backend/src/config.ts b/backend/src/config.ts index 3a3d2b56d..e97deb5e5 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -44,6 +44,7 @@ interface IConfig { GRAPH_REFRESH_INTERVAL: number; LOGGER_UPDATE_INTERVAL: number; FORENSICS_INTERVAL: number; + FORENSICS_RATE_LIMIT: number; }; LND: { TLS_CERT_PATH: string; @@ -205,6 +206,7 @@ const defaults: IConfig = { 'GRAPH_REFRESH_INTERVAL': 600, 'LOGGER_UPDATE_INTERVAL': 30, 'FORENSICS_INTERVAL': 43200, + 'FORENSICS_RATE_LIMIT': 20, }, 'LND': { 'TLS_CERT_PATH': '', diff --git a/backend/src/tasks/lightning/forensics.service.ts b/backend/src/tasks/lightning/forensics.service.ts index 7acb36e89..c62639411 100644 --- a/backend/src/tasks/lightning/forensics.service.ts +++ b/backend/src/tasks/lightning/forensics.service.ts @@ -7,7 +7,6 @@ import { IEsploraApi } from '../../api/bitcoin/esplora-api.interface'; import { Common } from '../../api/common'; import { ILightningApi } from '../../api/lightning/lightning-api.interface'; -const throttleDelay = 20; //ms const tempCacheSize = 10000; class ForensicsService { @@ -91,7 +90,7 @@ class ForensicsService { let outspends: IEsploraApi.Outspend[] | undefined; try { outspends = await bitcoinApi.$getOutspends(channel.closing_transaction_id); - await Common.sleep$(throttleDelay); + await Common.sleep$(config.LIGHTNING.FORENSICS_RATE_LIMIT); } catch (e) { logger.err(`Failed to call ${config.ESPLORA.REST_API_URL + '/tx/' + channel.closing_transaction_id + '/outspends'}. Reason ${e instanceof Error ? e.message : e}`); continue; @@ -340,7 +339,7 @@ class ForensicsService { let outspends: IEsploraApi.Outspend[] | undefined; try { outspends = await bitcoinApi.$getOutspends(input.txid); - await Common.sleep$(throttleDelay); + await Common.sleep$(config.LIGHTNING.FORENSICS_RATE_LIMIT); } catch (e) { logger.err(`Failed to call ${config.ESPLORA.REST_API_URL + '/tx/' + input.txid + '/outspends'}. Reason ${e instanceof Error ? e.message : e}`); } @@ -429,7 +428,7 @@ class ForensicsService { if (temp) { this.tempCached.push(txid); } - await Common.sleep$(throttleDelay); + await Common.sleep$(config.LIGHTNING.FORENSICS_RATE_LIMIT); } catch (e) { logger.err(`Failed to call ${config.ESPLORA.REST_API_URL + '/tx/' + txid + '/outspends'}. Reason ${e instanceof Error ? e.message : e}`); return null;