Skip to content

Commit df8ed65

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

File tree

9 files changed

+194
-18
lines changed

9 files changed

+194
-18
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,13 +1818,19 @@ 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),
18231830
(2, payment_hash, option),
18241831
(4, path.hops, required_vec),
18251832
(6, path.blinded_tail, option),
1833+
(8, hold_times, option),
18261834
})
18271835
},
18281836
&Event::PaymentFailed { ref payment_id, ref payment_hash, ref reason } => {
@@ -2311,11 +2319,13 @@ impl MaybeReadable for Event {
23112319
(2, payment_hash, option),
23122320
(4, path, required_vec),
23132321
(6, blinded_tail, option),
2322+
(8, hold_times, option),
23142323
});
23152324
Ok(Some(Event::PaymentPathSuccessful {
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: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ impl OutboundHTLCState {
402402
enum OutboundHTLCOutcome {
403403
/// We started always filling in the preimages here in 0.0.105, and the requirement
404404
/// that the preimages always be filled in was added in 0.2.
405-
Success(PaymentPreimage, #[allow(dead_code)] Option<AttributionData>),
405+
Success(PaymentPreimage, Option<AttributionData>),
406406
Failure(HTLCFailReason),
407407
}
408408

@@ -1181,7 +1181,7 @@ pub(super) struct MonitorRestoreUpdates {
11811181
pub order: RAACommitmentOrder,
11821182
pub accepted_htlcs: Vec<(PendingHTLCInfo, u64)>,
11831183
pub failed_htlcs: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
1184-
pub finalized_claimed_htlcs: Vec<HTLCSource>,
1184+
pub finalized_claimed_htlcs: Vec<(HTLCSource, Option<AttributionData>)>,
11851185
pub pending_update_adds: Vec<msgs::UpdateAddHTLC>,
11861186
pub funding_broadcastable: Option<Transaction>,
11871187
pub channel_ready: Option<msgs::ChannelReady>,
@@ -2313,7 +2313,7 @@ where
23132313
// but need to handle this somehow or we run the risk of losing HTLCs!
23142314
monitor_pending_forwards: Vec<(PendingHTLCInfo, u64)>,
23152315
monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
2316-
monitor_pending_finalized_fulfills: Vec<HTLCSource>,
2316+
monitor_pending_finalized_fulfills: Vec<(HTLCSource, Option<AttributionData>)>,
23172317
monitor_pending_update_adds: Vec<msgs::UpdateAddHTLC>,
23182318
monitor_pending_tx_signatures: Option<msgs::TxSignatures>,
23192319

@@ -6749,7 +6749,7 @@ where
67496749

67506750
self.mark_outbound_htlc_removed(
67516751
msg.htlc_id,
6752-
OutboundHTLCOutcome::Success(msg.payment_preimage, None),
6752+
OutboundHTLCOutcome::Success(msg.payment_preimage, msg.attribution_data.clone()),
67536753
)
67546754
.map(|htlc| {
67556755
(htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat, htlc.send_timestamp)
@@ -7554,8 +7554,11 @@ where
75547554
});
75557555
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
75567556
},
7557-
OutboundHTLCOutcome::Success(..) => {
7558-
finalized_claimed_htlcs.push(htlc.source.clone());
7557+
OutboundHTLCOutcome::Success(_, attribution_data) => {
7558+
// Even though a fast track was taken for fulfilled HTLCs to the incoming side, we still
7559+
// pass along attribution data here so that we can include hold time information in the
7560+
// final PaymentPathSuccessful events.
7561+
finalized_claimed_htlcs.push((htlc.source.clone(), attribution_data));
75597562
// They fulfilled, so we sent them money
75607563
value_to_self_msat_diff -= htlc.amount_msat as i64;
75617564
},
@@ -8048,7 +8051,7 @@ where
80488051
&mut self, resend_raa: bool, resend_commitment: bool, resend_channel_ready: bool,
80498052
mut pending_forwards: Vec<(PendingHTLCInfo, u64)>,
80508053
mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
8051-
mut pending_finalized_claimed_htlcs: Vec<HTLCSource>,
8054+
mut pending_finalized_claimed_htlcs: Vec<(HTLCSource, Option<AttributionData>)>,
80528055
) {
80538056
self.context.monitor_pending_revoke_and_ack |= resend_raa;
80548057
self.context.monitor_pending_commitment_signed |= resend_commitment;

lightning/src/ln/channelmanager.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ use crate::ln::onion_payment::{
7878
};
7979
use crate::ln::onion_utils::{self};
8080
use crate::ln::onion_utils::{process_fulfill_attribution_data, AttributionData};
81-
use crate::ln::onion_utils::{HTLCFailReason, LocalHTLCFailureReason};
81+
use crate::ln::onion_utils::{process_onion_success, HTLCFailReason, LocalHTLCFailureReason};
8282
use crate::ln::our_peer_storage::EncryptedOurPeerStorage;
8383
#[cfg(test)]
8484
use crate::ln::outbound_payment;
@@ -8002,8 +8002,30 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80028002
);
80038003
}
80048004

8005-
fn finalize_claims(&self, sources: Vec<HTLCSource>) {
8006-
self.pending_outbound_payments.finalize_claims(sources, &self.pending_events);
8005+
fn finalize_claims(&self, sources: Vec<(HTLCSource, Option<AttributionData>)>) {
8006+
// Decode attribution data to hold times.
8007+
let hold_times = sources
8008+
.into_iter()
8009+
.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+
process_onion_success(
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+
.collect();
8027+
8028+
self.pending_outbound_payments.finalize_claims(hold_times, &self.pending_events);
80078029
}
80088030

80098031
fn claim_funds_internal(
@@ -16723,15 +16745,15 @@ mod tests {
1672316745
let events = nodes[0].node.get_and_clear_pending_events();
1672416746
assert_eq!(events.len(), 2);
1672516747
match events[0] {
16726-
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
16748+
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path, .. } => {
1672716749
assert_eq!(payment_id, *actual_payment_id);
1672816750
assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
1672916751
assert_eq!(route.paths[0], *path);
1673016752
},
1673116753
_ => panic!("Unexpected event"),
1673216754
}
1673316755
match events[1] {
16734-
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
16756+
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path, ..} => {
1673516757
assert_eq!(payment_id, *actual_payment_id);
1673616758
assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
1673716759
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
@@ -54,6 +54,8 @@ pub use onion_utils::{create_payment_onion, LocalHTLCFailureReason};
5454

5555
#[cfg(fuzzing)]
5656
pub use onion_utils::process_onion_failure;
57+
#[cfg(fuzzing)]
58+
pub use onion_utils::process_onion_success;
5759

5860
#[cfg(fuzzing)]
5961
pub use onion_utils::AttributionData;

0 commit comments

Comments
 (0)