From 56491d91bbc2d73b97e222ccb33b69f8e17cd4a2 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Wed, 26 May 2021 17:17:29 +0000 Subject: [PATCH] Simplify HolderHTLCOutput constructor and track CLTV expiry This allows us to interrogate a PackageTemplate for the CLTV timelock of the resulting transaction. --- lightning/src/chain/channelmonitor.rs | 17 ++++++++++------- lightning/src/chain/package.rs | 22 +++++++++++++++++----- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/lightning/src/chain/channelmonitor.rs b/lightning/src/chain/channelmonitor.rs index e53ce4523..7fc512a5a 100644 --- a/lightning/src/chain/channelmonitor.rs +++ b/lightning/src/chain/channelmonitor.rs @@ -1786,14 +1786,17 @@ impl ChannelMonitorImpl { 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); } diff --git a/lightning/src/chain/package.rs b/lightning/src/chain/package.rs index 9e92e2031..bb4460431 100644 --- a/lightning/src/chain/package.rs +++ b/lightning/src/chain/package.rs @@ -213,21 +213,33 @@ impl_writeable_tlv_based!(CounterpartyReceivedHTLCOutput, { pub(crate) struct HolderHTLCOutput { preimage: Option, amount: u64, + /// Defaults to 0 for HTLC-Success transactions, which have no expiry + cltv_expiry: u32, } impl HolderHTLCOutput { - pub(crate) fn build(preimage: Option, 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)) } } } -- 2.39.5