X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Fln%2Fpriv_short_conf_tests.rs;h=7a5b62e00d55226bbccc6bde57dfd3a395a398a9;hb=e61f3a238a70cbac87209e223b7c396108a49b97;hp=fa44a0655c1462cdf321fede0aeb0de83141626b;hpb=d0f69f77bd6ed40bff7ef1026f23e4444a5a884a;p=rust-lightning diff --git a/lightning/src/ln/priv_short_conf_tests.rs b/lightning/src/ln/priv_short_conf_tests.rs index fa44a065..7a5b62e0 100644 --- a/lightning/src/ln/priv_short_conf_tests.rs +++ b/lightning/src/ln/priv_short_conf_tests.rs @@ -11,17 +11,18 @@ //! other behavior that exists only on private channels or with a semi-trusted counterparty (eg //! LSP). -use chain::Watch; +use chain::{ChannelMonitorUpdateStatus, Watch}; use chain::channelmonitor::ChannelMonitor; use chain::keysinterface::{Recipient, KeysInterface}; -use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, MIN_CLTV_EXPIRY_DELTA}; -use routing::network_graph::RoutingFees; +use ln::channelmanager::{self, ChannelManager, ChannelManagerReadArgs, MIN_CLTV_EXPIRY_DELTA}; +use routing::gossip::RoutingFees; use routing::router::{PaymentParameters, RouteHint, RouteHintHop}; -use ln::features::{InitFeatures, InvoiceFeatures}; +use ln::features::ChannelTypeFeatures; use ln::msgs; -use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, OptionalField}; +use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, ChannelUpdate, ErrorAction}; +use ln::wire::Encode; use util::enforcing_trait_impls::EnforcingSigner; -use util::events::{Event, MessageSendEvent, MessageSendEventsProvider}; +use util::events::{ClosureReason, Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider}; use util::config::UserConfig; use util::ser::{Writeable, ReadableArgs}; use util::test_utils; @@ -53,8 +54,8 @@ fn test_priv_forwarding_rejection() { let nodes_1_deserialized: ChannelManager; 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, InitFeatures::known(), InitFeatures::known()).2; - let chan_id_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known()).0.channel_id; + let chan_id_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2; + let chan_id_2 = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features()).0.channel_id; // We should always be able to forward through nodes[1] as long as its out through a public // channel: @@ -72,7 +73,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()) - .with_features(InvoiceFeatures::known()) + .with_features(channelmanager::provided_invoice_features()) .with_route_hints(last_hops); let (route, our_payment_hash, our_payment_preimage, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, TEST_FINAL_CLTV); @@ -136,24 +137,26 @@ fn test_priv_forwarding_rejection() { assert!(nodes_1_read.is_empty()); nodes_1_deserialized = nodes_1_deserialized_tmp; - assert!(nodes[1].chain_monitor.watch_channel(monitor_a.get_funding_txo().0, monitor_a).is_ok()); - assert!(nodes[1].chain_monitor.watch_channel(monitor_b.get_funding_txo().0, monitor_b).is_ok()); + assert_eq!(nodes[1].chain_monitor.watch_channel(monitor_a.get_funding_txo().0, monitor_a), + ChannelMonitorUpdateStatus::Completed); + assert_eq!(nodes[1].chain_monitor.watch_channel(monitor_b.get_funding_txo().0, monitor_b), + ChannelMonitorUpdateStatus::Completed); check_added_monitors!(nodes[1], 2); nodes[1].node = &nodes_1_deserialized; - nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known(), remote_network_address: None }); - nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); - let as_reestablish = get_event_msg!(nodes[0], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()); - let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[0].node.get_our_node_id()); + nodes[0].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap(); + nodes[1].node.peer_connected(&nodes[0].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap(); + let as_reestablish = get_chan_reestablish_msgs!(nodes[0], nodes[1]).pop().unwrap(); + let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[0]).pop().unwrap(); nodes[1].node.handle_channel_reestablish(&nodes[0].node.get_our_node_id(), &as_reestablish); nodes[0].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish); get_event_msg!(nodes[0], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id()); - nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: InitFeatures::known(), remote_network_address: None }); - nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: InitFeatures::empty(), remote_network_address: None }); - let bs_reestablish = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReestablish, nodes[2].node.get_our_node_id()); - let cs_reestablish = get_event_msg!(nodes[2], MessageSendEvent::SendChannelReestablish, nodes[1].node.get_our_node_id()); + nodes[1].node.peer_connected(&nodes[2].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap(); + nodes[2].node.peer_connected(&nodes[1].node.get_our_node_id(), &msgs::Init { features: channelmanager::provided_init_features(), remote_network_address: None }).unwrap(); + let bs_reestablish = get_chan_reestablish_msgs!(nodes[1], nodes[2]).pop().unwrap(); + let cs_reestablish = get_chan_reestablish_msgs!(nodes[2], nodes[1]).pop().unwrap(); nodes[2].node.handle_channel_reestablish(&nodes[1].node.get_our_node_id(), &bs_reestablish); nodes[1].node.handle_channel_reestablish(&nodes[2].node.get_our_node_id(), &cs_reestablish); get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id()); @@ -166,31 +169,31 @@ fn test_priv_forwarding_rejection() { } fn do_test_1_conf_open(connect_style: ConnectStyle) { - // Previously, if the minium_depth config was set to 1, we'd never send a funding_locked. This + // Previously, if the minium_depth config was set to 1, we'd never send a channel_ready. This // tests that we properly send one in that case. let mut alice_config = UserConfig::default(); - alice_config.own_channel_config.minimum_depth = 1; - alice_config.channel_options.announced_channel = true; - alice_config.peer_channel_config_limits.force_announced_channel_preference = false; + 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; let mut bob_config = UserConfig::default(); - bob_config.own_channel_config.minimum_depth = 1; - bob_config.channel_options.announced_channel = true; - bob_config.peer_channel_config_limits.force_announced_channel_preference = false; + 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; 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)]); let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); *nodes[0].connect_style.borrow_mut() = connect_style; - let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, InitFeatures::known(), InitFeatures::known()); + let tx = create_chan_between_nodes_with_value_init(&nodes[0], &nodes[1], 100000, 10001, channelmanager::provided_init_features(), channelmanager::provided_init_features()); mine_transaction(&nodes[1], &tx); - nodes[0].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[0].node.get_our_node_id())); + nodes[0].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[0].node.get_our_node_id())); assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); mine_transaction(&nodes[0], &tx); let as_msg_events = nodes[0].node.get_and_clear_pending_msg_events(); assert_eq!(as_msg_events.len(), 2); - let as_funding_locked = if let MessageSendEvent::SendFundingLocked { ref node_id, ref msg } = as_msg_events[0] { + let as_channel_ready = if let MessageSendEvent::SendChannelReady { ref node_id, ref msg } = as_msg_events[0] { assert_eq!(*node_id, nodes[1].node.get_our_node_id()); msg.clone() } else { panic!("Unexpected event"); }; @@ -198,7 +201,7 @@ fn do_test_1_conf_open(connect_style: ConnectStyle) { assert_eq!(*node_id, nodes[1].node.get_our_node_id()); } else { panic!("Unexpected event"); } - nodes[1].node.handle_funding_locked(&nodes[0].node.get_our_node_id(), &as_funding_locked); + nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &as_channel_ready); let bs_msg_events = nodes[1].node.get_and_clear_pending_msg_events(); assert_eq!(bs_msg_events.len(), 1); if let MessageSendEvent::SendChannelUpdate { ref node_id, msg: _ } = bs_msg_events[0] { @@ -235,9 +238,9 @@ fn do_test_1_conf_open(connect_style: ConnectStyle) { assert_eq!(announcement, bs_announcement); for node in nodes { - assert!(node.net_graph_msg_handler.handle_channel_announcement(&announcement).unwrap()); - node.net_graph_msg_handler.handle_channel_update(&as_update).unwrap(); - node.net_graph_msg_handler.handle_channel_update(&bs_update).unwrap(); + assert!(node.gossip_sync.handle_channel_announcement(&announcement).unwrap()); + node.gossip_sync.handle_channel_update(&as_update).unwrap(); + node.gossip_sync.handle_channel_update(&bs_update).unwrap(); } } #[test] @@ -257,8 +260,8 @@ fn test_routed_scid_alias() { let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(no_announce_cfg), None]); let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); - create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known()).2; - let mut as_funding_locked = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000, InitFeatures::known(), InitFeatures::known()).0; + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2; + let mut as_channel_ready = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 500_000_000, channelmanager::provided_init_features(), channelmanager::provided_init_features()).0; let last_hop = nodes[2].node.list_usable_channels(); let hop_hints = vec![RouteHint(vec![RouteHintHop { @@ -273,7 +276,7 @@ fn test_routed_scid_alias() { htlc_minimum_msat: None, }])]; let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id()) - .with_features(InvoiceFeatures::known()) + .with_features(channelmanager::provided_invoice_features()) .with_route_hints(hop_hints); let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42); assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap()); @@ -283,17 +286,17 @@ fn test_routed_scid_alias() { pass_along_route(&nodes[0], &[&[&nodes[1], &nodes[2]]], 100_000, payment_hash, payment_secret); claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage); - // Now test that if a peer sends us a second funding_locked after the channel is operational we + // Now test that if a peer sends us a second channel_ready after the channel is operational we // will use the new alias. - as_funding_locked.short_channel_id_alias = Some(0xdeadbeef); - nodes[2].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &as_funding_locked); - // Note that we always respond to a funding_locked with a channel_update. Not a lot of reason + as_channel_ready.short_channel_id_alias = Some(0xdeadbeef); + nodes[2].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &as_channel_ready); + // Note that we always respond to a channel_ready with a channel_update. Not a lot of reason // to bother updating that code, so just drop the message here. get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); let updated_channel_info = nodes[2].node.list_usable_channels(); assert_eq!(updated_channel_info.len(), 1); assert_eq!(updated_channel_info[0].inbound_scid_alias.unwrap(), 0xdeadbeef); - // Note that because we never send a duplicate funding_locked we can't send a payment through + // Note that because we never send a duplicate channel_ready we can't send a payment through // the 0xdeadbeef SCID alias. } @@ -307,8 +310,8 @@ fn test_scid_privacy_on_pub_channel() { let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let mut scid_privacy_cfg = test_default_channel_config(); - scid_privacy_cfg.channel_options.announced_channel = true; - scid_privacy_cfg.own_channel_config.negotiate_scid_privacy = true; + 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(); let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); @@ -316,7 +319,7 @@ fn test_scid_privacy_on_pub_channel() { 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. - nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel); + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel); let err = get_err_msg!(nodes[1], nodes[0].node.get_our_node_id()); assert_eq!(err.data, "SCID Alias/Privacy Channel Type cannot be set on a public channel"); } @@ -331,8 +334,8 @@ fn test_scid_privacy_negotiation() { let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs); let mut scid_privacy_cfg = test_default_channel_config(); - scid_privacy_cfg.channel_options.announced_channel = false; - scid_privacy_cfg.own_channel_config.negotiate_scid_privacy = true; + 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(); let init_open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); @@ -349,8 +352,8 @@ fn test_scid_privacy_negotiation() { 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()); - nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &second_open_channel); - nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())); + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &second_open_channel); + nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())); let events = nodes[0].node.get_and_clear_pending_events(); assert_eq!(events.len(), 1); @@ -374,22 +377,22 @@ fn test_inbound_scid_privacy() { let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(accept_forward_cfg), None]); let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs); - create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, InitFeatures::known(), InitFeatures::known()); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); let mut no_announce_cfg = test_default_channel_config(); - no_announce_cfg.channel_options.announced_channel = false; - no_announce_cfg.own_channel_config.negotiate_scid_privacy = true; + 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(); 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()); - nodes[2].node.handle_open_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &open_channel); + nodes[2].node.handle_open_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel); let accept_channel = get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[1].node.get_our_node_id()); - nodes[1].node.handle_accept_channel(&nodes[2].node.get_our_node_id(), InitFeatures::known(), &accept_channel); + nodes[1].node.handle_accept_channel(&nodes[2].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel); - let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[1], 100_000, 42); - nodes[1].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap(); + let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[1], &nodes[2].node.get_our_node_id(), 100_000, 42); + nodes[1].node.funding_transaction_generated(&temporary_channel_id, &nodes[2].node.get_our_node_id(), tx.clone()).unwrap(); nodes[2].node.handle_funding_created(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingCreated, nodes[2].node.get_our_node_id())); check_added_monitors!(nodes[2], 1); @@ -402,10 +405,10 @@ fn test_inbound_scid_privacy() { connect_blocks(&nodes[1], CHAN_CONFIRM_DEPTH - 1); confirm_transaction_at(&nodes[2], &tx, conf_height); connect_blocks(&nodes[2], CHAN_CONFIRM_DEPTH - 1); - let bs_funding_locked = get_event_msg!(nodes[1], MessageSendEvent::SendFundingLocked, nodes[2].node.get_our_node_id()); - nodes[1].node.handle_funding_locked(&nodes[2].node.get_our_node_id(), &get_event_msg!(nodes[2], MessageSendEvent::SendFundingLocked, nodes[1].node.get_our_node_id())); + let bs_channel_ready = get_event_msg!(nodes[1], MessageSendEvent::SendChannelReady, nodes[2].node.get_our_node_id()); + nodes[1].node.handle_channel_ready(&nodes[2].node.get_our_node_id(), &get_event_msg!(nodes[2], MessageSendEvent::SendChannelReady, nodes[1].node.get_our_node_id())); let bs_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[2].node.get_our_node_id()); - nodes[2].node.handle_funding_locked(&nodes[1].node.get_our_node_id(), &bs_funding_locked); + nodes[2].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &bs_channel_ready); let cs_update = get_event_msg!(nodes[2], MessageSendEvent::SendChannelUpdate, nodes[1].node.get_our_node_id()); nodes[1].node.handle_channel_update(&nodes[2].node.get_our_node_id(), &cs_update); @@ -426,7 +429,7 @@ fn test_inbound_scid_privacy() { htlc_minimum_msat: None, }])]; let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id()) - .with_features(InvoiceFeatures::known()) + .with_features(channelmanager::provided_invoice_features()) .with_route_hints(hop_hints.clone()); let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 100_000, 42); assert_eq!(route.paths[0][1].short_channel_id, last_hop[0].inbound_scid_alias.unwrap()); @@ -441,7 +444,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()) - .with_features(InvoiceFeatures::known()) + .with_features(channelmanager::provided_invoice_features()) .with_route_hints(hop_hints); let (route_2, payment_hash_2, _, payment_secret_2) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params_2, 100_000, 42); assert_eq!(route_2.paths[0][1].short_channel_id, last_hop[0].short_channel_id.unwrap()); @@ -459,7 +462,7 @@ fn test_inbound_scid_privacy() { nodes[0].node.handle_update_fail_htlc(&nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]); commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false); - expect_payment_failed_conditions!(nodes[0], payment_hash_2, false, + expect_payment_failed_conditions(&nodes[0], payment_hash_2, false, PaymentFailedConditions::new().blamed_scid(last_hop[0].short_channel_id.unwrap()) .blamed_chan_closed(true).expected_htlc_error_data(0x4000|10, &[0; 0])); } @@ -476,8 +479,8 @@ fn test_scid_alias_returned() { let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(accept_forward_cfg), None]); let nodes = create_network(3, &node_cfgs, &node_chanmgrs); - create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, InitFeatures::known(), InitFeatures::known()); - create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000, 0, InitFeatures::known(), InitFeatures::known()); + create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 10_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); + let chan = create_unannounced_chan_between_nodes_with_value(&nodes, 1, 2, 10_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); let last_hop = nodes[2].node.list_usable_channels(); let mut hop_hints = vec![RouteHint(vec![RouteHintHop { @@ -492,7 +495,7 @@ fn test_scid_alias_returned() { htlc_minimum_msat: None, }])]; let payment_params = PaymentParameters::from_node_id(nodes[2].node.get_our_node_id()) - .with_features(InvoiceFeatures::known()) + .with_features(channelmanager::provided_invoice_features()) .with_route_hints(hop_hints); let (mut route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], payment_params, 10_000, 42); assert_eq!(route.paths[0][1].short_channel_id, nodes[2].node.list_usable_channels()[0].inbound_scid_alias.unwrap()); @@ -507,7 +510,7 @@ fn test_scid_alias_returned() { commitment_signed_dance!(nodes[1], nodes[0], &as_updates.commitment_signed, false, true); expect_pending_htlcs_forwardable!(nodes[1]); - expect_pending_htlcs_forwardable!(nodes[1]); + expect_pending_htlcs_forwardable_and_htlc_handling_failed!(nodes[1], vec![HTLCDestination::NextHopChannel { node_id: Some(nodes[2].node.get_our_node_id()), channel_id: chan.0.channel_id }]); check_added_monitors!(nodes[1], 1); let bs_updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id()); @@ -520,20 +523,25 @@ fn test_scid_alias_returned() { short_channel_id: last_hop[0].inbound_scid_alias.unwrap(), timestamp: 21, flags: 1, - cltv_expiry_delta: accept_forward_cfg.channel_options.cltv_expiry_delta, + cltv_expiry_delta: accept_forward_cfg.channel_config.cltv_expiry_delta, htlc_minimum_msat: 1_000, - htlc_maximum_msat: OptionalField::Present(1_000_000), // Defaults to 10% of the channel value + htlc_maximum_msat: 1_000_000, // Defaults to 10% of the channel value fee_base_msat: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_base_msat, fee_proportional_millionths: last_hop[0].counterparty.forwarding_info.as_ref().unwrap().fee_proportional_millionths, excess_data: Vec::new(), }; let msg_hash = Sha256dHash::hash(&contents.encode()[..]); - let signature = Secp256k1::new().sign(&hash_to_message!(&msg_hash[..]), &nodes[1].keys_manager.get_node_secret(Recipient::Node).unwrap()); + let signature = Secp256k1::new().sign_ecdsa(&hash_to_message!(&msg_hash[..]), &nodes[1].keys_manager.get_node_secret(Recipient::Node).unwrap()); let msg = msgs::ChannelUpdate { signature, contents }; - expect_payment_failed_conditions!(nodes[0], payment_hash, false, + let mut err_data = Vec::new(); + err_data.extend_from_slice(&(msg.serialized_length() as u16 + 2).to_be_bytes()); + err_data.extend_from_slice(&ChannelUpdate::TYPE.to_be_bytes()); + err_data.extend_from_slice(&msg.encode()); + + expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap()) - .blamed_chan_closed(false).expected_htlc_error_data(0x1000|7, &msg.encode_with_len())); + .blamed_chan_closed(false).expected_htlc_error_data(0x1000|7, &err_data)); route.paths[0][1].fee_msat = 10_000; // Reset to the correct payment amount route.paths[0][0].fee_msat = 0; // But set fee paid to the middle hop to 0 @@ -551,8 +559,446 @@ fn test_scid_alias_returned() { let mut err_data = Vec::new(); err_data.extend_from_slice(&10_000u64.to_be_bytes()); - err_data.extend_from_slice(&msg.encode_with_len()); - expect_payment_failed_conditions!(nodes[0], payment_hash, false, + err_data.extend_from_slice(&(msg.serialized_length() as u16 + 2).to_be_bytes()); + err_data.extend_from_slice(&ChannelUpdate::TYPE.to_be_bytes()); + err_data.extend_from_slice(&msg.encode()); + expect_payment_failed_conditions(&nodes[0], payment_hash, false, PaymentFailedConditions::new().blamed_scid(last_hop[0].inbound_scid_alias.unwrap()) .blamed_chan_closed(false).expected_htlc_error_data(0x1000|12, &err_data)); } + +#[test] +fn test_simple_0conf_channel() { + // If our peer tells us they will accept our channel with 0 confs, and we funded the channel, + // we should trust the funding won't be double-spent (assuming `trust_own_funding_0conf` is + // set)! + // Further, if we `accept_inbound_channel_from_trusted_peer_0conf`, `channel_ready` messages + // should fly immediately and the channel should be available for use as soon as they are + // received. + + 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); + + open_zero_conf_channel(&nodes[0], &nodes[1], None); + + send_payment(&nodes[0], &[&nodes[1]], 100_000); +} + +#[test] +fn test_0conf_channel_with_async_monitor() { + // Test that we properly send out channel_ready in (both inbound- and outbound-) zero-conf + // channels if ChannelMonitor updates return a `TemporaryFailure` during the initial channel + // negotiation. + + let chanmon_cfgs = create_chanmon_cfgs(3); + let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); + let mut chan_config = test_default_channel_config(); + chan_config.manually_accept_inbound_channels = true; + let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, &[None, Some(chan_config), None]); + let nodes = create_network(3, &node_cfgs, &node_chanmgrs); + + create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 1_000_000, 0, channelmanager::provided_init_features(), channelmanager::provided_init_features()); + + 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(); + 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(), channelmanager::provided_init_features(), &open_channel); + let events = nodes[1].node.get_and_clear_pending_events(); + assert_eq!(events.len(), 1); + match events[0] { + Event::OpenChannelRequest { temporary_channel_id, .. } => { + nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap(); + }, + _ => panic!("Unexpected event"), + }; + + 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); + nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel); + + let (temporary_channel_id, tx, funding_output) = create_funding_transaction(&nodes[0], &nodes[1].node.get_our_node_id(), 100000, 42); + nodes[0].node.funding_transaction_generated(&temporary_channel_id, &nodes[1].node.get_our_node_id(), tx.clone()).unwrap(); + let funding_created = get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id()); + + chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress); + nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &funding_created); + 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(); + nodes[1].chain_monitor.complete_sole_pending_chan_update(&channel_id); + + let bs_signed_locked = nodes[1].node.get_and_clear_pending_msg_events(); + assert_eq!(bs_signed_locked.len(), 2); + chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress); + + match &bs_signed_locked[0] { + MessageSendEvent::SendFundingSigned { node_id, msg } => { + assert_eq!(*node_id, nodes[0].node.get_our_node_id()); + nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &msg); + check_added_monitors!(nodes[0], 1); + } + _ => panic!("Unexpected event"), + } + match &bs_signed_locked[1] { + MessageSendEvent::SendChannelReady { node_id, msg } => { + assert_eq!(*node_id, nodes[0].node.get_our_node_id()); + nodes[0].node.handle_channel_ready(&nodes[1].node.get_our_node_id(), &msg); + } + _ => panic!("Unexpected event"), + } + + assert!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().is_empty()); + + assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty()); + nodes[0].chain_monitor.complete_sole_pending_chan_update(&channel_id); + let as_locked_update = nodes[0].node.get_and_clear_pending_msg_events(); + + // Note that the funding transaction is actually released when + // get_and_clear_pending_msg_events, above, checks for monitor events. + assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().len(), 1); + assert_eq!(nodes[0].tx_broadcaster.txn_broadcasted.lock().unwrap().split_off(0)[0], tx); + + match &as_locked_update[0] { + MessageSendEvent::SendChannelReady { node_id, msg } => { + assert_eq!(*node_id, nodes[1].node.get_our_node_id()); + nodes[1].node.handle_channel_ready(&nodes[0].node.get_our_node_id(), &msg); + } + _ => panic!("Unexpected event"), + } + let bs_channel_update = get_event_msg!(nodes[1], MessageSendEvent::SendChannelUpdate, nodes[0].node.get_our_node_id()); + + let as_channel_update = match &as_locked_update[1] { + MessageSendEvent::SendChannelUpdate { node_id, msg } => { + assert_eq!(*node_id, nodes[1].node.get_our_node_id()); + msg.clone() + } + _ => panic!("Unexpected event"), + }; + + chanmon_cfgs[0].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed); + chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed); + + nodes[0].node.handle_channel_update(&nodes[1].node.get_our_node_id(), &bs_channel_update); + nodes[1].node.handle_channel_update(&nodes[0].node.get_our_node_id(), &as_channel_update); + + assert_eq!(nodes[0].node.list_usable_channels().len(), 1); + assert_eq!(nodes[1].node.list_usable_channels().len(), 2); + + send_payment(&nodes[0], &[&nodes[1]], 100_000); + + // Now that we have useful channels, try sending a payment where the we hit a temporary monitor + // failure before we've ever confirmed the funding transaction. This previously caused a panic. + let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[2], 1_000_000); + + nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap(); + check_added_monitors!(nodes[0], 1); + + let as_send = SendEvent::from_node(&nodes[0]); + nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &as_send.msgs[0]); + nodes[1].node.handle_commitment_signed(&nodes[0].node.get_our_node_id(), &as_send.commitment_msg); + check_added_monitors!(nodes[1], 1); + + let (bs_raa, bs_commitment_signed) = get_revoke_commit_msgs!(nodes[1], nodes[0].node.get_our_node_id()); + nodes[0].node.handle_revoke_and_ack(&nodes[1].node.get_our_node_id(), &bs_raa); + check_added_monitors!(nodes[0], 1); + + nodes[0].node.handle_commitment_signed(&nodes[1].node.get_our_node_id(), &bs_commitment_signed); + check_added_monitors!(nodes[0], 1); + + chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::InProgress); + nodes[1].node.handle_revoke_and_ack(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendRevokeAndACK, nodes[1].node.get_our_node_id())); + check_added_monitors!(nodes[1], 1); + assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty()); + + chanmon_cfgs[1].persister.set_update_ret(ChannelMonitorUpdateStatus::Completed); + let (outpoint, _, latest_update) = nodes[1].chain_monitor.latest_monitor_update_id.lock().unwrap().get(&bs_raa.channel_id).unwrap().clone(); + nodes[1].chain_monitor.chain_monitor.channel_monitor_updated(outpoint, latest_update).unwrap(); + check_added_monitors!(nodes[1], 0); + expect_pending_htlcs_forwardable!(nodes[1]); + check_added_monitors!(nodes[1], 1); + + let bs_send = SendEvent::from_node(&nodes[1]); + nodes[2].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &bs_send.msgs[0]); + commitment_signed_dance!(nodes[2], nodes[1], bs_send.commitment_msg, false); + expect_pending_htlcs_forwardable!(nodes[2]); + expect_payment_received!(nodes[2], payment_hash, payment_secret, 1_000_000); + claim_payment(&nodes[0], &[&nodes[1], &nodes[2]], payment_preimage); + + confirm_transaction(&nodes[0], &tx); + confirm_transaction(&nodes[1], &tx); + + send_payment(&nodes[0], &[&nodes[1]], 100_000); +} + +#[test] +fn test_0conf_close_no_early_chan_update() { + // Tests that even with a public channel 0conf channel, we don't generate a channel_update on + // closing. + 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; + open_zero_conf_channel(&nodes[0], &nodes[1], Some(chan_config)); + + // We can use the channel immediately, but won't generate a channel_update until we get confs + send_payment(&nodes[0], &[&nodes[1]], 100_000); + + nodes[0].node.force_close_all_channels_broadcasting_latest_txn(); + check_added_monitors!(nodes[0], 1); + check_closed_event!(&nodes[0], 1, ClosureReason::HolderForceClosed); + let _ = get_err_msg!(nodes[0], nodes[1].node.get_our_node_id()); +} + +#[test] +fn test_public_0conf_channel() { + // Tests that we will announce a public channel (after confirmation) even if its 0conf. + 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()); + 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[1].node.handle_announcement_signatures(&nodes[0].node.get_our_node_id(), &as_announcement_sigs); + nodes[0].node.handle_announcement_signatures(&nodes[1].node.get_our_node_id(), &bs_announcement_sigs); + + let bs_announcement = nodes[1].node.get_and_clear_pending_msg_events(); + assert_eq!(bs_announcement.len(), 1); + let announcement; + let bs_update; + match bs_announcement[0] { + MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => { + announcement = msg.clone(); + bs_update = update_msg.clone(); + }, + _ => panic!("Unexpected event"), + }; + + let as_announcement = nodes[0].node.get_and_clear_pending_msg_events(); + assert_eq!(as_announcement.len(), 1); + match as_announcement[0] { + MessageSendEvent::BroadcastChannelAnnouncement { ref msg, ref update_msg } => { + assert!(announcement == *msg); + assert_eq!(update_msg.contents.short_channel_id, scid); + assert_eq!(update_msg.contents.short_channel_id, announcement.contents.short_channel_id); + assert_eq!(update_msg.contents.short_channel_id, bs_update.contents.short_channel_id); + }, + _ => panic!("Unexpected event"), + }; +} + +#[test] +fn test_0conf_channel_reorg() { + // If we accept a 0conf channel, which is then confirmed, but then changes SCID in a reorg, we + // have to make sure we handle this correctly (or, currently, just force-close the channel). + + 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); + + mine_transaction(&nodes[0], &tx); + mine_transaction(&nodes[1], &tx); + + // Send a payment using the channel's real SCID, which will be public in a few blocks once we + // can generate a channel_announcement. + let real_scid = nodes[0].node.list_usable_channels()[0].short_channel_id.unwrap(); + assert_eq!(nodes[1].node.list_usable_channels()[0].short_channel_id.unwrap(), real_scid); + + let (mut route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 10_000); + assert_eq!(route.paths[0][0].short_channel_id, real_scid); + send_along_route_with_secret(&nodes[0], route, &[&[&nodes[1]]], 10_000, payment_hash, payment_secret); + claim_payment(&nodes[0], &[&nodes[1]], payment_preimage); + + disconnect_blocks(&nodes[0], 1); + disconnect_blocks(&nodes[1], 1); + + // At this point the channel no longer has an SCID again. In the future we should likely + // support simply un-setting the SCID and waiting until the channel gets re-confirmed, but for + // 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() + }); + check_closed_broadcast!(nodes[0], true); + check_closed_event!(&nodes[1], 1, ClosureReason::ProcessingError { + err: "Funding transaction was un-confirmed. Locked at 0 confs, now have 0 confs.".to_owned() + }); + check_closed_broadcast!(nodes[1], true); +} + +#[test] +fn test_zero_conf_accept_reject() { + let mut channel_type_features = ChannelTypeFeatures::only_static_remote_key(); + channel_type_features.set_zero_conf_required(); + + // 1. Check we reject zero conf channels by default + 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); + + nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, 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()); + + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), &open_channel_msg); + + let msg_events = nodes[1].node.get_and_clear_pending_msg_events(); + match msg_events[0] { + MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg, .. }, .. } => { + assert_eq!(msg.data, "No zero confirmation channels accepted".to_owned()); + }, + _ => panic!(), + } + + // 2. Check we can manually accept zero conf channels via the right method + let mut manually_accept_conf = UserConfig::default(); + manually_accept_conf.manually_accept_inbound_channels = true; + + 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, Some(manually_accept_conf.clone())]); + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); + + // 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(); + 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()); + + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), + &open_channel_msg); + + // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in the `msg_events`. + assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty()); + + let events = nodes[1].node.get_and_clear_pending_events(); + + match events[0] { + Event::OpenChannelRequest { temporary_channel_id, .. } => { + // Assert we fail to accept via the non-0conf method + assert!(nodes[1].node.accept_inbound_channel(&temporary_channel_id, + &nodes[0].node.get_our_node_id(), 0).is_err()); + }, + _ => panic!(), + } + + let msg_events = nodes[1].node.get_and_clear_pending_msg_events(); + match msg_events[0] { + MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg, .. }, .. } => { + assert_eq!(msg.data, "No zero confirmation channels accepted".to_owned()); + }, + _ => panic!(), + } + + // 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(); + 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); + + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), channelmanager::provided_init_features(), + &open_channel_msg); + + let events = nodes[1].node.get_and_clear_pending_events(); + + match events[0] { + Event::OpenChannelRequest { temporary_channel_id, .. } => { + // Assert we can accept via the 0conf method + assert!(nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf( + &temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).is_ok()); + }, + _ => panic!(), + } + + // Check we would send accept + let msg_events = nodes[1].node.get_and_clear_pending_msg_events(); + match msg_events[0] { + MessageSendEvent::SendAcceptChannel { .. } => {}, + _ => panic!(), + } +} + +#[test] +fn test_connect_before_funding() { + // Tests for a particularly dumb explicit panic that existed prior to 0.0.111 for 0conf + // channels. If we received a block while awaiting funding for 0-conf channels we'd hit an + // explicit panic when deciding if we should broadcast our channel_ready message. + let chanmon_cfgs = create_chanmon_cfgs(2); + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); + + let mut manually_accept_conf = test_default_channel_config(); + manually_accept_conf.manually_accept_inbound_channels = true; + + 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(); + 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(), channelmanager::provided_init_features(), &open_channel); + let events = nodes[1].node.get_and_clear_pending_events(); + assert_eq!(events.len(), 1); + match events[0] { + Event::OpenChannelRequest { temporary_channel_id, .. } => { + nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).unwrap(); + }, + _ => panic!("Unexpected event"), + }; + + 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); + nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), channelmanager::provided_init_features(), &accept_channel); + + let events = nodes[0].node.get_and_clear_pending_events(); + assert_eq!(events.len(), 1); + match events[0] { + Event::FundingGenerationReady { .. } => {}, + _ => panic!("Unexpected event"), + } + + connect_blocks(&nodes[0], 1); + connect_blocks(&nodes[1], 1); +}