Prefer well-connected nodes for introduction nodes
[rust-lightning] / lightning / src / ln / offers_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 //! Functional tests for the BOLT 12 Offers payment flow.
11 //!
12 //! [`ChannelManager`] provides utilities to create [`Offer`]s and [`Refund`]s along with utilities
13 //! to initiate and request payment for them, respectively. It also manages the payment flow via
14 //! implementing [`OffersMessageHandler`]. This module tests that functionality, including the
15 //! resulting [`Event`] generation.
16 //!
17 //! Two-node success tests use an announced channel:
18 //!
19 //! Alice --- Bob
20 //!
21 //! While two-node failure tests use an unannounced channel:
22 //!
23 //! Alice ... Bob
24 //!
25 //! Six-node tests use unannounced channels for the sender and recipient and announced channels for
26 //! the rest of the network.
27 //!
28 //!               nodes[4]
29 //!              /        \
30 //!             /          \
31 //!            /            \
32 //! Alice ... Bob -------- Charlie ... David
33 //!            \            /
34 //!             \          /
35 //!              \        /
36 //!               nodes[5]
37 //!
38 //! Unnamed nodes are needed to ensure unannounced nodes can create two-hop blinded paths.
39 //!
40 //! Nodes without channels are disconnected and connected as needed to ensure that deterministic
41 //! blinded paths are used.
42
43 use core::time::Duration;
44 use crate::blinded_path::BlindedPath;
45 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
46 use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
47 use crate::ln::functional_test_utils::*;
48 use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
49 use crate::offers::invoice::Bolt12Invoice;
50 use crate::offers::invoice_error::InvoiceError;
51 use crate::offers::invoice_request::InvoiceRequest;
52 use crate::offers::parse::Bolt12SemanticError;
53 use crate::onion_message::messenger::PeeledOnion;
54 use crate::onion_message::offers::OffersMessage;
55 use crate::onion_message::packet::ParsedOnionMessageContents;
56 use crate::routing::gossip::{NodeAlias, NodeId};
57 use crate::sign::{NodeSigner, Recipient};
58
59 use crate::prelude::*;
60
61 macro_rules! expect_recent_payment {
62         ($node: expr, $payment_state: path, $payment_id: expr) => {
63                 match $node.node.list_recent_payments().first() {
64                         Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
65                                 assert_eq!($payment_id, actual_payment_id);
66                         },
67                         Some(_) => panic!("Unexpected recent payment state"),
68                         None => panic!("No recent payments"),
69                 }
70         }
71 }
72
73 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
74         let node_id_a = node_a.node.get_our_node_id();
75         let node_id_b = node_b.node.get_our_node_id();
76
77         let init_a = Init {
78                 features: node_a.init_features(&node_id_b),
79                 networks: None,
80                 remote_network_address: None,
81         };
82         let init_b = Init {
83                 features: node_b.init_features(&node_id_a),
84                 networks: None,
85                 remote_network_address: None,
86         };
87
88         node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
89         node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
90         node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
91         node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
92 }
93
94 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
95         for node_b in peers {
96                 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
97                 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
98                 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
99                 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
100         }
101 }
102
103 fn announce_node_address<'a, 'b, 'c>(
104         node: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>], address: SocketAddress,
105 ) {
106         let features = node.onion_messenger.provided_node_features()
107                 | node.gossip_sync.provided_node_features();
108         let rgb = [0u8; 3];
109         let announcement = UnsignedNodeAnnouncement {
110                 features,
111                 timestamp: 1000,
112                 node_id: NodeId::from_pubkey(&node.keys_manager.get_node_id(Recipient::Node).unwrap()),
113                 rgb,
114                 alias: NodeAlias([0u8; 32]),
115                 addresses: vec![address],
116                 excess_address_data: Vec::new(),
117                 excess_data: Vec::new(),
118         };
119         let signature = node.keys_manager.sign_gossip_message(
120                 UnsignedGossipMessage::NodeAnnouncement(&announcement)
121         ).unwrap();
122
123         let msg = NodeAnnouncement {
124                 signature,
125                 contents: announcement
126         };
127
128         node.gossip_sync.handle_node_announcement(&msg).unwrap();
129         for peer in peers {
130                 peer.gossip_sync.handle_node_announcement(&msg).unwrap();
131         }
132 }
133
134 fn route_bolt12_payment<'a, 'b, 'c>(
135         node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
136 ) {
137         // Monitor added when handling the invoice onion message.
138         check_added_monitors(node, 1);
139
140         let mut events = node.node.get_and_clear_pending_msg_events();
141         assert_eq!(events.len(), 1);
142         let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
143
144         // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
145         // invoice contains the payment_hash but it was encrypted inside an onion message.
146         let amount_msats = invoice.amount_msats();
147         let payment_hash = invoice.payment_hash();
148         do_pass_along_path(
149                 node, path, amount_msats, payment_hash, None, ev, false, false, None, false
150         );
151 }
152
153 fn claim_bolt12_payment<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>]) {
154         let recipient = &path[path.len() - 1];
155         match get_event!(recipient, Event::PaymentClaimable) {
156                 Event::PaymentClaimable {
157                         purpose: PaymentPurpose::InvoicePayment {
158                                 payment_preimage: Some(payment_preimage), ..
159                         }, ..
160                 } => claim_payment(node, path, payment_preimage),
161                 _ => panic!(),
162         };
163 }
164
165 fn extract_invoice_request<'a, 'b, 'c>(
166         node: &Node<'a, 'b, 'c>, message: &OnionMessage
167 ) -> (InvoiceRequest, Option<BlindedPath>) {
168         match node.onion_messenger.peel_onion_message(message) {
169                 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
170                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
171                                 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
172                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
173                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
174                         },
175                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
176                 },
177                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
178                 Err(e) => panic!("Failed to process onion message {:?}", e),
179         }
180 }
181
182 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
183         match node.onion_messenger.peel_onion_message(message) {
184                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
185                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
186                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
187                                 OffersMessage::Invoice(invoice) => invoice,
188                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
189                         },
190                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
191                 },
192                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
193                 Err(e) => panic!("Failed to process onion message {:?}", e),
194         }
195 }
196
197 fn extract_invoice_error<'a, 'b, 'c>(
198         node: &Node<'a, 'b, 'c>, message: &OnionMessage
199 ) -> InvoiceError {
200         match node.onion_messenger.peel_onion_message(message) {
201                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
202                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
203                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
204                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
205                                 OffersMessage::InvoiceError(error) => error,
206                         },
207                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
208                 },
209                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
210                 Err(e) => panic!("Failed to process onion message {:?}", e),
211         }
212 }
213
214 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
215 #[test]
216 fn prefers_non_tor_nodes_in_blinded_paths() {
217         let mut accept_forward_cfg = test_default_channel_config();
218         accept_forward_cfg.accept_forwards_to_priv_channels = true;
219
220         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
221         features.set_onion_messages_optional();
222         features.set_route_blinding_optional();
223
224         let chanmon_cfgs = create_chanmon_cfgs(6);
225         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
226
227         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
228
229         let node_chanmgrs = create_node_chanmgrs(
230                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
231         );
232         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
233
234         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
235         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
236         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
237         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
238         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
239         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
240         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
241
242         // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
243         create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
244
245         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
246         let bob_id = bob.node.get_our_node_id();
247         let charlie_id = charlie.node.get_our_node_id();
248
249         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
250         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
251
252         let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
253         announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor);
254
255         let offer = bob.node
256                 .create_offer_builder("coffee".to_string()).unwrap()
257                 .amount_msats(10_000_000)
258                 .build().unwrap();
259         assert_ne!(offer.signing_pubkey(), bob_id);
260         assert!(!offer.paths().is_empty());
261         for path in offer.paths() {
262                 assert_ne!(path.introduction_node_id, charlie_id);
263         }
264 }
265
266 /// Checks that blinded paths prefer an introduction node that is the most connected.
267 #[test]
268 fn prefers_more_connected_nodes_in_blinded_paths() {
269         let mut accept_forward_cfg = test_default_channel_config();
270         accept_forward_cfg.accept_forwards_to_priv_channels = true;
271
272         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
273         features.set_onion_messages_optional();
274         features.set_route_blinding_optional();
275
276         let chanmon_cfgs = create_chanmon_cfgs(6);
277         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
278
279         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
280
281         let node_chanmgrs = create_node_chanmgrs(
282                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
283         );
284         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
285
286         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
287         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
288         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
289         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
290         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
291         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
292         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
293
294         // Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
295         // more than the others.
296         create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
297         create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
298
299         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
300         let bob_id = bob.node.get_our_node_id();
301
302         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
303         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
304
305         let offer = bob.node
306                 .create_offer_builder("coffee".to_string()).unwrap()
307                 .amount_msats(10_000_000)
308                 .build().unwrap();
309         assert_ne!(offer.signing_pubkey(), bob_id);
310         assert!(!offer.paths().is_empty());
311         for path in offer.paths() {
312                 assert_eq!(path.introduction_node_id, nodes[4].node.get_our_node_id());
313         }
314 }
315
316 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
317 /// rather than exposing a node's pubkey.
318 #[test]
319 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
320         let mut accept_forward_cfg = test_default_channel_config();
321         accept_forward_cfg.accept_forwards_to_priv_channels = true;
322
323         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
324         features.set_onion_messages_optional();
325         features.set_route_blinding_optional();
326
327         let chanmon_cfgs = create_chanmon_cfgs(6);
328         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
329
330         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
331
332         let node_chanmgrs = create_node_chanmgrs(
333                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
334         );
335         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
336
337         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
338         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
339         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
340         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
341         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
342         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
343         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
344
345         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
346         let alice_id = alice.node.get_our_node_id();
347         let bob_id = bob.node.get_our_node_id();
348         let charlie_id = charlie.node.get_our_node_id();
349         let david_id = david.node.get_our_node_id();
350
351         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
352         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
353
354         let offer = alice.node
355                 .create_offer_builder("coffee".to_string()).unwrap()
356                 .amount_msats(10_000_000)
357                 .build().unwrap();
358         assert_ne!(offer.signing_pubkey(), alice_id);
359         assert!(!offer.paths().is_empty());
360         for path in offer.paths() {
361                 assert_eq!(path.introduction_node_id, bob_id);
362         }
363
364         let payment_id = PaymentId([1; 32]);
365         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
366                 .unwrap();
367         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
368
369         connect_peers(david, bob);
370
371         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
372         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
373
374         connect_peers(alice, charlie);
375
376         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
377         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
378
379         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
380         assert_eq!(invoice_request.amount_msats(), None);
381         assert_ne!(invoice_request.payer_id(), david_id);
382         assert_eq!(reply_path.unwrap().introduction_node_id, charlie_id);
383
384         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
385         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
386
387         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
388         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
389
390         let invoice = extract_invoice(david, &onion_message);
391         assert_eq!(invoice.amount_msats(), 10_000_000);
392         assert_ne!(invoice.signing_pubkey(), alice_id);
393         assert!(!invoice.payment_paths().is_empty());
394         for (_, path) in invoice.payment_paths() {
395                 assert_eq!(path.introduction_node_id, bob_id);
396         }
397
398         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
399         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
400
401         claim_bolt12_payment(david, &[charlie, bob, alice]);
402         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
403 }
404
405 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
406 /// rather than exposing a node's pubkey.
407 #[test]
408 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
409         let mut accept_forward_cfg = test_default_channel_config();
410         accept_forward_cfg.accept_forwards_to_priv_channels = true;
411
412         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
413         features.set_onion_messages_optional();
414         features.set_route_blinding_optional();
415
416         let chanmon_cfgs = create_chanmon_cfgs(6);
417         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
418
419         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
420
421         let node_chanmgrs = create_node_chanmgrs(
422                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
423         );
424         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
425
426         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
427         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
428         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
429         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
430         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
431         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
432         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
433
434         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
435         let alice_id = alice.node.get_our_node_id();
436         let bob_id = bob.node.get_our_node_id();
437         let charlie_id = charlie.node.get_our_node_id();
438         let david_id = david.node.get_our_node_id();
439
440         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
441         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
442
443         let absolute_expiry = Duration::from_secs(u64::MAX);
444         let payment_id = PaymentId([1; 32]);
445         let refund = david.node
446                 .create_refund_builder(
447                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
448                 )
449                 .unwrap()
450                 .build().unwrap();
451         assert_eq!(refund.amount_msats(), 10_000_000);
452         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
453         assert_ne!(refund.payer_id(), david_id);
454         assert!(!refund.paths().is_empty());
455         for path in refund.paths() {
456                 assert_eq!(path.introduction_node_id, charlie_id);
457         }
458         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
459
460         alice.node.request_refund_payment(&refund).unwrap();
461
462         connect_peers(alice, charlie);
463
464         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
465         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
466
467         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
468         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
469
470         let invoice = extract_invoice(david, &onion_message);
471         assert_eq!(invoice.amount_msats(), 10_000_000);
472         assert_ne!(invoice.signing_pubkey(), alice_id);
473         assert!(!invoice.payment_paths().is_empty());
474         for (_, path) in invoice.payment_paths() {
475                 assert_eq!(path.introduction_node_id, bob_id);
476         }
477
478         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
479         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
480
481         claim_bolt12_payment(david, &[charlie, bob, alice]);
482         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
483 }
484
485 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
486 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
487 /// introduction node of the blinded path.
488 #[test]
489 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
490         let chanmon_cfgs = create_chanmon_cfgs(2);
491         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
492         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
493         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
494
495         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
496
497         let alice = &nodes[0];
498         let alice_id = alice.node.get_our_node_id();
499         let bob = &nodes[1];
500         let bob_id = bob.node.get_our_node_id();
501
502         let offer = alice.node
503                 .create_offer_builder("coffee".to_string()).unwrap()
504                 .amount_msats(10_000_000)
505                 .build().unwrap();
506         assert_ne!(offer.signing_pubkey(), alice_id);
507         assert!(!offer.paths().is_empty());
508         for path in offer.paths() {
509                 assert_eq!(path.introduction_node_id, alice_id);
510         }
511
512         let payment_id = PaymentId([1; 32]);
513         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
514         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
515
516         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
517         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
518
519         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
520         assert_eq!(invoice_request.amount_msats(), None);
521         assert_ne!(invoice_request.payer_id(), bob_id);
522         assert_eq!(reply_path.unwrap().introduction_node_id, bob_id);
523
524         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
525         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
526
527         let invoice = extract_invoice(bob, &onion_message);
528         assert_eq!(invoice.amount_msats(), 10_000_000);
529         assert_ne!(invoice.signing_pubkey(), alice_id);
530         assert!(!invoice.payment_paths().is_empty());
531         for (_, path) in invoice.payment_paths() {
532                 assert_eq!(path.introduction_node_id, alice_id);
533         }
534
535         route_bolt12_payment(bob, &[alice], &invoice);
536         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
537
538         claim_bolt12_payment(bob, &[alice]);
539         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
540 }
541
542 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
543 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
544 /// introduction node of the blinded path.
545 #[test]
546 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
547         let chanmon_cfgs = create_chanmon_cfgs(2);
548         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
549         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
550         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
551
552         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
553
554         let alice = &nodes[0];
555         let alice_id = alice.node.get_our_node_id();
556         let bob = &nodes[1];
557         let bob_id = bob.node.get_our_node_id();
558
559         let absolute_expiry = Duration::from_secs(u64::MAX);
560         let payment_id = PaymentId([1; 32]);
561         let refund = bob.node
562                 .create_refund_builder(
563                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
564                 )
565                 .unwrap()
566                 .build().unwrap();
567         assert_eq!(refund.amount_msats(), 10_000_000);
568         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
569         assert_ne!(refund.payer_id(), bob_id);
570         assert!(!refund.paths().is_empty());
571         for path in refund.paths() {
572                 assert_eq!(path.introduction_node_id, bob_id);
573         }
574         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
575
576         alice.node.request_refund_payment(&refund).unwrap();
577
578         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
579         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
580
581         let invoice = extract_invoice(bob, &onion_message);
582         assert_eq!(invoice.amount_msats(), 10_000_000);
583         assert_ne!(invoice.signing_pubkey(), alice_id);
584         assert!(!invoice.payment_paths().is_empty());
585         for (_, path) in invoice.payment_paths() {
586                 assert_eq!(path.introduction_node_id, alice_id);
587         }
588
589         route_bolt12_payment(bob, &[alice], &invoice);
590         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
591
592         claim_bolt12_payment(bob, &[alice]);
593         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
594 }
595
596 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
597 /// the requested is sent directly using the node's pubkey, the response and the payment still use
598 /// blinded paths as required by the spec.
599 #[test]
600 fn pays_for_offer_without_blinded_paths() {
601         let chanmon_cfgs = create_chanmon_cfgs(2);
602         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
603         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
604         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
605
606         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
607
608         let alice = &nodes[0];
609         let alice_id = alice.node.get_our_node_id();
610         let bob = &nodes[1];
611         let bob_id = bob.node.get_our_node_id();
612
613         let offer = alice.node
614                 .create_offer_builder("coffee".to_string()).unwrap()
615                 .clear_paths()
616                 .amount_msats(10_000_000)
617                 .build().unwrap();
618         assert_eq!(offer.signing_pubkey(), alice_id);
619         assert!(offer.paths().is_empty());
620
621         let payment_id = PaymentId([1; 32]);
622         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
623         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
624
625         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
626         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
627
628         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
629         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
630
631         let invoice = extract_invoice(bob, &onion_message);
632         route_bolt12_payment(bob, &[alice], &invoice);
633         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
634
635         claim_bolt12_payment(bob, &[alice]);
636         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
637 }
638
639 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
640 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
641 #[test]
642 fn pays_for_refund_without_blinded_paths() {
643         let chanmon_cfgs = create_chanmon_cfgs(2);
644         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
645         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
646         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
647
648         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
649
650         let alice = &nodes[0];
651         let alice_id = alice.node.get_our_node_id();
652         let bob = &nodes[1];
653         let bob_id = bob.node.get_our_node_id();
654
655         let absolute_expiry = Duration::from_secs(u64::MAX);
656         let payment_id = PaymentId([1; 32]);
657         let refund = bob.node
658                 .create_refund_builder(
659                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
660                 )
661                 .unwrap()
662                 .clear_paths()
663                 .build().unwrap();
664         assert_eq!(refund.payer_id(), bob_id);
665         assert!(refund.paths().is_empty());
666         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
667
668         alice.node.request_refund_payment(&refund).unwrap();
669
670         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
671         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
672
673         let invoice = extract_invoice(bob, &onion_message);
674         route_bolt12_payment(bob, &[alice], &invoice);
675         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
676
677         claim_bolt12_payment(bob, &[alice]);
678         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
679 }
680
681 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
682 #[test]
683 fn fails_creating_offer_without_blinded_paths() {
684         let chanmon_cfgs = create_chanmon_cfgs(2);
685         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
686         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
687         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
688
689         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
690
691         match nodes[0].node.create_offer_builder("coffee".to_string()) {
692                 Ok(_) => panic!("Expected error"),
693                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
694         }
695 }
696
697 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
698 #[test]
699 fn fails_creating_refund_without_blinded_paths() {
700         let chanmon_cfgs = create_chanmon_cfgs(2);
701         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
702         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
703         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
704
705         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
706
707         let absolute_expiry = Duration::from_secs(u64::MAX);
708         let payment_id = PaymentId([1; 32]);
709
710         match nodes[0].node.create_refund_builder(
711                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
712         ) {
713                 Ok(_) => panic!("Expected error"),
714                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
715         }
716
717         assert!(nodes[0].node.list_recent_payments().is_empty());
718 }
719
720 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
721 /// the node's id.
722 #[test]
723 fn fails_creating_invoice_request_without_blinded_reply_path() {
724         let chanmon_cfgs = create_chanmon_cfgs(6);
725         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
726         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
727         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
728
729         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
730         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
731         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
732         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
733         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
734
735         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
736
737         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
738         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
739
740         let offer = alice.node
741                 .create_offer_builder("coffee".to_string()).unwrap()
742                 .amount_msats(10_000_000)
743                 .build().unwrap();
744
745         let payment_id = PaymentId([1; 32]);
746
747         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
748                 Ok(_) => panic!("Expected error"),
749                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
750         }
751
752         assert!(nodes[0].node.list_recent_payments().is_empty());
753 }
754
755 #[test]
756 fn fails_creating_invoice_request_with_duplicate_payment_id() {
757         let chanmon_cfgs = create_chanmon_cfgs(6);
758         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
759         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
760         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
761
762         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
763         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
764         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
765         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
766         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
767         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
768         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
769
770         let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
771
772         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
773
774         let offer = alice.node
775                 .create_offer_builder("coffee".to_string()).unwrap()
776                 .amount_msats(10_000_000)
777                 .build().unwrap();
778
779         let payment_id = PaymentId([1; 32]);
780         assert!(
781                 david.node.pay_for_offer(
782                         &offer, None, None, None, payment_id, Retry::Attempts(0), None
783                 ).is_ok()
784         );
785         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
786
787         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
788                 Ok(_) => panic!("Expected error"),
789                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
790         }
791
792         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
793 }
794
795 #[test]
796 fn fails_creating_refund_with_duplicate_payment_id() {
797         let chanmon_cfgs = create_chanmon_cfgs(2);
798         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
799         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
800         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
801
802         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
803
804         let absolute_expiry = Duration::from_secs(u64::MAX);
805         let payment_id = PaymentId([1; 32]);
806         assert!(
807                 nodes[0].node.create_refund_builder(
808                         "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
809                 ).is_ok()
810         );
811         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
812
813         match nodes[0].node.create_refund_builder(
814                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
815         ) {
816                 Ok(_) => panic!("Expected error"),
817                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
818         }
819
820         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
821 }
822
823 #[test]
824 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
825         let mut accept_forward_cfg = test_default_channel_config();
826         accept_forward_cfg.accept_forwards_to_priv_channels = true;
827
828         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
829         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
830         features.set_onion_messages_optional();
831         features.clear_route_blinding();
832
833         let chanmon_cfgs = create_chanmon_cfgs(6);
834         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
835
836         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
837
838         let node_chanmgrs = create_node_chanmgrs(
839                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
840         );
841         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
842
843         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
844         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
845         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
846         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
847         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
848         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
849         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
850
851         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
852         let alice_id = alice.node.get_our_node_id();
853         let bob_id = bob.node.get_our_node_id();
854         let charlie_id = charlie.node.get_our_node_id();
855         let david_id = david.node.get_our_node_id();
856
857         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
858         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
859
860         let offer = alice.node
861                 .create_offer_builder("coffee".to_string()).unwrap()
862                 .amount_msats(10_000_000)
863                 .build().unwrap();
864
865         let payment_id = PaymentId([1; 32]);
866         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
867                 .unwrap();
868
869         connect_peers(david, bob);
870
871         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
872         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
873
874         connect_peers(alice, charlie);
875
876         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
877         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
878
879         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
880         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
881
882         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
883         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
884
885         let invoice_error = extract_invoice_error(david, &onion_message);
886         assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
887 }
888
889 #[test]
890 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
891         let mut accept_forward_cfg = test_default_channel_config();
892         accept_forward_cfg.accept_forwards_to_priv_channels = true;
893
894         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
895         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
896         features.set_onion_messages_optional();
897         features.clear_route_blinding();
898
899         let chanmon_cfgs = create_chanmon_cfgs(6);
900         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
901
902         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
903
904         let node_chanmgrs = create_node_chanmgrs(
905                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
906         );
907         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
908
909         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
910         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
911         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
912         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
913         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
914         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
915         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
916
917         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
918
919         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
920         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
921
922         let absolute_expiry = Duration::from_secs(u64::MAX);
923         let payment_id = PaymentId([1; 32]);
924         let refund = david.node
925                 .create_refund_builder(
926                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
927                 )
928                 .unwrap()
929                 .build().unwrap();
930
931         match alice.node.request_refund_payment(&refund) {
932                 Ok(_) => panic!("Expected error"),
933                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
934         }
935 }
936
937 #[test]
938 fn fails_paying_invoice_more_than_once() {
939         let mut accept_forward_cfg = test_default_channel_config();
940         accept_forward_cfg.accept_forwards_to_priv_channels = true;
941
942         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
943         features.set_onion_messages_optional();
944         features.set_route_blinding_optional();
945
946         let chanmon_cfgs = create_chanmon_cfgs(6);
947         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
948
949         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
950
951         let node_chanmgrs = create_node_chanmgrs(
952                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
953         );
954         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
955
956         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
957         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
958         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
959         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
960         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
961         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
962         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
963
964         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
965         let alice_id = alice.node.get_our_node_id();
966         let bob_id = bob.node.get_our_node_id();
967         let charlie_id = charlie.node.get_our_node_id();
968         let david_id = david.node.get_our_node_id();
969
970         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
971         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
972
973         let absolute_expiry = Duration::from_secs(u64::MAX);
974         let payment_id = PaymentId([1; 32]);
975         let refund = david.node
976                 .create_refund_builder(
977                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
978                 )
979                 .unwrap()
980                 .build().unwrap();
981         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
982
983         // Alice sends the first invoice
984         alice.node.request_refund_payment(&refund).unwrap();
985
986         connect_peers(alice, charlie);
987
988         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
989         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
990
991         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
992         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
993
994         // David pays the first invoice
995         let invoice1 = extract_invoice(david, &onion_message);
996
997         route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
998         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
999
1000         claim_bolt12_payment(david, &[charlie, bob, alice]);
1001         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1002
1003         disconnect_peers(alice, &[charlie]);
1004
1005         // Alice sends the second invoice
1006         alice.node.request_refund_payment(&refund).unwrap();
1007
1008         connect_peers(alice, charlie);
1009         connect_peers(david, bob);
1010
1011         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1012         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1013
1014         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1015         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1016
1017         let invoice2 = extract_invoice(david, &onion_message);
1018         assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1019
1020         // David sends an error instead of paying the second invoice
1021         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1022         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1023
1024         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1025         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1026
1027         let invoice_error = extract_invoice_error(alice, &onion_message);
1028         assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));
1029 }