Unblock channels awaiting monitor update based on `ChanMan` queue
[rust-lightning] / lightning / src / ln / offers_tests.rs
index eedd82c569be4acdb1c0d50390db02c7819e5b18..c7fb5f8fd59b09fda9784f9474e066f5de11c28b 100644 (file)
@@ -46,7 +46,7 @@ use core::time::Duration;
 use crate::blinded_path::{BlindedPath, IntroductionNode};
 use crate::blinded_path::payment::{Bolt12OfferContext, Bolt12RefundContext, PaymentContext};
 use crate::events::{Event, MessageSendEventsProvider, PaymentPurpose};
-use crate::ln::channelmanager::{PaymentId, RecentPaymentDetails, Retry, self};
+use crate::ln::channelmanager::{MAX_SHORT_LIVED_RELATIVE_EXPIRY, PaymentId, RecentPaymentDetails, Retry, self};
 use crate::ln::functional_test_utils::*;
 use crate::ln::msgs::{ChannelMessageHandler, Init, NodeAnnouncement, OnionMessage, OnionMessageHandler, RoutingMessageHandler, SocketAddress, UnsignedGossipMessage, UnsignedNodeAnnouncement};
 use crate::offers::invoice::Bolt12Invoice;
@@ -274,7 +274,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
        announce_node_address(charlie, &[alice, bob, david, &nodes[4], &nodes[5]], tor.clone());
 
        let offer = bob.node
-               .create_offer_builder().unwrap()
+               .create_offer_builder(None).unwrap()
                .amount_msats(10_000_000)
                .build().unwrap();
        assert_ne!(offer.signing_pubkey(), Some(bob_id));
@@ -290,7 +290,7 @@ fn prefers_non_tor_nodes_in_blinded_paths() {
        announce_node_address(&nodes[5], &[alice, bob, charlie, david, &nodes[4]], tor.clone());
 
        let offer = bob.node
-               .create_offer_builder().unwrap()
+               .create_offer_builder(None).unwrap()
                .amount_msats(10_000_000)
                .build().unwrap();
        assert_ne!(offer.signing_pubkey(), Some(bob_id));
@@ -341,7 +341,7 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
        disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
 
        let offer = bob.node
-               .create_offer_builder().unwrap()
+               .create_offer_builder(None).unwrap()
                .amount_msats(10_000_000)
                .build().unwrap();
        assert_ne!(offer.signing_pubkey(), Some(bob_id));
@@ -352,6 +352,124 @@ fn prefers_more_connected_nodes_in_blinded_paths() {
        }
 }
 
+/// Checks that blinded paths are compact for short-lived offers.
+#[test]
+fn creates_short_lived_offer() {
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
+
+       let alice = &nodes[0];
+       let alice_id = alice.node.get_our_node_id();
+       let bob = &nodes[1];
+
+       let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
+       let offer = alice.node
+               .create_offer_builder(Some(absolute_expiry)).unwrap()
+               .build().unwrap();
+       assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
+       assert!(!offer.paths().is_empty());
+       for path in offer.paths() {
+               let introduction_node_id = resolve_introduction_node(bob, &path);
+               assert_eq!(introduction_node_id, alice_id);
+               assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
+       }
+}
+
+/// Checks that blinded paths are not compact for long-lived offers.
+#[test]
+fn creates_long_lived_offer() {
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
+
+       let alice = &nodes[0];
+       let alice_id = alice.node.get_our_node_id();
+
+       let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
+               + Duration::from_secs(1);
+       let offer = alice.node
+               .create_offer_builder(Some(absolute_expiry))
+               .unwrap()
+               .build().unwrap();
+       assert_eq!(offer.absolute_expiry(), Some(absolute_expiry));
+       assert!(!offer.paths().is_empty());
+       for path in offer.paths() {
+               assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
+       }
+
+       let offer = alice.node
+               .create_offer_builder(None).unwrap()
+               .build().unwrap();
+       assert_eq!(offer.absolute_expiry(), None);
+       assert!(!offer.paths().is_empty());
+       for path in offer.paths() {
+               assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
+       }
+}
+
+/// Checks that blinded paths are compact for short-lived refunds.
+#[test]
+fn creates_short_lived_refund() {
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
+
+       let alice = &nodes[0];
+       let bob = &nodes[1];
+       let bob_id = bob.node.get_our_node_id();
+
+       let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
+       let payment_id = PaymentId([1; 32]);
+       let refund = bob.node
+               .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
+               .unwrap()
+               .build().unwrap();
+       assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
+       assert!(!refund.paths().is_empty());
+       for path in refund.paths() {
+               let introduction_node_id = resolve_introduction_node(alice, &path);
+               assert_eq!(introduction_node_id, bob_id);
+               assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
+       }
+}
+
+/// Checks that blinded paths are not compact for long-lived refunds.
+#[test]
+fn creates_long_lived_refund() {
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
+
+       let bob = &nodes[1];
+       let bob_id = bob.node.get_our_node_id();
+
+       let absolute_expiry = bob.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY
+               + Duration::from_secs(1);
+       let payment_id = PaymentId([1; 32]);
+       let refund = bob.node
+               .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
+               .unwrap()
+               .build().unwrap();
+       assert_eq!(refund.absolute_expiry(), Some(absolute_expiry));
+       assert!(!refund.paths().is_empty());
+       for path in refund.paths() {
+               assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
+       }
+}
+
 /// Checks that an offer can be paid through blinded paths and that ephemeral pubkeys are used
 /// rather than exposing a node's pubkey.
 #[test]
