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::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};
63 use crate::prelude::*;
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);
71 Some(_) => panic!("Unexpected recent payment state"),
72 None => panic!("No recent payments"),
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();
82 features: node_a.init_features(&node_id_b),
84 remote_network_address: None,
87 features: node_b.init_features(&node_id_a),
89 remote_network_address: None,
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();
98 fn disconnect_peers<'a, 'b, 'c>(node_a: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>]) {
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());
107 fn announce_node_address<'a, 'b, 'c>(
108 node: &Node<'a, 'b, 'c>, peers: &[&Node<'a, 'b, 'c>], address: SocketAddress,
110 let features = node.onion_messenger.provided_node_features()
111 | node.gossip_sync.provided_node_features();
113 let announcement = UnsignedNodeAnnouncement {
116 node_id: NodeId::from_pubkey(&node.keys_manager.get_node_id(Recipient::Node).unwrap()),
118 alias: NodeAlias([0u8; 32]),
119 addresses: vec![address],
120 excess_address_data: Vec::new(),
121 excess_data: Vec::new(),
123 let signature = node.keys_manager.sign_gossip_message(
124 UnsignedGossipMessage::NodeAnnouncement(&announcement)
127 let msg = NodeAnnouncement {
129 contents: announcement
132 node.gossip_sync.handle_node_announcement(&msg).unwrap();
134 peer.gossip_sync.handle_node_announcement(&msg).unwrap();
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())
144 fn route_bolt12_payment<'a, 'b, 'c>(
145 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], invoice: &Bolt12Invoice
147 // Monitor added when handling the invoice onion message.
148 check_added_monitors(node, 1);
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);
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);
163 fn claim_bolt12_payment<'a, 'b, 'c>(
164 node: &Node<'a, 'b, 'c>, path: &[&Node<'a, 'b, 'c>], expected_payment_context: PaymentContext
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"),
171 let payment_preimage = match payment_purpose.preimage() {
172 Some(preimage) => preimage,
173 None => panic!("No preimage in Event::PaymentClaimable"),
175 match payment_purpose {
176 PaymentPurpose::Bolt12OfferPayment { payment_context, .. } => {
177 assert_eq!(PaymentContext::Bolt12Offer(payment_context), expected_payment_context);
179 PaymentPurpose::Bolt12RefundPayment { payment_context, .. } => {
180 assert_eq!(PaymentContext::Bolt12Refund(payment_context), expected_payment_context);
182 _ => panic!("Unexpected payment purpose: {:?}", payment_purpose),
184 claim_payment(node, path, payment_preimage);
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 #[cfg(async_payments)]
196 OffersMessage::StaticInvoice(invoice) => panic!("Unexpected static invoice: {:?}", invoice),
197 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
199 #[cfg(async_payments)]
200 ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
201 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
203 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
204 Err(e) => panic!("Failed to process onion message {:?}", e),
208 fn extract_invoice<'a, 'b, 'c>(node: &Node<'a, 'b, 'c>, message: &OnionMessage) -> Bolt12Invoice {
209 match node.onion_messenger.peel_onion_message(message) {
210 Ok(PeeledOnion::Receive(message, _, _)) => match message {
211 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
212 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
213 OffersMessage::Invoice(invoice) => invoice,
214 #[cfg(async_payments)]
215 OffersMessage::StaticInvoice(invoice) => panic!("Unexpected static invoice: {:?}", invoice),
216 OffersMessage::InvoiceError(error) => panic!("Unexpected invoice_error: {:?}", error),
218 #[cfg(async_payments)]
219 ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
220 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
222 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
223 Err(e) => panic!("Failed to process onion message {:?}", e),
227 fn extract_invoice_error<'a, 'b, 'c>(
228 node: &Node<'a, 'b, 'c>, message: &OnionMessage
230 match node.onion_messenger.peel_onion_message(message) {
231 Ok(PeeledOnion::Receive(message, _, _)) => match message {
232 ParsedOnionMessageContents::Offers(offers_message) => match offers_message {
233 OffersMessage::InvoiceRequest(invoice_request) => panic!("Unexpected invoice_request: {:?}", invoice_request),
234 OffersMessage::Invoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
235 #[cfg(async_payments)]
236 OffersMessage::StaticInvoice(invoice) => panic!("Unexpected invoice: {:?}", invoice),
237 OffersMessage::InvoiceError(error) => error,
239 #[cfg(async_payments)]
240 ParsedOnionMessageContents::AsyncPayments(message) => panic!("Unexpected async payments message: {:?}", message),
241 ParsedOnionMessageContents::Custom(message) => panic!("Unexpected custom message: {:?}", message),
243 Ok(PeeledOnion::Forward(_, _)) => panic!("Unexpected onion message forward"),
244 Err(e) => panic!("Failed to process onion message {:?}", e),
248 /// Checks that blinded paths without Tor-only nodes are preferred when constructing an offer.
250 fn prefers_non_tor_nodes_in_blinded_paths() {
251 let mut accept_forward_cfg = test_default_channel_config();
252 accept_forward_cfg.accept_forwards_to_priv_channels = true;
254 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
255 features.set_onion_messages_optional();
256 features.set_route_blinding_optional();
258 let chanmon_cfgs = create_chanmon_cfgs(6);
259 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
261 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
263 let node_chanmgrs = create_node_chanmgrs(
264 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
266 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
268 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
269 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
270 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
271 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
272 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
273 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
274 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
276 // Add an extra channel so that more than one of Bob's peers have MIN_PEER_CHANNELS.
277 create_announced_chan_between_nodes_with_value(&nodes, 4, 5, 10_000_000, 1_000_000_000);
279 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
280 let bob_id = bob.node.get_our_node_id();
281 let charlie_id = charlie.node.get_our_node_id();
283 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
284 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
286 let tor = SocketAddress::OnionV2([255, 254, 253, 252, 251, 250, 249, 248, 247, 246, 38, 7]);
287 announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
290 .create_offer_builder(None).unwrap()
291 .amount_msats(10_000_000)
293 assert_ne!(offer.signing_pubkey(), Some(bob_id));
294 assert!(!offer.paths().is_empty());
295 for path in offer.paths() {
296 let introduction_node_id = resolve_introduction_node(david, &path);
297 assert_ne!(introduction_node_id, bob_id);
298 assert_ne!(introduction_node_id, charlie_id);
301 // Use a one-hop blinded path when Bob is announced and all his peers are Tor-only.
302 announce_node_address(&nodes[4], &[alice, bob, charlie, david, &nodes[5]], tor.clone());
303 announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
306 .create_offer_builder(None).unwrap()
307 .amount_msats(10_000_000)
309 assert_ne!(offer.signing_pubkey(), Some(bob_id));
310 assert!(!offer.paths().is_empty());
311 for path in offer.paths() {
312 let introduction_node_id = resolve_introduction_node(david, &path);
313 assert_eq!(introduction_node_id, bob_id);
317 /// Checks that blinded paths prefer an introduction node that is the most connected.
319 fn prefers_more_connected_nodes_in_blinded_paths() {
320 let mut accept_forward_cfg = test_default_channel_config();
321 accept_forward_cfg.accept_forwards_to_priv_channels = true;
323 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
324 features.set_onion_messages_optional();
325 features.set_route_blinding_optional();
327 let chanmon_cfgs = create_chanmon_cfgs(6);
328 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
330 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
332 let node_chanmgrs = create_node_chanmgrs(
333 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
335 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
337 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
338 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
339 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
340 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
341 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
342 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
343 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
345 // Add extra channels so that more than one of Bob's peers have MIN_PEER_CHANNELS and one has
346 // more than the others.
347 create_announced_chan_between_nodes_with_value(&nodes, 0, 4, 10_000_000, 1_000_000_000);
348 create_announced_chan_between_nodes_with_value(&nodes, 3, 4, 10_000_000, 1_000_000_000);
350 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
351 let bob_id = bob.node.get_our_node_id();
353 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
354 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
357 .create_offer_builder(None).unwrap()
358 .amount_msats(10_000_000)
360 assert_ne!(offer.signing_pubkey(), Some(bob_id));
361 assert!(!offer.paths().is_empty());
362 for path in offer.paths() {
363 let introduction_node_id = resolve_introduction_node(david, &path);
364 assert_eq!(introduction_node_id, nodes[4].node.get_our_node_id());
368 /// Checks that blinded paths are compact for short-lived offers.
370 fn creates_short_lived_offer() {
371 let chanmon_cfgs = create_chanmon_cfgs(2);
372 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
373 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
374 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
376 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
378 let alice = &nodes[0];
379 let alice_id = alice.node.get_our_node_id();
382 let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
383 let offer = alice.node
384 .create_offer_builder(Some(absolute_expiry)).unwrap()
386 assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
387 assert!(!offer.paths().is_empty());
388 for path in offer.paths() {
389 let introduction_node_id = resolve_introduction_node(bob, &path);
390 assert_eq!(introduction_node_id, alice_id);
391 assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
395 /// Checks that blinded paths are not compact for long-lived offers.
397 fn creates_long_lived_offer() {
398 let chanmon_cfgs = create_chanmon_cfgs(2);
399 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
400 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
401 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
403 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
405 let alice = &nodes[0];
406 let alice_id = alice.node.get_our_node_id();
408 let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
409 + Duration::from_secs(1);
410 let offer = alice.node
411 .create_offer_builder(Some(absolute_expiry))
414 assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
415 assert!(!offer.paths().is_empty());
416 for path in offer.paths() {
417 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
420 let offer = alice.node
421 .create_offer_builder(None).unwrap()
423 assert_eq!(offer.absolute_expiry(), None);
424 assert!(!offer.paths().is_empty());
425 for path in offer.paths() {
426 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
430 /// Checks that blinded paths are compact for short-lived refunds.
432 fn creates_short_lived_refund() {
433 let chanmon_cfgs = create_chanmon_cfgs(2);
434 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
435 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
436 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
438 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
440 let alice = &nodes[0];
442 let bob_id = bob.node.get_our_node_id();
444 let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
445 let payment_id = PaymentId([1; 32]);
446 let refund = bob.node
447 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
450 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
451 assert!(!refund.paths().is_empty());
452 for path in refund.paths() {
453 let introduction_node_id = resolve_introduction_node(alice, &path);
454 assert_eq!(introduction_node_id, bob_id);
455 assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
459 /// Checks that blinded paths are not compact for long-lived refunds.
461 fn creates_long_lived_refund() {
462 let chanmon_cfgs = create_chanmon_cfgs(2);
463 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
464 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
465 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
467 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
470 let bob_id = bob.node.get_our_node_id();
472 let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
473 + Duration::from_secs(1);
474 let payment_id = PaymentId([1; 32]);
475 let refund = bob.node
476 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
479 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
480 assert!(!refund.paths().is_empty());
481 for path in refund.paths() {
482 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
486 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
487 /// rather than exposing a node's pubkey.
489 fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
490 let mut accept_forward_cfg = test_default_channel_config();
491 accept_forward_cfg.accept_forwards_to_priv_channels = true;
493 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
494 features.set_onion_messages_optional();
495 features.set_route_blinding_optional();
497 let chanmon_cfgs = create_chanmon_cfgs(6);
498 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
500 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
502 let node_chanmgrs = create_node_chanmgrs(
503 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
505 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
507 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
508 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
509 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
510 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
511 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
512 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
513 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
515 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
516 let alice_id = alice.node.get_our_node_id();
517 let bob_id = bob.node.get_our_node_id();
518 let charlie_id = charlie.node.get_our_node_id();
519 let david_id = david.node.get_our_node_id();
521 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
522 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
524 let offer = alice.node
525 .create_offer_builder(None)
527 .amount_msats(10_000_000)
529 assert_ne!(offer.signing_pubkey(), Some(alice_id));
530 assert!(!offer.paths().is_empty());
531 for path in offer.paths() {
532 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
535 let payment_id = PaymentId([1; 32]);
536 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
538 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
540 connect_peers(david, bob);
542 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
543 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
545 connect_peers(alice, charlie);
547 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
548 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
550 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
551 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
552 offer_id: offer.id(),
553 invoice_request: InvoiceRequestFields {
554 payer_id: invoice_request.payer_id(),
556 payer_note_truncated: None,
559 assert_eq!(invoice_request.amount_msats(), None);
560 assert_ne!(invoice_request.payer_id(), david_id);
561 assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(charlie_id));
563 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
564 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
566 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
567 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
569 let invoice = extract_invoice(david, &onion_message);
570 assert_eq!(invoice.amount_msats(), 10_000_000);
571 assert_ne!(invoice.signing_pubkey(), alice_id);
572 assert!(!invoice.payment_paths().is_empty());
573 for (_, path) in invoice.payment_paths() {
574 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
577 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
578 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
580 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
581 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
584 /// Checks that a refund can be paid through blinded paths and that ephemeral pubkeys are used
585 /// rather than exposing a node's pubkey.
587 fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
588 let mut accept_forward_cfg = test_default_channel_config();
589 accept_forward_cfg.accept_forwards_to_priv_channels = true;
591 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
592 features.set_onion_messages_optional();
593 features.set_route_blinding_optional();
595 let chanmon_cfgs = create_chanmon_cfgs(6);
596 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
598 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
600 let node_chanmgrs = create_node_chanmgrs(
601 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
603 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
605 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
606 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
607 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
608 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
609 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
610 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
611 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
613 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
614 let alice_id = alice.node.get_our_node_id();
615 let bob_id = bob.node.get_our_node_id();
616 let charlie_id = charlie.node.get_our_node_id();
617 let david_id = david.node.get_our_node_id();
619 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
620 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
622 let absolute_expiry = Duration::from_secs(u64::MAX);
623 let payment_id = PaymentId([1; 32]);
624 let refund = david.node
625 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
628 assert_eq!(refund.amount_msats(), 10_000_000);
629 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
630 assert_ne!(refund.payer_id(), david_id);
631 assert!(!refund.paths().is_empty());
632 for path in refund.paths() {
633 assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
635 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
637 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
638 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
640 connect_peers(alice, charlie);
642 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
643 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
645 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
646 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
648 let invoice = extract_invoice(david, &onion_message);
649 assert_eq!(invoice, expected_invoice);
651 assert_eq!(invoice.amount_msats(), 10_000_000);
652 assert_ne!(invoice.signing_pubkey(), alice_id);
653 assert!(!invoice.payment_paths().is_empty());
654 for (_, path) in invoice.payment_paths() {
655 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
658 route_bolt12_payment(david, &[charlie, bob, alice], &invoice);
659 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
661 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
662 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
665 /// Checks that an offer can be paid through a one-hop blinded path and that ephemeral pubkeys are
666 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
667 /// introduction node of the blinded path.
669 fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
670 let chanmon_cfgs = create_chanmon_cfgs(2);
671 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
672 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
673 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
675 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
677 let alice = &nodes[0];
678 let alice_id = alice.node.get_our_node_id();
680 let bob_id = bob.node.get_our_node_id();
682 let offer = alice.node
683 .create_offer_builder(None).unwrap()
684 .amount_msats(10_000_000)
686 assert_ne!(offer.signing_pubkey(), Some(alice_id));
687 assert!(!offer.paths().is_empty());
688 for path in offer.paths() {
689 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
692 let payment_id = PaymentId([1; 32]);
693 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
694 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
696 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
697 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
699 let (invoice_request, reply_path) = extract_invoice_request(alice, &onion_message);
700 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
701 offer_id: offer.id(),
702 invoice_request: InvoiceRequestFields {
703 payer_id: invoice_request.payer_id(),
705 payer_note_truncated: None,
708 assert_eq!(invoice_request.amount_msats(), None);
709 assert_ne!(invoice_request.payer_id(), bob_id);
710 assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(bob_id));
712 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
713 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
715 let invoice = extract_invoice(bob, &onion_message);
716 assert_eq!(invoice.amount_msats(), 10_000_000);
717 assert_ne!(invoice.signing_pubkey(), alice_id);
718 assert!(!invoice.payment_paths().is_empty());
719 for (_, path) in invoice.payment_paths() {
720 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
723 route_bolt12_payment(bob, &[alice], &invoice);
724 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
726 claim_bolt12_payment(bob, &[alice], payment_context);
727 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
730 /// Checks that a refund can be paid through a one-hop blinded path and that ephemeral pubkeys are
731 /// used rather than exposing a node's pubkey. However, the node's pubkey is still used as the
732 /// introduction node of the blinded path.
734 fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
735 let chanmon_cfgs = create_chanmon_cfgs(2);
736 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
737 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
738 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
740 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
742 let alice = &nodes[0];
743 let alice_id = alice.node.get_our_node_id();
745 let bob_id = bob.node.get_our_node_id();
747 let absolute_expiry = Duration::from_secs(u64::MAX);
748 let payment_id = PaymentId([1; 32]);
749 let refund = bob.node
750 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
753 assert_eq!(refund.amount_msats(), 10_000_000);
754 assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
755 assert_ne!(refund.payer_id(), bob_id);
756 assert!(!refund.paths().is_empty());
757 for path in refund.paths() {
758 assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
760 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
762 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
763 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
765 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
766 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
768 let invoice = extract_invoice(bob, &onion_message);
769 assert_eq!(invoice, expected_invoice);
771 assert_eq!(invoice.amount_msats(), 10_000_000);
772 assert_ne!(invoice.signing_pubkey(), alice_id);
773 assert!(!invoice.payment_paths().is_empty());
774 for (_, path) in invoice.payment_paths() {
775 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
778 route_bolt12_payment(bob, &[alice], &invoice);
779 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
781 claim_bolt12_payment(bob, &[alice], payment_context);
782 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
785 /// Checks that an invoice for an offer without any blinded paths can be requested. Note that while
786 /// the requested is sent directly using the node's pubkey, the response and the payment still use
787 /// blinded paths as required by the spec.
789 fn pays_for_offer_without_blinded_paths() {
790 let chanmon_cfgs = create_chanmon_cfgs(2);
791 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
792 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
793 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
795 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
797 let alice = &nodes[0];
798 let alice_id = alice.node.get_our_node_id();
800 let bob_id = bob.node.get_our_node_id();
802 let offer = alice.node
803 .create_offer_builder(None).unwrap()
805 .amount_msats(10_000_000)
807 assert_eq!(offer.signing_pubkey(), Some(alice_id));
808 assert!(offer.paths().is_empty());
810 let payment_id = PaymentId([1; 32]);
811 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
812 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
814 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
815 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
817 let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
818 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
819 offer_id: offer.id(),
820 invoice_request: InvoiceRequestFields {
821 payer_id: invoice_request.payer_id(),
823 payer_note_truncated: None,
827 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
828 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
830 let invoice = extract_invoice(bob, &onion_message);
831 route_bolt12_payment(bob, &[alice], &invoice);
832 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
834 claim_bolt12_payment(bob, &[alice], payment_context);
835 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
838 /// Checks that a refund without any blinded paths can be paid. Note that while the invoice is sent
839 /// directly using the node's pubkey, the payment still use blinded paths as required by the spec.
841 fn pays_for_refund_without_blinded_paths() {
842 let chanmon_cfgs = create_chanmon_cfgs(2);
843 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
844 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
845 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
847 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
849 let alice = &nodes[0];
850 let alice_id = alice.node.get_our_node_id();
852 let bob_id = bob.node.get_our_node_id();
854 let absolute_expiry = Duration::from_secs(u64::MAX);
855 let payment_id = PaymentId([1; 32]);
856 let refund = bob.node
857 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
861 assert_eq!(refund.payer_id(), bob_id);
862 assert!(refund.paths().is_empty());
863 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
865 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
866 let expected_invoice = alice.node.request_refund_payment(&refund).unwrap();
868 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
869 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
871 let invoice = extract_invoice(bob, &onion_message);
872 assert_eq!(invoice, expected_invoice);
874 route_bolt12_payment(bob, &[alice], &invoice);
875 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
877 claim_bolt12_payment(bob, &[alice], payment_context);
878 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
881 /// Checks that a deferred invoice can be paid asynchronously from an Event::InvoiceReceived.
883 fn pays_bolt12_invoice_asynchronously() {
884 let mut manually_pay_cfg = test_default_channel_config();
885 manually_pay_cfg.manually_handle_bolt12_invoices = true;
887 let chanmon_cfgs = create_chanmon_cfgs(2);
888 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
889 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_pay_cfg)]);
890 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
892 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
894 let alice = &nodes[0];
895 let alice_id = alice.node.get_our_node_id();
897 let bob_id = bob.node.get_our_node_id();
899 let offer = alice.node
900 .create_offer_builder(None).unwrap()
901 .amount_msats(10_000_000)
904 let payment_id = PaymentId([1; 32]);
905 bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None).unwrap();
906 expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
908 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
909 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
911 let (invoice_request, _) = extract_invoice_request(alice, &onion_message);
912 let payment_context = PaymentContext::Bolt12Offer(Bolt12OfferContext {
913 offer_id: offer.id(),
914 invoice_request: InvoiceRequestFields {
915 payer_id: invoice_request.payer_id(),
917 payer_note_truncated: None,
921 let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
922 bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
924 let invoice = match get_event!(bob, Event::InvoiceReceived) {
925 Event::InvoiceReceived { payment_id: actual_payment_id, invoice, .. } => {
926 assert_eq!(actual_payment_id, payment_id);
929 _ => panic!("No Event::InvoiceReceived"),
931 assert_eq!(invoice.amount_msats(), 10_000_000);
932 assert_ne!(invoice.signing_pubkey(), alice_id);
933 assert!(!invoice.payment_paths().is_empty());
934 for (_, path) in invoice.payment_paths() {
935 assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
938 assert!(bob.node.send_payment_for_bolt12_invoice(&invoice).is_ok());
940 bob.node.send_payment_for_bolt12_invoice(&invoice),
941 Err(Bolt12PaymentError::DuplicateInvoice),
944 route_bolt12_payment(bob, &[alice], &invoice);
945 expect_recent_payment!(bob, RecentPaymentDetails::Pending, payment_id);
947 claim_bolt12_payment(bob, &[alice], payment_context);
948 expect_recent_payment!(bob, RecentPaymentDetails::Fulfilled, payment_id);
951 bob.node.send_payment_for_bolt12_invoice(&invoice),
952 Err(Bolt12PaymentError::DuplicateInvoice),
955 for _ in 0..=IDEMPOTENCY_TIMEOUT_TICKS {
956 bob.node.timer_tick_occurred();
960 bob.node.send_payment_for_bolt12_invoice(&invoice),
961 Err(Bolt12PaymentError::UnexpectedInvoice),
965 /// Fails creating an offer when a blinded path cannot be created without exposing the node's id.
967 fn fails_creating_offer_without_blinded_paths() {
968 let chanmon_cfgs = create_chanmon_cfgs(2);
969 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
970 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
971 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
973 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
975 match nodes[0].node.create_offer_builder(None) {
976 Ok(_) => panic!("Expected error"),
977 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
981 /// Fails creating a refund when a blinded path cannot be created without exposing the node's id.
983 fn fails_creating_refund_without_blinded_paths() {
984 let chanmon_cfgs = create_chanmon_cfgs(2);
985 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
986 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
987 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
989 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
991 let absolute_expiry = Duration::from_secs(u64::MAX);
992 let payment_id = PaymentId([1; 32]);
994 match nodes[0].node.create_refund_builder(
995 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
997 Ok(_) => panic!("Expected error"),
998 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1001 assert!(nodes[0].node.list_recent_payments().is_empty());
1004 /// Fails creating or paying an offer when a blinded path cannot be created because no peers are
1007 fn fails_creating_or_paying_for_offer_without_connected_peers() {
1008 let chanmon_cfgs = create_chanmon_cfgs(6);
1009 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1010 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1011 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1013 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1014 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1015 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1016 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1017 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1018 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1019 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1021 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1023 disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
1024 disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1026 let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1027 match alice.node.create_offer_builder(Some(absolute_expiry)) {
1028 Ok(_) => panic!("Expected error"),
1029 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1032 let mut args = ReconnectArgs::new(alice, bob);
1033 args.send_channel_ready = (true, true);
1034 reconnect_nodes(args);
1036 let offer = alice.node
1037 .create_offer_builder(Some(absolute_expiry)).unwrap()
1038 .amount_msats(10_000_000)
1041 let payment_id = PaymentId([1; 32]);
1043 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1044 Ok(_) => panic!("Expected error"),
1045 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1048 assert!(nodes[0].node.list_recent_payments().is_empty());
1050 let mut args = ReconnectArgs::new(charlie, david);
1051 args.send_channel_ready = (true, true);
1052 reconnect_nodes(args);
1055 david.node.pay_for_offer(
1056 &offer, None, None, None, payment_id, Retry::Attempts(0), None
1060 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1063 /// Fails creating or sending an invoice for a refund when a blinded path cannot be created because
1064 /// no peers are connected.
1066 fn fails_creating_refund_or_sending_invoice_without_connected_peers() {
1067 let mut accept_forward_cfg = test_default_channel_config();
1068 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1070 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1071 features.set_onion_messages_optional();
1072 features.set_route_blinding_optional();
1074 let chanmon_cfgs = create_chanmon_cfgs(6);
1075 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1077 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1079 let node_chanmgrs = create_node_chanmgrs(
1080 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1082 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1084 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1085 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1086 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1087 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1088 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1089 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1090 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1092 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1094 disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
1095 disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
1097 let absolute_expiry = david.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
1098 let payment_id = PaymentId([1; 32]);
1099 match david.node.create_refund_builder(
1100 10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1102 Ok(_) => panic!("Expected error"),
1103 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1106 let mut args = ReconnectArgs::new(charlie, david);
1107 args.send_channel_ready = (true, true);
1108 reconnect_nodes(args);
1110 let refund = david.node
1111 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1115 match alice.node.request_refund_payment(&refund) {
1116 Ok(_) => panic!("Expected error"),
1117 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1120 let mut args = ReconnectArgs::new(alice, bob);
1121 args.send_channel_ready = (true, true);
1122 reconnect_nodes(args);
1124 assert!(alice.node.request_refund_payment(&refund).is_ok());
1127 /// Fails creating an invoice request when the offer contains an unsupported chain.
1129 fn fails_creating_invoice_request_for_unsupported_chain() {
1130 let chanmon_cfgs = create_chanmon_cfgs(2);
1131 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1132 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1133 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1135 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1137 let alice = &nodes[0];
1138 let bob = &nodes[1];
1140 let offer = alice.node
1141 .create_offer_builder(None).unwrap()
1143 .chain(Network::Signet)
1146 let payment_id = PaymentId([1; 32]);
1147 match bob.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1148 Ok(_) => panic!("Expected error"),
1149 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
1153 /// Fails requesting a payment when the refund contains an unsupported chain.
1155 fn fails_sending_invoice_with_unsupported_chain_for_refund() {
1156 let chanmon_cfgs = create_chanmon_cfgs(2);
1157 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1158 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1159 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1161 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1163 let alice = &nodes[0];
1164 let bob = &nodes[1];
1166 let absolute_expiry = Duration::from_secs(u64::MAX);
1167 let payment_id = PaymentId([1; 32]);
1168 let refund = bob.node
1169 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1171 .chain(Network::Signet)
1174 match alice.node.request_refund_payment(&refund) {
1175 Ok(_) => panic!("Expected error"),
1176 Err(e) => assert_eq!(e, Bolt12SemanticError::UnsupportedChain),
1180 /// Fails creating an invoice request when a blinded reply path cannot be created without exposing
1183 fn fails_creating_invoice_request_without_blinded_reply_path() {
1184 let chanmon_cfgs = create_chanmon_cfgs(6);
1185 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1186 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1187 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1189 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1190 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1191 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1192 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1193 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1195 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1197 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1198 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1200 let offer = alice.node
1201 .create_offer_builder(None).unwrap()
1202 .amount_msats(10_000_000)
1205 let payment_id = PaymentId([1; 32]);
1207 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1208 Ok(_) => panic!("Expected error"),
1209 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1212 assert!(nodes[0].node.list_recent_payments().is_empty());
1216 fn fails_creating_invoice_request_with_duplicate_payment_id() {
1217 let chanmon_cfgs = create_chanmon_cfgs(6);
1218 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1219 let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
1220 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1222 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1223 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1224 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1225 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1226 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1227 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1228 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1230 let (alice, _bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1232 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1234 let offer = alice.node
1235 .create_offer_builder(None).unwrap()
1236 .amount_msats(10_000_000)
1239 let payment_id = PaymentId([1; 32]);
1241 david.node.pay_for_offer(
1242 &offer, None, None, None, payment_id, Retry::Attempts(0), None
1245 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1247 match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
1248 Ok(_) => panic!("Expected error"),
1249 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
1252 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1256 fn fails_creating_refund_with_duplicate_payment_id() {
1257 let chanmon_cfgs = create_chanmon_cfgs(2);
1258 let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1259 let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1260 let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1262 create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1264 let absolute_expiry = Duration::from_secs(u64::MAX);
1265 let payment_id = PaymentId([1; 32]);
1267 nodes[0].node.create_refund_builder(
1268 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1271 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
1273 match nodes[0].node.create_refund_builder(
1274 10_000, absolute_expiry, payment_id, Retry::Attempts(0), None
1276 Ok(_) => panic!("Expected error"),
1277 Err(e) => assert_eq!(e, Bolt12SemanticError::DuplicatePaymentId),
1280 expect_recent_payment!(nodes[0], RecentPaymentDetails::AwaitingInvoice, payment_id);
1284 fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
1285 let mut accept_forward_cfg = test_default_channel_config();
1286 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1288 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1289 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1290 features.set_onion_messages_optional();
1291 features.clear_route_blinding();
1293 let chanmon_cfgs = create_chanmon_cfgs(6);
1294 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1296 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1298 let node_chanmgrs = create_node_chanmgrs(
1299 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1301 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1303 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1304 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1305 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1306 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1307 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1308 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1309 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1311 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1312 let alice_id = alice.node.get_our_node_id();
1313 let bob_id = bob.node.get_our_node_id();
1314 let charlie_id = charlie.node.get_our_node_id();
1315 let david_id = david.node.get_our_node_id();
1317 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1318 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1320 let offer = alice.node
1321 .create_offer_builder(None).unwrap()
1322 .amount_msats(10_000_000)
1325 let payment_id = PaymentId([1; 32]);
1326 david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None)
1329 connect_peers(david, bob);
1331 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1332 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1334 connect_peers(alice, charlie);
1336 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1337 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1339 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1340 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1342 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1343 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1345 let invoice_error = extract_invoice_error(david, &onion_message);
1346 assert_eq!(invoice_error, InvoiceError::from(Bolt12SemanticError::MissingPaths));
1350 fn fails_sending_invoice_without_blinded_payment_paths_for_refund() {
1351 let mut accept_forward_cfg = test_default_channel_config();
1352 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1354 // Clearing route_blinding prevents forming any payment paths since the node is unannounced.
1355 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1356 features.set_onion_messages_optional();
1357 features.clear_route_blinding();
1359 let chanmon_cfgs = create_chanmon_cfgs(6);
1360 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1362 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1364 let node_chanmgrs = create_node_chanmgrs(
1365 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1367 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1369 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1370 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1371 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1372 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1373 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1374 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1375 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1377 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1379 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1380 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1382 let absolute_expiry = Duration::from_secs(u64::MAX);
1383 let payment_id = PaymentId([1; 32]);
1384 let refund = david.node
1385 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1389 match alice.node.request_refund_payment(&refund) {
1390 Ok(_) => panic!("Expected error"),
1391 Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
1396 fn fails_paying_invoice_more_than_once() {
1397 let mut accept_forward_cfg = test_default_channel_config();
1398 accept_forward_cfg.accept_forwards_to_priv_channels = true;
1400 let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
1401 features.set_onion_messages_optional();
1402 features.set_route_blinding_optional();
1404 let chanmon_cfgs = create_chanmon_cfgs(6);
1405 let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
1407 *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
1409 let node_chanmgrs = create_node_chanmgrs(
1410 6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
1412 let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
1414 create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
1415 create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
1416 create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
1417 create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
1418 create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
1419 create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
1420 create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
1422 let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
1423 let alice_id = alice.node.get_our_node_id();
1424 let bob_id = bob.node.get_our_node_id();
1425 let charlie_id = charlie.node.get_our_node_id();
1426 let david_id = david.node.get_our_node_id();
1428 disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
1429 disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
1431 let absolute_expiry = Duration::from_secs(u64::MAX);
1432 let payment_id = PaymentId([1; 32]);
1433 let refund = david.node
1434 .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
1437 expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
1439 // Alice sends the first invoice
1440 alice.node.request_refund_payment(&refund).unwrap();
1442 connect_peers(alice, charlie);
1444 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1445 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1447 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1448 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1450 // David pays the first invoice
1451 let payment_context = PaymentContext::Bolt12Refund(Bolt12RefundContext {});
1452 let invoice1 = extract_invoice(david, &onion_message);
1454 route_bolt12_payment(david, &[charlie, bob, alice], &invoice1);
1455 expect_recent_payment!(david, RecentPaymentDetails::Pending, payment_id);
1457 claim_bolt12_payment(david, &[charlie, bob, alice], payment_context);
1458 expect_recent_payment!(david, RecentPaymentDetails::Fulfilled, payment_id);
1460 disconnect_peers(alice, &[charlie]);
1462 // Alice sends the second invoice
1463 alice.node.request_refund_payment(&refund).unwrap();
1465 connect_peers(alice, charlie);
1466 connect_peers(david, bob);
1468 let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
1469 charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
1471 let onion_message = charlie.onion_messenger.next_onion_message_for_peer(david_id).unwrap();
1472 david.onion_messenger.handle_onion_message(&charlie_id, &onion_message);
1474 let invoice2 = extract_invoice(david, &onion_message);
1475 assert_eq!(invoice1.payer_metadata(), invoice2.payer_metadata());
1477 // David sends an error instead of paying the second invoice
1478 let onion_message = david.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
1479 bob.onion_messenger.handle_onion_message(&david_id, &onion_message);
1481 let onion_message = bob.onion_messenger.next_onion_message_for_peer(alice_id).unwrap();
1482 alice.onion_messenger.handle_onion_message(&bob_id, &onion_message);
1484 let invoice_error = extract_invoice_error(alice, &onion_message);
1485 assert_eq!(invoice_error, InvoiceError::from_string("DuplicateInvoice".to_string()));