Skip to content

Commit cd289da

Browse files
committed
Decode hold times as a sender
Hold times are surfaced via the PaymentPathSuccessful event.
1 parent cd1f95c commit cd289da

File tree

9 files changed

+218
-26
lines changed

9 files changed

+218
-26
lines changed

fuzz/src/process_onion_failure.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
115115
let path = Path { hops, blinded_tail };
116116

117117
let htlc_source = HTLCSource::OutboundRoute {
118-
path,
118+
path: path.clone(),
119119
session_priv,
120120
first_hop_htlc_msat: 0,
121121
payment_id,
@@ -133,8 +133,19 @@ fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
133133
} else {
134134
None
135135
};
136-
let encrypted_packet = OnionErrorPacket { data: failure_data.into(), attribution_data };
136+
let encrypted_packet =
137+
OnionErrorPacket { data: failure_data.into(), attribution_data: attribution_data.clone() };
137138
lightning::ln::process_onion_failure(&secp_ctx, &logger, &htlc_source, encrypted_packet);
139+
140+
if let Some(attribution_data) = attribution_data {
141+
lightning::ln::process_onion_success(
142+
&secp_ctx,
143+
&logger,
144+
&path,
145+
&session_priv,
146+
attribution_data,
147+
);
148+
}
138149
}
139150

140151
/// Method that needs to be added manually, {name}_test

