@@ -293,6 +293,12 @@ impl<ChanSigner: ChannelKeys> ChannelHolder<ChanSigner> {
293293 }
294294}
295295
296+ /// State we hold per-peer. In the future we should put channels in here, but for now we only hold
297+ /// the latest Init features we heard from the peer.
298+ struct PerPeerState {
299+ latest_features : InitFeatures ,
300+ }
301+
296302#[ cfg( not( any( target_pointer_width = "32" , target_pointer_width = "64" ) ) ) ]
297303const ERR : ( ) = "You need at least 32 bit pointers (well, usize, but we'll assume they're the same) for ChannelManager::latest_block_height" ;
298304
@@ -346,6 +352,14 @@ pub struct ChannelManager<ChanSigner: ChannelKeys> {
346352 channel_state : Mutex < ChannelHolder < ChanSigner > > ,
347353 our_network_key : SecretKey ,
348354
355+ /// Per-peer state storage.
356+ /// Because adding or removing an entry is rare, we usually take an outer read lock and then
357+ /// operate on the inner value freely. Sadly, this prevents parallel operation when opening a
358+ /// new channel.
359+ /// If we are connected to a peer we always at least have an entry here, to store their features
360+ /// so that we have them available if we open a channel with them and need it for routing.
361+ per_peer_state : RwLock < HashMap < PublicKey , Mutex < PerPeerState > > > ,
362+
349363 pending_events : Mutex < Vec < events:: Event > > ,
350364 /// Used when we have to take a BIG lock to make sure everything is self-consistent.
351365 /// Essentially just when we're serializing ourselves out.
@@ -628,6 +642,8 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
628642 } ) ,
629643 our_network_key : keys_manager. get_node_secret ( ) ,
630644
645+ per_peer_state : RwLock :: new ( HashMap :: new ( ) ) ,
646+
631647 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
632648 total_consistency_lock : RwLock :: new ( ( ) ) ,
633649
@@ -2798,6 +2814,7 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
27982814 let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
27992815 let mut failed_channels = Vec :: new ( ) ;
28002816 let mut failed_payments = Vec :: new ( ) ;
2817+ let mut no_channels_remain = true ;
28012818 {
28022819 let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
28032820 let channel_state = channel_state_lock. borrow_parts ( ) ;
@@ -2825,6 +2842,7 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
28252842 log_debug ! ( self , "Marking channels with {} disconnected and generating channel_updates" , log_pubkey!( their_node_id) ) ;
28262843 channel_state. by_id . retain ( |_, chan| {
28272844 if chan. get_their_node_id ( ) == * their_node_id {
2845+ no_channels_remain = false ;
28282846 let failed_adds = chan. remove_uncommitted_htlcs_and_mark_paused ( ) ;
28292847 chan. to_disabled_marked ( ) ;
28302848 if !failed_adds. is_empty ( ) {
@@ -2861,6 +2879,10 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
28612879 }
28622880 } ) ;
28632881 }
2882+ if no_channels_remain {
2883+ self . per_peer_state . write ( ) . unwrap ( ) . remove ( their_node_id) ;
2884+ }
2885+
28642886 for failure in failed_channels. drain ( ..) {
28652887 self . finish_force_close_channel ( failure) ;
28662888 }
@@ -2871,10 +2893,25 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
28712893 }
28722894 }
28732895
2874- fn peer_connected ( & self , their_node_id : & PublicKey , _init_msg : & msgs:: Init ) {
2896+ fn peer_connected ( & self , their_node_id : & PublicKey , init_msg : & msgs:: Init ) {
28752897 log_debug ! ( self , "Generating channel_reestablish events for {}" , log_pubkey!( their_node_id) ) ;
28762898
28772899 let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2900+
2901+ {
2902+ let mut peer_state_lock = self . per_peer_state . write ( ) . unwrap ( ) ;
2903+ match peer_state_lock. entry ( their_node_id. clone ( ) ) {
2904+ hash_map:: Entry :: Vacant ( e) => {
2905+ e. insert ( Mutex :: new ( PerPeerState {
2906+ latest_features : init_msg. features . clone ( ) ,
2907+ } ) ) ;
2908+ } ,
2909+ hash_map:: Entry :: Occupied ( e) => {
2910+ e. get ( ) . lock ( ) . unwrap ( ) . latest_features = init_msg. features . clone ( ) ;
2911+ } ,
2912+ }
2913+ }
2914+
28782915 let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
28792916 let channel_state = channel_state_lock. borrow_parts ( ) ;
28802917 let pending_msg_events = channel_state. pending_msg_events ;
@@ -3141,6 +3178,14 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for ChannelManager<ChanSigne
31413178 }
31423179 }
31433180
3181+ let per_peer_state = self . per_peer_state . write ( ) . unwrap ( ) ;
3182+ ( per_peer_state. len ( ) as u64 ) . write ( writer) ?;
3183+ for ( peer_pubkey, peer_state_mutex) in per_peer_state. iter ( ) {
3184+ peer_pubkey. write ( writer) ?;
3185+ let peer_state = peer_state_mutex. lock ( ) . unwrap ( ) ;
3186+ peer_state. latest_features . write ( writer) ?;
3187+ }
3188+
31443189 Ok ( ( ) )
31453190 }
31463191}
@@ -3274,6 +3319,16 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArg
32743319 claimable_htlcs. insert ( payment_hash, previous_hops) ;
32753320 }
32763321
3322+ let peer_count: u64 = Readable :: read ( reader) ?;
3323+ let mut per_peer_state = HashMap :: with_capacity ( cmp:: min ( peer_count as usize , 128 ) ) ;
3324+ for _ in 0 ..peer_count {
3325+ let peer_pubkey = Readable :: read ( reader) ?;
3326+ let peer_state = PerPeerState {
3327+ latest_features : Readable :: read ( reader) ?,
3328+ } ;
3329+ per_peer_state. insert ( peer_pubkey, Mutex :: new ( peer_state) ) ;
3330+ }
3331+
32773332 let channel_manager = ChannelManager {
32783333 genesis_hash,
32793334 fee_estimator : args. fee_estimator ,
@@ -3293,6 +3348,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArg
32933348 } ) ,
32943349 our_network_key : args. keys_manager . get_node_secret ( ) ,
32953350
3351+ per_peer_state : RwLock :: new ( per_peer_state) ,
3352+
32963353 pending_events : Mutex :: new ( Vec :: new ( ) ) ,
32973354 total_consistency_lock : RwLock :: new ( ( ) ) ,
32983355 keys_manager : args. keys_manager ,
0 commit comments