From b42ebd892b161e846cd37e0bb3639cca9b6c08b8 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 16 Feb 2022 04:21:29 +0000 Subject: [PATCH] Expose chan type in `Event::OpenChannelRequest` & `ChannelDetails` As we add new supported channel types, inbound channels which use new features may cause backwards-compatibility issues for clients. If a new channel is opened using new features while a client still wishes to ensure support for downgrading to a previous version of LDK, that new channel may cause the `ChannelManager` to fail deserialization due to unsupported feature flags. By exposing the channel type flags to the user in channel requests, users wishing to support downgrading to previous versions of LDK can reject channels which use channel features which previous versions of LDK do not understand. --- fuzz/src/router.rs | 1 + lightning/src/ln/channel.rs | 5 +++++ lightning/src/ln/channelmanager.rs | 11 ++++++++++- lightning/src/routing/router.rs | 2 ++ lightning/src/util/events.rs | 5 +++++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/fuzz/src/router.rs b/fuzz/src/router.rs index 5494b26c..bff177b1 100644 --- a/fuzz/src/router.rs +++ b/fuzz/src/router.rs @@ -215,6 +215,7 @@ pub fn do_test(data: &[u8], out: Out) { forwarding_info: None, }, funding_txo: Some(OutPoint { txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0 }), + channel_type: None, short_channel_id: Some(scid), inbound_scid_alias: None, channel_value_satoshis: slice_to_be64(get_slice!(8)), diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 71e0ea12..e1f292b7 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -4232,6 +4232,11 @@ impl Channel { self.user_id } + /// Gets the channel's type + pub fn get_channel_type(&self) -> &ChannelTypeFeatures { + &self.channel_type + } + /// Guaranteed to be Some after both FundingLocked messages have been exchanged (and, thus, /// is_usable() returns true). /// Allowed in any state (including after shutdown) diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index f3913c51..87f89419 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -42,7 +42,7 @@ use chain::transaction::{OutPoint, TransactionData}; // construct one themselves. use ln::{PaymentHash, PaymentPreimage, PaymentSecret}; use ln::channel::{Channel, ChannelError, ChannelUpdateStatus, UpdateFulfillCommitFetch}; -use ln::features::{InitFeatures, NodeFeatures}; +use ln::features::{ChannelTypeFeatures, InitFeatures, NodeFeatures}; use routing::router::{PaymentParameters, Route, RouteHop, RoutePath, RouteParameters}; use ln::msgs; use ln::msgs::NetAddress; @@ -1200,6 +1200,10 @@ pub struct ChannelDetails { /// Note that, if this has been set, `channel_id` will be equivalent to /// `funding_txo.unwrap().to_channel_id()`. pub funding_txo: Option, + /// The features which this channel operates with. See individual features for more info. + /// + /// `None` until negotiation completes and the channel type is finalized. + pub channel_type: Option, /// The position of the funding transaction in the chain. None if the funding transaction has /// not yet been confirmed and the channel fully opened. /// @@ -1922,6 +1926,9 @@ impl ChannelMana forwarding_info: channel.counterparty_forwarding_info(), }, funding_txo: channel.get_funding_txo(), + // Note that accept_channel (or open_channel) is always the first message, so + // `have_received_message` indicates that type negotiation has completed. + channel_type: if channel.have_received_message() { Some(channel.get_channel_type().clone()) } else { None }, short_channel_id: channel.get_short_channel_id(), inbound_scid_alias: channel.latest_inbound_scid_alias(), channel_value_satoshis: channel.get_value_satoshis(), @@ -4315,6 +4322,7 @@ impl ChannelMana counterparty_node_id: counterparty_node_id.clone(), funding_satoshis: msg.funding_satoshis, push_msat: msg.push_msat, + channel_type: channel.get_channel_type().clone(), } ); } @@ -6054,6 +6062,7 @@ impl_writeable_tlv_based!(ChannelCounterparty, { impl_writeable_tlv_based!(ChannelDetails, { (1, inbound_scid_alias, option), (2, channel_id, required), + (3, channel_type, option), (4, counterparty, required), (6, funding_txo, option), (8, short_channel_id, option), diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 62cbda9a..34d4cd32 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -1667,6 +1667,7 @@ mod tests { forwarding_info: None, }, funding_txo: Some(OutPoint { txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0 }), + channel_type: None, short_channel_id, inbound_scid_alias: None, channel_value_satoshis: 0, @@ -5363,6 +5364,7 @@ mod benches { funding_txo: Some(OutPoint { txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0 }), + channel_type: None, short_channel_id: Some(1), inbound_scid_alias: None, channel_value_satoshis: 10_000_000, diff --git a/lightning/src/util/events.rs b/lightning/src/util/events.rs index 3b90015c..ef2fc0fd 100644 --- a/lightning/src/util/events.rs +++ b/lightning/src/util/events.rs @@ -17,6 +17,7 @@ use chain::keysinterface::SpendableOutputDescriptor; use ln::channelmanager::PaymentId; use ln::channel::FUNDING_CONF_DEADLINE_BLOCKS; +use ln::features::ChannelTypeFeatures; use ln::msgs; use ln::msgs::DecodeError; use ln::{PaymentPreimage, PaymentHash, PaymentSecret}; @@ -429,6 +430,10 @@ pub enum Event { funding_satoshis: u64, /// Our starting balance in the channel if the request is accepted, in milli-satoshi. push_msat: u64, + /// The features that this channel will operate with. If you reject the channel, a + /// well-behaved counterparty may automatically re-attempt the channel with a new set of + /// feature flags. + channel_type: ChannelTypeFeatures, }, } -- 2.30.2