Skip to content

Commit 0b20cf6

Browse files
committed
Simplify handling of OnChainTx.holder_commitment
It is no longer optional since it is available at construction time.
1 parent 63c56a4 commit 0b20cf6

File tree

2 files changed

+70
-85
lines changed

2 files changed

+70
-85
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 41 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -969,7 +969,6 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
969969

970970
let key_derivation_params = keys.key_derivation_params();
971971
let holder_revocation_basepoint = keys.pubkeys().revocation_basepoint;
972-
let mut onchain_tx_handler = OnchainTxHandler::new(destination_script.clone(), keys, channel_parameters.clone());
973972

974973
let secp_ctx = Secp256k1::new();
975974

@@ -991,7 +990,9 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
991990
};
992991
(holder_commitment_tx, trusted_tx.commitment_number())
993992
};
994-
onchain_tx_handler.provide_latest_holder_tx(initial_holder_commitment_tx);
993+
994+
let onchain_tx_handler =
995+
OnchainTxHandler::new(destination_script.clone(), keys, channel_parameters.clone(), initial_holder_commitment_tx);
995996

996997
let mut outputs_to_watch = HashMap::new();
997998
outputs_to_watch.insert(funding_info.0.txid, vec![(funding_info.0.index as u32, funding_info.1.clone())]);
@@ -1725,28 +1726,26 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17251726
pub fn get_latest_holder_commitment_txn<L: Deref>(&mut self, logger: &L) -> Vec<Transaction> where L::Target: Logger {
17261727
log_trace!(logger, "Getting signed latest holder commitment transaction!");
17271728
self.holder_tx_signed = true;
1728-
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript) {
1729-
let txid = commitment_tx.txid();
1730-
let mut res = vec![commitment_tx];
1731-
for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
1732-
if let Some(vout) = htlc.0.transaction_output_index {
1733-
let preimage = if !htlc.0.offered {
1734-
if let Some(preimage) = self.payment_preimages.get(&htlc.0.payment_hash) { Some(preimage.clone()) } else {
1735-
// We can't build an HTLC-Success transaction without the preimage
1736-
continue;
1737-
}
1738-
} else { None };
1739-
if let Some(htlc_tx) = self.onchain_tx_handler.get_fully_signed_htlc_tx(
1740-
&::bitcoin::OutPoint { txid, vout }, &preimage) {
1741-
res.push(htlc_tx);
1729+
let commitment_tx = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript);
1730+
let txid = commitment_tx.txid();
1731+
let mut res = vec![commitment_tx];
1732+
for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
1733+
if let Some(vout) = htlc.0.transaction_output_index {
1734+
let preimage = if !htlc.0.offered {
1735+
if let Some(preimage) = self.payment_preimages.get(&htlc.0.payment_hash) { Some(preimage.clone()) } else {
1736+
// We can't build an HTLC-Success transaction without the preimage
1737+
continue;
17421738
}
1739+
} else { None };
1740+
if let Some(htlc_tx) = self.onchain_tx_handler.get_fully_signed_htlc_tx(
1741+
&::bitcoin::OutPoint { txid, vout }, &preimage) {
1742+
res.push(htlc_tx);
17431743
}
17441744
}
1745-
// 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.
1746-
// The data will be re-generated and tracked in check_spend_holder_transaction if we get a confirmation.
1747-
return res
17481745
}
1749-
Vec::new()
1746+
// 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.
1747+
// The data will be re-generated and tracked in check_spend_holder_transaction if we get a confirmation.
1748+
return res;
17501749
}
17511750

