Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
)));
}
}

Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,13 @@ use crate::ln::script::ShutdownScript;
#[cfg(any(test, feature = "_rln_test_hooks"))]
pub static DROP_FUNDING_SIGNED_ON_NODE: Mutex<Option<PublicKey>> = 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<Option<(PublicKey, ContractId, u64)>> =
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
Expand Down Expand Up @@ -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 = {
Expand Down
Loading