From: Matt Corallo Date: Mon, 9 Jan 2023 18:42:34 +0000 (+0000) Subject: Allow htlc_satisfies_config to override the amount to forward X-Git-Url: http://git.bitcoin.ninja/?a=commitdiff_plain;h=657c75aa1f6260aebd573de9a81d84de57a7ead2;p=rust-lightning Allow htlc_satisfies_config to override the amount to forward This allows us to calculate implicit fees before forwarding. --- diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 29f1a30a6..583a98ec6 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -4724,7 +4724,7 @@ impl Channel { fn internal_htlc_satisfies_config( &self, htlc: &msgs::UpdateAddHTLC, amt_to_forward: u64, outgoing_cltv_value: u32, config: &ChannelConfig, - ) -> Result<(), (&'static str, u16)> { + ) -> Result { let fee = amt_to_forward.checked_mul(config.forwarding_fee_proportional_millionths as u64) .and_then(|prop_fee| (prop_fee / 1000000).checked_add(config.forwarding_fee_base_msat as u64)); if fee.is_none() || htlc.amount_msat < fee.unwrap() || @@ -4740,15 +4740,17 @@ impl Channel { 0x1000 | 13, // incorrect_cltv_expiry )); } - Ok(()) + Ok(amt_to_forward) } /// Determines whether the parameters of an incoming HTLC to be forwarded satisfy the channel's /// [`ChannelConfig`]. This first looks at the channel's current [`ChannelConfig`], and if /// unsuccessful, falls back to the previous one if one exists. + /// + /// Returns the amount we should actually forward to our counterparty. pub fn htlc_satisfies_config( &self, htlc: &msgs::UpdateAddHTLC, amt_to_forward: u64, outgoing_cltv_value: u32, - ) -> Result<(), (&'static str, u16)> { + ) -> Result { self.internal_htlc_satisfies_config(&htlc, amt_to_forward, outgoing_cltv_value, &self.config()) .or_else(|err| { if let Some(prev_config) = self.prev_config() { diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index a17332225..a39ca1e20 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -2014,7 +2014,7 @@ where }, }; - let pending_forward_info = match next_hop { + let mut pending_forward_info = match next_hop { onion_utils::Hop::Receive(next_hop_data) => { // OUR PAYMENT! match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash, msg.amount_msat, msg.cltv_expiry, None) { @@ -2058,7 +2058,7 @@ where } }; - if let &PendingHTLCStatus::Forward(PendingHTLCInfo { ref routing, ref outgoing_amt_msat, ref outgoing_cltv_value, .. }) = &pending_forward_info { + if let &mut PendingHTLCStatus::Forward(PendingHTLCInfo { ref routing, ref mut outgoing_amt_msat, ref outgoing_cltv_value, .. }) = &mut pending_forward_info { // If short_channel_id is 0 here, we'll reject the HTLC as there cannot be a channel // with a short_channel_id of 0. This is important as various things later assume // short_channel_id is non-0 in any ::Forward. @@ -2115,8 +2115,9 @@ where if *outgoing_amt_msat < chan.get_counterparty_htlc_minimum_msat() { // amount_below_minimum break Some(("HTLC amount was below the htlc_minimum_msat", 0x1000 | 11, chan_update_opt)); } - if let Err((err, code)) = chan.htlc_satisfies_config(&msg, *outgoing_amt_msat, *outgoing_cltv_value) { - break Some((err, code, chan_update_opt)); + match chan.htlc_satisfies_config(&msg, *outgoing_amt_msat, *outgoing_cltv_value) { + Err((err, code)) => break Some((err, code, chan_update_opt)), + Ok(amt_to_forward_msat) => *outgoing_amt_msat = amt_to_forward_msat, } chan_update_opt } else {