+ 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), PaymentId(payment_hash.0)).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_claimable!(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(), &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(), &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(), &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(), &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(), &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);