Skip to content

Commit 697b794

Browse files
committed
Simplify HolderHTLCOutput constructor and track CLTV expiry
This allows us to interrogate a PackageTemplate for the CLTV timelock of the resulting transaction.
1 parent 0ab955f commit 697b794

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,14 +1785,16 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
17851785

17861786
for &(ref htlc, _, _) in holder_tx.htlc_outputs.iter() {
17871787
if let Some(transaction_output_index) = htlc.transaction_output_index {
1788-
let htlc_output = HolderHTLCOutput::build(if !htlc.offered {
1789-
if let Some(preimage) = self.payment_preimages.get(&htlc.payment_hash) {
1790-
Some(preimage.clone())
1788+
let htlc_output = if htlc.offered {
1789+
HolderHTLCOutput::build_offered(htlc.amount_msat, htlc.cltv_expiry)
17911790
} else {
1792-
// We can't build an HTLC-Success transaction without the preimage
1793-
continue;
1794-
}
1795-
} else { None }, htlc.amount_msat);
1791+
HolderHTLCOutput::build_accepted(if let Some(preimage) = self.payment_preimages.get(&htlc.payment_hash) {
1792+
preimage.clone()
1793+
} else {
1794+
// We can't build an HTLC-Success transaction without the preimage
1795+
continue;
1796+
}, htlc.amount_msat)
1797+
};
17961798
let htlc_package = PackageTemplate::build_package(holder_tx.txid, transaction_output_index, PackageSolvingData::HolderHTLCOutput(htlc_output), height, false, height);
17971799
claim_requests.push(htlc_package);
17981800
}

lightning/src/chain/package.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,21 +213,33 @@ impl_writeable_tlv_based!(CounterpartyReceivedHTLCOutput, {
213213
pub(crate) struct HolderHTLCOutput {
214214
preimage: Option<PaymentPreimage>,
215215
amount: u64,
216+
/// Defaults to 0 for HTLC-Success transactions, which have no expiry
217+
cltv_expiry: u32,
216218
}
217219

218220
impl HolderHTLCOutput {
219-
pub(crate) fn build(preimage: Option<PaymentPreimage>, amount: u64) -> Self {
221+
pub(crate) fn build_offered(amount: u64, cltv_expiry: u32) -> Self {
220222
HolderHTLCOutput {
221-
preimage,
222-
amount
223+
preimage: None,
224+
amount,
225+
cltv_expiry,
226+
}
227+
}
228+
229+
pub(crate) fn build_accepted(preimage: PaymentPreimage, amount: u64) -> Self {
230+
HolderHTLCOutput {
231+
preimage: Some(preimage),
232+
amount,
233+
cltv_expiry: 0,
223234
}
224235
}
225236
}
226237

227238
impl_writeable_tlv_based!(HolderHTLCOutput, {
228239
(0, amount),
240+
(2, cltv_expiry),
229241
}, {
230-
(2, preimage),
242+
(4, preimage),
231243
}, {});
232244

233245
/// A struct to describe the channel output on the funding transaction.
@@ -879,7 +891,7 @@ mod tests {
879891
() => {
880892
{
881893
let preimage = PaymentPreimage([2;32]);
882-
PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build(Some(preimage), 0))
894+
PackageSolvingData::HolderHTLCOutput(HolderHTLCOutput::build_accepted(preimage, 0))
883895
}
884896
}
885897
}

0 commit comments

Comments
 (0)