Skip to content

Commit 499165a

Browse files
committed
Make channel_type required
Set the option_channel_type feature as required and fail the channel if channel_type is not included during channel negotiation in open_channel or accept_channel.
1 parent 808d1dc commit 499165a

File tree

4 files changed

+48
-39
lines changed

4 files changed

+48
-39
lines changed

fuzz/src/full_stack.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ fn two_peer_forwarding_seed() -> Vec<u8> {
10671067
// inbound read from peer id 0 of len 32
10681068
ext_from_hex("030020", &mut test);
10691069
// init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1070-
ext_from_hex("0010 00021aaa 0008aaa208aa2a0a9aaa 03000000000000000000000000000000", &mut test);
1070+
ext_from_hex("0010 00021aaa 0008aaa218aa2a0a9aaa 03000000000000000000000000000000", &mut test);
10711071

10721072
// inbound read from peer id 0 of len 18
10731073
ext_from_hex("030012", &mut test);
@@ -1168,7 +1168,7 @@ fn two_peer_forwarding_seed() -> Vec<u8> {
11681168
// inbound read from peer id 1 of len 32
11691169
ext_from_hex("030120", &mut test);
11701170
// init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1171-
ext_from_hex("0010 00021aaa 0008aaa208aa2a0a9aaa 01000000000000000000000000000000", &mut test);
1171+
ext_from_hex("0010 00021aaa 0008aaa218aa2a0a9aaa 01000000000000000000000000000000", &mut test);
11721172

11731173
// create outbound channel to peer 1 for 50k sat
11741174
ext_from_hex(
@@ -1181,16 +1181,16 @@ fn two_peer_forwarding_seed() -> Vec<u8> {
11811181
// inbound read from peer id 1 of len 18
11821182
ext_from_hex("030112", &mut test);
11831183
// message header indicating message length 274
1184-
ext_from_hex("0112 01000000000000000000000000000000", &mut test);
1184+
ext_from_hex("0116 01000000000000000000000000000000", &mut test);
11851185
// inbound read from peer id 1 of len 255
11861186
ext_from_hex("0301ff", &mut test);
11871187
// beginning of accept_channel
11881188
ext_from_hex("0021 0000000000000000000000000000000000000000000000000000000000000e12 0000000000000162 00000000004c4b40 00000000000003e8 00000000000003e8 00000002 03f0 0005 030000000000000000000000000000000000000000000000000000000000000100 030000000000000000000000000000000000000000000000000000000000000200 030000000000000000000000000000000000000000000000000000000000000300 030000000000000000000000000000000000000000000000000000000000000400 030000000000000000000000000000000000000000000000000000000000000500 02660000000000000000000000000000", &mut test);
11891189
// inbound read from peer id 1 of len 35
1190-
ext_from_hex("030123", &mut test);
1190+
ext_from_hex("030127", &mut test);
11911191
// rest of accept_channel and mac
11921192
ext_from_hex(
1193-
"0000000000000000000000000000000000 0000 01000000000000000000000000000000",
1193+
"0000000000000000000000000000000000 0000 01021000 01000000000000000000000000000000",
11941194
&mut test,
11951195
);
11961196

@@ -1583,7 +1583,7 @@ fn gossip_exchange_seed() -> Vec<u8> {
15831583
// inbound read from peer id 0 of len 32
15841584
ext_from_hex("030020", &mut test);
15851585
// init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1586-
ext_from_hex("0010 00021aaa 0008aaa20aaa2a0a9aaa 03000000000000000000000000000000", &mut test);
1586+
ext_from_hex("0010 00021aaa 0008aaa218aa2a0a9aaa 03000000000000000000000000000000", &mut test);
15871587

15881588
// new inbound connection with id 1
15891589
ext_from_hex("01", &mut test);
@@ -1603,7 +1603,7 @@ fn gossip_exchange_seed() -> Vec<u8> {
16031603
// inbound read from peer id 1 of len 32
16041604
ext_from_hex("030120", &mut test);
16051605
// init message (type 16) with static_remotekey required, no channel_type/anchors/taproot, and other bits optional and mac
1606-
ext_from_hex("0010 00021aaa 0008aaa20aaa2a0a9aaa 01000000000000000000000000000000", &mut test);
1606+
ext_from_hex("0010 00021aaa 0008aaa218aa2a0a9aaa 01000000000000000000000000000000", &mut test);
16071607

16081608
// inbound read from peer id 0 of len 18
16091609
ext_from_hex("030012", &mut test);

lightning/src/ln/channel.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3842,14 +3842,8 @@ where
38423842
if ty != funding.get_channel_type() {
38433843
return Err(ChannelError::close("Channel Type in accept_channel didn't match the one sent in open_channel.".to_owned()));
38443844
}
3845-
} else if their_features.supports_channel_type() {
3846-
// Assume they've accepted the channel type as they said they understand it.
38473845
} else {
3848-
let channel_type = ChannelTypeFeatures::from_init(&their_features);
3849-
if channel_type != ChannelTypeFeatures::only_static_remote_key() {
3850-
return Err(ChannelError::close("Only static_remote_key is supported for non-negotiated channel types".to_owned()));
3851-
}
3852-
funding.channel_transaction_parameters.channel_type_features = channel_type;
3846+
return Err(ChannelError::close("channel_type assumed to be supported".to_owned()));
38533847
}
38543848

38553849
if common_fields.dust_limit_satoshis > 21000000 * 100000000 {
@@ -11542,8 +11536,7 @@ where
1154211536
/// [`msgs::CommonOpenChannelFields`].
1154311537
#[rustfmt::skip]
1154411538
pub(super) fn channel_type_from_open_channel(
11545-
common_fields: &msgs::CommonOpenChannelFields, their_features: &InitFeatures,
11546-
our_supported_features: &ChannelTypeFeatures
11539+
common_fields: &msgs::CommonOpenChannelFields, our_supported_features: &ChannelTypeFeatures
1154711540
) -> Result<ChannelTypeFeatures, ChannelError> {
1154811541
if let Some(channel_type) = &common_fields.channel_type {
1154911542
if channel_type.supports_any_optional_bits() {
@@ -11567,11 +11560,7 @@ pub(super) fn channel_type_from_open_channel(
1156711560
}
1156811561
Ok(channel_type.clone())
1156911562
} else {
11570-
let channel_type = ChannelTypeFeatures::from_init(&their_features);
11571-
if channel_type != ChannelTypeFeatures::only_static_remote_key() {
11572-
return Err(ChannelError::close("Only static_remote_key is supported for non-negotiated channel types".to_owned()));
11573-
}
11574-
Ok(channel_type)
11563+
return Err(ChannelError::close("channel_type assumed to be supported".to_owned()));
1157511564
}
1157611565
}
1157711566

@@ -11596,7 +11585,7 @@ where
1159611585

1159711586
// First check the channel type is known, failing before we do anything else if we don't
1159811587
// support this channel type.
11599-
let channel_type = channel_type_from_open_channel(&msg.common_fields, their_features, our_supported_features)?;
11588+
let channel_type = channel_type_from_open_channel(&msg.common_fields, our_supported_features)?;
1160011589

1160111590
let holder_selected_channel_reserve_satoshis = get_holder_selected_channel_reserve_satoshis(msg.common_fields.funding_satoshis, config);
1160211591
let counterparty_pubkeys = ChannelPublicKeys {
@@ -11999,7 +11988,7 @@ where
1199911988
return Err(ChannelError::close(format!("Rejecting V2 channel {} missing channel_type",
1200011989
msg.common_fields.temporary_channel_id)))
1200111990
}
12002-
let channel_type = channel_type_from_open_channel(&msg.common_fields, their_features, our_supported_features)?;
11991+
let channel_type = channel_type_from_open_channel(&msg.common_fields, our_supported_features)?;
1200311992

1200411993
let counterparty_pubkeys = ChannelPublicKeys {
1200511994
funding_pubkey: msg.common_fields.funding_pubkey,

lightning/src/ln/channel_type_tests.rs

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ fn do_test_supports_channel_type(config: UserConfig, expected_channel_type: Chan
295295
}
296296

297297
#[test]
298-
fn test_rejects_implicit_simple_anchors() {
299-
// Tests that if `option_anchors` is being negotiated implicitly through the intersection of
300-
// each side's `InitFeatures`, it is rejected.
298+
fn test_rejects_if_channel_type_not_set() {
299+
// Tests that if `channel_type` is not set in `open_channel` and `accept_channel`, it is
300+
// rejected.
301301
let secp_ctx = Secp256k1::new();
302302
let test_est = TestFeeEstimator::new(15000);
303303
let fee_estimator = LowerBoundedFeeEstimator::new(&test_est);
@@ -312,13 +312,6 @@ fn test_rejects_implicit_simple_anchors() {
312312

313313
let config = UserConfig::default();
314314

315-
// See feature bit assignments: https://github.com/lightning/bolts/blob/master/09-features.md
316-
let static_remote_key_required: u64 = 1 << 12;
317-
let simple_anchors_required: u64 = 1 << 20;
318-
let raw_init_features = static_remote_key_required | simple_anchors_required;
319-
let init_features_with_simple_anchors =
320-
InitFeatures::from_le_bytes(raw_init_features.to_le_bytes().to_vec());
321-
322315
let mut channel_a = OutboundV1Channel::<&TestKeysInterface>::new(
323316
&fee_estimator,
324317
&&keys_provider,
@@ -336,20 +329,18 @@ fn test_rejects_implicit_simple_anchors() {
336329
)
337330
.unwrap();
338331

339-
// Set `channel_type` to `None` to force the implicit feature negotiation.
332+
// Set `channel_type` to `None` to cause failure.
340333
let mut open_channel_msg =
341334
channel_a.get_open_channel(ChainHash::using_genesis_block(network), &&logger).unwrap();
342335
open_channel_msg.common_fields.channel_type = None;
343336

344-
// Since A supports both `static_remote_key` and `option_anchors`, but B only accepts
345-
// `static_remote_key`, it will fail the channel.
346337
let channel_b = InboundV1Channel::<&TestKeysInterface>::new(
347338
&fee_estimator,
348339
&&keys_provider,
349340
&&keys_provider,
350341
node_id_a,
351342
&channelmanager::provided_channel_type_features(&config),
352-
&init_features_with_simple_anchors,
343+
&channelmanager::provided_init_features(&config),
353344
&open_channel_msg,
354345
7,
355346
&config,
@@ -358,6 +349,35 @@ fn test_rejects_implicit_simple_anchors() {
358349
/*is_0conf=*/ false,
359350
);
360351
assert!(channel_b.is_err());
352+
353+
open_channel_msg.common_fields.channel_type =
354+
Some(channel_a.funding.get_channel_type().clone());
355+
let mut channel_b = InboundV1Channel::<&TestKeysInterface>::new(
356+
&fee_estimator,
357+
&&keys_provider,
358+
&&keys_provider,
359+
node_id_a,
360+
&channelmanager::provided_channel_type_features(&config),
361+
&channelmanager::provided_init_features(&config),
362+
&open_channel_msg,
363+
7,
364+
&config,
365+
0,
366+
&&logger,
367+
/*is_0conf=*/ false,
368+
)
369+
.unwrap();
370+
371+
// Set `channel_type` to `None` in `accept_channel` to cause failure.
372+
let mut accept_channel_msg = channel_b.get_accept_channel_message(&&logger).unwrap();
373+
accept_channel_msg.common_fields.channel_type = None;
374+
375+
let res = channel_a.accept_channel(
376+
&accept_channel_msg,
377+
&config.channel_handshake_limits,
378+
&channelmanager::provided_init_features(&config),
379+
);
380+
assert!(res.is_err());
361381
}
362382

363383
#[test]

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8593,7 +8593,7 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
85938593
// We can get the channel type at this point already as we'll need it immediately in both the
85948594
// manual and the automatic acceptance cases.
85958595
let channel_type = channel::channel_type_from_open_channel(
8596-
common_fields, &peer_state.latest_features, &self.channel_type_features()
8596+
common_fields, &self.channel_type_features()
85978597
).map_err(|e| MsgHandleErrInternal::from_chan_no_close(e, common_fields.temporary_channel_id))?;
85988598

85998599
// If we're doing manual acceptance checks on the channel, then defer creation until we're sure we want to accept.
@@ -13694,7 +13694,7 @@ pub fn provided_init_features(config: &UserConfig) -> InitFeatures {
1369413694
features.set_basic_mpp_optional();
1369513695
features.set_wumbo_optional();
1369613696
features.set_shutdown_any_segwit_optional();
13697-
features.set_channel_type_optional();
13697+
features.set_channel_type_required();
1369813698
features.set_scid_privacy_optional();
1369913699
features.set_zero_conf_optional();
1370013700
features.set_route_blinding_optional();

0 commit comments

Comments
 (0)