Skip to content

Commit 9a1354e

Browse files
Async payments tests: stop hardcoding keysend bytes
We're about to add a bunch more async payments tests, so take this opportunity to clean up the existing tests by no longer hardcoding the keysend payment preimage bytes ahead of time. This previously caused an MPP test to spuriously fail because all the session_privs were the same, and is generally not ideal. Also add a few comments to an existing test and a few more trivial cleanups.
1 parent 756977c commit 9a1354e

File tree

1 file changed

+43
-45
lines changed

1 file changed

+43
-45
lines changed

lightning/src/ln/async_payments_tests.rs

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use crate::blinded_path::message::{MessageContext, OffersContext};
1111
use crate::blinded_path::payment::PaymentContext;
1212
use crate::blinded_path::payment::{AsyncBolt12OfferContext, BlindedPaymentTlvs};
1313
use crate::chain::channelmonitor::{HTLC_FAIL_BACK_BUFFER, LATENCY_GRACE_PERIOD_BLOCKS};
14-
use crate::events::{Event, HTLCHandlingFailureType, PaidBolt12Invoice, PaymentFailureReason};
14+
use crate::events::{
15+
Event, HTLCHandlingFailureType, PaidBolt12Invoice, PaymentFailureReason, PaymentPurpose,
16+
};
1517
use crate::ln::blinded_payment_tests::{fail_blinded_htlc_backwards, get_blinded_route_parameters};
1618
use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
1719
use crate::ln::functional_test_utils::*;
@@ -128,6 +130,25 @@ fn create_static_invoice<T: secp256k1::Signing + secp256k1::Verification>(
128130
(offer, static_invoice)
129131
}
130132

