Skip to content

Commit 741ee1e

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

File tree

9 files changed

+263
-25
lines changed

9 files changed

+263
-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::decode_fulfill_attribution_data(
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
@@ -1102,6 +1102,8 @@ pub enum Event {
11021102
///
11031103
/// May contain a closed channel if the HTLC sent along the path was fulfilled on chain.
11041104
path: Path,
1105+
/// The hold times as reported by each hop.
1106+
hold_times: Option<Vec<u32>>,
11051107
},
11061108
/// Indicates an outbound HTLC we sent failed, likely due to an intermediary node being unable to
11071109
/// handle the HTLC.
@@ -1910,10 +1912,16 @@ impl Writeable for Event {
19101912
(4, funding_info, required),
19111913
})
19121914
},
1913-
&Event::PaymentPathSuccessful { ref payment_id, ref payment_hash, ref path } => {
1915+
&Event::PaymentPathSuccessful {
1916+
ref payment_id,
1917+
ref payment_hash,
1918+
ref path,
1919+
ref hold_times,
1920+
} => {
19141921
13u8.write(writer)?;
19151922
write_tlv_fields!(writer, {
19161923
(0, payment_id, required),
1924+
(1, hold_times, option),
19171925
(2, payment_hash, option),
19181926
(4, path.hops, required_vec),
19191927
(6, path.blinded_tail, option),
@@ -2413,6 +2421,7 @@ impl MaybeReadable for Event {
24132421
let mut f = || {
24142422
_init_and_read_len_prefixed_tlv_fields!(reader, {
24152423
(0, payment_id, required),
2424+
(1, hold_times, option),
24162425
(2, payment_hash, option),
24172426
(4, path, required_vec),
24182427
(6, blinded_tail, option),
@@ -2421,6 +2430,7 @@ impl MaybeReadable for Event {
24212430
payment_id: payment_id.0.unwrap(),
24222431
payment_hash,
24232432
path: Path { hops: path, blinded_tail },
2433+
hold_times,
24242434
}))
24252435
};
24262436
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>,
@@ -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

@@ -6695,7 +6695,8 @@ where
66956695
));
66966696
}
66976697

6698-
let outcome = OutboundHTLCOutcome::Success(msg.payment_preimage, None);
6698+
let outcome =
6699+
OutboundHTLCOutcome::Success(msg.payment_preimage, msg.attribution_data.clone());
66996700
self.mark_outbound_htlc_removed(msg.htlc_id, outcome).map(|htlc| {
67006701
(htlc.source.clone(), htlc.amount_msat, htlc.skimmed_fee_msat, htlc.send_timestamp)
67016702
})
@@ -7516,16 +7517,21 @@ where
75167517
&htlc.payment_hash
75177518
);
75187519
// We really want take() here, but, again, non-mut ref :(
7519-
if let OutboundHTLCOutcome::Failure(mut reason) = outcome.clone() {
7520-
hold_time(htlc.send_timestamp, now).map(|hold_time| {
7521-
reason.set_hold_time(hold_time);
7522-
});
7523-
7524-
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
7525-
} else {
7526-
finalized_claimed_htlcs.push(htlc.source.clone());
7527-
// They fulfilled, so we sent them money
7528-
value_to_self_msat_diff -= htlc.amount_msat as i64;
7520+
match outcome.clone() {
7521+
OutboundHTLCOutcome::Failure(mut reason) => {
7522+
hold_time(htlc.send_timestamp, now).map(|hold_time| {
7523+
reason.set_hold_time(hold_time);
7524+
});
7525+
revoked_htlcs.push((htlc.source.clone(), htlc.payment_hash, reason));
7526+
},
7527+
OutboundHTLCOutcome::Success(_, attribution_data) => {
7528+
// Even though a fast track was taken for fulfilled HTLCs to the incoming side, we still
7529+
// pass along attribution data here so that we can include hold time information in the
7530+
// final PaymentPathSuccessful events.
7531+
finalized_claimed_htlcs.push((htlc.source.clone(), attribution_data));
7532+
// They fulfilled, so we sent them money
7533+
value_to_self_msat_diff -= htlc.amount_msat as i64;
7534+
},
75297535
}
75307536
false
75317537
} else {
@@ -8008,7 +8014,7 @@ where
80088014
&mut self, resend_raa: bool, resend_commitment: bool, resend_channel_ready: bool,
80098015
mut pending_forwards: Vec<(PendingHTLCInfo, u64)>,
80108016
mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
8011-
mut pending_finalized_claimed_htlcs: Vec<HTLCSource>,
8017+
mut pending_finalized_claimed_htlcs: Vec<(HTLCSource, Option<AttributionData>)>,
80128018
) {
80138019
self.context.monitor_pending_revoke_and_ack |= resend_raa;
80148020
self.context.monitor_pending_commitment_signed |= resend_commitment;

lightning/src/ln/channelmanager.rs

Lines changed: 37 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;
@@ -7980,8 +7982,38 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
79807982
);
79817983
}
79827984

7983-
fn finalize_claims(&self, sources: Vec<HTLCSource>) {
7984-
self.pending_outbound_payments.finalize_claims(sources, &self.pending_events);
7985+
fn finalize_claims(&self, sources: Vec<(HTLCSource, Option<AttributionData>)>) {
7986+
// Decode attribution data to hold times.
7987+
let hold_times = sources.into_iter().filter_map(|(source, attribution_data)| {
7988+
if let HTLCSource::OutboundRoute { ref session_priv, ref path, .. } = source {
7989+
// If the path has trampoline hops, we need to hash the session private key to get the outer session key.
7990+
let derived_key;
7991+
let session_priv = if path.has_trampoline_hops() {
7992+
let session_priv_hash =
7993+
Sha256::hash(&session_priv.secret_bytes()).to_byte_array();
7994+
derived_key = SecretKey::from_slice(&session_priv_hash[..]).unwrap();
7995+
&derived_key
7996+
} else {
7997+
session_priv
7998+
};
7999+
8000+
let hold_times = attribution_data.map(|attribution_data| {
8001+
decode_fulfill_attribution_data(
8002+
&self.secp_ctx,
8003+
&self.logger,
8004+
path,
8005+
session_priv,
8006+
attribution_data,
8007+
)
8008+
});
8009+
8010+
Some((source, hold_times))
8011+
} else {
8012+
None
8013+
}
8014+
});
8015+
8016+
self.pending_outbound_payments.finalize_claims(hold_times, &self.pending_events);
79858017
}
79868018

79878019
fn claim_funds_internal(
@@ -16730,15 +16762,15 @@ mod tests {
1673016762
let events = nodes[0].node.get_and_clear_pending_events();
1673116763
assert_eq!(events.len(), 2);
1673216764
match events[0] {
16733-
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
16765+
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path, .. } => {
1673416766
assert_eq!(payment_id, *actual_payment_id);
1673516767
assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
1673616768
assert_eq!(route.paths[0], *path);
1673716769
},
1673816770
_ => panic!("Unexpected event"),
1673916771
}
1674016772
match events[1] {
16741-
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path } => {
16773+
Event::PaymentPathSuccessful { payment_id: ref actual_payment_id, ref payment_hash, ref path, ..} => {
1674216774
assert_eq!(payment_id, *actual_payment_id);
1674316775
assert_eq!(our_payment_hash, *payment_hash.as_ref().unwrap());
1674416776
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)