Skip to content

Commit 0a2e187

Browse files
committed
ln/refactor: move channel acceptance tests into separate test file
1 parent b9ec89c commit 0a2e187

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
//! Various unit tests covering acceptance of incoming channels and negotiation of channel types.
2+
3+
use lightning_types::features::ChannelTypeFeatures;
4+
use crate::events::Event;
5+
use crate::ln::functional_test_utils::*;
6+
use crate::ln::msgs::{BaseMessageHandler, ChannelMessageHandler, AcceptChannel, ErrorAction, MessageSendEvent};
7+
use crate::prelude::*;
8+
use crate::util::config::{ChannelHandshakeConfigUpdate, UserConfig, ChannelConfigOverrides};
9+
10+
#[test]
11+
fn test_inbound_anchors_manual_acceptance() {
12+
let mut anchors_cfg = test_default_channel_config();
13+
anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
14+
do_test_manual_inbound_accept_with_override(anchors_cfg, None);
15+
}
16+
17+
#[test]
18+
fn test_inbound_anchors_manual_acceptance_overridden() {
19+
let overrides = ChannelConfigOverrides {
20+
handshake_overrides: Some(ChannelHandshakeConfigUpdate {
21+
max_inbound_htlc_value_in_flight_percent_of_channel: Some(5),
22+
htlc_minimum_msat: Some(1000),
23+
minimum_depth: Some(2),
24+
to_self_delay: Some(200),
25+
max_accepted_htlcs: Some(5),
26+
channel_reserve_proportional_millionths: Some(20000),
27+
}),
28+
update_overrides: None,
29+
};
30+
31+
let mut anchors_cfg = test_default_channel_config();
32+
anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
33+
34+
let accept_message = do_test_manual_inbound_accept_with_override(anchors_cfg, Some(overrides));
35+
assert_eq!(accept_message.common_fields.max_htlc_value_in_flight_msat, 5_000_000);
36+
assert_eq!(accept_message.common_fields.htlc_minimum_msat, 1_000);
37+
assert_eq!(accept_message.common_fields.minimum_depth, 2);
38+
assert_eq!(accept_message.common_fields.to_self_delay, 200);
39+
assert_eq!(accept_message.common_fields.max_accepted_htlcs, 5);
40+
assert_eq!(accept_message.channel_reserve_satoshis, 2_000);
41+
}
42+
43+
#[test]
44+
fn test_inbound_zero_fee_commitments_acceptance() {
45+
let mut zero_fee_cfg = test_default_channel_config();
46+
zero_fee_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
47+
do_test_manual_inbound_accept_with_override(zero_fee_cfg, None);
48+
}
49+
50+
fn do_test_manual_inbound_accept_with_override(start_cfg: UserConfig,
51+
config_overrides: Option<ChannelConfigOverrides>) -> AcceptChannel {
52+
53+
let mut mannual_accept_cfg = start_cfg.clone();
54+
mannual_accept_cfg.manually_accept_inbound_channels = true;
55+
56+
let chanmon_cfgs = create_chanmon_cfgs(3);
57+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
58+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs,
59+
&[Some(start_cfg.clone()), Some(start_cfg.clone()), Some(mannual_accept_cfg.clone())]);
60+
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
61+
62+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap();
63+
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
64+
65+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
66+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
67+
let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
68+
match &msg_events[0] {
69+
MessageSendEvent::HandleError { node_id, action } => {
70+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
71+
match action {
72+
ErrorAction::SendErrorMessage { msg } =>
73+
assert_eq!(msg.data, "No channels with anchor outputs accepted".to_owned()),
74+
_ => panic!("Unexpected error action"),
75+
}
76+
}
77+
_ => panic!("Unexpected event"),
78+
}
79+
80+
nodes[2].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
81+
let events = nodes[2].node.get_and_clear_pending_events();
82+
match events[0] {
83+
Event::OpenChannelRequest { temporary_channel_id, .. } =>
84+
nodes[2].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 23, config_overrides).unwrap(),
85+
_ => panic!("Unexpected event"),
86+
}
87+
get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())
88+
}
89+
90+
#[test]
91+
fn test_anchors_zero_fee_htlc_tx_downgrade() {
92+
// Tests that if both nodes support anchors, but the remote node does not want to accept
93+
// anchor channels at the moment, an error it sent to the local node such that it can retry
94+
// the channel without the anchors feature.
95+
let mut anchors_config = test_default_channel_config();
96+
anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
97+
anchors_config.manually_accept_inbound_channels = true;
98+
99+
do_test_channel_type_downgrade(anchors_config, |features| features.supports_anchors_zero_fee_htlc_tx())
100+
}
101+
102+
#[test]
103+
fn test_zero_fee_commitments_downgrade() {
104+
// Tests that the local node will retry without zero fee commitments in the case where the
105+
// remote node supports the feature but does not accept it.
106+
let mut zero_fee_config = test_default_channel_config();
107+
zero_fee_config.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
108+
zero_fee_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
109+
zero_fee_config.manually_accept_inbound_channels = true;
110+
111+
do_test_channel_type_downgrade(zero_fee_config, |features| features.supports_anchor_zero_fee_commitments())
112+
}
113+
114+
fn do_test_channel_type_downgrade<F>(user_cfg: UserConfig, start_type_set: F)
115+
where F: Fn(&ChannelTypeFeatures) -> bool {
116+
let chanmon_cfgs = create_chanmon_cfgs(2);
117+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
118+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(user_cfg.clone()), Some(user_cfg.clone())]);
119+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
120+
let error_message = "Channel force-closed";
121+
122+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 0, None, None).unwrap();
123+
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
124+
assert!(start_type_set(open_channel_msg.common_fields.channel_type.as_ref().unwrap()));
125+
126+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
127+
let events = nodes[1].node.get_and_clear_pending_events();
128+
match events[0] {
129+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
130+
nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id(), error_message.to_string()).unwrap();
131+
}
132+
_ => panic!("Unexpected event"),
133+
}
134+
135+
let error_msg = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
136+
nodes[0].node.handle_error(nodes[1].node.get_our_node_id(), &error_msg);
137+
138+
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
139+
assert!(!start_type_set(open_channel_msg.common_fields.channel_type.as_ref().unwrap()));
140+
141+
// Since nodes[1] should not have accepted the channel, it should
142+
// not have generated any events.
143+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
144+
}

lightning/src/ln/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,5 +116,8 @@ mod offers_tests;
116116
#[cfg(test)]
117117
#[allow(unused_mut)]
118118
mod dual_funding_tests;
119+
#[cfg(test)]
120+
#[allow(unused_mut)]
121+
mod channel_acceptance_tests;
119122

120123
pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;

rustfmt_excluded_files

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ lightning/src/ln/blinded_payment_tests.rs
1111
lightning/src/ln/chan_utils.rs
1212
lightning/src/ln/chanmon_update_fail_tests.rs
1313
lightning/src/ln/channel.rs
14+
lightning/src/ln/channel_acceptance_tests.rs
1415
lightning/src/ln/channelmanager.rs
1516
lightning/src/ln/functional_test_utils.rs
1617
lightning/src/ln/max_payment_path_len_tests.rs

0 commit comments

Comments
 (0)