Skip to content

Commit 5c32e3c

Browse files
committed
Clean up forward_/claimable_htlcs handling and document consistency
1 parent 9aed28f commit 5c32e3c

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/ln/channelmanager.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,19 @@ struct ChannelHolder {
115115
short_to_id: HashMap<u64, [u8; 32]>,
116116
next_forward: Instant,
117117
/// short channel id -> forward infos. Key of 0 means payments received
118+
/// Note that while this is held in the same mutex as the channels themselves, no consistency
119+
/// guarantees are made about there existing a channel with the short id here, nor the short
120+
/// ids in the PendingForwardHTLCInfo!
118121
forward_htlcs: HashMap<u64, Vec<PendingForwardHTLCInfo>>,
122+
/// Note that while this is held in the same mutex as the channels themselves, no consistency
123+
/// guarantees are made about the channels given here actually existing anymore by the time you
124+
/// go to read them!
119125
claimable_htlcs: HashMap<[u8; 32], PendingOutboundHTLC>,
120126
}
121127
struct MutChannelHolder<'a> {
122128
by_id: &'a mut HashMap<[u8; 32], Channel>,
123129
short_to_id: &'a mut HashMap<u64, [u8; 32]>,
124130
next_forward: &'a mut Instant,
125-
/// short channel id -> forward infos. Key of 0 means payments received
126131
forward_htlcs: &'a mut HashMap<u64, Vec<PendingForwardHTLCInfo>>,
127132
claimable_htlcs: &'a mut HashMap<[u8; 32], PendingOutboundHTLC>,
128133
}
@@ -884,6 +889,12 @@ impl ChannelManager {
884889
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 15, data: Vec::new() })
885890
}
886891

892+
/// Fails an HTLC backwards to the sender of it to us.
893+
/// Note that while we take a channel_state lock as input, we do *not* assume consistency here.
894+
/// There are several callsites that do stupid things like loop over a list of payment_hashes
895+
/// to fail and take the channel_state lock for each iteration (as we take ownership and may
896+
/// drop it). In other words, no assumptions are made that entries in claimable_htlcs point to
897+
/// still-available channels.
887898
fn fail_htlc_backwards_internal(&self, mut channel_state: MutexGuard<ChannelHolder>, payment_hash: &[u8; 32], onion_error: HTLCFailReason) -> bool {
888899
let mut pending_htlc = {
889900
match channel_state.claimable_htlcs.remove(payment_hash) {
@@ -904,7 +915,7 @@ impl ChannelManager {
904915
}
905916

906917
match pending_htlc {
907-
PendingOutboundHTLC::CycledRoute { .. } => { panic!("WAT"); },
918+
PendingOutboundHTLC::CycledRoute { .. } => unreachable!(),
908919
PendingOutboundHTLC::OutboundRoute { .. } => {
909920
mem::drop(channel_state);
910921

@@ -1000,7 +1011,7 @@ impl ChannelManager {
10001011
}
10011012

10021013
match pending_htlc {
1003-
PendingOutboundHTLC::CycledRoute { .. } => { panic!("WAT"); },
1014+
PendingOutboundHTLC::CycledRoute { .. } => unreachable!(),
10041015
PendingOutboundHTLC::OutboundRoute { .. } => {
10051016
if from_user {
10061017
panic!("Called claim_funds with a preimage for an outgoing payment. There is nothing we can do with this, and something is seriously wrong if you knew this...");
@@ -1016,13 +1027,20 @@ impl ChannelManager {
10161027
let (node_id, fulfill_msgs) = {
10171028
let chan_id = match channel_state.short_to_id.get(&source_short_channel_id) {
10181029
Some(chan_id) => chan_id.clone(),
1019-
None => return false
1030+
None => {
1031+
// TODO: There is probably a channel manager somewhere that needs to
1032+
// learn the preimage as the channel already hit the chain and that's
1033+
// why its missing.
1034+
return false
1035+
}
10201036
};
10211037

10221038
let chan = channel_state.by_id.get_mut(&chan_id).unwrap();
10231039
match chan.get_update_fulfill_htlc_and_commit(payment_preimage) {
10241040
Ok(msg) => (chan.get_their_node_id(), msg),
10251041
Err(_e) => {
1042+
// TODO: There is probably a channel manager somewhere that needs to
1043+
// learn the preimage as the channel may be about to hit the chain.
10261044
//TODO: Do something with e?
10271045
return false;
10281046
},
@@ -1571,7 +1589,7 @@ impl ChannelMessageHandler for ChannelManager {
15711589
pending_forward_info.prev_short_channel_id = short_channel_id;
15721590
(short_channel_id, chan.update_add_htlc(&msg, pending_forward_info)?)
15731591
},
1574-
None => return Err(HandleError{err: "Failed to find corresponding channel", action: None}), //TODO: panic?
1592+
None => return Err(HandleError{err: "Failed to find corresponding channel", action: None}),
15751593
};
15761594

15771595
match claimable_htlcs_entry {
@@ -1581,7 +1599,7 @@ impl ChannelMessageHandler for ChannelManager {
15811599
&mut PendingOutboundHTLC::OutboundRoute { ref route, ref session_priv } => {
15821600
(route.clone(), session_priv.clone())
15831601
},
1584-
_ => { panic!("WAT") },
1602+
_ => unreachable!(),
15851603
};
15861604
*outbound_route = PendingOutboundHTLC::CycledRoute {
15871605
source_short_channel_id,

0 commit comments

Comments
 (0)