17521751
/// Unsafe test-only version of get_latest_holder_commitment_txn used by our test framework
@@ -1755,26 +1754,24 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17551754
#[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
17561755
pub fn unsafe_get_latest_holder_commitment_txn<L: Deref>(&mut self, logger: &L) -> Vec<Transaction> where L::Target: Logger {
17571756
log_trace!(logger, "Getting signed copy of latest holder commitment transaction!");
1758-
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_copy_holder_tx(&self.funding_redeemscript) {
1759-
let txid = commitment_tx.txid();
1760-
let mut res = vec![commitment_tx];
1761-
for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
1762-
if let Some(vout) = htlc.0.transaction_output_index {
1763-
let preimage = if !htlc.0.offered {
1764-
if let Some(preimage) = self.payment_preimages.get(&htlc.0.payment_hash) { Some(preimage.clone()) } else {
1765-
// We can't build an HTLC-Success transaction without the preimage
1766-
continue;
1767-
}
1768-
} else { None };
1769-
if let Some(htlc_tx) = self.onchain_tx_handler.unsafe_get_fully_signed_htlc_tx(
1770-
&::bitcoin::OutPoint { txid, vout }, &preimage) {
1771-
res.push(htlc_tx);
1757+
let commitment_tx = self.onchain_tx_handler.get_fully_signed_copy_holder_tx(&self.funding_redeemscript);
1758+
let txid = commitment_tx.txid();
1759+
let mut res = vec![commitment_tx];
1760+
for htlc in self.current_holder_commitment_tx.htlc_outputs.iter() {
1761+
if let Some(vout) = htlc.0.transaction_output_index {
1762+
let preimage = if !htlc.0.offered {
1763+
if let Some(preimage) = self.payment_preimages.get(&htlc.0.payment_hash) { Some(preimage.clone()) } else {
1764+
// We can't build an HTLC-Success transaction without the preimage
1765+
continue;
17721766
}
1767+
} else { None };
1768+
if let Some(htlc_tx) = self.onchain_tx_handler.unsafe_get_fully_signed_htlc_tx(
1769+
&::bitcoin::OutPoint { txid, vout }, &preimage) {
1770+
res.push(htlc_tx);
17731771
}
17741772
}
1775-
return res
17761773
}
1777-
Vec::new()
1774+
return res
17781775
}
17791776

17801777
/// Processes transactions in a newly connected block, which may result in any of the following:
@@ -1853,15 +1850,14 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18531850
}
18541851
if should_broadcast {
18551852
self.pending_monitor_events.push(MonitorEvent::CommitmentTxBroadcasted(self.funding_info.0));
1856-
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript) {
1857-
self.holder_tx_signed = true;
1858-
let (mut new_outpoints, _) = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx);
1859-
let new_outputs = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, &commitment_tx);
1860-
if !new_outputs.is_empty() {
1861-
watch_outputs.push((self.current_holder_commitment_tx.txid.clone(), new_outputs));
1862-
}
1863-
claimable_outpoints.append(&mut new_outpoints);
1864-
}
1853+
let commitment_tx = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript);
1854+
self.holder_tx_signed = true;
1855+
let (mut new_outpoints, _) = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx);
1856+
let new_outputs = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, &commitment_tx);
1857+
if !new_outputs.is_empty() {
1858+
watch_outputs.push((self.current_holder_commitment_tx.txid.clone(), new_outputs));
1859+
}
1860+
claimable_outpoints.append(&mut new_outpoints);
18651861
}
18661862
if let Some(events) = self.onchain_events_waiting_threshold_conf.remove(&height) {
18671863
for ev in events {

lightning/src/ln/onchaintx.rs

Lines changed: 29 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use util::byte_utils;
3535
use std::collections::{HashMap, hash_map};
3636
use std::cmp;
3737
use std::ops::Deref;
38+
use std::mem::replace;
3839

3940
const MAX_ALLOC_SIZE: usize = 64*1024;
4041

@@ -241,7 +242,7 @@ impl Writeable for Option<Vec<Option<(usize, Signature)>>> {
241242
/// do RBF bumping if possible.
242243
pub struct OnchainTxHandler<ChanSigner: ChannelKeys> {
243244
destination_script: Script,
244-
holder_commitment: Option<HolderCommitmentTransaction>,
245+
holder_commitment: HolderCommitmentTransaction,
245246
// holder_htlc_sigs and prev_holder_htlc_sigs are in the order as they appear in the commitment
246247
// transaction outputs (hence the Option<>s inside the Vec). The first usize is the index in
247248
// the set of HTLCs in the HolderCommitmentTransaction.
@@ -423,13 +424,13 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::ChanKeySi
423424
}
424425

425426
impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
426-
pub(crate) fn new(destination_script: Script, keys: ChanSigner, channel_parameters: ChannelTransactionParameters) -> Self {
427+
pub(crate) fn new(destination_script: Script, keys: ChanSigner, channel_parameters: ChannelTransactionParameters, holder_commitment: HolderCommitmentTransaction) -> Self {
427428

428429
let key_storage = keys;
429430

430431
OnchainTxHandler {
431432
destination_script,
432-
holder_commitment: None,
433+
holder_commitment,
433434
holder_htlc_sigs: None,
434435
prev_holder_commitment: None,
435436
prev_holder_htlc_sigs: None,
@@ -663,10 +664,10 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
663664
return None;
664665
},
665666
&InputMaterial::Funding { ref funding_redeemscript } => {
666-
let signed_tx = self.get_fully_signed_holder_tx(funding_redeemscript).unwrap();
667+
let signed_tx = self.get_fully_signed_holder_tx(funding_redeemscript);
667668
// Timer set to $NEVER given we can't bump tx without anchor outputs
668669
log_trace!(logger, "Going to broadcast Holder Transaction {} claiming funding output {} from {}...", signed_tx.txid(), outp.vout, outp.txid);
669-
return Some((None, self.holder_commitment.as_ref().unwrap().feerate_per_kw(), signed_tx));
670+
return Some((None, self.holder_commitment.feerate_per_kw(), signed_tx));
670671
}
671672
_ => unreachable!()
672673
}
@@ -904,25 +905,22 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
904905
}
905906

906907
pub(crate) fn provide_latest_holder_tx(&mut self, tx: HolderCommitmentTransaction) {
907-
self.prev_holder_commitment = self.holder_commitment.take();
908+
self.prev_holder_commitment = Some(replace(&mut self.holder_commitment, tx));
908909
self.holder_htlc_sigs = None;
909-
self.holder_commitment = Some(tx);
910910
}
911911

912912
// Normally holder HTLCs are signed at the same time as the holder commitment tx. However,
913913
// in some configurations, the holder commitment tx has been signed and broadcast by a
914914
// ChannelMonitor replica, so we handle that case here.
915915
fn sign_latest_holder_htlcs(&mut self) {
916916
if self.holder_htlc_sigs.is_none() {
917-
if let Some(ref holder_commitment) = self.holder_commitment {
918-
let (_sig, sigs) = self.key_storage.sign_holder_commitment_and_htlcs(holder_commitment, &self.secp_ctx).expect("sign holder commitment");
919-
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, sigs));
920-
}
917+
let (_sig, sigs) = self.key_storage.sign_holder_commitment_and_htlcs(&self.holder_commitment, &self.secp_ctx).expect("sign holder commitment");
918+
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(&self.holder_commitment, sigs));
921919
}
922920
}
923921

