Skip to content

Commit e8bc408

Browse files
committed
Move serialization of channel_state fields to channel_state.rs
1f616c0 moved a handful of structs to the new `channel_state.rs` but forgot to move their serialization logic with them, which is corrected here.
1 parent 15c709d commit e8bc408

File tree

2 files changed

+145
-142
lines changed

2 files changed

+145
-142
lines changed

lightning/src/ln/channel_state.rs

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ use bitcoin::secp256k1::PublicKey;
1515

1616
use crate::chain::chaininterface::{FeeEstimator, LowerBoundedFeeEstimator};
1717
use crate::chain::transaction::OutPoint;
18+
use crate::io;
1819
use crate::ln::channel::ChannelContext;
1920
use crate::ln::features::{ChannelTypeFeatures, InitFeatures};
21+
use crate::ln::msgs::DecodeError;
2022
use crate::ln::types::{ChannelId, PaymentHash};
2123
use crate::sign::SignerProvider;
2224
use crate::util::config::ChannelConfig;
25+
use crate::util::ser::{Writeable, Writer, Readable};
2326

2427
use core::ops::Deref;
2528

@@ -224,6 +227,12 @@ pub struct CounterpartyForwardingInfo {
224227
pub cltv_expiry_delta: u16,
225228
}
226229

230+
impl_writeable_tlv_based!(CounterpartyForwardingInfo, {
231+
(2, fee_base_msat, required),
232+
(4, fee_proportional_millionths, required),
233+
(6, cltv_expiry_delta, required),
234+
});
235+
227236
/// Channel parameters which apply to our counterparty. These are split out from [`ChannelDetails`]
228237
/// to better separate parameters.
229238
#[derive(Clone, Debug, PartialEq)]
@@ -253,6 +262,15 @@ pub struct ChannelCounterparty {
253262
pub outbound_htlc_maximum_msat: Option<u64>,
254263
}
255264

265+
impl_writeable_tlv_based!(ChannelCounterparty, {
266+
(2, node_id, required),
267+
(4, features, required),
268+
(6, unspendable_punishment_reserve, required),
269+
(8, forwarding_info, option),
270+
(9, outbound_htlc_minimum_msat, option),
271+
(11, outbound_htlc_maximum_msat, option),
272+
});
273+
256274
/// Details of a channel, as returned by [`ChannelManager::list_channels`] and [`ChannelManager::list_usable_channels`]
257275
///
258276
/// [`ChannelManager::list_channels`]: crate::ln::channelmanager::ChannelManager::list_channels
@@ -537,6 +555,125 @@ impl ChannelDetails {
537555
}
538556
}
539557

