Skip to content

Commit 919ec65

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 1891450 commit 919ec65

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
@@ -2271,7 +2271,14 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
22712271
Some((_cp_id, chan_id)) => Some(chan_id.clone()),
22722272
};
22732273
let chan_update_opt = if let Some(forwarding_id) = forwarding_id_opt {
2274-
let chan = channel_state.by_id.get_mut(&forwarding_id).unwrap();
2274+
let chan = match channel_state.by_id.get_mut(&forwarding_id){
2275+
None => {
2276+
// Channel was removed. The short_to_chan_info and by_id maps have
2277+
// no consistency guarantees.
2278+
break Some(("Don't have available channel for forwarding as requested.", 0x4000 | 10, None));
2279+
},
2280+
Some(chan) => chan
2281+
};
22752282
if !chan.should_announce() && !self.default_configuration.accept_forwards_to_priv_channels {
22762283
// Note that the behavior here should be identical to the above block - we
22772284
// should NOT reveal the existence or non-existence of a private channel if
@@ -2517,7 +2524,9 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
25172524
},
25182525
None => { insert_outbound_payment!(); },
25192526
}
2520-
} else { unreachable!(); }
2527+
} else {
2528+
return Err(APIError::ChannelUnavailable{err: "No channel available with first hop!".to_owned()});
2529+
}
25212530
return Ok(());
25222531
};
25232532

@@ -3106,9 +3115,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31063115

31073116
for (short_chan_id, mut pending_forwards) in channel_state.forward_htlcs.drain() {
31083117
if short_chan_id != 0 {
3109-
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3110-
Some((_cp_id, chan_id)) => chan_id.clone(),
3111-
None => {
3118+
macro_rules! fail_pending_forwards {
3119+
() => {
31123120
for forward_info in pending_forwards.drain(..) {
31133121
match forward_info {
31143122
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
@@ -3195,6 +3203,12 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31953203
}
31963204
}
31973205
}
3206+
}
3207+
}
3208+
let forward_chan_id = match self.short_to_chan_info.read().unwrap().get(&short_chan_id) {
3209+
Some((_cp_id, chan_id)) => chan_id.clone(),
3210+
None => {
3211+
fail_pending_forwards!();
31983212
continue;
31993213
}
32003214
};
@@ -3321,7 +3335,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
33213335
});
33223336
}
33233337
} else {
3324-
unreachable!();
3338+
fail_pending_forwards!();
3339+
continue;
33253340
}
33263341
} else {
33273342
for forward_info in pending_forwards.drain(..) {
@@ -4250,7 +4265,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
42504265
return ClaimFundsFromHop::MonitorUpdateFail(counterparty_node_id, res, None);
42514266
},
42524267
}
4253-
} else { unreachable!(); }
4268+
} else { return ClaimFundsFromHop::PrevHopForceClosed }
42544269
}
42554270

42564271
fn finalize_claims(&self, mut sources: Vec<HTLCSource>) {
@@ -5160,7 +5175,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
51605175
try_chan_entry!(self, chan.get_mut().channel_update(&msg), chan);
51615176
}
51625177
},
5163-
hash_map::Entry::Vacant(_) => unreachable!()
5178+
hash_map::Entry::Vacant(_) => return Ok(NotifyOption::SkipPersist)
51645179
}
51655180
Ok(NotifyOption::DoPersist)
51665181
}

0 commit comments

Comments
 (0)