diff --git a/backend/mempool-config.sample.json b/backend/mempool-config.sample.json index b29e8cb7a..2f27dab6a 100644 --- a/backend/mempool-config.sample.json +++ b/backend/mempool-config.sample.json @@ -59,7 +59,8 @@ "RETRY_UNIX_SOCKET_AFTER": 30000, "REQUEST_TIMEOUT": 10000, "FALLBACK_TIMEOUT": 5000, - "FALLBACK": [] + "FALLBACK": [], + "MAX_BEHIND_TIP": 2, }, "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 f81e889ca..37e21d4d0 100644 --- a/backend/src/__fixtures__/mempool-config.template.json +++ b/backend/src/__fixtures__/mempool-config.template.json @@ -60,7 +60,8 @@ "RETRY_UNIX_SOCKET_AFTER": 888, "REQUEST_TIMEOUT": 10000, "FALLBACK_TIMEOUT": 5000, - "FALLBACK": [] + "FALLBACK": [], + "MAX_BEHIND_TIP": 2 }, "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 18f72f88c..7d04338f4 100644 --- a/backend/src/__tests__/config.test.ts +++ b/backend/src/__tests__/config.test.ts @@ -63,6 +63,7 @@ describe('Mempool Backend Config', () => { REQUEST_TIMEOUT: 10000, FALLBACK_TIMEOUT: 5000, FALLBACK: [], + MAX_BEHIND_TIP: 2, }); expect(config.CORE_RPC).toStrictEqual({ diff --git a/backend/src/api/bitcoin/esplora-api.ts b/backend/src/api/bitcoin/esplora-api.ts index 90e50d4c2..c8d96a277 100644 --- a/backend/src/api/bitcoin/esplora-api.ts +++ b/backend/src/api/bitcoin/esplora-api.ts @@ -25,6 +25,7 @@ interface FailoverHost { class FailoverRouter { activeHost: FailoverHost; fallbackHost: FailoverHost; + maxSlippage: number = config.ESPLORA.MAX_BEHIND_TIP ?? 2; maxHeight: number = 0; hosts: FailoverHost[]; multihost: boolean; @@ -93,13 +94,13 @@ class FailoverRouter { ); if (result) { const height = result.data; - this.maxHeight = Math.max(height, this.maxHeight); + this.maxHeight = Math.max(height || 0, ...this.hosts.map(host => (!(host.unreachable || host.timedOut || host.outOfSync) ? host.latestHeight || 0 : 0))); const rtt = result.config['meta'].rtt; host.rtts.unshift(rtt); host.rtts.slice(0, 5); host.rtt = host.rtts.reduce((acc, l) => acc + l, 0) / host.rtts.length; host.latestHeight = height; - if (height == null || isNaN(height) || (this.maxHeight - height > 2)) { + if (height == null || isNaN(height) || (this.maxHeight - height > this.maxSlippage)) { host.outOfSync = true; } else { host.outOfSync = false; diff --git a/backend/src/config.ts b/backend/src/config.ts index 2c479c098..33a43fd91 100644 --- a/backend/src/config.ts +++ b/backend/src/config.ts @@ -51,6 +51,7 @@ interface IConfig { REQUEST_TIMEOUT: number; FALLBACK_TIMEOUT: number; FALLBACK: string[]; + MAX_BEHIND_TIP: number; }; LIGHTNING: { ENABLED: boolean; @@ -208,6 +209,7 @@ const defaults: IConfig = { 'REQUEST_TIMEOUT': 10000, 'FALLBACK_TIMEOUT': 5000, 'FALLBACK': [], + 'MAX_BEHIND_TIP': 2, }, 'ELECTRUM': { 'HOST': '127.0.0.1', diff --git a/docker/backend/mempool-config.json b/docker/backend/mempool-config.json index 24ecf60c7..8116e4d3b 100644 --- a/docker/backend/mempool-config.json +++ b/docker/backend/mempool-config.json @@ -60,7 +60,8 @@ "RETRY_UNIX_SOCKET_AFTER": __ESPLORA_RETRY_UNIX_SOCKET_AFTER__, "REQUEST_TIMEOUT": __ESPLORA_REQUEST_TIMEOUT__, "FALLBACK_TIMEOUT": __ESPLORA_FALLBACK_TIMEOUT__, - "FALLBACK": __ESPLORA_FALLBACK__ + "FALLBACK": __ESPLORA_FALLBACK__, + "MAX_BEHIND_TIP": __ESPLORA_MAX_BEHIND_TIP__ }, "SECOND_CORE_RPC": { "HOST": "__SECOND_CORE_RPC_HOST__", diff --git a/docker/backend/start.sh b/docker/backend/start.sh index 4108e0534..0ace54672 100755 --- a/docker/backend/start.sh +++ b/docker/backend/start.sh @@ -62,6 +62,7 @@ __ESPLORA_RETRY_UNIX_SOCKET_AFTER__=${ESPLORA_RETRY_UNIX_SOCKET_AFTER:=30000} __ESPLORA_REQUEST_TIMEOUT__=${ESPLORA_REQUEST_TIMEOUT:=5000} __ESPLORA_FALLBACK_TIMEOUT__=${ESPLORA_FALLBACK_TIMEOUT:=5000} __ESPLORA_FALLBACK__=${ESPLORA_FALLBACK:=[]} +__ESPLORA_MAX_BEHIND_TIP__=${ESPLORA_MAX_BEHIND_TIP:=[]} # SECOND_CORE_RPC __SECOND_CORE_RPC_HOST__=${SECOND_CORE_RPC_HOST:=127.0.0.1} @@ -214,6 +215,7 @@ sed -i "s!__ESPLORA_RETRY_UNIX_SOCKET_AFTER__!${__ESPLORA_RETRY_UNIX_SOCKET_AFTE sed -i "s!__ESPLORA_REQUEST_TIMEOUT__!${__ESPLORA_REQUEST_TIMEOUT__}!g" mempool-config.json sed -i "s!__ESPLORA_FALLBACK_TIMEOUT__!${__ESPLORA_FALLBACK_TIMEOUT__}!g" mempool-config.json sed -i "s!__ESPLORA_FALLBACK__!${__ESPLORA_FALLBACK__}!g" mempool-config.json +sed -i "s!__ESPLORA_MAX_BEHIND_TIP__!${__ESPLORA_MAX_BEHIND_TIP__}!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