Skip to content

Commit 9864a26

Browse files
Add counterparty_node_id to short_to_id map
1 parent 8fce6a1 commit 9864a26

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,12 @@ pub(super) enum RAACommitmentOrder {
406406
// Note this is only exposed in cfg(test):
407407
pub(super) struct ChannelHolder<Signer: Sign> {
408408
pub(super) by_id: HashMap<[u8; 32], Channel<Signer>>,
409-
/// SCIDs (and outbound SCID aliases) to the real channel id. Outbound SCID aliases are added
410-
/// here once the channel is available for normal use, with SCIDs being added once the funding
411-
/// transaction is confirmed at the channel's required confirmation depth.
412-
pub(super) short_to_id: HashMap<u64, [u8; 32]>,
409+
/// SCIDs (and outbound SCID aliases) -> `counterparty_node_id`s and `channel_id`s.
410+
///
411+
/// Outbound SCID aliases are added here once the channel is available for normal use, with
412+
/// SCIDs being added once the funding transaction is confirmed at the channel's required
413+
/// confirmation depth.
414+
pub(super) short_to_id: HashMap<u64, (PublicKey, [u8; 32])>,
413415
/// SCID/SCID Alias -> forward infos. Key of 0 means payments received.
414416
///
415417
/// Note that because we may have an SCID Alias as the key we can have two entries per channel,
@@ -1412,12 +1414,12 @@ macro_rules! send_channel_ready {
14121414
});
14131415
// Note that we may send a `channel_ready` multiple times for a channel if we reconnect, so
14141416
// we allow collisions, but we shouldn't ever be updating the channel ID pointed to.
1415-
let outbound_alias_insert = $short_to_id.insert($channel.outbound_scid_alias(), $channel.channel_id());
1416-
assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == $channel.channel_id(),
1417+
let outbound_alias_insert = $short_to_id.insert($channel.outbound_scid_alias(), ($channel.get_counterparty_node_id(), $channel.channel_id()));
1418+
assert!(outbound_alias_insert.is_none() || outbound_alias_insert.unwrap() == ($channel.get_counterparty_node_id(), $channel.channel_id()),
14171419
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
14181420
if let Some(real_scid) = $channel.get_short_channel_id() {
1419-
let scid_insert = $short_to_id.insert(real_scid, $channel.channel_id());
1420-
assert!(scid_insert.is_none() || scid_insert.unwrap() == $channel.channel_id(),
1421+
let scid_insert = $short_to_id.insert(real_scid, ($channel.get_counterparty_node_id(), $channel.channel_id()));
1422+
assert!(scid_insert.is_none() || scid_insert.unwrap() == ($channel.get_counterparty_node_id(), $channel.channel_id()),
14211423
"SCIDs should never collide - ensure you weren't behind the chain tip by a full month when creating channels");
14221424
}
14231425
}
@@ -2223,7 +2225,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22232225
break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
22242226
}
22252227
},
2226-
Some(id) => Some(id.clone()),
2228+
Some((_cp_id, id)) => Some(id.clone()),
22272229
};
22282230
let (chan_update_opt, forwardee_cltv_expiry_delta) = if let Some(forwarding_id) = forwarding_id_opt {
22292231
let chan = channel_state.as_mut().unwrap().by_id.get_mut(&forwarding_id).unwrap();
@@ -2404,7 +2406,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
24042406

24052407
let id = match channel_lock.short_to_id.get(&path.first().unwrap().short_channel_id) {
24062408
None => return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()}),
2407-
Some(id) => id.clone(),
2409+
Some((_cp_id, id)) => id.clone(),
24082410
};
24092411

