Skip to content

Commit af85171

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 e7e7dc6 commit af85171

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2299,7 +2299,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
22992299
Some((_cp_id, chan_id)) => Some(chan_id.clone()),
23002300
};
23012301
let chan_update_opt = if let Some(forwarding_id) = forwarding_id_opt {
2302-
let chan = channel_state.by_id.get_mut(&forwarding_id).unwrap();
2302+
let chan = match channel_state.by_id.get_mut(&forwarding_id) {
2303+
None => {
2304+
// Channel was removed. The short_to_chan_info and by_id maps have
2305+
// no consistency guarantees.
2306+
break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
2307+
},
2308+
Some(chan) => chan
2309+
};
23032310
if !chan.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
23042311
// Note that the behavior here should be identical to the above block - we
23052312
// should NOT reveal the existence or non-existence of a private channel if
@@ -2555,7 +2562,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
25552562
},
25562563
None => { insert_outbound_payment!(); },
25572564
}
2558-
} else { unreachable!(); }
2565+
} else {
2566+
// The channel was likely removed after we fetched the id from the
2567+
// `short_to_chan_info` map, but before we successfully locked the `by_id` map.
2568+
// This can occur as no consistency guarantees exists between the two maps.
2569+
return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()});
2570+
}
25592571
return Ok(());
25602572
};
25612573

@@ -3062,9 +3074,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
30623074
let mut channel_state_lock = self.channel_state.lock().unwrap();
30633075
let channel_state = &mut *channel_state_lock;
30643076
if short_chan_id != 0 {
3065-
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3066-
Some((_cp_id, chan_id)) => chan_id.clone(),
3067-
None => {
3077+
macro_rules! forwarding_channel_not_found {
3078+
() => {
30683079
for forward_info in pending_forwards.drain(..) {
30693080
match forward_info {
30703081
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
@@ -3151,6 +3162,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31513162
}
31523163
}
31533164
}
3165+
}
3166+
}
3167+
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3168+
Some((_cp_id, chan_id)) => chan_id.clone(),
3169+
None => {
3170+
forwarding_channel_not_found!();
31543171
continue;
31553172
}
31563173
};
@@ -3280,7 +3297,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
32803297
});
32813298
}
32823299
} else {
3283-
unreachable!();
3300+
forwarding_channel_not_found!();
3301+
continue;
32843302
}
32853303
} else {
32863304
for forward_info in pending_forwards.drain(..) {
@@ -4198,7 +4216,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
41984216
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
41994217
},
42004218
}
4201-
} else { unreachable!(); }
4219+
} else { return ClaimFundsFromHop::PrevHopForceClosed }
42024220
}
42034221

42044222
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -5118,7 +5136,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
51185136
try_chan_entry!(self, chan.get_mut().channel_update(&msg), chan);
51195137
}
51205138
},
5121-
hash_map::Entry::Vacant(_) => unreachable!()
5139+
hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersist)
51225140
}
51235141
Ok(NotifyOption::DoPersist)
51245142
}

0 commit comments

Comments
 (0)