@@ -391,16 +509,14 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
        disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
 
        let offer = alice.node
-               .create_offer_builder()
+               .create_offer_builder(None)
                .unwrap()
                .amount_msats(10_000_000)
                .build().unwrap();
        assert_ne!(offer.signing_pubkey(), Some(alice_id));
        assert!(!offer.paths().is_empty());
        for path in offer.paths() {
-               let introduction_node_id = resolve_introduction_node(david, &path);
-               assert_eq!(introduction_node_id, bob_id);
-               assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
+               assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
        }
 
        let payment_id = PaymentId([1; 32]);
@@ -427,11 +543,9 @@ fn creates_and_pays_for_offer_using_two_hop_blinded_path() {
                        payer_note_truncated: None,
                },
        });
-       let introduction_node_id = resolve_introduction_node(alice, &reply_path);
        assert_eq!(invoice_request.amount_msats(), None);
        assert_ne!(invoice_request.payer_id(), david_id);
-       assert_eq!(introduction_node_id, charlie_id);
-       assert!(matches!(reply_path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
+       assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(charlie_id));
 
        let onion_message = alice.onion_messenger.next_onion_message_for_peer(charlie_id).unwrap();
        charlie.onion_messenger.handle_onion_message(&alice_id, &onion_message);
@@ -503,9 +617,7 @@ fn creates_and_pays_for_refund_using_two_hop_blinded_path() {
        assert_ne!(refund.payer_id(), david_id);
        assert!(!refund.paths().is_empty());
        for path in refund.paths() {
-               let introduction_node_id = resolve_introduction_node(alice, &path);
-               assert_eq!(introduction_node_id, charlie_id);
-               assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
+               assert_eq!(path.introduction_node, IntroductionNode::NodeId(charlie_id));
        }
        expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
 
@@ -555,15 +667,13 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
        let bob_id = bob.node.get_our_node_id();
 
        let offer = alice.node
-               .create_offer_builder().unwrap()
+               .create_offer_builder(None).unwrap()
                .amount_msats(10_000_000)
                .build().unwrap();
        assert_ne!(offer.signing_pubkey(), Some(alice_id));
        assert!(!offer.paths().is_empty());
        for path in offer.paths() {
-               let introduction_node_id = resolve_introduction_node(bob, &path);
-               assert_eq!(introduction_node_id, alice_id);
-               assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
+               assert_eq!(path.introduction_node, IntroductionNode::NodeId(alice_id));
        }
 
        let payment_id = PaymentId([1; 32]);
@@ -582,11 +692,9 @@ fn creates_and_pays_for_offer_using_one_hop_blinded_path() {
                        payer_note_truncated: None,
                },
        });
-       let introduction_node_id = resolve_introduction_node(alice, &reply_path);
        assert_eq!(invoice_request.amount_msats(), None);
        assert_ne!(invoice_request.payer_id(), bob_id);
