Recommend funding_tx to apply anti-fee sniping
[rust-lightning] / lightning / src / ln / priv_short_conf_tests.rs
index 13f66805b74b62a9ba66d75cf8384a8a6f567698..1785280db750dbf1b19fde5f295ce12c8980a22b 100644 (file)
@@ -17,9 +17,9 @@ use chain::keysinterface::{Recipient, KeysInterface};
 use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, MIN_CLTV_EXPIRY_DELTA};
 use routing::gossip::RoutingFees;
 use routing::router::{PaymentParameters, RouteHint, RouteHintHop};
-use ln::features::{InitFeatures, InvoiceFeatures};
+use ln::features::{InitFeatures, InvoiceFeatures, ChannelTypeFeatures};
 use ln::msgs;
-use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, OptionalField, ChannelUpdate};
+use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, OptionalField, ChannelUpdate, ErrorAction};
 use ln::wire::Encode;
 use util::enforcing_trait_impls::EnforcingSigner;
 use util::events::{ClosureReason, Event, MessageSendEvent, MessageSendEventsProvider};
@@ -460,7 +460,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]));
 }
@@ -537,7 +537,7 @@ fn test_scid_alias_returned() {
        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,
+       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, &err_data));
 
@@ -560,7 +560,7 @@ fn test_scid_alias_returned() {
        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,
+       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));
 }
@@ -922,3 +922,102 @@ fn test_0conf_channel_reorg() {
        });
        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(), InitFeatures::known(), &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(), InitFeatures::known(),
+               &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(), InitFeatures::known(),
+               &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!(),
+       }
+}