From 489320ceed8d58afb0bb946ea822bcedb5591d27 Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Wed, 25 Nov 2026 14:22:07 +0100 Subject: [PATCH] Accept LSPS2 JIT channels before LSP discovery We only allowed underpaying HTLCs on an inbound channel if protocol discovery had found the counterparty to support LSPS2. Discovery results are kept in memory, so after a restart during which the LSP could not be reached, a JIT channel opened for an earlier invoice was accepted without the override. The fee-skimmed payment was then rejected, and as the override cannot be set later, the invoice could never be paid. We now set the override for any configured LSP, as we already do when deciding whether to trust it for 0conf. The skimmed fee is still checked against the limit from the payment metadata before claiming. Co-Authored-By: HAL 9000 --- src/event.rs | 5 ++- tests/integration_tests_rust.rs | 71 ++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) diff --git a/src/event.rs b/src/event.rs index ed6a8e738f..923d860153 100644 --- a/src/event.rs +++ b/src/event.rs @@ -2097,7 +2097,10 @@ where let mut channel_override_config = None; - if self.liquidity_source.get_lsp_config(&counterparty_node_id, 2).await.is_some() { + // This must not depend on protocol discovery having succeeded: an LSP may open a + // channel for a JIT invoice it issued before we last restarted, and the override + // cannot be set once the channel is accepted. + if self.liquidity_source.is_lsps_node(&counterparty_node_id) { // When we're an LSPS2 client, allow claiming underpaying HTLCs as the LSP will skim off some fee. We'll // check that they don't take too much before claiming. channel_override_config = Some(ChannelConfigOverrides { diff --git a/tests/integration_tests_rust.rs b/tests/integration_tests_rust.rs index 1e43a25080..2451816650 100644 --- a/tests/integration_tests_rust.rs +++ b/tests/integration_tests_rust.rs @@ -8685,6 +8685,21 @@ async fn setup_lsps2_nodes( bitcoind: &BitcoinD, electrsd: &ElectrsD, service_store: S, service_funds_sat: u64, client_trusts_0conf: bool, ) -> (TestNode, TestNode, TestNode) { + let (service_node, client_node, payer_node, _) = setup_lsps2_nodes_with_client_config( + bitcoind, + electrsd, + service_store, + service_funds_sat, + client_trusts_0conf, + ) + .await; + (service_node, client_node, payer_node) +} + +async fn setup_lsps2_nodes_with_client_config( + bitcoind: &BitcoinD, electrsd: &ElectrsD, service_store: S, service_funds_sat: u64, + client_trusts_0conf: bool, +) -> (TestNode, TestNode, TestNode, TestConfig) { let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap()); let mut sync_config = EsploraSyncConfig::default(); @@ -8757,7 +8772,7 @@ async fn setup_lsps2_nodes( expect_channel_ready_event!(payer_node, service_node.node_id()); expect_channel_ready_event!(service_node, payer_node.node_id()); - (service_node, client_node, payer_node) + (service_node, client_node, payer_node, client_config) } #[tokio::test(flavor = "multi_thread", worker_threads = 1)] @@ -8900,6 +8915,60 @@ async fn lsps2_service_fails_payment_if_client_is_disconnected() { expect_payment_received_event!(client_node, jit_amount_msat * 99 / 100); } +#[tokio::test(flavor = "multi_thread", worker_threads = 1)] +async fn lsps2_client_receives_jit_payment_before_discovering_lsp() { + let (bitcoind, electrsd) = setup_bitcoind_and_electrsd(); + let service_store = TestSyncStore::new(random_config().node_config.storage_dir_path.into()); + let (service_node, client_node, payer_node, client_config) = + setup_lsps2_nodes_with_client_config(&bitcoind, &electrsd, service_store, 10_000_000, true) + .await; + let service_node_id = service_node.node_id(); + let service_addr = service_node.listening_addresses().unwrap().first().unwrap().clone(); + let client_node_id = client_node.node_id(); + let client_addr = client_node.listening_addresses().unwrap().first().unwrap().clone(); + + let invoice_description = + Bolt11InvoiceDescription::Direct(Description::new(String::from("asdf")).unwrap()); + let jit_amount_msat = 100_000_000; + let jit_invoice = client_node + .bolt11_payment() + .receive_via_jit_channel(jit_amount_msat, &invoice_description.into(), 1024) + .unwrap(); + + // Restart the client while the service is down, so that it cannot learn which protocols its + // LSP supports. + client_node.stop().unwrap(); + drop(client_node); + service_node.stop().unwrap(); + + let esplora_url = format!("http://{}", electrsd.esplora_url.as_ref().unwrap()); + let mut sync_config = EsploraSyncConfig::default(); + sync_config.background_sync_config = None; + setup_builder!(client_builder, client_config.node_config); + client_builder.set_chain_source_esplora(esplora_url, Some(sync_config)); + client_builder.add_liquidity_source(service_node_id, service_addr.clone(), None, true); + let client_node = client_builder.build(client_config.node_entropy.into()).unwrap(); + client_node.start().unwrap(); + + // Protocol discovery is retried with a growing delay, first after 5 and then after another + // 10 seconds. Bring the service back right after the first retry failed, so that the payment + // below reaches the client before the second one. + tokio::time::sleep(Duration::from_secs(6)).await; + service_node.start().unwrap(); + service_node.connect(client_node_id, client_addr, false).unwrap(); + payer_node.connect(service_node_id, service_addr, false).unwrap(); + wait_for_channel_ready_to_send(&payer_node, &service_node, jit_amount_msat).await; + + let payer_payment_id = payer_node.bolt11_payment().send(&jit_invoice, None).unwrap(); + expect_channel_pending_event!(service_node, client_node.node_id()); + expect_channel_ready_event!(service_node, client_node.node_id()); + expect_event!(service_node, PaymentForwarded); + expect_channel_pending_event!(client_node, service_node.node_id()); + expect_channel_ready_event!(client_node, service_node.node_id()); + expect_payment_successful_event!(payer_node, payer_payment_id, None); + expect_payment_received_event!(client_node, jit_amount_msat * 99 / 100); +} + #[tokio::test(flavor = "multi_thread", worker_threads = 1)] async fn lsps2_service_fails_payment_if_channel_closes_before_ready() { let (bitcoind, electrsd) = setup_bitcoind_and_electrsd();