1 // This file is Copyright its original authors, visible in version control
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
10 //! Functional tests for the BOLT 12 Offers payment flow.
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.
17 //! Two-node success tests use an announced channel:
21 //! While two-node failure tests use an unannounced channel:
25 //! Six-node tests use unannounced channels for the sender and recipient and announced channels for
26 //! the rest of the network.
32 //! Alice ... Bob -------- Charlie ... David
38 //! Unnamed nodes are needed to ensure unannounced nodes can create two-hop blinded paths.
40 //! Nodes without channels are disconnected and connected as needed to ensure that deterministic
41 //! blinded paths are used.
43 use bitcoin::network::constants::Network;
44 use core::time::Duration;
45 use crate::blinded_path::{BlindedPath, IntroductionNode};
46 use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
47 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
48 use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
49 use crate::ln::functional_test_utils::*;
50 use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
51 use crate::offers::invoice::Bolt12Invoice;
52 use crate::offers::invoice_error::InvoiceError;
53 use crate::offers::invoice_request::{InvoiceRequest, InvoiceRequestFields};
54 use crate::offers::parse::Bolt12SemanticError;
55 use crate::onion_message::messenger::PeeledOnion;
56 use crate::onion_message::offers::OffersMessage;
57 use crate::onion_message::packet::ParsedOnionMessageContents;
58 use crate::routing::gossip::{NodeAlias, NodeId};
59 use crate::sign::{NodeSigner, Recipient};
61 use crate::prelude::*;
63 macro_rules! expect_recent_payment {
64 ($node: expr, $payment_state: path, $payment_id: expr) => {
65 match $node.node.list_recent_payments().first() {
66 Some(&$payment_state { payment_id: actual_payment_id, .. }) => {
67 assert_eq!($payment_id, actual_payment_id);
69 Some(_) => panic!("Unexpected recent payment state"),
70 None => panic!("No recent payments"),
75 fn connect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, node_b: &Node<'a, 'b, 'c>) {
76 let node_id_a = node_a.node.get_our_node_id();
77 let node_id_b = node_b.node.get_our_node_id();
80 features: node_a.init_features(&node_id_b),
82 remote_network_address: None,
85 features: node_b.init_features(&node_id_a),
87 remote_network_address: None,
90 node_a.node.peer_connected(&node_id_b, &init_b, true).unwrap();
91 node_b.node.peer_connected(&node_id_a, &init_a, false).unwrap();
92 node_a.onion_messenger.peer_connected(&node_id_b, &init_b, true).unwrap();
93 node_b.onion_messenger.peer_connected(&node_id_a, &init_a, false).unwrap();
96 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
98 node_a.node.peer_disconnected(&node_b.node.get_our_node_id());
99 node_b.node.peer_disconnected(&node_a.node.get_our_node_id());
100 node_a.onion_messenger.peer_disconnected(&node_b.node.get_our_node_id());
101 node_b.onion_messenger.peer_disconnected(&node_a.node.get_our_node_id());
105 fn announce_node_address<'a, 'b, 'c>(
106 node: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>], address: SocketAddress,
108 let features = node.onion_messenger.provided_node_features()
109 | node.gossip_sync.provided_node_features();
111 let announcement = UnsignedNodeAnnouncement {
114 node_id: NodeId::from_pubkey(&node.keys_manager.get_node_id(Recipient::Node).unwrap()),
116 alias: NodeAlias([0u8; 32]),
117 addresses: vec![address],
118 excess_address_data: Vec::new(),
119 excess_data: Vec::new(),
121 let signature = node.keys_manager.sign_gossip_message(
122 UnsignedGossipMessage::NodeAnnouncement(&announcement)
125 let msg = NodeAnnouncement {
127 contents: announcement
130 node.gossip_sync.handle_node_announcement(&msg).unwrap();
132 peer.gossip_sync.handle_node_announcement(&msg).unwrap();
136 fn route_bolt12_payment<'a, 'b, 'c>(
137 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
139 // Monitor added when handling the invoice onion message.
140 check_added_monitors(node, 1);
142 let mut events = node.node.get_and_clear_pending_msg_events();
143 assert_eq!(events.len(), 1);
144 let ev = remove_first_msg_event_to_node(&path[0].node.get_our_node_id(), &mut events);
146 // Use a fake payment_hash and bypass checking for the PaymentClaimable event since the
147 // invoice contains the payment_hash but it was encrypted inside an onion message.
148 let amount_msats = invoice.amount_msats();
149 let payment_hash = invoice.payment_hash();
150 let args = PassAlongPathArgs::new(node, path, amount_msats, payment_hash, ev)
151 .without_clearing_recipient_events();
152 do_pass_along_path(args);
155 fn claim_bolt12_payment<'a, 'b, 'c>(
156 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], expected_payment_context: PaymentContext
158 let recipient = &path[path.len() - 1];
159 let payment_purpose = match get_event!(recipient, Event::PaymentClaimable) {
160 Event::PaymentClaimable { purpose, .. } => purpose,
161 _ => panic!("No Event::PaymentClaimable"),
163 let payment_preimage = match payment_purpose.preimage() {
164 Some(preimage) => preimage,
165 None => panic!("No preimage in Event::PaymentClaimable"),
167 match payment_purpose {
168 PaymentPurpose::Bolt12OfferPayment { payment_context, .. } => {
169 assert_eq!(PaymentContext::Bolt12Offer(payment_context), expected_payment_context);
171 PaymentPurpose::Bolt12RefundPayment { payment_context, .. } => {
172 assert_eq!(PaymentContext::Bolt12Refund(payment_context), expected_payment_context);
174 _ => panic!("Unexpected payment purpose: {:?}", payment_purpose),
176 claim_payment(node, path, payment_preimage);
179 fn extract_invoice_request<'a, 'b, 'c>(
180 node: &Node<'a, 'b, 'c>, message: &OnionMessage
181 ) -> (InvoiceRequest, Option<BlindedPath>) {
182 match node.onion_messenger.peel_onion_message(message) {
183 Ok(PeeledOnion::Receive(message, _, reply_path)) => match message {
184 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
185 OffersMessage::InvoiceRequest(invoice_request) => (invoice_request, reply_path),
186 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
187 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
189 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
191 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
192 Err(e) => panic!("Failed to process onion message {:?}", e),
196 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
197 match node.onion_messenger.peel_onion_message(message) {
198 Ok(PeeledOnion::Receive(message, _, _)) => match message {
199 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
200 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
201 OffersMessage::Invoice(invoice) => invoice,
202 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
204 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
206 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
207 Err(e) => panic!("Failed to process onion message {:?}", e),
211 fn extract_invoice_error<'a, 'b, 'c>(
212 node: &Node<'a, 'b, 'c>, message: &OnionMessage
214 match node.onion_messenger.peel_onion_message(message) {
215 Ok(PeeledOnion::Receive(message, _, _)) => match message {
216 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
217 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
218 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
219 OffersMessage::InvoiceError(error) => error,
221 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
223 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
224 Err(e) => panic!("Failed to process onion message {:?}", e),
228 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
230 fn prefers_non_tor_nodes_in_blinded_paths() {
231 let mut accept_forward_cfg = test_default_channel_config();
232 accept_forward_cfg.accept_forwards_to_priv_channels = true;
234 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
235 features.set_onion_messages_optional();
236 features.set_route_blinding_optional();
238 let chanmon_cfgs = create_chanmon_cfgs(6);
239 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
241 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
243 let node_chanmgrs = create_node_chanmgrs(
244 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
246 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
248 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
249 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
250 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
251 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
252 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
253 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
254 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
256 // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
257 create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
259 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
260 let bob_id = bob.node.get_our_node_id();
261 let charlie_id = charlie.node.get_our_node_id();
263 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
264 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
266 let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
267 announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
270 .create_offer_builder().unwrap()
271 .amount_msats(10_000_000)
273 assert_ne!(offer.signing_pubkey(), Some(bob_id));
274 assert!(!offer.paths().is_empty());
275 for path in offer.paths() {
276 assert_ne!(path.introduction_node, IntroductionNode::NodeId(bob_id));
277 assert_ne!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
280 // Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
281 announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
282 announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
285 .create_offer_builder().unwrap()
286 .amount_msats(10_000_000)
288 assert_ne!(offer.signing_pubkey(), Some(bob_id));
289 assert!(!offer.paths().is_empty());
290 for path in offer.paths() {
291 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
295 /// Checks that blinded paths prefer an introduction node that is the most connected.
297 fn prefers_more_connected_nodes_in_blinded_paths() {
298 let mut accept_forward_cfg = test_default_channel_config();
299 accept_forward_cfg.accept_forwards_to_priv_channels = true;
301 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
302 features.set_onion_messages_optional();
303 features.set_route_blinding_optional();
305 let chanmon_cfgs = create_chanmon_cfgs(6);
306 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
308 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
310 let node_chanmgrs = create_node_chanmgrs(
311 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
313 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
315 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
316 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
317 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
318 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
319 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
320 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
321 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
323 // Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
324 // more than the others.
325 create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
326 create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
328 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
329 let bob_id = bob.node.get_our_node_id();
331 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
332 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
335 .create_offer_builder().unwrap()
336 .amount_msats(10_000_000)
338 assert_ne!(offer.signing_pubkey(), Some(bob_id));
339 assert!(!offer.paths().is_empty());
340 for path in offer.paths() {
341 assert_eq!(path.introduction_node, IntroductionNode::NodeId(nodes[4].node.get_our_node_id()));
345 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
346 /// rather than exposing a node's pubkey.
348 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
349 let mut accept_forward_cfg = test_default_channel_config();
350 accept_forward_cfg.accept_forwards_to_priv_channels = true;
352 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
353 features.set_onion_messages_optional();
354 features.set_route_blinding_optional();
356 let chanmon_cfgs = create_chanmon_cfgs(6);
357 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
359 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
361 let node_chanmgrs = create_node_chanmgrs(
362 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
364 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
366 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
367 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
368 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
369 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
370 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
371 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
372 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
374 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
375 let alice_id = alice.node.get_our_node_id();
376 let bob_id = bob.node.get_our_node_id();
377 let charlie_id = charlie.node.get_our_node_id();
378 let david_id = david.node.get_our_node_id();
380 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
381 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
383 let offer = alice.node
384 .create_offer_builder()
386 .amount_msats(10_000_000)
388 assert_ne!(offer.signing_pubkey(), Some(alice_id));
389 assert!(!offer.paths().is_empty());
390 for path in offer.paths() {
391 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
394 let payment_id = PaymentId([1; 32]);
395 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
397 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
399 connect_peers(david, bob);
401 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
402 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
404 connect_peers(alice, charlie);
406 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
407 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
409 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
410 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
411 offer_id: offer.id(),
412 invoice_request: InvoiceRequestFields {
413 payer_id: invoice_request.payer_id(),
415 payer_note_truncated: None,
418 assert_eq!(invoice_request.amount_msats(), None);
419 assert_ne!(invoice_request.payer_id(), david_id);
420 assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(charlie_id));
422 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
423 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
425 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
426 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
428 let invoice = extract_invoice(david, &onion_message);
429 assert_eq!(invoice.amount_msats(), 10_000_000);
430 assert_ne!(invoice.signing_pubkey(), alice_id);
431 assert!(!invoice.payment_paths().is_empty());
432 for (_, path) in invoice.payment_paths() {
433 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
436 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
437 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
439 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
440 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
443 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
444 /// rather than exposing a node's pubkey.
446 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
447 let mut accept_forward_cfg = test_default_channel_config();
448 accept_forward_cfg.accept_forwards_to_priv_channels = true;
450 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
451 features.set_onion_messages_optional();
452 features.set_route_blinding_optional();
454 let chanmon_cfgs = create_chanmon_cfgs(6);
455 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
457 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
459 let node_chanmgrs = create_node_chanmgrs(
460 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
462 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
464 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
465 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
466 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
467 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
468 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
469 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
470 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
472 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
473 let alice_id = alice.node.get_our_node_id();
474 let bob_id = bob.node.get_our_node_id();
475 let charlie_id = charlie.node.get_our_node_id();
476 let david_id = david.node.get_our_node_id();
478 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
479 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
481 let absolute_expiry = Duration::from_secs(u64::MAX);
482 let payment_id = PaymentId([1; 32]);
483 let refund = david.node
484 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
487 assert_eq!(refund.amount_msats(), 10_000_000);
488 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
489 assert_ne!(refund.payer_id(), david_id);
490 assert!(!refund.paths().is_empty());
491 for path in refund.paths() {
492 assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
494 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
496 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
497 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
499 connect_peers(alice, charlie);
501 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
502 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
504 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
505 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
507 let invoice = extract_invoice(david, &onion_message);
508 assert_eq!(invoice, expected_invoice);
510 assert_eq!(invoice.amount_msats(), 10_000_000);
511 assert_ne!(invoice.signing_pubkey(), alice_id);
512 assert!(!invoice.payment_paths().is_empty());
513 for (_, path) in invoice.payment_paths() {
514 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
517 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
518 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
520 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
521 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
524 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
525 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
526 /// introduction node of the blinded path.
528 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
529 let chanmon_cfgs = create_chanmon_cfgs(2);
530 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
531 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
532 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
534 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
536 let alice = &nodes[0];
537 let alice_id = alice.node.get_our_node_id();
539 let bob_id = bob.node.get_our_node_id();
541 let offer = alice.node
542 .create_offer_builder().unwrap()
543 .amount_msats(10_000_000)
545 assert_ne!(offer.signing_pubkey(), Some(alice_id));
546 assert!(!offer.paths().is_empty());
547 for path in offer.paths() {
548 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
551 let payment_id = PaymentId([1; 32]);
552 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
553 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
555 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
556 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
558 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
559 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
560 offer_id: offer.id(),
561 invoice_request: InvoiceRequestFields {
562 payer_id: invoice_request.payer_id(),
564 payer_note_truncated: None,
567 assert_eq!(invoice_request.amount_msats(), None);
568 assert_ne!(invoice_request.payer_id(), bob_id);
569 assert_eq!(reply_path.unwrap().introduction_node, IntroductionNode::NodeId(bob_id));
571 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
572 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
574 let invoice = extract_invoice(bob, &onion_message);
575 assert_eq!(invoice.amount_msats(), 10_000_000);
576 assert_ne!(invoice.signing_pubkey(), alice_id);
577 assert!(!invoice.payment_paths().is_empty());
578 for (_, path) in invoice.payment_paths() {
579 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
582 route_bolt12_payment(bob, &[alice], &invoice);
583 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
585 claim_bolt12_payment(bob, &[alice], payment_context);
586 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
589 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
590 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
591 /// introduction node of the blinded path.
593 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
594 let chanmon_cfgs = create_chanmon_cfgs(2);
595 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
596 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
597 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
599 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
601 let alice = &nodes[0];
602 let alice_id = alice.node.get_our_node_id();
604 let bob_id = bob.node.get_our_node_id();
606 let absolute_expiry = Duration::from_secs(u64::MAX);
607 let payment_id = PaymentId([1; 32]);
608 let refund = bob.node
609 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
612 assert_eq!(refund.amount_msats(), 10_000_000);
613 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
614 assert_ne!(refund.payer_id(), bob_id);
615 assert!(!refund.paths().is_empty());
616 for path in refund.paths() {
617 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
619 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
621 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
622 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
624 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
625 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
627 let invoice = extract_invoice(bob, &onion_message);
628 assert_eq!(invoice, expected_invoice);
630 assert_eq!(invoice.amount_msats(), 10_000_000);
631 assert_ne!(invoice.signing_pubkey(), alice_id);
632 assert!(!invoice.payment_paths().is_empty());
633 for (_, path) in invoice.payment_paths() {
634 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
637 route_bolt12_payment(bob, &[alice], &invoice);
638 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
640 claim_bolt12_payment(bob, &[alice], payment_context);
641 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
644 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
645 /// the requested is sent directly using the node's pubkey, the response and the payment still use
646 /// blinded paths as required by the spec.
648 fn pays_for_offer_without_blinded_paths() {
649 let chanmon_cfgs = create_chanmon_cfgs(2);
650 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
651 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
652 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
654 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
656 let alice = &nodes[0];
657 let alice_id = alice.node.get_our_node_id();
659 let bob_id = bob.node.get_our_node_id();
661 let offer = alice.node
662 .create_offer_builder().unwrap()
664 .amount_msats(10_000_000)
666 assert_eq!(offer.signing_pubkey(), Some(alice_id));
667 assert!(offer.paths().is_empty());
669 let payment_id = PaymentId([1; 32]);
670 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
671 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
673 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
674 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
676 let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
677 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
678 offer_id: offer.id(),
679 invoice_request: InvoiceRequestFields {
680 payer_id: invoice_request.payer_id(),
682 payer_note_truncated: None,
686 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
687 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
689 let invoice = extract_invoice(bob, &onion_message);
690 route_bolt12_payment(bob, &[alice], &invoice);
691 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
693 claim_bolt12_payment(bob, &[alice], payment_context);
694 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
697 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
698 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
700 fn pays_for_refund_without_blinded_paths() {
701 let chanmon_cfgs = create_chanmon_cfgs(2);
702 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
703 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
704 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
706 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
708 let alice = &nodes[0];
709 let alice_id = alice.node.get_our_node_id();
711 let bob_id = bob.node.get_our_node_id();
713 let absolute_expiry = Duration::from_secs(u64::MAX);
714 let payment_id = PaymentId([1; 32]);
715 let refund = bob.node
716 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
720 assert_eq!(refund.payer_id(), bob_id);
721 assert!(refund.paths().is_empty());
722 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
724 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
725 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
727 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
728 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
730 let invoice = extract_invoice(bob, &onion_message);
731 assert_eq!(invoice, expected_invoice);
733 route_bolt12_payment(bob, &[alice], &invoice);
734 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
736 claim_bolt12_payment(bob, &[alice], payment_context);
737 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
740 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
742 fn fails_creating_offer_without_blinded_paths() {
743 let chanmon_cfgs = create_chanmon_cfgs(2);
744 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
745 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
746 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
748 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
750 match nodes[0].node.create_offer_builder() {
751 Ok(_) => panic!("Expected error"),
752 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
756 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
758 fn fails_creating_refund_without_blinded_paths() {
759 let chanmon_cfgs = create_chanmon_cfgs(2);
760 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
761 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
762 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
764 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
766 let absolute_expiry = Duration::from_secs(u64::MAX);
767 let payment_id = PaymentId([1; 32]);
769 match nodes[0].node.create_refund_builder(
770 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
772 Ok(_) => panic!("Expected error"),
773 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
776 assert!(nodes[0].node.list_recent_payments().is_empty());
779 /// Fails creating an invoice request when the offer contains an unsupported chain.
781 fn fails_creating_invoice_request_for_unsupported_chain() {
782 let chanmon_cfgs = create_chanmon_cfgs(2);
783 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
784 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
785 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
787 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
789 let alice = &nodes[0];
792 let offer = alice.node
793 .create_offer_builder().unwrap()
795 .chain(Network::Signet)
798 let payment_id = PaymentId([1; 32]);
799 match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
800 Ok(_) => panic!("Expected error"),
801 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
805 /// Fails requesting a payment when the refund contains an unsupported chain.
807 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
808 let chanmon_cfgs = create_chanmon_cfgs(2);
809 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
810 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
811 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
813 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
815 let alice = &nodes[0];
818 let absolute_expiry = Duration::from_secs(u64::MAX);
819 let payment_id = PaymentId([1; 32]);
820 let refund = bob.node
821 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
823 .chain(Network::Signet)
826 match alice.node.request_refund_payment(&refund) {
827 Ok(_) => panic!("Expected error"),
828 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
832 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
835 fn fails_creating_invoice_request_without_blinded_reply_path() {
836 let chanmon_cfgs = create_chanmon_cfgs(6);
837 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
838 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
839 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
841 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
842 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
843 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
844 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
845 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
847 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
849 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
850 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
852 let offer = alice.node
853 .create_offer_builder().unwrap()
854 .amount_msats(10_000_000)
857 let payment_id = PaymentId([1; 32]);
859 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
860 Ok(_) => panic!("Expected error"),
861 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
864 assert!(nodes[0].node.list_recent_payments().is_empty());
868 fn fails_creating_invoice_request_with_duplicate_payment_id() {
869 let chanmon_cfgs = create_chanmon_cfgs(6);
870 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
871 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
872 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
874 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
875 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
876 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
877 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
878 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
879 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
880 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
882 let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
884 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
886 let offer = alice.node
887 .create_offer_builder().unwrap()
888 .amount_msats(10_000_000)
891 let payment_id = PaymentId([1; 32]);
893 david.node.pay_for_offer(
894 &offer, None, None, None, payment_id, Retry::Attempts(0), None
897 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
899 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
900 Ok(_) => panic!("Expected error"),
901 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
904 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
908 fn fails_creating_refund_with_duplicate_payment_id() {
909 let chanmon_cfgs = create_chanmon_cfgs(2);
910 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
911 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
912 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
914 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
916 let absolute_expiry = Duration::from_secs(u64::MAX);
917 let payment_id = PaymentId([1; 32]);
919 nodes[0].node.create_refund_builder(
920 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
923 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
925 match nodes[0].node.create_refund_builder(
926 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
928 Ok(_) => panic!("Expected error"),
929 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
932 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
936 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
937 let mut accept_forward_cfg = test_default_channel_config();
938 accept_forward_cfg.accept_forwards_to_priv_channels = true;
940 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
941 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
942 features.set_onion_messages_optional();
943 features.clear_route_blinding();
945 let chanmon_cfgs = create_chanmon_cfgs(6);
946 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
948 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
950 let node_chanmgrs = create_node_chanmgrs(
951 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
953 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
955 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
956 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
957 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
958 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
959 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
960 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
961 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
963 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
964 let alice_id = alice.node.get_our_node_id();
965 let bob_id = bob.node.get_our_node_id();
966 let charlie_id = charlie.node.get_our_node_id();
967 let david_id = david.node.get_our_node_id();
969 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
970 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
972 let offer = alice.node
973 .create_offer_builder().unwrap()
974 .amount_msats(10_000_000)
977 let payment_id = PaymentId([1; 32]);
978 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
981 connect_peers(david, bob);
983 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
984 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
986 connect_peers(alice, charlie);
988 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
989 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
991 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
992 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
994 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
995 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
997 let invoice_error = extract_invoice_error(david, &onion_message);
998 assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
1002 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
1003 let mut accept_forward_cfg = test_default_channel_config();
1004 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1006 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1007 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1008 features.set_onion_messages_optional();
1009 features.clear_route_blinding();
1011 let chanmon_cfgs = create_chanmon_cfgs(6);
1012 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1014 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1016 let node_chanmgrs = create_node_chanmgrs(
1017 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1019 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1021 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1022 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1023 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1024 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1025 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1026 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1027 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1029 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1031 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1032 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1034 let absolute_expiry = Duration::from_secs(u64::MAX);
1035 let payment_id = PaymentId([1; 32]);
1036 let refund = david.node
1037 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1041 match alice.node.request_refund_payment(&refund) {
1042 Ok(_) => panic!("Expected error"),
1043 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1048 fn fails_paying_invoice_more_than_once() {
1049 let mut accept_forward_cfg = test_default_channel_config();
1050 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1052 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1053 features.set_onion_messages_optional();
1054 features.set_route_blinding_optional();
1056 let chanmon_cfgs = create_chanmon_cfgs(6);
1057 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1059 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1061 let node_chanmgrs = create_node_chanmgrs(
1062 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1064 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1066 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1067 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1068 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1069 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1070 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1071 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1072 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1074 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1075 let alice_id = alice.node.get_our_node_id();
1076 let bob_id = bob.node.get_our_node_id();
1077 let charlie_id = charlie.node.get_our_node_id();
1078 let david_id = david.node.get_our_node_id();
1080 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1081 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1083 let absolute_expiry = Duration::from_secs(u64::MAX);
1084 let payment_id = PaymentId([1; 32]);
1085 let refund = david.node
1086 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1089 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1091 // Alice sends the first invoice
1092 alice.node.request_refund_payment(&refund).unwrap();
1094 connect_peers(alice, charlie);
1096 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1097 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1099 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1100 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1102 // David pays the first invoice
1103 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
1104 let invoice1 = extract_invoice(david, &onion_message);
1106 route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1107 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1109 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
1110 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1112 disconnect_peers(alice, &[charlie]);
1114 // Alice sends the second invoice
1115 alice.node.request_refund_payment(&refund).unwrap();
1117 connect_peers(alice, charlie);
1118 connect_peers(david, bob);
1120 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1121 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1123 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1124 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1126 let invoice2 = extract_invoice(david, &onion_message);
1127 assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1129 // David sends an error instead of paying the second invoice
1130 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1131 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1133 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1134 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1136 let invoice_error = extract_invoice_error(alice, &onion_message);
1137 assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));