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