+
+ /// Check if any transaction broadcasted is resolving HTLC output by a success or timeout on a local
+ /// or remote commitment tx, if so send back the source, preimage if found and payment_hash of resolved HTLC
+ fn is_resolving_htlc_output(&mut self, tx: &Transaction) -> Vec<(HTLCSource, Option<PaymentPreimage>, PaymentHash)> {
+ let mut htlc_updated = Vec::new();
+
+ 'outer_loop: for input in &tx.input {
+ let mut payment_data = None;
+
+ macro_rules! scan_commitment {
+ ($htlc_outputs: expr, $htlc_sources: expr) => {
+ for &(ref payment_hash, ref source, ref vout) in $htlc_sources.iter() {
+ if &Some(input.previous_output.vout) == vout {
+ payment_data = Some((source.clone(), *payment_hash));
+ }
+ }
+ if payment_data.is_none() {
+ for htlc_output in $htlc_outputs {
+ if input.previous_output.vout == htlc_output.transaction_output_index {
+ log_info!(self, "Inbound HTLC timeout at {} from {} resolved by {}", input.previous_output.vout, input.previous_output.txid, tx.txid());
+ continue 'outer_loop;
+ }
+ }
+ }
+ }
+ }
+
+ if let Some(ref current_local_signed_commitment_tx) = self.current_local_signed_commitment_tx {
+ if input.previous_output.txid == current_local_signed_commitment_tx.txid {
+ scan_commitment!(current_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a), current_local_signed_commitment_tx.htlc_sources);
+ }
+ }
+ if let Some(ref prev_local_signed_commitment_tx) = self.prev_local_signed_commitment_tx {
+ if input.previous_output.txid == prev_local_signed_commitment_tx.txid {
+ scan_commitment!(prev_local_signed_commitment_tx.htlc_outputs.iter().map(|&(ref a, _, _)| a), prev_local_signed_commitment_tx.htlc_sources);
+ }
+ }
+ if let Some(&(ref htlc_outputs, ref htlc_sources)) = self.remote_claimable_outpoints.get(&input.previous_output.txid) {
+ scan_commitment!(htlc_outputs, htlc_sources);
+ }
+
+ // If tx isn't solving htlc output from local/remote commitment tx and htlc isn't outbound we don't need
+ // to broadcast solving backward
+ if let Some((source, payment_hash)) = payment_data {
+ let mut payment_preimage = PaymentPreimage([0; 32]);
+ if input.witness.len() == 5 && input.witness[4].len() == ACCEPTED_HTLC_SCRIPT_WEIGHT {
+ payment_preimage.0.copy_from_slice(&tx.input[0].witness[3]);
+ htlc_updated.push((source, Some(payment_preimage), payment_hash));
+ } else if input.witness.len() == 3 && input.witness[2].len() == OFFERED_HTLC_SCRIPT_WEIGHT {
+ payment_preimage.0.copy_from_slice(&tx.input[0].witness[1]);
+ htlc_updated.push((source, Some(payment_preimage), payment_hash));
+ } else {
+ htlc_updated.push((source, None, payment_hash));
+ }
+ }
+ }
+ htlc_updated
+ }