558+
impl Writeable for ChannelDetails {
559+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
560+
// `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
561+
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
562+
let user_channel_id_low = self.user_channel_id as u64;
563+
let user_channel_id_high_opt = Some((self.user_channel_id >> 64) as u64);
564+
write_tlv_fields!(writer, {
565+
(1, self.inbound_scid_alias, option),
566+
(2, self.channel_id, required),
567+
(3, self.channel_type, option),
568+
(4, self.counterparty, required),
569+
(5, self.outbound_scid_alias, option),
570+
(6, self.funding_txo, option),
571+
(7, self.config, option),
572+
(8, self.short_channel_id, option),
573+
(9, self.confirmations, option),
574+
(10, self.channel_value_satoshis, required),
575+
(12, self.unspendable_punishment_reserve, option),
576+
(14, user_channel_id_low, required),
577+
(16, self.balance_msat, required),
578+
(18, self.outbound_capacity_msat, required),
579+
(19, self.next_outbound_htlc_limit_msat, required),
580+
(20, self.inbound_capacity_msat, required),
581+
(21, self.next_outbound_htlc_minimum_msat, required),
582+
(22, self.confirmations_required, option),
583+
(24, self.force_close_spend_delay, option),
584+
(26, self.is_outbound, required),
585+
(28, self.is_channel_ready, required),
586+
(30, self.is_usable, required),
587+
(32, self.is_public, required),
588+
(33, self.inbound_htlc_minimum_msat, option),
589+
(35, self.inbound_htlc_maximum_msat, option),
590+
(37, user_channel_id_high_opt, option),
591+
(39, self.feerate_sat_per_1000_weight, option),
592+
(41, self.channel_shutdown_state, option),
593+
(43, self.pending_inbound_htlcs, optional_vec),
594+
(45, self.pending_outbound_htlcs, optional_vec),
595+
});
596+
Ok(())
597+
}
598+
}
599+
600+
impl Readable for ChannelDetails {
601+
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
602+
_init_and_read_len_prefixed_tlv_fields!(reader, {
603+
(1, inbound_scid_alias, option),
604+
(2, channel_id, required),
605+
(3, channel_type, option),
606+
(4, counterparty, required),
607+
(5, outbound_scid_alias, option),
608+
(6, funding_txo, option),
609+
(7, config, option),
610+
(8, short_channel_id, option),
611+
(9, confirmations, option),
612+
(10, channel_value_satoshis, required),
613+
(12, unspendable_punishment_reserve, option),
614+
(14, user_channel_id_low, required),
615+
(16, balance_msat, required),
616+
(18, outbound_capacity_msat, required),
617+
// Note that by the time we get past the required read above, outbound_capacity_msat will be
618+
// filled in, so we can safely unwrap it here.
619+
(19, next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat.0.unwrap() as u64)),
620+
(20, inbound_capacity_msat, required),
621+
(21, next_outbound_htlc_minimum_msat, (default_value, 0)),
622+
(22, confirmations_required, option),
623+
(24, force_close_spend_delay, option),
624+
(26, is_outbound, required),
625+
(28, is_channel_ready, required),
626+
(30, is_usable, required),
627+
(32, is_public, required),
628+
(33, inbound_htlc_minimum_msat, option),
629+
(35, inbound_htlc_maximum_msat, option),
630+
(37, user_channel_id_high_opt, option),
631+
(39, feerate_sat_per_1000_weight, option),
632+
(41, channel_shutdown_state, option),
633+
(43, pending_inbound_htlcs, optional_vec),
634+
(45, pending_outbound_htlcs, optional_vec),
635+
});
636+
637+
// `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
638+
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
639+
let user_channel_id_low: u64 = user_channel_id_low.0.unwrap();
640+
let user_channel_id = user_channel_id_low as u128 +
641+
((user_channel_id_high_opt.unwrap_or(0 as u64) as u128) << 64);
642+
643+
Ok(Self {
644+
inbound_scid_alias,
645+
channel_id: channel_id.0.unwrap(),
646+
channel_type,
647+
counterparty: counterparty.0.unwrap(),
648+
outbound_scid_alias,
649+
funding_txo,
650+
config,
651+
short_channel_id,
652+
channel_value_satoshis: channel_value_satoshis.0.unwrap(),
653+
unspendable_punishment_reserve,
654+
user_channel_id,
655+
balance_msat: balance_msat.0.unwrap(),
656+
outbound_capacity_msat: outbound_capacity_msat.0.unwrap(),
657+
next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
658+
next_outbound_htlc_minimum_msat: next_outbound_htlc_minimum_msat.0.unwrap(),
659+
inbound_capacity_msat: inbound_capacity_msat.0.unwrap(),
660+
confirmations_required,
661+
confirmations,
662+
force_close_spend_delay,
663+
is_outbound: is_outbound.0.unwrap(),
664+
is_channel_ready: is_channel_ready.0.unwrap(),
665+
is_usable: is_usable.0.unwrap(),
666+
is_public: is_public.0.unwrap(),
667+
inbound_htlc_minimum_msat,
668+
inbound_htlc_maximum_msat,
669+
feerate_sat_per_1000_weight,
670+
channel_shutdown_state,
671+
pending_inbound_htlcs: pending_inbound_htlcs.unwrap_or(Vec::new()),
672+
pending_outbound_htlcs: pending_outbound_htlcs.unwrap_or(Vec::new()),
673+
})
674+
}
675+
}
676+
540677
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
541678
/// Further information on the details of the channel shutdown.
542679
/// Upon channels being forced closed (i.e. commitment transaction confirmation detected
@@ -558,3 +695,11 @@ pub enum ChannelShutdownState {
558695
/// to drop the channel.
559696
ShutdownComplete,
560697
}
698+
699+
impl_writeable_tlv_based_enum!(ChannelShutdownState,
700+
(0, NotShuttingDown) => {},
701+
(2, ShutdownInitiated) => {},
702+
(4, ResolvingHTLCs) => {},
703+
(6, NegotiatingClosingFee) => {},
704+
(8, ShutdownComplete) => {}, ;
705+
);

