Skip to content

Commit 4a83796

Browse files
f - Ensure consistency guarantees with by_id map
1 parent f0903b4 commit 4a83796

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,18 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
748748
/// Outbound SCID aliases are added here once the channel is available for normal use, with
749749
/// SCIDs being added once the funding transaction is confirmed at the channel's required
750750
/// 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`.
751763
#[cfg(any(test, feature = "_test_utils"))]
752764
pub(super) short_to_chan_info: FairRwLock<HashMap<u64, (PublicKey, [u8; 32])>>,
753765
#[cfg(not(any(test, feature = "_test_utils")))]
@@ -1335,9 +1347,9 @@ macro_rules! try_chan_entry {
13351347
macro_rules! remove_channel {
13361348
($self: expr, $channel_state: expr, $entry: expr) => {
13371349
{
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
13411353
}
13421354
}
13431355
}
@@ -4639,17 +4651,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
46394651
}
46404652
let mut channel_state_lock = self.channel_state.lock().unwrap();
46414653
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) {
46434656
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))
46454658
},
46464659
hash_map::Entry::Vacant(e) => {
46474660
let mut id_to_peer = self.id_to_peer.lock().unwrap();
46484661
match id_to_peer.entry(chan.channel_id()) {
46494662
hash_map::Entry::Occupied(_) => {
46504663
return Err(MsgHandleErrInternal::send_err_msg_no_close(
46514664
"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))
46534666
},
46544667
hash_map::Entry::Vacant(i_e) => {
46554668
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
46594672
node_id: counterparty_node_id.clone(),
46604673
msg: funding_msg,
46614674
});
4662-
if let Some(msg) = channel_ready {
4663-
send_channel_ready!(self, channel_state.pending_msg_events, chan, msg);
4664-
}
46654675
e.insert(chan);
46664676
}
46674677
}
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+
}
46684686
Ok(())
46694687
}
46704688

0 commit comments

Comments
 (0)