Skip to content

Commit 9db962c

Browse files
committed
Add test for duplicate keysend payment
The logic has been changed around duplicate keysend payments such that it's no longer explicitly clear that we reject duplicate keysend payments now that we handle receiving multi-part keysends. This test catches that. Note that this also tests that we reject MPP keysends when our config states we should, and that we reject MPP keysends without payemnt secrets when our config states we support MPP keysends.
1 parent 8dde177 commit 9db962c

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8529,13 +8529,26 @@ mod tests {
85298529

85308530
#[test]
85318531
fn test_keysend_dup_payment_hash() {
8532+
do_test_keysend_dup_payment_hash(false);
8533+
do_test_keysend_dup_payment_hash(true);
8534+
}
8535+
8536+
fn do_test_keysend_dup_payment_hash(accept_mpp_keysend: bool) {
85328537
// (1): Test that a keysend payment with a duplicate payment hash to an existing pending
85338538
// outbound regular payment fails as expected.
85348539
// (2): Test that a regular payment with a duplicate payment hash to an existing keysend payment
85358540
// fails as expected.
8541+
// (3): Test that a keysend payment with a duplicate payment hash to an existing keysend
8542+
// payment fails as expected. When `accept_mpp_keysend` is false, this tests that we
8543+
// reject MPP keysend payments, since in this case where the payment has no payment
8544+
// secret, a keysend payment with a duplicate hash is basically an MPP keysend. If
8545+
// `accept_mpp_keysend` is true, this tests that we only accept MPP keysends with
8546+
// payment secrets and reject otherwise.
85368547
let chanmon_cfgs = create_chanmon_cfgs(2);
85378548
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
8538-
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
8549+
let mut mpp_keysend_cfg = test_default_channel_config();
8550+
mpp_keysend_cfg.accept_mpp_keysend = accept_mpp_keysend;
8551+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(mpp_keysend_cfg)]);
85398552
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
85408553
create_announced_chan_between_nodes(&nodes, 0, 1);
85418554
let scorer = test_utils::TestScorer::new();
@@ -8624,6 +8637,53 @@ mod tests {
86248637

86258638
// Finally, succeed the keysend payment.
86268639
claim_payment(&nodes[0], &expected_route, payment_preimage);
8640+
8641+
// To start (3), send a keysend payment but don't claim it.
8642+
let payment_id_1 = PaymentId([44; 32]);
8643+
let payment_hash = nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
8644+
RecipientOnionFields::spontaneous_empty(), payment_id_1).unwrap();
8645+
check_added_monitors!(nodes[0], 1);
8646+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8647+
assert_eq!(events.len(), 1);
8648+
let event = events.pop().unwrap();
8649+
let path = vec![&nodes[1]];
8650+
pass_along_path(&nodes[0], &path, 100_000, payment_hash, None, event, true, Some(payment_preimage));
8651+
8652+
// Next, attempt a keysend payment and make sure it fails.
8653+
let route_params = RouteParameters {
8654+
payment_params: PaymentParameters::for_keysend(expected_route.last().unwrap().node.get_our_node_id(), TEST_FINAL_CLTV, false),
8655+
final_value_msat: 100_000,
8656+
};
8657+
let route = find_route(
8658+
&nodes[0].node.get_our_node_id(), &route_params, &nodes[0].network_graph,
8659+
None, nodes[0].logger, &scorer, &(), &random_seed_bytes
8660+
).unwrap();
8661+
let payment_id_2 = PaymentId([45; 32]);
8662+
nodes[0].node.send_spontaneous_payment(&route, Some(payment_preimage),
8663+
RecipientOnionFields::spontaneous_empty(), payment_id_2).unwrap();
8664+
check_added_monitors!(nodes[0], 1);
8665+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
8666+
assert_eq!(events.len(), 1);
8667+
let ev = events.drain(..).next().unwrap();
8668+
let payment_event = SendEvent::from_event(ev);
8669+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
8670+
check_added_monitors!(nodes[1], 0);
8671+
commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
8672+
expect_pending_htlcs_forwardable!(nodes[1]);
8673+
expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::FailedPayment { payment_hash }]);
8674+
check_added_monitors!(nodes[1], 1);
8675+
let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
8676+
assert!(updates.update_add_htlcs.is_empty());
8677+
assert!(updates.update_fulfill_htlcs.is_empty());
8678+
assert_eq!(updates.update_fail_htlcs.len(), 1);
8679+
assert!(updates.update_fail_malformed_htlcs.is_empty());
8680+
assert!(updates.update_fee.is_none());
8681+
nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
8682+
commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, true, true);
8683+
expect_payment_failed!(nodes[0], payment_hash, true);
8684+
8685+
// Finally, claim the original payment.
8686+
claim_payment(&nodes[0], &expected_route, payment_preimage);
86278687
}
86288688

86298689
#[test]

0 commit comments

Comments
 (0)