Skip to content

Commit b9ec89c

Browse files
TheBlueMattcarlaKC
authored andcommitted
ln: set max_htlcs based on the channel type
1 parent 08e6a0f commit b9ec89c

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

lightning/src/ln/chan_utils.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,13 @@ use crate::prelude::*;
5656
/// 483 for non-zero-fee-commitment channels and 114 for zero-fee-commitment channels.
5757
///
5858
/// Actual maximums can be set equal to or below this value by each channel participant.
59-
pub fn max_htlcs(_channel_type: &ChannelTypeFeatures) -> u16 {
60-
483
59+
pub fn max_htlcs(channel_type: &ChannelTypeFeatures) -> u16 {
60+
if channel_type.supports_anchor_zero_fee_commitments() {
61+
// TRUC restricts the size of our commitment transactions to 10K vB rather than 100K vB
62+
114
63+
} else {
64+
483
65+
}
6166
}
6267
/// The weight of a BIP141 witnessScript for a BOLT3's "offered HTLC output" on a commitment transaction, non-anchor variant.
6368
pub const OFFERED_HTLC_SCRIPT_WEIGHT: usize = 133;

lightning/src/ln/functional_tests.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,66 @@ pub fn test_insane_channel_opens() {
256256
});
257257
}
258258

259+
#[test]
260+
fn test_insane_zero_fee_channel_open() {
261+
let mut cfg = UserConfig::default();
262+
cfg.manually_accept_inbound_channels = true;
263+
cfg.channel_handshake_config.negotiate_anchor_zero_fee_commitments = true;
264+
265+
let chanmon_cfgs = create_chanmon_cfgs(2);
266+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
267+
let node_chanmgrs =
268+
create_node_chanmgrs(2, &node_cfgs, &[Some(cfg.clone()), Some(cfg.clone())]);
269+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
270+
271+
let node_a_id = nodes[0].node.get_our_node_id();
272+
let node_b_id = nodes[1].node.get_our_node_id();
273+
274+
nodes[0].node.create_channel(node_b_id, 100_000, 0, 42, None, None).unwrap();
275+
276+
let open_channel_message =
277+
get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, node_b_id);
278+
279+
let insane_open_helper =
280+
|expected_error_str: &str, message_mutator: fn(msgs::OpenChannel) -> msgs::OpenChannel| {
281+
let open_channel_mutated = message_mutator(open_channel_message.clone());
282+
nodes[1].node.handle_open_channel(node_a_id, &open_channel_mutated);
283+
284+
let events = nodes[1].node.get_and_clear_pending_events();
285+
match events[0] {
286+
Event::OpenChannelRequest { temporary_channel_id, .. } => {
287+
match nodes[1].node.accept_inbound_channel(
288+
&temporary_channel_id,
289+
&nodes[0].node.get_our_node_id(),
290+
23,
291+
None,
292+
) {
293+
Ok(_) => panic!("Unexpected successful channel accept"),
294+
Err(e) => assert!(format!("{:?}", e).contains(expected_error_str)),
295+
}
296+
},
297+
_ => panic!("Unexpected event"),
298+
}
299+
300+
let events = nodes[1].node.get_and_clear_pending_msg_events();
301+
assert_eq!(events.len(), 1);
302+
assert!(matches!(events[0], MessageSendEvent::HandleError { .. }));
303+
};
304+
305+
insane_open_helper(
306+
"max_accepted_htlcs was 115. It must not be larger than 114".into(),
307+
|mut msg| {
308+
msg.common_fields.max_accepted_htlcs = 115;
309+
msg
310+
},
311+
);
312+
313+
insane_open_helper("Zero Fee Channels must never attempt to use a fee".into(), |mut msg| {
314+
msg.common_fields.commitment_feerate_sat_per_1000_weight = 123;
315+
msg
316+
});
317+
}
318+
259319
#[xtest(feature = "_externalize_tests")]
260320
pub fn test_funding_exceeds_no_wumbo_limit() {
261321
// Test that if a peer does not support wumbo channels, we'll refuse to open a wumbo channel to

0 commit comments

Comments
 (0)