Skip to content

Commit 4eed222

Browse files
committed
ln/refactor: move channelmanager acceptance tests to separate file
1 parent e430212 commit 4eed222

File tree

3 files changed

+249
-241
lines changed

3 files changed

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

0 commit comments

Comments
 (0)