924922
// Normally only the latest commitment tx and HTLCs need to be signed. However, in some
925-
// configurations we may have updated our holder commtiment but a replica of the ChannelMonitor
923+
// configurations we may have updated our holder commitment but a replica of the ChannelMonitor
926924
// broadcast the previous one before we sync with it. We handle that case here.
927925
fn sign_prev_holder_htlcs(&mut self) {
928926
if self.prev_holder_htlc_sigs.is_none() {
@@ -947,43 +945,34 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
947945
// have empty holder commitment transaction if a ChannelMonitor is asked to force-close just after Channel::get_outbound_funding_created,
948946
// before providing a initial commitment transaction. For outbound channel, init ChannelMonitor at Channel::funding_signed, there is nothing
949947
// to monitor before.
950-
pub(crate) fn get_fully_signed_holder_tx(&mut self, funding_redeemscript: &Script) -> Option<Transaction> {
951-
if let Some(ref mut holder_commitment) = self.holder_commitment {
952-
let (sig, htlc_sigs) = self.key_storage.sign_holder_commitment_and_htlcs(holder_commitment, &self.secp_ctx).expect("signing holder commitment");
953-
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, htlc_sigs));
954-
Some(holder_commitment.add_holder_sig(funding_redeemscript, sig))
955-
} else {
956-
None
957-
}
948+
pub(crate) fn get_fully_signed_holder_tx(&mut self, funding_redeemscript: &Script) -> Transaction {
949+
let (sig, htlc_sigs) = self.key_storage.sign_holder_commitment_and_htlcs(&self.holder_commitment, &self.secp_ctx).expect("signing holder commitment");
950+
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(&self.holder_commitment, htlc_sigs));
951+
self.holder_commitment.add_holder_sig(funding_redeemscript, sig)
958952
}
959953

