Use `crate::prelude::*` rather than specific imports
[rust-lightning] / lightning / src / ln / priv_short_conf_tests.rs
index 8d3fbf0ec64667a756c48777fced8f53a2997b5b..6fd8623d317376b5b6d156869c2a5949bca7e33d 100644 (file)
 use crate::chain::ChannelMonitorUpdateStatus;
 use crate::sign::NodeSigner;
 use crate::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider};
-use crate::ln::channelmanager::{ChannelManager, MIN_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnionFields};
+use crate::ln::channelmanager::{MIN_CLTV_EXPIRY_DELTA, PaymentId, RecipientOnionFields};
 use crate::routing::gossip::RoutingFees;
 use crate::routing::router::{PaymentParameters, RouteHint, RouteHintHop};
 use crate::ln::features::ChannelTypeFeatures;
-use crate::ln::msgs;
+use crate::ln::{msgs, ChannelId};
 use crate::ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ChannelUpdate, ErrorAction};
 use crate::ln::wire::Encode;
-use crate::util::config::UserConfig;
+use crate::util::config::{UserConfig, MaxDustHTLCExposure};
 use crate::util::ser::Writeable;
 use crate::util::test_utils;
 
 use crate::prelude::*;
-use core::default::Default;
 
 use crate::ln::functional_test_utils::*;
 
-use bitcoin::blockdata::constants::genesis_block;
+use bitcoin::blockdata::constants::ChainHash;
 use bitcoin::network::constants::Network;
 
 #[test]
@@ -42,10 +41,10 @@ fn test_priv_forwarding_rejection() {
        let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
        let mut no_announce_cfg = test_default_channel_config();
        no_announce_cfg.accept_forwards_to_priv_channels = false;
+       let persister;
+       let new_chain_monitor;
        let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]);
-       let persister: test_utils::TestPersister;
-       let new_chain_monitor: test_utils::TestChainMonitor;
-       let nodes_1_deserialized: ChannelManager<&test_utils::TestChainMonitor, &test_utils::TestBroadcaster, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestKeysInterface, &test_utils::TestFeeEstimator, &test_utils::TestRouter, &test_utils::TestLogger>;
+       let nodes_1_deserialized;
        let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
 
        let chan_id_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000).2;
@@ -67,7 +66,7 @@ fn test_priv_forwarding_rejection() {
        }]);
        let last_hops = vec![route_hint];
        let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), TEST_FINAL_CLTV)
-               .with_bolt11_features(nodes[2].node.invoice_features()).unwrap()
+               .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap()
                .with_route_hints(last_hops).unwrap();
        let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000);
 
@@ -141,10 +140,12 @@ fn do_test_1_conf_open(connect_style: ConnectStyle) {
        alice_config.channel_handshake_config.minimum_depth = 1;
        alice_config.channel_handshake_config.announced_channel = true;
        alice_config.channel_handshake_limits.force_announced_channel_preference = false;
+       alice_config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253);
        let mut bob_config = UserConfig::default();
        bob_config.channel_handshake_config.minimum_depth = 1;
        bob_config.channel_handshake_config.announced_channel = true;
        bob_config.channel_handshake_limits.force_announced_channel_preference = false;
+       bob_config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FeeRateMultiplier(5_000_000 / 253);
        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, &[Some(alice_config), Some(bob_config)]);
@@ -244,7 +245,7 @@ fn test_routed_scid_alias() {
                htlc_minimum_msat: None,
        }])];
        let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
