Add OffersMessage variant for static invoices.
[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::Network;
44 use bitcoin::secp256k1::PublicKey;
45 use core::time::Duration;
46 use crate::blinded_path::{BlindedPath, IntroductionNode};
47 use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
48 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
49 use crate::ln::channelmanager::{Bolt12PaymentError, MAX_SHORT_LIVED_RELATIVE_EXPIRY, PaymentId, RecentPaymentDetails, Retry, self};
50 use crate::ln::functional_test_utils::*;
51 use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
52 use crate::ln::outbound_payment::IDEMPOTENCY_TIMEOUT_TICKS;
53 use crate::offers::invoice::Bolt12Invoice;
54 use crate::offers::invoice_error::InvoiceError;
55 use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
56 use crate::offers::parse::Bolt12SemanticError;
57 use crate::onion_message::messenger::PeeledOnion;
58 use crate::onion_message::offers::OffersMessage;
59 use crate::onion_message::packet::ParsedOnionMessageContents;
60 use crate::routing::gossip::{NodeAlias, NodeId};
61 use crate::sign::{NodeSigner, Recipient};
62
63 use crate::prelude::*;
64
65 macro_rules! expect_recent_payment {
66         ($node: expr, $payment_state: path, $payment_id: expr) => {
67                 match $node.node.list_recent_payments().first() {
68                         Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
69                                 assert_eq!($payment_id, actual_payment_id);
70                         },
71                         Some(_) => panic!("Unexpected recent payment state"),
72                         None => panic!("No recent payments"),
73                 }
74         }
75 }
76
77 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
78         let node_id_a = node_a.node.get_our_node_id();
79         let node_id_b = node_b.node.get_our_node_id();
80
81         let init_a = Init {
82                 features: node_a.init_features(&node_id_b),
83                 networks: None,
84                 remote_network_address: None,
85         };
86         let init_b = Init {
87                 features: node_b.init_features(&node_id_a),
88                 networks: None,
89                 remote_network_address: None,
90         };
91
92         node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
93         node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
94         node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
95         node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
96 }
97
98 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
99         for node_b in peers {
100                 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
101                 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
102                 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
103                 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
104         }
105 }
106
107 fn announce_node_address<'a, 'b, 'c>(
108         node: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>], address: SocketAddress,
109 ) {
110         let features = node.onion_messenger.provided_node_features()
111                 | node.gossip_sync.provided_node_features();
112         let rgb = [0u8; 3];
113         let announcement = UnsignedNodeAnnouncement {
114                 features,
115                 timestamp: 1000,
116                 node_id: NodeId::from_pubkey(&node.keys_manager.get_node_id(Recipient::Node).unwrap()),
117                 rgb,
118                 alias: NodeAlias([0u8; 32]),
119                 addresses: vec![address],
120                 excess_address_data: Vec::new(),
121                 excess_data: Vec::new(),
122         };
123         let signature = node.keys_manager.sign_gossip_message(
124                 UnsignedGossipMessage::NodeAnnouncement(&announcement)
125         ).unwrap();
126
127         let msg = NodeAnnouncement {
128                 signature,
129                 contents: announcement
130         };
131
132         node.gossip_sync.handle_node_announcement(&msg).unwrap();
133         for peer in peers {
134                 peer.gossip_sync.handle_node_announcement(&msg).unwrap();
135         }
136 }
137
138 fn resolve_introduction_node<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, path: &BlindedPath) -> PublicKey {
139         path.public_introduction_node_id(&node.network_graph.read_only())
140                 .and_then(|node_id| node_id.as_pubkey().ok())
141                 .unwrap()
142 }
143
144 fn route_bolt12_payment<'a, 'b, 'c>(
145         node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
146 ) {
147         // Monitor added when handling the invoice onion message.
148         check_added_monitors(node, 1);
149
150         let mut events = node.node.get_and_clear_pending_msg_events();
151         assert_eq!(events.len(), 1);
152         let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
153
154         // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
155         // invoice contains the payment_hash but it was encrypted inside an onion message.
156         let amount_msats = invoice.amount_msats();
157         let payment_hash = invoice.payment_hash();
158         let args = PassAlongPathArgs::new(node, path, amount_msats, payment_hash, ev)
159                 .without_clearing_recipient_events();
160         do_pass_along_path(args);
161 }
162
163 fn claim_bolt12_payment<'a, 'b, 'c>(
164         node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], expected_payment_context: PaymentContext
165 ) {
166         let recipient = &path[path.len() - 1];
167         let payment_purpose = match get_event!(recipient, Event::PaymentClaimable) {
168                 Event::PaymentClaimable { purpose, .. } => purpose,
169                 _ => panic!("No Event::PaymentClaimable"),
170         };
171         let payment_preimage = match payment_purpose.preimage() {
172                 Some(preimage) => preimage,
173                 None => panic!("No preimage in Event::PaymentClaimable"),
174         };
175         match payment_purpose {
176                 PaymentPurpose::Bolt12OfferPayment { payment_context, .. } => {
177                         assert_eq!(PaymentContext::Bolt12Offer(payment_context), expected_payment_context);
178                 },
179                 PaymentPurpose::Bolt12RefundPayment { payment_context, .. } => {
180                         assert_eq!(PaymentContext::Bolt12Refund(payment_context), expected_payment_context);
181                 },
182                 _ => panic!("Unexpected payment purpose: {:?}", payment_purpose),
183         }
184         claim_payment(node, path, payment_preimage);
185 }
186
187 fn extract_invoice_request<'a, 'b, 'c>(
188         node: &Node<'a, 'b, 'c>, message: &OnionMessage
189 ) -> (InvoiceRequest, BlindedPath) {
190         match node.onion_messenger.peel_onion_message(message) {
191                 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
192                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
193                                 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path.unwrap()),
194                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
195                                 OffersMessage::StaticInvoice(invoice) => panic!("Unexpected static invoice: {:?}", invoice),
196                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
197                         },
198                         ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
199                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
200                 },
201                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
202                 Err(e) => panic!("Failed to process onion message {:?}", e),
203         }
204 }
205
206 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
207         match node.onion_messenger.peel_onion_message(message) {
208                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
209                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
210                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
211                                 OffersMessage::Invoice(invoice) => invoice,
212                                 OffersMessage::StaticInvoice(invoice) => panic!("Unexpected static invoice: {:?}", invoice),
213                                 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
214                         },
215                         ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
216                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
217                 },
218                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
219                 Err(e) => panic!("Failed to process onion message {:?}", e),
220         }
221 }
222
223 fn extract_invoice_error<'a, 'b, 'c>(
224         node: &Node<'a, 'b, 'c>, message: &OnionMessage
225 ) -> InvoiceError {
226         match node.onion_messenger.peel_onion_message(message) {
227                 Ok(PeeledOnion::Receive(message, _, _)) => match message {
228                         ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
229                                 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
230                                 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
231                                 OffersMessage::StaticInvoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
232                                 OffersMessage::InvoiceError(error) => error,
233                         },
234                         ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
235                         ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
236                 },
237                 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
238                 Err(e) => panic!("Failed to process onion message {:?}", e),
239         }
240 }
241
242 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
243 #[test]
244 fn prefers_non_tor_nodes_in_blinded_paths() {
245         let mut accept_forward_cfg = test_default_channel_config();
246         accept_forward_cfg.accept_forwards_to_priv_channels = true;
247
248         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
249         features.set_onion_messages_optional();
250         features.set_route_blinding_optional();
251
252         let chanmon_cfgs = create_chanmon_cfgs(6);
253         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
254
255         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
256
257         let node_chanmgrs = create_node_chanmgrs(
258                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
259         );
260         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
261
262         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
263         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
264         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
265         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
266         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
267         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
268         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
269
270         // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
271         create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
272
273         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
274         let bob_id = bob.node.get_our_node_id();
275         let charlie_id = charlie.node.get_our_node_id();
276
277         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
278         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
279
280         let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
281         announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
282
283         let offer = bob.node
284                 .create_offer_builder(None).unwrap()
285                 .amount_msats(10_000_000)
286                 .build().unwrap();
287         assert_ne!(offer.signing_pubkey(), Some(bob_id));
288         assert!(!offer.paths().is_empty());
289         for path in offer.paths() {
290                 let introduction_node_id = resolve_introduction_node(david, &path);
291                 assert_ne!(introduction_node_id, bob_id);
292                 assert_ne!(introduction_node_id, charlie_id);
293         }
294
295         // Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
296         announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
297         announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
298
299         let offer = bob.node
300                 .create_offer_builder(None).unwrap()
301                 .amount_msats(10_000_000)
302                 .build().unwrap();
303         assert_ne!(offer.signing_pubkey(), Some(bob_id));
304         assert!(!offer.paths().is_empty());
305         for path in offer.paths() {
306                 let introduction_node_id = resolve_introduction_node(david, &path);
307                 assert_eq!(introduction_node_id, bob_id);
308         }
309 }
310
311 /// Checks that blinded paths prefer an introduction node that is the most connected.
312 #[test]
313 fn prefers_more_connected_nodes_in_blinded_paths() {
314         let mut accept_forward_cfg = test_default_channel_config();
315         accept_forward_cfg.accept_forwards_to_priv_channels = true;
316
317         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
318         features.set_onion_messages_optional();
319         features.set_route_blinding_optional();
320
321         let chanmon_cfgs = create_chanmon_cfgs(6);
322         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
323
324         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
325
326         let node_chanmgrs = create_node_chanmgrs(
327                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
328         );
329         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
330
331         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
332         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
333         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
334         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
335         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
336         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
337         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
338
339         // Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
340         // more than the others.
341         create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
342         create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
343
344         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
345         let bob_id = bob.node.get_our_node_id();
346
347         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
348         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
349
350         let offer = bob.node
351                 .create_offer_builder(None).unwrap()
352                 .amount_msats(10_000_000)
353                 .build().unwrap();
354         assert_ne!(offer.signing_pubkey(), Some(bob_id));
355         assert!(!offer.paths().is_empty());
356         for path in offer.paths() {
357                 let introduction_node_id = resolve_introduction_node(david, &path);
358                 assert_eq!(introduction_node_id, nodes[4].node.get_our_node_id());
359         }
360 }
361
362 /// Checks that blinded paths are compact for short-lived offers.
363 #[test]
364 fn creates_short_lived_offer() {
365         let chanmon_cfgs = create_chanmon_cfgs(2);
366         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
367         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
368         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
369
370         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
371
372         let alice = &nodes[0];
373         let alice_id = alice.node.get_our_node_id();
374         let bob = &nodes[1];
375
376         let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
377         let offer = alice.node
378                 .create_offer_builder(Some(absolute_expiry)).unwrap()
379                 .build().unwrap();
380         assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
381         assert!(!offer.paths().is_empty());
382         for path in offer.paths() {
383                 let introduction_node_id = resolve_introduction_node(bob, &path);
384                 assert_eq!(introduction_node_id, alice_id);
385                 assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
386         }
387 }
388
389 /// Checks that blinded paths are not compact for long-lived offers.
390 #[test]
391 fn creates_long_lived_offer() {
392         let chanmon_cfgs = create_chanmon_cfgs(2);
393         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
394         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
395         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
396
397         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
398
399         let alice = &nodes[0];
400         let alice_id = alice.node.get_our_node_id();
401
402         let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
403                 + Duration::from_secs(1);
404         let offer = alice.node
405                 .create_offer_builder(Some(absolute_expiry))
406                 .unwrap()
407                 .build().unwrap();
408         assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
409         assert!(!offer.paths().is_empty());
410         for path in offer.paths() {
411                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
412         }
413
414         let offer = alice.node
415                 .create_offer_builder(None).unwrap()
416                 .build().unwrap();
417         assert_eq!(offer.absolute_expiry(), None);
418         assert!(!offer.paths().is_empty());
419         for path in offer.paths() {
420                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
421         }
422 }
423
424 /// Checks that blinded paths are compact for short-lived refunds.
425 #[test]
426 fn creates_short_lived_refund() {
427         let chanmon_cfgs = create_chanmon_cfgs(2);
428         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
429         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
430         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
431
432         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
433
434         let alice = &nodes[0];
435         let bob = &nodes[1];
436         let bob_id = bob.node.get_our_node_id();
437
438         let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
439         let payment_id = PaymentId([1; 32]);
440         let refund = bob.node
441                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
442                 .unwrap()
443                 .build().unwrap();
444         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
445         assert!(!refund.paths().is_empty());
446         for path in refund.paths() {
447                 let introduction_node_id = resolve_introduction_node(alice, &path);
448                 assert_eq!(introduction_node_id, bob_id);
449                 assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
450         }
451 }
452
453 /// Checks that blinded paths are not compact for long-lived refunds.
454 #[test]
455 fn creates_long_lived_refund() {
456         let chanmon_cfgs = create_chanmon_cfgs(2);
457         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
458         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
459         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
460
461         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
462
463         let bob = &nodes[1];
464         let bob_id = bob.node.get_our_node_id();
465
466         let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
467                 + Duration::from_secs(1);
468         let payment_id = PaymentId([1; 32]);
469         let refund = bob.node
470                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
471                 .unwrap()
472                 .build().unwrap();
473         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
474         assert!(!refund.paths().is_empty());
475         for path in refund.paths() {
476                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
477         }
478 }
479
480 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
481 /// rather than exposing a node's pubkey.
482 #[test]
483 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
484         let mut accept_forward_cfg = test_default_channel_config();
485         accept_forward_cfg.accept_forwards_to_priv_channels = true;
486
487         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
488         features.set_onion_messages_optional();
489         features.set_route_blinding_optional();
490
491         let chanmon_cfgs = create_chanmon_cfgs(6);
492         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
493
494         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
495
496         let node_chanmgrs = create_node_chanmgrs(
497                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
498         );
499         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
500
501         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
502         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
503         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
504         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
505         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
506         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
507         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
508
509         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
510         let alice_id = alice.node.get_our_node_id();
511         let bob_id = bob.node.get_our_node_id();
512         let charlie_id = charlie.node.get_our_node_id();
513         let david_id = david.node.get_our_node_id();
514
515         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
516         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
517
518         let offer = alice.node
519                 .create_offer_builder(None)
520                 .unwrap()
521                 .amount_msats(10_000_000)
522                 .build().unwrap();
523         assert_ne!(offer.signing_pubkey(), Some(alice_id));
524         assert!(!offer.paths().is_empty());
525         for path in offer.paths() {
526                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
527         }
528
529         let payment_id = PaymentId([1; 32]);
530         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
531                 .unwrap();
532         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
533
534         connect_peers(david, bob);
535
536         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
537         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
538
539         connect_peers(alice, charlie);
540
541         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
542         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
543
544         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
545         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
546                 offer_id: offer.id(),
547                 invoice_request: InvoiceRequestFields {
548                         payer_id: invoice_request.payer_id(),
549                         quantity: None,
550                         payer_note_truncated: None,
551                 },
552         });
553         assert_eq!(invoice_request.amount_msats(), None);
554         assert_ne!(invoice_request.payer_id(), david_id);
555         assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(charlie_id));
556
557         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
558         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
559
560         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
561         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
562
563         let invoice = extract_invoice(david, &onion_message);
564         assert_eq!(invoice.amount_msats(), 10_000_000);
565         assert_ne!(invoice.signing_pubkey(), alice_id);
566         assert!(!invoice.payment_paths().is_empty());
567         for (_, path) in invoice.payment_paths() {
568                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
569         }
570
571         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
572         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
573
574         claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
575         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
576 }
577
578 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
579 /// rather than exposing a node's pubkey.
580 #[test]
581 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
582         let mut accept_forward_cfg = test_default_channel_config();
583         accept_forward_cfg.accept_forwards_to_priv_channels = true;
584
585         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
586         features.set_onion_messages_optional();
587         features.set_route_blinding_optional();
588
589         let chanmon_cfgs = create_chanmon_cfgs(6);
590         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
591
592         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
593
594         let node_chanmgrs = create_node_chanmgrs(
595                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
596         );
597         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
598
599         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
600         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
601         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
602         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
603         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
604         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
605         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
606
607         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
608         let alice_id = alice.node.get_our_node_id();
609         let bob_id = bob.node.get_our_node_id();
610         let charlie_id = charlie.node.get_our_node_id();
611         let david_id = david.node.get_our_node_id();
612
613         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
614         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
615
616         let absolute_expiry = Duration::from_secs(u64::MAX);
617         let payment_id = PaymentId([1; 32]);
618         let refund = david.node
619                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
620                 .unwrap()
621                 .build().unwrap();
622         assert_eq!(refund.amount_msats(), 10_000_000);
623         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
624         assert_ne!(refund.payer_id(), david_id);
625         assert!(!refund.paths().is_empty());
626         for path in refund.paths() {
627                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
628         }
629         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
630
631         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
632         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
633
634         connect_peers(alice, charlie);
635
636         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
637         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
638
639         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
640         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
641
642         let invoice = extract_invoice(david, &onion_message);
643         assert_eq!(invoice, expected_invoice);
644
645         assert_eq!(invoice.amount_msats(), 10_000_000);
646         assert_ne!(invoice.signing_pubkey(), alice_id);
647         assert!(!invoice.payment_paths().is_empty());
648         for (_, path) in invoice.payment_paths() {
649                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
650         }
651
652         route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
653         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
654
655         claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
656         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
657 }
658
659 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
660 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
661 /// introduction node of the blinded path.
662 #[test]
663 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
664         let chanmon_cfgs = create_chanmon_cfgs(2);
665         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
666         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
667         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
668
669         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
670
671         let alice = &nodes[0];
672         let alice_id = alice.node.get_our_node_id();
673         let bob = &nodes[1];
674         let bob_id = bob.node.get_our_node_id();
675
676         let offer = alice.node
677                 .create_offer_builder(None).unwrap()
678                 .amount_msats(10_000_000)
679                 .build().unwrap();
680         assert_ne!(offer.signing_pubkey(), Some(alice_id));
681         assert!(!offer.paths().is_empty());
682         for path in offer.paths() {
683                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
684         }
685
686         let payment_id = PaymentId([1; 32]);
687         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
688         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
689
690         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
691         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
692
693         let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
694         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
695                 offer_id: offer.id(),
696                 invoice_request: InvoiceRequestFields {
697                         payer_id: invoice_request.payer_id(),
698                         quantity: None,
699                         payer_note_truncated: None,
700                 },
701         });
702         assert_eq!(invoice_request.amount_msats(), None);
703         assert_ne!(invoice_request.payer_id(), bob_id);
704         assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(bob_id));
705
706         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
707         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
708
709         let invoice = extract_invoice(bob, &onion_message);
710         assert_eq!(invoice.amount_msats(), 10_000_000);
711         assert_ne!(invoice.signing_pubkey(), alice_id);
712         assert!(!invoice.payment_paths().is_empty());
713         for (_, path) in invoice.payment_paths() {
714                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
715         }
716
717         route_bolt12_payment(bob, &[alice], &invoice);
718         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
719
720         claim_bolt12_payment(bob, &[alice], payment_context);
721         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
722 }
723
724 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
725 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
726 /// introduction node of the blinded path.
727 #[test]
728 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
729         let chanmon_cfgs = create_chanmon_cfgs(2);
730         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
731         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
732         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
733
734         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
735
736         let alice = &nodes[0];
737         let alice_id = alice.node.get_our_node_id();
738         let bob = &nodes[1];
739         let bob_id = bob.node.get_our_node_id();
740
741         let absolute_expiry = Duration::from_secs(u64::MAX);
742         let payment_id = PaymentId([1; 32]);
743         let refund = bob.node
744                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
745                 .unwrap()
746                 .build().unwrap();
747         assert_eq!(refund.amount_msats(), 10_000_000);
748         assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
749         assert_ne!(refund.payer_id(), bob_id);
750         assert!(!refund.paths().is_empty());
751         for path in refund.paths() {
752                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
753         }
754         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
755
756         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
757         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
758
759         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
760         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
761
762         let invoice = extract_invoice(bob, &onion_message);
763         assert_eq!(invoice, expected_invoice);
764
765         assert_eq!(invoice.amount_msats(), 10_000_000);
766         assert_ne!(invoice.signing_pubkey(), alice_id);
767         assert!(!invoice.payment_paths().is_empty());
768         for (_, path) in invoice.payment_paths() {
769                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
770         }
771
772         route_bolt12_payment(bob, &[alice], &invoice);
773         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
774
775         claim_bolt12_payment(bob, &[alice], payment_context);
776         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
777 }
778
779 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
780 /// the requested is sent directly using the node's pubkey, the response and the payment still use
781 /// blinded paths as required by the spec.
782 #[test]
783 fn pays_for_offer_without_blinded_paths() {
784         let chanmon_cfgs = create_chanmon_cfgs(2);
785         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
786         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
787         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
788
789         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
790
791         let alice = &nodes[0];
792         let alice_id = alice.node.get_our_node_id();
793         let bob = &nodes[1];
794         let bob_id = bob.node.get_our_node_id();
795
796         let offer = alice.node
797                 .create_offer_builder(None).unwrap()
798                 .clear_paths()
799                 .amount_msats(10_000_000)
800                 .build().unwrap();
801         assert_eq!(offer.signing_pubkey(), Some(alice_id));
802         assert!(offer.paths().is_empty());
803
804         let payment_id = PaymentId([1; 32]);
805         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
806         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
807
808         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
809         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
810
811         let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
812         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
813                 offer_id: offer.id(),
814                 invoice_request: InvoiceRequestFields {
815                         payer_id: invoice_request.payer_id(),
816                         quantity: None,
817                         payer_note_truncated: None,
818                 },
819         });
820
821         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
822         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
823
824         let invoice = extract_invoice(bob, &onion_message);
825         route_bolt12_payment(bob, &[alice], &invoice);
826         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
827
828         claim_bolt12_payment(bob, &[alice], payment_context);
829         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
830 }
831
832 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
833 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
834 #[test]
835 fn pays_for_refund_without_blinded_paths() {
836         let chanmon_cfgs = create_chanmon_cfgs(2);
837         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
838         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
839         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
840
841         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
842
843         let alice = &nodes[0];
844         let alice_id = alice.node.get_our_node_id();
845         let bob = &nodes[1];
846         let bob_id = bob.node.get_our_node_id();
847
848         let absolute_expiry = Duration::from_secs(u64::MAX);
849         let payment_id = PaymentId([1; 32]);
850         let refund = bob.node
851                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
852                 .unwrap()
853                 .clear_paths()
854                 .build().unwrap();
855         assert_eq!(refund.payer_id(), bob_id);
856         assert!(refund.paths().is_empty());
857         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
858
859         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
860         let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
861
862         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
863         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
864
865         let invoice = extract_invoice(bob, &onion_message);
866         assert_eq!(invoice, expected_invoice);
867
868         route_bolt12_payment(bob, &[alice], &invoice);
869         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
870
871         claim_bolt12_payment(bob, &[alice], payment_context);
872         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
873 }
874
875 /// Checks that a deferred invoice can be paid asynchronously from an Event::InvoiceReceived.
876 #[test]
877 fn pays_bolt12_invoice_asynchronously() {
878         let mut manually_pay_cfg = test_default_channel_config();
879         manually_pay_cfg.manually_handle_bolt12_invoices = true;
880
881         let chanmon_cfgs = create_chanmon_cfgs(2);
882         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
883         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_pay_cfg)]);
884         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
885
886         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
887
888         let alice = &nodes[0];
889         let alice_id = alice.node.get_our_node_id();
890         let bob = &nodes[1];
891         let bob_id = bob.node.get_our_node_id();
892
893         let offer = alice.node
894                 .create_offer_builder(None).unwrap()
895                 .amount_msats(10_000_000)
896                 .build().unwrap();
897
898         let payment_id = PaymentId([1; 32]);
899         bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
900         expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
901
902         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
903         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
904
905         let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
906         let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
907                 offer_id: offer.id(),
908                 invoice_request: InvoiceRequestFields {
909                         payer_id: invoice_request.payer_id(),
910                         quantity: None,
911                         payer_note_truncated: None,
912                 },
913         });
914
915         let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
916         bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
917
918         let invoice = match get_event!(bob, Event::InvoiceReceived) {
919                 Event::InvoiceReceived { payment_id: actual_payment_id, invoice, .. } => {
920                         assert_eq!(actual_payment_id, payment_id);
921                         invoice
922                 },
923                 _ => panic!("No Event::InvoiceReceived"),
924         };
925         assert_eq!(invoice.amount_msats(), 10_000_000);
926         assert_ne!(invoice.signing_pubkey(), alice_id);
927         assert!(!invoice.payment_paths().is_empty());
928         for (_, path) in invoice.payment_paths() {
929                 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
930         }
931
932         assert!(bob.node.send_payment_for_bolt12_invoice(&invoice).is_ok());
933         assert_eq!(
934                 bob.node.send_payment_for_bolt12_invoice(&invoice),
935                 Err(Bolt12PaymentError::DuplicateInvoice),
936         );
937
938         route_bolt12_payment(bob, &[alice], &invoice);
939         expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
940
941         claim_bolt12_payment(bob, &[alice], payment_context);
942         expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
943
944         assert_eq!(
945                 bob.node.send_payment_for_bolt12_invoice(&invoice),
946                 Err(Bolt12PaymentError::DuplicateInvoice),
947         );
948
949         for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
950                 bob.node.timer_tick_occurred();
951         }
952
953         assert_eq!(
954                 bob.node.send_payment_for_bolt12_invoice(&invoice),
955                 Err(Bolt12PaymentError::UnexpectedInvoice),
956         );
957 }
958
959 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
960 #[test]
961 fn fails_creating_offer_without_blinded_paths() {
962         let chanmon_cfgs = create_chanmon_cfgs(2);
963         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
964         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
965         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
966
967         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
968
969         match nodes[0].node.create_offer_builder(None) {
970                 Ok(_) => panic!("Expected error"),
971                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
972         }
973 }
974
975 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
976 #[test]
977 fn fails_creating_refund_without_blinded_paths() {
978         let chanmon_cfgs = create_chanmon_cfgs(2);
979         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
980         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
981         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
982
983         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
984
985         let absolute_expiry = Duration::from_secs(u64::MAX);
986         let payment_id = PaymentId([1; 32]);
987
988         match nodes[0].node.create_refund_builder(
989                 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
990         ) {
991                 Ok(_) => panic!("Expected error"),
992                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
993         }
994
995         assert!(nodes[0].node.list_recent_payments().is_empty());
996 }
997
998 /// Fails creating or paying an offer when a blinded path cannot be created because no peers are
999 /// connected.
1000 #[test]
1001 fn fails_creating_or_paying_for_offer_without_connected_peers() {
1002         let chanmon_cfgs = create_chanmon_cfgs(6);
1003         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1004         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1005         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1006
1007         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1008         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1009         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1010         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1011         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1012         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1013         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1014
1015         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1016
1017         disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
1018         disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1019
1020         let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1021         match alice.node.create_offer_builder(Some(absolute_expiry)) {
1022                 Ok(_) => panic!("Expected error"),
1023                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1024         }
1025
1026         let mut args = ReconnectArgs::new(alice, bob);
1027         args.send_channel_ready = (true, true);
1028         reconnect_nodes(args);
1029
1030         let offer = alice.node
1031                 .create_offer_builder(Some(absolute_expiry)).unwrap()
1032                 .amount_msats(10_000_000)
1033                 .build().unwrap();
1034
1035         let payment_id = PaymentId([1; 32]);
1036
1037         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1038                 Ok(_) => panic!("Expected error"),
1039                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1040         }
1041
1042         assert!(nodes[0].node.list_recent_payments().is_empty());
1043
1044         let mut args = ReconnectArgs::new(charlie, david);
1045         args.send_channel_ready = (true, true);
1046         reconnect_nodes(args);
1047
1048         assert!(
1049                 david.node.pay_for_offer(
1050                         &offer, None, None, None, payment_id, Retry::Attempts(0), None
1051                 ).is_ok()
1052         );
1053
1054         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1055 }
1056
1057 /// Fails creating or sending an invoice for a refund when a blinded path cannot be created because
1058 /// no peers are connected.
1059 #[test]
1060 fn fails_creating_refund_or_sending_invoice_without_connected_peers() {
1061         let mut accept_forward_cfg = test_default_channel_config();
1062         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1063
1064         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1065         features.set_onion_messages_optional();
1066         features.set_route_blinding_optional();
1067
1068         let chanmon_cfgs = create_chanmon_cfgs(6);
1069         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1070
1071         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1072
1073         let node_chanmgrs = create_node_chanmgrs(
1074                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1075         );
1076         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1077
1078         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1079         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1080         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1081         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1082         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1083         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1084         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1085
1086         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1087
1088         disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
1089         disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1090
1091         let absolute_expiry = david.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1092         let payment_id = PaymentId([1; 32]);
1093         match david.node.create_refund_builder(
1094                 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1095         ) {
1096                 Ok(_) => panic!("Expected error"),
1097                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1098         }
1099
1100         let mut args = ReconnectArgs::new(charlie, david);
1101         args.send_channel_ready = (true, true);
1102         reconnect_nodes(args);
1103
1104         let refund = david.node
1105                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1106                 .unwrap()
1107                 .build().unwrap();
1108
1109         match alice.node.request_refund_payment(&refund) {
1110                 Ok(_) => panic!("Expected error"),
1111                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1112         }
1113
1114         let mut args = ReconnectArgs::new(alice, bob);
1115         args.send_channel_ready = (true, true);
1116         reconnect_nodes(args);
1117
1118         assert!(alice.node.request_refund_payment(&refund).is_ok());
1119 }
1120
1121 /// Fails creating an invoice request when the offer contains an unsupported chain.
1122 #[test]
1123 fn fails_creating_invoice_request_for_unsupported_chain() {
1124         let chanmon_cfgs = create_chanmon_cfgs(2);
1125         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1126         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1127         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1128
1129         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1130
1131         let alice = &nodes[0];
1132         let bob = &nodes[1];
1133
1134         let offer = alice.node
1135                 .create_offer_builder(None).unwrap()
1136                 .clear_chains()
1137                 .chain(Network::Signet)
1138                 .build().unwrap();
1139
1140         let payment_id = PaymentId([1; 32]);
1141         match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1142                 Ok(_) => panic!("Expected error"),
1143                 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
1144         }
1145 }
1146
1147 /// Fails requesting a payment when the refund contains an unsupported chain.
1148 #[test]
1149 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
1150         let chanmon_cfgs = create_chanmon_cfgs(2);
1151         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1152         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1153         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1154
1155         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1156
1157         let alice = &nodes[0];
1158         let bob = &nodes[1];
1159
1160         let absolute_expiry = Duration::from_secs(u64::MAX);
1161         let payment_id = PaymentId([1; 32]);
1162         let refund = bob.node
1163                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1164                 .unwrap()
1165                 .chain(Network::Signet)
1166                 .build().unwrap();
1167
1168         match alice.node.request_refund_payment(&refund) {
1169                 Ok(_) => panic!("Expected error"),
1170                 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
1171         }
1172 }
1173
1174 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
1175 /// the node's id.
1176 #[test]
1177 fn fails_creating_invoice_request_without_blinded_reply_path() {
1178         let chanmon_cfgs = create_chanmon_cfgs(6);
1179         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1180         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1181         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1182
1183         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1184         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1185         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1186         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1187         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1188
1189         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1190
1191         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1192         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1193
1194         let offer = alice.node
1195                 .create_offer_builder(None).unwrap()
1196                 .amount_msats(10_000_000)
1197                 .build().unwrap();
1198
1199         let payment_id = PaymentId([1; 32]);
1200
1201         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1202                 Ok(_) => panic!("Expected error"),
1203                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1204         }
1205
1206         assert!(nodes[0].node.list_recent_payments().is_empty());
1207 }
1208
1209 #[test]
1210 fn fails_creating_invoice_request_with_duplicate_payment_id() {
1211         let chanmon_cfgs = create_chanmon_cfgs(6);
1212         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1213         let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1214         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1215
1216         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1217         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1218         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1219         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1220         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1221         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1222         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1223
1224         let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1225
1226         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1227
1228         let offer = alice.node
1229                 .create_offer_builder(None).unwrap()
1230                 .amount_msats(10_000_000)
1231                 .build().unwrap();
1232
1233         let payment_id = PaymentId([1; 32]);
1234         assert!(
1235                 david.node.pay_for_offer(
1236                         &offer, None, None, None, payment_id, Retry::Attempts(0), None
1237                 ).is_ok()
1238         );
1239         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1240
1241         match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1242                 Ok(_) => panic!("Expected error"),
1243                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
1244         }
1245
1246         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1247 }
1248
1249 #[test]
1250 fn fails_creating_refund_with_duplicate_payment_id() {
1251         let chanmon_cfgs = create_chanmon_cfgs(2);
1252         let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1253         let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1254         let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1255
1256         create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1257
1258         let absolute_expiry = Duration::from_secs(u64::MAX);
1259         let payment_id = PaymentId([1; 32]);
1260         assert!(
1261                 nodes[0].node.create_refund_builder(
1262                         10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1263                 ).is_ok()
1264         );
1265         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
1266
1267         match nodes[0].node.create_refund_builder(
1268                 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1269         ) {
1270                 Ok(_) => panic!("Expected error"),
1271                 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
1272         }
1273
1274         expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
1275 }
1276
1277 #[test]
1278 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
1279         let mut accept_forward_cfg = test_default_channel_config();
1280         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1281
1282         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1283         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1284         features.set_onion_messages_optional();
1285         features.clear_route_blinding();
1286
1287         let chanmon_cfgs = create_chanmon_cfgs(6);
1288         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1289
1290         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1291
1292         let node_chanmgrs = create_node_chanmgrs(
1293                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1294         );
1295         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1296
1297         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1298         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1299         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1300         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1301         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1302         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1303         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1304
1305         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1306         let alice_id = alice.node.get_our_node_id();
1307         let bob_id = bob.node.get_our_node_id();
1308         let charlie_id = charlie.node.get_our_node_id();
1309         let david_id = david.node.get_our_node_id();
1310
1311         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1312         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1313
1314         let offer = alice.node
1315                 .create_offer_builder(None).unwrap()
1316                 .amount_msats(10_000_000)
1317                 .build().unwrap();
1318
1319         let payment_id = PaymentId([1; 32]);
1320         david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
1321                 .unwrap();
1322
1323         connect_peers(david, bob);
1324
1325         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1326         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1327
1328         connect_peers(alice, charlie);
1329
1330         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1331         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1332
1333         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1334         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1335
1336         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1337         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1338
1339         let invoice_error = extract_invoice_error(david, &onion_message);
1340         assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
1341 }
1342
1343 #[test]
1344 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
1345         let mut accept_forward_cfg = test_default_channel_config();
1346         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1347
1348         // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1349         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1350         features.set_onion_messages_optional();
1351         features.clear_route_blinding();
1352
1353         let chanmon_cfgs = create_chanmon_cfgs(6);
1354         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1355
1356         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1357
1358         let node_chanmgrs = create_node_chanmgrs(
1359                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1360         );
1361         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1362
1363         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1364         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1365         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1366         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1367         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1368         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1369         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1370
1371         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1372
1373         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1374         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1375
1376         let absolute_expiry = Duration::from_secs(u64::MAX);
1377         let payment_id = PaymentId([1; 32]);
1378         let refund = david.node
1379                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1380                 .unwrap()
1381                 .build().unwrap();
1382
1383         match alice.node.request_refund_payment(&refund) {
1384                 Ok(_) => panic!("Expected error"),
1385                 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1386         }
1387 }
1388
1389 #[test]
1390 fn fails_paying_invoice_more_than_once() {
1391         let mut accept_forward_cfg = test_default_channel_config();
1392         accept_forward_cfg.accept_forwards_to_priv_channels = true;
1393
1394         let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1395         features.set_onion_messages_optional();
1396         features.set_route_blinding_optional();
1397
1398         let chanmon_cfgs = create_chanmon_cfgs(6);
1399         let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1400
1401         *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1402
1403         let node_chanmgrs = create_node_chanmgrs(
1404                 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1405         );
1406         let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1407
1408         create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1409         create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1410         create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1411         create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1412         create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1413         create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1414         create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1415
1416         let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1417         let alice_id = alice.node.get_our_node_id();
1418         let bob_id = bob.node.get_our_node_id();
1419         let charlie_id = charlie.node.get_our_node_id();
1420         let david_id = david.node.get_our_node_id();
1421
1422         disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1423         disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1424
1425         let absolute_expiry = Duration::from_secs(u64::MAX);
1426         let payment_id = PaymentId([1; 32]);
1427         let refund = david.node
1428                 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1429                 .unwrap()
1430                 .build().unwrap();
1431         expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1432
1433         // Alice sends the first invoice
1434         alice.node.request_refund_payment(&refund).unwrap();
1435
1436         connect_peers(alice, charlie);
1437
1438         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1439         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1440
1441         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1442         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1443
1444         // David pays the first invoice
1445         let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
1446         let invoice1 = extract_invoice(david, &onion_message);
1447
1448         route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1449         expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1450
1451         claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
1452         expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1453
1454         disconnect_peers(alice, &[charlie]);
1455
1456         // Alice sends the second invoice
1457         alice.node.request_refund_payment(&refund).unwrap();
1458
1459         connect_peers(alice, charlie);
1460         connect_peers(david, bob);
1461
1462         let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1463         charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1464
1465         let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1466         david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1467
1468         let invoice2 = extract_invoice(david, &onion_message);
1469         assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1470
1471         // David sends an error instead of paying the second invoice
1472         let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1473         bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1474
1475         let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1476         alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1477
1478         let invoice_error = extract_invoice_error(alice, &onion_message);
1479         assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));
1480 }