@@ -730,6 +730,25 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
730
730
/// active channel list on load.
731
731
outbound_scid_aliases : Mutex < HashSet < u64 > > ,
732
732
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
+
733
752
our_network_key : SecretKey ,
734
753
our_network_pubkey : PublicKey ,
735
754
@@ -1245,6 +1264,7 @@ macro_rules! update_maps_on_chan_removal {
1245
1264
let alias_removed = $self. outbound_scid_aliases. lock( ) . unwrap( ) . remove( & $channel. outbound_scid_alias( ) ) ;
1246
1265
debug_assert!( alias_removed) ;
1247
1266
}
1267
+ $self. id_to_peer. lock( ) . unwrap( ) . remove( & $channel. channel_id( ) ) ;
1248
1268
$short_to_chan_info. remove( & $channel. outbound_scid_alias( ) ) ;
1249
1269
}
1250
1270
}
@@ -1583,6 +1603,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1583
1603
outbound_scid_aliases : Mutex :: new ( HashSet :: new ( ) ) ,
1584
1604
pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1585
1605
pending_outbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1606
+ id_to_peer : Mutex :: new ( HashMap :: new ( ) ) ,
1586
1607
1587
1608
our_network_key : keys_manager. get_node_secret ( Recipient :: Node ) . unwrap ( ) ,
1588
1609
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
2771
2792
panic ! ( "Generated duplicate funding txid?" ) ;
2772
2793
} ,
2773
2794
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
+ }
2774
2799
e. insert ( chan) ;
2775
2800
}
2776
2801
}
@@ -4513,6 +4538,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4513
4538
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg. channel_id ) )
4514
4539
} ,
4515
4540
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
+ }
4516
4545
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingSigned {
4517
4546
node_id : counterparty_node_id. clone ( ) ,
4518
4547
msg : funding_msg,
@@ -6777,6 +6806,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
6777
6806
let channel_count: u64 = Readable :: read ( reader) ?;
6778
6807
let mut funding_txo_set = HashSet :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6779
6808
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 ) ) ;
6780
6810
let mut short_to_chan_info = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6781
6811
let mut channel_closures = Vec :: new ( ) ;
6782
6812
for _ in 0 ..channel_count {
@@ -6819,6 +6849,9 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
6819
6849
if let Some ( short_channel_id) = channel. get_short_channel_id ( ) {
6820
6850
short_to_chan_info. insert ( short_channel_id, ( channel. get_counterparty_node_id ( ) , channel. channel_id ( ) ) ) ;
6821
6851
}
6852
+ if channel. is_funding_initiated ( ) {
6853
+ id_to_peer. insert ( channel. channel_id ( ) , channel. get_counterparty_node_id ( ) ) ;
6854
+ }
6822
6855
by_id. insert ( channel. channel_id ( ) , channel) ;
6823
6856
}
6824
6857
} else {
@@ -7145,6 +7178,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7145
7178
pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
7146
7179
7147
7180
outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
7181
+ id_to_peer : Mutex :: new ( id_to_peer) ,
7148
7182
fake_scid_rand_bytes : fake_scid_rand_bytes. unwrap ( ) ,
7149
7183
7150
7184
our_network_key,
0 commit comments