@@ -58,12 +58,12 @@ use crate::events::{
5858use crate::events::{FundingInfo, PaidBolt12Invoice};
5959// Since this struct is returned in `list_channels` methods, expose it here in case users want to
6060// construct one themselves.
61- use crate::ln::channel::PendingV2Channel;
6261use crate::ln::channel::{
63- self, Channel, ChannelError, ChannelUpdateStatus, FundedChannel, InboundV1Channel,
62+ self, hold_time, Channel, ChannelError, ChannelUpdateStatus, FundedChannel, InboundV1Channel,
6463 OutboundV1Channel, ReconnectionMsg, ShutdownResult, UpdateFulfillCommitFetch,
6564 WithChannelContext,
6665};
66+ use crate::ln::channel::{duration_since_epoch, PendingV2Channel};
6767use crate::ln::channel_state::ChannelDetails;
6868use crate::ln::inbound_payment;
6969use crate::ln::msgs;
@@ -77,6 +77,7 @@ use crate::ln::onion_payment::{
7777 NextPacketDetails,
7878};
7979use crate::ln::onion_utils::{self};
80+ use crate::ln::onion_utils::{process_fulfill_attribution_data, AttributionData};
8081use crate::ln::onion_utils::{HTLCFailReason, LocalHTLCFailureReason};
8182use crate::ln::our_peer_storage::EncryptedOurPeerStorage;
8283#[cfg(test)]
@@ -7938,10 +7939,30 @@ where
79387939 pending_claim: PendingMPPClaimPointer(Arc::clone(pending_claim)),
79397940 }
79407941 });
7942+
7943+ // Create new attribution data as the final hop. Always report a zero hold time, because reporting a
7944+ // non-zero value will not make a difference in the penalty that may be applied by the sender. If there
7945+ // is a phantom hop, we need to double-process.
7946+ let attribution_data =
7947+ if let Some(phantom_secret) = htlc.prev_hop.phantom_shared_secret {
7948+ let attribution_data =
7949+ process_fulfill_attribution_data(None, &phantom_secret, 0);
7950+ Some(attribution_data)
7951+ } else {
7952+ None
7953+ };
7954+
7955+ let attribution_data = process_fulfill_attribution_data(
7956+ attribution_data.as_ref(),
7957+ &htlc.prev_hop.incoming_packet_shared_secret,
7958+ 0,
7959+ );
7960+
79417961 self.claim_funds_from_hop(
79427962 htlc.prev_hop,
79437963 payment_preimage,
79447964 payment_info.clone(),
7965+ Some(attribution_data),
79457966 |_, definitely_duplicate| {
79467967 debug_assert!(
79477968 !definitely_duplicate,
@@ -7986,7 +8007,8 @@ where
79868007 ) -> (Option<MonitorUpdateCompletionAction>, Option<RAAMonitorUpdateBlockingAction>),
79878008 >(
79888009 &self, prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage,
7989- payment_info: Option<PaymentClaimDetails>, completion_action: ComplFunc,
8010+ payment_info: Option<PaymentClaimDetails>, attribution_data: Option<AttributionData>,
8011+ completion_action: ComplFunc,
79908012 ) {
79918013 let counterparty_node_id = prev_hop.counterparty_node_id.or_else(|| {
79928014 let short_to_chan_info = self.short_to_chan_info.read().unwrap();
@@ -7999,7 +8021,13 @@ where
79998021 channel_id: prev_hop.channel_id,
80008022 htlc_id: prev_hop.htlc_id,
80018023 };
8002- self.claim_mpp_part(htlc_source, payment_preimage, payment_info, completion_action)
8024+ self.claim_mpp_part(
8025+ htlc_source,
8026+ payment_preimage,
8027+ payment_info,
8028+ attribution_data,
8029+ completion_action,
8030+ )
80038031 }
80048032
80058033 fn claim_mpp_part<
@@ -8009,7 +8037,8 @@ where
80098037 ) -> (Option<MonitorUpdateCompletionAction>, Option<RAAMonitorUpdateBlockingAction>),
80108038 >(
80118039 &self, prev_hop: HTLCClaimSource, payment_preimage: PaymentPreimage,
8012- payment_info: Option<PaymentClaimDetails>, completion_action: ComplFunc,
8040+ payment_info: Option<PaymentClaimDetails>, attribution_data: Option<AttributionData>,
8041+ completion_action: ComplFunc,
80138042 ) {
80148043 //TODO: Delay the claimed_funds relaying just like we do outbound relay!
80158044
@@ -8050,6 +8079,7 @@ where
80508079 prev_hop.htlc_id,
80518080 payment_preimage,
80528081 payment_info,
8082+ attribution_data,
80538083 &&logger,
80548084 );
80558085
@@ -8258,7 +8288,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82588288 forwarded_htlc_value_msat: Option<u64>, skimmed_fee_msat: Option<u64>, from_onchain: bool,
82598289 startup_replay: bool, next_channel_counterparty_node_id: PublicKey,
82608290 next_channel_outpoint: OutPoint, next_channel_id: ChannelId,
8261- next_user_channel_id: Option<u128>,
8291+ next_user_channel_id: Option<u128>, attribution_data: Option<&AttributionData>,
8292+ send_timestamp: Option<Duration>,
82628293 ) {
82638294 match source {
82648295 HTLCSource::OutboundRoute {
@@ -8290,10 +8321,25 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
82908321 let prev_node_id = hop_data.counterparty_node_id;
82918322 let completed_blocker =
82928323 RAAMonitorUpdateBlockingAction::from_prev_hop_data(&hop_data);
8324+
8325+ // Obtain hold time, if available.
8326+ let now = duration_since_epoch();
8327+ let hold_time = hold_time(send_timestamp, now).unwrap_or(0);
8328+
8329+ // If attribution data was received from downstream, we shift it and get it ready for adding our hold
8330+ // time. Note that fulfilled HTLCs take a fast path to the incoming side. We don't need to wait for RAA
8331+ // to record the hold time like we do for failed HTLCs.
8332+ let attribution_data = process_fulfill_attribution_data(
8333+ attribution_data,
8334+ &hop_data.incoming_packet_shared_secret,
8335+ hold_time,
8336+ );
8337+
82938338 self.claim_funds_from_hop(
82948339 hop_data,
82958340 payment_preimage,
82968341 None,
8342+ Some(attribution_data),
82978343 |htlc_claim_value_msat, definitely_duplicate| {
82988344 let chan_to_release = Some(EventUnblockedChannel {
82998345 counterparty_node_id: next_channel_counterparty_node_id,
@@ -9852,7 +9898,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
98529898 ) -> Result<(), MsgHandleErrInternal> {
98539899 let funding_txo;
98549900 let next_user_channel_id;
9855- let (htlc_source, forwarded_htlc_value, skimmed_fee_msat) = {
9901+ let (htlc_source, forwarded_htlc_value, skimmed_fee_msat, send_timestamp ) = {
98569902 let per_peer_state = self.per_peer_state.read().unwrap();
98579903 let peer_state_mutex = per_peer_state.get(counterparty_node_id).ok_or_else(|| {
98589904 debug_assert!(false);
@@ -9907,6 +9953,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
99079953 funding_txo,
99089954 msg.channel_id,
99099955 Some(next_user_channel_id),
9956+ msg.attribution_data.as_ref(),
9957+ send_timestamp,
99109958 );
99119959
99129960 Ok(())
@@ -10800,6 +10848,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1080010848 "Claiming HTLC with preimage {} from our monitor",
1080110849 preimage
1080210850 );
10851+ // Claim the funds from the previous hop, if there is one. Because this is in response to a
10852+ // chain event, no attribution data is available.
1080310853 self.claim_funds_internal(
1080410854 htlc_update.source,
1080510855 preimage,
@@ -10811,6 +10861,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1081110861 funding_outpoint,
1081210862 channel_id,
1081310863 None,
10864+ None,
10865+ None,
1081410866 );
1081510867 } else {
1081610868 log_trace!(
@@ -16677,10 +16729,14 @@ where
1667716729 // Note that we don't need to pass the `payment_info` here - its
1667816730 // already (clearly) durably on disk in the `ChannelMonitor` so there's
1667916731 // no need to worry about getting it into others.
16732+ //
16733+ // We don't encode any attribution data, because the required onion shared secret isn't
16734+ // available here.
1668016735 channel_manager.claim_mpp_part(
1668116736 part.into(),
1668216737 payment_preimage,
1668316738 None,
16739+ None,
1668416740 |_, _| {
1668516741 (
1668616742 Some(MonitorUpdateCompletionAction::PaymentClaimed {
@@ -16825,6 +16881,7 @@ where
1682516881 // We use `downstream_closed` in place of `from_onchain` here just as a guess - we
1682616882 // don't remember in the `ChannelMonitor` where we got a preimage from, but if the
1682716883 // channel is closed we just assume that it probably came from an on-chain claim.
16884+ // The same holds for attribution data. We don't have any, so we pass an empty one.
1682816885 channel_manager.claim_funds_internal(
1682916886 source,
1683016887 preimage,
@@ -16836,6 +16893,8 @@ where
1683616893 downstream_funding,
1683716894 downstream_channel_id,
1683816895 None,
16896+ None,
16897+ None,
1683916898 );
1684016899 }
1684116900
0 commit comments