Skip to content

Commit abcd89b

Browse files
committed
ln/refactor: move channelmanager acceptance tests to separate file
1 parent b8673f3 commit abcd89b

File tree

3 files changed

+368
-358
lines changed

3 files changed

+368
-358
lines changed
Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
1+
use crate::events::Event;
2+
use crate::ln::channelmanager::{MAX_UNFUNDED_CHANNEL_PEERS, MAX_UNFUNDED_CHANS_PER_PEER};
3+
use crate::ln::msgs::{
4+
AcceptChannel, BaseMessageHandler, ChannelMessageHandler, ErrorAction, MessageSendEvent,
5+
};
6+
use crate::ln::types::ChannelId;
7+
use crate::ln::{functional_test_utils::*, msgs};
8+
use crate::sign::EntropySource;
9+
use crate::util::config::{ChannelConfigOverrides, ChannelHandshakeConfigUpdate, UserConfig};
10+
use crate::util::errors::APIError;
11+
use bitcoin::secp256k1::{PublicKey, SecretKey};
12+
use lightning_types::features::ChannelTypeFeatures;
13+
14+
#[test]
15+
#[rustfmt::skip]
16+
fn test_outbound_chans_unlimited() {
17+
// Test that we never refuse an outbound channel even if a peer is unfuned-channel-limited
18+
let chanmon_cfgs = create_chanmon_cfgs(2);
19+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
20+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
21+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
22+
23+
// Note that create_network connects the nodes together for us
24+
25+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap();
26+
let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
27+
28+
for _ in 0..MAX_UNFUNDED_CHANS_PER_PEER {
29+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
30+
get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
31+
open_channel_msg.common_fields.temporary_channel_id = ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
32+
}
33+
34+
// Once we have MAX_UNFUNDED_CHANS_PER_PEER unfunded channels, new inbound channels will be
35+
// rejected.
36+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
37+
assert_eq!(get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id()).channel_id,
38+
open_channel_msg.common_fields.temporary_channel_id);
39+
40+
// but we can still open an outbound channel.
41+
nodes[1].node.create_channel(nodes[0].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap();
42+
get_event_msg!(nodes[1], MessageSendEvent::SendOpenChannel, nodes[0].node.get_our_node_id());
43+
44+
// but even with such an outbound channel, additional inbound channels will still fail.
45+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
46+
assert_eq!(get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id()).channel_id,
47+
open_channel_msg.common_fields.temporary_channel_id);
48+
}
49+
50+
#[test]
51+
#[rustfmt::skip]
52+
fn test_0conf_limiting() {
53+
// Tests that we properly limit inbound channels when we have the manual-channel-acceptance
54+
// flag set and (sometimes) accept channels as 0conf.
55+
let chanmon_cfgs = create_chanmon_cfgs(2);
56+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
57+
let mut settings = test_default_channel_config();
58+
settings.manually_accept_inbound_channels = true;
59+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(settings)]);
60+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
61+
62+
// Note that create_network connects the nodes together for us
63+
64+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap();
65+
let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
66+
67+
// First, get us up to MAX_UNFUNDED_CHANNEL_PEERS so we can test at the edge
68+
for _ in 0..MAX_UNFUNDED_CHANNEL_PEERS - 1 {
69+
let random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
70+
&SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
71+
nodes[1].node.peer_connected(random_pk, &msgs::Init {
72+
features: nodes[0].node.init_features(), networks: None, remote_network_address: None
73+
}, true).unwrap();
74+
75+
nodes[1].node.handle_open_channel(random_pk, &open_channel_msg);
76+
let events = nodes[1].node.get_and_clear_pending_events();
77+
match events[0] {
78+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
79+
nodes[1].node.accept_inbound_channel(&temporary_channel_id, &random_pk, 23, None).unwrap();
80+
}
81+
_ => panic!("Unexpected event"),
82+
}
83+
get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, random_pk);
84+
open_channel_msg.common_fields.temporary_channel_id = ChannelId::temporary_from_entropy_source(&nodes[0].keys_manager);
85+
}
86+
87+
// If we try to accept a channel from another peer non-0conf it will fail.
88+
let last_random_pk = PublicKey::from_secret_key(&nodes[0].node.secp_ctx,
89+
&SecretKey::from_slice(&nodes[1].keys_manager.get_secure_random_bytes()).unwrap());
90+
nodes[1].node.peer_connected(last_random_pk, &msgs::Init {
91+
features: nodes[0].node.init_features(), networks: None, remote_network_address: None
92+
}, true).unwrap();
93+
nodes[1].node.handle_open_channel(last_random_pk, &open_channel_msg);
94+
let events = nodes[1].node.get_and_clear_pending_events();
95+
match events[0] {
96+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
97+
match nodes[1].node.accept_inbound_channel(&temporary_channel_id, &last_random_pk, 23, None) {
98+
Err(APIError::APIMisuseError { err }) =>
99+
assert_eq!(err, "Too many peers with unfunded channels, refusing to accept new ones"),
100+
_ => panic!(),
101+
}
102+
}
103+
_ => panic!("Unexpected event"),
104+
}
105+
assert_eq!(get_err_msg(&nodes[1], &last_random_pk).channel_id,
106+
open_channel_msg.common_fields.temporary_channel_id);
107+
108+
// ...however if we accept the same channel 0conf it should work just fine.
109+
nodes[1].node.handle_open_channel(last_random_pk, &open_channel_msg);
110+
let events = nodes[1].node.get_and_clear_pending_events();
111+
match events[0] {
112+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
113+
nodes[1].node.accept_inbound_channel_from_trusted_peer_0conf(&temporary_channel_id, &last_random_pk, 23, None).unwrap();
114+
}
115+
_ => panic!("Unexpected event"),
116+
}
117+
get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, last_random_pk);
118+
}
119+
120+
#[test]
121+
fn test_inbound_anchors_manual_acceptance() {
122+
let mut anchors_cfg = test_default_channel_config();
123+
anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
124+
do_test_manual_inbound_accept_with_override(anchors_cfg, None);
125+
}
126+
127+
#[test]
128+
fn test_inbound_anchors_manual_acceptance_overridden() {
129+
let overrides = ChannelConfigOverrides {
130+
handshake_overrides: Some(ChannelHandshakeConfigUpdate {
131+
max_inbound_htlc_value_in_flight_percent_of_channel: Some(5),
132+
htlc_minimum_msat: Some(1000),
133+
minimum_depth: Some(2),
134+
to_self_delay: Some(200),
135+
max_accepted_htlcs: Some(5),
136+
channel_reserve_proportional_millionths: Some(20000),
137+
}),
138+
update_overrides: None,
139+
};
140+
141+
let mut anchors_cfg = test_default_channel_config();
142+
anchors_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
143+
144+
let accept_message = do_test_manual_inbound_accept_with_override(anchors_cfg, Some(overrides));
145+
assert_eq!(accept_message.common_fields.max_htlc_value_in_flight_msat, 5_000_000);
146+
assert_eq!(accept_message.common_fields.htlc_minimum_msat, 1_000);
147+
assert_eq!(accept_message.common_fields.minimum_depth, 2);
148+
assert_eq!(accept_message.common_fields.to_self_delay, 200);
149+
assert_eq!(accept_message.common_fields.max_accepted_htlcs, 5);
150+
assert_eq!(accept_message.channel_reserve_satoshis, 2_000);
151+
}
152+
153+
#[test]
154+
fn test_inbound_zero_fee_commitments_manual_acceptance() {
155+
let mut zero_fee_cfg = test_default_channel_config();
156+
zero_fee_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
157+
do_test_manual_inbound_accept_with_override(zero_fee_cfg, None);
158+
}
159+
160+
#[rustfmt::skip]
161+
fn do_test_manual_inbound_accept_with_override(start_cfg: UserConfig,
162+
config_overrides: Option<ChannelConfigOverrides>) -> AcceptChannel {
163+
164+
let mut mannual_accept_cfg = start_cfg.clone();
165+
mannual_accept_cfg.manually_accept_inbound_channels = true;
166+
167+
let chanmon_cfgs = create_chanmon_cfgs(3);
168+
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
169+
let node_chanmgrs = create_node_chanmgrs(3, &node_cfgs,
170+
&[Some(start_cfg.clone()), Some(start_cfg.clone()), Some(mannual_accept_cfg.clone())]);
171+
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
172+
173+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None).unwrap();
174+
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
175+
176+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
177+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
178+
let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
179+
match &msg_events[0] {
180+
MessageSendEvent::HandleError { node_id, action } => {
181+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
182+
match action {
183+
ErrorAction::SendErrorMessage { msg } =>
184+
assert_eq!(msg.data, "No channels with anchor outputs accepted".to_owned()),
185+
_ => panic!("Unexpected error action"),
186+
}
187+
}
188+
_ => panic!("Unexpected event"),
189+
}
190+
191+
nodes[2].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
192+
let events = nodes[2].node.get_and_clear_pending_events();
193+
match events[0] {
194+
Event::OpenChannelRequest { temporary_channel_id, .. } =>
195+
nodes[2].node.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 23, config_overrides).unwrap(),
196+
_ => panic!("Unexpected event"),
197+
}
198+
get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id())
199+
}
200+
201+
#[test]
202+
fn test_anchors_zero_fee_htlc_tx_downgrade() {
203+
// Tests that if both nodes support anchors, but the remote node does not want to accept
204+
// anchor channels at the moment, an error it sent to the local node such that it can retry
205+
// the channel without the anchors feature.
206+
let mut initiator_cfg = test_default_channel_config();
207+
initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
208+
209+
let mut receiver_cfg = test_default_channel_config();
210+
receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
211+
receiver_cfg.manually_accept_inbound_channels = true;
212+
213+
let start_type = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
214+
let end_type = ChannelTypeFeatures::only_static_remote_key();
215+
do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, vec![end_type]);
216+
}
217+
218+
#[test]
219+
fn test_scid_privacy_downgrade() {
220+
// Tests downgrade from `anchors_zero_fee_commitments` with `option_scid_alias` when the
221+
// remote node advertises the features but does not accept the channel, asserting that
222+
// `option_scid_alias` is the last feature to be downgraded.
223+
let mut initiator_cfg = test_default_channel_config();
224+
initiator_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
225+
initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
226+
initiator_cfg.channel_handshake_config.negotiate_scid_privacy = true;
227+
initiator_cfg.channel_handshake_config.announce_for_forwarding = false;
228+
229+
let mut receiver_cfg = test_default_channel_config();
230+
receiver_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
231+
receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
232+
receiver_cfg.channel_handshake_config.negotiate_scid_privacy = true;
233+
receiver_cfg.manually_accept_inbound_channels = true;
234+
235+
let mut start_type = ChannelTypeFeatures::anchors_zero_fee_commitments();
236+
start_type.set_scid_privacy_required();
237+
let mut with_anchors = ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies();
238+
with_anchors.set_scid_privacy_required();
239+
let mut with_scid_privacy = ChannelTypeFeatures::only_static_remote_key();
240+
with_scid_privacy.set_scid_privacy_required();
241+
let static_remote = ChannelTypeFeatures::only_static_remote_key();
242+
let downgrade_types = vec![with_anchors, with_scid_privacy, static_remote];
243+
244+
do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, downgrade_types);
245+
}
246+
247+
#[test]
248+
fn test_zero_fee_commitments_downgrade() {
249+
// Tests that the local node will retry without zero fee commitments in the case where the
250+
// remote node supports the feature but does not accept it.
251+
let mut initiator_cfg = test_default_channel_config();
252+
initiator_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
253+
initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
254+
255+
let mut receiver_cfg = test_default_channel_config();
256+
receiver_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
257+
receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
258+
receiver_cfg.manually_accept_inbound_channels = true;
259+
260+
let start_type = ChannelTypeFeatures::anchors_zero_fee_commitments();
261+
let downgrade_types = vec![
262+
ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies(),
263+
ChannelTypeFeatures::only_static_remote_key(),
264+
];
265+
do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, downgrade_types);
266+
}
267+
268+
#[test]
269+
fn test_zero_fee_commitments_downgrade_to_static_remote() {
270+
// Tests that the local node will retry with static remote key when zero fee commitments
271+
// are supported (but not accepted), but not legacy anchors.
272+
let mut initiator_cfg = test_default_channel_config();
273+
initiator_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
274+
initiator_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
275+
276+
let mut receiver_cfg = test_default_channel_config();
277+
receiver_cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
278+
receiver_cfg.manually_accept_inbound_channels = true;
279+
280+
let start_type = ChannelTypeFeatures::anchors_zero_fee_commitments();
281+
let end_type = ChannelTypeFeatures::only_static_remote_key();
282+
do_test_channel_type_downgrade(initiator_cfg, receiver_cfg, start_type, vec![end_type]);
283+
}
284+
285+
#[rustfmt::skip]
286+
fn do_test_channel_type_downgrade(initiator_cfg: UserConfig, acceptor_cfg: UserConfig,
287+
start_type: ChannelTypeFeatures, downgrade_types: Vec<ChannelTypeFeatures>) {
288+
let chanmon_cfgs = create_chanmon_cfgs(2);
289+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
290+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(initiator_cfg), Some(acceptor_cfg)]);
291+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
292+
let error_message = "Channel force-closed";
293+
294+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 0, None, None).unwrap();
295+
let mut open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
296+
assert_eq!(open_channel_msg.common_fields.channel_type.as_ref().unwrap(), &start_type);
297+
298+
for downgrade_type in downgrade_types {
299+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
300+
let events = nodes[1].node.get_and_clear_pending_events();
301+
match events[0] {
302+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
303+
nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id(), error_message.to_string()).unwrap();
304+
}
305+
_ => panic!("Unexpected event"),
306+
}
307+
308+
let error_msg = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
309+
nodes[0].node.handle_error(nodes[1].node.get_our_node_id(), &error_msg);
310+
311+
open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
312+
let channel_type = open_channel_msg.common_fields.channel_type.as_ref().unwrap();
313+
assert_eq!(channel_type, &downgrade_type);
314+
315+
// Since nodes[1] should not have accepted the channel, it should
316+
// not have generated any events.
317+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
318+
}
319+
}
320+
321+
#[test]
322+
#[rustfmt::skip]
323+
fn test_no_channel_downgrade() {
324+
// Tests that the local node will not retry when a `option_static_remote` channel is
325+
// rejected by a peer that advertises support for the feature.
326+
let initiator_cfg = test_default_channel_config();
327+
let mut receiver_cfg = test_default_channel_config();
328+
receiver_cfg.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
329+
receiver_cfg.manually_accept_inbound_channels = true;
330+
331+
let chanmon_cfgs = create_chanmon_cfgs(2);
332+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
333+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(initiator_cfg), Some(receiver_cfg)]);
334+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
335+
let error_message = "Channel force-closed";
336+
337+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 0, None, None).unwrap();
338+
let open_channel_msg = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
339+
let start_type = ChannelTypeFeatures::only_static_remote_key();
340+
assert_eq!(open_channel_msg.common_fields.channel_type.as_ref().unwrap(), &start_type);
341+
342+
nodes[1].node.handle_open_channel(nodes[0].node.get_our_node_id(), &open_channel_msg);
343+
let events = nodes[1].node.get_and_clear_pending_events();
344+
match events[0] {
345+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
346+
nodes[1].node.force_close_broadcasting_latest_txn(&temporary_channel_id, &nodes[0].node.get_our_node_id(), error_message.to_string()).unwrap();
347+
}
348+
_ => panic!("Unexpected event"),
349+
}
350+
351+
let error_msg = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
352+
nodes[0].node.handle_error(nodes[1].node.get_our_node_id(), &error_msg);
353+
354+
// Since nodes[0] could not retry the channel with a different type, it should close it.
355+
let chan_closed_events = nodes[0].node.get_and_clear_pending_events();
356+
assert_eq!(chan_closed_events.len(), 1);
357+
if let Event::ChannelClosed { .. } = chan_closed_events[0] { } else { panic!(); }
358+
}

0 commit comments

Comments
 (0)