Skip to content

Commit 5ea51d2

Browse files
committed
Introduce LegacyChannelConfig to remain backwards compatible
ChannelConfig now has its static fields removed with a new TLV structure defined. 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 3bbd4f2 commit 5ea51d2

File tree

2 files changed

+116
-34
lines changed

2 files changed

+116
-34
lines changed

lightning/src/ln/channel.rs

+45-22
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, ChannelConfig, ChannelHandshakeConfig, ChannelHandshakeLimits, LegacyChannelConfig};
4343
use util::scid_utils::scid_from_parts;
4444

4545
use io;
@@ -490,6 +490,10 @@ pub(crate) const MIN_AFFORDABLE_HTLC_COUNT: usize = 4;
490490
// Holder designates channel data owned for the benefice of the user client.
491491
// Counterparty designates channel data owned by the another channel participant entity.
492492
pub(super) struct Channel<Signer: Sign> {
493+
// This only here for backwards-compatibility in serialization, in the future it can be removed,
494+
// breaking clients running 0.0.107 and earlier.
495+
_legacy_config: LegacyChannelConfig,
496+
493497
#[cfg(any(test, feature = "_test_utils"))]
494498
pub(crate) config: ChannelConfig,
495499
#[cfg(not(any(test, feature = "_test_utils")))]
@@ -929,17 +933,24 @@ impl<Signer: Sign> Channel<Signer> {
929933
}
930934
}
931935

932-
// Also update `ChannelConfig::announced_channel` to make sure we still allow users to
933-
// downgrade versions after the fact.
936+
// Also update `LegacyChannelConfig` to make sure we still allow users to downgrade versions
937+
// after the fact.
934938
//
935-
// TODO: This can be removed once we prevent downgrades to v0.0.107.
936-
let mut channel_options = config.channel_options.clone();
937-
channel_options.announced_channel = config.own_channel_config.announced_channel;
939+
// TODO: This can be removed once we break compatibility with versions 0.0.107 and earlier.
940+
let legacy_config = LegacyChannelConfig {
941+
config: config.channel_options.clone(),
942+
announced_channel: config.own_channel_config.announced_channel,
943+
commit_upfront_shutdown_pubkey: config.own_channel_config.commit_upfront_shutdown_pubkey,
944+
};
938945

