@@ -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 add the `counterparty_node_id` to `ChannelMonitor`s. However, adding it as a required
746+ /// field in `ChannelMonitor`s 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,10 @@ 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+ if id_to_peer. insert ( funding_msg. channel_id , * counterparty_node_id) . is_some ( ) {
4543+ panic ! ( "id_to_peer map already contained funding txid, which shouldn't be possible" ) ;
4544+ }
45164545 channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingSigned {
45174546 node_id : counterparty_node_id. clone ( ) ,
45184547 msg : funding_msg,
@@ -6777,6 +6806,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
67776806 let channel_count: u64 = Readable :: read ( reader) ?;
67786807 let mut funding_txo_set = HashSet :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
67796808 let mut by_id = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6809+ let mut id_to_peer = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
67806810 let mut short_to_chan_info = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
67816811 let mut channel_closures = Vec :: new ( ) ;
67826812 for _ in 0 ..channel_count {
@@ -6819,6 +6849,9 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
68196849 if let Some ( short_channel_id) = channel. get_short_channel_id ( ) {
68206850 short_to_chan_info. insert ( short_channel_id, ( channel. get_counterparty_node_id ( ) , channel. channel_id ( ) ) ) ;
68216851 }
6852+ if channel. is_funding_initiated ( ) {
6853+ id_to_peer. insert ( channel. channel_id ( ) , channel. get_counterparty_node_id ( ) ) ;
6854+ }
68226855 by_id. insert ( channel. channel_id ( ) , channel) ;
68236856 }
68246857 } else {
@@ -7145,6 +7178,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
71457178 pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
71467179
71477180 outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
7181+ id_to_peer : Mutex :: new ( id_to_peer) ,
71487182 fake_scid_rand_bytes : fake_scid_rand_bytes. unwrap ( ) ,
71497183
71507184 our_network_key,
0 commit comments