Merge pull request #3028 from jkczyz/2024-04-offer-id-followups
[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, InvoiceRequestFields};
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         assert_ne!(offer.signing_pubkey(), alice_id);
389         assert!(!offer.paths().is_empty());
390         for path in offer.paths() {
391                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
392         }
393
394         let payment_id = PaymentId([1; 32]);
395         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
396                 .unwrap();
397         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
398
399         connect_peers(david, bob);
400
401         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
402         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
403
404         connect_peers(alice, charlie);
405
406         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
407         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
408
409         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
410         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
411                 offer_id: offer.id(),
412                 invoice_request: InvoiceRequestFields {
413                         payer_id: invoice_request.payer_id(),
414                         quantity: None,
415                         payer_note_truncated: None,
416                 },
417         });
418         assert_eq!(invoice_request.amount_msats(), None);
419         assert_ne!(invoice_request.payer_id(), david_id);
420         assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(charlie_id));
421
422         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
423         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
424
425         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
426         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
427
428         let invoice = extract_invoice(david, &onion_message);
429         assert_eq!(invoice.amount_msats(), 10_000_000);
430         assert_ne!(invoice.signing_pubkey(), alice_id);
431         assert!(!invoice.payment_paths().is_empty());
432         for (_, path) in invoice.payment_paths() {
433                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
434         }
435
436         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
437         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
438
439         claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
440         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
441 }
442
443 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
444 /// rather than exposing a node's pubkey.
445 #[test]
446 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
447         let mut accept_forward_cfg = test_default_channel_config();
448         accept_forward_cfg.accept_forwards_to_priv_channels = true;
449
450         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
451         features.set_onion_messages_optional();
452         features.set_route_blinding_optional();
453
454         let chanmon_cfgs = create_chanmon_cfgs(6);
455         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
456
457         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
458
459         let node_chanmgrs = create_node_chanmgrs(
460                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
461         );
462         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
463
464         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
465         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
466         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
467         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
468         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
469         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
470         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
471
472         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
473         let alice_id = alice.node.get_our_node_id();
474         let bob_id = bob.node.get_our_node_id();
475         let charlie_id = charlie.node.get_our_node_id();
476         let david_id = david.node.get_our_node_id();
477
478         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
479         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
480
481         let absolute_expiry = Duration::from_secs(u64::MAX);
482         let payment_id = PaymentId([1; 32]);
483         let refund = david.node
484                 .create_refund_builder(
485                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
486                 )
487                 .unwrap()
488                 .build().unwrap();
489         assert_eq!(refund.amount_msats(), 10_000_000);
490         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
491         assert_ne!(refund.payer_id(), david_id);
492         assert!(!refund.paths().is_empty());
493         for path in refund.paths() {
494                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
495         }
496         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
497
498         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
499         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
500
501         connect_peers(alice, charlie);
502
503         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
504         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
505
506         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
507         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
508
509         let invoice = extract_invoice(david, &onion_message);
510         assert_eq!(invoice, expected_invoice);
511
512         assert_eq!(invoice.amount_msats(), 10_000_000);
513         assert_ne!(invoice.signing_pubkey(), alice_id);
514         assert!(!invoice.payment_paths().is_empty());
515         for (_, path) in invoice.payment_paths() {
516                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
517         }
518
519         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
520         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
521
522         claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
523         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
524 }
525
526 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
527 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
528 /// introduction node of the blinded path.
529 #[test]
530 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
531         let chanmon_cfgs = create_chanmon_cfgs(2);
532         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
533         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
534         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
535
536         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
537
538         let alice = &nodes[0];
539         let alice_id = alice.node.get_our_node_id();
540         let bob = &nodes[1];
541         let bob_id = bob.node.get_our_node_id();
542
543         let offer = alice.node
544                 .create_offer_builder("coffee".to_string()).unwrap()
545                 .amount_msats(10_000_000)
546                 .build().unwrap();
547         assert_ne!(offer.signing_pubkey(), alice_id);
548         assert!(!offer.paths().is_empty());
549         for path in offer.paths() {
550                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
551         }
552
553         let payment_id = PaymentId([1; 32]);
554         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
555         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
556
557         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
558         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
559
560         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
561         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
562                 offer_id: offer.id(),
563                 invoice_request: InvoiceRequestFields {
564                         payer_id: invoice_request.payer_id(),
565                         quantity: None,
566                         payer_note_truncated: None,
567                 },
568         });
569         assert_eq!(invoice_request.amount_msats(), None);
570         assert_ne!(invoice_request.payer_id(), bob_id);
571         assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(bob_id));
572
573         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
574         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
575
576         let invoice = extract_invoice(bob, &onion_message);
577         assert_eq!(invoice.amount_msats(), 10_000_000);
578         assert_ne!(invoice.signing_pubkey(), alice_id);
579         assert!(!invoice.payment_paths().is_empty());
580         for (_, path) in invoice.payment_paths() {
581                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
582         }
583
584         route_bolt12_payment(bob, &[alice], &invoice);
585         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
586
587         claim_bolt12_payment(bob, &[alice], payment_context);
588         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
589 }
590
591 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
592 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
593 /// introduction node of the blinded path.
594 #[test]
595 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
596         let chanmon_cfgs = create_chanmon_cfgs(2);
597         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
598         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
599         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
600
601         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
602
603         let alice = &nodes[0];
604         let alice_id = alice.node.get_our_node_id();
605         let bob = &nodes[1];
606         let bob_id = bob.node.get_our_node_id();
607
608         let absolute_expiry = Duration::from_secs(u64::MAX);
609         let payment_id = PaymentId([1; 32]);
610         let refund = bob.node
611                 .create_refund_builder(
612                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
613                 )
614                 .unwrap()
615                 .build().unwrap();
616         assert_eq!(refund.amount_msats(), 10_000_000);
617         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
618         assert_ne!(refund.payer_id(), bob_id);
619         assert!(!refund.paths().is_empty());
620         for path in refund.paths() {
621                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
622         }
623         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
624
625         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
626         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
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         assert_eq!(invoice, expected_invoice);
633
634         assert_eq!(invoice.amount_msats(), 10_000_000);
635         assert_ne!(invoice.signing_pubkey(), alice_id);
636         assert!(!invoice.payment_paths().is_empty());
637         for (_, path) in invoice.payment_paths() {
638                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
639         }
640
641         route_bolt12_payment(bob, &[alice], &invoice);
642         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
643
644         claim_bolt12_payment(bob, &[alice], payment_context);
645         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
646 }
647
648 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
649 /// the requested is sent directly using the node's pubkey, the response and the payment still use
650 /// blinded paths as required by the spec.
651 #[test]
652 fn pays_for_offer_without_blinded_paths() {
653         let chanmon_cfgs = create_chanmon_cfgs(2);
654         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
655         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
656         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
657
658         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
659
660         let alice = &nodes[0];
661         let alice_id = alice.node.get_our_node_id();
662         let bob = &nodes[1];
663         let bob_id = bob.node.get_our_node_id();
664
665         let offer = alice.node
666                 .create_offer_builder("coffee".to_string()).unwrap()
667                 .clear_paths()
668                 .amount_msats(10_000_000)
669                 .build().unwrap();
670         assert_eq!(offer.signing_pubkey(), alice_id);
671         assert!(offer.paths().is_empty());
672
673         let payment_id = PaymentId([1; 32]);
674         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
675         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
676
677         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
678         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
679
680         let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
681         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
682                 offer_id: offer.id(),
683                 invoice_request: InvoiceRequestFields {
684                         payer_id: invoice_request.payer_id(),
685                         quantity: None,
686                         payer_note_truncated: None,
687                 },
688         });
689
690         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
691         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
692
693         let invoice = extract_invoice(bob, &onion_message);
694         route_bolt12_payment(bob, &[alice], &invoice);
695         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
696
697         claim_bolt12_payment(bob, &[alice], payment_context);
698         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
699 }
700
701 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
702 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
703 #[test]
704 fn pays_for_refund_without_blinded_paths() {
705         let chanmon_cfgs = create_chanmon_cfgs(2);
706         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
707         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
708         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
709
710         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
711
712         let alice = &nodes[0];
713         let alice_id = alice.node.get_our_node_id();
714         let bob = &nodes[1];
715         let bob_id = bob.node.get_our_node_id();
716
717         let absolute_expiry = Duration::from_secs(u64::MAX);
718         let payment_id = PaymentId([1; 32]);
719         let refund = bob.node
720                 .create_refund_builder(
721                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
722                 )
723                 .unwrap()
724                 .clear_paths()
725                 .build().unwrap();
726         assert_eq!(refund.payer_id(), bob_id);
727         assert!(refund.paths().is_empty());
728         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
729
730         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
731         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
732
733         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
734         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
735
736         let invoice = extract_invoice(bob, &onion_message);
737         assert_eq!(invoice, expected_invoice);
738
739         route_bolt12_payment(bob, &[alice], &invoice);
740         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
741
742         claim_bolt12_payment(bob, &[alice], payment_context);
743         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
744 }
745
746 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
747 #[test]
748 fn fails_creating_offer_without_blinded_paths() {
749         let chanmon_cfgs = create_chanmon_cfgs(2);
750         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
751         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
752         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
753
754         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
755
756         match nodes[0].node.create_offer_builder("coffee".to_string()) {
757                 Ok(_) => panic!("Expected error"),
758                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
759         }
760 }
761
762 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
763 #[test]
764 fn fails_creating_refund_without_blinded_paths() {
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_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
771
772         let absolute_expiry = Duration::from_secs(u64::MAX);
773         let payment_id = PaymentId([1; 32]);
774
775         match nodes[0].node.create_refund_builder(
776                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
777         ) {
778                 Ok(_) => panic!("Expected error"),
779                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
780         }
781
782         assert!(nodes[0].node.list_recent_payments().is_empty());
783 }
784
785 /// Fails creating an invoice request when the offer contains an unsupported chain.
786 #[test]
787 fn fails_creating_invoice_request_for_unsupported_chain() {
788         let chanmon_cfgs = create_chanmon_cfgs(2);
789         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
790         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
791         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
792
793         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
794
795         let alice = &nodes[0];
796         let bob = &nodes[1];
797
798         let offer = alice.node
799                 .create_offer_builder("coffee".to_string()).unwrap()
800                 .clear_chains()
801                 .chain(Network::Signet)
802                 .build().unwrap();
803
804         let payment_id = PaymentId([1; 32]);
805         match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
806                 Ok(_) => panic!("Expected error"),
807                 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
808         }
809 }
810
811 /// Fails requesting a payment when the refund contains an unsupported chain.
812 #[test]
813 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
814         let chanmon_cfgs = create_chanmon_cfgs(2);
815         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
816         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
817         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
818
819         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
820
821         let alice = &nodes[0];
822         let bob = &nodes[1];
823
824         let absolute_expiry = Duration::from_secs(u64::MAX);
825         let payment_id = PaymentId([1; 32]);
826         let refund = bob.node
827                 .create_refund_builder(
828                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
829                 )
830                 .unwrap()
831                 .chain(Network::Signet)
832                 .build().unwrap();
833
834         match alice.node.request_refund_payment(&refund) {
835                 Ok(_) => panic!("Expected error"),
836                 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
837         }
838 }
839
840 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
841 /// the node's id.
842 #[test]
843 fn fails_creating_invoice_request_without_blinded_reply_path() {
844         let chanmon_cfgs = create_chanmon_cfgs(6);
845         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
846         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
847         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
848
849         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
850         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
851         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
852         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
853         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
854
855         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
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
867         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
868                 Ok(_) => panic!("Expected error"),
869                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
870         }
871
872         assert!(nodes[0].node.list_recent_payments().is_empty());
873 }
874
875 #[test]
876 fn fails_creating_invoice_request_with_duplicate_payment_id() {
877         let chanmon_cfgs = create_chanmon_cfgs(6);
878         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
879         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
880         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
881
882         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
883         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
884         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
885         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
886         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
887         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
888         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
889
890         let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
891
892         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
893
894         let offer = alice.node
895                 .create_offer_builder("coffee".to_string()).unwrap()
896                 .amount_msats(10_000_000)
897                 .build().unwrap();
898
899         let payment_id = PaymentId([1; 32]);
900         assert!(
901                 david.node.pay_for_offer(
902                         &offer, None, None, None, payment_id, Retry::Attempts(0), None
903                 ).is_ok()
904         );
905         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
906
907         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
908                 Ok(_) => panic!("Expected error"),
909                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
910         }
911
912         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
913 }
914
915 #[test]
916 fn fails_creating_refund_with_duplicate_payment_id() {
917         let chanmon_cfgs = create_chanmon_cfgs(2);
918         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
919         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
920         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
921
922         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
923
924         let absolute_expiry = Duration::from_secs(u64::MAX);
925         let payment_id = PaymentId([1; 32]);
926         assert!(
927                 nodes[0].node.create_refund_builder(
928                         "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
929                 ).is_ok()
930         );
931         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
932
933         match nodes[0].node.create_refund_builder(
934                 "refund".to_string(), 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
935         ) {
936                 Ok(_) => panic!("Expected error"),
937                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
938         }
939
940         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
941 }
942
943 #[test]
944 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
945         let mut accept_forward_cfg = test_default_channel_config();
946         accept_forward_cfg.accept_forwards_to_priv_channels = true;
947
948         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
949         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
950         features.set_onion_messages_optional();
951         features.clear_route_blinding();
952
953         let chanmon_cfgs = create_chanmon_cfgs(6);
954         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
955
956         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
957
958         let node_chanmgrs = create_node_chanmgrs(
959                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
960         );
961         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
962
963         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
964         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
965         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
966         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
967         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
968         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
969         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
970
971         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
972         let alice_id = alice.node.get_our_node_id();
973         let bob_id = bob.node.get_our_node_id();
974         let charlie_id = charlie.node.get_our_node_id();
975         let david_id = david.node.get_our_node_id();
976
977         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
978         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
979
980         let offer = alice.node
981                 .create_offer_builder("coffee".to_string()).unwrap()
982                 .amount_msats(10_000_000)
983                 .build().unwrap();
984
985         let payment_id = PaymentId([1; 32]);
986         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
987                 .unwrap();
988
989         connect_peers(david, bob);
990
991         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
992         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
993
994         connect_peers(alice, charlie);
995
996         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
997         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
998
999         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1000         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1001
1002         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1003         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1004
1005         let invoice_error = extract_invoice_error(david, &onion_message);
1006         assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
1007 }
1008
1009 #[test]
1010 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
1011         let mut accept_forward_cfg = test_default_channel_config();
1012         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1013
1014         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1015         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1016         features.set_onion_messages_optional();
1017         features.clear_route_blinding();
1018
1019         let chanmon_cfgs = create_chanmon_cfgs(6);
1020         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1021
1022         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1023
1024         let node_chanmgrs = create_node_chanmgrs(
1025                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1026         );
1027         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1028
1029         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1030         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1031         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1032         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1033         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1034         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1035         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1036
1037         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1038
1039         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1040         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1041
1042         let absolute_expiry = Duration::from_secs(u64::MAX);
1043         let payment_id = PaymentId([1; 32]);
1044         let refund = david.node
1045                 .create_refund_builder(
1046                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1047                 )
1048                 .unwrap()
1049                 .build().unwrap();
1050
1051         match alice.node.request_refund_payment(&refund) {
1052                 Ok(_) => panic!("Expected error"),
1053                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1054         }
1055 }
1056
1057 #[test]
1058 fn fails_paying_invoice_more_than_once() {
1059         let mut accept_forward_cfg = test_default_channel_config();
1060         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1061
1062         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1063         features.set_onion_messages_optional();
1064         features.set_route_blinding_optional();
1065
1066         let chanmon_cfgs = create_chanmon_cfgs(6);
1067         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1068
1069         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1070
1071         let node_chanmgrs = create_node_chanmgrs(
1072                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1073         );
1074         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1075
1076         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1077         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1078         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1079         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1080         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1081         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1082         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1083
1084         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1085         let alice_id = alice.node.get_our_node_id();
1086         let bob_id = bob.node.get_our_node_id();
1087         let charlie_id = charlie.node.get_our_node_id();
1088         let david_id = david.node.get_our_node_id();
1089
1090         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1091         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1092
1093         let absolute_expiry = Duration::from_secs(u64::MAX);
1094         let payment_id = PaymentId([1; 32]);
1095         let refund = david.node
1096                 .create_refund_builder(
1097                         "refund".to_string(), 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1098                 )
1099                 .unwrap()
1100                 .build().unwrap();
1101         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1102
1103         // Alice sends the first invoice
1104         alice.node.request_refund_payment(&refund).unwrap();
1105
1106         connect_peers(alice, charlie);
1107
1108         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1109         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1110
1111         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1112         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1113
1114         // David pays the first invoice
1115         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
1116         let invoice1 = extract_invoice(david, &onion_message);
1117
1118         route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1119         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1120
1121         claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
1122         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1123
1124         disconnect_peers(alice, &[charlie]);
1125
1126         // Alice sends the second invoice
1127         alice.node.request_refund_payment(&refund).unwrap();
1128
1129         connect_peers(alice, charlie);
1130         connect_peers(david, bob);
1131
1132         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1133         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1134
1135         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1136         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1137
1138         let invoice2 = extract_invoice(david, &onion_message);
1139         assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1140
1141         // David sends an error instead of paying the second invoice
1142         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1143         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1144
1145         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1146         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1147
1148         let invoice_error = extract_invoice_error(alice, &onion_message);
1149         assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));
1150 }