@@ -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 ( ) ) ,
@@ -2756,12 +2777,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
2756
2777
node_id : chan. get_counterparty_node_id ( ) ,
2757
2778
msg,
2758
2779
} ) ;
2759
- match channel_state. by_id . entry ( chan. channel_id ( ) ) {
2780
+ let chan_id = chan. channel_id ( ) ;
2781
+ match channel_state. by_id . entry ( chan_id) {
2760
2782
hash_map:: Entry :: Occupied ( _) => {
2761
2783
panic ! ( "Generated duplicate funding txid?" ) ;
2762
2784
} ,
2763
2785
hash_map:: Entry :: Vacant ( e) => {
2786
+ let counterparty_node_id = chan. get_counterparty_node_id ( ) ;
2764
2787
e. insert ( chan) ;
2788
+ let mut id_to_peer = self . id_to_peer . lock ( ) . unwrap ( ) ;
2789
+ if id_to_peer. insert ( chan_id, counterparty_node_id) . is_some ( ) {
2790
+ panic ! ( "id_to_peer map already contained funding txid, which shouldn't be possible" ) ;
2791
+ }
2765
2792
}
2766
2793
}
2767
2794
Ok ( ( ) )
@@ -4408,9 +4435,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4408
4435
}
4409
4436
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4410
4437
let channel_state = & mut * channel_state_lock;
4411
- match channel_state. by_id . entry ( funding_msg. channel_id ) {
4438
+ let channel_id = funding_msg. channel_id ;
4439
+ match channel_state. by_id . entry ( channel_id) {
4412
4440
hash_map:: Entry :: Occupied ( _) => {
4413
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg . channel_id ) )
4441
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , channel_id) )
4414
4442
} ,
4415
4443
hash_map:: Entry :: Vacant ( e) => {
4416
4444
channel_state. pending_msg_events . push ( events:: MessageSendEvent :: SendFundingSigned {
@@ -4421,6 +4449,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4421
4449
send_channel_ready ! ( channel_state. short_to_chan_info, channel_state. pending_msg_events, chan, msg) ;
4422
4450
}
4423
4451
e. insert ( chan) ;
4452
+ let mut id_to_peer = self . id_to_peer . lock ( ) . unwrap ( ) ;
4453
+ if id_to_peer. insert ( channel_id, * counterparty_node_id) . is_some ( ) {
4454
+ panic ! ( "id_to_peer map already contained funding txid, which shouldn't be possible" ) ;
4455
+ }
4424
4456
}
4425
4457
}
4426
4458
Ok ( ( ) )
@@ -6676,6 +6708,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
6676
6708
let channel_count: u64 = Readable :: read ( reader) ?;
6677
6709
let mut funding_txo_set = HashSet :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6678
6710
let mut by_id = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6711
+ let mut id_to_peer = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6679
6712
let mut short_to_chan_info = HashMap :: with_capacity ( cmp:: min ( channel_count as usize , 128 ) ) ;
6680
6713
let mut channel_closures = Vec :: new ( ) ;
6681
6714
for _ in 0 ..channel_count {
@@ -6718,6 +6751,9 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
6718
6751
if let Some ( short_channel_id) = channel. get_short_channel_id ( ) {
6719
6752
short_to_chan_info. insert ( short_channel_id, ( channel. get_counterparty_node_id ( ) , channel. channel_id ( ) ) ) ;
6720
6753
}
6754
+ if channel. is_funding_initiated ( ) {
6755
+ id_to_peer. insert ( channel. channel_id ( ) , channel. get_counterparty_node_id ( ) ) ;
6756
+ }
6721
6757
by_id. insert ( channel. channel_id ( ) , channel) ;
6722
6758
}
6723
6759
} else {
@@ -7044,6 +7080,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
7044
7080
pending_outbound_payments : Mutex :: new ( pending_outbound_payments. unwrap ( ) ) ,
7045
7081
7046
7082
outbound_scid_aliases : Mutex :: new ( outbound_scid_aliases) ,
7083
+ id_to_peer : Mutex :: new ( id_to_peer) ,
7047
7084
fake_scid_rand_bytes : fake_scid_rand_bytes. unwrap ( ) ,
7048
7085
7049
7086
our_network_key,
0 commit comments