@@ -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)]
@@ -7691,10 +7692,30 @@ where
76917692 pending_claim: PendingMPPClaimPointer(Arc::clone(pending_claim)),
76927693 }
76937694 });
7695+
7696+ // Create new attribution data as the final hop. Always report a zero hold time, because reporting a
7697+ // non-zero value will not make a difference in the penalty that may be applied by the sender. If there
7698+ // is a phantom hop, we need to double-process.
7699+ let attribution_data =
7700+ if let Some(phantom_secret) = htlc.prev_hop.phantom_shared_secret {
7701+ let attribution_data =
7702+ process_fulfill_attribution_data(None, &phantom_secret, 0);
7703+ Some(attribution_data)
7704+ } else {
7705+ None
7706+ };
7707+
7708+ let attribution_data = process_fulfill_attribution_data(
7709+ attribution_data.as_ref(),
7710+ &htlc.prev_hop.incoming_packet_shared_secret,
7711+ 0,
7712+ );
7713+
76947714 self.claim_funds_from_hop(
76957715 htlc.prev_hop,
76967716 payment_preimage,
76977717 payment_info.clone(),
7718+ Some(attribution_data),
76987719 |_, definitely_duplicate| {
76997720 debug_assert!(
77007721 !definitely_duplicate,
@@ -7739,7 +7760,8 @@ where
77397760 ) -> (Option<MonitorUpdateCompletionAction>, Option<RAAMonitorUpdateBlockingAction>),
77407761 >(
77417762 &self, prev_hop: HTLCPreviousHopData, payment_preimage: PaymentPreimage,
7742- payment_info: Option<PaymentClaimDetails>, completion_action: ComplFunc,
7763+ payment_info: Option<PaymentClaimDetails>, attribution_data: Option<AttributionData>,
7764+ completion_action: ComplFunc,
77437765 ) {
77447766 let counterparty_node_id = prev_hop.counterparty_node_id.or_else(|| {
77457767 let short_to_chan_info = self.short_to_chan_info.read().unwrap();
@@ -7752,7 +7774,13 @@ where
77527774 channel_id: prev_hop.channel_id,
77537775 htlc_id: prev_hop.htlc_id,
77547776 };
7755- self.claim_mpp_part(htlc_source, payment_preimage, payment_info, completion_action)
7777+ self.claim_mpp_part(
7778+ htlc_source,
7779+ payment_preimage,
7780+ payment_info,
7781+ attribution_data,
7782+ completion_action,
7783+ )
77567784 }
77577785
77587786 fn claim_mpp_part<
@@ -7762,7 +7790,8 @@ where
77627790 ) -> (Option<MonitorUpdateCompletionAction>, Option<RAAMonitorUpdateBlockingAction>),
77637791 >(
77647792 &self, prev_hop: HTLCClaimSource, payment_preimage: PaymentPreimage,
7765- payment_info: Option<PaymentClaimDetails>, completion_action: ComplFunc,
7793+ payment_info: Option<PaymentClaimDetails>, attribution_data: Option<AttributionData>,
7794+ completion_action: ComplFunc,
77667795 ) {
77677796 //TODO: Delay the claimed_funds relaying just like we do outbound relay!
77687797
@@ -7803,6 +7832,7 @@ where
78037832 prev_hop.htlc_id,
78047833 payment_preimage,
78057834 payment_info,
7835+ attribution_data,
78067836 &&logger,
78077837 );
78087838
@@ -8011,7 +8041,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80118041 forwarded_htlc_value_msat: Option<u64>, skimmed_fee_msat: Option<u64>, from_onchain: bool,
80128042 startup_replay: bool, next_channel_counterparty_node_id: PublicKey,
80138043 next_channel_outpoint: OutPoint, next_channel_id: ChannelId,
8014- next_user_channel_id: Option<u128>,
8044+ next_user_channel_id: Option<u128>, attribution_data: Option<&AttributionData>,
8045+ send_timestamp: Option<Duration>,
80158046 ) {
80168047 match source {
80178048 HTLCSource::OutboundRoute {
@@ -8043,10 +8074,25 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80438074 let prev_node_id = hop_data.counterparty_node_id;
80448075 let completed_blocker =
80458076 RAAMonitorUpdateBlockingAction::from_prev_hop_data(&hop_data);
8077+
8078+ // Obtain hold time, if available.
8079+ let now = duration_since_epoch();
8080+ let hold_time = hold_time(send_timestamp, now).unwrap_or(0);
8081+
8082+ // If attribution data was received from downstream, we shift it and get it ready for adding our hold
8083+ // time. Note that fulfilled HTLCs take a fast path to the incoming side. We don't need to wait for RAA
8084+ // to record the hold time like we do for failed HTLCs.
8085+ let attribution_data = process_fulfill_attribution_data(
8086+ attribution_data,
8087+ &hop_data.incoming_packet_shared_secret,
8088+ hold_time,
8089+ );
8090+
80468091 self.claim_funds_from_hop(
80478092 hop_data,
80488093 payment_preimage,
80498094 None,
8095+ Some(attribution_data),
80508096 |htlc_claim_value_msat, definitely_duplicate| {
80518097 let chan_to_release = Some(EventUnblockedChannel {
80528098 counterparty_node_id: next_channel_counterparty_node_id,
@@ -9604,7 +9650,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
96049650 ) -> Result<(), MsgHandleErrInternal> {
96059651 let funding_txo;
96069652 let next_user_channel_id;
9607- let (htlc_source, forwarded_htlc_value, skimmed_fee_msat) = {
9653+ let (htlc_source, forwarded_htlc_value, skimmed_fee_msat, send_timestamp ) = {
96089654 let per_peer_state = self.per_peer_state.read().unwrap();
96099655 let peer_state_mutex = per_peer_state.get(counterparty_node_id).ok_or_else(|| {
96109656 debug_assert!(false);
@@ -9659,6 +9705,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
96599705 funding_txo,
96609706 msg.channel_id,
96619707 Some(next_user_channel_id),
9708+ msg.attribution_data.as_ref(),
9709+ send_timestamp,
96629710 );
96639711
96649712 Ok(())
@@ -10482,6 +10530,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1048210530 "Claiming HTLC with preimage {} from our monitor",
1048310531 preimage
1048410532 );
10533+ // Claim the funds from the previous hop, if there is one. Because this is in response to a
10534+ // chain event, no attribution data is available.
1048510535 self.claim_funds_internal(
1048610536 htlc_update.source,
1048710537 preimage,
@@ -10493,6 +10543,8 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1049310543 funding_outpoint,
1049410544 channel_id,
1049510545 None,
10546+ None,
10547+ None,
1049610548 );
1049710549 } else {
1049810550 log_trace!(
@@ -16377,10 +16429,14 @@ where
1637716429 // Note that we don't need to pass the `payment_info` here - its
1637816430 // already (clearly) durably on disk in the `ChannelMonitor` so there's
1637916431 // no need to worry about getting it into others.
16432+ //
16433+ // We don't encode any attribution data, because the required onion shared secret isn't
16434+ // available here.
1638016435 channel_manager.claim_mpp_part(
1638116436 part.into(),
1638216437 payment_preimage,
1638316438 None,
16439+ None,
1638416440 |_, _| {
1638516441 (
1638616442 Some(MonitorUpdateCompletionAction::PaymentClaimed {
@@ -16525,6 +16581,7 @@ where
1652516581 // We use `downstream_closed` in place of `from_onchain` here just as a guess - we
1652616582 // don't remember in the `ChannelMonitor` where we got a preimage from, but if the
1652716583 // channel is closed we just assume that it probably came from an on-chain claim.
16584+ // The same holds for attribution data. We don't have any, so we pass an empty one.
1652816585 channel_manager.claim_funds_internal(
1652916586 source,
1653016587 preimage,
@@ -16536,6 +16593,8 @@ where
1653616593 downstream_funding,
1653716594 downstream_channel_id,
1653816595 None,
16596+ None,
16597+ None,
1653916598 );
1654016599 }
1654116600
0 commit comments