|
| 1 | +use crate::events::{Event, MessageSendEvent, MessageSendEventsProvider}; |
| 2 | +use crate::ln::msgs::ChannelMessageHandler; |
| 3 | +use crate::ln::msgs::ErrorAction; |
| 4 | +use crate::ln::{functional_test_utils::*, ChannelId}; |
| 5 | +use crate::prelude::*; |
| 6 | +use crate::util::config::ChannelConfigUpdate; |
| 7 | +use crate::util::errors::APIError; |
| 8 | + |
| 9 | +#[test] |
| 10 | +fn test_inbound_anchors_manual_acceptance() { |
| 11 | + // Tests that we properly limit inbound channels when we have the manual-channel-acceptance |
| 12 | + // flag set and (sometimes) accept channels as 0conf. |
| 13 | + let mut anchors_cfg = test_default_channel_config(); |
| 14 | + anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true; |
| 15 | + |
| 16 | + let mut anchors_manual_accept_cfg = anchors_cfg.clone(); |
| 17 | + anchors_manual_accept_cfg.manually_accept_inbound_channels = true; |
| 18 | + |
| 19 | + let chanmon_cfgs = create_chanmon_cfgs(3); |
| 20 | + let node_cfgs = create_node_cfgs(3, &chanmon_cfgs); |
| 21 | + let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs, |
| 22 | + &[Some(anchors_cfg.clone()), Some(anchors_cfg.clone()), Some(anchors_manual_accept_cfg.clone())]); |
| 23 | + let nodes = create_network(3, &node_cfgs, &node_chanmgrs); |
| 24 | + |
| 25 | + nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap(); |
| 26 | + let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); |
| 27 | + |
| 28 | + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg); |
| 29 | + assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); |
| 30 | + let msg_events = nodes[1].node.get_and_clear_pending_msg_events(); |
| 31 | + match &msg_events[0] { |
| 32 | + MessageSendEvent::HandleError { node_id, action } => { |
| 33 | + assert_eq!(*node_id, nodes[0].node.get_our_node_id()); |
| 34 | + match action { |
| 35 | + ErrorAction::SendErrorMessage { msg } => |
| 36 | + assert_eq!(msg.data, "No channels with anchor outputs accepted".to_owned()), |
| 37 | + _ => panic!("Unexpected error action"), |
| 38 | + } |
| 39 | + } |
| 40 | + _ => panic!("Unexpected event"), |
| 41 | + } |
| 42 | + |
| 43 | + nodes[2].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg); |
| 44 | + let events = nodes[2].node.get_and_clear_pending_events(); |
| 45 | + match events[0] { |
| 46 | + Event::OpenChannelRequest { temporary_channel_id, .. } => |
| 47 | + nodes[2].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 23).unwrap(), |
| 48 | + _ => panic!("Unexpected event"), |
| 49 | + } |
| 50 | + get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id()); |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn test_anchors_zero_fee_htlc_tx_fallback() { |
| 55 | + // Tests that if both nodes support anchors, but the remote node does not want to accept |
| 56 | + // anchor channels at the moment, an error it sent to the local node such that it can retry |
| 57 | + // the channel without the anchors feature. |
| 58 | + let chanmon_cfgs = create_chanmon_cfgs(2); |
| 59 | + let node_cfgs = create_node_cfgs(2, &chanmon_cfgs); |
| 60 | + let mut anchors_config = test_default_channel_config(); |
| 61 | + anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true; |
| 62 | + anchors_config.manually_accept_inbound_channels = true; |
| 63 | + let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(anchors_config.clone()), Some(anchors_config.clone())]); |
| 64 | + let nodes = create_network(2, &node_cfgs, &node_chanmgrs); |
| 65 | + |
| 66 | + nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 0, None, None).unwrap(); |
| 67 | + let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); |
| 68 | + assert!(open_channel_msg.common_fields.channel_type.as_ref().unwrap().supports_anchors_zero_fee_htlc_tx()); |
| 69 | + |
| 70 | + nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg); |
| 71 | + let events = nodes[1].node.get_and_clear_pending_events(); |
| 72 | + match events[0] { |
| 73 | + Event::OpenChannelRequest { temporary_channel_id, .. } => { |
| 74 | + nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id()).unwrap(); |
| 75 | + } |
| 76 | + _ => panic!("Unexpected event"), |
| 77 | + } |
| 78 | + |
| 79 | + let error_msg = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id()); |
| 80 | + nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &error_msg); |
| 81 | + |
| 82 | + let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id()); |
| 83 | + assert!(!open_channel_msg.common_fields.channel_type.unwrap().supports_anchors_zero_fee_htlc_tx()); |
| 84 | + |
| 85 | + // Since nodes[1] should not have accepted the channel, it should |
| 86 | + // not have generated any events. |
| 87 | + assert!(nodes[1].node.get_and_clear_pending_events().is_empty()); |
| 88 | +} |
| 89 | + |
| 90 | +#[test] |
| 91 | +fn test_update_channel_config() { |
| 92 | + let chanmon_cfg = create_chanmon_cfgs(2); |
| 93 | + let node_cfg = create_node_cfgs(2, &chanmon_cfg); |
| 94 | + let mut user_config = test_default_channel_config(); |
| 95 | + let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[Some(user_config), Some(user_config)]); |
| 96 | + let nodes = create_network(2, &node_cfg, &node_chanmgr); |
| 97 | + let _ = create_announced_chan_between_nodes(&nodes, 0, 1); |
| 98 | + let channel = &nodes[0].node.list_channels()[0]; |
| 99 | + |
| 100 | + nodes[0].node.update_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &user_config.channel_config).unwrap(); |
| 101 | + let events = nodes[0].node.get_and_clear_pending_msg_events(); |
| 102 | + assert_eq!(events.len(), 0); |
| 103 | + |
| 104 | + user_config.channel_config.forwarding_fee_base_msat += 10; |
| 105 | + nodes[0].node.update_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &user_config.channel_config).unwrap(); |
| 106 | + assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_base_msat, user_config.channel_config.forwarding_fee_base_msat); |
| 107 | + let events = nodes[0].node.get_and_clear_pending_msg_events(); |
| 108 | + assert_eq!(events.len(), 1); |
| 109 | + match &events[0] { |
| 110 | + MessageSendEvent::BroadcastChannelUpdate { .. } => {}, |
| 111 | + _ => panic!("expected BroadcastChannelUpdate event"), |
| 112 | + } |
| 113 | + |
| 114 | + nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate::default()).unwrap(); |
| 115 | + let events = nodes[0].node.get_and_clear_pending_msg_events(); |
| 116 | + assert_eq!(events.len(), 0); |
| 117 | + |
| 118 | + let new_cltv_expiry_delta = user_config.channel_config.cltv_expiry_delta + 6; |
| 119 | + nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate { |
| 120 | + cltv_expiry_delta: Some(new_cltv_expiry_delta), |
| 121 | + ..Default::default() |
| 122 | + }).unwrap(); |
| 123 | + assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().cltv_expiry_delta, new_cltv_expiry_delta); |
| 124 | + let events = nodes[0].node.get_and_clear_pending_msg_events(); |
| 125 | + assert_eq!(events.len(), 1); |
| 126 | + match &events[0] { |
| 127 | + MessageSendEvent::BroadcastChannelUpdate { .. } => {}, |
| 128 | + _ => panic!("expected BroadcastChannelUpdate event"), |
| 129 | + } |
| 130 | + |
| 131 | + let new_fee = user_config.channel_config.forwarding_fee_proportional_millionths + 100; |
| 132 | + nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id], &ChannelConfigUpdate { |
| 133 | + forwarding_fee_proportional_millionths: Some(new_fee), |
| 134 | + ..Default::default() |
| 135 | + }).unwrap(); |
| 136 | + assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().cltv_expiry_delta, new_cltv_expiry_delta); |
| 137 | + assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths, new_fee); |
| 138 | + let events = nodes[0].node.get_and_clear_pending_msg_events(); |
| 139 | + assert_eq!(events.len(), 1); |
| 140 | + match &events[0] { |
| 141 | + MessageSendEvent::BroadcastChannelUpdate { .. } => {}, |
| 142 | + _ => panic!("expected BroadcastChannelUpdate event"), |
| 143 | + } |
| 144 | + |
| 145 | + // If we provide a channel_id not associated with the peer, we should get an error and no updates |
| 146 | + // should be applied to ensure update atomicity as specified in the API docs. |
| 147 | + let bad_channel_id = ChannelId::v1_from_funding_txid(&[10; 32], 10); |
| 148 | + let current_fee = nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths; |
| 149 | + let new_fee = current_fee + 100; |
| 150 | + assert!( |
| 151 | + matches!( |
| 152 | + nodes[0].node.update_partial_channel_config(&channel.counterparty.node_id, &[channel.channel_id, bad_channel_id], &ChannelConfigUpdate { |
| 153 | + forwarding_fee_proportional_millionths: Some(new_fee), |
| 154 | + ..Default::default() |
| 155 | + }), |
| 156 | + Err(APIError::ChannelUnavailable { err: _ }), |
| 157 | + ) |
| 158 | + ); |
| 159 | + // Check that the fee hasn't changed for the channel that exists. |
| 160 | + assert_eq!(nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths, current_fee); |
| 161 | + let events = nodes[0].node.get_and_clear_pending_msg_events(); |
| 162 | + assert_eq!(events.len(), 0); |
| 163 | +} |
0 commit comments