@@ -613,6 +613,18 @@ pub(super) struct PeerState<Signer: ChannelSigner> {
613
613
/// `channel_id`, the `temporary_channel_id` key in the map is updated and is replaced by the
614
614
/// `channel_id`.
615
615
pub ( super ) channel_by_id : HashMap < [ u8 ; 32 ] , Channel < Signer > > ,
616
+ /// `temporary_channel_id` -> `OutboundV1Channel`.
617
+ ///
618
+ /// Holds all outbound V1 channels where the peer is the counterparty. Once an outbound channel has
619
+ /// been assigned a `channel_id`, the entry in this map is removed and one is created in
620
+ /// `channel_by_id`.
621
+ pub ( super ) outbound_v1_channel_by_id : HashMap < [ u8 ; 32 ] , OutboundV1Channel < Signer > > ,
622
+ /// `temporary_channel_id` -> `InboundV1Channel`.
623
+ ///
624
+ /// Holds all inbound V1 channels where the peer is the counterparty. Once an inbound channel has
625
+ /// been assigned a `channel_id`, the entry in this map is removed and one is created in
626
+ /// `channel_by_id`.
627
+ pub ( super ) inbound_v1_channel_by_id : HashMap < [ u8 ; 32 ] , InboundV1Channel < Signer > > ,
616
628
/// The latest `InitFeatures` we heard from the peer.
617
629
latest_features : InitFeatures ,
618
630
/// Messages to send to the peer - pushed to in the same lock that they are generated in (except
@@ -654,6 +666,20 @@ impl <Signer: ChannelSigner> PeerState<Signer> {
654
666
}
655
667
self . channel_by_id . is_empty ( ) && self . monitor_update_blocked_actions . is_empty ( )
656
668
}
669
+
670
+ // Returns a count of all channels we have with this peer, including pending channels.
671
+ fn total_channel_count ( & self ) -> usize {
672
+ self . channel_by_id . len ( ) +
673
+ self . outbound_v1_channel_by_id . len ( ) +
674
+ self . inbound_v1_channel_by_id . len ( )
675
+ }
676
+
677
+ // Returns a bool indicating if the given `channel_id` matches a channel we have with this peer.
678
+ fn has_channel ( & self , channel_id : & [ u8 ; 32 ] ) -> bool {
679
+ self . channel_by_id . contains_key ( channel_id) ||
680
+ self . outbound_v1_channel_by_id . contains_key ( channel_id) ||
681
+ self . inbound_v1_channel_by_id . contains_key ( channel_id)
682
+ }
657
683
}
658
684
659
685
/// Stores a PaymentSecret and any other data we may need to validate an inbound payment is
@@ -4765,13 +4791,14 @@ where
4765
4791
fn do_accept_inbound_channel ( & self , temporary_channel_id : & [ u8 ; 32 ] , counterparty_node_id : & PublicKey , accept_0conf : bool , user_channel_id : u128 ) -> Result < ( ) , APIError > {
4766
4792
let _persistence_guard = PersistenceNotifierGuard :: notify_on_drop ( self ) ;
4767
4793
4768
- let peers_without_funded_channels = self . peers_without_funded_channels ( |peer| !peer. channel_by_id . is_empty ( ) ) ;
4794
+ let peers_without_funded_channels =
4795
+ self . peers_without_funded_channels ( |peer| { peer. total_channel_count ( ) > 0 } ) ;
4769
4796
let per_peer_state = self . per_peer_state . read ( ) . unwrap ( ) ;
4770
4797
let peer_state_mutex = per_peer_state. get ( counterparty_node_id)
4771
4798
. ok_or_else ( || APIError :: ChannelUnavailable { err : format ! ( "Can't find a peer matching the passed counterparty node_id {}" , counterparty_node_id) } ) ?;
4772
4799
let mut peer_state_lock = peer_state_mutex. lock ( ) . unwrap ( ) ;
4773
4800
let peer_state = & mut * peer_state_lock;
4774
- let is_only_peer_channel = peer_state. channel_by_id . len ( ) == 1 ;
4801
+ let is_only_peer_channel = peer_state. total_channel_count ( ) == 1 ;
4775
4802
match peer_state. channel_by_id . entry ( temporary_channel_id. clone ( ) ) {
4776
4803
hash_map:: Entry :: Occupied ( mut channel) => {
4777
4804
if !channel. get ( ) . inbound_is_awaiting_accept ( ) {
@@ -4833,7 +4860,7 @@ where
4833
4860
let peer = peer_mtx. lock ( ) . unwrap ( ) ;
4834
4861
if !maybe_count_peer ( & * peer) { continue ; }
4835
4862
let num_unfunded_channels = Self :: unfunded_channel_count ( & peer, best_block_height) ;
4836
- if num_unfunded_channels == peer. channel_by_id . len ( ) {
4863
+ if num_unfunded_channels == peer. total_channel_count ( ) {
4837
4864
peers_without_funded_channels += 1 ;
4838
4865
}
4839
4866
}
@@ -4912,33 +4939,31 @@ where
4912
4939
} ,
4913
4940
Ok ( res) => res
4914
4941
} ;
4915
- match peer_state. channel_by_id . entry ( channel. context . channel_id ( ) ) {
4916
- hash_map:: Entry :: Occupied ( _) => {
4917
- self . outbound_scid_aliases . lock ( ) . unwrap ( ) . remove ( & outbound_scid_alias) ;
4918
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "temporary_channel_id collision for the same peer!" . to_owned ( ) , msg. temporary_channel_id . clone ( ) ) )
4919
- } ,
4920
- hash_map:: Entry :: Vacant ( entry) => {
4921
- if !self . default_configuration . manually_accept_inbound_channels {
4922
- if channel. context . get_channel_type ( ) . requires_zero_conf ( ) {
4923
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "No zero confirmation channels accepted" . to_owned ( ) , msg. temporary_channel_id . clone ( ) ) ) ;
4924
- }
4925
- peer_state. pending_msg_events . push ( events:: MessageSendEvent :: SendAcceptChannel {
4926
- node_id : counterparty_node_id. clone ( ) ,
4927
- msg : channel. accept_inbound_channel ( user_channel_id) ,
4928
- } ) ;
4929
- } else {
4930
- let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
4931
- pending_events. push_back ( ( events:: Event :: OpenChannelRequest {
4932
- temporary_channel_id : msg. temporary_channel_id . clone ( ) ,
4933
- counterparty_node_id : counterparty_node_id. clone ( ) ,
4934
- funding_satoshis : msg. funding_satoshis ,
4935
- push_msat : msg. push_msat ,
4936
- channel_type : channel. context . get_channel_type ( ) . clone ( ) ,
4937
- } , None ) ) ;
4942
+ let channel_id = channel. context . channel_id ( ) ;
4943
+ let channel_exists = peer_state. has_channel ( & channel_id) ;
4944
+ if channel_exists {
4945
+ self . outbound_scid_aliases . lock ( ) . unwrap ( ) . remove ( & outbound_scid_alias) ;
4946
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "temporary_channel_id collision for the same peer!" . to_owned ( ) , msg. temporary_channel_id . clone ( ) ) )
4947
+ } else {
4948
+ if !self . default_configuration . manually_accept_inbound_channels {
4949
+ if channel. context . get_channel_type ( ) . requires_zero_conf ( ) {
4950
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "No zero confirmation channels accepted" . to_owned ( ) , msg. temporary_channel_id . clone ( ) ) ) ;
4938
4951
}
4939
-
4940
- entry. insert ( channel) ;
4952
+ peer_state. pending_msg_events . push ( events:: MessageSendEvent :: SendAcceptChannel {
4953
+ node_id : counterparty_node_id. clone ( ) ,
4954
+ msg : channel. accept_inbound_channel ( user_channel_id) ,
4955
+ } ) ;
4956
+ } else {
4957
+ let mut pending_events = self . pending_events . lock ( ) . unwrap ( ) ;
4958
+ pending_events. push_back ( ( events:: Event :: OpenChannelRequest {
4959
+ temporary_channel_id : msg. temporary_channel_id . clone ( ) ,
4960
+ counterparty_node_id : counterparty_node_id. clone ( ) ,
4961
+ funding_satoshis : msg. funding_satoshis ,
4962
+ push_msat : msg. push_msat ,
4963
+ channel_type : channel. context . get_channel_type ( ) . clone ( ) ,
4964
+ } , None ) ) ;
4941
4965
}
4966
+ peer_state. channel_by_id . insert ( channel_id, channel) ;
4942
4967
}
4943
4968
Ok ( ( ) )
4944
4969
}
@@ -6878,6 +6903,8 @@ where
6878
6903
}
6879
6904
e. insert ( Mutex :: new ( PeerState {
6880
6905
channel_by_id : HashMap :: new ( ) ,
6906
+ outbound_v1_channel_by_id : HashMap :: new ( ) ,
6907
+ inbound_v1_channel_by_id : HashMap :: new ( ) ,
6881
6908
latest_features : init_msg. features . clone ( ) ,
6882
6909
pending_msg_events : Vec :: new ( ) ,
6883
6910
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
@@ -8081,6 +8108,8 @@ where
8081
8108
let peer_pubkey = Readable :: read ( reader) ?;
8082
8109
let peer_state = PeerState {
8083
8110
channel_by_id : peer_channels. remove ( & peer_pubkey) . unwrap_or ( HashMap :: new ( ) ) ,
8111
+ outbound_v1_channel_by_id : HashMap :: new ( ) ,
8112
+ inbound_v1_channel_by_id : HashMap :: new ( ) ,
8084
8113
latest_features : Readable :: read ( reader) ?,
8085
8114
pending_msg_events : Vec :: new ( ) ,
8086
8115
monitor_update_blocked_actions : BTreeMap :: new ( ) ,
0 commit comments