From 5b554852bbcd9c1a28cd67593894e09fa63ff744 Mon Sep 17 00:00:00 2001 From: Mononaut Date: Thu, 20 Jun 2024 14:29:35 +0000 Subject: [PATCH] Recover from esplora failover after a reorg to lower height --- backend/src/api/bitcoin/esplora-api.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/backend/src/api/bitcoin/esplora-api.ts b/backend/src/api/bitcoin/esplora-api.ts index c8d96a277..a484e689e 100644 --- a/backend/src/api/bitcoin/esplora-api.ts +++ b/backend/src/api/bitcoin/esplora-api.ts @@ -94,12 +94,12 @@ class FailoverRouter { ); if (result) { const height = result.data; - this.maxHeight = Math.max(height || 0, ...this.hosts.map(host => (!(host.unreachable || host.timedOut || host.outOfSync) ? host.latestHeight || 0 : 0))); + host.latestHeight = height; + this.maxHeight = Math.max(height || 0, ...this.hosts.map(h => (!(h.unreachable || h.timedOut || h.outOfSync) ? h.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 > this.maxSlippage)) { host.outOfSync = true; } else { @@ -127,7 +127,6 @@ class FailoverRouter { host.checked = true; host.lastChecked = Date.now(); - // switch if the current host is out of sync or significantly slower than the next best alternative const rankOrder = this.sortHosts(); // switch if the current host is out of sync or significantly slower than the next best alternative if (this.activeHost.outOfSync || this.activeHost.unreachable || (this.activeHost !== rankOrder[0] && rankOrder[0].preferred) || (!this.activeHost.preferred && this.activeHost.rtt > (rankOrder[0].rtt * 2) + 50)) { @@ -185,7 +184,6 @@ class FailoverRouter { // depose the active host and choose the next best replacement private electHost(): void { - this.activeHost.outOfSync = true; this.activeHost.failures = 0; const rankOrder = this.sortHosts(); this.activeHost = rankOrder[0]; @@ -196,6 +194,7 @@ class FailoverRouter { host.failures++; if (host.failures > 5 && this.multihost) { logger.warn(`🚨🚨🚨 Too many esplora failures on ${this.activeHost.host}, falling back to next best alternative 🚨🚨🚨`); + this.activeHost.unreachable = true; this.electHost(); return this.activeHost; } else {