Skip to content

Commit 09a06a8

Browse files
committed
Use the new legacy en/decoding to de/ser ChannelDetails
Allowing us to use `impl_writeable_tlv_based!` again rather than manually writing the de/ser logic.
1 parent 7f9c9a4 commit 09a06a8

File tree

2 files changed

+45
-125
lines changed

2 files changed

+45
-125
lines changed

lightning/src/ln/channel_state.rs

+39-125
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@ use bitcoin::secp256k1::PublicKey;
1515

1616
use crate::chain::chaininterface::{FeeEstimator, LowerBoundedFeeEstimator};
1717
use crate::chain::transaction::OutPoint;
18-
use crate::io;
1918
use crate::ln::channel::ChannelContext;
20-
use crate::ln::msgs::DecodeError;
2119
use crate::ln::types::ChannelId;
2220
use crate::sign::SignerProvider;
2321
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
2422
use crate::types::payment::PaymentHash;
2523
use crate::util::config::ChannelConfig;
26-
use crate::util::ser::{Readable, Writeable, Writer};
2724

2825
use core::ops::Deref;
2926

@@ -549,128 +546,45 @@ impl ChannelDetails {
549546
}
550547
}
551548

552-
impl Writeable for ChannelDetails {
553-
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
554-
// `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
555-
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
556-
let user_channel_id_low = self.user_channel_id as u64;
557-
let user_channel_id_high_opt = Some((self.user_channel_id >> 64) as u64);
558-
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.
559-
{
560-
write_tlv_fields!(writer, {
561-
(1, self.inbound_scid_alias, option),
562-
(2, self.channel_id, required),
563-
(3, self.channel_type, option),
564-
(4, self.counterparty, required),
565-
(5, self.outbound_scid_alias, option),
566-
(6, self.funding_txo, option),
567-
(7, self.config, option),
568-
(8, self.short_channel_id, option),
569-
(9, self.confirmations, option),
570-
(10, self.channel_value_satoshis, required),
571-
(12, self.unspendable_punishment_reserve, option),
572-
(14, user_channel_id_low, required),
573-
(16, self.next_outbound_htlc_limit_msat, required), // Forwards compatibility for removed balance_msat field.
574-
(18, self.outbound_capacity_msat, required),
575-
(19, self.next_outbound_htlc_limit_msat, required),
576-
(20, self.inbound_capacity_msat, required),
577-
(21, self.next_outbound_htlc_minimum_msat, required),
578-
(22, self.confirmations_required, option),
579-
(24, self.force_close_spend_delay, option),
580-
(26, self.is_outbound, required),
581-
(28, self.is_channel_ready, required),
582-
(30, self.is_usable, required),
583-
(32, self.is_announced, required),
584-
(33, self.inbound_htlc_minimum_msat, option),
585-
(35, self.inbound_htlc_maximum_msat, option),
586-
(37, user_channel_id_high_opt, option),
587-
(39, self.feerate_sat_per_1000_weight, option),
588-
(41, self.channel_shutdown_state, option),
589-
(43, self.pending_inbound_htlcs, optional_vec),
590-
(45, self.pending_outbound_htlcs, optional_vec),
591-
});
592-
}
593-
Ok(())
594-
}
595-
}
596-
597-
impl Readable for ChannelDetails {
598-
fn read<R: io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
599-
_init_and_read_len_prefixed_tlv_fields!(reader, {
600-
(1, inbound_scid_alias, option),
601-
(2, channel_id, required),
602-
(3, channel_type, option),
603-
(4, counterparty, required),
604-
(5, outbound_scid_alias, option),
605-
(6, funding_txo, option),
606-
(7, config, option),
607-
(8, short_channel_id, option),
608-
(9, confirmations, option),
609-
(10, channel_value_satoshis, required),
610-
(12, unspendable_punishment_reserve, option),
611-
(14, user_channel_id_low, required),
612-
(16, _balance_msat, option), // Backwards compatibility for removed balance_msat field.
613-
(18, outbound_capacity_msat, required),
614-
// Note that by the time we get past the required read above, outbound_capacity_msat will be
615-
// filled in, so we can safely unwrap it here.
616-
(19, next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat.0.unwrap() as u64)),
617-
(20, inbound_capacity_msat, required),
618-
(21, next_outbound_htlc_minimum_msat, (default_value, 0)),
619-
(22, confirmations_required, option),
620-
(24, force_close_spend_delay, option),
621-
(26, is_outbound, required),
622-
(28, is_channel_ready, required),
623-
(30, is_usable, required),
624-
(32, is_announced, required),
625-
(33, inbound_htlc_minimum_msat, option),
626-
(35, inbound_htlc_maximum_msat, option),
627-
(37, user_channel_id_high_opt, option),
628-
(39, feerate_sat_per_1000_weight, option),
629-
(41, channel_shutdown_state, option),
630-
(43, pending_inbound_htlcs, optional_vec),
631-
(45, pending_outbound_htlcs, optional_vec),
632-
});
633-
634-
// `user_channel_id` used to be a single u64 value. In order to remain backwards compatible with
635-
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
636-
let user_channel_id_low: u64 = user_channel_id_low.0.unwrap();
637-
let user_channel_id = user_channel_id_low as u128
638-
+ ((user_channel_id_high_opt.unwrap_or(0 as u64) as u128) << 64);
639-
640-
let _balance_msat: Option<u64> = _balance_msat;
641-
642-
Ok(Self {
643-
inbound_scid_alias,
644-
channel_id: channel_id.0.unwrap(),
645-
channel_type,
646-
counterparty: counterparty.0.unwrap(),
647-
outbound_scid_alias,
648-
funding_txo,
649-
config,
650-
short_channel_id,
651-
channel_value_satoshis: channel_value_satoshis.0.unwrap(),
652-
unspendable_punishment_reserve,
653-
user_channel_id,
654-
outbound_capacity_msat: outbound_capacity_msat.0.unwrap(),
655-
next_outbound_htlc_limit_msat: next_outbound_htlc_limit_msat.0.unwrap(),
656-
next_outbound_htlc_minimum_msat: next_outbound_htlc_minimum_msat.0.unwrap(),
657-
inbound_capacity_msat: inbound_capacity_msat.0.unwrap(),
658-
confirmations_required,
659-
confirmations,
660-
force_close_spend_delay,
661-
is_outbound: is_outbound.0.unwrap(),
662-
is_channel_ready: is_channel_ready.0.unwrap(),
663-
is_usable: is_usable.0.unwrap(),
664-
is_announced: is_announced.0.unwrap(),
665-
inbound_htlc_minimum_msat,
666-
inbound_htlc_maximum_msat,
667-
feerate_sat_per_1000_weight,
668-
channel_shutdown_state,
669-
pending_inbound_htlcs: pending_inbound_htlcs.unwrap_or(Vec::new()),
670-
pending_outbound_htlcs: pending_outbound_htlcs.unwrap_or(Vec::new()),
671-
})
672-
}
673-
}
549+
impl_writeable_tlv_based!(ChannelDetails, {
550+
(1, inbound_scid_alias, option),
551+
(2, channel_id, required),
552+
(3, channel_type, option),
553+
(4, counterparty, required),
554+
(5, outbound_scid_alias, option),
555+
(6, funding_txo, option),
556+
(7, config, option),
557+
(8, short_channel_id, option),
558+
(9, confirmations, option),
559+
(10, channel_value_satoshis, required),
560+
(12, unspendable_punishment_reserve, option),
561+
// Note that _user_channel_id_low is used below, but rustc warns anyway
562+
(14, _user_channel_id_low, (legacy, u64,
563+
|us: &ChannelDetails| Some(us.user_channel_id as u64))),
564+
(16, _balance_msat, (legacy, u64, |us: &ChannelDetails| Some(us.next_outbound_htlc_limit_msat))),
565+
(18, outbound_capacity_msat, required),
566+
(19, next_outbound_htlc_limit_msat, (default_value, outbound_capacity_msat)),
567+
(20, inbound_capacity_msat, required),
568+
(21, next_outbound_htlc_minimum_msat, (default_value, 0)),
569+
(22, confirmations_required, option),
570+
(24, force_close_spend_delay, option),
571+
(26, is_outbound, required),
572+
(28, is_channel_ready, required),
573+
(30, is_usable, required),
574+
(32, is_announced, required),
575+
(33, inbound_htlc_minimum_msat, option),
576+
(35, inbound_htlc_maximum_msat, option),
577+
// Note that _user_channel_id_high is used below, but rustc warns anyway
578+
(37, _user_channel_id_high, (legacy, u64,
579+
|us: &ChannelDetails| Some((us.user_channel_id >> 64) as u64))),
580+
(39, feerate_sat_per_1000_weight, option),
581+
(41, channel_shutdown_state, option),
582+
(43, pending_inbound_htlcs, optional_vec),
583+
(45, pending_outbound_htlcs, optional_vec),
584+
(_unused, user_channel_id, (static_value,
585+
_user_channel_id_low.unwrap_or(0) as u128 | ((_user_channel_id_high.unwrap_or(0) as u128) << 64)
586+
)),
587+
});
674588

675589
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
676590
/// Further information on the details of the channel shutdown.

lightning/src/util/ser.rs

+6
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,12 @@ impl<T> From<T> for RequiredWrapper<T> {
413413
RequiredWrapper(Some(t))
414414
}
415415
}
416+
impl<T: Clone> Clone for RequiredWrapper<T> {
417+
fn clone(&self) -> Self {
418+
Self(self.0.clone())
419+
}
420+
}
421+
impl<T: Copy> Copy for RequiredWrapper<T> {}
416422

417423
/// Wrapper to read a required (non-optional) TLV record that may have been upgraded without
418424
/// backwards compat.

0 commit comments

Comments
 (0)