Adapt Channel::fail_htlc for failing with malformed OR update_fail_htlc.
authorValentine Wallace <vwallace@protonmail.com>
Mon, 4 Dec 2023 20:26:30 +0000 (15:26 -0500)
committerValentine Wallace <vwallace@protonmail.com>
Tue, 12 Dec 2023 23:38:58 +0000 (18:38 -0500)
Useful for failing blinded payments back with malformed, and will also be
useful in the future when we move onion decoding into
process_pending_htlc_forwards, after which Channel::fail_htlc will be used for
all malformed htlcs.

lightning/src/ln/channel.rs

index 44b09a861ff53c944e8d90fd46787fd0c3f31e0b..a991c8eeda13fbe29a35cb9b8845dccea202ae09 100644 (file)
@@ -2543,6 +2543,29 @@ impl FailHTLCContents for msgs::OnionErrorPacket {
                HTLCUpdateAwaitingACK::FailHTLC { htlc_id, err_packet: self }
        }
 }
+impl FailHTLCContents for (u16, [u8; 32]) {
+       type Message = msgs::UpdateFailMalformedHTLC; // (failure_code, sha256_of_onion)
+       fn to_message(self, htlc_id: u64, channel_id: ChannelId) -> Self::Message {
+               msgs::UpdateFailMalformedHTLC {
+                       htlc_id,
+                       channel_id,
+                       failure_code: self.0,
+                       sha256_of_onion: self.1
+               }
+       }
+       fn to_inbound_htlc_state(self) -> InboundHTLCState {
+               InboundHTLCState::LocalRemoved(
+                       InboundHTLCRemovalReason::FailMalformed((self.1, self.0))
+               )
+       }
+       fn to_htlc_update_awaiting_ack(self, htlc_id: u64) -> HTLCUpdateAwaitingACK {
+               HTLCUpdateAwaitingACK::FailMalformedHTLC {
+                       htlc_id,
+                       failure_code: self.0,
+                       sha256_of_onion: self.1
+               }
+       }
+}
 
 trait FailHTLCMessageName {
        fn name() -> &'static str;
@@ -2552,6 +2575,11 @@ impl FailHTLCMessageName for msgs::UpdateFailHTLC {
                "update_fail_htlc"
        }
 }
+impl FailHTLCMessageName for msgs::UpdateFailMalformedHTLC {
+       fn name() -> &'static str {
+               "update_fail_malformed_htlc"
+       }
+}
 
 impl<SP: Deref> Channel<SP> where
        SP::Target: SignerProvider,
@@ -3598,8 +3626,19 @@ impl<SP: Deref> Channel<SP> where
                                                        }
                                                }
                                        },
-                                       &HTLCUpdateAwaitingACK::FailMalformedHTLC { .. } => {
-                                               todo!()
+                                       &HTLCUpdateAwaitingACK::FailMalformedHTLC { htlc_id, failure_code, sha256_of_onion } => {
+                                               match self.fail_htlc(htlc_id, (failure_code, sha256_of_onion), false, logger) {
+                                                       Ok(update_fail_malformed_opt) => {
+                                                               debug_assert!(update_fail_malformed_opt.is_some()); // See above comment
+                                                               update_fail_count += 1;
+                                                       },
+                                                       Err(e) => {
+                                                               if let ChannelError::Ignore(_) = e {}
+                                                               else {
+                                                                       panic!("Got a non-IgnoreError action trying to fail holding cell HTLC");
+                                                               }
+                                                       }
+                                               }
                                        },
                                }
                        }