From 03fe5026c59791e5be4622777701ce403c1ef997 Mon Sep 17 00:00:00 2001 From: Turtle Date: Wed, 7 Oct 2026 15:32:49 -0500 Subject: [PATCH 1/2] Add FORCE_PATH_RGB_PAYMENT_ON_NODE test hook Lets a test make a sender put an arbitrary RGB payment on every hop, like a payer whose wire client is not bound by the invoice. It also rewrites the sender's outbound RGB payment info file so commitment coloring agrees with the HTLC. Co-Authored-By: Claude --- lightning/src/ln/channelmanager.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 1ff820b76..0d0fbbba7 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -209,6 +209,13 @@ use crate::ln::script::ShutdownScript; #[cfg(any(test, feature = "_rln_test_hooks"))] pub static DROP_FUNDING_SIGNED_ON_NODE: Mutex> = Mutex::new(None); +/// Test-only hook: if set to a node pubkey and RGB payment, that sender puts the given RGB payment +/// on every hop of its outgoing payments (overriding the amount from the payment's RGB info), as +/// a payer whose wire client is not bound by the invoice would. +#[cfg(any(test, feature = "_rln_test_hooks"))] +pub static FORCE_PATH_RGB_PAYMENT_ON_NODE: Mutex> = + Mutex::new(None); + // We hold various information about HTLC relay in the HTLC objects in Channel itself: // // Upon receipt of an HTLC from a peer, we'll give it a PendingHTLCStatus indicating if it should @@ -5296,6 +5303,28 @@ where } else { None }; + #[cfg(any(test, feature = "_rln_test_hooks"))] + let needs_rgb_modification = match FORCE_PATH_RGB_PAYMENT_ON_NODE.lock().unwrap().clone() { + Some((node_id, contract_id, amount)) + if node_id == self.get_our_node_id() && needs_rgb_modification.is_some() => + { + needs_rgb_modification.map(|mut info| { + info.contract_id = contract_id; + info.amount = amount; + // commitment coloring reads the file too, so keep it in sync with the HTLC + crate::rgb_utils::write_rgb_payment_info_file( + &self.ldk_data_dir, + &payment_hash, + contract_id, + amount, + info.swap_payment, + false, + ); + info + }) + }, + _ => needs_rgb_modification, + }; let modified_path; let path = if let Some(rgb_payment_info) = needs_rgb_modification { modified_path = { From 75e7dff490c1061e09167cfd70907394d2028009 Mon Sep 17 00:00:00 2001 From: Turtle Date: Thu, 8 Oct 2026 19:29:36 -0500 Subject: [PATCH 2/2] Count in-flight RGB HTLCs and check asset on incoming HTLCs Balance checks only looked at the settled RGB balance, so several pending HTLCs could together exceed it, making the sender panic while coloring the commitment. They now subtract RGB already in flight, and incoming HTLCs must carry the channel's asset. Co-Authored-By: Claude --- lightning/src/ln/channel.rs | 38 +++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 6c9373a87..38ebd8544 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -4398,6 +4398,25 @@ where } } + /// RGB amount locked in pending HTLCs offered by us + fn rgb_in_flight_outbound(&self) -> u64 { + self.pending_outbound_htlcs + .iter() + .filter_map(|htlc| htlc.rgb_payment.map(|(_, amount)| amount)) + .fold(0u64, |acc, amount| acc.saturating_add(amount)) + } + + /// RGB amount locked in pending HTLCs offered by the counterparty. HTLCs we already resolved + /// no longer lock the counterparty's RGB, so they are skipped: counting them could make us + /// close the channel on a valid HTLC. + fn rgb_in_flight_inbound(&self) -> u64 { + self.pending_inbound_htlcs + .iter() + .filter(|htlc| !matches!(htlc.state, InboundHTLCState::LocalRemoved(_))) + .filter_map(|htlc| htlc.rgb_payment.map(|(_, amount)| amount)) + .fold(0u64, |acc, amount| acc.saturating_add(amount)) + } + /// Get the channel RGB capacity pub fn get_rgb_capacity(&self) -> u64 { self.get_local_rgb_amount() + self.get_remote_rgb_amount() @@ -7851,9 +7870,17 @@ where return Err(ChannelError::close(format!("Remote side tried to send less than our minimum HTLC value. Lower limit: ({}). Actual: ({})", self.context.holder_htlc_minimum_msat, msg.amount_msat))); } - if let Some((_, amount_rgb)) = msg.rgb_payment { - if amount_rgb > self.context.get_remote_rgb_amount() { - return Err(ChannelError::close("Not enough RGB funds to accept this HTLC".to_owned())); + if let Some((contract_id, amount_rgb)) = msg.rgb_payment { + if self.funding.contract_id() != Some(contract_id) { + return Err(ChannelError::close("RGB HTLC asset does not match the channel asset".to_owned())); + } + let remote_rgb_amount = self.context.get_remote_rgb_amount(); + let in_flight = self.context.rgb_in_flight_inbound(); + if amount_rgb > remote_rgb_amount.saturating_sub(in_flight) { + return Err(ChannelError::close(format!( + "Not enough RGB funds to accept this HTLC: amount {}, remote balance {}, already in flight {}", + amount_rgb, remote_rgb_amount, in_flight + ))); } } @@ -12781,7 +12808,10 @@ where } if let Some((_, amount_rgb)) = rgb_payment { - let local_rgb_amount = self.context.get_local_rgb_amount(); + let local_rgb_amount = self + .context + .get_local_rgb_amount() + .saturating_sub(self.context.rgb_in_flight_outbound()); if amount_rgb > local_rgb_amount { return Err(( LocalHTLCFailureReason::RgbHTLCMaximum,