Skip to content

Commit 9aed28f

Browse files
committed
Fix force_shutdown() bug where we lose knowledge of a preimage
In case we are in AwaitingRemoteRevoke and we go to claim an HTLC (at which point we've already given up the money to the next hop in the payment) we just write it to holding_cell_htlc_updates. However, we should be ensuring we *also* write it to our channel_monitor as we need to make sure we can still claim it after a force_shutdown() or otherwise after hitting the chain.
1 parent 982317a commit 9aed28f

File tree

2 files changed

+57
-49
lines changed

2 files changed

+57
-49
lines changed

src/ln/channel.rs

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use chain::transaction::OutPoint;
2424
use util::{transaction_utils,rng};
2525
use util::sha2::Sha256;
2626

27+
use std;
2728
use std::default::Default;
2829
use std::{cmp,mem};
2930
use std::time::Instant;
@@ -927,7 +928,7 @@ impl Channel {
927928
Ok(our_sig)
928929
}
929930

930-
pub fn get_update_fulfill_htlc(&mut self, payment_preimage_arg: [u8; 32]) -> Result<Option<(msgs::UpdateFulfillHTLC, ChannelMonitor)>, HandleError> {
931+
fn get_update_fulfill_htlc(&mut self, payment_preimage_arg: [u8; 32]) -> Result<(Option<msgs::UpdateFulfillHTLC>, Option<ChannelMonitor>), HandleError> {
931932
// Either ChannelFunded got set (which means it wont bet unset) or there is no way any
932933
// caller thought we could have something claimed (cause we wouldn't have accepted in an
933934
// incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us,
@@ -942,13 +943,31 @@ impl Channel {
942943
let mut payment_hash_calc = [0; 32];
943944
sha.result(&mut payment_hash_calc);
944945

946+
let mut pending_idx = std::usize::MAX;
947+
for (idx, htlc) in self.pending_htlcs.iter().enumerate() {
948+
if !htlc.outbound && htlc.payment_hash == payment_hash_calc {
949+
if pending_idx != std::usize::MAX {
950+
panic!("Duplicate HTLC payment_hash, you probably re-used payment preimages, NEVER DO THIS!");
951+
}
952+
pending_idx = idx;
953+
}
954+
}
955+
if pending_idx == std::usize::MAX {
956+
return Err(HandleError{err: "Unable to find a pending HTLC which matched the given payment preimage", action: None});
957+
}
958+
945959
// Now update local state:
960+
//
961+
// We have to put the payment_preimage in the channel_monitor right away here to ensure we
962+
// can claim it even if the channel hits the chain before we see their next commitment.
963+
self.channel_monitor.provide_payment_preimage(&payment_hash_calc, &payment_preimage_arg);
964+
946965
if (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == (ChannelState::AwaitingRemoteRevoke as u32) {
947966
for pending_update in self.holding_cell_htlc_updates.iter() {
948967
match pending_update {
949968
&HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, .. } => {
950969
if payment_preimage_arg == *payment_preimage {
951-
return Ok(None);
970+
return Ok((None, None));
952971
}
953972
},
954973
&HTLCUpdateAwaitingACK::FailHTLC { ref payment_hash, .. } => {
@@ -962,49 +981,39 @@ impl Channel {
962981
self.holding_cell_htlc_updates.push(HTLCUpdateAwaitingACK::ClaimHTLC {
963982
payment_preimage: payment_preimage_arg, payment_hash: payment_hash_calc,
964983
});
965-
return Ok(None);
966-
}
967-
968-
let mut htlc_id = 0;
969-
let mut htlc_amount_msat = 0;
970-
for htlc in self.pending_htlcs.iter_mut() {
971-
if !htlc.outbound && htlc.payment_hash == payment_hash_calc {
972-
if htlc_id != 0 {
973-
panic!("Duplicate HTLC payment_hash, you probably re-used payment preimages, NEVER DO THIS!");
974-
}
975-
htlc_id = htlc.htlc_id;
976-
htlc_amount_msat += htlc.amount_msat;
977-
if htlc.state == HTLCState::Committed {
978-
htlc.state = HTLCState::LocalRemoved;
979-
htlc.local_removed_fulfilled = true;
980-
} else if htlc.state == HTLCState::RemoteAnnounced {
981-
panic!("Somehow forwarded HTLC prior to remote revocation!");
982-
} else if htlc.state == HTLCState::LocalRemoved || htlc.state == HTLCState::LocalRemovedAwaitingCommitment {
983-
return Err(HandleError{err: "Unable to find a pending HTLC which matched the given payment preimage", action: None});
984-
} else {
985-
panic!("Have an inbound HTLC when not awaiting remote revoke that had a garbage state");
986-
}
984+
return Ok((None, Some(self.channel_monitor.clone())));
985+
}
986+
987+
let htlc_id = {
988+
let mut htlc = &mut self.pending_htlcs[pending_idx];
989+
if htlc.state == HTLCState::Committed {
990+
htlc.state = HTLCState::LocalRemoved;
991+
htlc.local_removed_fulfilled = true;
992+
} else if htlc.state == HTLCState::RemoteAnnounced {
993+
panic!("Somehow forwarded HTLC prior to remote revocation!");
994+
} else if htlc.state == HTLCState::LocalRemoved || htlc.state == HTLCState::LocalRemovedAwaitingCommitment {
995+
return Err(HandleError{err: "Unable to find a pending HTLC which matched the given payment preimage", action: None});
996+
} else {
997+
panic!("Have an inbound HTLC when not awaiting remote revoke that had a garbage state");
987998
}
988-
}
989-
if htlc_amount_msat == 0 {
990-
return Err(HandleError{err: "Unable to find a pending HTLC which matched the given payment preimage", action: None});
991-
}
992-
self.channel_monitor.provide_payment_preimage(&payment_hash_calc, &payment_preimage_arg);
999+
htlc.htlc_id
1000+
};
9931001

994-
Ok(Some((msgs::UpdateFulfillHTLC {
1002+
Ok((Some(msgs::UpdateFulfillHTLC {
9951003
channel_id: self.channel_id(),
9961004
htlc_id: htlc_id,
9971005
payment_preimage: payment_preimage_arg,
998-
}, self.channel_monitor.clone())))
1006+
}), Some(self.channel_monitor.clone())))
9991007
}
10001008

1001-
pub fn get_update_fulfill_htlc_and_commit(&mut self, payment_preimage: [u8; 32]) -> Result<Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned, ChannelMonitor)>, HandleError> {
1009+
pub fn get_update_fulfill_htlc_and_commit(&mut self, payment_preimage: [u8; 32]) -> Result<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, Option<ChannelMonitor>), HandleError> {
10021010
match self.get_update_fulfill_htlc(payment_preimage)? {
1003-
Some(update_fulfill_htlc) => {
1011+
(Some(update_fulfill_htlc), _) => {
10041012
let (commitment, monitor_update) = self.send_commitment_no_status_check()?;
1005-
Ok(Some((update_fulfill_htlc.0, commitment, monitor_update)))
1013+
Ok((Some((update_fulfill_htlc, commitment)), Some(monitor_update)))
10061014
},
1007-
None => Ok(None)
1015+
(None, Some(channel_monitor)) => Ok((None, Some(channel_monitor))),
1016+
(None, None) => Ok((None, None))
10081017
}
10091018
}
10101019

@@ -1491,7 +1500,7 @@ impl Channel {
14911500
},
14921501
&HTLCUpdateAwaitingACK::ClaimHTLC { payment_preimage, .. } => {
14931502
match self.get_update_fulfill_htlc(payment_preimage) {
1494-
Ok(update_fulfill_msg_option) => update_fulfill_htlcs.push(update_fulfill_msg_option.unwrap().0),
1503+
Ok(update_fulfill_msg_option) => update_fulfill_htlcs.push(update_fulfill_msg_option.0.unwrap()),
14951504
Err(e) => {
14961505
err = Some(e);
14971506
}

src/ln/channelmanager.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,20 +1030,19 @@ impl ChannelManager {
10301030
};
10311031

10321032
mem::drop(channel_state);
1033-
match fulfill_msgs {
1034-
Some((msg, commitment_msg, chan_monitor)) => {
1035-
if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
1036-
unimplemented!();// but def dont push the event...
1037-
}
1033+
if let Some(chan_monitor) = fulfill_msgs.1 {
1034+
if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
1035+
unimplemented!();// but def dont push the event...
1036+
}
1037+
}
10381038

1039-
let mut pending_events = self.pending_events.lock().unwrap();
1040-
pending_events.push(events::Event::SendFulfillHTLC {
1041-
node_id: node_id,
1042-
msg,
1043-
commitment_msg,
1044-
});
1045-
},
1046-
None => {},
1039+
if let Some((msg, commitment_msg)) = fulfill_msgs.0 {
1040+
let mut pending_events = self.pending_events.lock().unwrap();
1041+
pending_events.push(events::Event::SendFulfillHTLC {
1042+
node_id: node_id,
1043+
msg,
1044+
commitment_msg,
1045+
});
10471046
}
10481047
true
10491048
},

0 commit comments

Comments
 (0)