@@ -730,6 +730,25 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
730730 /// active channel list on load.
731731 outbound_scid_aliases : Mutex < HashSet < u64 > > ,
732732
733+ /// `channel_id` -> `counterparty_node_id`.
734+ ///
735+ /// Only `channel_id`s are allowed as keys in this map, and not `temporary_channel_id`s. As
736+ /// multiple channels with the same `temporary_channel_id` to different peers can exist,
737+ /// allowing `temporary_channel_id`s in this map would cause collisions for such channels.
738+ ///
739+ /// Note that this map should only be used for `MonitorEvent` handling, to be able to access
740+ /// the corresponding channel for the event, as we only have access to the `channel_id` during
741+ /// the handling of the events.
742+ ///
743+ /// TODO:
744+ /// The `counterparty_node_id` isn't passed with `MonitorEvent`s currently. To pass it, we need
745+ /// to make `counterparty_node_id`'s a required field in `ChannelMonitor`s, which unfortunately
746+ /// would break backwards compatability.
747+ /// We should add `counterparty_node_id`s to `MonitorEvent`s, and eventually rely on it in the
748+ /// future. That would make this map redundant, as only the `ChannelManager::per_peer_state` is
749+ /// required to access the channel with the `counterparty_node_id`.
750+ id_to_peer : Mutex < HashMap < [ u8 ; 32 ] , PublicKey > > ,
751+
733752 our_network_key : SecretKey ,
734753 our_network_pubkey : PublicKey ,
735754
@@ -1245,6 +1264,7 @@ macro_rules! update_maps_on_chan_removal {
12451264 let alias_removed = $self. outbound_scid_aliases. lock( ) . unwrap( ) . remove( & $channel. outbound_scid_alias( ) ) ;
12461265 debug_assert!( alias_removed) ;
12471266 }
1267+ $self. id_to_peer. lock( ) . unwrap( ) . remove( & $channel. channel_id( ) ) ;
12481268 $short_to_chan_info. remove( & $channel. outbound_scid_alias( ) ) ;
12491269 }
12501270}
@@ -1583,6 +1603,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
15831603 outbound_scid_aliases : Mutex :: new ( HashSet :: new ( ) ) ,
15841604 pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
15851605 pending_outbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1606+ id_to_peer : Mutex :: new ( HashMap :: new ( ) ) ,
15861607
15871608 our_network_key : keys_manager. get_node_secret ( Recipient :: Node ) . unwrap ( ) ,
15881609 our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & keys_manager. get_node_secret ( Recipient :: Node ) . unwrap ( ) ) ,
@@ -2771,6 +2792,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
27712792 panic ! ( "Generated duplicate funding txid?" ) ;
27722793 } ,
27732794 hash_map:: Entry :: Vacant ( e) => {
2795+ let mut id_to_peer = self . id_to_peer . lock ( ) . unwrap ( ) ;
2796+ if id_to_peer. insert ( chan. channel_id ( ) , chan. get_counterparty_node_id ( ) ) . is_some ( ) {
2797+ panic ! ( "id_to_peer map already contained funding txid, which shouldn't be possible" ) ;
2798+ }
27742799 e. insert ( chan) ;
27752800 }
27762801 }
@@ -4513,6 +4538,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
45134538 return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg. channel_id ) )
45144539 } ,
45154540 hash_map:: Entry :: Vacant ( e) => {
4541+ let mut id_to_peer = self . id_to_peer . lock ( ) . unwrap ( ) ;
4542+ match id_to_peer. entry ( chan. channel_id ( ) ) {
4543+ hash_map:: Entry :: Occupied ( _) => {
4544+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close (
4545+ "The funding_created message had the same funding_txid as an existing channel - funding is not possible" . to_owned ( ) ,
4546+ funding_msg. channel_id ) )
4547+ } ,
4548+ hash_map:: Entry :: Vacant ( i_e) => {
4549+ i_e. insert ( chan. get_counterparty_node_id ( ) ) ;
4550+ }
4551+ }
45164552 channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingSigned {
45174553 node_id : counterparty_node_id. clone ( ) ,
45184554 msg : funding_msg,
@@ -6777,6 +6813,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
67776813 let channel_count: u64 = Readable :: read ( reader) ?;
67786814 let mut funding_txo_set = HashSet :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
67796815 let mut by_id = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6816+ let mut id_to_peer = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
67806817 let mut short_to_chan_info = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
67816818 let mut channel_closures = Vec :: new ( ) ;
67826819 for _ in 0 ..channel_count {
@@ -6819,6 +6856,9 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
68196856 if let Some ( short_channel_id) = channel. get_short_channel_id ( ) {
68206857 short_to_chan_info. insert ( short_channel_id, ( channel. get_counterparty_node_id ( ) , channel. channel_id ( ) ) ) ;
68216858 }
6859+ if channel. is_funding_initiated ( ) {
6860+ id_to_peer. insert ( channel. channel_id ( ) , channel. get_counterparty_node_id ( ) ) ;
6861+ }
68226862 by_id. insert ( channel. channel_id ( ) , channel) ;
68236863 }
68246864 } else {
@@ -7145,6 +7185,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
71457185 pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
71467186
71477187 outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
7188+ id_to_peer : Mutex :: new ( id_to_peer) ,
71487189 fake_scid_rand_bytes : fake_scid_rand_bytes. unwrap ( ) ,
71497190
71507191 our_network_key,
0 commit comments