Skip to content

Commit 1e7dd6c

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 6dc21d5 commit 1e7dd6c

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
@@ -2304,7 +2304,14 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
23042304
Some((_cp_id, chan_id)) => Some(chan_id.clone()),
23052305
};
23062306
let chan_update_opt = if let Some(forwarding_id) = forwarding_id_opt {
2307-
let chan = channel_state.by_id.get_mut(&forwarding_id).unwrap();
2307+
let chan = match channel_state.by_id.get_mut(&forwarding_id){
2308+
None => {
2309+
// Channel was removed. The short_to_chan_info and by_id maps have
2310+
// no consistency guarantees.
2311+
break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
2312+
},
2313+
Some(chan) => chan
2314+
};
23082315
if !chan.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
23092316
// Note that the behavior here should be identical to the above block - we
23102317
// should NOT reveal the existence or non-existence of a private channel if
@@ -2561,7 +2568,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
25612568
},
25622569
None => { insert_outbound_payment!(); },
25632570
}
2564-
} else { unreachable!(); }
2571+
} else {
2572+
// The channel was likely removed after we fetched the id from the
2573+
// `short_to_chan_info` map, but before we successfully locked the `by_id` map.
2574+
// This can occur as no consistency guarantees exists between the two maps.
2575+
return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()});
2576+
}
25652577
return Ok(());
25662578
};
25672579

@@ -3068,9 +3080,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
30683080
let mut channel_state_lock = self.channel_state.lock().unwrap();
30693081
let channel_state = &mut *channel_state_lock;
30703082
if short_chan_id != 0 {
3071-
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3072-
Some((_cp_id, chan_id)) => chan_id.clone(),
3073-
None => {
3083+
macro_rules! forwarding_channel_not_found {
3084+
() => {
30743085
for forward_info in pending_forwards.drain(..) {
30753086
match forward_info {
30763087
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
@@ -3157,6 +3168,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31573168
}
31583169
}
31593170
}
3171+
}
3172+
}
3173+
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3174+
Some((_cp_id, chan_id)) => chan_id.clone(),
3175+
None => {
3176+
forwarding_channel_not_found!();
31603177
continue;
31613178
}
31623179
};
@@ -3286,7 +3303,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
32863303
});
32873304
}
32883305
} else {
3289-
unreachable!();
3306+
forwarding_channel_not_found!();
3307+
continue;
32903308
}
32913309
} else {
32923310
for forward_info in pending_forwards.drain(..) {
@@ -4204,7 +4222,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42044222
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
42054223
},
42064224
}
4207-
} else { unreachable!(); }
4225+
} else { return ClaimFundsFromHop::PrevHopForceClosed }
42084226
}
42094227

42104228
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -5124,7 +5142,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
51245142
try_chan_entry!(self, chan.get_mut().channel_update(&msg), chan);
51255143
}
51265144
},
5127-
hash_map::Entry::Vacant(_) => unreachable!()
5145+
hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersist)
51285146
}
51295147
Ok(NotifyOption::DoPersist)
51305148
}

0 commit comments

Comments
 (0)