@@ -748,6 +748,18 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
748
748
/// Outbound SCID aliases are added here once the channel is available for normal use, with
749
749
/// SCIDs being added once the funding transaction is confirmed at the channel's required
750
750
/// confirmation depth.
751
+ ///
752
+ /// Consistancy guarantees with the `channel_state::by_id` map, guarantees that any
753
+ /// `channel_id`s stored, are guaranteed to be existing in the `channel_state::by_id` map.
754
+ /// Therefore when adding or removing a channel, the channel must be added to the
755
+ /// `channel_state::by_id` map before to being added to `short_to_chan_info`, as well as
756
+ /// be removed from the `short_to_chan_info` prior to being removed from the
757
+ /// `channel_state::by_id` map. Alternatively, if the add/remove order can't be upheld, the
758
+ /// `short_to_chan_info` lock must be held while the channel is added to both
759
+ /// `channel_state::by_id` and `short_to_chan_info`, or removed from the respective maps.
760
+ ///
761
+ /// If also holding `channel_state` lock, must lock `channel_state` prior to
762
+ /// `short_to_chan_info`.
751
763
#[ cfg( any( test, feature = "_test_utils" ) ) ]
752
764
pub ( super ) short_to_chan_info : FairRwLock < HashMap < u64 , ( PublicKey , [ u8 ; 32 ] ) > > ,
753
765
#[ cfg( not( any( test, feature = "_test_utils" ) ) ) ]
@@ -1335,9 +1347,9 @@ macro_rules! try_chan_entry {
1335
1347
macro_rules! remove_channel {
1336
1348
( $self: expr, $channel_state: expr, $entry: expr) => {
1337
1349
{
1338
- let channel = $entry. remove_entry ( ) . 1 ;
1339
- update_maps_on_chan_removal! ( $self , channel ) ;
1340
- channel
1350
+ update_maps_on_chan_removal! ( $self , $entry. get ( ) ) ;
1351
+ let removed_channel = $entry . remove_entry ( ) . 1 ;
1352
+ removed_channel
1341
1353
}
1342
1354
}
1343
1355
}
@@ -4639,17 +4651,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4639
4651
}
4640
4652
let mut channel_state_lock = self . channel_state . lock ( ) . unwrap ( ) ;
4641
4653
let channel_state = & mut * channel_state_lock;
4642
- match channel_state. by_id . entry ( funding_msg. channel_id ) {
4654
+ let channel_id = funding_msg. channel_id ;
4655
+ match channel_state. by_id . entry ( channel_id) {
4643
4656
hash_map:: Entry :: Occupied ( _) => {
4644
- return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , funding_msg . channel_id ) )
4657
+ return Err ( MsgHandleErrInternal :: send_err_msg_no_close ( "Already had channel with the new channel_id" . to_owned ( ) , channel_id) )
4645
4658
} ,
4646
4659
hash_map:: Entry :: Vacant ( e) => {
4647
4660
let mut id_to_peer = self . id_to_peer . lock ( ) . unwrap ( ) ;
4648
4661
match id_to_peer. entry ( chan. channel_id ( ) ) {
4649
4662
hash_map:: Entry :: Occupied ( _) => {
4650
4663
return Err ( MsgHandleErrInternal :: send_err_msg_no_close (
4651
4664
"The funding_created message had the same funding_txid as an existing channel - funding is not possible" . to_owned ( ) ,
4652
- funding_msg . channel_id ) )
4665
+ channel_id) )
4653
4666
} ,
4654
4667
hash_map:: Entry :: Vacant ( i_e) => {
4655
4668
i_e. insert ( chan. get_counterparty_node_id ( ) ) ;
@@ -4659,12 +4672,17 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
4659
4672
node_id : counterparty_node_id. clone ( ) ,
4660
4673
msg : funding_msg,
4661
4674
} ) ;
4662
- if let Some ( msg) = channel_ready {
4663
- send_channel_ready ! ( self , channel_state. pending_msg_events, chan, msg) ;
4664
- }
4665
4675
e. insert ( chan) ;
4666
4676
}
4667
4677
}
4678
+
4679
+ if let Some ( msg) = channel_ready {
4680
+ // Call send_channel_ready after the channel has been added with the real chan_id to
4681
+ // the by_id map, to ensure that short_to_chan_info is populated after by_id.
4682
+ if let Some ( chan) = channel_state. by_id . get ( & channel_id) {
4683
+ send_channel_ready ! ( self , channel_state. pending_msg_events, chan, msg) ;
4684
+ }
4685
+ }
4668
4686
Ok ( ( ) )
4669
4687
}
4670
4688
0 commit comments