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