-               .with_bolt11_features(nodes[2].node.invoice_features()).unwrap()
+               .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap()
                .with_route_hints(hop_hints).unwrap();
        let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000);
        assert_eq!(route.paths[0].hops[1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
@@ -288,12 +289,12 @@ fn test_scid_privacy_on_pub_channel() {
        let mut scid_privacy_cfg = test_default_channel_config();
        scid_privacy_cfg.channel_handshake_config.announced_channel = true;
        scid_privacy_cfg.channel_handshake_config.negotiate_scid_privacy = true;
-       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(scid_privacy_cfg)).unwrap();
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, Some(scid_privacy_cfg)).unwrap();
        let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
 
-       assert!(!open_channel.channel_type.as_ref().unwrap().supports_scid_privacy()); // we ignore `negotiate_scid_privacy` on pub channels
-       open_channel.channel_type.as_mut().unwrap().set_scid_privacy_required();
-       assert_eq!(open_channel.channel_flags & 1, 1); // The `announce_channel` bit is set.
+       assert!(!open_channel.common_fields.channel_type.as_ref().unwrap().supports_scid_privacy()); // we ignore `negotiate_scid_privacy` on pub channels
+       open_channel.common_fields.channel_type.as_mut().unwrap().set_scid_privacy_required();
+       assert_eq!(open_channel.common_fields.channel_flags & 1, 1); // The `announce_channel` bit is set.
 
        nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
        let err = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
@@ -312,22 +313,22 @@ fn test_scid_privacy_negotiation() {
        let mut scid_privacy_cfg = test_default_channel_config();
        scid_privacy_cfg.channel_handshake_config.announced_channel = false;
        scid_privacy_cfg.channel_handshake_config.negotiate_scid_privacy = true;
-       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(scid_privacy_cfg)).unwrap();
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, Some(scid_privacy_cfg)).unwrap();
 
        let init_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
-       assert!(init_open_channel.channel_type.as_ref().unwrap().supports_scid_privacy());
+       assert!(init_open_channel.common_fields.channel_type.as_ref().unwrap().supports_scid_privacy());
        assert!(nodes[0].node.list_channels()[0].channel_type.is_none()); // channel_type is none until counterparty accepts
 
        // now simulate nodes[1] responding with an Error message, indicating it doesn't understand
        // SCID alias.
        nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &msgs::ErrorMessage {
-               channel_id: init_open_channel.temporary_channel_id,
+               channel_id: init_open_channel.common_fields.temporary_channel_id,
                data: "Yo, no SCID aliases, no privacy here!".to_string()
        });
        assert!(nodes[0].node.list_channels()[0].channel_type.is_none()); // channel_type is none until counterparty accepts
 
        let second_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
-       assert!(!second_open_channel.channel_type.as_ref().unwrap().supports_scid_privacy());
+       assert!(!second_open_channel.common_fields.channel_type.as_ref().unwrap().supports_scid_privacy());
        nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &second_open_channel);
        nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()));
 
@@ -358,10 +359,10 @@ fn test_inbound_scid_privacy() {
        let mut no_announce_cfg = test_default_channel_config();
        no_announce_cfg.channel_handshake_config.announced_channel = false;
        no_announce_cfg.channel_handshake_config.negotiate_scid_privacy = true;
-       nodes[1].node.create_channel(nodes[2].node.get_our_node_id(), 100_000, 10_000, 42, Some(no_announce_cfg)).unwrap();
+       nodes[1].node.create_channel(nodes[2].node.get_our_node_id(), 100_000, 10_000, 42, None, Some(no_announce_cfg)).unwrap();
        let mut open_channel = get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[2].node.get_our_node_id());
 
-       assert!(open_channel.channel_type.as_ref().unwrap().requires_scid_privacy());
+       assert!(open_channel.common_fields.channel_type.as_ref().unwrap().requires_scid_privacy());
 
        nodes[2].node.handle_open_channel(&nodes[1].node.get_our_node_id(), &open_channel);
        let accept_channel = get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id());
@@ -410,7 +411,7 @@ fn test_inbound_scid_privacy() {
                htlc_minimum_msat: None,
        }])];
        let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
