Skip to content

Commit f28e038

Browse files
committed
Define methods on ChannelTypeFeatures for anchors-related logic.
Specifically, introduce a new constructor for an anchors-supporting feature set, as well as methods that will maintain forwards-compatible deserialization in older versions.
1 parent 4e79eb5 commit f28e038

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

lightning/src/ln/features.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,62 @@ impl ChannelTypeFeatures {
581581
<sealed::ChannelTypeContext as sealed::StaticRemoteKey>::set_required_bit(&mut ret.flags);
582582
ret
583583
}
584+
585+
#[cfg(any(anchors, test))]
586+
/// Constructs a ChannelTypeFeatures with anchors support
587+
pub(crate) fn static_remote_key_with_anchors() -> Self {
588+
let mut ret = Self::empty();
589+
<sealed::ChannelTypeContext as sealed::StaticRemoteKey>::set_required_bit(&mut ret.flags);
590+
<sealed::ChannelTypeContext as sealed::AnchorsZeroFeeHtlcTx>::set_required_bit(&mut ret.flags);
591+
ret
592+
}
593+
594+
/// Simplified accessor for checking if anchors are supported
595+
pub fn supports_anchors(&self) -> bool {
596+
self.supports_anchors_zero_fee_htlc_tx()
597+
}
598+
599+
/// There are six legacy structs where we need [`ChannelTypeFeatures`] for keeping track of
600+
/// anchors and Taproot support. Those six structs are
601+
/// — [`ChannelTransactionParameters`]
602+
/// — [`CommitmentTransaction`]
603+
/// — [`CounterpartyOfferedHTLCOutput`]
604+
/// — [`CounterpartyReceivedHTLCOutput`]
605+
/// — [`HolderHTLCOutput`]
606+
/// — [`HolderFundingOutput`]
607+
/// Because pre-anchors versions of LDK do not expect additional fields on those structs,
608+
/// non-anchor channels must serialize non-anchor features as empty options, i. e. not
609+
/// serialize them at all.
610+
/// However, when instantiating those objects based on the full set of features determined
611+
/// at channel construction, any additional feature bits will be lost in legacy channels.
612+
/// That would typically trigger a pre- and post-serialization equality check failure, so to
613+
/// avoid that, we hereby introduce a method that creates a watered-down, deserialization-safe
614+
/// copy.
615+
/// Note: this issue only affects unit testing!
616+
///
617+
/// [`ChannelTransactionParameters`]: crate::ln::chan_utils::ChannelTransactionParameters
618+
/// [`CommitmentTransaction`]: crate::ln::chan_utils::CommitmentTransaction
619+
/// [`CounterpartyOfferedHTLCOutput`]: crate::chain::package::CounterpartyOfferedHTLCOutput
620+
/// [`CounterpartyReceivedHTLCOutput`]: crate::chain::package::CounterpartyReceivedHTLCOutput
621+
/// [`HolderHTLCOutput`]: crate::chain::package::HolderHTLCOutput
622+
/// [`HolderFundingOutput`]: crate::chain::package::HolderFundingOutput
623+
pub(crate) fn legacy_serialization_safe_version(&self) -> Self {
624+
if self.supports_anchors() {
625+
self.clone()
626+
} else {
627+
Self::only_static_remote_key()
628+
}
629+
}
630+
631+
/// To actually serialize the features in a manner that won't break the structs read by
632+
/// older versions of LDK, we should skip serialization entirely if anchors are not supported.
633+
/// This is a helper method to do just that.
634+
pub(crate) fn ldk_0_0_115_safe_serializable_option(&self) -> Option<Self> {
635+
if !self.supports_anchors() {
636+
return None;
637+
}
638+
Some(self.clone())
639+
}
584640
}
585641

586642
impl ToBase32 for InvoiceFeatures {

0 commit comments

Comments
 (0)