-       assert_eq!(introduction_node_id, bob_id);
-       assert!(matches!(reply_path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
+       assert_eq!(reply_path.introduction_node, IntroductionNode::NodeId(bob_id));
 
        let onion_message = alice.onion_messenger.next_onion_message_for_peer(bob_id).unwrap();
        bob.onion_messenger.handle_onion_message(&alice_id, &onion_message);
@@ -634,9 +742,7 @@ fn creates_and_pays_for_refund_using_one_hop_blinded_path() {
        assert_ne!(refund.payer_id(), bob_id);
        assert!(!refund.paths().is_empty());
        for path in refund.paths() {
-               let introduction_node_id = resolve_introduction_node(alice, &path);
-               assert_eq!(introduction_node_id, bob_id);
-               assert!(matches!(path.introduction_node, IntroductionNode::DirectedShortChannelId(..)));
+               assert_eq!(path.introduction_node, IntroductionNode::NodeId(bob_id));
        }
        expect_recent_payment!(bob, RecentPaymentDetails::AwaitingInvoice, payment_id);
 
@@ -681,7 +787,7 @@ fn pays_for_offer_without_blinded_paths() {
        let bob_id = bob.node.get_our_node_id();
 
        let offer = alice.node
-               .create_offer_builder().unwrap()
+               .create_offer_builder(None).unwrap()
                .clear_paths()
                .amount_msats(10_000_000)
                .build().unwrap();
@@ -769,7 +875,7 @@ fn fails_creating_offer_without_blinded_paths() {
 
        create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
 
-       match nodes[0].node.create_offer_builder() {
+       match nodes[0].node.create_offer_builder(None) {
                Ok(_) => panic!("Expected error"),
                Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
        }
@@ -798,6 +904,129 @@ fn fails_creating_refund_without_blinded_paths() {
        assert!(nodes[0].node.list_recent_payments().is_empty());
 }
 
+/// Fails creating or paying an offer when a blinded path cannot be created because no peers are
+/// connected.
+#[test]
+fn fails_creating_or_paying_for_offer_without_connected_peers() {
+       let chanmon_cfgs = create_chanmon_cfgs(6);
+       let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
+       let node_chanmgrs = create_node_chanmgrs(6, &node_cfgs, &[None, None, None, None, None, None]);
+       let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
+
+       create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
+       create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
+       create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
+       create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
+       create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
+       create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
+       create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
+
+       let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
+
+       disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
+       disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
+
+       let absolute_expiry = alice.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
+       match alice.node.create_offer_builder(Some(absolute_expiry)) {
+               Ok(_) => panic!("Expected error"),
+               Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
+       }
+
+       let mut args = ReconnectArgs::new(alice, bob);
+       args.send_channel_ready = (true, true);
+       reconnect_nodes(args);
+
+       let offer = alice.node
+               .create_offer_builder(Some(absolute_expiry)).unwrap()
+               .amount_msats(10_000_000)
+               .build().unwrap();
+
+       let payment_id = PaymentId([1; 32]);
+
+       match david.node.pay_for_offer(&offer, None, None, None, payment_id, Retry::Attempts(0), None) {
+               Ok(_) => panic!("Expected error"),
+               Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
+       }
+
+       assert!(nodes[0].node.list_recent_payments().is_empty());
+
+       let mut args = ReconnectArgs::new(charlie, david);
+       args.send_channel_ready = (true, true);
+       reconnect_nodes(args);
+
+       assert!(
+               david.node.pay_for_offer(
+                       &offer, None, None, None, payment_id, Retry::Attempts(0), None
+               ).is_ok()
+       );
+
+       expect_recent_payment!(david, RecentPaymentDetails::AwaitingInvoice, payment_id);
+}
+
+/// Fails creating or sending an invoice for a refund when a blinded path cannot be created because
+/// no peers are connected.
+#[test]
+fn fails_creating_refund_or_sending_invoice_without_connected_peers() {
+       let mut accept_forward_cfg = test_default_channel_config();
+       accept_forward_cfg.accept_forwards_to_priv_channels = true;
+
+       let mut features = channelmanager::provided_init_features(&accept_forward_cfg);
+       features.set_onion_messages_optional();
+       features.set_route_blinding_optional();
+
+       let chanmon_cfgs = create_chanmon_cfgs(6);
+       let node_cfgs = create_node_cfgs(6, &chanmon_cfgs);
+
+       *node_cfgs[1].override_init_features.borrow_mut() = Some(features);
+
+       let node_chanmgrs = create_node_chanmgrs(
+               6, &node_cfgs, &[None, Some(accept_forward_cfg), None, None, None, None]
+       );
+       let nodes = create_network(6, &node_cfgs, &node_chanmgrs);
+
+       create_unannounced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 1_000_000_000);
+       create_unannounced_chan_between_nodes_with_value(&nodes, 2, 3, 10_000_000, 1_000_000_000);
+       create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000_000, 1_000_000_000);
+       create_announced_chan_between_nodes_with_value(&nodes, 1, 4, 10_000_000, 1_000_000_000);
+       create_announced_chan_between_nodes_with_value(&nodes, 1, 5, 10_000_000, 1_000_000_000);
+       create_announced_chan_between_nodes_with_value(&nodes, 2, 4, 10_000_000, 1_000_000_000);
+       create_announced_chan_between_nodes_with_value(&nodes, 2, 5, 10_000_000, 1_000_000_000);
+
+       let (alice, bob, charlie, david) = (&nodes[0], &nodes[1], &nodes[2], &nodes[3]);
+
+       disconnect_peers(alice, &[bob, charlie, david, &nodes[4], &nodes[5]]);
+       disconnect_peers(david, &[bob, charlie, &nodes[4], &nodes[5]]);
+
+       let absolute_expiry = david.node.duration_since_epoch() + MAX_SHORT_LIVED_RELATIVE_EXPIRY;
+       let payment_id = PaymentId([1; 32]);
+       match david.node.create_refund_builder(
+               10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None
+       ) {
+               Ok(_) => panic!("Expected error"),
+               Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
+       }
+
+       let mut args = ReconnectArgs::new(charlie, david);
+       args.send_channel_ready = (true, true);
+       reconnect_nodes(args);
+
+       let refund = david.node
+               .create_refund_builder(10_000_000, absolute_expiry, payment_id, Retry::Attempts(0), None)
+               .unwrap()
+               .build().unwrap();
+
+       match alice.node.request_refund_payment(&refund) {
+               Ok(_) => panic!("Expected error"),
+               Err(e) => assert_eq!(e, Bolt12SemanticError::MissingPaths),
+       }
+
+       let mut args = ReconnectArgs::new(alice, bob);
+       args.send_channel_ready = (true, true);
+       reconnect_nodes(args);
+
+       assert!(alice.node.request_refund_payment(&refund).is_ok());
+}
+
 /// Fails creating an invoice request when the offer contains an unsupported chain.
 #[test]
 fn fails_creating_invoice_request_for_unsupported_chain() {
@@ -812,7 +1041,7 @@ fn fails_creating_invoice_request_for_unsupported_chain() {
        let bob = &nodes[1];
 
        let offer = alice.node
-               .create_offer_builder().unwrap()
+               .create_offer_builder(None).unwrap()
                .clear_chains()
                .chain(Network::Signet)
                .build().unwrap();
@@ -872,7 +1101,7 @@ fn fails_creating_invoice_request_without_blinded_reply_path() {
        disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
 
        let offer = alice.node
-               .create_offer_builder().unwrap()
+               .create_offer_builder(None).unwrap()
                .amount_msats(10_000_000)
                .build().unwrap();
 
@@ -906,7 +1135,7 @@ fn fails_creating_invoice_request_with_duplicate_payment_id() {
        disconnect_peers(alice, &[charlie, david, &nodes[4], &nodes[5]]);
 
        let offer = alice.node
-               .create_offer_builder().unwrap()
+               .create_offer_builder(None).unwrap()
                .amount_msats(10_000_000)
                .build().unwrap();
 
@@ -992,7 +1221,7 @@ fn fails_sending_invoice_without_blinded_payment_paths_for_offer() {
        disconnect_peers(david, &[bob, &nodes[4], &nodes[5]]);
 
        let offer = alice.node
-               .create_offer_builder().unwrap()
+               .create_offer_builder(None).unwrap()
                .amount_msats(10_000_000)
                .build().unwrap();