Skip to content

Commit 56491d9

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 f492a19 commit 56491d9

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,14 +1786,17 @@ impl<Signer: Sign> ChannelMonitorImpl<Signer> {
17861786

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

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)