Skip to content

Commit e70f485

Browse files
Split channelmonitor's broadcast_by_holder_state
Now callers will separately retrieve the claim requests/ holder revokable script and the new watched holder outputs. This will be used in the next commit for times when we need to get holder claim requests, but don't have access to the holder commitment transaction.
1 parent a3e4f9c commit e70f485

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,9 +1525,11 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15251525
(claimable_outpoints, Some((htlc_txid, outputs)))
15261526
}
15271527

1528-
fn broadcast_by_holder_state(&self, commitment_tx: &Transaction, holder_tx: &HolderSignedTx) -> (Vec<ClaimRequest>, Vec<(u32, TxOut)>, Option<(Script, PublicKey, PublicKey)>) {
1528+
// Returns (1) `ClaimRequest`s that can be given to the OnChainTxHandler, so that the handler can
1529+
// broadcast transactions claiming holder HTLC commitment outputs and (2) a holder revokable
1530+
// script so we can detect whether a holder transaction has been seen on-chain.
1531+
fn get_broadcasted_holder_claims(&self, holder_tx: &HolderSignedTx) -> (Vec<ClaimRequest>, Option<(Script, PublicKey, PublicKey)>) {
15291532
let mut claim_requests = Vec::with_capacity(holder_tx.htlc_outputs.len());
1530-
let mut watch_outputs = Vec::with_capacity(holder_tx.htlc_outputs.len());
15311533

15321534
let redeemscript = chan_utils::get_revokeable_redeemscript(&holder_tx.revocation_key, self.on_holder_tx_csv, &holder_tx.delayed_payment_key);
15331535
let broadcasted_holder_revokable_script = Some((redeemscript.to_v0_p2wsh(), holder_tx.per_commitment_point.clone(), holder_tx.revocation_key.clone()));
@@ -1546,11 +1548,21 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15461548
} else { None },
15471549
amount: htlc.amount_msat,
15481550
}});
1549-
watch_outputs.push((transaction_output_index, commitment_tx.output[transaction_output_index as usize].clone()));
15501551
}
15511552
}
15521553

1553-
(claim_requests, watch_outputs, broadcasted_holder_revokable_script)
1554+
(claim_requests, broadcasted_holder_revokable_script)
1555+
}
1556+
1557+
// Returns holder HTLC outputs to watch and react to in case of spending.
1558+
fn get_broadcasted_holder_watch_outputs(&self, holder_tx: &HolderSignedTx, commitment_tx: &Transaction) -> Vec<(u32, TxOut)> {
1559+
let mut watch_outputs = Vec::with_capacity(holder_tx.htlc_outputs.len());
1560+
for &(ref htlc, _, _) in holder_tx.htlc_outputs.iter() {
1561+
if let Some(transaction_output_index) = htlc.transaction_output_index {
1562+
watch_outputs.push((transaction_output_index, commitment_tx.output[transaction_output_index as usize].clone()));
1563+
}
1564+
}
1565+
watch_outputs
15541566
}
15551567

15561568
/// Attempts to claim any claimable HTLCs in a commitment transaction which was not (yet)
@@ -1585,10 +1597,10 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15851597
}
15861598

15871599
macro_rules! append_onchain_update {
1588-
($updates: expr) => {
1600+
($updates: expr, $to_watch: expr) => {
15891601
claim_requests = $updates.0;
1590-
watch_outputs.append(&mut $updates.1);
1591-
self.broadcasted_holder_revokable_script = $updates.2;
1602+
self.broadcasted_holder_revokable_script = $updates.1;
1603+
watch_outputs.append(&mut $to_watch);
15921604
}
15931605
}
15941606

@@ -1598,14 +1610,16 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
15981610
if self.current_holder_commitment_tx.txid == commitment_txid {
15991611
is_holder_tx = true;
16001612
log_trace!(logger, "Got latest holder commitment tx broadcast, searching for available HTLCs to claim");
1601-
let mut res = self.broadcast_by_holder_state(tx, &self.current_holder_commitment_tx);
1602-
append_onchain_update!(res);
1613+
let res = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx);
1614+
let mut to_watch = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, tx);
1615+
append_onchain_update!(res, to_watch);
16031616
} else if let &Some(ref holder_tx) = &self.prev_holder_signed_commitment_tx {
16041617
if holder_tx.txid == commitment_txid {
16051618
is_holder_tx = true;
16061619
log_trace!(logger, "Got previous holder commitment tx broadcast, searching for available HTLCs to claim");
1607-
let mut res = self.broadcast_by_holder_state(tx, holder_tx);
1608-
append_onchain_update!(res);
1620+
let res = self.get_broadcasted_holder_claims(holder_tx);
1621+
let mut to_watch = self.get_broadcasted_holder_watch_outputs(holder_tx, tx);
1622+
append_onchain_update!(res, to_watch);
16091623
}
16101624
}
16111625

@@ -1773,7 +1787,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17731787
self.pending_monitor_events.push(MonitorEvent::CommitmentTxBroadcasted(self.funding_info.0));
17741788
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_holder_tx(&self.funding_redeemscript) {
17751789
self.holder_tx_signed = true;
1776-
let (mut new_outpoints, new_outputs, _) = self.broadcast_by_holder_state(&commitment_tx, &self.current_holder_commitment_tx);
1790+
let (mut new_outpoints, _) = self.get_broadcasted_holder_claims(&self.current_holder_commitment_tx);
1791+
let new_outputs = self.get_broadcasted_holder_watch_outputs(&self.current_holder_commitment_tx, &commitment_tx);
17771792
if !new_outputs.is_empty() {
17781793
watch_outputs.push((self.current_holder_commitment_tx.txid.clone(), new_outputs));
17791794
}

0 commit comments

Comments
 (0)