lightning-background-processor/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,6 +2732,7 @@ mod tests {
27322732
payment_id: PaymentId([42; 32]),
27332733
payment_hash: None,
27342734
path: path.clone(),
2735+
hold_times: None,
27352736
});
27362737
let event = $receive.expect("PaymentPathSuccessful not handled within deadline");
27372738
match event {

lightning/src/events/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,8 @@ pub enum Event {
10801080
///
10811081
/// May contain a closed channel if the HTLC sent along the path was fulfilled on chain.
10821082
path: Path,
1083+
/// The hold times as reported by each hop.
1084+
hold_times: Option<Vec<u32>>,
10831085
},
10841086
/// Indicates an outbound HTLC we sent failed, likely due to an intermediary node being unable to
10851087
/// handle the HTLC.
@@ -1816,10 +1818,16 @@ impl Writeable for Event {
18161818
(4, funding_info, required),
18171819
})
18181820
},
1819-
&Event::PaymentPathSuccessful { ref payment_id, ref payment_hash, ref path } => {
1821+
&Event::PaymentPathSuccessful {
1822+
ref payment_id,
1823+
ref payment_hash,
1824+
ref path,
1825+
ref hold_times,
1826+
} => {
18201827
13u8.write(writer)?;
18211828
write_tlv_fields!(writer, {
18221829
(0, payment_id, required),
1830+
(1, hold_times, option),
18231831
(2, payment_hash, option),
18241832
(4, path.hops, required_vec),
18251833
(6, path.blinded_tail, option),
@@ -2308,6 +2316,7 @@ impl MaybeReadable for Event {
23082316
let mut f = || {
23092317
_init_and_read_len_prefixed_tlv_fields!(reader, {
23102318
(0, payment_id, required),
2319+
(1, hold_times, option),
23112320
(2, payment_hash, option),
23122321
(4, path, required_vec),
23132322
(6, blinded_tail, option),
@@ -2316,6 +2325,7 @@ impl MaybeReadable for Event {
23162325
payment_id: payment_id.0.unwrap(),
23172326
payment_hash,
23182327
path: Path { hops: path, blinded_tail },
2328+
hold_times,
23192329
}))
23202330
};
23212331
f()

lightning/src/ln/channel.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ impl OutboundHTLCState {
404404
enum OutboundHTLCOutcome {
405405
/// We started always filling in the preimages here in 0.0.105, and the requirement
406406
/// that the preimages always be filled in was added in 0.2.
407-
Success(PaymentPreimage, #[allow(dead_code)] Option<AttributionData>),
407+
Success(PaymentPreimage, Option<AttributionData>),
408408
Failure(HTLCFailReason),
409409
}
410410

@@ -1184,7 +1184,7 @@ pub(super) struct MonitorRestoreUpdates {
11841184
pub order: RAACommitmentOrder,
11851185
pub accepted_htlcs: Vec<(PendingHTLCInfo, u64)>,
11861186
pub failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
1187-
pub finalized_claimed_htlcs: Vec<HTLCSource>,
1187+
pub finalized_claimed_htlcs: Vec<(HTLCSource, Option<AttributionData>)>,
11881188
pub pending_update_adds: Vec<msgs::UpdateAddHTLC>,
11891189
pub funding_broadcastable: Option<Transaction>,
11901190
pub channel_ready: Option<msgs::ChannelReady>,
@@ -2316,7 +2316,7 @@ where
23162316
// but need to handle this somehow or we run the risk of losing HTLCs!
23172317
monitor_pending_forwards: Vec<(PendingHTLCInfo, u64)>,
23182318
monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
2319-
monitor_pending_finalized_fulfills: Vec<HTLCSource>,
2319+
monitor_pending_finalized_fulfills: Vec<(HTLCSource, Option<AttributionData>)>,
23202320
monitor_pending_update_adds: Vec<msgs::UpdateAddHTLC>,
23212321
monitor_pending_tx_signatures: Option<msgs::TxSignatures>,
23222322

@@ -6690,7 +6690,8 @@ where
66906690
));
66916691
}
66926692

6693-
let outcome = OutboundHTLCOutcome::Success(msg.payment_preimage, None);
6693+
let outcome =
6694+
OutboundHTLCOutcome::Success(msg.payment_preimage, msg.attribution_data.clone());
66946695
self.mark_outbound_htlc_removed(msg.htlc_id, outcome).map(|htlc| {
66956696
(htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat, htlc.send_timestamp)
66966697
})
@@ -7513,16 +7514,21 @@ where
75137514
&htlc.payment_hash
75147515
);
75157516
// We really want take() here, but, again, non-mut ref :(
7516-
if let OutboundHTLCOutcome::Failure(mut reason) = outcome.clone() {
7517-
hold_time(htlc.send_timestamp, now).map(|hold_time| {
7518-
reason.set_hold_time(hold_time);
7519-
});
7520-
7521-
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
7522-
} else {
7523-
finalized_claimed_htlcs.push(htlc.source.clone());
7524-
// They fulfilled, so we sent them money
7525-
value_to_self_msat_diff -= htlc.amount_msat as i64;
7517+
match outcome.clone() {
7518+
OutboundHTLCOutcome::Failure(mut reason) => {
7519+
hold_time(htlc.send_timestamp, now).map(|hold_time| {
7520+
reason.set_hold_time(hold_time);
7521+
});
7522+
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
7523+
},
7524+
OutboundHTLCOutcome::Success(_, attribution_data) => {
7525+
// Even though a fast track was taken for fulfilled HTLCs to the incoming side, we still
7526+
// pass along attribution data here so that we can include hold time information in the
7527+
// final PaymentPathSuccessful events.
7528+
finalized_claimed_htlcs.push((htlc.source.clone(), attribution_data));
7529+
// They fulfilled, so we sent them money
7530+
value_to_self_msat_diff -= htlc.amount_msat as i64;
7531+
},
75267532
}
75277533
false
75287534
} else {
@@ -8012,7 +8018,7 @@ where
80128018
&mut self, resend_raa: bool, resend_commitment: bool, resend_channel_ready: bool,
80138019
mut pending_forwards: Vec<(PendingHTLCInfo, u64)>,
80148020
mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
8015-
mut pending_finalized_claimed_htlcs: Vec<HTLCSource>,
8021+
mut pending_finalized_claimed_htlcs: Vec<(HTLCSource, Option<AttributionData>)>,
80168022
) {
80178023
self.context.monitor_pending_revoke_and_ack |= resend_raa;
80188024
self.context.monitor_pending_commitment_signed |= resend_commitment;

lightning/src/ln/channelmanager.rs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,10 @@ use crate::ln::onion_payment::{
7777
NextPacketDetails,
7878
};
7979
use crate::ln::onion_utils::{self};
80+
use crate::ln::onion_utils::{
81+
decode_fulfill_attribution_data, HTLCFailReason, LocalHTLCFailureReason,
82+
};
8083
use crate::ln::onion_utils::{process_fulfill_attribution_data, AttributionData};
81-
use crate::ln::onion_utils::{HTLCFailReason, LocalHTLCFailureReason};
8284
use crate::ln::our_peer_storage::EncryptedOurPeerStorage;
8385
#[cfg(test)]
8486
use crate::ln::outbound_payment;
@@ -8002,8 +8004,27 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80028004
);
80038005
}
80048006

8005-
fn finalize_claims(&self, sources: Vec<HTLCSource>) {
8006-
self.pending_outbound_payments.finalize_claims(sources, &self.pending_events);
8007+
fn finalize_claims(&self, sources: Vec<(HTLCSource, Option<AttributionData>)>) {
8008+
// Decode attribution data to hold times.
8009+
let hold_times = sources.into_iter().filter_map(|(source, attribution_data)| {
8010+
if let HTLCSource::OutboundRoute { ref session_priv, ref path, .. } = source {
8011+
let hold_times = attribution_data.map(|attribution_data| {
8012+
decode_fulfill_attribution_data(
8013+
&self.secp_ctx,
8014+
&self.logger,
8015+
path,
8016+
session_priv,
8017+
attribution_data,
8018+
)
8019+
});
8020+
8021+
Some((source, hold_times))
8022+
} else {
8023+
None
8024+
}
8025+
});
8026+
8027+
self.pending_outbound_payments.finalize_claims(hold_times, &self.pending_events);
80078028
}
80088029

80098030
fn claim_funds_internal(
@@ -16708,15 +16729,15 @@ mod tests {
1670816729
let events = nodes[0].node.get_and_clear_pending_events();
1670916730
assert_eq!(events.len(), 2);
1671016731
match events[0] {
16711-
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
16732+
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path, .. } => {
1671216733
assert_eq!(payment_id, *actual_payment_id);
1671316734
assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
1671416735
assert_eq!(route.paths[0], *path);
1671516736
},
1671616737
_ => panic!("Unexpected event"),
1671716738
}
1671816739
match events[1] {
16719-
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
16740+
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path, ..} => {
1672016741
assert_eq!(payment_id, *actual_payment_id);
1672116742
assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
1672216743
assert_eq!(route.paths[0], *path);

lightning/src/ln/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ pub use onion_utils::{create_payment_onion, LocalHTLCFailureReason};
5252
// without the node parameter being mut. This is incorrect, and thus newer rustcs will complain
5353
// about an unnecessary mut. Thus, we silence the unused_mut warning in two test modules below.
5454

55+
#[cfg(fuzzing)]
56+
pub use onion_utils::decode_fulfill_attribution_data;
5557
#[cfg(fuzzing)]
5658
pub use onion_utils::process_onion_failure;
5759

0 commit comments

Comments
 (0)