@@ -739,6 +739,25 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
739
739
/// active channel list on load.
740
740
outbound_scid_aliases : Mutex < HashSet < u64 > > ,
741
741
742
+ /// `channel_id` -> `counterparty_node_id`.
743
+ ///
744
+ /// Only `channel_id`s are allowed as keys in this map, and not `temporary_channel_id`s. As
745
+ /// multiple channels with the same `temporary_channel_id` to different peers can exist,
746
+ /// allowing `temporary_channel_id`s in this map would cause collisions for such channels.
747
+ ///
748
+ /// Note that this map should only be used for `MonitorEvent` handling, to be able to access
749
+ /// the corresponding channel for the event, as we only have access to the `channel_id` during
750
+ /// the handling of the events.
751
+ ///
752
+ /// TODO:
753
+ /// The `counterparty_node_id` isn't passed with `MonitorEvent`s currently. To pass it, we need
754
+ /// to add the `counterparty_node_id` to `ChannelMonitor`s. However, adding it as a required
755
+ /// field in `ChannelMonitor`s would break backwards compatability.
756
+ /// We should add `counterparty_node_id`s to `MonitorEvent`s, and eventually rely on it in the
757
+ /// future. That would make this map redundant, as only the `ChannelManager::per_peer_state` is
758
+ /// required to access the channel with the `counterparty_node_id`.
759
+ id_to_peer : Mutex < HashMap < [ u8 ; 32 ] , PublicKey > > ,
760
+
742
761
our_network_key : SecretKey ,
743
762
our_network_pubkey : PublicKey ,
744
763
@@ -1244,6 +1263,7 @@ macro_rules! update_maps_on_chan_removal {
1244
1263
let alias_removed = $self. outbound_scid_aliases. lock( ) . unwrap( ) . remove( & $channel. outbound_scid_alias( ) ) ;
1245
1264
debug_assert!( alias_removed) ;
1246
1265
}
1266
+ $self. id_to_peer. lock( ) . unwrap( ) . remove( & $channel. channel_id( ) ) ;
1247
1267
$short_to_chan_info. remove( & $channel. outbound_scid_alias( ) ) ;
1248
1268
}
1249
1269
}
@@ -1589,6 +1609,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
1589
1609
outbound_scid_aliases : Mutex :: new ( HashSet :: new ( ) ) ,
1590
1610
pending_inbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1591
1611
pending_outbound_payments : Mutex :: new ( HashMap :: new ( ) ) ,
1612
+ id_to_peer : Mutex :: new ( HashMap :: new ( ) ) ,
1592
1613
1593
1614
our_network_key : keys_manager. get_node_secret ( Recipient :: Node ) . unwrap ( ) ,
1594
1615
our_network_pubkey : PublicKey :: from_secret_key ( & secp_ctx, & keys_manager. get_node_secret ( Recipient :: Node ) . unwrap ( ) ) ,
@@ -2761,6 +2782,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2761
2782
panic ! ( "Generated duplicate funding txid?" ) ;
2762
2783
} ,
2763
2784
hash_map:: Entry :: Vacant ( e) => {
2785
+ let mut id_to_peer = self . id_to_peer . lock ( ) . unwrap ( ) ;
2786
+ if id_to_peer. insert ( chan. channel_id ( ) , chan. get_counterparty_node_id ( ) ) . is_some ( ) {
2787
+ panic ! ( "id_to_peer map already contained funding txid, which shouldn't be possible" ) ;
2788
+ }
2764
2789
e. insert ( chan) ;
2765
2790
}
2766
2791
}
@@ -4413,6 +4438,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4413
4438
return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg. channel_id ) )
4414
4439
} ,
4415
4440
hash_map:: Entry :: Vacant ( e) => {
4441
+ let mut id_to_peer = self . id_to_peer . lock ( ) . unwrap ( ) ;
4442
+ if id_to_peer. insert ( funding_msg. channel_id , * counterparty_node_id) . is_some ( ) {
4443
+ panic ! ( "id_to_peer map already contained funding txid, which shouldn't be possible" ) ;
4444
+ }
4416
4445
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingSigned {
4417
4446
node_id : counterparty_node_id. clone ( ) ,
4418
4447
msg : funding_msg,
@@ -6676,6 +6705,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
6676
6705
let channel_count: u64 = Readable :: read ( reader) ?;
6677
6706
let mut funding_txo_set = HashSet :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6678
6707
let mut by_id = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6708
+ let mut id_to_peer = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6679
6709
let mut short_to_chan_info = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6680
6710
let mut channel_closures = Vec :: new ( ) ;
6681
6711
for _ in 0 ..channel_count {
@@ -6718,6 +6748,9 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
6718
6748
if let Some ( short_channel_id) = channel. get_short_channel_id ( ) {
6719
6749
short_to_chan_info. insert ( short_channel_id, ( channel. get_counterparty_node_id ( ) , channel. channel_id ( ) ) ) ;
6720
6750
}
6751
+ if channel. is_funding_initiated ( ) {
6752
+ id_to_peer. insert ( channel. channel_id ( ) , channel. get_counterparty_node_id ( ) ) ;
6753
+ }
6721
6754
by_id. insert ( channel. channel_id ( ) , channel) ;
6722
6755
}
6723
6756
} else {
@@ -7044,6 +7077,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7044
7077
pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
7045
7078
7046
7079
outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
7080
+ id_to_peer : Mutex :: new ( id_to_peer) ,
7047
7081
fake_scid_rand_bytes : fake_scid_rand_bytes. unwrap ( ) ,
7048
7082
7049
7083
our_network_key,
0 commit comments