-               .with_bolt11_features(nodes[2].node.invoice_features()).unwrap()
+               .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap()
                .with_route_hints(hop_hints.clone()).unwrap();
        let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000);
        assert_eq!(route.paths[0].hops[1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap());
@@ -426,7 +427,7 @@ fn test_inbound_scid_privacy() {
        hop_hints[0].0[0].short_channel_id = last_hop[0].short_channel_id.unwrap();
 
        let payment_params_2 = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
-               .with_bolt11_features(nodes[2].node.invoice_features()).unwrap()
+               .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap()
                .with_route_hints(hop_hints).unwrap();
        let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params_2, 100_000);
        assert_eq!(route_2.paths[0].hops[1].short_channel_id, last_hop[0].short_channel_id.unwrap());
@@ -478,7 +479,7 @@ fn test_scid_alias_returned() {
                htlc_minimum_msat: None,
        }])];
        let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id(), 42)
-               .with_bolt11_features(nodes[2].node.invoice_features()).unwrap()
+               .with_bolt11_features(nodes[2].node.bolt11_invoice_features()).unwrap()
                .with_route_hints(hop_hints).unwrap();
        let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000);
        assert_eq!(route.paths[0].hops[1].short_channel_id, nodes[2].node.list_usable_channels()[0].inbound_scid_alias.unwrap());
@@ -503,7 +504,7 @@ fn test_scid_alias_returned() {
 
        // Build the expected channel update
        let contents = msgs::UnsignedChannelUpdate {
-               chain_hash: genesis_block(Network::Testnet).header.block_hash(),
+               chain_hash: ChainHash::using_genesis_block(Network::Testnet),
                short_channel_id: last_hop[0].inbound_scid_alias.unwrap(),
                timestamp: 21,
                flags: 1,
@@ -589,7 +590,7 @@ fn test_0conf_channel_with_async_monitor() {
        create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0);
 
        chan_config.channel_handshake_config.announced_channel = false;
-       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, Some(chan_config)).unwrap();
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, Some(chan_config)).unwrap();
        let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
 
        nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
@@ -603,7 +604,7 @@ fn test_0conf_channel_with_async_monitor() {
        };
 
        let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
-       assert_eq!(accept_channel.minimum_depth, 0);
+       assert_eq!(accept_channel.common_fields.minimum_depth, 0);
        nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
 
        let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42);
@@ -615,7 +616,7 @@ fn test_0conf_channel_with_async_monitor() {
        check_added_monitors!(nodes[1], 1);
        assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
 
-       let channel_id = funding_output.to_channel_id();
+       let channel_id = ChannelId::v1_from_funding_outpoint(funding_output);
        nodes[1].chain_monitor.complete_sole_pending_chan_update(&channel_id);
        expect_channel_pending_event(&nodes[1], &nodes[0].node.get_our_node_id());
 
@@ -762,7 +763,7 @@ fn test_0conf_close_no_early_chan_update() {
 
        nodes[0].node.force_close_all_channels_broadcasting_latest_txn();
        check_added_monitors!(nodes[0], 1);
-       check_closed_event!(&nodes[0], 1, ClosureReason::HolderForceClosed);
+       check_closed_event!(&nodes[0], 1, ClosureReason::HolderForceClosed, [nodes[1].node.get_our_node_id()], 100000);
        let _ = get_err_msg(&nodes[0], &nodes[1].node.get_our_node_id());
 }
 
@@ -859,12 +860,12 @@ fn test_0conf_channel_reorg() {
        // now we force-close the channel here.
        check_closed_event!(&nodes[0], 1, ClosureReason::ProcessingError {
                err: "Funding transaction was un-confirmed. Locked at 0 confs, now have 0 confs.".to_owned()
-       });
+       }, [nodes[1].node.get_our_node_id()], 100000);
        check_closed_broadcast!(nodes[0], true);
        check_added_monitors(&nodes[0], 1);
        check_closed_event!(&nodes[1], 1, ClosureReason::ProcessingError {
                err: "Funding transaction was un-confirmed. Locked at 0 confs, now have 0 confs.".to_owned()
-       });
+       }, [nodes[0].node.get_our_node_id()], 100000);
        check_closed_broadcast!(nodes[1], true);
        check_added_monitors(&nodes[1], 1);
 }
