@@ -24,7 +24,7 @@ use ln::channel::{Channel, ChannelError};
2424use ln::{chan_utils, onion_utils};
2525use ln::chan_utils::{htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputInCommitment};
2626use routing::router::{PaymentParameters, Route, RouteHop, RouteParameters, find_route, get_route};
27- use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
27+ use ln::features::{ChannelFeatures, ChannelTypeFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
2828use ln::msgs;
2929use ln::msgs::{ChannelMessageHandler, RoutingMessageHandler, OptionalField, ErrorAction};
3030use util::enforcing_trait_impls::EnforcingSigner;
@@ -10057,3 +10057,99 @@ fn test_max_dust_htlc_exposure() {
1005710057 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, false);
1005810058 do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, true);
1005910059}
10060+
10061+ #[test]
10062+ fn test_zero_conf_accept_reject() {
10063+ let mut channel_type_features = ChannelTypeFeatures::only_static_remote_key();
10064+ channel_type_features.set_zero_conf_required();
10065+
10066+ // 1. Check we reject zero conf channels by default
10067+ let chanmon_cfgs = create_chanmon_cfgs(2);
10068+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10069+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
10070+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10071+
10072+ nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42, None).unwrap();
10073+ let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
10074+
10075+ open_channel_msg.channel_type = Some(channel_type_features.clone());
10076+
10077+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel_msg);
10078+
10079+ let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
10080+ match msg_events[0] {
10081+ MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg, .. }, .. } => {
10082+ assert_eq!(msg.data, "No zero confirmation channels accepted".to_owned());
10083+ },
10084+ _ => panic!(),
10085+ }
10086+
10087+ // 2. Check we can manually accept zero conf channels via the right method
10088+ let mut manually_accept_conf = UserConfig::default();
10089+ manually_accept_conf.manually_accept_inbound_channels = true;
10090+
10091+ let chanmon_cfgs = create_chanmon_cfgs(2);
10092+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10093+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs,
10094+ &[None, Some(manually_accept_conf.clone())]);
10095+ let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10096+
10097+ // 2.1 First try the non-0conf method to manually accept
10098+ nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42,
10099+ Some(manually_accept_conf)).unwrap();
10100+ let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel,
10101+ nodes[1].node.get_our_node_id());
10102+
10103+ open_channel_msg.channel_type = Some(channel_type_features.clone());
10104+
10105+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(),
10106+ &open_channel_msg);
10107+
10108+ // Assert that `nodes[1]` has no `MessageSendEvent::SendAcceptChannel` in the `msg_events`.
10109+ assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
10110+
10111+ let events = nodes[1].node.get_and_clear_pending_events();
10112+
10113+ match events[0] {
10114+ Event::OpenChannelRequest { temporary_channel_id, .. } => {
10115+ // Assert we fail to accept via the non-0conf method
10116+ assert!(nodes[1].node.accept_inbound_channel(&temporary_channel_id,
10117+ &nodes[0].node.get_our_node_id(), 0).is_err());
10118+ },
10119+ _ => panic!(),
10120+ }
10121+
10122+ let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
10123+ match msg_events[0] {
10124+ MessageSendEvent::HandleError { action: ErrorAction::SendErrorMessage { ref msg, .. }, .. } => {
10125+ assert_eq!(msg.data, "No zero confirmation channels accepted".to_owned());
10126+ },
10127+ _ => panic!(),
10128+ }
10129+
10130+ // 2.2 Try again with the 0conf method to manually accept
10131+ nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100000, 10001, 42,
10132+ Some(manually_accept_conf)).unwrap();
10133+ let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel,
10134+ nodes[1].node.get_our_node_id());
10135+
10136+ open_channel_msg.channel_type = Some(channel_type_features);
10137+
10138+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(),
10139+ &open_channel_msg);
10140+
10141+ let events = nodes[1].node.get_and_clear_pending_events();
10142+
10143+ match events[0] {
10144+ Event::OpenChannelRequest { temporary_channel_id, .. } => {
10145+ // Assert we can accept via the 0conf method
10146+ assert!(nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(
10147+ &temporary_channel_id, &nodes[0].node.get_our_node_id(), 0).is_ok());
10148+ },
10149+ _ => panic!(),
10150+ }
10151+
10152+ // Don't handle remaining events
10153+ nodes[1].node.get_and_clear_pending_msg_events();
10154+ nodes[1].node.get_and_clear_pending_events();
10155+ }
0 commit comments