]> git.bitcoin.ninja Git - rust-lightning/blob - lightning/src/ln/blinded_payment_tests.rs
Add an immediately-freeing `MonitorUpdateCompletionAction`.
[rust-lightning] / lightning / src / ln / blinded_payment_tests.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 use bitcoin::secp256k1::Secp256k1;
11 use crate::blinded_path::BlindedPath;
12 use crate::blinded_path::payment::{PaymentConstraints, ReceiveTlvs};
13 use crate::events::MessageSendEventsProvider;
14 use crate::ln::channelmanager;
15 use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
16 use crate::ln::features::Bolt12InvoiceFeatures;
17 use crate::ln::functional_test_utils::*;
18 use crate::ln::outbound_payment::Retry;
19 use crate::prelude::*;
20 use crate::routing::router::{PaymentParameters, RouteParameters};
21 use crate::util::config::UserConfig;
22
23 #[test]
24 fn one_hop_blinded_path() {
25         do_one_hop_blinded_path(true);
26         do_one_hop_blinded_path(false);
27 }
28
29 fn do_one_hop_blinded_path(success: bool) {
30         let chanmon_cfgs = create_chanmon_cfgs(2);
31         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
32         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
33         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
34         let chan_upd = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0).0.contents;
35
36         let amt_msat = 5000;
37         let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[1], Some(amt_msat), None);
38         let payee_tlvs = ReceiveTlvs {
39                 payment_secret,
40                 payment_constraints: PaymentConstraints {
41                         max_cltv_expiry: u32::max_value(),
42                         htlc_minimum_msat: chan_upd.htlc_minimum_msat,
43                 },
44         };
45         let mut secp_ctx = Secp256k1::new();
46         let blinded_path = BlindedPath::one_hop_for_payment(
47                 nodes[1].node.get_our_node_id(), payee_tlvs, &chanmon_cfgs[1].keys_manager, &secp_ctx
48         ).unwrap();
49
50         let route_params = RouteParameters::from_payment_params_and_value(
51                 PaymentParameters::blinded(vec![blinded_path]),
52                 amt_msat,
53         );
54         nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(),
55         PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
56         check_added_monitors(&nodes[0], 1);
57         pass_along_route(&nodes[0], &[&[&nodes[1]]], amt_msat, payment_hash, payment_secret);
58         if success {
59                 claim_payment(&nodes[0], &[&nodes[1]], payment_preimage);
60         } else {
61                 fail_payment(&nodes[0], &[&nodes[1]], payment_hash);
62         }
63 }
64
65 #[test]
66 fn mpp_to_one_hop_blinded_path() {
67         let chanmon_cfgs = create_chanmon_cfgs(4);
68         let node_cfgs = create_node_cfgs(4, &chanmon_cfgs);
69         let node_chanmgrs = create_node_chanmgrs(4, &node_cfgs, &[None, None, None, None]);
70         let nodes = create_network(4, &node_cfgs, &node_chanmgrs);
71         let mut secp_ctx = Secp256k1::new();
72
73         create_announced_chan_between_nodes(&nodes, 0, 1);
74         create_announced_chan_between_nodes(&nodes, 0, 2);
75         let chan_upd_1_3 = create_announced_chan_between_nodes(&nodes, 1, 3).0.contents;
76         create_announced_chan_between_nodes(&nodes, 2, 3).0.contents;
77
78         let amt_msat = 15_000_000;
79         let (payment_preimage, payment_hash, payment_secret) = get_payment_preimage_hash(&nodes[3], Some(amt_msat), None);
80         let payee_tlvs = ReceiveTlvs {
81                 payment_secret,
82                 payment_constraints: PaymentConstraints {
83                         max_cltv_expiry: u32::max_value(),
84                         htlc_minimum_msat: chan_upd_1_3.htlc_minimum_msat,
85                 },
86         };
87         let blinded_path = BlindedPath::one_hop_for_payment(
88                 nodes[3].node.get_our_node_id(), payee_tlvs, &chanmon_cfgs[3].keys_manager, &secp_ctx
89         ).unwrap();
90
91         let bolt12_features: Bolt12InvoiceFeatures =
92                 channelmanager::provided_invoice_features(&UserConfig::default()).to_context();
93         let route_params = RouteParameters::from_payment_params_and_value(
94                 PaymentParameters::blinded(vec![blinded_path])
95                         .with_bolt12_features(bolt12_features).unwrap(),
96                 amt_msat,
97         );
98         nodes[0].node.send_payment(payment_hash, RecipientOnionFields::spontaneous_empty(), PaymentId(payment_hash.0), route_params, Retry::Attempts(0)).unwrap();
99         check_added_monitors(&nodes[0], 2);
100
101         let expected_route: &[&[&Node]] = &[&[&nodes[1], &nodes[3]], &[&nodes[2], &nodes[3]]];
102         let mut events = nodes[0].node.get_and_clear_pending_msg_events();
103         assert_eq!(events.len(), 2);
104
105         let ev = remove_first_msg_event_to_node(&nodes[1].node.get_our_node_id(), &mut events);
106         pass_along_path(&nodes[0], expected_route[0], amt_msat, payment_hash.clone(),
107                 Some(payment_secret), ev.clone(), false, None);
108
109         let ev = remove_first_msg_event_to_node(&nodes[2].node.get_our_node_id(), &mut events);
110         pass_along_path(&nodes[0], expected_route[1], amt_msat, payment_hash.clone(),
111                 Some(payment_secret), ev.clone(), true, None);
112         claim_payment_along_route(&nodes[0], expected_route, false, payment_preimage);
113 }