Skip to content

Commit f89e28f

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 270ce0c commit f89e28f

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
@@ -2274,7 +2274,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22742274
Some((_cp_id, chan_id)) => Some(chan_id.clone()),
22752275
};
22762276
let chan_update_opt = if let Some(forwarding_id) = forwarding_id_opt {
2277-
let chan = channel_state.by_id.get_mut(&forwarding_id).unwrap();
2277+
let chan = match channel_state.by_id.get_mut(&forwarding_id){
2278+
None => {
2279+
// Channel was removed. The short_to_chan_info and by_id maps have
2280+
// no consistency guarantees.
2281+
break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
2282+
},
2283+
Some(chan) => chan
2284+
};
22782285
if !chan.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
22792286
// Note that the behavior here should be identical to the above block - we
22802287
// should NOT reveal the existence or non-existence of a private channel if
@@ -2520,7 +2527,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25202527
},
25212528
None => { insert_outbound_payment!(); },
25222529
}
2523-
} else { unreachable!(); }
2530+
} else {
2531+
return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()});
2532+
}
25242533
return Ok(());
25252534
};
25262535

@@ -3109,9 +3118,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31093118

31103119
for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
31113120
if short_chan_id != 0 {
3112-
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3113-
Some((_cp_id, chan_id)) => chan_id.clone(),
3114-
None => {
3121+
macro_rules! fail_pending_forwards {
3122+
() => {
31153123
for forward_info in pending_forwards.drain(..) {
31163124
match forward_info {
31173125
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
@@ -3198,6 +3206,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31983206
}
31993207
}
32003208
}
3209+
}
3210+
}
3211+
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3212+
Some((_cp_id, chan_id)) => chan_id.clone(),
3213+
None => {
3214+
fail_pending_forwards!();
32013215
continue;
32023216
}
32033217
};
@@ -3324,7 +3338,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33243338
});
33253339
}
33263340
} else {
3327-
unreachable!();
3341+
fail_pending_forwards!();
3342+
continue;
33283343
}
33293344
} else {
33303345
for forward_info in pending_forwards.drain(..) {
@@ -4263,7 +4278,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42634278
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
42644279
},
42654280
}
4266-
} else { unreachable!(); }
4281+
} else { return ClaimFundsFromHop::PrevHopForceClosed }
42674282
}
42684283

42694284
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -5174,7 +5189,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
51745189
try_chan_entry!(self, chan.get_mut().channel_update(&msg), chan);
51755190
}
51765191
},
5177-
hash_map::Entry::Vacant(_) => unreachable!()
5192+
hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersist)
51785193
}
51795194
Ok(NotifyOption::DoPersist)
51805195
}

0 commit comments

Comments
 (0)