diff options
Diffstat (limited to 'names/withdraw.js')
| -rw-r--r-- | names/withdraw.js | 38 |
1 files changed, 31 insertions, 7 deletions
diff --git a/names/withdraw.js b/names/withdraw.js index 255c66a..b3c9bb8 100644 --- a/names/withdraw.js +++ b/names/withdraw.js @@ -29,11 +29,18 @@ const MAX_TOTAL_SAT = 2_000_000; export const feeCapSat = (sat) => Math.max(10, Math.ceil(sat * 0.01)); const msatOf = (v) => Number(v?.msat ?? v ?? 0); -export function withdrawService({ state, persist, log = () => {}, lnFor, ourNodeId, payCircular, circularPick, canCircular = () => true, publicBase, now = () => Date.now() }) { +// A claim the network can't route right now is tried again before it's +// given back: on 2026-11-14 a 1M-sat claim found this node's own channels +// empty for a moment ("could not find a usable set of paths") and failed in +// a tenth of a second, though a route was there minutes later. +export const RETRY_DELAYS = [30_000, 90_000, 180_000, 300_000]; + +export function withdrawService({ state, persist, log = () => {}, lnFor, ourNodeId, payCircular, circularPick, canCircular = () => true, publicBase, now = () => Date.now(), retryDelays = RETRY_DELAYS, wait = (ms) => new Promise((r) => setTimeout(r, ms)) }) { const links = () => (state.withdraws ||= {}); const funding = () => (state.withdrawFunding ||= {}); // fund payment hash -> id const paidHashes = () => (state.withdrawPaid ||= {}); // claim payment hash -> id (never pay one twice) const busy = new Set(); // ids with a claim being validated/started right now + const active = new Set(); // claim hashes this process is still paying (settlePending leaves them alone) const remaining = (w) => w.totalSat - w.spentSat; const perClaimMax = (w) => Math.min(w.sat, remaining(w)); @@ -41,7 +48,7 @@ export function withdrawService({ state, persist, log = () => {}, lnFor, ourNode id: w.id, url: `${publicBase}/w/${w.id}`, net: w.net, sat: w.sat, uses: w.uses, used: w.used, totalSat: w.totalSat, spentSat: w.spentSat, remainingSat: remaining(w), note: w.note || '', status: w.status, created: w.created, - claims: (w.claims || []).map((c) => ({ sat: c.sat, status: c.status, at: c.at, reclaim: !!c.reclaim })), + claims: (w.claims || []).map((c) => ({ sat: c.sat, status: c.status, at: c.at, reclaim: !!c.reclaim, ...(c.tries > 1 ? { tries: c.tries } : {}) })), }); function sweep() { @@ -164,14 +171,31 @@ export function withdrawService({ state, persist, log = () => {}, lnFor, ourNode if (r.status !== 'complete') throw new Error('payment ' + r.status); return Math.ceil(feeMsat / 1000); })() : (async () => { - const r = await ln.call('pay', { bolt11: pr, maxfee: `${feeCapSat(sat)}sat`, retry_for: 60 }); - if (r.status && r.status !== 'complete') throw new Error(`payment ${r.status}`); - return Math.ceil((msatOf(r.amount_sent_msat) - msatOf(r.amount_msat)) / 1000); + const expiresAt = dec.created_at && dec.expiry ? (dec.created_at + dec.expiry) * 1000 : Infinity; + for (let i = 0; ; i++) { + claim.tries = i + 1; + try { + const r = await ln.call('pay', { bolt11: pr, maxfee: `${feeCapSat(sat)}sat`, retry_for: 60 }); + if (r.status && r.status !== 'complete') throw new Error(`payment ${r.status}`); + return Math.ceil((msatOf(r.amount_sent_msat) - msatOf(r.amount_msat)) / 1000); + } catch (e) { + // a part may still be out there: never start another attempt over it + const st = await outcome(ln, hash).catch(() => 'unknown'); + if (st !== 'failed' && st !== 'none') throw e; + const delay = reclaim ? undefined : retryDelays[i]; + if (delay == null || now() + delay + 60_000 > expiresAt) throw e; + claim.error = String(e.message || e).slice(0, 200); + persist(); + log(`withdraw link ${w.id}: ${sat} sat payout attempt ${i + 1} failed (${claim.error.slice(0, 120)}), trying again in ${Math.round(delay / 1000)} s`); + await wait(delay); + } + } })(); // the answer to the claimer does not wait for the route (LUD-03: OK, then // pay); `settled` is true once paid, false once definitely not, null if unknown - const settled = run.then((fee) => { done(fee); return true; }, async (e) => { + active.add(hash); + const settled = run.finally(() => active.delete(hash)).then((fee) => { done(fee); return true; }, async (e) => { if (e.definite) { fail(e.message); return false; } // did it really fail, or is it still out there? const st = await outcome(ln, hash).catch(() => 'unknown'); @@ -244,7 +268,7 @@ export function withdrawService({ state, persist, log = () => {}, lnFor, ourNode async function settlePending() { for (const w of Object.values(links())) { for (const c of w.claims || []) { - if (c.status !== 'paying' || now() - c.at < 120_000) continue; + if (c.status !== 'paying' || active.has(c.hash) || now() - c.at < 120_000) continue; const st = await outcome(lnFor(w.net), c.hash).catch(() => 'unknown'); if (st === 'complete') { c.status = 'paid'; persist(); log(`withdraw link ${w.id}: ${c.sat} sat payout settled late`); } else if (st === 'failed' || st === 'none') { |