lightning/src/ln/channelmanager.rs

Lines changed: 0 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -10274,140 +10274,6 @@ pub fn provided_init_features(config: &UserConfig) -> InitFeatures {
1027410274
const SERIALIZATION_VERSION: u8 = 1;
1027510275
const MIN_SERIALIZATION_VERSION: u8 = 1;
1027610276

10277-
impl_writeable_tlv_based!(CounterpartyForwardingInfo, {
10278-
(2, fee_base_msat, required),
10279-
(4, fee_proportional_millionths, required),
10280-
(6, cltv_expiry_delta, required),
10281-
});
10282-
10283-
impl_writeable_tlv_based!(ChannelCounterparty, {
10284-
(2, node_id, required),
10285-
(4, features, required),
10286-
(6, unspendable_punishment_reserve, required),
10287-
(8, forwarding_info, option),
10288-
(9, outbound_htlc_minimum_msat, option),
10289-
(11, outbound_htlc_maximum_msat, option),
10290-
});
10291-
10292-
impl Writeable for ChannelDetails {
10293-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
10294-
// `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
10295-
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
10296-
let user_channel_id_low = self.user_channel_id as u64;
10297-
let user_channel_id_high_opt = Some((self.user_channel_id >> 64) as u64);
10298-
write_tlv_fields!(writer, {
10299-
(1, self.inbound_scid_alias, option),
10300-
(2, self.channel_id, required),
10301-
(3, self.channel_type, option),
10302-
(4, self.counterparty, required),
10303-
(5, self.outbound_scid_alias, option),
10304-
(6, self.funding_txo, option),
10305-
(7, self.config, option),
10306-
(8, self.short_channel_id, option),
10307-
(9, self.confirmations, option),
10308-
(10, self.channel_value_satoshis, required),
10309-
(12, self.unspendable_punishment_reserve, option),
10310-
(14, user_channel_id_low, required),
10311-
(16, self.balance_msat, required),
10312-
(18, self.outbound_capacity_msat, required),
10313-
(19, self.next_outbound_htlc_limit_msat, required),
10314-
(20, self.inbound_capacity_msat, required),
10315-
(21, self.next_outbound_htlc_minimum_msat, required),
10316-
(22, self.confirmations_required, option),
10317-
(24, self.force_close_spend_delay, option),
10318-
(26, self.is_outbound, required),
10319-
(28, self.is_channel_ready, required),
10320-
(30, self.is_usable, required),
10321-
(32, self.is_public, required),
10322-
(33, self.inbound_htlc_minimum_msat, option),
10323-
(35, self.inbound_htlc_maximum_msat, option),
10324-
(37, user_channel_id_high_opt, option),
10325-
(39, self.feerate_sat_per_1000_weight, option),
10326-
(41, self.channel_shutdown_state, option),
10327-
(43, self.pending_inbound_htlcs, optional_vec),
10328-
(45, self.pending_outbound_htlcs, optional_vec),
10329-
});
10330-
Ok(())
10331-
}
10332-
}
10333-
10334-
impl Readable for ChannelDetails {
10335-
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
10336-
_init_and_read_len_prefixed_tlv_fields!(reader, {
10337-
(1, inbound_scid_alias, option),
10338-
(2, channel_id, required),
10339-
(3, channel_type, option),
10340-
(4, counterparty, required),
10341-
(5, outbound_scid_alias, option),
10342-
(6, funding_txo, option),
10343-
(7, config, option),
10344-
(8, short_channel_id, option),
10345-
(9, confirmations, option),
10346-
(10, channel_value_satoshis, required),
10347-
(12, unspendable_punishment_reserve, option),
10348-
(14, user_channel_id_low, required),
10349-
(16, balance_msat, required),
10350-
(18, outbound_capacity_msat, required),
10351-
// Note that by the time we get past the required read above, outbound_capacity_msat will be
10352-
// filled in, so we can safely unwrap it here.
10353-
(19, next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat.0.unwrap() as u64)),
10354-
(20, inbound_capacity_msat, required),
10355-
(21, next_outbound_htlc_minimum_msat, (default_value, 0)),
10356-
(22, confirmations_required, option),
10357-
(24, force_close_spend_delay, option),
10358-
(26, is_outbound, required),
10359-
(28, is_channel_ready, required),
10360-
(30, is_usable, required),
10361-
(32, is_public, required),
10362-
(33, inbound_htlc_minimum_msat, option),
10363-
(35, inbound_htlc_maximum_msat, option),
10364-
(37, user_channel_id_high_opt, option),
10365-
(39, feerate_sat_per_1000_weight, option),
10366-
(41, channel_shutdown_state, option),
10367-
(43, pending_inbound_htlcs, optional_vec),
10368-
(45, pending_outbound_htlcs, optional_vec),
10369-
});
10370-
10371-
// `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
10372-
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
10373-
let user_channel_id_low: u64 = user_channel_id_low.0.unwrap();
10374-
let user_channel_id = user_channel_id_low as u128 +
10375-
((user_channel_id_high_opt.unwrap_or(0 as u64) as u128) << 64);
10376-
10377-
Ok(Self {
10378-
inbound_scid_alias,
10379-
channel_id: channel_id.0.unwrap(),
10380-
channel_type,
10381-
counterparty: counterparty.0.unwrap(),
10382-
outbound_scid_alias,
10383-
funding_txo,
10384-
config,
10385-
short_channel_id,
10386-
channel_value_satoshis: channel_value_satoshis.0.unwrap(),
10387-
unspendable_punishment_reserve,
10388-
user_channel_id,
10389-
balance_msat: balance_msat.0.unwrap(),
10390-
outbound_capacity_msat: outbound_capacity_msat.0.unwrap(),
10391-
next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
10392-
next_outbound_htlc_minimum_msat: next_outbound_htlc_minimum_msat.0.unwrap(),
10393-
inbound_capacity_msat: inbound_capacity_msat.0.unwrap(),
10394-
confirmations_required,
10395-
confirmations,
10396-
force_close_spend_delay,
10397-
is_outbound: is_outbound.0.unwrap(),
10398-
is_channel_ready: is_channel_ready.0.unwrap(),
10399-
is_usable: is_usable.0.unwrap(),
10400-
is_public: is_public.0.unwrap(),
10401-
inbound_htlc_minimum_msat,
10402-
inbound_htlc_maximum_msat,
10403-
feerate_sat_per_1000_weight,
10404-
channel_shutdown_state,
10405-
pending_inbound_htlcs: pending_inbound_htlcs.unwrap_or(Vec::new()),
10406-
pending_outbound_htlcs: pending_outbound_htlcs.unwrap_or(Vec::new()),
10407-
})
10408-
}
10409-
}
10410-
1041110277
impl_writeable_tlv_based!(PhantomRouteHints, {
1041210278
(2, channels, required_vec),
1041310279
(4, phantom_scid, required),
@@ -11042,14 +10908,6 @@ impl Readable for VecDeque<(Event, Option<EventCompletionAction>)> {
1104210908
}
1104310909
}
1104410910

11045-
impl_writeable_tlv_based_enum!(ChannelShutdownState,
11046-
(0, NotShuttingDown) => {},
11047-
(2, ShutdownInitiated) => {},
11048-
(4, ResolvingHTLCs) => {},
11049-
(6, NegotiatingClosingFee) => {},
11050-
(8, ShutdownComplete) => {}, ;
11051-
);
11052-
1105310911
/// Arguments for the creation of a ChannelManager that are not deserialized.
1105410912
///
1105510913
/// At a high-level, the process for deserializing a ChannelManager and resuming normal operation

0 commit comments

Comments
 (0)