Skip to content

lsps-plugin: keep the client's zero-conf approval per buy until its channel is open - #9716

Open
nepet wants to merge 2 commits into
ElementsProject:masterfrom
nepet:lsps2-client-approval-per-buy
Open

nepet wants to merge 2 commits into
ElementsProject:masterfrom
nepet:lsps2-client-approval-per-buy

Conversation

@nepet

@nepet nepet commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Checklist

Before submitting the PR, ensure the following tasks are completed. If an item is not applicable to your PR, please mark it as checked:

  • The changelog has been updated in the relevant commit(s) according to the guidelines.
  • Tests have been added or modified to reflect the changes.
  • Documentation has been reviewed and updated as needed.
  • Related issues have been listed and linked, including any that this PR closes.
  • Important All PRs must consider how to reverse any persistent changes for tools/lightning-downgrade

Fixes #9715.

The LSPS2 client accepted a zero-conf channel from an LSP with a single datastore record per LSP, and deleted it in the openchannel hook. So if the open failed after the hook, e.g. because we disconnected before sending funding_signed, the LSP's open for the payer's retry was refused. And two outstanding JIT invoices from the same LSP shared the record, so the second invoice's channel was refused.

This keeps an approval per buy, lsps/client/<lsp_id>/<jit_channel_scid>, until the buy's invoice expires:

  • the openchannel hook allows a zero-conf channel while the LSP has an approval left, but doesn't spend it: open_channel doesn't say which buy it is for
  • an approval is spent once lightningd has saved the channel (channel_opened, which comes before funding_signed), so the LSP still gets one channel per buy. A dual-funded channel doesn't spend one: the hook only sees v1 opens, so it never made that channel zero-conf
  • if that channel is still waiting for channel_ready when the LSP reconnects (peer_connected), the LSP never got our funding_signed, and the approval is given back for its retry
  • if that channel is gone, the approval is given back without waiting for a reconnect: lightningd forgets a channel the LSP opened when the LSP fails it before it got going. The openchannel hook gives those back when all of the LSP's approvals are spent, so the retry gets one
  • once the channel got going, the spent approval is deleted, when the buy's invoice is paid or on reconnect, so the client still leaves nothing behind after a buy
  • expired approvals, and the record older versions kept, are deleted when the LSP's approvals are next read

The first commit lets the mock tests pay an invoice they already have. The second adds three tests against the mock LSP: the retry after a disconnect before funding_signed, two invoices from the same LSP, paid in either order, and an LSP that opens a second channel for a buy (refused before and after, the test checks it is the zero-conf refusal).

Limits: an LSP that disconnects before sending channel_ready gets the approval back on reconnect, at the cost of a funding output it never broadcasts. Any v1 channel the LSP opens while a buy is outstanding spends an approval, as the hook makes it zero-conf, and a channel we close ourselves before it got going counts as got going; both only err towards refusing. The openchannel hook and the channel_opened notification run as separate tasks. lightningd refuses a second open while one is in progress, so the LSP's next open can only start once the first channel is saved. An LSP that sends it right away may still have its hook run before the notification spends the approval, and get a second zero-conf channel for the buy. lsps-lsps2-approve now looks up the buy's invoice by payment_hash to take its expiry, so it fails if no such invoice exists. lsps-lsps2-invoice calls it after creating the invoice.

Changelog-Experimental: lsps-plugin: the LSPS2 client accepts the JIT channel for a payment retried after a failed open, and for each of several JIT invoices from the same LSP.

@Andezion Andezion left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In on_channel_opened, why spend the approval that expires first, and not keep a small "pending opens per peer" set filled by the openchannel hook? also on_lsps_lsps2_approve, on_channel_opened and on_peer_connected each rebuild rpc_path and call ClnRpc::new. maybe we could add a small State::rpc() helper, it could remove about 5 copies

Comment thread plugins/lsps-plugin/src/client.rs Resolved
Comment thread plugins/lsps-plugin/src/client.rs Resolved
Comment thread plugins/lsps-plugin/src/client.rs Resolved
Comment thread plugins/lsps-plugin/src/client.rs Resolved
Comment thread plugins/lsps-plugin/src/client.rs Resolved
@nepet
nepet force-pushed the lsps2-client-approval-per-buy branch from a697d20 to ead247a Compare November 12, 2026 14:53
@nepet

