Remove redundant payment preimag hashing in HTLC claim pipeline 2023-08-earlier-payment-hash-log
authorMatt Corallo <git@bluematt.me>
Wed, 23 Aug 2023 03:30:56 +0000 (03:30 +0000)
committerMatt Corallo <git@bluematt.me>
Wed, 23 Aug 2023 16:45:15 +0000 (16:45 +0000)
Currently, when we receive an HTLC claim from a peer, we first hash
the preimage they gave us before removing the HTLC, then
immediately pass the preimage to the inbound channel and hash the
preimage again before removing the HTLC and sending our peer an
`update_fulfill_htlc`. This second hash is actually only asserted
on, never used in any meaningful way as we have the htlc data
present in the same code.

Here we simply drop this second hash and move it into a
`debug_assert`.

lightning/src/ln/channel.rs

index d1479dd1bca575a5d9f1bbf977c5a56952e24d92..691ce3425dae94316dafac7d59e2fd6f5ff73277 100644 (file)
@@ -2206,8 +2206,6 @@ impl<SP: Deref> Channel<SP> where
                }
                assert_eq!(self.context.channel_state & ChannelState::ShutdownComplete as u32, 0);
 
-               let payment_hash_calc = PaymentHash(Sha256::hash(&payment_preimage_arg.0[..]).into_inner());
-
                // ChannelManager may generate duplicate claims/fails due to HTLC update events from
                // on-chain ChannelsMonitors during block rescan. Ideally we'd figure out a way to drop
                // these, but for now we just have to treat them as normal.
@@ -2216,7 +2214,7 @@ impl<SP: Deref> Channel<SP> where
                let mut htlc_value_msat = 0;
                for (idx, htlc) in self.context.pending_inbound_htlcs.iter().enumerate() {
                        if htlc.htlc_id == htlc_id_arg {
-                               assert_eq!(htlc.payment_hash, payment_hash_calc);
+                               debug_assert_eq!(htlc.payment_hash, PaymentHash(Sha256::hash(&payment_preimage_arg.0[..]).into_inner()));
                                log_debug!(logger, "Claiming inbound HTLC id {} with payment hash {} with preimage {}",
                                        htlc.htlc_id, htlc.payment_hash, payment_preimage_arg);
                                match htlc.state {