aboutsummaryrefslogtreecommitdiffstats
path: root/src/nwc-respond.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-11-10 07:35:30 -0700
committerAdam Soltys <[email protected]>2026-11-10 07:35:34 -0700
commit520aa7a1e39dbe9d8e0fd44b5cc3ce06e79f5abf (patch)
tree46d1fcb8c9c13cf62cf3bb7decd35906e3e2749e /src/nwc-respond.js
parentf6970a3be5eb1019675debe47b0eedd77892a933 (diff)
downloadcoinosv3-520aa7a1e39dbe9d8e0fd44b5cc3ce06e79f5abf.tar.gz
coinosv3-520aa7a1e39dbe9d8e0fd44b5cc3ce06e79f5abf.zip
A zap answered over NWC waits on far fewer round trips, and a phone with Coinos behind another app answers from its worker. "I'm on V3 … I find nwc zaps a lot more slower than usual" (398ja, 2026-11-10). Measured from the notifier's wakes and the ASP's log over four days: a closed app took about six seconds from the push to its cosign (the Lightning part after that is one and a half), an Android phone fifteen at the median, up to thirty. Where it went: every call to the ASP and the notifier was two round trips (no Access-Control-Max-Age, so Chrome re-asked the CORS preflight after five seconds; a Cloudflare response-header rule now sets 7200 on OPTIONS for ark.coinos.io, ark-staging and nwcpush), and the worker made them one after another: answered?, GetArkInfo, Handshake, claim, a save, the chain tip, then the payment, then status ("pending"), the tip again, the held status call, and status once more for the preimage it had just been handed. Now: what the mirror alone decides (invoice, network, budgets, coins, keys) comes first with no network; the claim (which now also says "already answered", coinosv3-server 3c5e9c3) goes out beside the manager's start, whose info call, handshake and tip run together (init, warmTip/_tipFresh); a send just initiated returns to its caller instead of asking at once, and awaitLnPay settles from the held call's own answer. A worker-style send on regtest: eight calls, six in sequence, where there were thirteen. The app's own answer no longer waits a second on a phone that is on screen (only one behind another app holds back for a computer).
Android freezes an app behind another one, and the worker's nudge to it sat unread until it was opened: on a phone the worker now hands a request only to a window on screen (as it already did on iOS), nudges a hidden one in case it is awake, and answers itself; the request's lock decides between them. For that the mirror is no longer written over the worker's hand: the worker stamps its writes (workerAt), the app writes under the worker's own lock (nwc-bg-work) and folds the worker's state in first (never mid-payment), carries the stamp, absorbs it when it comes back on screen (arkBgAbsorb), and refreshes the mirror as it goes behind another app. Tests: ln-pay-double-wake (the one held call settles; a failed or mismatched answer doesn't), sw-push-quota (an Android phone's hidden window), nwc-sw-test's notifier stand-in answers /claim as the real one; tools/ark/ln-e2e-test's pay half against regtest captaind 0.8 + CLN Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'src/nwc-respond.js')
-rw-r--r--src/nwc-respond.js78
1 files changed, 58 insertions, 20 deletions
diff --git a/src/nwc-respond.js b/src/nwc-respond.js
index 98fd19c..311b928 100644
--- a/src/nwc-respond.js
+++ b/src/nwc-respond.js
@@ -85,15 +85,21 @@ export async function bgManager(rec, walletKey, saveFn, log = null) {
log(`ln-pay ${String(a.paymentHash || '').slice(0, 8)} → ${a.step}${a.error ? ': ' + a.error : ''}`);
}
}
+ // the worker's hand on the mirror, for an app alive behind it: it
+ // folds this state in before it next writes its own over it
+ rec.workerAt = Date.now();
Promise.resolve(saveFn(walletKey, rec)).catch(() => {});
},
};
- return new ArkManager({
+ const m = new ArkManager({
account: shimAccount(rec), storage,
arkUrl: rec.ark.arkUrl, esploraUrl: rec.ark.esploraUrl, network: rec.ark.network,
honorsExpired: !!rec.ark.honorsExpired,
lnOldestFirst: () => !!rec.custody && rec.custody !== 'custodial',
- }).init();
+ });
+ // the chain tip a payment needs, asked for while the manager starts
+ try { m.warmTip().catch(() => {}); } catch { /* asked again when needed */ }
+ return m.init();
}
// ---- sends in flight across wakes ------------------------------------------
@@ -300,6 +306,8 @@ export async function respondFromBg(data, {
// ...and hands each pay request to the first device that claims it, so
// two awake devices don't each lock an HTLC for one zap. Unreachable →
// proceed: a lone worker must not go silent over a notifier hiccup.
+ // (the same call says when a reply already exists: one round trip where
+ // "answered?" and "mine?" used to be two, one after the other)
const claim = async () => {
try {
const r = await fetchFn(`${notifier}/claim`, {
@@ -307,9 +315,25 @@ export async function respondFromBg(data, {
body: JSON.stringify({ event: ev.id, by: `sw-${walletKey}`.slice(0, 64) }),
});
if (!r.ok) return true;
- return (await r.json()).ok !== false;
+ const j = await r.json();
+ if (j.answered) return 'answered';
+ return j.ok !== false;
} catch { return true; }
};
+ // a claim this worker can't make good on (the wallet's server unreachable)
+ // is handed back, so the app the user opens isn't told to stand down
+ const unclaim = () => {
+ try {
+ Promise.resolve(fetchFn(`${notifier}/claim`, {
+ method: 'POST', headers: { 'content-type': 'application/json' },
+ body: JSON.stringify({ event: ev.id, by: `sw-${walletKey}`.slice(0, 64), release: true }),
+ })).catch(() => {});
+ } catch { /* it lapses by itself */ }
+ };
+ const lostClaim = (c) => {
+ log(c === 'answered' ? 'already answered elsewhere' : 'another device claimed this request — standing down');
+ return true;
+ };
if (method === 'get_info') {
await publish({
@@ -433,12 +457,21 @@ export async function respondFromBg(data, {
}
// ---- pay_invoice ------------------------------------------------------
- if (await answered()) { log('already answered elsewhere'); return true; }
-
+ // What the mirror alone can decide comes first, with no network at all;
+ // then the claim (which also says if someone already answered) goes out
+ // beside the manager's start. Each of these used to wait for the one
+ // before it: a zap answered from a closed phone spent about six seconds
+ // getting to the payment (398ja, 2026-11-10: "a lot more slower").
+ // A refusal never goes out over another device's answer.
+ const refuse = async (code, message) => {
+ if (await answered()) { log('already answered elsewhere'); return true; }
+ await publish(errRes('pay_invoice', code, message));
+ return true;
+ };
const invoice = params.invoice;
const dec = invoice && maybeBolt11(invoice);
- if (!dec) { await publish(errRes('pay_invoice', 'OTHER', 'not a bolt11 invoice')); return true; }
- if (!dec.amountSat) { await publish(errRes('pay_invoice', 'OTHER', 'zero-amount invoices are not supported')); return true; }
+ if (!dec) return refuse('OTHER', 'not a bolt11 invoice');
+ if (!dec.amountSat) return refuse('OTHER', 'zero-amount invoices are not supported');
// The bolt11 names its network; only a record on that network can pay it.
// A mismatched copy fails in milliseconds with a nonsense error that beats
// a sibling device's real payment to the client — so stand down instead,
@@ -451,17 +484,11 @@ export async function respondFromBg(data, {
return answered();
}
({ walletKey, rec, conn } = fit);
- if (dec.amountSat > conn.maxSat) {
- await publish(errRes('pay_invoice', 'QUOTA_EXCEEDED', `over the ${conn.maxSat} sat per-payment limit`));
- return true;
- }
+ if (dec.amountSat > conn.maxSat) return refuse('QUOTA_EXCEEDED', `over the ${conn.maxSat} sat per-payment limit`);
// What the app spent this window rides on the mirrored connection; the
// worker's own spends the app has not yet folded in are counted from its log.
const { left } = spentInWindow(conn, rec.spends || []);
- if (dec.amountSat > left) {
- await publish(errRes('pay_invoice', 'QUOTA_EXCEEDED', `over the remaining ${renewalPeriod(conn.period)} budget (${left} sat)`));
- return true;
- }
+ if (dec.amountSat > left) return refuse('QUOTA_EXCEEDED', `over the remaining ${renewalPeriod(conn.period)} budget (${left} sat)`);
// A send this worker began on an earlier wake and never finished (PullCap's
// night of 2026-10-23: three hours of fifteen). Its coin sat reserved in
// the mirror, so a fresh attempt at the SAME invoice was refused as
@@ -511,7 +538,8 @@ export async function respondFromBg(data, {
return sentNotice(rec, a, c, dec.amountSat);
};
if (same) {
- if (!(await claim())) { log('another device claimed this request — standing down'); return true; }
+ const c = await claim();
+ if (c !== true) return lostClaim(c);
log(`a send for this invoice is already ${same.step} — finishing it`);
const wasDone = same.step === 'done';
let a = same;
@@ -530,6 +558,7 @@ export async function respondFromBg(data, {
// Only when they hold the coins this payment needs: otherwise the answer
// comes first and the wake's tending finishes them after.
if (stuck.length && spendableSat(rec) < dec.amountSat) {
+ if (await answered()) { log('already answered elsewhere'); return true; }
try {
await ready();
for (const a of stuck) {
@@ -558,12 +587,21 @@ export async function respondFromBg(data, {
// outputs allocate from nextKeyIndex, which must stay inside the key
// window the app pre-derived; if it ran out, wake the user instead.
if (!rec.keys[String(rec.mgr.nextKeyIndex || 1)]) return declineLater('key window exhausted');
- try { await ready(); } catch (e) { return declineLater('ark init failed: ' + e.message); }
-
- if (!(await claim())) { log('another device claimed this request — standing down'); return true; }
+ // the claim and the manager's start (begun above), side by side
+ const claimP = claim();
+ try { await ready(); } catch (e) {
+ const c = await claimP;
+ if (c !== true) return lostClaim(c);
+ unclaim();
+ return declineLater('ark init failed: ' + e.message);
+ }
+ const mine = await claimP;
+ if (mine !== true) return lostClaim(mine);
// whose send this is, for the wake that finishes it should this one not
notePending(rec, dec.paymentHash, conn);
- await saveFn(walletKey, rec);
+ // (not waited on: the manager's own save of the new send, a moment on,
+ // writes this same record)
+ Promise.resolve(saveFn(walletKey, rec)).catch(() => {});
log(`paying ${dec.amountSat} sat`);
let a = null;
for (let attempt = 0; ; attempt++) {