From 51428790ea558f8e34c3034e75d2e49a076261b8 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sun, 29 Mar 2026 23:40:15 -0700 Subject: nwc heartbeat --- lib/changeid.ts | 13 +++++++------ lib/nwc.ts | 29 +++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/lib/changeid.ts b/lib/changeid.ts index 49d3a0fb..1c6a6688 100644 --- a/lib/changeid.ts +++ b/lib/changeid.ts @@ -80,7 +80,7 @@ export default async function changeid(un: string) { } else if (k.includes("user:")) { await client.del(k); } else { - await client.rename(k, nk); + if (await client.exists(k)) await client.rename(k, nk); } } catch (e: any) { err(`changeid [${label}]`, k, e.message); @@ -100,17 +100,18 @@ export default async function changeid(un: string) { } await db.del(`${id}:apps`); - // Update invoices that reference the old uid - for await (const k of db.scanIterator({ MATCH: "invoice:*" })) { + // Update uid/aid in invoice objects and their offer pointers + const invoiceIds = await db.lRange(`${nid}:invoices`, 0, -1); + for (const iid of invoiceIds) { try { - const inv = await g(k); + const inv = await g(`invoice:${iid}`); if (!inv || typeof inv === "string") continue; let changed = false; if (inv.uid === id) { inv.uid = nid; changed = true; } if (inv.aid === id) { inv.aid = nid; changed = true; } if (changed) { - await s(k, inv); - l("changeid: updated invoice", k); + await s(`invoice:${iid}`, inv); + l("changeid: updated invoice", iid); } } catch (e) {} } diff --git a/lib/nwc.ts b/lib/nwc.ts index 646ed4b0..d09e6e6c 100644 --- a/lib/nwc.ts +++ b/lib/nwc.ts @@ -49,12 +49,30 @@ const nwcRequestTimes: Map = new Map(); export default () => { let r: any; + let heartbeatInterval: any; function connect() { + if (heartbeatInterval) clearInterval(heartbeatInterval); r = new Relay("ws://sf:7777", { reconnect: false }); r.on("open", async (_) => { l("nwc connected to strfry"); + + // Periodically check if connection is alive + heartbeatInterval = setInterval(() => { + try { + if (!r?.ws || r.ws.readyState !== 1) { + warn("nwc heartbeat: connection dead, reconnecting"); + clearInterval(heartbeatInterval); + try { r.close(); } catch (_) {} + setTimeout(connect, 1000); + } + } catch (_) { + warn("nwc heartbeat: error, reconnecting"); + clearInterval(heartbeatInterval); + setTimeout(connect, 1000); + } + }, 30000); r.subscribe("nwc", { kinds: [23194], "#p": [serverPubkey, serverPubkey2], since: Math.floor(Date.now() / 1000) - nwcEventMaxAgeSeconds }); const info = await finalizeEvent( { @@ -87,6 +105,7 @@ export default () => { r.on("close", () => { warn("nwc strfry connection lost, reconnecting in 5s"); + if (heartbeatInterval) clearInterval(heartbeatInterval); setTimeout(connect, 5000); }); @@ -147,16 +166,10 @@ export default () => { response = await finalizeEvent(response, hexToBytes(sk)); r.send(["EVENT", response]); } catch (e) { - // err( - // "problem with nwc", - // pubkey, - // method, - // JSON.stringify(params), - // e.message, - // ); + err("problem with nwc", pubkey, method, e.message); } } catch (e) { - // err("problem with nwc", e.message); + err("problem with nwc event", e.message); } }); } -- cgit v1.2.3