Skip to content

Commit 7833589

Browse files
Add id_to_peer map
1 parent 0780fb9 commit 7833589

File tree

1 file changed

+40
-3
lines changed

1 file changed

+40
-3
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 40 additions & 3 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()),
@@ -2756,12 +2777,18 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
27562777
node_id: chan.get_counterparty_node_id(),
27572778
msg,
27582779
});
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) {
27602782
hash_map::Entry::Occupied(_) => {
27612783
panic!("Generated duplicate funding txid?");
27622784
},
27632785
hash_map::Entry::Vacant(e) => {
2786+
let counterparty_node_id = chan.get_counterparty_node_id();
27642787
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+
}
27652792
}
27662793
}
27672794
Ok(())
@@ -4408,9 +4435,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
44084435
}
44094436
let mut channel_state_lock = self.channel_state.lock().unwrap();
44104437
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) {
44124440
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))
44144442
},
44154443
hash_map::Entry::Vacant(e) => {
44164444
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
44214449
send_channel_ready!(channel_state.short_to_chan_info, channel_state.pending_msg_events, chan, msg);
44224450
}
44234451
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+
}
44244456
}
44254457
}
44264458
Ok(())
@@ -6676,6 +6708,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
66766708
let channel_count: u64 = Readable::read(reader)?;
66776709
let mut funding_txo_set = HashSet::with_capacity(cmp::min(channel_count as usize, 128));
66786710
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));
66796712
let mut short_to_chan_info = HashMap::with_capacity(cmp::min(channel_count as usize, 128));
66806713
let mut channel_closures = Vec::new();
66816714
for _ in 0..channel_count {
@@ -6718,6 +6751,9 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
67186751
if let Some(short_channel_id) = channel.get_short_channel_id() {
67196752
short_to_chan_info.insert(short_channel_id, (channel.get_counterparty_node_id(), channel.channel_id()));
67206753
}
6754+
if channel.is_funding_initiated() {
6755+
id_to_peer.insert(channel.channel_id(), channel.get_counterparty_node_id());
6756+
}
67216757
by_id.insert(channel.channel_id(), channel);
67226758
}
67236759
} else {
@@ -7044,6 +7080,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
70447080
pending_outbound_payments: Mutex::new(pending_outbound_payments.unwrap()),
70457081

70467082
outbound_scid_aliases: Mutex::new(outbound_scid_aliases),
7083+
id_to_peer: Mutex::new(id_to_peer),
70477084
fake_scid_rand_bytes: fake_scid_rand_bytes.unwrap(),
70487085

70497086
our_network_key,

0 commit comments

Comments
 (0)