@@ -880,10 +881,10 @@ fn test_zero_conf_accept_reject() {
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
        let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 
-       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None, None).unwrap();
        let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
 
-       open_channel_msg.channel_type = Some(channel_type_features.clone());
+       open_channel_msg.common_fields.channel_type = Some(channel_type_features.clone());
 
        nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
 
@@ -907,11 +908,11 @@ fn test_zero_conf_accept_reject() {
 
        // 2.1 First try the non-0conf method to manually accept
        nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42,
-               Some(manually_accept_conf)).unwrap();
+               None, Some(manually_accept_conf)).unwrap();
        let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel,
                nodes[1].node.get_our_node_id());
 
-       open_channel_msg.channel_type = Some(channel_type_features.clone());
+       open_channel_msg.common_fields.channel_type = Some(channel_type_features.clone());
 
        nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
 
@@ -939,11 +940,11 @@ fn test_zero_conf_accept_reject() {
 
        // 2.2 Try again with the 0conf method to manually accept
        nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42,
-               Some(manually_accept_conf)).unwrap();
+               None, Some(manually_accept_conf)).unwrap();
        let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel,
                nodes[1].node.get_our_node_id());
 
-       open_channel_msg.channel_type = Some(channel_type_features);
+       open_channel_msg.common_fields.channel_type = Some(channel_type_features);
 
        nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
 
@@ -980,7 +981,7 @@ fn test_connect_before_funding() {
        let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(manually_accept_conf)]);
        let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
 
-       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 10_001, 42, None).unwrap();
+       nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 10_001, 42, None, None).unwrap();
        let open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
 
        nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel);
@@ -994,7 +995,7 @@ fn test_connect_before_funding() {
        };
 
        let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
-       assert_eq!(accept_channel.minimum_depth, 0);
+       assert_eq!(accept_channel.common_fields.minimum_depth, 0);
        nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), &accept_channel);
 
        let events = nodes[0].node.get_and_clear_pending_events();
@@ -1007,3 +1008,38 @@ fn test_connect_before_funding() {
        connect_blocks(&nodes[0], 1);
        connect_blocks(&nodes[1], 1);
 }
+
+#[test]
+fn test_0conf_ann_sigs_racing_conf() {
+       // Previously we had a bug where we'd panic when receiving a counterparty's
+       // announcement_signatures message for a 0conf channel pending confirmation on-chain. Here we
+       // check that we just error out, ignore the announcement_signatures message, and proceed
+       // instead.
+       let chanmon_cfgs = create_chanmon_cfgs(2);
+       let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
+       let mut chan_config = test_default_channel_config();
+       chan_config.manually_accept_inbound_channels = true;
+
+       let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(chan_config)]);
+       let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
+
+       // This is the default but we force it on anyway
+       chan_config.channel_handshake_config.announced_channel = true;
+       let (tx, ..) = open_zero_conf_channel(&nodes[0], &nodes[1], Some(chan_config));
+
+       // We can use the channel immediately, but we can't announce it until we get 6+ confirmations
+       send_payment(&nodes[0], &[&nodes[1]], 100_000);
+
+       let scid = confirm_transaction(&nodes[0], &tx);
+       let as_announcement_sigs = get_event_msg!(nodes[0], MessageSendEvent::SendAnnouncementSignatures, nodes[1].node.get_our_node_id());
+
+       // Handling the announcement_signatures prior to the first confirmation would panic before.
+       nodes[1].node.handle_announcement_signatures(&nodes[0].node.get_our_node_id(), &as_announcement_sigs);
+
+       assert_eq!(confirm_transaction(&nodes[1], &tx), scid);
+       let bs_announcement_sigs = get_event_msg!(nodes[1], MessageSendEvent::SendAnnouncementSignatures, nodes[0].node.get_our_node_id());
+
+       nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs);
+       let as_announcement = nodes[0].node.get_and_clear_pending_msg_events();
+       assert_eq!(as_announcement.len(), 1);
+}