@@ -275,6 +275,12 @@ pub(super) struct ChannelHolder<ChanSigner: ChannelKeys> {
275275 pub ( super ) pending_msg_events : Vec < events:: MessageSendEvent > ,
276276}
277277
278+ /// State we hold per-peer. In the future we should put channels in here, but for now we only hold
279+ /// the latest Init features we heard from the peer.
280+ struct PeerState {
281+ latest_features : InitFeatures ,
282+ }
283+
278284#[ cfg( not( any( target_pointer_width = "32" , target_pointer_width = "64" ) ) ) ]
279285const ERR : ( ) = "You need at least 32 bit pointers (well, usize, but we'll assume they're the same) for ChannelManager::latest_block_height" ;
280286
@@ -328,6 +334,14 @@ pub struct ChannelManager<ChanSigner: ChannelKeys> {
328334 channel_state : Mutex < ChannelHolder < ChanSigner > > ,
329335 our_network_key : SecretKey ,
330336
337+ /// The bulk of our storage will eventually be here (channels and message queues and the like).
338+ /// If we are connected to a peer we always at least have an entry here, even if no channels
339+ /// are currently open with that peer.
340+ /// Because adding or removing an entry is rare, we usually take an outer read lock and then
341+ /// operate on the inner value freely. Sadly, this prevents parallel operation when opening a
342+ /// new channel.
343+ per_peer_state : RwLock < HashMap < PublicKey , Mutex < PeerState > > > ,
344+
331345 pending_events : Mutex < Vec < events:: Event > > ,
332346 /// Used when we have to take a BIG lock to make sure everything is self-consistent.
333347 /// Essentially just when we're serializing ourselves out.
@@ -390,6 +404,10 @@ pub struct ChannelDetails {
390404 pub short_channel_id : Option < u64 > ,
391405 /// The node_id of our counterparty
392406 pub remote_network_id : PublicKey ,
407+ /// The Features the channel counterparty provided upon last connection.
408+ /// Useful for routing as it is the most up-to-date copy of the counterparty's features and
409+ /// many routing-relevant features are present in the init context.
410+ pub counterparty_features : InitFeatures ,
393411 /// The value, in satoshis, of this channel as appears in the funding output
394412 pub channel_value_satoshis : u64 ,
395413 /// The user_id passed in to create_channel, or 0 if the channel was inbound.
@@ -610,6 +628,8 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
610628 } ) ,
611629 our_network_key : keys_manager. get_node_secret ( ) ,
612630
631+ per_peer_state : RwLock :: new ( HashMap :: new ( ) ) ,
632+
613633 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
614634 total_consistency_lock : RwLock :: new ( ( ) ) ,
615635
@@ -660,56 +680,53 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
660680 Ok ( ( ) )
661681 }
662682
663- /// Gets the list of open channels, in random order. See ChannelDetail field documentation for
664- /// more information.
665- pub fn list_channels ( & self ) -> Vec < ChannelDetails > {
666- let channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
667- let mut res = Vec :: with_capacity ( channel_state. by_id . len ( ) ) ;
668- for ( channel_id, channel) in channel_state. by_id . iter ( ) {
669- let ( inbound_capacity_msat, outbound_capacity_msat) = channel. get_inbound_outbound_available_balance_msat ( ) ;
670- res. push ( ChannelDetails {
671- channel_id : ( * channel_id) . clone ( ) ,
672- short_channel_id : channel. get_short_channel_id ( ) ,
673- remote_network_id : channel. get_their_node_id ( ) ,
674- channel_value_satoshis : channel. get_value_satoshis ( ) ,
675- inbound_capacity_msat,
676- outbound_capacity_msat,
677- user_id : channel. get_user_id ( ) ,
678- is_live : channel. is_live ( ) ,
679- } ) ;
680- }
681- res
682- }
683-
684- /// Gets the list of usable channels, in random order. Useful as an argument to
685- /// Router::get_route to ensure non-announced channels are used.
686- ///
687- /// These are guaranteed to have their is_live value set to true, see the documentation for
688- /// ChannelDetails::is_live for more info on exactly what the criteria are.
689- pub fn list_usable_channels ( & self ) -> Vec < ChannelDetails > {
690- let channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
691- let mut res = Vec :: with_capacity ( channel_state. by_id . len ( ) ) ;
692- for ( channel_id, channel) in channel_state. by_id . iter ( ) {
693- // Note we use is_live here instead of usable which leads to somewhat confused
694- // internal/external nomenclature, but that's ok cause that's probably what the user
695- // really wanted anyway.
696- if channel. is_live ( ) {
683+ fn list_channels_with_filter < F : FnMut ( & ( & [ u8 ; 32 ] , & Channel < ChanSigner > ) ) -> bool > ( & self , f : F ) -> Vec < ChannelDetails > {
684+ let mut res = Vec :: new ( ) ;
685+ {
686+ let channel_state = self . channel_state . lock ( ) . unwrap ( ) ;
687+ res. reserve ( channel_state. by_id . len ( ) ) ;
688+ for ( channel_id, channel) in channel_state. by_id . iter ( ) . filter ( f) {
697689 let ( inbound_capacity_msat, outbound_capacity_msat) = channel. get_inbound_outbound_available_balance_msat ( ) ;
698690 res. push ( ChannelDetails {
699691 channel_id : ( * channel_id) . clone ( ) ,
700692 short_channel_id : channel. get_short_channel_id ( ) ,
701693 remote_network_id : channel. get_their_node_id ( ) ,
694+ counterparty_features : InitFeatures :: empty ( ) ,
702695 channel_value_satoshis : channel. get_value_satoshis ( ) ,
703696 inbound_capacity_msat,
704697 outbound_capacity_msat,
705698 user_id : channel. get_user_id ( ) ,
706- is_live : true ,
699+ is_live : channel . is_live ( ) ,
707700 } ) ;
708701 }
709702 }
703+ let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
704+ for chan in res. iter_mut ( ) {
705+ if let Some ( peer_state) = per_peer_state. get ( & chan. remote_network_id ) {
706+ chan. counterparty_features = peer_state. lock ( ) . unwrap ( ) . latest_features . clone ( ) ;
707+ }
708+ }
710709 res
711710 }
712711
712+ /// Gets the list of open channels, in random order. See ChannelDetail field documentation for
713+ /// more information.
714+ pub fn list_channels ( & self ) -> Vec < ChannelDetails > {
715+ self . list_channels_with_filter ( |_| true )
716+ }
717+
718+ /// Gets the list of usable channels, in random order. Useful as an argument to
719+ /// Router::get_route to ensure non-announced channels are used.
720+ ///
721+ /// These are guaranteed to have their is_live value set to true, see the documentation for
722+ /// ChannelDetails::is_live for more info on exactly what the criteria are.
723+ pub fn list_usable_channels ( & self ) -> Vec < ChannelDetails > {
724+ // Note we use is_live here instead of usable which leads to somewhat confused
725+ // internal/external nomenclature, but that's ok cause that's probably what the user
726+ // really wanted anyway.
727+ self . list_channels_with_filter ( |& ( _, ref channel) | channel. is_live ( ) )
728+ }
729+
713730 /// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
714731 /// will be accepted on the given channel, and after additional timeout/the closing of all
715732 /// pending HTLCs, the channel will be closed on chain.
@@ -2780,6 +2797,7 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
27802797 let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
27812798 let mut failed_channels = Vec :: new ( ) ;
27822799 let mut failed_payments = Vec :: new ( ) ;
2800+ let mut no_channels_remain = true ;
27832801 {
27842802 let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
27852803 let channel_state = & mut * channel_state_lock;
@@ -2818,6 +2836,8 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
28182836 short_to_id. remove ( & short_id) ;
28192837 }
28202838 return false ;
2839+ } else {
2840+ no_channels_remain = false ;
28212841 }
28222842 }
28232843 true
@@ -2843,6 +2863,10 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
28432863 }
28442864 } ) ;
28452865 }
2866+ if no_channels_remain {
2867+ self . per_peer_state . write ( ) . unwrap ( ) . remove ( their_node_id) ;
2868+ }
2869+
28462870 for failure in failed_channels. drain ( ..) {
28472871 self . finish_force_close_channel ( failure) ;
28482872 }
@@ -2853,10 +2877,25 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
28532877 }
28542878 }
28552879
2856- fn peer_connected ( & self , their_node_id : & PublicKey ) {
2880+ fn peer_connected ( & self , their_node_id : & PublicKey , init_msg : & msgs :: Init ) {
28572881 log_debug ! ( self , "Generating channel_reestablish events for {}" , log_pubkey!( their_node_id) ) ;
28582882
28592883 let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2884+
2885+ {
2886+ let mut peer_state_lock = self . per_peer_state . write ( ) . unwrap ( ) ;
2887+ match peer_state_lock. entry ( their_node_id. clone ( ) ) {
2888+ hash_map:: Entry :: Vacant ( e) => {
2889+ e. insert ( Mutex :: new ( PeerState {
2890+ latest_features : init_msg. features . clone ( ) ,
2891+ } ) ) ;
2892+ } ,
2893+ hash_map:: Entry :: Occupied ( e) => {
2894+ e. get ( ) . lock ( ) . unwrap ( ) . latest_features = init_msg. features . clone ( ) ;
2895+ } ,
2896+ }
2897+ }
2898+
28602899 let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
28612900 let channel_state = & mut * channel_state_lock;
28622901 let pending_msg_events = & mut channel_state. pending_msg_events ;
@@ -3123,6 +3162,14 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for ChannelManager<ChanSigne
31233162 }
31243163 }
31253164
3165+ let per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
3166+ ( per_peer_state. len ( ) as u64 ) . write ( writer) ?;
3167+ for ( peer_pubkey, peer_state_mutex) in per_peer_state. iter ( ) {
3168+ peer_pubkey. write ( writer) ?;
3169+ let peer_state = peer_state_mutex. lock ( ) . unwrap ( ) ;
3170+ peer_state. latest_features . write ( writer) ?;
3171+ }
3172+
31263173 Ok ( ( ) )
31273174 }
31283175}
@@ -3256,6 +3303,16 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArg
32563303 claimable_htlcs. insert ( payment_hash, previous_hops) ;
32573304 }
32583305
3306+ let peer_count: u64 = Readable :: read ( reader) ?;
3307+ let mut per_peer_state = HashMap :: with_capacity ( cmp:: min ( peer_count as usize , 128 ) ) ;
3308+ for _ in 0 ..peer_count {
3309+ let peer_pubkey = Readable :: read ( reader) ?;
3310+ let peer_state = PeerState {
3311+ latest_features : Readable :: read ( reader) ?,
3312+ } ;
3313+ per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
3314+ }
3315+
32593316 let channel_manager = ChannelManager {
32603317 genesis_hash,
32613318 fee_estimator : args. fee_estimator ,
@@ -3275,6 +3332,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArg
32753332 } ) ,
32763333 our_network_key : args. keys_manager . get_node_secret ( ) ,
32773334
3335+ per_peer_state : RwLock :: new ( per_peer_state) ,
3336+
32783337 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
32793338 total_consistency_lock : RwLock :: new ( ( ) ) ,
32803339 keys_manager : args. keys_manager ,
0 commit comments