Skip to content

Commit f1366d8

Browse files
committed
Add commit_upfront_shutdown_pubkey to ChannelHandshakeConfig
1 parent 89cbb6d commit f1366d8

File tree

4 files changed

+51
-28
lines changed

4 files changed

+51
-28
lines changed

lightning/src/ln/chanmon_update_fail_tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2461,7 +2461,7 @@ fn test_temporary_error_during_shutdown() {
24612461
// Test that temporary failures when updating the monitor's shutdown script delay cooperative
24622462
// close.
24632463
let mut config = test_default_channel_config();
2464-
config.channel_options.commit_upfront_shutdown_pubkey = false;
2464+
config.own_channel_config.commit_upfront_shutdown_pubkey = false;
24652465

24662466
let chanmon_cfgs = create_chanmon_cfgs(2);
24672467
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
@@ -2516,7 +2516,7 @@ fn test_permanent_error_during_sending_shutdown() {
25162516
// Test that permanent failures when updating the monitor's shutdown script result in a force
25172517
// close when initiating a cooperative close.
25182518
let mut config = test_default_channel_config();
2519-
config.channel_options.commit_upfront_shutdown_pubkey = false;
2519+
config.own_channel_config.commit_upfront_shutdown_pubkey = false;
25202520

25212521
let chanmon_cfgs = create_chanmon_cfgs(2);
25222522
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
@@ -2537,7 +2537,7 @@ fn test_permanent_error_during_handling_shutdown() {
25372537
// Test that permanent failures when updating the monitor's shutdown script result in a force
25382538
// close when handling a cooperative close.
25392539
let mut config = test_default_channel_config();
2540-
config.channel_options.commit_upfront_shutdown_pubkey = false;
2540+
config.own_channel_config.commit_upfront_shutdown_pubkey = false;
25412541

25422542
let chanmon_cfgs = create_chanmon_cfgs(2);
25432543
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);

lightning/src/ln/channel.rs

+26-6
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ pub(super) struct Channel<Signer: Sign> {
425425
pub(crate) config: ChannelConfig,
426426
#[cfg(not(any(test, feature = "_test_utils")))]
427427
config: ChannelConfig,
428+
commit_upfront_shutdown_pubkey: bool,
428429

429430
user_id: u64,
430431

@@ -751,7 +752,7 @@ impl<Signer: Sign> Channel<Signer> {
751752
let mut secp_ctx = Secp256k1::new();
752753
secp_ctx.seeded_randomize(&keys_provider.get_secure_random_bytes());
753754

754-
let shutdown_scriptpubkey = if config.channel_options.commit_upfront_shutdown_pubkey {
755+
let shutdown_scriptpubkey = if config.own_channel_config.commit_upfront_shutdown_pubkey {
755756
Some(keys_provider.get_shutdown_scriptpubkey())
756757
} else { None };
757758

@@ -764,6 +765,7 @@ impl<Signer: Sign> Channel<Signer> {
764765
Ok(Channel {
765766
user_id,
766767
config: config.channel_options.clone(),
768+
commit_upfront_shutdown_pubkey: config.own_channel_config.commit_upfront_shutdown_pubkey.clone(),
767769

768770
channel_id: keys_provider.get_secure_random_bytes(),
769771
channel_state: ChannelState::OurInitSent as u32,
@@ -1046,7 +1048,7 @@ impl<Signer: Sign> Channel<Signer> {
10461048
}
10471049
} else { None };
10481050

1049-
let shutdown_scriptpubkey = if config.channel_options.commit_upfront_shutdown_pubkey {
1051+
let shutdown_scriptpubkey = if config.own_channel_config.commit_upfront_shutdown_pubkey {
10501052
Some(keys_provider.get_shutdown_scriptpubkey())
10511053
} else { None };
10521054

@@ -1062,7 +1064,7 @@ impl<Signer: Sign> Channel<Signer> {
10621064
let chan = Channel {
10631065
user_id,
10641066
config: local_config,
1065-
1067+
commit_upfront_shutdown_pubkey: config.own_channel_config.commit_upfront_shutdown_pubkey,
10661068
channel_id: msg.temporary_channel_id,
10671069
channel_state: (ChannelState::OurInitSent as u32) | (ChannelState::TheirInitSent as u32),
10681070
secp_ctx,
@@ -5191,7 +5193,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
51915193
self.config.forwarding_fee_proportional_millionths.write(writer)?;
51925194
self.config.cltv_expiry_delta.write(writer)?;
51935195
self.config.announced_channel.write(writer)?;
5194-
self.config.commit_upfront_shutdown_pubkey.write(writer)?;
5196+
self.commit_upfront_shutdown_pubkey.write(writer)?;
51955197

51965198
self.channel_id.write(writer)?;
51975199
(self.channel_state | ChannelState::PeerDisconnected as u32).write(writer)?;
@@ -5434,6 +5436,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
54345436
(9, self.target_closing_feerate_sats_per_kw, option),
54355437
(11, self.monitor_pending_finalized_fulfills, vec_type),
54365438
(13, self.channel_creation_height, required),
5439+
(15, self.commit_upfront_shutdown_pubkey, required),
54375440
});
54385441

54395442
Ok(())
@@ -5675,6 +5678,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
56755678
// only, so we default to that if none was written.
56765679
let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
56775680
let mut channel_creation_height = Some(serialized_height);
5681+
let mut commit_upfront_shutdown_pubkey = None;
56785682
read_tlv_fields!(reader, {
56795683
(0, announcement_sigs, option),
56805684
(1, minimum_depth, option),
@@ -5687,6 +5691,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
56875691
(9, target_closing_feerate_sats_per_kw, option),
56885692
(11, monitor_pending_finalized_fulfills, vec_type),
56895693
(13, channel_creation_height, option),
5694+
(15, commit_upfront_shutdown_pubkey, option),
56905695
});
56915696

56925697
let chan_features = channel_type.as_ref().unwrap();
@@ -5701,13 +5706,28 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
57015706
return Err(DecodeError::InvalidValue);
57025707
}
57035708

5709+
if commit_upfront_shutdown_pubkey.is_none() {
5710+
// commit_upfront_shutdown_pubkey has moved around a good bit, in version 1
5711+
// serialization, it was written out as a part of the explicit field list of the
5712+
// `ChannelConfig`. Then, it was written out as a field in the `ChannelConfig` itself.
5713+
// Now, it is written out explicitly as its own TLV (as the field has moved to
5714+
// `ChannelHandshakeConfig`).
5715+
// Thus, if its not in a TLV, we here pull it from the `ChannelConfig`, and if we can't
5716+
// find it at all, fail.
5717+
let legacy_commit_upfront_shutdown_pubkey = config.as_ref().unwrap().commit_upfront_shutdown_pubkey;
5718+
if let Some(val) = legacy_commit_upfront_shutdown_pubkey {
5719+
commit_upfront_shutdown_pubkey = Some(val);
5720+
} else {
5721+
return Err(DecodeError::InvalidValue);
5722+
}
5723+
}
5724+
57045725
let mut secp_ctx = Secp256k1::new();
57055726
secp_ctx.seeded_randomize(&keys_source.get_secure_random_bytes());
57065727

57075728
Ok(Channel {
57085729
user_id,
5709-
5710-
config: config.unwrap(),
5730+
commit_upfront_shutdown_pubkey: commit_upfront_shutdown_pubkey.unwrap(), config: config.unwrap(),
57115731
channel_id,
57125732
channel_state,
57135733
secp_ctx,

lightning/src/ln/shutdown_tests.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ fn test_upfront_shutdown_script() {
404404
let mut config = UserConfig::default();
405405
config.channel_options.announced_channel = true;
406406
config.peer_channel_config_limits.force_announced_channel_preference = false;
407-
config.channel_options.commit_upfront_shutdown_pubkey = false;
407+
config.own_channel_config.commit_upfront_shutdown_pubkey = false;
408408
let user_cfgs = [None, Some(config), None];
409409
let chanmon_cfgs = create_chanmon_cfgs(3);
410410
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
@@ -569,7 +569,7 @@ fn test_segwit_v0_shutdown_script() {
569569
let mut config = UserConfig::default();
570570
config.channel_options.announced_channel = true;
571571
config.peer_channel_config_limits.force_announced_channel_preference = false;
572-
config.channel_options.commit_upfront_shutdown_pubkey = false;
572+
config.own_channel_config.commit_upfront_shutdown_pubkey = false;
573573
let user_cfgs = [None, Some(config), None];
574574
let chanmon_cfgs = create_chanmon_cfgs(3);
575575
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
@@ -604,7 +604,7 @@ fn test_anysegwit_shutdown_script() {
604604
let mut config = UserConfig::default();
605605
config.channel_options.announced_channel = true;
606606
config.peer_channel_config_limits.force_announced_channel_preference = false;
607-
config.channel_options.commit_upfront_shutdown_pubkey = false;
607+
config.own_channel_config.commit_upfront_shutdown_pubkey = false;
608608
let user_cfgs = [None, Some(config), None];
609609
let chanmon_cfgs = create_chanmon_cfgs(3);
610610
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
@@ -639,7 +639,7 @@ fn test_unsupported_anysegwit_shutdown_script() {
639639
let mut config = UserConfig::default();
640640
config.channel_options.announced_channel = true;
641641
config.peer_channel_config_limits.force_announced_channel_preference = false;
642-
config.channel_options.commit_upfront_shutdown_pubkey = false;
642+
config.own_channel_config.commit_upfront_shutdown_pubkey = false;
643643
let user_cfgs = [None, Some(config), None];
644644
let chanmon_cfgs = create_chanmon_cfgs(3);
645645
let mut node_cfgs = create_node_cfgs(3, &chanmon_cfgs);
@@ -681,7 +681,7 @@ fn test_invalid_shutdown_script() {
681681
let mut config = UserConfig::default();
682682
config.channel_options.announced_channel = true;
683683
config.peer_channel_config_limits.force_announced_channel_preference = false;
684-
config.channel_options.commit_upfront_shutdown_pubkey = false;
684+
config.own_channel_config.commit_upfront_shutdown_pubkey = false;
685685
let user_cfgs = [None, Some(config), None];
686686
let chanmon_cfgs = create_chanmon_cfgs(3);
687687
let node_cfgs = create_node_cfgs(3, &chanmon_cfgs);

lightning/src/util/config.rs

+17-14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ pub struct ChannelHandshakeConfig {
4747
/// Default value: 1. If the value is less than 1, it is ignored and set to 1, as is required
4848
/// by the protocol.
4949
pub our_htlc_minimum_msat: u64,
50+
/// When set, we commit to an upfront shutdown_pubkey at channel open. If our counterparty
51+
/// supports it, they will then enforce the mutual-close output to us matches what we provided
52+
/// at intialization, preventing us from closing to an alternate pubkey.
53+
///
54+
/// This is set to true by default to provide a slight increase in security, though ultimately
55+
/// any attacker who is able to take control of a channel can just as easily send the funds via
56+
/// lightning payments, so we never require that our counterparties support this option.
57+
///
58+
/// This cannot be changed after a channel has been initialized.
59+
///
60+
/// Default value: true.
61+
pub commit_upfront_shutdown_pubkey: bool
5062
}
5163

5264
impl Default for ChannelHandshakeConfig {
@@ -55,6 +67,7 @@ impl Default for ChannelHandshakeConfig {
5567
minimum_depth: 6,
5668
our_to_self_delay: BREAKDOWN_TIMEOUT,
5769
our_htlc_minimum_msat: 1,
70+
commit_upfront_shutdown_pubkey: true
5871
}
5972
}
6073
}
@@ -195,18 +208,8 @@ pub struct ChannelConfig {
195208
///
196209
/// Default value: false.
197210
pub announced_channel: bool,
198-
/// When set, we commit to an upfront shutdown_pubkey at channel open. If our counterparty
199-
/// supports it, they will then enforce the mutual-close output to us matches what we provided
200-
/// at intialization, preventing us from closing to an alternate pubkey.
201-
///
202-
/// This is set to true by default to provide a slight increase in security, though ultimately
203-
/// any attacker who is able to take control of a channel can just as easily send the funds via
204-
/// lightning payments, so we never require that our counterparties support this option.
205-
///
206-
/// This cannot be changed after a channel has been initialized.
207-
///
208-
/// Default value: true.
209-
pub commit_upfront_shutdown_pubkey: bool,
211+
/// This value is moved to ChannelHandshakeConfig, optional here for old serialiization
212+
pub(crate) commit_upfront_shutdown_pubkey: Option<bool>,
210213
/// Limit our total exposure to in-flight HTLCs which are burned to fees as they are too
211214
/// small to claim on-chain.
212215
///
@@ -256,7 +259,7 @@ impl Default for ChannelConfig {
256259
forwarding_fee_base_msat: 1000,
257260
cltv_expiry_delta: 6 * 12, // 6 blocks/hour * 12 hours
258261
announced_channel: false,
259-
commit_upfront_shutdown_pubkey: true,
262+
commit_upfront_shutdown_pubkey: None,
260263
max_dust_htlc_exposure_msat: 5_000_000,
261264
force_close_avoidance_max_fee_satoshis: 1000,
262265
}
@@ -269,7 +272,7 @@ impl_writeable_tlv_based!(ChannelConfig, {
269272
(2, cltv_expiry_delta, required),
270273
(3, force_close_avoidance_max_fee_satoshis, (default_value, 1000)),
271274
(4, announced_channel, required),
272-
(6, commit_upfront_shutdown_pubkey, required),
275+
(6, commit_upfront_shutdown_pubkey, option),
273276
(8, forwarding_fee_base_msat, required),
274277
});
275278

0 commit comments

Comments
 (0)