Skip to content

Commit a75d425

Browse files
author
Antoine Riard
committed
Add OnchainTxHandler::get_fully_signed_htlc
In case of channel force-closure, access to local commitment transactions and its dependent HTLCs is needed. Instead of using broadcast_by_local_state which registers outpoint to claim and outputs to watch which are going to be discarded in this case, we simply ask OnchainTxHandler to build and sign HTLC transactions through new API.
1 parent eb81834 commit a75d425

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

lightning/src/ln/channelmonitor.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,10 +1812,17 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18121812
pub fn get_latest_local_commitment_txn(&mut self) -> Vec<Transaction> {
18131813
log_trace!(self, "Getting signed latest local commitment transaction!");
18141814
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_local_tx(self.channel_value_satoshis.unwrap()) {
1815+
let txid = commitment_tx.txid();
18151816
let mut res = vec![commitment_tx];
18161817
if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
1817-
let mut htlc_txn = self.broadcast_by_local_state(res.get(0).unwrap(), local_tx).0;
1818-
res.append(&mut htlc_txn);
1818+
for htlc in local_tx.htlc_outputs.iter() {
1819+
if let Some(htlc_index) = htlc.0.transaction_output_index {
1820+
let preimage = if let Some(preimage) = self.payment_preimages.get(&htlc.0.payment_hash) { Some(*preimage) } else { None };
1821+
if let Some(htlc_tx) = self.onchain_tx_handler.get_fully_signed_htlc_tx(txid, htlc_index, preimage) {
1822+
res.push(htlc_tx);
1823+
}
1824+
}
1825+
}
18191826
// We throw away the generated waiting_first_conf data as we aren't (yet) confirmed and we don't actually know what the caller wants to do.
18201827
// The data will be re-generated and tracked in check_spend_local_transaction if we get a confirmation.
18211828
}

lightning/src/ln/onchaintx.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use secp256k1;
1515

1616
use ln::msgs::DecodeError;
1717
use ln::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER, InputMaterial, ClaimRequest};
18-
use ln::channelmanager::HTLCSource;
18+
use ln::channelmanager::{HTLCSource, PaymentPreimage};
1919
use ln::chan_utils;
2020
use ln::chan_utils::{HTLCType, LocalCommitmentTransaction, TxCreationKeys, HTLCOutputInCommitment};
2121
use chain::chaininterface::{FeeEstimator, BroadcasterInterface, ConfirmationTarget, MIN_RELAY_FEE_SAT_PER_1000_WEIGHT};
@@ -872,4 +872,23 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
872872
self.key_storage.sign_local_commitment(&mut local_commitment, &self.funding_redeemscript, channel_value_satoshis, &self.secp_ctx);
873873
return Some(local_commitment.with_valid_witness().clone());
874874
}
875+
876+
pub(super) fn get_fully_signed_htlc_tx(&mut self, txid: Sha256dHash, htlc_index: u32, preimage: Option<PaymentPreimage>) -> Option<Transaction> {
877+
//TODO: store preimage in OnchainTxHandler
878+
if let Some(ref local_commitment) = self.local_commitment {
879+
if local_commitment.txid() == txid {
880+
if let Some(ref htlc_cache) = self.current_htlc_cache {
881+
if let Some(htlc) = htlc_cache.per_htlc.get(&htlc_index) {
882+
if !htlc.0.offered && preimage.is_none() { return None; }; // If we don't have preimage for HTLC-Success, don't try to generate
883+
let htlc_secret = if !htlc.0.offered { preimage } else { None }; // If we have a preimage for a HTLC-Timeout, don't use it that's likely a duplicate HTLC hash
884+
let mut htlc_tx = chan_utils::build_htlc_transaction(&txid, htlc_cache.feerate_per_kw, self.local_csv, &htlc.0, &htlc_cache.local_keys.a_delayed_payment_key, &htlc_cache.local_keys.revocation_key);
885+
self.key_storage.sign_htlc_transaction(&mut htlc_tx, htlc.1.as_ref().unwrap(), &htlc_secret, &htlc.0, &htlc_cache.local_keys.a_htlc_key, &htlc_cache.local_keys.b_htlc_key, &htlc_cache.local_keys.revocation_key, &htlc_cache.local_keys.per_commitment_point, &self.secp_ctx);
886+
return Some(htlc_tx);
887+
888+
}
889+
}
890+
}
891+
}
892+
None
893+
}
875894
}

0 commit comments

Comments
 (0)