From f8d978042cd47905f64fa37bbd28c2dcd4546b5f Mon Sep 17 00:00:00 2001 From: Valentine Wallace Date: Wed, 24 May 2023 19:07:50 -0400 Subject: [PATCH] Store our skimmed fee in PendingHTLCRouting Receivers need to use this value to verify incoming payments if ChannelConfig::accept_underpaying_htlcs is set. This breaks compatibility with LDK versions prior to 0.0.116 due to the way the field is serialized in the outgoing Channel on forward. --- lightning/src/ln/channelmanager.rs | 17 +++++++++++++++-- lightning/src/util/config.rs | 2 +- pending_changelog/forward-underpaying-htlc.txt | 4 ++++ 3 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 pending_changelog/forward-underpaying-htlc.txt diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index c7e96b66ca1..5db9e0d7298 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -104,6 +104,9 @@ pub(super) enum PendingHTLCRouting { /// The SCID from the onion that we should forward to. This could be a real SCID or a fake one /// generated using `get_fake_scid` from the scid_utils::fake_scid module. short_channel_id: u64, // This should be NonZero eventually when we bump MSRV + /// The fee we're skimming off the top of this HTLC. See + /// [`ChannelConfig::accept_underpaying_htlcs`]. + skimmed_fee_msat: Option, }, Receive { payment_data: msgs::FinalOnionHopData, @@ -2764,6 +2767,7 @@ where routing: PendingHTLCRouting::Forward { onion_packet: outgoing_packet, short_channel_id, + skimmed_fee_msat: None, }, payment_hash: msg.payment_hash.clone(), incoming_shared_secret: shared_secret, @@ -3402,8 +3406,16 @@ where })?; let routing = match payment.forward_info.routing { - PendingHTLCRouting::Forward { onion_packet, .. } => { - PendingHTLCRouting::Forward { onion_packet, short_channel_id: next_hop_scid } + PendingHTLCRouting::Forward { onion_packet, skimmed_fee_msat, .. } => { + debug_assert!(skimmed_fee_msat.is_none()); + PendingHTLCRouting::Forward { + onion_packet, + short_channel_id: next_hop_scid, + skimmed_fee_msat: + // The minuend here must match the expected forward amount generated for the + // HTLCIntercepted event. + Some(payment.forward_info.outgoing_amt_msat.saturating_sub(amt_to_forward_msat)), + } }, _ => unreachable!() // Only `PendingHTLCRouting::Forward`s are intercepted }; @@ -7311,6 +7323,7 @@ impl_writeable_tlv_based!(PhantomRouteHints, { impl_writeable_tlv_based_enum!(PendingHTLCRouting, (0, Forward) => { (0, onion_packet, required), + (1, skimmed_fee_msat, option), (2, short_channel_id, required), }, (1, Receive) => { diff --git a/lightning/src/util/config.rs b/lightning/src/util/config.rs index 96a9ae8ed79..e579c2842c4 100644 --- a/lightning/src/util/config.rs +++ b/lightning/src/util/config.rs @@ -636,7 +636,7 @@ pub struct UserConfig { /// fake short channel ids generated via [`ChannelManager::get_intercept_scid`]. Upon HTLC /// intercept, LDK will generate an [`Event::HTLCIntercepted`] which MUST be handled by the user. /// - /// Setting this to true may break backwards compatibility with LDK versions < 0.0.113. + /// Setting this to true may break backwards compatibility with LDK versions < 0.0.116. /// /// Default value: false. /// diff --git a/pending_changelog/forward-underpaying-htlc.txt b/pending_changelog/forward-underpaying-htlc.txt new file mode 100644 index 00000000000..7c2a760ea61 --- /dev/null +++ b/pending_changelog/forward-underpaying-htlc.txt @@ -0,0 +1,4 @@ +## Backwards Compat + +* Once upgraded, users with `UserConfig::accept_intercept_htlcs` set may not be able to downgrade to + LDK versions prior to 0.0.116.