Skip to content

Commit 1ba7e73

Browse files
Consider channel_ids in short_to_chan_info as unguaranteed
As the `short_to_chan_info` map has been removed from the `channel_state`, there is no longer any consistency guarantees between the `by_id` and `short_to_chan_info` maps. This commit ensures that we don't force unwrap channels where the channel_id has been queried from the `short_to_chan_info` map.
1 parent bbe4d7c commit 1ba7e73

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2279,7 +2279,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22792279
Some((_cp_id, chan_id)) => Some(chan_id.clone()),
22802280
};
22812281
let chan_update_opt = if let Some(forwarding_id) = forwarding_id_opt {
2282-
let chan = channel_state.by_id.get_mut(&forwarding_id).unwrap();
2282+
let chan = match channel_state.by_id.get_mut(&forwarding_id){
2283+
None => {
2284+
// Channel was removed. The short_to_chan_info and by_id maps have
2285+
// no consistency guarantees.
2286+
break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
2287+
},
2288+
Some(chan) => chan
2289+
};
22832290
if !chan.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
22842291
// Note that the behavior here should be identical to the above block - we
22852292
// should NOT reveal the existence or non-existence of a private channel if
@@ -2525,7 +2532,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25252532
},
25262533
None => { insert_outbound_payment!(); },
25272534
}
2528-
} else { unreachable!(); }
2535+
} else {
2536+
return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()});
2537+
}
25292538
return Ok(());
25302539
};
25312540

@@ -3114,9 +3123,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31143123

31153124
for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
31163125
if short_chan_id != 0 {
3117-
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3118-
Some((_cp_id, chan_id)) => chan_id.clone(),
3119-
None => {
3126+
macro_rules! fail_pending_forwards {
3127+
() => {
31203128
for forward_info in pending_forwards.drain(..) {
31213129
match forward_info {
31223130
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
@@ -3203,6 +3211,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32033211
}
32043212
}
32053213
}
3214+
}
3215+
}
3216+
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3217+
Some((_cp_id, chan_id)) => chan_id.clone(),
3218+
None => {
3219+
fail_pending_forwards!();
32063220
continue;
32073221
}
32083222
};
@@ -3329,7 +3343,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33293343
});
33303344
}
33313345
} else {
3332-
unreachable!();
3346+
fail_pending_forwards!();
3347+
continue;
33333348
}
33343349
} else {
33353350
for forward_info in pending_forwards.drain(..) {
@@ -4268,7 +4283,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42684283
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
42694284
},
42704285
}
4271-
} else { unreachable!(); }
4286+
} else { return ClaimFundsFromHop::PrevHopForceClosed }
42724287
}
42734288

42744289
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -5178,7 +5193,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
51785193
try_chan_entry!(self, chan.get_mut().channel_update(&msg), chan);
51795194
}
51805195
},
5181-
hash_map::Entry::Vacant(_) => unreachable!()
5196+
hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersist)
51825197
}
51835198
Ok(NotifyOption::DoPersist)
51845199
}

0 commit comments

Comments
 (0)