Skip to content

Commit 36c6586

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 1e06fc8 commit 36c6586

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
@@ -10217,140 +10217,6 @@ pub fn provided_init_features(config: &UserConfig) -> InitFeatures {
1021710217
const SERIALIZATION_VERSION: u8 = 1;
1021810218
const MIN_SERIALIZATION_VERSION: u8 = 1;
1021910219

10220-
impl_writeable_tlv_based!(CounterpartyForwardingInfo, {
10221-
(2, fee_base_msat, required),
10222-
(4, fee_proportional_millionths, required),
10223-
(6, cltv_expiry_delta, required),
10224-
});
10225-
10226-
impl_writeable_tlv_based!(ChannelCounterparty, {
10227-
(2, node_id, required),
10228-
(4, features, required),
10229-
(6, unspendable_punishment_reserve, required),
10230-
(8, forwarding_info, option),
10231-
(9, outbound_htlc_minimum_msat, option),
10232-
(11, outbound_htlc_maximum_msat, option),
10233-
});
10234-
10235-
impl Writeable for ChannelDetails {
10236-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
10237-
// `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
10238-
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
10239-
let user_channel_id_low = self.user_channel_id as u64;
10240-
let user_channel_id_high_opt = Some((self.user_channel_id >> 64) as u64);
10241-
write_tlv_fields!(writer, {
10242-
(1, self.inbound_scid_alias, option),
10243-
(2, self.channel_id, required),
10244-
(3, self.channel_type, option),
10245-
(4, self.counterparty, required),
10246-
(5, self.outbound_scid_alias, option),
10247-
(6, self.funding_txo, option),
10248-
(7, self.config, option),
10249-
(8, self.short_channel_id, option),
10250-
(9, self.confirmations, option),
10251-
(10, self.channel_value_satoshis, required),
10252-
(12, self.unspendable_punishment_reserve, option),
10253-
(14, user_channel_id_low, required),
10254-
(16, self.balance_msat, required),
10255-
(18, self.outbound_capacity_msat, required),
10256-
(19, self.next_outbound_htlc_limit_msat, required),
10257-
(20, self.inbound_capacity_msat, required),
10258-
(21, self.next_outbound_htlc_minimum_msat, required),
10259-
(22, self.confirmations_required, option),
10260-
(24, self.force_close_spend_delay, option),
10261-
(26, self.is_outbound, required),
10262-
(28, self.is_channel_ready, required),
10263-
(30, self.is_usable, required),
10264-
(32, self.is_public, required),
10265-
(33, self.inbound_htlc_minimum_msat, option),
10266-
(35, self.inbound_htlc_maximum_msat, option),
10267-
(37, user_channel_id_high_opt, option),
10268-
(39, self.feerate_sat_per_1000_weight, option),
10269-
(41, self.channel_shutdown_state, option),
10270-
(43, self.pending_inbound_htlcs, optional_vec),
10271-
(45, self.pending_outbound_htlcs, optional_vec),
10272-
});
10273-
Ok(())
10274-
}
10275-
}
10276-
10277-
impl Readable for ChannelDetails {
10278-
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
10279-
_init_and_read_len_prefixed_tlv_fields!(reader, {
10280-
(1, inbound_scid_alias, option),
10281-
(2, channel_id, required),
10282-
(3, channel_type, option),
10283-
(4, counterparty, required),
10284-
(5, outbound_scid_alias, option),
10285-
(6, funding_txo, option),
10286-
(7, config, option),
10287-
(8, short_channel_id, option),
10288-
(9, confirmations, option),
10289-
(10, channel_value_satoshis, required),
10290-
(12, unspendable_punishment_reserve, option),
10291-
(14, user_channel_id_low, required),
10292-
(16, balance_msat, required),
10293-
(18, outbound_capacity_msat, required),
10294-
// Note that by the time we get past the required read above, outbound_capacity_msat will be
10295-
// filled in, so we can safely unwrap it here.
10296-
(19, next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat.0.unwrap() as u64)),
10297-
(20, inbound_capacity_msat, required),
10298-
(21, next_outbound_htlc_minimum_msat, (default_value, 0)),
10299-
(22, confirmations_required, option),
10300-
(24, force_close_spend_delay, option),
10301-
(26, is_outbound, required),
10302-
(28, is_channel_ready, required),
10303-
(30, is_usable, required),
10304-
(32, is_public, required),
10305-
(33, inbound_htlc_minimum_msat, option),
10306-
(35, inbound_htlc_maximum_msat, option),
10307-
(37, user_channel_id_high_opt, option),
10308-
(39, feerate_sat_per_1000_weight, option),
10309-
(41, channel_shutdown_state, option),
10310-
(43, pending_inbound_htlcs, optional_vec),
10311-
(45, pending_outbound_htlcs, optional_vec),
10312-
});
10313-
10314-
// `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
10315-
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
10316-
let user_channel_id_low: u64 = user_channel_id_low.0.unwrap();
10317-
let user_channel_id = user_channel_id_low as u128 +
10318-
((user_channel_id_high_opt.unwrap_or(0 as u64) as u128) << 64);
10319-
10320-
Ok(Self {
10321-
inbound_scid_alias,
10322-
channel_id: channel_id.0.unwrap(),
10323-
channel_type,
10324-
counterparty: counterparty.0.unwrap(),
10325-
outbound_scid_alias,
10326-
funding_txo,
10327-
config,
10328-
short_channel_id,
10329-
channel_value_satoshis: channel_value_satoshis.0.unwrap(),
10330-
unspendable_punishment_reserve,
10331-
user_channel_id,
10332-
balance_msat: balance_msat.0.unwrap(),
10333-
outbound_capacity_msat: outbound_capacity_msat.0.unwrap(),
10334-
next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
10335-
next_outbound_htlc_minimum_msat: next_outbound_htlc_minimum_msat.0.unwrap(),
10336-
inbound_capacity_msat: inbound_capacity_msat.0.unwrap(),
10337-
confirmations_required,
10338-
confirmations,
10339-
force_close_spend_delay,
10340-
is_outbound: is_outbound.0.unwrap(),
10341-
is_channel_ready: is_channel_ready.0.unwrap(),
10342-
is_usable: is_usable.0.unwrap(),
10343-
is_public: is_public.0.unwrap(),
10344-
inbound_htlc_minimum_msat,
10345-
inbound_htlc_maximum_msat,
10346-
feerate_sat_per_1000_weight,
10347-
channel_shutdown_state,
10348-
pending_inbound_htlcs: pending_inbound_htlcs.unwrap_or(Vec::new()),
10349-
pending_outbound_htlcs: pending_outbound_htlcs.unwrap_or(Vec::new()),
10350-
})
10351-
}
10352-
}
10353-
1035410220
impl_writeable_tlv_based!(PhantomRouteHints, {
1035510221
(2, channels, required_vec),
1035610222
(4, phantom_scid, required),
@@ -10985,14 +10851,6 @@ impl Readable for VecDeque<(Event, Option<EventCompletionAction>)> {
1098510851
}
1098610852
}
1098710853

10988-
impl_writeable_tlv_based_enum!(ChannelShutdownState,
10989-
(0, NotShuttingDown) => {},
10990-
(2, ShutdownInitiated) => {},
10991-
(4, ResolvingHTLCs) => {},
10992-
(6, NegotiatingClosingFee) => {},
10993-
(8, ShutdownComplete) => {}, ;
10994-
);
10995-
1099610854
/// Arguments for the creation of a ChannelManager that are not deserialized.
1099710855
///
1099810856
/// At a high-level, the process for deserializing a ChannelManager and resuming normal operation

0 commit comments

Comments
 (0)