133+
fn extract_payment_hash(event: &MessageSendEvent) -> PaymentHash {
134+
match event {
135+
MessageSendEvent::UpdateHTLCs { ref updates, .. } => {
136+
updates.update_add_htlcs[0].payment_hash
137+
},
138+
_ => panic!(),
139+
}
140+
}
141+
142+
fn extract_payment_preimage(event: &Event) -> PaymentPreimage {
143+
match event {
144+
Event::PaymentClaimable {
145+
purpose: PaymentPurpose::Bolt12OfferPayment { payment_preimage, .. },
146+
..
147+
} => payment_preimage.unwrap(),
148+
_ => panic!(),
149+
}
150+
}
151+
131152
#[test]
132153
fn invalid_keysend_payment_secret() {
133154
let chanmon_cfgs = create_chanmon_cfgs(3);
@@ -215,6 +236,7 @@ fn static_invoice_unknown_required_features() {
215236
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
216237
create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
217238

239+
// Manually construct a static invoice so we can set unknown required features.
218240
let blinded_paths_to_always_online_node = nodes[1]
219241
.message_router
220242
.create_blinded_paths(
@@ -237,6 +259,8 @@ fn static_invoice_unknown_required_features() {
237259
.build_and_sign(&secp_ctx)
238260
.unwrap();
239261

262+
// Initiate payment to the offer corresponding to the manually-constructed invoice that has
263+
// unknown required features.
240264
let amt_msat = 5000;
241265
let payment_id = PaymentId([1; 32]);
242266
let params = RouteParametersConfig::default();
@@ -264,6 +288,8 @@ fn static_invoice_unknown_required_features() {
264288
)
265289
.unwrap();
266290

291+
// Check that paying the static invoice fails as expected with
292+
// `PaymentFailureReason::UnknownRequiredFeatures`.
267293
let static_invoice_om = nodes[1]
268294
.onion_messenger
269295
.next_onion_message_for_peer(nodes[0].node.get_our_node_id())
@@ -404,12 +430,6 @@ fn async_receive_flow_success() {
404430
create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0);
405431
create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
406432

407-
// Set the random bytes so we can predict the payment preimage and hash.
408-
let hardcoded_random_bytes = [42; 32];
409-
let keysend_preimage = PaymentPreimage(hardcoded_random_bytes);
410-
let payment_hash: PaymentHash = keysend_preimage.into();
411-
*nodes[0].keys_manager.override_random_bytes.lock().unwrap() = Some(hardcoded_random_bytes);
412-
413433
let relative_expiry = Duration::from_secs(1000);
414434
let (offer, static_invoice) =
415435
create_static_invoice(&nodes[1], &nodes[2], Some(relative_expiry), &secp_ctx);
@@ -433,6 +453,7 @@ fn async_receive_flow_success() {
433453
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
434454
assert_eq!(events.len(), 1);
435455
let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
456+
let payment_hash = extract_payment_hash(&ev);
436457
check_added_monitors!(nodes[0], 1);
437458

438459
// Receiving a duplicate release_htlc message doesn't result in duplicate payment.
@@ -442,9 +463,9 @@ fn async_receive_flow_success() {
442463
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
443464

444465
let route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
445-
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev)
446-
.with_payment_preimage(keysend_preimage);
447-
do_pass_along_path(args);
466+
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev);
467+
let claimable_ev = do_pass_along_path(args).unwrap();
468+
let keysend_preimage = extract_payment_preimage(&claimable_ev);
448469
let res =
449470
claim_payment_along_route(ClaimAlongRouteArgs::new(&nodes[0], route, keysend_preimage));
450471
assert!(res.is_some());
@@ -556,9 +577,6 @@ fn async_receive_mpp() {
556577

557578
let (offer, static_invoice) = create_static_invoice(&nodes[1], &nodes[3], None, &secp_ctx);
558579

559-
// In other tests we hardcode the sender's random bytes so we can predict the keysend preimage to
560-
// check later in the test, but that doesn't work for MPP because it causes the session_privs for
561-
// the different MPP parts to not be unique.
562580
let amt_msat = 15_000_000;
563581
let payment_id = PaymentId([1; 32]);
564582
let params = RouteParametersConfig::default();
@@ -593,8 +611,8 @@ fn async_receive_mpp() {
593611
let args = PassAlongPathArgs::new(&nodes[0], expected_route[1], amt_msat, payment_hash, ev);
594612
let claimable_ev = do_pass_along_path(args).unwrap();
595613
let keysend_preimage = match claimable_ev {
596-
crate::events::Event::PaymentClaimable {
597-
purpose: crate::events::PaymentPurpose::Bolt12OfferPayment { payment_preimage, .. },
614+
Event::PaymentClaimable {
615+
purpose: PaymentPurpose::Bolt12OfferPayment { payment_preimage, .. },
598616
..
599617
} => payment_preimage.unwrap(),
600618
_ => panic!(),
@@ -643,13 +661,6 @@ fn amount_doesnt_match_invreq() {
643661
connect_blocks(&nodes[3], 4 * CHAN_CONFIRM_DEPTH + 1 - nodes[3].best_block_info().1);
644662

645663
let (offer, static_invoice) = create_static_invoice(&nodes[1], &nodes[3], None, &secp_ctx);
646-
647-
// Set the random bytes so we can predict the payment preimage and hash.
648-
let hardcoded_random_bytes = [42; 32];
649-
let keysend_preimage = PaymentPreimage(hardcoded_random_bytes);
650-
let payment_hash: PaymentHash = keysend_preimage.into();
651-
*nodes[0].keys_manager.override_random_bytes.lock().unwrap() = Some(hardcoded_random_bytes);
652-
653664
let amt_msat = 5000;
654665
let payment_id = PaymentId([1; 32]);
655666
let params = RouteParametersConfig::default();
@@ -696,10 +707,10 @@ fn amount_doesnt_match_invreq() {
696707
let mut ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
697708
assert!(matches!(
698709
ev, MessageSendEvent::UpdateHTLCs { ref updates, .. } if updates.update_add_htlcs.len() == 1));
710+
let payment_hash = extract_payment_hash(&ev);
699711

700712
let route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]]];
701713
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev)
702-
.with_payment_preimage(keysend_preimage)
703714
.without_claimable_event()
704715
.expect_failure(HTLCHandlingFailureType::Receive { payment_hash });
705716
do_pass_along_path(args);
@@ -725,9 +736,9 @@ fn amount_doesnt_match_invreq() {
725736
ev, MessageSendEvent::UpdateHTLCs { ref updates, .. } if updates.update_add_htlcs.len() == 1));
726737
check_added_monitors!(nodes[0], 1);
727738
let route: &[&[&Node]] = &[&[&nodes[2], &nodes[3]]];
728-
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev)
729-
.with_payment_preimage(keysend_preimage);
730-
do_pass_along_path(args);
739+
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev);
740+
let claimable_ev = do_pass_along_path(args).unwrap();
741+
let keysend_preimage = extract_payment_preimage(&claimable_ev);
731742
claim_payment_along_route(ClaimAlongRouteArgs::new(&nodes[0], route, keysend_preimage));
732743
}
733744

@@ -882,12 +893,6 @@ fn invalid_async_receive_with_retry<F1, F2>(
882893
.build_and_sign(&secp_ctx)
883894
.unwrap();
884895

885-
// Set the random bytes so we can predict the payment preimage and hash.
886-
let hardcoded_random_bytes = [42; 32];
887-
let keysend_preimage = PaymentPreimage(hardcoded_random_bytes);
888-
let payment_hash: PaymentHash = keysend_preimage.into();
889-
*nodes[0].keys_manager.override_random_bytes.lock().unwrap() = Some(hardcoded_random_bytes);
890-
891896
let params = RouteParametersConfig::default();
892897
nodes[0]
893898
.node
@@ -906,10 +911,10 @@ fn invalid_async_receive_with_retry<F1, F2>(
906911
let mut ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
907912
assert!(matches!(
908913
ev, MessageSendEvent::UpdateHTLCs { ref updates, .. } if updates.update_add_htlcs.len() == 1));
914+
let payment_hash = extract_payment_hash(&ev);
909915

910916
let route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
911-
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev)
912-
.with_payment_preimage(keysend_preimage);
917+
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev);
913918
do_pass_along_path(args);
914919

915920
// Fail the HTLC backwards to enable us to more easily modify the now-Retryable outbound to test
@@ -935,7 +940,6 @@ fn invalid_async_receive_with_retry<F1, F2>(
935940
check_added_monitors!(nodes[0], 1);
936941
let route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
937942
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev)
938-
.with_payment_preimage(keysend_preimage)
939943
.without_claimable_event()
940944
.expect_failure(HTLCHandlingFailureType::Receive { payment_hash });
941945
do_pass_along_path(args);
@@ -949,9 +953,9 @@ fn invalid_async_receive_with_retry<F1, F2>(
949953
let mut ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
950954
check_added_monitors!(nodes[0], 1);
951955
let route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
952-
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev)
953-
.with_payment_preimage(keysend_preimage);
954-
do_pass_along_path(args);
956+
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev);
957+
let claimable_ev = do_pass_along_path(args).unwrap();
958+
let keysend_preimage = extract_payment_preimage(&claimable_ev);
955959
claim_payment_along_route(ClaimAlongRouteArgs::new(&nodes[0], route, keysend_preimage));
956960
}
957961

