aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-11-26 07:17:40 -0700
committerAdam Soltys <[email protected]>2026-11-26 07:17:40 -0700
commitdcda8c643da72ebbecd590ae9e065d1641c96976 (patch)
treecab860fc59da76b1099475cfb5be904834830557
parentf891de2e288d4af734f7ef20b81e6f96705a14bd (diff)
downloadcoinos-server-dcda8c643da72ebbecd590ae9e065d1641c96976.tar.gz
coinos-server-dcda8c643da72ebbecd590ae9e065d1641c96976.zip
meltguard: report cap-refused pays as failed so melts don't stick pending
A pay refused for the per-melt or rolling cap never reaches cl, so listpays returns nothing for its hash. nutshell 0.22 treats that as NOT_FOUND and keeps the melt quote PENDING with the proofs locked indefinitely. Record cap-refused hashes in GUARD_DIR/rejected and answer a listpays for one of them with a failed pay when cl has none, so the mint marks the quote unpaid and releases the proofs. cl's own answer still wins when it has a pay for the hash. Repeat-hash and partial refusals are not recorded. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6W9CsdxmhFrfaKNVHU
-rw-r--r--meltguard.ts35
1 files changed, 33 insertions, 2 deletions
diff --git a/meltguard.ts b/meltguard.ts
index a50b3d87..33b579bf 100644
--- a/meltguard.ts
+++ b/meltguard.ts
@@ -29,6 +29,15 @@
// over it is REJECTED rather than freezing the guard (for the live mint, where
// normal redemptions would otherwise trip a lifetime cap).
//
+// A pay the guard refuses for an amount cap (per-melt or rolling window) never
+// reaches cl, so cl's listpays has nothing for that hash. nutshell >= 0.22 reads
+// an empty listpays as NOT_FOUND and keeps the melt quote PENDING forever, which
+// locks the user's proofs. Such hashes are recorded in GUARD_DIR/rejected, and a
+// listpays for one of them that cl answers with no pays is reported as a failed
+// pay, so the mint marks the quote UNPAID and releases the proofs. When cl has a
+// pay for the hash, cl's answer is passed through unchanged. Repeat-hash and
+// partial-pay refusals are not recorded: another quote on that hash may be real.
+//
// Env: CL_URL (http://cl:3010), MELT_MAX_SAT, MELT_CUM_MAX_SAT, MELT_WINDOW_SEC,
// GUARD_DIR (/guard), PORT (3011). Run: GUARD_DIR=./guard bun meltguard.ts
import { readFileSync, writeFileSync, existsSync, appendFileSync, mkdirSync } from "fs";
@@ -47,6 +56,10 @@ const WINDOW_FILE = `${DIR}/window.json`;
const WINDOW = Number(process.env.MELT_WINDOW_SEC || 0) * 1000;
const seen = new Set(existsSync(HASH_FILE) ? readFileSync(HASH_FILE, "utf8").split("\n").filter(Boolean) : []);
const remember = (h: string) => { seen.add(h); appendFileSync(HASH_FILE, h + "\n"); };
+const REJECTED_FILE = `${DIR}/rejected`;
+const loadRejected = () => new Set(existsSync(REJECTED_FILE) ? readFileSync(REJECTED_FILE, "utf8").split("\n").map((x) => x.trim()).filter(Boolean) : []);
+let rejected = loadRejected();
+const markRejected = (h: string) => { if (rejected.has(h)) return; rejected.add(h); appendFileSync(REJECTED_FILE, h + "\n"); };
type Spend = { ts: number; amt: number };
let spends: Spend[] = existsSync(WINDOW_FILE) ? JSON.parse(readFileSync(WINDOW_FILE, "utf8") || "[]") : [];
const windowSum = () => { const cut = Date.now() - WINDOW; spends = spends.filter((x) => x.ts >= cut); return spends.reduce((a, x) => a + x.amt, 0); };
@@ -111,10 +124,10 @@ Bun.serve({
const amt = inv.sat;
if (extra.partial_msat != null || extra.amount_msat != null) { log(`REJECT partial pay ${extra.partial_msat ?? extra.amount_msat} msat of ${amt} sat hash=${inv.hash}`); return err(403, "meltguard: partial payments are not allowed"); }
if (seen.has(inv.hash)) { log(`REJECT repeat pay hash=${inv.hash} ${amt} sat`); return err(403, "meltguard: this invoice has already been attempted"); }
- if (amt > PER) { log(`REJECT per-melt ${amt} > ${PER}`); return err(403, `meltguard: per-melt cap ${PER} sat exceeded (${amt})`); }
+ if (amt > PER) { markRejected(inv.hash); log(`REJECT per-melt ${amt} > ${PER}`); return err(403, `meltguard: per-melt cap ${PER} sat exceeded (${amt})`); }
const cum = WINDOW ? windowSum() : readCum();
if (cum + amt > CUM) {
- if (WINDOW) { log(`REJECT window ${cum}+${amt} > ${CUM}`); return err(403, `meltguard: rolling cap ${CUM} sat reached, try later`); }
+ if (WINDOW) { markRejected(inv.hash); log(`REJECT window ${cum}+${amt} > ${CUM}`); return err(403, `meltguard: rolling cap ${CUM} sat reached, try later`); }
freeze(`cumulative ${cum}+${amt} > ${CUM}`); return err(403, `meltguard: cumulative cap ${CUM} sat — FROZEN, operator review required`);
}
// Claim the hash and the window budget before forwarding, so concurrent
@@ -136,6 +149,24 @@ Bun.serve({
// transparent proxy for all other CLNRest calls
const body = req.method === "GET" || req.method === "HEAD" ? undefined : await req.arrayBuffer();
const resp = await fetch(`${CL}${url.pathname}${url.search}`, { method: req.method, headers: { ...(req.headers.get("content-type") ? { "content-type": req.headers.get("content-type")! } : {}), ...(rune ? { Rune: rune } : {}) }, body });
+ if (req.method === "POST" && url.pathname === "/v1/listpays" && body && resp.ok) {
+ const raw = new TextDecoder().decode(body);
+ let hash = "";
+ try { hash = new URLSearchParams(raw).get("payment_hash") || ""; } catch {}
+ if (!hash) { try { hash = JSON.parse(raw).payment_hash || ""; } catch {} }
+ // re-read so an operator can add a hash without a restart
+ if (hash) rejected = loadRejected();
+ if (hash && rejected.has(hash)) {
+ const text = await resp.text();
+ let j: any = null;
+ try { j = JSON.parse(text); } catch {}
+ if (j && Array.isArray(j.pays) && j.pays.length === 0) {
+ log(`listpays ${hash}: refused by guard, cl has no pay -> reporting failed`);
+ return new Response(JSON.stringify({ pays: [{ payment_hash: hash, status: "failed" }] }), { status: 200, headers: { "content-type": "application/json" } });
+ }
+ return new Response(text, { status: resp.status, headers: { "content-type": resp.headers.get("content-type") || "application/json" } });
+ }
+ }
return new Response(await resp.arrayBuffer(), { status: resp.status, headers: { "content-type": resp.headers.get("content-type") || "application/json" } });
},
});