939946
Ok(Channel {
940947
user_id,
941-
config: channel_options,
948+
949+
_legacy_config: legacy_config,
950+
config: config.channel_options.clone(),
951+
942952
announced: config.own_channel_config.announced_channel,
953+
943954
inbound_handshake_limits_override: Some(config.peer_channel_config_limits.clone()),
944955

945956
channel_id: keys_provider.get_secure_random_bytes(),
@@ -1195,12 +1206,15 @@ impl<Signer: Sign> Channel<Signer> {
11951206
return Err(ChannelError::Close("Peer tried to open channel but their announcement preference is different from ours".to_owned()));
11961207
}
11971208
}
1198-
// Also update `ChannelConfig::announced_channel` to make sure we still allow users to
1199-
// downgrade versions after the fact.
1209+
// Also update `LegacyChannelConfig` to make sure we still allow users to downgrade versions
1210+
// after the fact.
12001211
//
1201-
// TODO: This can be removed once we prevent downgrades to v0.0.107.
1202-
let mut channel_options = config.channel_options.clone();
1203-
channel_options.announced_channel = announced_channel;
1212+
// TODO: This can be removed once we break compatibility with versions 0.0.107 and earlier.
1213+
let legacy_config = LegacyChannelConfig {
1214+
config: config.channel_options.clone(),
1215+
announced_channel,
1216+
commit_upfront_shutdown_pubkey: config.own_channel_config.commit_upfront_shutdown_pubkey,
1217+
};
12041218

12051219
let holder_selected_channel_reserve_satoshis = Channel::<Signer>::get_holder_selected_channel_reserve_satoshis(msg.funding_satoshis);
12061220
if holder_selected_channel_reserve_satoshis < MIN_CHAN_DUST_LIMIT_SATOSHIS {
@@ -1267,8 +1281,12 @@ impl<Signer: Sign> Channel<Signer> {
12671281

12681282
let chan = Channel {
12691283
user_id,
1270-
config: channel_options,
1284+
1285+
_legacy_config: legacy_config,
1286+
config: config.channel_options.clone(),
1287+
12711288
announced: announced_channel,
1289+
12721290
inbound_handshake_limits_override: None,
12731291

12741292
channel_id: msg.temporary_channel_id,
@@ -6011,7 +6029,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
60116029
(2, chan_type, option),
60126030
(3, self.counterparty_selected_channel_reserve_satoshis, option),
60136031
(4, serialized_holder_selected_reserve, option),
6014-
(5, self.config, required),
6032+
(5, self._legacy_config, required),
60156033
(6, serialized_holder_htlc_max_in_flight, option),
60166034
(7, self.shutdown_scriptpubkey, option),
60176035
(9, self.target_closing_feerate_sats_per_kw, option),
@@ -6022,6 +6040,7 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
60226040
(19, self.latest_inbound_scid_alias, option),
60236041
(21, self.outbound_scid_alias, required),
60246042
(23, self.announced, required),
6043+
(25, self.config, required),
60256044
});
60266045

60276046
Ok(())
@@ -6037,13 +6056,13 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
60376056

60386057
let user_id = Readable::read(reader)?;
60396058

6040-
let mut config = Some(ChannelConfig::default());
6059+
let mut legacy_config = Some(LegacyChannelConfig::default());
60416060
if ver == 1 {
60426061
// Read the old serialization of the ChannelConfig from version 0.0.98.
6043-
config.as_mut().unwrap().forwarding_fee_proportional_millionths = Readable::read(reader)?;
6044-
config.as_mut().unwrap().cltv_expiry_delta = Readable::read(reader)?;
6045-
config.as_mut().unwrap().announced_channel = Readable::read(reader)?;
6046-
config.as_mut().unwrap().commit_upfront_shutdown_pubkey = Readable::read(reader)?;
6062+
legacy_config.as_mut().unwrap().config.forwarding_fee_proportional_millionths = Readable::read(reader)?;
6063+
legacy_config.as_mut().unwrap().config.cltv_expiry_delta = Readable::read(reader)?;
6064+
legacy_config.as_mut().unwrap().announced_channel = Readable::read(reader)?;
6065+
legacy_config.as_mut().unwrap().commit_upfront_shutdown_pubkey = Readable::read(reader)?;
60476066
} else {
60486067
// Read the 8 bytes of backwards-compatibility ChannelConfig data.
60496068
let mut _val: u64 = Readable::read(reader)?;
@@ -6280,14 +6299,15 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
62806299
let mut latest_inbound_scid_alias = None;
62816300
let mut outbound_scid_alias = None;
62826301
let mut announced = None;
6302+
let mut config = None;
62836303

62846304
read_tlv_fields!(reader, {
62856305
(0, announcement_sigs, option),
62866306
(1, minimum_depth, option),
62876307
(2, channel_type, option),
62886308
(3, counterparty_selected_channel_reserve_satoshis, option),
62896309
(4, holder_selected_channel_reserve_satoshis, option),
6290-
(5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
6310+
(5, legacy_config, option), // Note that if none is provided we will *not* overwrite the existing one.
62916311
(6, holder_max_htlc_value_in_flight_msat, option),
62926312
(7, shutdown_scriptpubkey, option),
62936313
(9, target_closing_feerate_sats_per_kw, option),
@@ -6298,6 +6318,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
62986318
(19, latest_inbound_scid_alias, option),
62996319
(21, outbound_scid_alias, option),
63006320
(23, announced, option),
6321+
(25, config, option),
63016322
});
63026323

63036324
if let Some(preimages) = preimages_opt {
@@ -6337,8 +6358,10 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
63376358
Ok(Channel {
63386359
user_id,
63396360

6340-
config: config.unwrap(),
6341-
announced: announced.unwrap_or(config.unwrap().announced_channel),
6361+
_legacy_config: legacy_config.unwrap(),
6362+
config: config.unwrap_or(legacy_config.unwrap().config),
6363+
6364+
announced: announced.unwrap_or(legacy_config.unwrap().announced_channel),
63426365

63436366
// Note that we don't care about serializing handshake limits as we only ever serialize
63446367
// channel data after the handshake has completed.

lightning/src/util/config.rs

+71-12
Original file line numberDiff line numberDiff line change
@@ -291,10 +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-
/// Deprecated, use [`ChannelHandshakeConfig::announced_channel`] instead.
295-
pub(crate) announced_channel: bool,
296-
/// Deprecated, use [`ChannelHandshakeConfig::commit_upfront_shutdown_pubkey`] instead.
297-
pub(crate) commit_upfront_shutdown_pubkey: bool,
298294
/// Limit our total exposure to in-flight HTLCs which are burned to fees as they are too
299295
/// small to claim on-chain.
300296
///
@@ -343,8 +339,6 @@ impl Default for ChannelConfig {
343339
forwarding_fee_proportional_millionths: 0,
344340
forwarding_fee_base_msat: 1000,
345341
cltv_expiry_delta: 6 * 12, // 6 blocks/hour * 12 hours
346-
announced_channel: false,
347-
commit_upfront_shutdown_pubkey: true,
348342
max_dust_htlc_exposure_msat: 5_000_000,
349343
force_close_avoidance_max_fee_satoshis: 1000,
350344
}
@@ -353,14 +347,79 @@ impl Default for ChannelConfig {
353347

354348
impl_writeable_tlv_based!(ChannelConfig, {
355349
(0, forwarding_fee_proportional_millionths, required),
356-
(1, max_dust_htlc_exposure_msat, (default_value, 5_000_000)),
357-
(2, cltv_expiry_delta, required),
358-
(3, force_close_avoidance_max_fee_satoshis, (default_value, 1000)),
359-
(4, announced_channel, required),
360-
(6, commit_upfront_shutdown_pubkey, required),
361-
(8, forwarding_fee_base_msat, required),
350+
(2, forwarding_fee_base_msat, required),
351+
(4, cltv_expiry_delta, required),
352+
(6, max_dust_htlc_exposure_msat, required),
353+
(8, force_close_avoidance_max_fee_satoshis, required),
362354
});
363355

356+
/// Legacy version of [`ChannelConfig`] that stored the static [`announced_channel`] and
357+
/// [`commit_upfront_shutdown_pubkey`] fields.
358+
#[derive(Copy, Clone, Debug)]
359+
pub(crate) struct LegacyChannelConfig {
360+
pub(crate) config: ChannelConfig,
361+
pub(crate) announced_channel: bool,
362+
pub(crate) commit_upfront_shutdown_pubkey: bool,
363+
}
364+
365+
impl Default for LegacyChannelConfig {
366+
fn default() -> Self {
367+
Self {
368+
config: ChannelConfig::default(),
369+
announced_channel: false,
370+
commit_upfront_shutdown_pubkey: true,
371+
}
372+
}
373+
}
374+
375+
// ChannelConfig redefined its TLV fields, so LegacyChannelConfig should remain consistent with what
376+
// ChannelConfig was previously.
377+
impl ::util::ser::Writeable for LegacyChannelConfig {
378+
fn write<W: ::util::ser::Writer>(&self, writer: &mut W) -> Result<(), ::io::Error> {
379+
write_tlv_fields!(writer, {
380+
(0, self.config.forwarding_fee_proportional_millionths, required),
381+
(1, self.config.max_dust_htlc_exposure_msat, (default_value, 5_000_000)),
382+
(2, self.config.cltv_expiry_delta, required),
383+
(3, self.config.force_close_avoidance_max_fee_satoshis, (default_value, 1000)),
384+
(4, self.announced_channel, required),
385+
(6, self.commit_upfront_shutdown_pubkey, required),
386+
(8, self.config.forwarding_fee_base_msat, required),
387+
});
388+
Ok(())
389+
}
390+
}
391+
impl ::util::ser::Readable for LegacyChannelConfig {
392+
fn read<R: ::io::Read>(reader: &mut R) -> Result<Self, ::ln::msgs::DecodeError> {
393+
let mut forwarding_fee_proportional_millionths = 0;
394+
let mut max_dust_htlc_exposure_msat = 5_000_000;
395+
let mut cltv_expiry_delta = 0;
396+
let mut force_close_avoidance_max_fee_satoshis = 1000;
397+
let mut announced_channel = false;
398+
let mut commit_upfront_shutdown_pubkey = false;
399+
let mut forwarding_fee_base_msat = 0;
400+
read_tlv_fields!(reader, {
401+
(0, forwarding_fee_proportional_millionths, required),
402+
(1, max_dust_htlc_exposure_msat, (default_value, 5_000_000)),
403+
(2, cltv_expiry_delta, required),
404+
(3, force_close_avoidance_max_fee_satoshis, (default_value, 1000)),
405+
(4, announced_channel, required),
406+
(6, commit_upfront_shutdown_pubkey, required),
407+
(8, forwarding_fee_base_msat, required),
408+
});
409+
Ok(Self {
410+
config: ChannelConfig {
411+
forwarding_fee_proportional_millionths,
412+
max_dust_htlc_exposure_msat,
413+
cltv_expiry_delta,
414+
force_close_avoidance_max_fee_satoshis,
415+
forwarding_fee_base_msat,
416+
},
417+
announced_channel,
418+
commit_upfront_shutdown_pubkey,
419+
})
420+
}
421+
}
422+
364423
/// Top-level config which holds ChannelHandshakeLimits and ChannelConfig.
365424
///
366425
/// Default::default() provides sane defaults for most configurations

0 commit comments

Comments
 (0)