Skip to content

Commit ec7ccf0

Browse files
committed
Introduce LegacyChannelConfig to remain backwards compatible
ChannelConfig now has its static fields removed. We introduce a new LegacyChannelConfig struct that maintains the serialization as previously defined by ChannelConfig to remain backwards compatible with clients running 0.0.107 and earlier.
1 parent 8027c2f commit ec7ccf0

File tree

4 files changed

+101
-53
lines changed

4 files changed

+101
-53
lines changed

lightning/src/ln/channel.rs

+25-16
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use util::events::ClosureReason;
3939
use util::ser::{Readable, ReadableArgs, Writeable, Writer, VecWriter};
4040
use util::logger::Logger;
4141
use util::errors::APIError;
42-
use util::config::{UserConfig, ChannelConfig, ChannelHandshakeConfig, ChannelHandshakeLimits};
42+
use util::config::{UserConfig, LegacyChannelConfig, ChannelHandshakeConfig, ChannelHandshakeLimits};
4343
use util::scid_utils::scid_from_parts;
4444

4545
use io;
@@ -491,9 +491,9 @@ pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
491491
// Counterparty designates channel data owned by the another channel participant entity.
492492
pub(super) struct Channel<Signer: Sign> {
493493
#[cfg(any(test, feature = "_test_utils"))]
494-
pub(crate) config: ChannelConfig,
494+
pub(crate) config: LegacyChannelConfig,
495495
#[cfg(not(any(test, feature = "_test_utils")))]
496-
config: ChannelConfig,
496+
config: LegacyChannelConfig,
497497

498498
inbound_handshake_limits_override: Option<ChannelHandshakeLimits>,
499499

@@ -930,7 +930,13 @@ impl<Signer: Sign> Channel<Signer> {
930930

931931
Ok(Channel {
932932
user_id,
933-
config: config.channel_options.clone(),
933+
934+
config: LegacyChannelConfig {
935+
mutable: config.channel_options.clone(),
936+
announced_channel: config.own_channel_config.announced_channel,
937+
commit_upfront_shutdown_pubkey: config.own_channel_config.commit_upfront_shutdown_pubkey,
938+
},
939+
934940
inbound_handshake_limits_override: Some(config.peer_channel_config_limits.clone()),
935941

936942
channel_id: keys_provider.get_secure_random_bytes(),
@@ -1117,7 +1123,6 @@ impl<Signer: Sign> Channel<Signer> {
11171123
delayed_payment_basepoint: msg.delayed_payment_basepoint,
11181124
htlc_basepoint: msg.htlc_basepoint
11191125
};
1120-
let mut local_config = (*config).channel_options.clone();
11211126

11221127
if config.own_channel_config.our_to_self_delay < BREAKDOWN_TIMEOUT {
11231128
return Err(ChannelError::Close(format!("Configured with an unreasonable our_to_self_delay ({}) putting user funds at risks. It must be greater than {}", config.own_channel_config.our_to_self_delay, BREAKDOWN_TIMEOUT)));
@@ -1186,8 +1191,6 @@ impl<Signer: Sign> Channel<Signer> {
11861191
return Err(ChannelError::Close("Peer tried to open channel but their announcement preference is different from ours".to_owned()));
11871192
}
11881193
}
1189-
// we either accept their preference or the preferences match
1190-
local_config.announced_channel = announced_channel;
11911194

11921195
let holder_selected_channel_reserve_satoshis = Channel::<Signer>::get_holder_selected_channel_reserve_satoshis(msg.funding_satoshis);
11931196
if holder_selected_channel_reserve_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
@@ -1254,7 +1257,13 @@ impl<Signer: Sign> Channel<Signer> {
12541257

12551258
let chan = Channel {
12561259
user_id,
1257-
config: local_config,
1260+
1261+
config: LegacyChannelConfig {
1262+
mutable: config.channel_options.clone(),
1263+
announced_channel,
1264+
commit_upfront_shutdown_pubkey: config.own_channel_config.commit_upfront_shutdown_pubkey,
1265+
},
1266+
12581267
inbound_handshake_limits_override: None,
12591268

12601269
channel_id: msg.temporary_channel_id,
@@ -4011,7 +4020,7 @@ impl<Signer: Sign> Channel<Signer> {
40114020
// We always add force_close_avoidance_max_fee_satoshis to our normal
40124021
// feerate-calculated fee, but allow the max to be overridden if we're using a
40134022
// target feerate-calculated fee.
4014-
cmp::max(normal_feerate as u64 * tx_weight / 1000 + self.config.force_close_avoidance_max_fee_satoshis,
4023+
cmp::max(normal_feerate as u64 * tx_weight / 1000 + self.config.mutable.force_close_avoidance_max_fee_satoshis,
40154024
proposed_max_feerate as u64 * tx_weight / 1000)
40164025
} else {
40174026
self.channel_value_satoshis - (self.value_to_self_msat + 999) / 1000
@@ -4471,15 +4480,15 @@ impl<Signer: Sign> Channel<Signer> {
44714480
}
44724481

44734482
pub fn get_fee_proportional_millionths(&self) -> u32 {
4474-
self.config.forwarding_fee_proportional_millionths
4483+
self.config.mutable.forwarding_fee_proportional_millionths
44754484
}
44764485

44774486
pub fn get_cltv_expiry_delta(&self) -> u16 {
4478-
cmp::max(self.config.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
4487+
cmp::max(self.config.mutable.cltv_expiry_delta, MIN_CLTV_EXPIRY_DELTA)
44794488
}
44804489

44814490
pub fn get_max_dust_htlc_exposure_msat(&self) -> u64 {
4482-
self.config.max_dust_htlc_exposure_msat
4491+
self.config.mutable.max_dust_htlc_exposure_msat
44834492
}
44844493

44854494
pub fn get_feerate(&self) -> u32 {
@@ -4566,7 +4575,7 @@ impl<Signer: Sign> Channel<Signer> {
45664575
/// Gets the fee we'd want to charge for adding an HTLC output to this Channel
45674576
/// Allowed in any state (including after shutdown)
45684577
pub fn get_outbound_forwarding_fee_base_msat(&self) -> u32 {
4569-
self.config.forwarding_fee_base_msat
4578+
self.config.mutable.forwarding_fee_base_msat
45704579
}
45714580

45724581
/// Returns true if we've ever received a message from the remote end for this Channel
@@ -6022,11 +6031,11 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
60226031

60236032
let user_id = Readable::read(reader)?;
60246033

6025-
let mut config = Some(ChannelConfig::default());
6034+
let mut config = Some(LegacyChannelConfig::default());
60266035
if ver == 1 {
60276036
// Read the old serialization of the ChannelConfig from version 0.0.98.
6028-
config.as_mut().unwrap().forwarding_fee_proportional_millionths = Readable::read(reader)?;
6029-
config.as_mut().unwrap().cltv_expiry_delta = Readable::read(reader)?;
6037+
config.as_mut().unwrap().mutable.forwarding_fee_proportional_millionths = Readable::read(reader)?;
6038+
config.as_mut().unwrap().mutable.cltv_expiry_delta = Readable::read(reader)?;
60306039
config.as_mut().unwrap().announced_channel = Readable::read(reader)?;
60316040
config.as_mut().unwrap().commit_upfront_shutdown_pubkey = Readable::read(reader)?;
60326041
} else {

lightning/src/ln/functional_test_utils.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,9 @@ pub fn do_claim_payment_along_route<'a, 'b, 'c>(origin_node: &Node<'a, 'b, 'c>,
16791679
($node: expr, $prev_node: expr, $next_node: expr, $new_msgs: expr) => {
16801680
{
16811681
$node.node.handle_update_fulfill_htlc(&$prev_node.node.get_our_node_id(), &next_msgs.as_ref().unwrap().0);
1682-
let fee = $node.node.channel_state.lock().unwrap().by_id.get(&next_msgs.as_ref().unwrap().0.channel_id).unwrap().config.forwarding_fee_base_msat;
1682+
let fee = $node.node.channel_state.lock().unwrap()
1683+
.by_id.get(&next_msgs.as_ref().unwrap().0.channel_id).unwrap()
1684+
.config.mutable.forwarding_fee_base_msat;
16831685
expect_payment_forwarded!($node, $next_node, $prev_node, Some(fee as u64), false, false);
16841686
expected_total_fee_msat += fee as u64;
16851687
check_added_monitors!($node, 1);

lightning/src/ln/payment_tests.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,8 @@ fn do_retry_with_no_persist(confirm_before_reload: bool) {
531531
// Update the fee on the middle hop to ensure PaymentSent events have the correct (retried) fee
532532
// and not the original fee. We also update node[1]'s relevant config as
533533
// do_claim_payment_along_route expects us to never overpay.
534-
nodes[1].node.channel_state.lock().unwrap().by_id.get_mut(&chan_id_2).unwrap().config.forwarding_fee_base_msat += 100_000;
534+
nodes[1].node.channel_state.lock().unwrap().by_id.get_mut(&chan_id_2).unwrap()
535+
.config.mutable.forwarding_fee_base_msat += 100_000;
535536
new_route.paths[0][0].fee_msat += 100_000;
536537

537538
assert!(nodes[0].node.retry_payment(&new_route, payment_id_1).is_err()); // Shouldn't be allowed to retry a fulfilled payment

lightning/src/util/config.rs

+71-35
Original file line numberDiff line numberDiff line change
@@ -291,30 +291,6 @@ pub struct ChannelConfig {
291291
///
292292
/// [`MIN_CLTV_EXPIRY_DELTA`]: crate::ln::channelmanager::MIN_CLTV_EXPIRY_DELTA
293293
pub cltv_expiry_delta: u16,
294-
/// Set to announce the channel publicly and notify all nodes that they can route via this
295-
/// channel.
296-
///
297-
/// This should only be set to true for nodes which expect to be online reliably.
298-
///
299-
/// As the node which funds a channel picks this value this will only apply for new outbound
300-
/// channels unless [`ChannelHandshakeLimits::force_announced_channel_preference`] is set.
301-
///
302-
/// This cannot be changed after the initial channel handshake.
303-
///
304-
/// Default value: false.
305-
pub announced_channel: bool,
306-
/// When set, we commit to an upfront shutdown_pubkey at channel open. If our counterparty
307-
/// supports it, they will then enforce the mutual-close output to us matches what we provided
308-
/// at intialization, preventing us from closing to an alternate pubkey.
309-
///
310-
/// This is set to true by default to provide a slight increase in security, though ultimately
311-
/// any attacker who is able to take control of a channel can just as easily send the funds via
312-
/// lightning payments, so we never require that our counterparties support this option.
313-
///
314-
/// This cannot be changed after a channel has been initialized.
315-
///
316-
/// Default value: true.
317-
pub commit_upfront_shutdown_pubkey: bool,
318294
/// Limit our total exposure to in-flight HTLCs which are burned to fees as they are too
319295
/// small to claim on-chain.
320296
///
@@ -363,23 +339,83 @@ impl Default for ChannelConfig {
363339
forwarding_fee_proportional_millionths: 0,
364340
forwarding_fee_base_msat: 1000,
365341
cltv_expiry_delta: 6 * 12, // 6 blocks/hour * 12 hours
366-
announced_channel: false,
367-
commit_upfront_shutdown_pubkey: true,
368342
max_dust_htlc_exposure_msat: 5_000_000,
369343
force_close_avoidance_max_fee_satoshis: 1000,
370344
}
371345
}
372346
}
373347

374-
impl_writeable_tlv_based!(ChannelConfig, {
375-
(0, forwarding_fee_proportional_millionths, required),
376-
(1, max_dust_htlc_exposure_msat, (default_value, 5_000_000)),
377-
(2, cltv_expiry_delta, required),
378-
(3, force_close_avoidance_max_fee_satoshis, (default_value, 1000)),
379-
(4, announced_channel, required),
380-
(6, commit_upfront_shutdown_pubkey, required),
381-
(8, forwarding_fee_base_msat, required),
382-
});
348+
/// Legacy version of [`ChannelConfig`] that stored the static
349+
/// [`ChannelHandshakeConfig::announced_channel`] and
350+
/// [`ChannelHandshakeConfig::commit_upfront_shutdown_pubkey`] fields.
351+
#[derive(Copy, Clone, Debug)]
352+
pub(crate) struct LegacyChannelConfig {
353+
pub(crate) mutable: ChannelConfig,
354+
/// Deprecated but may still be read from. See [`ChannelHandshakeConfig::announced_channel`] to
355+
/// set this when opening/accepting a channel.
356+
pub(crate) announced_channel: bool,
357+
/// Deprecated but may still be read from. See
358+
/// [`ChannelHandshakeConfig::commit_upfront_shutdown_pubkey`] to set this when
359+
/// opening/accepting a channel.
360+
pub(crate) commit_upfront_shutdown_pubkey: bool,
361+
}
362+
363+
impl Default for LegacyChannelConfig {
364+
fn default() -> Self {
365+
Self {
366+
mutable: ChannelConfig::default(),
367+
announced_channel: false,
368+
commit_upfront_shutdown_pubkey: true,
369+
}
370+
}
371+
}
372+
373+
impl ::util::ser::Writeable for LegacyChannelConfig {
374+
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::io::Error> {
375+
write_tlv_fields!(writer, {
376+
(0, self.mutable.forwarding_fee_proportional_millionths, required),
377+
(1, self.mutable.max_dust_htlc_exposure_msat, (default_value, 5_000_000)),
378+
(2, self.mutable.cltv_expiry_delta, required),
379+
(3, self.mutable.force_close_avoidance_max_fee_satoshis, (default_value, 1000)),
380+
(4, self.announced_channel, required),
381+
(6, self.commit_upfront_shutdown_pubkey, required),
382+
(8, self.mutable.forwarding_fee_base_msat, required),
383+
});
384+
Ok(())
385+
}
386+
}
387+
388+
impl ::util::ser::Readable for LegacyChannelConfig {
389+
fn read<R: ::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
390+
let mut forwarding_fee_proportional_millionths = 0;
391+
let mut max_dust_htlc_exposure_msat = 5_000_000;
392+
let mut cltv_expiry_delta = 0;
393+
let mut force_close_avoidance_max_fee_satoshis = 1000;
394+
let mut announced_channel = false;
395+
let mut commit_upfront_shutdown_pubkey = false;
396+
let mut forwarding_fee_base_msat = 0;
397+
read_tlv_fields!(reader, {
398+
(0, forwarding_fee_proportional_millionths, required),
399+
(1, max_dust_htlc_exposure_msat, (default_value, 5_000_000)),
400+
(2, cltv_expiry_delta, required),
401+
(3, force_close_avoidance_max_fee_satoshis, (default_value, 1000)),
402+
(4, announced_channel, required),
403+
(6, commit_upfront_shutdown_pubkey, required),
404+
(8, forwarding_fee_base_msat, required),
405+
});
406+
Ok(Self {
407+
mutable: ChannelConfig {
408+
forwarding_fee_proportional_millionths,
409+
max_dust_htlc_exposure_msat,
410+
cltv_expiry_delta,
411+
force_close_avoidance_max_fee_satoshis,
412+
forwarding_fee_base_msat,
413+
},
414+
announced_channel,
415+
commit_upfront_shutdown_pubkey,
416+
})
417+
}
418+
}
383419

384420
/// Top-level config which holds ChannelHandshakeLimits and ChannelConfig.
385421
///

0 commit comments

Comments
 (0)