From 193f49a7a4fb73ef7bfa405c17e8da3ab94f4205 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Mon, 31 Aug 2026 09:28:48 -0700 Subject: The chain adapter learns a second road: mainnet esplora calls fail over between the configured host, blockstream.info, and mempool.space (remembering whichever answered last, rotating on network failures, 429s and 5xx) — one tester's network simply cannot reach mempool.space and every chain-touching feature was dying for it, stranded mailbox receives included. And a failed quote no longer wedges the Lightning pay form at an eternal spinner: it prices at the outage fallback and lets Pay proceed, where the payment path quotes again for real MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Fable 5 Claude-Session: https://claude.ai/code/session_01MrkLsNScXe9b6BviUSdmMr --- src/ark/manager.js | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) (limited to 'src/ark/manager.js') diff --git a/src/ark/manager.js b/src/ark/manager.js index b5c2aac..e9f79b0 100644 --- a/src/ark/manager.js +++ b/src/ark/manager.js @@ -190,21 +190,45 @@ export class ArkManager { _decoded(v) { return decodeVtxo(hex.decode(v.bytes)); } // ---- chain adapter (esplora REST, same API the wallet already speaks) ---- + // + // With FAILOVER on mainnet: some networks simply can't reach mempool.space + // (one tester's laptop failed every chain call for days — stranded mailbox + // receives, quotes that never arrived), and a single host made every one + // of those a dead end. A request tries the last host that worked, then the + // rest; rate limits and 5xx rotate too. Non-mainnet keeps its lone host — + // there is no public twin of a regtest or mutinynet esplora. + async _esplora(path, init) { + const hosts = (this._esploraHosts ||= [ + this.esploraUrl, + ...(this.network === 'mainnet' ? ['https://blockstream.info/api', 'https://mempool.space/api'] : []), + ].filter((h, i, a) => h && a.indexOf(h) === i)); + const start = this._esploraIdx || 0; + let lastErr; + for (let i = 0; i < hosts.length; i++) { + const idx = (start + i) % hosts.length; + try { + const r = await fetch(hosts[idx] + path, init); + if (r.status === 429 || r.status >= 500) { lastErr = new Error(`chain status ${r.status}`); continue; } + this._esploraIdx = idx; + return r; + } catch (e) { lastErr = e; } + } + throw lastErr; + } get chain() { - const base = this.esploraUrl; return { // no-store: a CDN-cached tip lags a 30s-block network by many blocks, // and a stale tip under-shoots HTLC expiries the server then rejects - tipHeight: async () => Number(await fetch(`${base}/blocks/tip/height`, { cache: 'no-store' }).then((r) => r.text())), + tipHeight: async () => Number(await this._esplora('/blocks/tip/height', { cache: 'no-store' }).then((r) => r.text())), getTxStatus: async (txid) => { - const r = await fetch(`${base}/tx/${txid}/status`); + const r = await this._esplora(`/tx/${txid}/status`); if (r.ok) return r.json(); if (r.status === 404) return null; // unknown tx is an answer - throw new Error(`chain status ${r.status}`); // rate limits etc must SURFACE + throw new Error(`chain status ${r.status}`); // odd refusals must SURFACE }, - getTxHex: async (txid) => fetch(`${base}/tx/${txid}/hex`).then((r) => r.ok ? r.text() : null), + getTxHex: async (txid) => this._esplora(`/tx/${txid}/hex`).then((r) => r.ok ? r.text() : null), broadcastTx: async (txHex) => { - const r = await fetch(`${base}/tx`, { method: 'POST', body: txHex }); + const r = await this._esplora('/tx', { method: 'POST', body: txHex }); const body = await r.text(); // a rebroadcast after a crash is fine — the tx being known already IS success if (!r.ok && !/already/i.test(body)) throw new Error(`broadcast failed: ${body.slice(0, 120)}`); -- cgit v1.2.3