]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Simplify HolderHTLCOutput constructor and track CLTV expiry
authorMatt Corallo <git@bluematt.me>
Wed, 26 May 2021 17:17:29 +0000 (17:17 +0000)
committerMatt Corallo <git@bluematt.me>
Fri, 28 May 2021 23:56:44 +0000 (23:56 +0000)
This allows us to interrogate a PackageTemplate for the CLTV
timelock of the resulting transaction.

lightning/src/chain/channelmonitor.rs
lightning/src/chain/package.rs

index e53ce45234ca35e238624bdf7fd6e770dd03357f..7fc512a5a1859429b265d2b7ffc967b92fec3f4b 100644 (file)
@@ -1786,14 +1786,17 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
 
                for &(ref htlc, _, _) in holder_tx.htlc_outputs.iter() {
                        if let Some(transaction_output_index) = htlc.transaction_output_index {
-                               let htlc_output = HolderHTLCOutput::build(if !htlc.offered {
-                                       if let Some(preimage) = self.payment_preimages.get(&htlc.payment_hash) {
-                                               Some(preimage.clone())
+                               let htlc_output = if htlc.offered {
+                                               HolderHTLCOutput::build_offered(htlc.amount_msat, htlc.cltv_expiry)
                                        } else {
-                                               // We can't build an HTLC-Success transaction without the preimage
-                                               continue;
-                                       }
-                               } else { None }, htlc.amount_msat);
+                                               let payment_preimage = if let Some(preimage) = self.payment_preimages.get(&htlc.payment_hash) {
+                                                       preimage.clone()
+                                               } else {
+                                                       // We can't build an HTLC-Success transaction without the preimage
+                                                       continue;
+                                               };
+                                               HolderHTLCOutput::build_accepted(payment_preimage, htlc.amount_msat)
+                                       };
                                let htlc_package = PackageTemplate::build_package(holder_tx.txid, transaction_output_index, PackageSolvingData::HolderHTLCOutput(htlc_output), height, false, height);
                                claim_requests.push(htlc_package);
                        }
index 9e92e2031bc1afe90c0d90d1c7f2a9322b298521..bb44604317f630f97e911d5cd158b6802851e8b4 100644 (file)
@@ -213,21 +213,33 @@ impl_writeable_tlv_based!(CounterpartyReceivedHTLCOutput, {
 pub(crate) struct HolderHTLCOutput {
        preimage: Option<PaymentPreimage>,
        amount: u64,
+       /// Defaults to 0 for HTLC-Success transactions, which have no expiry
+       cltv_expiry: u32,
 }
 
 impl HolderHTLCOutput {
-       pub(crate) fn build(preimage: Option<PaymentPreimage>, amount: u64) -> Self {
+       pub(crate) fn build_offered(amount: u64, cltv_expiry: u32) -> Self {
                HolderHTLCOutput {
-                       preimage,
-                       amount
+                       preimage: None,
+                       amount,
+                       cltv_expiry,
+               }
+       }
+
+       pub(crate) fn build_accepted(preimage: PaymentPreimage, amount: u64) -> Self {
+               HolderHTLCOutput {
+                       preimage: Some(preimage),
+                       amount,
+                       cltv_expiry: 0,
                }
        }
 }
 
 impl_writeable_tlv_based!(HolderHTLCOutput, {
        (0, amount),
+       (2, cltv_expiry),
 }, {
-       (2, preimage),
+       (4, preimage),
 }, {});
 
 /// A struct to describe the channel output on the funding transaction.
@@ -879,7 +891,7 @@ mod tests {
                () => {
                        {
                                let preimage = PaymentPreimage([2;32]);
-                               PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build(Some(preimage), 0))
+                               PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build_accepted(preimage, 0))
                        }
                }
        }