@@ -275,6 +275,12 @@ pub(super) struct ChannelHolder<ChanSigner: ChannelKeys> {
275
275
pub ( super ) pending_msg_events : Vec < events:: MessageSendEvent > ,
276
276
}
277
277
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
+
278
284
#[ cfg( not( any( target_pointer_width = "32" , target_pointer_width = "64" ) ) ) ]
279
285
const ERR : ( ) = "You need at least 32 bit pointers (well, usize, but we'll assume they're the same) for ChannelManager::latest_block_height" ;
280
286
@@ -328,6 +334,14 @@ pub struct ChannelManager<ChanSigner: ChannelKeys> {
328
334
channel_state : Mutex < ChannelHolder < ChanSigner > > ,
329
335
our_network_key : SecretKey ,
330
336
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
+
331
345
pending_events : Mutex < Vec < events:: Event > > ,
332
346
/// Used when we have to take a BIG lock to make sure everything is self-consistent.
333
347
/// Essentially just when we're serializing ourselves out.
@@ -390,6 +404,10 @@ pub struct ChannelDetails {
390
404
pub short_channel_id : Option < u64 > ,
391
405
/// The node_id of our counterparty
392
406
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 ,
393
411
/// The value, in satoshis, of this channel as appears in the funding output
394
412
pub channel_value_satoshis : u64 ,
395
413
/// The user_id passed in to create_channel, or 0 if the channel was inbound.
@@ -610,6 +628,8 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
610
628
} ) ,
611
629
our_network_key : keys_manager. get_node_secret ( ) ,
612
630
631
+ per_peer_state : RwLock :: new ( HashMap :: new ( ) ) ,
632
+
613
633
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
614
634
total_consistency_lock : RwLock :: new ( ( ) ) ,
615
635
@@ -660,56 +680,53 @@ impl<ChanSigner: ChannelKeys> ChannelManager<ChanSigner> {
660
680
Ok ( ( ) )
661
681
}
662
682
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) {
697
689
let ( inbound_capacity_msat, outbound_capacity_msat) = channel. get_inbound_outbound_available_balance_msat ( ) ;
698
690
res. push ( ChannelDetails {
699
691
channel_id : ( * channel_id) . clone ( ) ,
700
692
short_channel_id : channel. get_short_channel_id ( ) ,
701
693
remote_network_id : channel. get_their_node_id ( ) ,
694
+ counterparty_features : InitFeatures :: empty ( ) ,
702
695
channel_value_satoshis : channel. get_value_satoshis ( ) ,
703
696
inbound_capacity_msat,
704
697
outbound_capacity_msat,
705
698
user_id : channel. get_user_id ( ) ,
706
- is_live : true ,
699
+ is_live : channel . is_live ( ) ,
707
700
} ) ;
708
701
}
709
702
}
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
+ }
710
709
res
711
710
}
712
711
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
+
713
730
/// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
714
731
/// will be accepted on the given channel, and after additional timeout/the closing of all
715
732
/// pending HTLCs, the channel will be closed on chain.
@@ -2780,6 +2797,7 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
2780
2797
let _ = self . total_consistency_lock . read ( ) . unwrap ( ) ;
2781
2798
let mut failed_channels = Vec :: new ( ) ;
2782
2799
let mut failed_payments = Vec :: new ( ) ;
2800
+ let mut no_channels_remain = true ;
2783
2801
{
2784
2802
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2785
2803
let channel_state = & mut * channel_state_lock;
@@ -2818,6 +2836,8 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
2818
2836
short_to_id. remove ( & short_id) ;
2819
2837
}
2820
2838
return false ;
2839
+ } else {
2840
+ no_channels_remain = false ;
2821
2841
}
2822
2842
}
2823
2843
true
@@ -2843,6 +2863,10 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
2843
2863
}
2844
2864
} ) ;
2845
2865
}
2866
+ if no_channels_remain {
2867
+ self . per_peer_state . write ( ) . unwrap ( ) . remove ( their_node_id) ;
2868
+ }
2869
+
2846
2870
for failure in failed_channels. drain ( ..) {
2847
2871
self . finish_force_close_channel ( failure) ;
2848
2872
}
@@ -2853,10 +2877,25 @@ impl<ChanSigner: ChannelKeys> ChannelMessageHandler for ChannelManager<ChanSigne
2853
2877
}
2854
2878
}
2855
2879
2856
- fn peer_connected ( & self , their_node_id : & PublicKey ) {
2880
+ fn peer_connected ( & self , their_node_id : & PublicKey , init_msg : & msgs :: Init ) {
2857
2881
log_debug ! ( self , "Generating channel_reestablish events for {}" , log_pubkey!( their_node_id) ) ;
2858
2882
2859
2883
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
+
2860
2899
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
2861
2900
let channel_state = & mut * channel_state_lock;
2862
2901
let pending_msg_events = & mut channel_state. pending_msg_events ;
@@ -3123,6 +3162,14 @@ impl<ChanSigner: ChannelKeys + Writeable> Writeable for ChannelManager<ChanSigne
3123
3162
}
3124
3163
}
3125
3164
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
+
3126
3173
Ok ( ( ) )
3127
3174
}
3128
3175
}
@@ -3256,6 +3303,16 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArg
3256
3303
claimable_htlcs. insert ( payment_hash, previous_hops) ;
3257
3304
}
3258
3305
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
+
3259
3316
let channel_manager = ChannelManager {
3260
3317
genesis_hash,
3261
3318
fee_estimator : args. fee_estimator ,
@@ -3275,6 +3332,8 @@ impl<'a, R : ::std::io::Read, ChanSigner: ChannelKeys + Readable<R>> ReadableArg
3275
3332
} ) ,
3276
3333
our_network_key : args. keys_manager . get_node_secret ( ) ,
3277
3334
3335
+ per_peer_state : RwLock :: new ( per_peer_state) ,
3336
+
3278
3337
pending_events : Mutex :: new ( Vec :: new ( ) ) ,
3279
3338
total_consistency_lock : RwLock :: new ( ( ) ) ,
3280
3339
keys_manager : args. keys_manager ,
0 commit comments