Skip to content

Commit b05cc65

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

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
@@ -403,7 +403,7 @@ impl OutboundHTLCState {
403403
enum OutboundHTLCOutcome {
404404
/// We started always filling in the preimages here in 0.0.105, and the requirement
405405
/// that the preimages always be filled in was added in 0.2.
406-
Success(PaymentPreimage, #[allow(dead_code)] Option<AttributionData>),
406+
Success(PaymentPreimage, Option<AttributionData>),
407407
Failure(HTLCFailReason),
408408
}
409409

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

@@ -6720,7 +6720,8 @@ where
67206720
));
67216721
}
67226722

6723-
let outcome = OutboundHTLCOutcome::Success(msg.payment_preimage, None);
6723+
let outcome =
6724+
OutboundHTLCOutcome::Success(msg.payment_preimage, msg.attribution_data.clone());
67246725
self.mark_outbound_htlc_removed(msg.htlc_id, outcome).map(|htlc| {
67256726
(htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat, htlc.send_timestamp)
67266727
})
@@ -7543,16 +7544,21 @@ where
75437544
&htlc.payment_hash
75447545
);
75457546
// We really want take() here, but, again, non-mut ref :(
7546-
if let OutboundHTLCOutcome::Failure(mut reason) = outcome.clone() {
7547-
hold_time(htlc.send_timestamp, now).map(|hold_time| {
7548-
reason.set_hold_time(hold_time);
7549-
});
7550-
7551-
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
7552-
} else {
7553-
finalized_claimed_htlcs.push(htlc.source.clone());
7554-
// They fulfilled, so we sent them money
7555-
value_to_self_msat_diff -= htlc.amount_msat as i64;
7547+
match outcome.clone() {
7548+
OutboundHTLCOutcome::Failure(mut reason) => {
7549+
hold_time(htlc.send_timestamp, now).map(|hold_time| {
7550+
reason.set_hold_time(hold_time);
7551+
});
7552+
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
7553+
},
7554+
OutboundHTLCOutcome::Success(_, attribution_data) => {
7555+
// Even though a fast track was taken for fulfilled HTLCs to the incoming side, we still
7556+
// pass along attribution data here so that we can include hold time information in the
7557+
// final PaymentPathSuccessful events.
7558+
finalized_claimed_htlcs.push((htlc.source.clone(), attribution_data));
7559+
// They fulfilled, so we sent them money
7560+
value_to_self_msat_diff -= htlc.amount_msat as i64;
7561+
},
75567562
}
75577563
false
75587564
} else {
@@ -8042,7 +8048,7 @@ where
80428048
&mut self, resend_raa: bool, resend_commitment: bool, resend_channel_ready: bool,
80438049
mut pending_forwards: Vec<(PendingHTLCInfo, u64)>,
80448050
mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
8045-
mut pending_finalized_claimed_htlcs: Vec<HTLCSource>,
8051+
mut pending_finalized_claimed_htlcs: Vec<(HTLCSource, Option<AttributionData>)>,
80468052
) {
80478053
self.context.monitor_pending_revoke_and_ack |= resend_raa;
80488054
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)