Skip to content

Commit 9654bfe

Browse files
Add id_to_peer map
1 parent 8bbd9f0 commit 9654bfe

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,25 @@ pub struct ChannelManager<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref,
739739
/// active channel list on load.
740740
outbound_scid_aliases: Mutex<HashSet<u64>>,
741741

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+
742761
our_network_key: SecretKey,
743762
our_network_pubkey: PublicKey,
744763

@@ -1244,6 +1263,7 @@ macro_rules! update_maps_on_chan_removal {
12441263
let alias_removed = $self.outbound_scid_aliases.lock().unwrap().remove(&$channel.outbound_scid_alias());
12451264
debug_assert!(alias_removed);
12461265
}
1266+
$self.id_to_peer.lock().unwrap().remove(&$channel.channel_id());
12471267
$short_to_chan_info.remove(&$channel.outbound_scid_alias());
12481268
}
12491269
}
@@ -1589,6 +1609,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
15891609
outbound_scid_aliases: Mutex::new(HashSet::new()),
15901610
pending_inbound_payments: Mutex::new(HashMap::new()),
15911611
pending_outbound_payments: Mutex::new(HashMap::new()),
1612+
id_to_peer: Mutex::new(HashMap::new()),
15921613

15931614
our_network_key: keys_manager.get_node_secret(Recipient::Node).unwrap(),
15941615
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
27612782
panic!("Generated duplicate funding txid?");
27622783
},
27632784
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+
}
27642789
e.insert(chan);
27652790
}
27662791
}
@@ -4413,6 +4438,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
44134438
return Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id".to_owned(), funding_msg.channel_id))
44144439
},
44154440
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+
}
44164445
channel_state.pending_msg_events.push(events::MessageSendEvent::SendFundingSigned {
44174446
node_id: counterparty_node_id.clone(),
44184447
msg: funding_msg,
@@ -6676,6 +6705,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
66766705
let channel_count: u64 = Readable::read(reader)?;
66776706
let mut funding_txo_set = HashSet::with_capacity(cmp::min(channel_count as usize, 128));
66786707
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));
66796709
let mut short_to_chan_info = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
66806710
let mut channel_closures = Vec::new();
66816711
for _ in 0..channel_count {
@@ -6718,6 +6748,9 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
67186748
if let Some(short_channel_id) = channel.get_short_channel_id() {
67196749
short_to_chan_info.insert(short_channel_id, (channel.get_counterparty_node_id(), channel.channel_id()));
67206750
}
6751+
if channel.is_funding_initiated() {
6752+
id_to_peer.insert(channel.channel_id(), channel.get_counterparty_node_id());
6753+
}
67216754
by_id.insert(channel.channel_id(), channel);
67226755
}
67236756
} else {
@@ -7044,6 +7077,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
70447077
pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()),
70457078

70467079
outbound_scid_aliases: Mutex::new(outbound_scid_aliases),
7080+
id_to_peer: Mutex::new(id_to_peer),
70477081
fake_scid_rand_bytes: fake_scid_rand_bytes.unwrap(),
70487082

70497083
our_network_key,

0 commit comments

Comments
 (0)