Skip to content

Commit 52356f3

Browse files
committed
ln/refactor: move channelmanager acceptance tests to separate file
1 parent 0848e7a commit 52356f3

File tree

3 files changed

+254
-249
lines changed

3 files changed

+254
-249
lines changed
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
use crate::events::Event;
2+
use crate::ln::functional_test_utils::*;
3+
use crate::ln::msgs::{
4+
AcceptChannel, BaseMessageHandler, ChannelMessageHandler, ErrorAction, MessageSendEvent,
5+
};
6+
use crate::util::config::{ChannelConfigOverrides, ChannelHandshakeConfigUpdate, UserConfig};
7+
use lightning_types::features::ChannelTypeFeatures;
8+
9+
#[test]
10+
fn test_inbound_anchors_manual_acceptance() {
11+
let mut anchors_cfg = test_default_channel_config();
12+
anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
13+
do_test_manual_inbound_accept_with_override(anchors_cfg, None);
14+
}
15+
16+
#[test]
17+
fn test_inbound_anchors_manual_acceptance_overridden() {
18+
let overrides = ChannelConfigOverrides {
19+
handshake_overrides: Some(ChannelHandshakeConfigUpdate {
20+
max_inbound_htlc_value_in_flight_percent_of_channel: Some(5),
21+
htlc_minimum_msat: Some(1000),
22+
minimum_depth: Some(2),
23+
to_self_delay: Some(200),
24+
max_accepted_htlcs: Some(5),
25+
channel_reserve_proportional_millionths: Some(20000),
26+
}),
27+
update_overrides: None,
28+
};
29+
30+
let mut anchors_cfg = test_default_channel_config();
31+
anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
32+
33+
let accept_message = do_test_manual_inbound_accept_with_override(anchors_cfg, Some(overrides));
34+
assert_eq!(accept_message.common_fields.max_htlc_value_in_flight_msat, 5_000_000);
35+
assert_eq!(accept_message.common_fields.htlc_minimum_msat, 1_000);
36+
assert_eq!(accept_message.common_fields.minimum_depth, 2);
37+
assert_eq!(accept_message.common_fields.to_self_delay, 200);
38+
assert_eq!(accept_message.common_fields.max_accepted_htlcs, 5);
39+
assert_eq!(accept_message.channel_reserve_satoshis, 2_000);
40+
}
41+
42+
#[test]
43+
fn test_inbound_zero_fee_commitments_manual_acceptance() {
44+
let mut zero_fee_cfg = test_default_channel_config();
45+
zero_fee_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
46+
do_test_manual_inbound_accept_with_override(zero_fee_cfg, None);
47+
}
48+
49+
#[rustfmt::skip]
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 initiator_cfg = test_default_channel_config();
96+
initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
97+
98+
let mut receiver_cfg = test_default_channel_config();
99+
receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
100+
receiver_cfg.manually_accept_inbound_channels = true;
101+
102+
let start_type = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
103+
let end_type = ChannelTypeFeatures::only_static_remote_key();
104+
do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, vec![end_type]);
105+
}
106+
107+
#[test]
108+
fn test_scid_privacy_downgrade() {
109+
// Tests downgrade from `anchors_zero_fee_commitments` with `option_scid_alias` when the
110+
// remote node advertises the features but does not accept the channel, asserting that
111+
// `option_scid_alias` is the last feature to be downgraded.
112+
let mut initiator_cfg = test_default_channel_config();
113+
initiator_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
114+
initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
115+
initiator_cfg.channel_handshake_config.negotiate_scid_privacy = true;
116+
initiator_cfg.channel_handshake_config.announce_for_forwarding = false;
117+
118+
let mut receiver_cfg = test_default_channel_config();
119+
receiver_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
120+
receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
121+
receiver_cfg.channel_handshake_config.negotiate_scid_privacy = true;
122+
receiver_cfg.manually_accept_inbound_channels = true;
123+
124+
let mut start_type = ChannelTypeFeatures::anchors_zero_fee_commitments();
125+
start_type.set_scid_privacy_required();
126+
let mut with_anchors = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
127+
with_anchors.set_scid_privacy_required();
128+
let mut with_scid_privacy = ChannelTypeFeatures::only_static_remote_key();
129+
with_scid_privacy.set_scid_privacy_required();
130+
let static_remote = ChannelTypeFeatures::only_static_remote_key();
131+
let downgrade_types = vec![with_anchors, with_scid_privacy, static_remote];
132+
133+
do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, downgrade_types);
134+
}
135+
136+
#[test]
137+
fn test_zero_fee_commitments_downgrade() {
138+
// Tests that the local node will retry without zero fee commitments in the case where the
139+
// remote node supports the feature but does not accept it.
140+
let mut initiator_cfg = test_default_channel_config();
141+
initiator_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
142+
initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
143+
144+
let mut receiver_cfg = test_default_channel_config();
145+
receiver_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
146+
receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
147+
receiver_cfg.manually_accept_inbound_channels = true;
148+
149+
let start_type = ChannelTypeFeatures::anchors_zero_fee_commitments();
150+
let downgrade_types = vec![
151+
ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(),
152+
ChannelTypeFeatures::only_static_remote_key(),
153+
];
154+
do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, downgrade_types);
155+
}
156+
157+
#[test]
158+
fn test_zero_fee_commitments_downgrade_to_static_remote() {
159+
// Tests that the local node will retry with static remote key when zero fee commitments
160+
// are supported (but not accepted), but not legacy anchors.
161+
let mut initiator_cfg = test_default_channel_config();
162+
initiator_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
163+
initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
164+
165+
let mut receiver_cfg = test_default_channel_config();
166+
receiver_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
167+
receiver_cfg.manually_accept_inbound_channels = true;
168+
169+
let start_type = ChannelTypeFeatures::anchors_zero_fee_commitments();
170+
let end_type = ChannelTypeFeatures::only_static_remote_key();
171+
do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, vec![end_type]);
172+
}
173+
174+
#[rustfmt::skip]
175+
fn do_test_channel_type_downgrade(initiator_cfg: UserConfig, acceptor_cfg: UserConfig,
176+
start_type: ChannelTypeFeatures, downgrade_types: Vec<ChannelTypeFeatures>) {
177+
let chanmon_cfgs = create_chanmon_cfgs(2);
178+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
179+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(initiator_cfg), Some(acceptor_cfg)]);
180+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
181+
let error_message = "Channel force-closed";
182+
183+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 0, None, None).unwrap();
184+
let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
185+
assert_eq!(open_channel_msg.common_fields.channel_type.as_ref().unwrap(), &start_type);
186+
187+
for downgrade_type in downgrade_types {
188+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
189+
let events = nodes[1].node.get_and_clear_pending_events();
190+
match events[0] {
191+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
192+
nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id(), error_message.to_string()).unwrap();
193+
}
194+
_ => panic!("Unexpected event"),
195+
}
196+
197+
let error_msg = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
198+
nodes[0].node.handle_error(nodes[1].node.get_our_node_id(), &error_msg);
199+
200+
open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
201+
let channel_type = open_channel_msg.common_fields.channel_type.as_ref().unwrap();
202+
assert_eq!(channel_type, &downgrade_type);
203+
204+
// Since nodes[1] should not have accepted the channel, it should
205+
// not have generated any events.
206+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
207+
}
208+
}
209+
210+
#[test]
211+
#[rustfmt::skip]
212+
fn test_no_channel_downgrade() {
213+
// Tests that the local node will not retry when a `option_static_remote` channel is
214+
// rejected by a peer that advertises support for the feature.
215+
let initiator_cfg = test_default_channel_config();
216+
let mut receiver_cfg = test_default_channel_config();
217+
receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
218+
receiver_cfg.manually_accept_inbound_channels = true;
219+
220+
let chanmon_cfgs = create_chanmon_cfgs(2);
221+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
222+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(initiator_cfg), Some(receiver_cfg)]);
223+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
224+
let error_message = "Channel force-closed";
225+
226+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 0, None, None).unwrap();
227+
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
228+
let start_type = ChannelTypeFeatures::only_static_remote_key();
229+
assert_eq!(open_channel_msg.common_fields.channel_type.as_ref().unwrap(), &start_type);
230+
231+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
232+
let events = nodes[1].node.get_and_clear_pending_events();
233+
match events[0] {
234+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
235+
nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id(), error_message.to_string()).unwrap();
236+
}
237+
_ => panic!("Unexpected event"),
238+
}
239+
240+
let error_msg = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
241+
nodes[0].node.handle_error(nodes[1].node.get_our_node_id(), &error_msg);
242+
243+
// Since nodes[0] could not retry the channel with a different type, it should close it.
244+
let chan_closed_events = nodes[0].node.get_and_clear_pending_events();
245+
assert_eq!(chan_closed_events.len(), 1);
246+
if let Event::ChannelClosed { .. } = chan_closed_events[0] { } else { panic!(); }
247+
}

0 commit comments

Comments
 (0)