Skip to content

Commit 388e6bc

Browse files
refactor channelmanager tests into more appropriate submodule tests
1 parent b8b1ef3 commit 388e6bc

File tree

4 files changed

+2181
-1351
lines changed

4 files changed

+2181
-1351
lines changed
Lines changed: 263 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
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(
22+
3,
23+
&node_cfgs,
24+
&[
25+
Some(anchors_cfg.clone()),
26+
Some(anchors_cfg.clone()),
27+
Some(anchors_manual_accept_cfg.clone()),
28+
],
29+
);
30+
let nodes = create_network(3, &node_cfgs, &node_chanmgrs);
31+
32+
nodes[0]
33+
.node
34+
.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 42, None, None)
35+
.unwrap();
36+
let open_channel_msg = get_event_msg!(
37+
nodes[0],
38+
MessageSendEvent::SendOpenChannel,
39+
nodes[1].node.get_our_node_id()
40+
);
41+
42+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
43+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
44+
let msg_events = nodes[1].node.get_and_clear_pending_msg_events();
45+
match &msg_events[0] {
46+
MessageSendEvent::HandleError { node_id, action } => {
47+
assert_eq!(*node_id, nodes[0].node.get_our_node_id());
48+
match action {
49+
ErrorAction::SendErrorMessage { msg } => {
50+
assert_eq!(msg.data, "No channels with anchor outputs accepted".to_owned())
51+
},
52+
_ => panic!("Unexpected error action"),
53+
}
54+
},
55+
_ => panic!("Unexpected event"),
56+
}
57+
58+
nodes[2].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
59+
let events = nodes[2].node.get_and_clear_pending_events();
60+
match events[0] {
61+
Event::OpenChannelRequest { temporary_channel_id, .. } => nodes[2]
62+
.node
63+
.accept_inbound_channel(&temporary_channel_id, &nodes[0].node.get_our_node_id(), 23)
64+
.unwrap(),
65+
_ => panic!("Unexpected event"),
66+
}
67+
get_event_msg!(nodes[2], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
68+
}
69+
70+
#[test]
71+
fn test_anchors_zero_fee_htlc_tx_fallback() {
72+
// Tests that if both nodes support anchors, but the remote node does not want to accept
73+
// anchor channels at the moment, an error it sent to the local node such that it can retry
74+
// the channel without the anchors feature.
75+
let chanmon_cfgs = create_chanmon_cfgs(2);
76+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
77+
let mut anchors_config = test_default_channel_config();
78+
anchors_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
79+
anchors_config.manually_accept_inbound_channels = true;
80+
let node_chanmgrs = create_node_chanmgrs(
81+
2,
82+
&node_cfgs,
83+
&[Some(anchors_config.clone()), Some(anchors_config.clone())],
84+
);
85+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
86+
87+
nodes[0]
88+
.node
89+
.create_channel(nodes[1].node.get_our_node_id(), 100_000, 0, 0, None, None)
90+
.unwrap();
91+
let open_channel_msg = get_event_msg!(
92+
nodes[0],
93+
MessageSendEvent::SendOpenChannel,
94+
nodes[1].node.get_our_node_id()
95+
);
96+
assert!(open_channel_msg
97+
.common_fields
98+
.channel_type
99+
.as_ref()
100+
.unwrap()
101+
.supports_anchors_zero_fee_htlc_tx());
102+
103+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), &open_channel_msg);
104+
let events = nodes[1].node.get_and_clear_pending_events();
105+
match events[0] {
106+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
107+
nodes[1]
108+
.node
109+
.force_close_broadcasting_latest_txn(
110+
&temporary_channel_id,
111+
&nodes[0].node.get_our_node_id(),
112+
)
113+
.unwrap();
114+
},
115+
_ => panic!("Unexpected event"),
116+
}
117+
118+
let error_msg = get_err_msg(&nodes[1], &nodes[0].node.get_our_node_id());
119+
nodes[0].node.handle_error(&nodes[1].node.get_our_node_id(), &error_msg);
120+
121+
let open_channel_msg = get_event_msg!(
122+
nodes[0],
123+
MessageSendEvent::SendOpenChannel,
124+
nodes[1].node.get_our_node_id()
125+
);
126+
assert!(!open_channel_msg
127+
.common_fields
128+
.channel_type
129+
.unwrap()
130+
.supports_anchors_zero_fee_htlc_tx());
131+
132+
// Since nodes[1] should not have accepted the channel, it should
133+
// not have generated any events.
134+
assert!(nodes[1].node.get_and_clear_pending_events().is_empty());
135+
}
136+
137+
#[test]
138+
fn test_update_channel_config() {
139+
let chanmon_cfg = create_chanmon_cfgs(2);
140+
let node_cfg = create_node_cfgs(2, &chanmon_cfg);
141+
let mut user_config = test_default_channel_config();
142+
let node_chanmgr = create_node_chanmgrs(2, &node_cfg, &[Some(user_config), Some(user_config)]);
143+
let nodes = create_network(2, &node_cfg, &node_chanmgr);
144+
let _ = create_announced_chan_between_nodes(&nodes, 0, 1);
145+
let channel = &nodes[0].node.list_channels()[0];
146+
147+
nodes[0]
148+
.node
149+
.update_channel_config(
150+
&channel.counterparty.node_id,
151+
&[channel.channel_id],
152+
&user_config.channel_config,
153+
)
154+
.unwrap();
155+
let events = nodes[0].node.get_and_clear_pending_msg_events();
156+
assert_eq!(events.len(), 0);
157+
158+
user_config.channel_config.forwarding_fee_base_msat += 10;
159+
nodes[0]
160+
.node
161+
.update_channel_config(
162+
&channel.counterparty.node_id,
163+
&[channel.channel_id],
164+
&user_config.channel_config,
165+
)
166+
.unwrap();
167+
assert_eq!(
168+
nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_base_msat,
169+
user_config.channel_config.forwarding_fee_base_msat
170+
);
171+
let events = nodes[0].node.get_and_clear_pending_msg_events();
172+
assert_eq!(events.len(), 1);
173+
match &events[0] {
174+
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
175+
_ => panic!("expected BroadcastChannelUpdate event"),
176+
}
177+
178+
nodes[0]
179+
.node
180+
.update_partial_channel_config(
181+
&channel.counterparty.node_id,
182+
&[channel.channel_id],
183+
&ChannelConfigUpdate::default(),
184+
)
185+
.unwrap();
186+
let events = nodes[0].node.get_and_clear_pending_msg_events();
187+
assert_eq!(events.len(), 0);
188+
189+
let new_cltv_expiry_delta = user_config.channel_config.cltv_expiry_delta + 6;
190+
nodes[0]
191+
.node
192+
.update_partial_channel_config(
193+
&channel.counterparty.node_id,
194+
&[channel.channel_id],
195+
&ChannelConfigUpdate {
196+
cltv_expiry_delta: Some(new_cltv_expiry_delta),
197+
..Default::default()
198+
},
199+
)
200+
.unwrap();
201+
assert_eq!(
202+
nodes[0].node.list_channels()[0].config.unwrap().cltv_expiry_delta,
203+
new_cltv_expiry_delta
204+
);
205+
let events = nodes[0].node.get_and_clear_pending_msg_events();
206+
assert_eq!(events.len(), 1);
207+
match &events[0] {
208+
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
209+
_ => panic!("expected BroadcastChannelUpdate event"),
210+
}
211+
212+
let new_fee = user_config.channel_config.forwarding_fee_proportional_millionths + 100;
213+
nodes[0]
214+
.node
215+
.update_partial_channel_config(
216+
&channel.counterparty.node_id,
217+
&[channel.channel_id],
218+
&ChannelConfigUpdate {
219+
forwarding_fee_proportional_millionths: Some(new_fee),
220+
..Default::default()
221+
},
222+
)
223+
.unwrap();
224+
assert_eq!(
225+
nodes[0].node.list_channels()[0].config.unwrap().cltv_expiry_delta,
226+
new_cltv_expiry_delta
227+
);
228+
assert_eq!(
229+
nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths,
230+
new_fee
231+
);
232+
let events = nodes[0].node.get_and_clear_pending_msg_events();
233+
assert_eq!(events.len(), 1);
234+
match &events[0] {
235+
MessageSendEvent::BroadcastChannelUpdate { .. } => {},
236+
_ => panic!("expected BroadcastChannelUpdate event"),
237+
}
238+
239+
// If we provide a channel_id not associated with the peer, we should get an error and no updates
240+
// should be applied to ensure update atomicity as specified in the API docs.
241+
let bad_channel_id = ChannelId::v1_from_funding_txid(&[10; 32], 10);
242+
let current_fee =
243+
nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths;
244+
let new_fee = current_fee + 100;
245+
assert!(matches!(
246+
nodes[0].node.update_partial_channel_config(
247+
&channel.counterparty.node_id,
248+
&[channel.channel_id, bad_channel_id],
249+
&ChannelConfigUpdate {
250+
forwarding_fee_proportional_millionths: Some(new_fee),
251+
..Default::default()
252+
}
253+
),
254+
Err(APIError::ChannelUnavailable { err: _ }),
255+
));
256+
// Check that the fee hasn't changed for the channel that exists.
257+
assert_eq!(
258+
nodes[0].node.list_channels()[0].config.unwrap().forwarding_fee_proportional_millionths,
259+
current_fee
260+
);
261+
let events = nodes[0].node.get_and_clear_pending_msg_events();
262+
assert_eq!(events.len(), 0);
263+
}

0 commit comments

Comments
 (0)