+ pub fn get_update_fail_htlc(&mut self, payment_hash: &[u8; 32], err_packet: msgs::OnionErrorPacket) -> Result<msgs::UpdateFailHTLC, HandleError> {
+ if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
+ return Err(HandleError{err: "Was asked to fail an HTLC when channel was not in an operational state", msg: None});
+ }
+
+ let mut htlc_id = 0;
+ let mut htlc_amount_msat = 0;
+ //TODO: swap_remove since we dont need to maintain ordering here
+ self.pending_htlcs.retain(|ref htlc| {
+ if !htlc.outbound && htlc.payment_hash == *payment_hash {
+ if htlc_id != 0 {
+ panic!("Duplicate HTLC payment_hash, you probably re-used payment preimages, NEVER DO THIS!");
+ }
+ htlc_id = htlc.htlc_id;
+ htlc_amount_msat += htlc.amount_msat;
+ false
+ } else { true }
+ });
+ if htlc_amount_msat == 0 {
+ return Err(HandleError{err: "Unable to find a pending HTLC which matched the given payment preimage", msg: None});
+ }
+
+ //TODO: This is racy af, they may have pending messages in flight to us that will not have
+ //received this yet!
+
+ Ok(msgs::UpdateFailHTLC {
+ channel_id: self.channel_id(),
+ htlc_id,
+ reason: err_packet
+ })
+ }
+