Skip to content

Commit f84c008

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 aff9a34 commit f84c008

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
@@ -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,9 @@ 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+
return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()});
2573+
}
25652574
return Ok(());
25662575
};
25672576

@@ -3068,9 +3077,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
30683077
let mut channel_state_lock = self.channel_state.lock().unwrap();
30693078
let channel_state = &mut *channel_state_lock;
30703079
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 => {
3080+
macro_rules! fail_pending_forwards {
3081+
() => {
30743082
for forward_info in pending_forwards.drain(..) {
30753083
match forward_info {
30763084
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
@@ -3157,6 +3165,12 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
31573165
}
31583166
}
31593167
}
3168+
}
3169+
}
3170+
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3171+
Some((_cp_id, chan_id)) => chan_id.clone(),
3172+
None => {
3173+
fail_pending_forwards!();
31603174
continue;
31613175
}
31623176
};
@@ -3286,7 +3300,8 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
32863300
});
32873301
}
32883302
} else {
3289-
unreachable!();
3303+
fail_pending_forwards!();
3304+
continue;
32903305
}
32913306
} else {
32923307
for forward_info in pending_forwards.drain(..) {
@@ -4204,7 +4219,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
42044219
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
42054220
},
42064221
}
4207-
} else { unreachable!(); }
4222+
} else { return ClaimFundsFromHop::PrevHopForceClosed }
42084223
}
42094224

42104225
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -5124,7 +5139,7 @@ impl<M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelManager<M, T, K, F
51245139
try_chan_entry!(self, chan.get_mut().channel_update(&msg), chan);
51255140
}
51265141
},
5127-
hash_map::Entry::Vacant(_) => unreachable!()
5142+
hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersist)
51285143
}
51295144
Ok(NotifyOption::DoPersist)
51305145
}

0 commit comments

Comments
 (0)