@@ -1031,12 +1035,6 @@ fn expired_static_invoice_payment_path() {
10311035
connect_blocks(&nodes[1], node_max_height - nodes[1].best_block_info().1);
10321036
connect_blocks(&nodes[2], node_max_height - nodes[2].best_block_info().1);
10331037

1034-
// Set the random bytes so we can predict the payment preimage and hash.
1035-
let hardcoded_random_bytes = [42; 32];
1036-
let keysend_preimage = PaymentPreimage(hardcoded_random_bytes);
1037-
let payment_hash: PaymentHash = keysend_preimage.into();
1038-
*nodes[0].keys_manager.override_random_bytes.lock().unwrap() = Some(hardcoded_random_bytes);
1039-
10401038
// Hardcode the blinded payment path returned by the router so we can expire it via mining blocks.
10411039
let (_, static_invoice_expired_paths) =
10421040
create_static_invoice(&nodes[1], &nodes[2], None, &secp_ctx);
@@ -1097,11 +1095,11 @@ fn expired_static_invoice_payment_path() {
10971095
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10981096
assert_eq!(events.len(), 1);
10991097
let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
1098+
let payment_hash = extract_payment_hash(&ev);
11001099
check_added_monitors!(nodes[0], 1);
11011100

11021101
let route: &[&[&Node]] = &[&[&nodes[1], &nodes[2]]];
11031102
let args = PassAlongPathArgs::new(&nodes[0], route[0], amt_msat, payment_hash, ev)
1104-
.with_payment_preimage(keysend_preimage)
11051103
.without_claimable_event()
11061104
.expect_failure(HTLCHandlingFailureType::Receive { payment_hash });
11071105
do_pass_along_path(args);

0 commit comments

Comments
 (0)