960954
#[cfg(any(test, feature="unsafe_revoked_tx_signing"))]
961-
pub(crate) fn get_fully_signed_copy_holder_tx(&mut self, funding_redeemscript: &Script) -> Option<Transaction> {
962-
if let Some(ref mut holder_commitment) = self.holder_commitment {
963-
let (sig, htlc_sigs) = self.key_storage.sign_holder_commitment_and_htlcs(holder_commitment, &self.secp_ctx).expect("sign holder commitment");
964-
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, htlc_sigs));
965-
Some(holder_commitment.add_holder_sig(funding_redeemscript, sig))
966-
} else {
967-
None
968-
}
955+
pub(crate) fn get_fully_signed_copy_holder_tx(&mut self, funding_redeemscript: &Script) -> Transaction {
956+
let (sig, htlc_sigs) = self.key_storage.sign_holder_commitment_and_htlcs(&self.holder_commitment, &self.secp_ctx).expect("sign holder commitment");
957+
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(&self.holder_commitment, htlc_sigs));
958+
self.holder_commitment.add_holder_sig(funding_redeemscript, sig)
969959
}
970960

971961
pub(crate) fn get_fully_signed_htlc_tx(&mut self, outp: &::bitcoin::OutPoint, preimage: &Option<PaymentPreimage>) -> Option<Transaction> {
972962
let mut htlc_tx = None;
973-
if self.holder_commitment.is_some() {
974-
let commitment_txid = self.holder_commitment.as_ref().unwrap().trust().txid();
975-
if commitment_txid == outp.txid {
976-
self.sign_latest_holder_htlcs();
977-
if let &Some(ref htlc_sigs) = &self.holder_htlc_sigs {
978-
let &(ref htlc_idx, ref htlc_sig) = htlc_sigs[outp.vout as usize].as_ref().unwrap();
979-
let holder_commitment = self.holder_commitment.as_ref().unwrap();
980-
let trusted_tx = holder_commitment.trust();
981-
let counterparty_htlc_sig = holder_commitment.counterparty_htlc_sigs[*htlc_idx];
982-
htlc_tx = Some(trusted_tx
983-
.get_signed_htlc_tx(&self.channel_transaction_parameters.as_holder_broadcastable(), *htlc_idx, &counterparty_htlc_sig, htlc_sig, preimage));
984-
}
963+
let commitment_txid = self.holder_commitment.trust().txid();
964+
// Check if the HTLC spends from the current holder commitment
965+
if commitment_txid == outp.txid {
966+
self.sign_latest_holder_htlcs();
967+
if let &Some(ref htlc_sigs) = &self.holder_htlc_sigs {
968+
let &(ref htlc_idx, ref htlc_sig) = htlc_sigs[outp.vout as usize].as_ref().unwrap();
969+
let trusted_tx = self.holder_commitment.trust();
970+
let counterparty_htlc_sig = self.holder_commitment.counterparty_htlc_sigs[*htlc_idx];
971+
htlc_tx = Some(trusted_tx
972+
.get_signed_htlc_tx(&self.channel_transaction_parameters.as_holder_broadcastable(), *htlc_idx, &counterparty_htlc_sig, htlc_sig, preimage));
985973
}
986974
}
975+
// If the HTLC doesn't spend the current holder commitment, check if it spends the previous one
987976
if htlc_tx.is_none() && self.prev_holder_commitment.is_some() {
988977
let commitment_txid = self.prev_holder_commitment.as_ref().unwrap().trust().txid();
989978
if commitment_txid == outp.txid {

0 commit comments

Comments
 (0)