Skip to content

Commit 9367528

Browse files
authored
Merge pull request #3801 from joostjager/fulfill-hold-times
2 parents 1e34de0 + ba52885 commit 9367528

16 files changed

+641
-205
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
@@ -2733,6 +2733,7 @@ mod tests {
27332733
payment_id: PaymentId([42; 32]),
27342734
payment_hash: None,
27352735
path: path.clone(),
2736+
hold_times: Vec::new(),
27362737
});
27372738
let event = $receive.expect("PaymentPathSuccessful not handled within deadline");
27382739
match event {

lightning/src/events/mod.rs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,12 @@ 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. The unit in which the hold times are expressed are 100's of
1106+
/// milliseconds. So a hop reporting 2 is a hold time that corresponds to roughly 200 milliseconds. As earlier
1107+
/// hops hold on to an HTLC for longer, the hold times in the list are expected to decrease. When our peer
1108+
/// didn't provide attribution data, the list is empty. The same applies to HTLCs that were resolved onchain.
1109+
/// Because of unavailability of hold times, the list may be shorter than the number of hops in the path.
1110+
hold_times: Vec<u32>,
11051111
},
11061112
/// Indicates an outbound HTLC we sent failed, likely due to an intermediary node being unable to
11071113
/// handle the HTLC.
@@ -1153,7 +1159,11 @@ pub enum Event {
11531159
error_code: Option<u16>,
11541160
#[cfg(any(test, feature = "_test_utils"))]
11551161
error_data: Option<Vec<u8>>,
1156-
#[cfg(any(test, feature = "_test_utils"))]
1162+
/// The hold times as reported by each hop. The unit in which the hold times are expressed are 100's of
1163+
/// milliseconds. So a hop reporting 2 is a hold time that corresponds to roughly 200 milliseconds. As earlier
1164+
/// hops hold on to an HTLC for longer, the hold times in the list are expected to decrease. When our peer
1165+
/// didn't provide attribution data, the list is empty. The same applies to HTLCs that were resolved onchain.
1166+
/// Because of unavailability of hold times, the list may be shorter than the number of hops in the path.
11571167
hold_times: Vec<u32>,
11581168
},
11591169
/// Indicates that a probe payment we sent returned successful, i.e., only failed at the destination.
@@ -1792,16 +1802,13 @@ impl Writeable for Event {
17921802
ref error_code,
17931803
#[cfg(any(test, feature = "_test_utils"))]
17941804
ref error_data,
1795-
#[cfg(any(test, feature = "_test_utils"))]
17961805
ref hold_times,
17971806
} => {
17981807
3u8.write(writer)?;
17991808
#[cfg(any(test, feature = "_test_utils"))]
18001809
error_code.write(writer)?;
18011810
#[cfg(any(test, feature = "_test_utils"))]
18021811
error_data.write(writer)?;
1803-
#[cfg(any(test, feature = "_test_utils"))]
1804-
hold_times.write(writer)?;
18051812
write_tlv_fields!(writer, {
18061813
(0, payment_hash, required),
18071814
(1, None::<NetworkUpdate>, option), // network_update in LDK versions prior to 0.0.114
@@ -1813,6 +1820,7 @@ impl Writeable for Event {
18131820
(9, None::<RouteParameters>, option), // retry in LDK versions prior to 0.0.115
18141821
(11, payment_id, option),
18151822
(13, failure, required),
1823+
(15, *hold_times, optional_vec),
18161824
});
18171825
},
18181826
&Event::PendingHTLCsForwardable { time_forwardable: _ } => {
@@ -1910,10 +1918,16 @@ impl Writeable for Event {
19101918
(4, funding_info, required),
19111919
})
19121920
},
1913-
&Event::PaymentPathSuccessful { ref payment_id, ref payment_hash, ref path } => {
1921+
&Event::PaymentPathSuccessful {
1922+
ref payment_id,
1923+
ref payment_hash,
1924+
ref path,
1925+
ref hold_times,
1926+
} => {
19141927
13u8.write(writer)?;
19151928
write_tlv_fields!(writer, {
19161929
(0, payment_id, required),
1930+
(1, *hold_times, optional_vec),
19171931
(2, payment_hash, option),
19181932
(4, path.hops, required_vec),
19191933
(6, path.blinded_tail, option),
@@ -2232,8 +2246,6 @@ impl MaybeReadable for Event {
22322246
let error_code = Readable::read(reader)?;
22332247
#[cfg(any(test, feature = "_test_utils"))]
22342248
let error_data = Readable::read(reader)?;
2235-
#[cfg(any(test, feature = "_test_utils"))]
2236-
let hold_times = Readable::read(reader)?;
22372249
let mut payment_hash = PaymentHash([0; 32]);
22382250
let mut payment_failed_permanently = false;
22392251
let mut network_update = None;
@@ -2242,6 +2254,7 @@ impl MaybeReadable for Event {
22422254
let mut short_channel_id = None;
22432255
let mut payment_id = None;
22442256
let mut failure_opt = None;
2257+
let mut hold_times = None;
22452258
read_tlv_fields!(reader, {
22462259
(0, payment_hash, required),
22472260
(1, network_update, upgradable_option),
@@ -2253,7 +2266,9 @@ impl MaybeReadable for Event {
22532266
(7, short_channel_id, option),
22542267
(11, payment_id, option),
22552268
(13, failure_opt, upgradable_option),
2269+
(15, hold_times, optional_vec),
22562270
});
2271+
let hold_times = hold_times.unwrap_or(Vec::new());
22572272
let failure =
22582273
failure_opt.unwrap_or_else(|| PathFailure::OnPath { network_update });
22592274
Ok(Some(Event::PaymentPathFailed {
@@ -2267,7 +2282,6 @@ impl MaybeReadable for Event {
22672282
error_code,
22682283
#[cfg(any(test, feature = "_test_utils"))]
22692284
error_data,
2270-
#[cfg(any(test, feature = "_test_utils"))]
22712285
hold_times,
22722286
}))
22732287
};
@@ -2413,14 +2427,19 @@ impl MaybeReadable for Event {
24132427
let mut f = || {
24142428
_init_and_read_len_prefixed_tlv_fields!(reader, {
24152429
(0, payment_id, required),
2430+
(1, hold_times, optional_vec),
24162431
(2, payment_hash, option),
24172432
(4, path, required_vec),
24182433
(6, blinded_tail, option),
24192434
});
2435+
2436+
let hold_times = hold_times.unwrap_or(Vec::new());
2437+
24202438
Ok(Some(Event::PaymentPathSuccessful {
24212439
payment_id: payment_id.0.unwrap(),
24222440
payment_hash,
24232441
path: Path { hops: path, blinded_tail },
2442+
hold_times,
24242443
}))
24252444
};
24262445
f()

lightning/src/ln/async_payments_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ fn async_receive_flow_success() {
607607
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev);
608608
let claimable_ev = do_pass_along_path(args).unwrap();
609609
let keysend_preimage = extract_payment_preimage(&claimable_ev);
610-
let res =
610+
let (res, _) =
611611
claim_payment_along_route(ClaimAlongRouteArgs::new(&nodes[0], route, keysend_preimage));
612612
assert_eq!(res, Some(PaidBolt12Invoice::StaticInvoice(static_invoice)));
613613
}
@@ -1726,7 +1726,7 @@ fn refresh_static_invoices() {
17261726
let claimable_ev = do_pass_along_path(args).unwrap();
17271727
let keysend_preimage = extract_payment_preimage(&claimable_ev);
17281728
let res = claim_payment_along_route(ClaimAlongRouteArgs::new(sender, route, keysend_preimage));
1729-
assert_eq!(res, Some(PaidBolt12Invoice::StaticInvoice(updated_invoice)));
1729+
assert_eq!(res.0, Some(PaidBolt12Invoice::StaticInvoice(updated_invoice)));
17301730
}
17311731

17321732
#[cfg_attr(feature = "std", ignore)]
@@ -2056,5 +2056,5 @@ fn invoice_server_is_not_channel_peer() {
20562056
let claimable_ev = do_pass_along_path(args).unwrap();
20572057
let keysend_preimage = extract_payment_preimage(&claimable_ev);
20582058
let res = claim_payment_along_route(ClaimAlongRouteArgs::new(sender, route, keysend_preimage));
2059-
assert_eq!(res, Some(PaidBolt12Invoice::StaticInvoice(invoice)));
2059+
assert_eq!(res.0, Some(PaidBolt12Invoice::StaticInvoice(invoice)));
20602060
}

lightning/src/ln/blinded_payment_tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,8 @@ fn test_trampoline_unblinded_receive() {
23412341
None,
23422342
).unwrap();
23432343

2344+
// Use a different session key to construct the replacement onion packet. Note that the sender isn't aware of
2345+
// this and won't be able to decode the fulfill hold times.
23442346
let outer_session_priv = secret_from_hex("e52c20461ed7acd46c4e7b591a37610519179482887bd73bf3b94617f8f03677");
23452347

23462348
let (outer_payloads, _, _) = onion_utils::build_onion_payloads(&route.paths[0], outer_total_msat, &recipient_onion_fields, outer_starting_htlc_offset, &None, None, Some(trampoline_packet)).unwrap();

lightning/src/ln/chanmon_update_fail_tests.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2890,8 +2890,12 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
28902890
as_raa = Some(get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, node_b_id));
28912891
}
28922892

2893-
let fulfill_msg =
2894-
msgs::UpdateFulfillHTLC { channel_id: chan_id_2, htlc_id: 0, payment_preimage };
2893+
let mut fulfill_msg = msgs::UpdateFulfillHTLC {
2894+
channel_id: chan_id_2,
2895+
htlc_id: 0,
2896+
payment_preimage,
2897+
attribution_data: None,
2898+
};
28952899
if second_fails {
28962900
nodes[2].node.fail_htlc_backwards(&payment_hash);
28972901
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(
@@ -2900,15 +2904,24 @@ fn do_test_reconnect_dup_htlc_claims(htlc_status: HTLCStatusAtDupClaim, second_f
29002904
);
29012905
check_added_monitors!(nodes[2], 1);
29022906
get_htlc_update_msgs!(nodes[2], node_b_id);
2907+
// Note that we don't populate fulfill_msg.attribution_data here, which will lead to hold times being
2908+
// unavailable.
29032909
} else {
29042910
nodes[2].node.claim_funds(payment_preimage);
29052911
check_added_monitors!(nodes[2], 1);
29062912
expect_payment_claimed!(nodes[2], payment_hash, 100_000);
29072913

29082914
let cs_updates = get_htlc_update_msgs!(nodes[2], node_b_id);
29092915
assert_eq!(cs_updates.update_fulfill_htlcs.len(), 1);
2910-
// Check that the message we're about to deliver matches the one generated:
2911-
assert_eq!(fulfill_msg, cs_updates.update_fulfill_htlcs[0]);
2916+
2917+
// Check that the message we're about to deliver matches the one generated. Ignore attribution data.
2918+
assert_eq!(fulfill_msg.channel_id, cs_updates.update_fulfill_htlcs[0].channel_id);
2919+
assert_eq!(fulfill_msg.htlc_id, cs_updates.update_fulfill_htlcs[0].htlc_id);
2920+
assert_eq!(
2921+
fulfill_msg.payment_preimage,
2922+
cs_updates.update_fulfill_htlcs[0].payment_preimage
2923+
);
2924+
fulfill_msg.attribution_data = cs_updates.update_fulfill_htlcs[0].attribution_data.clone();
29122925
}
29132926
nodes[1].node.handle_update_fulfill_htlc(node_c_id, &fulfill_msg);
29142927
expect_payment_forwarded!(nodes[1], nodes[0], nodes[2], Some(1000), false, false);

0 commit comments

Comments
 (0)