From da60095c360bf361189c6fe8ad1324f690f46184 Mon Sep 17 00:00:00 2001 From: cdecker Date: Sat, 24 Oct 2026 13:10:53 +0000 Subject: [PATCH 1/3] gl-plugin: Add channel availability rule Introduce `channels::is_channel_available`, a single rule that decides whether a channel can be used to send or receive, advertised as a routehint, or awaited for (re-)establishment: available := state == CHANNELD_NORMAL && (max_to_us_msat > 0 || short_channel_id.is_some()) `max_to_us_msat` is the monotonically increasing high-water mark of the balance we have ever held. For zeroconf JIT channels, which may not have a `short_channel_id` yet, a non-zero gauge signals that the incoming JIT payment that opened the channel was claimed, so the LSP server has no reason to have dropped the channel and awaiting it can succeed. A channel that never held funds and was never confirmed is a dangling channel open (typically an abandoned JIT channel): it will never come back, yet we currently await it until the wait deadline expires and fail otherwise perfectly payable invoices (gl#870, gl#871). The current balance (`spendable_msat`) is deliberately secondary: a drained but confirmed channel stays available. --- libs/gl-plugin/src/channels.rs | 73 ++++++++++++++++++++++++++++++++++ libs/gl-plugin/src/lib.rs | 1 + 2 files changed, 74 insertions(+) create mode 100644 libs/gl-plugin/src/channels.rs diff --git a/libs/gl-plugin/src/channels.rs b/libs/gl-plugin/src/channels.rs new file mode 100644 index 000000000..2ca03c62b --- /dev/null +++ b/libs/gl-plugin/src/channels.rs @@ -0,0 +1,73 @@ +//! A single rule that decides whether a channel is *available*. +//! +//! A channel is available when it can be used to send or receive, can be +//! advertised as a routehint, and is worth awaiting (re-)establishment +//! for. Unavailable channels must be skipped everywhere: awaiting them +//! stalls payments until the wait deadline expires, and advertising them +//! in invoices routes payments into a black hole. +//! +//! The rule is: +//! +//! ```text +//! available := state == CHANNELD_NORMAL +//! && (max_to_us_msat > 0 || short_channel_id.is_some()) +//! ``` +//! +//! `max_to_us_msat` is a monotonically increasing high-water mark of the +//! balance we have ever held on this channel. For JIT channels (zeroconf +//! channels opened by an LSP server to carry an incoming payment, which +//! may not have a `short_channel_id` yet) a non-zero gauge is the signal +//! that the incoming JIT payment that caused the channel to be opened +//! was claimed: the LSP server was paid and has no reason to abandon the +//! channel, so awaiting it can succeed. A channel that has never held +//! funds (`max_to_us_msat == 0`) *and* was never confirmed (no +//! `short_channel_id`) is a dangling channel open, most likely an +//! abandoned JIT channel whose LSP server may have dropped the channel; +//! it will never come back, so we neither await it nor advertise it. +//! +//! The current balance (`spendable_msat`) is deliberately secondary: a +//! drained but confirmed channel remains available (it may be refilled +//! or come back online), while a never-funded zeroconf channel is not, +//! even if it would momentarily show spendable capacity. + +/// Decide whether a channel is available for payments, awaiting and +/// routehints. +/// +/// The arguments are deliberately primitive so the rule can be applied +/// to whatever channel representation a call site has (RPC responses, +/// cached structs, …) without depending on a particular cln-rpc version. +pub fn is_channel_available( + state_is_normal: bool, + has_scid: bool, + max_to_us_msat_msat: u64, +) -> bool { + state_is_normal && (max_to_us_msat_msat > 0 || has_scid) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_availability_truth_table() { + // Confirmed and normal: always available, even never funded + // (e.g. an incoming-only channel) or currently drained. + assert!(is_channel_available(true, true, 0)); + assert!(is_channel_available(true, true, 1)); + + // Zeroconf (no scid) and the JIT payment was claimed: available, + // the LSP server has no reason to have dropped the channel. + assert!(is_channel_available(true, false, 1)); + + // Never funded and never confirmed: a dangling channel open, + // most likely an abandoned JIT channel, never await/hint it. + assert!(!is_channel_available(true, false, 0)); + + // Whatever the gauge says, a channel that is not + // CHANNELD_NORMAL can not be used or awaited right now. + assert!(!is_channel_available(false, true, 1)); + assert!(!is_channel_available(false, false, 1)); + assert!(!is_channel_available(false, true, 0)); + assert!(!is_channel_available(false, false, 0)); + } +} diff --git a/libs/gl-plugin/src/lib.rs b/libs/gl-plugin/src/lib.rs index b2baaa6cc..ade47c041 100644 --- a/libs/gl-plugin/src/lib.rs +++ b/libs/gl-plugin/src/lib.rs @@ -10,6 +10,7 @@ use tokio::sync::broadcast; extern crate gl_util; mod awaitables; +pub mod channels; pub mod config; pub mod events; pub mod hsm; From 1434cbc19b867f87af8a5d4f31f74fc11220dec5 Mon Sep 17 00:00:00 2001 From: cdecker Date: Sat, 24 Oct 2026 13:11:05 +0000 Subject: [PATCH 2/3] gl-plugin: Skip unavailable channels for sends and invoices Apply `channels::is_channel_available` to all channel selection sites: - `get_receivable_capacity`: the invoice-side JIT check now counts zeroconf channels whose JIT payment was claimed (gauge > 0) and confirmed incoming-only channels (scid), but never dangling, never-funded channel opens the LSP may have dropped. - `get_routehints`: unavailable channels never become routehints, so incoming payments are not routed into channels that may have been abandoned by their LSP server. - trampoline pay: drop unavailable channels before spawning awaitable tasks; awaiting an abandoned channel used to stall the payment for the full `AWAIT_CHANNELS_TIMEOUT_SEC` since `reestablished_channels` joins on every channel. The pre-existing `spendable_msat` filters remain for allocation purposes. This replaces the ad-hoc per-site filters (which used the current balance or `max_to_us_msat` alone, inconsistently) with the single availability rule (gl#871). --- libs/gl-plugin/src/node/mod.rs | 20 +++++++++++++++----- libs/gl-plugin/src/node/wrapper.rs | 18 +++++++++++++++--- libs/gl-plugin/src/tramp.rs | 19 ++++++++++++++++++- 3 files changed, 48 insertions(+), 9 deletions(-) diff --git a/libs/gl-plugin/src/node/mod.rs b/libs/gl-plugin/src/node/mod.rs index 4afc07cbc..fea0b4848 100644 --- a/libs/gl-plugin/src/node/mod.rs +++ b/libs/gl-plugin/src/node/mod.rs @@ -1055,8 +1055,13 @@ impl PluginNodeServer { /// Get the total receivable capacity across all active channels. /// - /// Returns the sum of `receivable_msat` for all channels in - /// `CHANNELD_NORMAL` state with a connected peer and meaningful receivable capacity. + /// Returns the sum of `receivable_msat` for all channels with a + /// connected peer that are available, i.e., pass + /// [`crate::channels::is_channel_available`]: this keeps channels we + /// can actually receive over, including zeroconf JIT channels whose + /// incoming payment was claimed, while excluding dangling channel + /// opens that were never funded and never confirmed (abandoned JIT + /// channels). async fn get_receivable_capacity(&self, rpc: &mut cln_rpc::ClnRpc) -> Result { use cln_rpc::primitives::ChannelState; @@ -1067,9 +1072,14 @@ impl PluginNodeServer { let total: u64 = res .channels .into_iter() - .filter(|c| c.peer_connected && c.state == ChannelState::CHANNELD_NORMAL) - // Filter out channels without meaningful receivable capacity (max_to_us_msat = 0 or None) - .filter(|c| c.max_to_us_msat.is_some() && c.max_to_us_msat.as_ref().unwrap().msat() > 0) + .filter(|c| { + c.peer_connected + && crate::channels::is_channel_available( + c.state == ChannelState::CHANNELD_NORMAL, + c.short_channel_id.is_some(), + c.max_to_us_msat.as_ref().map(|a| a.msat()).unwrap_or(0), + ) + }) .filter_map(|c| c.receivable_msat) .map(|a| a.msat()) .sum(); diff --git a/libs/gl-plugin/src/node/wrapper.rs b/libs/gl-plugin/src/node/wrapper.rs index 771256ee7..6af681b50 100644 --- a/libs/gl-plugin/src/node/wrapper.rs +++ b/libs/gl-plugin/src/node/wrapper.rs @@ -1400,8 +1400,13 @@ impl WrappedNodeServer { } async fn get_routehints(&self, rpc: &mut cln_rpc::ClnRpc) -> Result, Error> { - // Get a map of active channels to peers with a status of - // "CHANNELD_NORMAL", and it aliases. + // Get a map of available channels to peers with a status of + // "CHANNELD_NORMAL", and it aliases. Channels that are not + // available (`crate::channels::is_channel_available`, e.g., + // dangling channel opens or abandoned JIT channels that may have + // been dropped by the LSP server) must never show up as + // routehints, otherwise incoming payments are routed into a + // black hole. // Due to a bug in `listincoming` in CLN we get the real // `short_channel_id`, whereas we're supposed to use the remote alias if // the channel is unannounced. This patches the issue in GL, and should @@ -1416,7 +1421,14 @@ impl WrappedNodeServer { _ => res .channels .into_iter() - .filter(|c| c.peer_connected && c.state == ChannelState::CHANNELD_NORMAL) + .filter(|c| { + c.peer_connected + && crate::channels::is_channel_available( + c.state == ChannelState::CHANNELD_NORMAL, + c.short_channel_id.is_some(), + c.max_to_us_msat.as_ref().map(|a| a.msat()).unwrap_or(0), + ) + }) .filter_map(|c| { c.short_channel_id.map(|scid| { let value = c diff --git a/libs/gl-plugin/src/tramp.rs b/libs/gl-plugin/src/tramp.rs index 486a32443..22bd4c45e 100644 --- a/libs/gl-plugin/src/tramp.rs +++ b/libs/gl-plugin/src/tramp.rs @@ -1,7 +1,7 @@ use crate::awaitables::{AwaitableChannel, AwaitablePeer, Error as AwaitablePeerError}; use crate::pb; use cln_rpc::{ - primitives::{Amount, PublicKey, ShortChannelId}, + primitives::{Amount, ChannelState, PublicKey, ShortChannelId}, ClnRpc, RpcError, }; use futures::{future::join_all, FutureExt}; @@ -400,6 +400,23 @@ pub async fn trampolinepay( .channels .into_iter() .filter_map(|ch| { + // Skip channels we can neither use nor wait for: never + // funded (`max_to_us_msat == 0`) and never confirmed (no + // scid) channels are dangling channel opens that the LSP + // server may have dropped (e.g., abandoned JIT chan- + // nels), awaiting them would stall the payment until the + // deadline. See `crate::channels::is_channel_available`. + if !crate::channels::is_channel_available( + ch.state == ChannelState::CHANNELD_NORMAL, + ch.short_channel_id.is_some(), + ch.max_to_us_msat.as_ref().map(|a| a.msat()).unwrap_or(0), + ) { + debug!( + "Skipping unavailable channel with peer {}: state={:?}, scid={:?}, max_to_us_msat={:?}", + ch.peer_id, ch.state, ch.short_channel_id, ch.max_to_us_msat + ); + return None; + } let short_channel_id = ch.short_channel_id.or(ch.alias.and_then(|a| a.local)); let short_channel_id = match short_channel_id { Some(scid) => scid, From fe6f73454fabae6815182e191c117c3b0d917542 Mon Sep 17 00:00:00 2001 From: cdecker Date: Sat, 24 Oct 2026 13:11:23 +0000 Subject: [PATCH 3/3] gl-plugin: Fail fast when an awaited channel will not come back The billboard step of `AwaitableChannel` looped until the caller's wait deadline whenever a channel would never report "Channel ready" / "Reconnected, and reestablished", e.g. because it was closed or abandoned while the wait was in flight. Check the channel state on each billboard poll and return an error as soon as the channel is not (or no longer) in `CHANNELD_NORMAL`, so such channels are dropped from the wait immediately instead of stalling it until the deadline (gl#871). While at it remove the `v23.05gl1` version shims and the now unused `get_version` bookkeeping: nodes older than v25.12gl1 are not in use anymore. --- libs/gl-plugin/src/awaitables.rs | 120 +++++++++++-------------------- 1 file changed, 43 insertions(+), 77 deletions(-) diff --git a/libs/gl-plugin/src/awaitables.rs b/libs/gl-plugin/src/awaitables.rs index 54738226d..5bc72951d 100644 --- a/libs/gl-plugin/src/awaitables.rs +++ b/libs/gl-plugin/src/awaitables.rs @@ -1,9 +1,9 @@ use cln_rpc::{ model::{ - requests::{ConnectRequest, GetinfoRequest, GetrouteRequest, ListpeerchannelsRequest}, + requests::{ConnectRequest, GetrouteRequest, ListpeerchannelsRequest}, responses::GetrouteResponse, }, - primitives::{Amount, PublicKey, ShortChannelId}, + primitives::{Amount, ChannelState, PublicKey, ShortChannelId}, ClnRpc, }; use std::{ @@ -87,7 +87,6 @@ pub struct AwaitableChannel { peer_id: PublicKey, rpc_path: PathBuf, - version: Option, peer_connected: bool, channel_ready: bool, route_found: bool, @@ -95,7 +94,6 @@ pub struct AwaitableChannel { last_check: Option, rpc_call_delay: Duration, - get_version: Option> + Send>>>, ensure_peer_connection: Option> + Send>>>, billboard: Option, Error>> + Send>>>, get_route: Option> + Send>>>, @@ -119,22 +117,6 @@ impl Future for AwaitableChannel { } } - // Get version if not set already. - if self.version.is_none() { - if self.get_version.is_none() { - let fut = Box::pin(get_version(self.rpc_path.clone())); - self.get_version = Some(fut); - } - - let get_version = self.get_version.as_mut().unwrap(); - match get_version.as_mut().poll(cx) { - std::task::Poll::Ready(v) => { - self.version = Some(v?); - } - std::task::Poll::Pending => return std::task::Poll::Pending, - } - } - // Ensure that the peer is connected. if !self.peer_connected { if self.ensure_peer_connection.is_none() { @@ -161,7 +143,6 @@ impl Future for AwaitableChannel { if self.billboard.is_none() { let fut = Box::pin(billboard( self.rpc_path.clone(), - self.version.as_ref().unwrap().clone(), self.peer_id.clone(), self.scid, )); @@ -216,7 +197,6 @@ impl Future for AwaitableChannel { if self.spendable_msat.is_none() { let fut = Box::pin(spendable_msat( self.rpc_path.clone(), - self.version.as_ref().unwrap().clone(), self.peer_id.clone(), self.scid, )); @@ -237,13 +217,11 @@ impl AwaitableChannel { peer_id, scid, rpc_path, - version: None, peer_connected: false, channel_ready: false, route_found: false, last_check: None, rpc_call_delay: Duration::from_millis(RPC_CALL_DELAY_MSEC), - get_version: None, ensure_peer_connection: None, billboard: None, get_route: None, @@ -294,42 +272,37 @@ async fn ensure_peer_connection( Ok(()) } -async fn get_version(rpc_path: impl AsRef) -> Result { - let mut rpc = connect(rpc_path).await?; - let info = rpc.call_typed(&GetinfoRequest {}).await?; - Ok(info.version) -} - async fn billboard( rpc_path: impl AsRef, - version: String, peer_id: PublicKey, scid: ShortChannelId, ) -> Result, Error> { let mut rpc = connect(rpc_path).await?; - if *version >= *"v23.05gl1" { - Ok(rpc - .call_typed(&ListpeerchannelsRequest { id: Some(peer_id) }) - .await - .map_err(|e| Error::Rpc(e))? - .channels - .into_iter() - .filter(|c| { - c.short_channel_id == Some(scid) - || c.alias.clone().and_then(|a| a.local) == Some(scid) - }) - .nth(0) - .ok_or(Error::Channel( - "Could not find the channel in listpeerchannels", - ))? - .status - .ok_or(Error::Channel("Status not found"))?) - } else { - return Err(Error::Service(format!( - "Not supported in this version of core-lightning: {}, need at least v23.05gl1", - version, - ))); + let channel = rpc + .call_typed(&ListpeerchannelsRequest { id: Some(peer_id) }) + .await + .map_err(|e| Error::Rpc(e))? + .channels + .into_iter() + .filter(|c| { + c.short_channel_id == Some(scid) || c.alias.clone().and_then(|a| a.local) == Some(scid) + }) + .nth(0) + .ok_or(Error::Channel( + "Could not find the channel in listpeerchannels", + ))?; + + // A channel that is not (or no longer) in `CHANNELD_NORMAL` is + // opening, being closed, force-closed or abandoned: it will never + // report "ready" again, so abort waiting for it instead of + // spinning until the wait deadline expires. + if channel.state != ChannelState::CHANNELD_NORMAL { + return Err(Error::Channel( + "Channel is not in CHANNELD_NORMAL state, it will not become ready", + )); } + + channel.status.ok_or(Error::Channel("Status not found")) } async fn get_route( @@ -353,34 +326,27 @@ async fn get_route( async fn spendable_msat( rpc_path: impl AsRef, - version: String, peer_id: PublicKey, scid: ShortChannelId, ) -> Result { let mut rpc = connect(rpc_path).await?; - if *version >= *"v23.05gl1" { - Ok(rpc - .call_typed(&ListpeerchannelsRequest { id: Some(peer_id) }) - .await - .map_err(|e| Error::Rpc(e))? - .channels - .into_iter() - .filter(|c| { - c.short_channel_id == Some(scid) - || c.alias.clone().and_then(|a| a.local) == Some(scid) - }) - .nth(0) - .ok_or(Error::Channel( - "Could not find the channel in listpeerchannels", - ))? - .spendable_msat - .ok_or(Error::Channel("No amount found"))?) - } else { - return Err(Error::Service(format!( - "Not supported in this version of core-lightning: {}, need at least v23.05gl1", - version, - ))); - } + let channel = rpc + .call_typed(&ListpeerchannelsRequest { id: Some(peer_id) }) + .await + .map_err(|e| Error::Rpc(e))? + .channels + .into_iter() + .filter(|c| { + c.short_channel_id == Some(scid) || c.alias.clone().and_then(|a| a.local) == Some(scid) + }) + .nth(0) + .ok_or(Error::Channel( + "Could not find the channel in listpeerchannels", + ))?; + + channel + .spendable_msat + .ok_or(Error::Channel("No amount found")) } pub fn assert_send(_: T) {}