24102412
macro_rules! insert_outbound_payment {
@@ -2932,7 +2934,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
29322934
for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
29332935
if short_chan_id != 0 {
29342936
let forward_chan_id = match channel_state.short_to_id.get(&short_chan_id) {
2935-
Some(chan_id) => chan_id.clone(),
2937+
Some((_cp_id, chan_id)) => chan_id.clone(),
29362938
None => {
29372939
for forward_info in pending_forwards.drain(..) {
29382940
match forward_info {
@@ -3349,7 +3351,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33493351
self.process_background_events();
33503352
}
33513353

3352-
fn update_channel_fee(&self, short_to_id: &mut HashMap<u64, [u8; 32]>, pending_msg_events: &mut Vec<events::MessageSendEvent>, chan_id: &[u8; 32], chan: &mut Channel<Signer>, new_feerate: u32) -> (bool, NotifyOption, Result<(), MsgHandleErrInternal>) {
3354+
fn update_channel_fee(&self, short_to_id: &mut HashMap<u64, (PublicKey, [u8; 32])>, pending_msg_events: &mut Vec<events::MessageSendEvent>, chan_id: &[u8; 32], chan: &mut Channel<Signer>, new_feerate: u32) -> (bool, NotifyOption, Result<(), MsgHandleErrInternal>) {
33533355
if !chan.is_outbound() { return (true, NotifyOption::SkipPersist, Ok(())); }
33543356
// If the feerate has decreased by less than half, don't bother
33553357
if new_feerate <= chan.get_feerate() && new_feerate * 2 > chan.get_feerate() {
@@ -3965,7 +3967,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
39653967
//TODO: Delay the claimed_funds relaying just like we do outbound relay!
39663968
let channel_state = &mut **channel_state_lock;
39673969
let chan_id = match channel_state.short_to_id.get(&prev_hop.short_channel_id) {
3968-
Some(chan_id) => chan_id.clone(),
3970+
Some((_cp_id, chan_id)) => chan_id.clone(),
39693971
None => {
39703972
return ClaimFundsFromHop::PrevHopForceClosed
39713973
}
@@ -4887,7 +4889,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
48874889
let mut channel_state_lock = self.channel_state.lock().unwrap();
48884890
let channel_state = &mut *channel_state_lock;
48894891
let chan_id = match channel_state.short_to_id.get(&msg.contents.short_channel_id) {
4890-
Some(chan_id) => chan_id.clone(),
4892+
Some((_cp_id, chan_id)) => chan_id.clone(),
48914893
None => {
48924894
// It's not a local channel
48934895
return Ok(NotifyOption::SkipPersist)
@@ -5669,8 +5671,8 @@ where
56695671
// using the real SCID at relay-time (i.e. enforce option_scid_alias
56705672
// then), and if the funding tx is ever un-confirmed we force-close the
56715673
// channel, ensuring short_to_id is always consistent.
5672-
let scid_insert = short_to_id.insert(real_scid, channel.channel_id());
5673-
assert!(scid_insert.is_none() || scid_insert.unwrap() == channel.channel_id(),
5674+
let scid_insert = short_to_id.insert(real_scid, (channel.get_counterparty_node_id(), channel.channel_id()));
5675+
assert!(scid_insert.is_none() || scid_insert.unwrap() == (channel.get_counterparty_node_id(), channel.channel_id()),
56745676
"SCIDs should never collide - ensure you weren't behind by a full {} blocks when creating channels",
56755677
fake_scid::MAX_SCID_BLOCKS_FROM_NOW);
56765678
}
@@ -6713,7 +6715,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
67136715
} else {
67146716
log_info!(args.logger, "Successfully loaded channel {}", log_bytes!(channel.channel_id()));
67156717
if let Some(short_channel_id) = channel.get_short_channel_id() {
6716-
short_to_id.insert(short_channel_id, channel.channel_id());
6718+
short_to_id.insert(short_channel_id, (channel.get_counterparty_node_id(), channel.channel_id()));
67176719
}
67186720
by_id.insert(channel.channel_id(), channel);
67196721
}
@@ -6972,7 +6974,7 @@ impl<'a, Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref>
69726974
return Err(DecodeError::InvalidValue);
69736975
}
69746976
if chan.is_usable() {
6975-
if short_to_id.insert(chan.outbound_scid_alias(), *chan_id).is_some() {
6977+
if short_to_id.insert(chan.outbound_scid_alias(), (chan.get_counterparty_node_id(), *chan_id)).is_some() {
69766978
// Note that in rare cases its possible to hit this while reading an older
69776979
// channel if we just happened to pick a colliding outbound alias above.
69786980
log_error!(args.logger, "Got duplicate outbound SCID alias; {}", chan.outbound_scid_alias());

0 commit comments

Comments
 (0)