nepet commented Nov 12, 2026

Copy link
Copy Markdown
Member Author

@Andezion Thanks for your review!

On which approval to spend: open_channel doesn't say which buy it's for, so any approval does for the count. Spending the one that expires first means the approvals left always last at least as long as the invoices still outstanding. If buy B's channel comes first and spends A's approval, A is left with B's, which expires later, never the other way round.

A pending-opens set filled in the openchannel hook would close the hook/notification race. But something has to clear an entry when the open fails after the hook and before lightningd saves the channel, which is the case this PR fixes. Nothing notifies us of that failure. We'd have to clear on disconnect or reconnect and rely on every failure path disconnecting. Otherwise the payer's retry is refused again. So I took your other suggestion instead: on_channel_opened now takes the lock first. Its task is spawned a full round trip (our funding_signed, then the LSP's next open_channel) before the next hook's task, and tokio's Mutex is FIFO. So the notification gets the lock first unless it isn't polled for that whole time. I'd rather keep that documented window than add state that can get stuck.

State::rpc(): agreed, but most of the copies predate this PR (getinfo, buy, approve, invoice, htlc_accepted, invoice_payment, listprotocols). This PR adds two. I'll do it in a small follow-up so this one stays focused.

@nepet
nepet requested a review from Andezion November 12, 2026 15:05
@nepet
nepet force-pushed the lsps2-client-approval-per-buy branch from ead247a to 117f23c Compare November 13, 2026 11:44
Andezion
Andezion previously approved these changes Nov 13, 2026
Comment thread plugins/lsps-plugin/src/client.rs Resolved
Comment thread plugins/lsps-plugin/src/client.rs Resolved
Split lsps2_mock_pay() out of lsps2_mock_pay_var_invoice(), so that a
test can pay the same invoice twice, or several invoices.

Changelog-None
Comment thread plugins/lsps-plugin/src/client.rs Resolved
Comment thread plugins/lsps-plugin/src/client.rs Resolved
Comment thread plugins/lsps-plugin/src/client.rs Resolved
…hannel is open

The LSPS2 client approved zero-conf channels from an LSP with a single
datastore record, lsps/client/<lsp_id>, and deleted it in the
openchannel hook. So:

- if the open failed after the hook, e.g. because we disconnected
  before sending funding_signed, the LSP's open for the payer's retry
  was refused ("You required zeroconf, but you're not on our
  allowlist"), and the buy could not be paid;
- two outstanding JIT invoices from the same LSP shared the record, so
  the channel for the second one was refused.

Keep an approval per buy instead, lsps/client/<lsp_id>/<jit_channel_scid>,
until the buy's invoice expires. The openchannel hook allows a zero-conf
channel from the LSP while one of its approvals is left, but no longer
spends it; open_channel doesn't say which buy the channel is for anyway.

An approval is spent once lightningd has saved the channel: it sends
channel_opened before funding_signed, so the LSP still gets one channel
per buy. A dual-funded channel from the LSP doesn't spend one: the
openchannel hook only sees v1 opens, so it never made that channel
zero-conf. If a spent approval's channel is still waiting for
channel_ready when the LSP reconnects, the LSP never got our
funding_signed, and the approval is given back for its retry. Once the
channel got going, the spent approval is deleted: when the buy's invoice
is paid, or on reconnect. An approval given back remembers the channel
it came from, and is deleted the same way if that channel gets going
after all.

If the channel is gone, the approval is given back without waiting for
a reconnect: lightningd forgets a channel the LSP opened when the LSP
fails it before it got going. The openchannel hook gives those back
when all of the LSP's approvals are spent, so the LSP's retry gets one.
A channel we close ourselves before it got going counts as got going,
which errs towards refusing the LSP's next channel for the buy.

Expired approvals, and the record older versions kept, are deleted when
the LSP's approvals are next read.

Changelog-Experimental: lsps-plugin: the LSPS2 client accepts the JIT channel for a payment retried after a failed open, and for each of several JIT invoices from the same LSP.
@nepet
nepet force-pushed the lsps2-client-approval-per-buy branch from 25ae864 to f6610b8 Compare November 17, 2026 16:58
@nepet
nepet requested a review from Andezion November 17, 2026 16:59

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LSPS2 client: a failed JIT channel open spends the zero-conf approval, so the retry is refused

2 participants