Skip to content

Commit a7a6eb8

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 abf1e72 commit a7a6eb8

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
@@ -2277,7 +2277,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22772277
Some((_cp_id, chan_id)) => Some(chan_id.clone()),
22782278
};
22792279
let chan_update_opt = if let Some(forwarding_id) = forwarding_id_opt {
2280-
let chan = channel_state.by_id.get_mut(&forwarding_id).unwrap();
2280+
let chan = match channel_state.by_id.get_mut(&forwarding_id){
2281+
None => {
2282+
// Channel was removed. The short_to_chan_info and by_id maps have
2283+
// no consistency guarantees.
2284+
break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
2285+
},
2286+
Some(chan) => chan
2287+
};
22812288
if !chan.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
22822289
// Note that the behavior here should be identical to the above block - we
22832290
// should NOT reveal the existence or non-existence of a private channel if
@@ -2523,7 +2530,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25232530
},
25242531
None => { insert_outbound_payment!(); },
25252532
}
2526-
} else { unreachable!(); }
2533+
} else {
2534+
return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()});
2535+
}
25272536
return Ok(());
25282537
};
25292538

@@ -3112,9 +3121,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31123121

31133122
for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
31143123
if short_chan_id != 0 {
3115-
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3116-
Some((_cp_id, chan_id)) => chan_id.clone(),
3117-
None => {
3124+
macro_rules! fail_pending_forwards {
3125+
() => {
31183126
for forward_info in pending_forwards.drain(..) {
31193127
match forward_info {
31203128
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
@@ -3201,6 +3209,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32013209
}
32023210
}
32033211
}
3212+
}
3213+
}
3214+
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3215+
Some((_cp_id, chan_id)) => chan_id.clone(),
3216+
None => {
3217+
fail_pending_forwards!();
32043218
continue;
32053219
}
32063220
};
@@ -3327,7 +3341,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33273341
});
33283342
}
33293343
} else {
3330-
unreachable!();
3344+
fail_pending_forwards!();
3345+
continue;
33313346
}
33323347
} else {
33333348
for forward_info in pending_forwards.drain(..) {
@@ -4266,7 +4281,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42664281
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
42674282
},
42684283
}
4269-
} else { unreachable!(); }
4284+
} else { return ClaimFundsFromHop::PrevHopForceClosed }
42704285
}
42714286

42724287
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -5177,7 +5192,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
51775192
try_chan_entry!(self, chan.get_mut().channel_update(&msg), chan);
51785193
}
51795194
},
5180-
hash_map::Entry::Vacant(_) => unreachable!()
5195+
hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersist)
51815196
}
51825197
Ok(NotifyOption::DoPersist)
51835198
}

0 commit comments

Comments
 (0)