Skip to content

Commit c440aed

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

File tree

9 files changed

+261
-25
lines changed

9 files changed

+261
-25
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.
@@ -1888,10 +1890,16 @@ impl Writeable for Event {
18881890
(4, funding_info, required),
18891891
})
18901892
},
1891-
&Event::PaymentPathSuccessful { ref payment_id, ref payment_hash, ref path } => {
1893+
&Event::PaymentPathSuccessful {
1894+
ref payment_id,
1895+
ref payment_hash,
1896+
ref path,
1897+
ref hold_times,
1898+
} => {
18921899
13u8.write(writer)?;
18931900
write_tlv_fields!(writer, {
18941901
(0, payment_id, required),
1902+
(1, hold_times, option),
18951903
(2, payment_hash, option),
18961904
(4, path.hops, required_vec),
18971905
(6, path.blinded_tail, option),
@@ -2391,6 +2399,7 @@ impl MaybeReadable for Event {
23912399
let mut f = || {
23922400
_init_and_read_len_prefixed_tlv_fields!(reader, {
23932401
(0, payment_id, required),
2402+
(1, hold_times, option),
23942403
(2, payment_hash, option),
23952404
(4, path, required_vec),
23962405
(6, blinded_tail, option),
@@ -2399,6 +2408,7 @@ impl MaybeReadable for Event {
23992408
payment_id: payment_id.0.unwrap(),
24002409
payment_hash,
24012410
path: Path { hops: path, blinded_tail },
2411+
hold_times,
24022412
}))
24032413
};
24042414
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: 35 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;
@@ -8032,8 +8034,36 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
80328034
);
80338035
}
80348036

8035-
fn finalize_claims(&self, sources: Vec<HTLCSource>) {
8036-
self.pending_outbound_payments.finalize_claims(sources, &self.pending_events);
8037+
fn finalize_claims(&self, sources: Vec<(HTLCSource, Option<AttributionData>)>) {
8038+
// Decode attribution data to hold times.
8039+
let hold_times = sources.into_iter().filter_map(|(source, attribution_data)| {
8040+
if let HTLCSource::OutboundRoute { ref session_priv, ref path, .. } = source {
8041+
// If the path has trampoline hops, we need to hash the session private key to get the outer session key.
8042+
let session_priv = if path.has_trampoline_hops() {
8043+
let session_priv_hash =
8044+
Sha256::hash(&session_priv.secret_bytes()).to_byte_array();
8045+
&SecretKey::from_slice(&session_priv_hash[..]).unwrap()
8046+
} else {
8047+
session_priv
8048+
};
8049+
8050+
let hold_times = attribution_data.map(|attribution_data| {
8051+
decode_fulfill_attribution_data(
8052+
&self.secp_ctx,
8053+
&self.logger,
8054+
path,
8055+
session_priv,
8056+
attribution_data,
8057+
)
8058+
});
8059+
8060+
Some((source, hold_times))
8061+
} else {
8062+
None
8063+
}
8064+
});
8065+
8066+
self.pending_outbound_payments.finalize_claims(hold_times, &self.pending_events);
80378067
}
80388068

80398069
fn claim_funds_internal(
@@ -16814,15 +16844,15 @@ mod tests {
1681416844
let events = nodes[0].node.get_and_clear_pending_events();
1681516845
assert_eq!(events.len(), 2);
1681616846
match events[0] {
16817-
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
16847+
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path, .. } => {
1681816848
assert_eq!(payment_id, *actual_payment_id);
1681916849
assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
1682016850
assert_eq!(route.paths[0], *path);
1682116851
},
1682216852
_ => panic!("Unexpected event"),
1682316853
}
1682416854
match events[1] {
16825-
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
16855+
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path, ..} => {
1682616856
assert_eq!(payment_id, *actual_payment_id);
1682716857
assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
1682816858
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)