From 82ddc2e2e659c03a0c66148cc87d87f4411569c1 Mon Sep 17 00:00:00 2001 From: Justin Moeller Date: Thu, 7 Nov 2024 17:13:03 -0600 Subject: [PATCH] fix: LNv1 add min_cltv_delta to timelock constant --- devimint/src/external.rs | 11 ++++++++++- modules/fedimint-ln-client/src/lib.rs | 7 ++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/devimint/src/external.rs b/devimint/src/external.rs index 00fc955fef0..984c0e3cead 100644 --- a/devimint/src/external.rs +++ b/devimint/src/external.rs @@ -36,7 +36,7 @@ use crate::util::{ poll, poll_with_timeout, ClnLightningCli, GatewayClnExtension, ProcessHandle, ProcessManager, }; use crate::vars::utf8; -use crate::version_constants::VERSION_0_4_0_ALPHA; +use crate::version_constants::{VERSION_0_4_0_ALPHA, VERSION_0_5_0_ALPHA}; use crate::{cmd, poll_eq, Gatewayd}; #[derive(Clone)] @@ -776,12 +776,21 @@ impl Lnd { bitcoin::hashes::HashEngine::input(&mut engine, &preimage); bitcoin::hashes::sha256::Hash::from_engine(engine) }; + // TODO(support:v0.5): LNv1 cannot pay HOLD invoices with a CLTV expiry greater + // than 500 before v0.5 + let fedimint_cli_version = crate::util::FedimintCli::version_or_default().await; + let cltv_expiry = if fedimint_cli_version >= *VERSION_0_5_0_ALPHA { + 650 + } else { + 100 + }; let hold_request = self .invoices_client_lock() .await? .add_hold_invoice(tonic_lnd::invoicesrpc::AddHoldInvoiceRequest { value_msat: amount as i64, hash: hash.to_byte_array().to_vec(), + cltv_expiry, ..Default::default() }) .await? diff --git a/modules/fedimint-ln-client/src/lib.rs b/modules/fedimint-ln-client/src/lib.rs index 173c832603f..9f4e9681a69 100644 --- a/modules/fedimint-ln-client/src/lib.rs +++ b/modules/fedimint-ln-client/src/lib.rs @@ -711,7 +711,12 @@ impl LightningClientModule { .fetch_consensus_block_count() .await? .ok_or(format_err!("Cannot get consensus block count"))?; - let absolute_timelock = consensus_count + OUTGOING_LN_CONTRACT_TIMELOCK - 1; + + // Add the timelock to the current block count and the invoice's + // `min_cltv_delta` + let min_final_cltv = invoice.min_final_cltv_expiry_delta(); + let absolute_timelock = + consensus_count + min_final_cltv + OUTGOING_LN_CONTRACT_TIMELOCK - 1; // Compute amount to lock in the outgoing contract let invoice_amount = Amount::from_msats(