Skip to content

Commit 8880b55

Browse files
authored
Merge pull request #3081 from G8XSU/2024-05-08-claimable-persist-3049-outputs
Watch all outputs irrespective of claimable outpoints
2 parents 3cd1cb5 + 149fa12 commit 8880b55

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,16 +3233,14 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32333233
/// height > height + CLTV_SHARED_CLAIM_BUFFER. In any case, will install monitoring for
32343234
/// HTLC-Success/HTLC-Timeout transactions.
32353235
///
3236-
/// Returns packages to claim the revoked output(s), as well as additional outputs to watch and
3237-
/// general information about the output that is to the counterparty in the commitment
3238-
/// transaction.
3236+
/// Returns packages to claim the revoked output(s) and general information about the output that
3237+
/// is to the counterparty in the commitment transaction.
32393238
fn check_spend_counterparty_transaction<L: Deref>(&mut self, tx: &Transaction, height: u32, block_hash: &BlockHash, logger: &L)
3240-
-> (Vec<PackageTemplate>, TransactionOutputs, CommitmentTxCounterpartyOutputInfo)
3239+
-> (Vec<PackageTemplate>, CommitmentTxCounterpartyOutputInfo)
32413240
where L::Target: Logger {
32423241
// Most secp and related errors trying to create keys means we have no hope of constructing
32433242
// a spend transaction...so we return no transactions to broadcast
32443243
let mut claimable_outpoints = Vec::new();
3245-
let mut watch_outputs = Vec::new();
32463244
let mut to_counterparty_output_info = None;
32473245

32483246
let commitment_txid = tx.txid(); //TODO: This is gonna be a performance bottleneck for watchtowers!
@@ -3252,7 +3250,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32523250
( $thing : expr ) => {
32533251
match $thing {
32543252
Ok(a) => a,
3255-
Err(_) => return (claimable_outpoints, (commitment_txid, watch_outputs), to_counterparty_output_info)
3253+
Err(_) => return (claimable_outpoints, to_counterparty_output_info)
32563254
}
32573255
};
32583256
}
@@ -3286,8 +3284,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
32863284
if transaction_output_index as usize >= tx.output.len() ||
32873285
tx.output[transaction_output_index as usize].value != htlc.to_bitcoin_amount() {
32883286
// per_commitment_data is corrupt or our commitment signing key leaked!
3289-
return (claimable_outpoints, (commitment_txid, watch_outputs),
3290-
to_counterparty_output_info);
3287+
return (claimable_outpoints, to_counterparty_output_info);
32913288
}
32923289
let revk_htlc_outp = RevokedHTLCOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, htlc.amount_msat / 1000, htlc.clone(), &self.onchain_tx_handler.channel_transaction_parameters.channel_type_features);
32933290
let justice_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp), htlc.cltv_expiry, height);
@@ -3300,9 +3297,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33003297
if !claimable_outpoints.is_empty() || per_commitment_option.is_some() { // ie we're confident this is actually ours
33013298
// We're definitely a counterparty commitment transaction!
33023299
log_error!(logger, "Got broadcast of revoked counterparty commitment transaction, going to generate general spend tx with {} inputs", claimable_outpoints.len());
3303-
for (idx, outp) in tx.output.iter().enumerate() {
3304-
watch_outputs.push((idx as u32, outp.clone()));
3305-
}
33063300
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
33073301

33083302
if let Some(per_commitment_claimable_data) = per_commitment_option {
@@ -3328,9 +3322,6 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33283322
// already processed the block, resulting in the counterparty_commitment_txn_on_chain entry
33293323
// not being generated by the above conditional. Thus, to be safe, we go ahead and
33303324
// insert it here.
3331-
for (idx, outp) in tx.output.iter().enumerate() {
3332-
watch_outputs.push((idx as u32, outp.clone()));
3333-
}
33343325
self.counterparty_commitment_txn_on_chain.insert(commitment_txid, commitment_number);
33353326

33363327
log_info!(logger, "Got broadcast of non-revoked counterparty commitment transaction {}", commitment_txid);
@@ -3346,7 +3337,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
33463337
}
33473338

33483339
}
3349-
(claimable_outpoints, (commitment_txid, watch_outputs), to_counterparty_output_info)
3340+
(claimable_outpoints, to_counterparty_output_info)
33503341
}
33513342

33523343
/// Returns the HTLC claim package templates and the counterparty output info
@@ -3778,24 +3769,25 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
37783769
self.funding_spend_seen = true;
37793770
let mut commitment_tx_to_counterparty_output = None;
37803771
if (tx.input[0].sequence.0 >> 8*3) as u8 == 0x80 && (tx.lock_time.to_consensus_u32() >> 8*3) as u8 == 0x20 {
3781-
let (mut new_outpoints, new_outputs, counterparty_output_idx_sats) =
3782-
self.check_spend_counterparty_transaction(&tx, height, &block_hash, &logger);
3783-
commitment_tx_to_counterparty_output = counterparty_output_idx_sats;
3784-
if !new_outputs.1.is_empty() {
3785-
watch_outputs.push(new_outputs);
3786-
}
3787-
claimable_outpoints.append(&mut new_outpoints);
3788-
if new_outpoints.is_empty() {
3789-
if let Some((mut new_outpoints, new_outputs)) = self.check_spend_holder_transaction(&tx, height, &block_hash, &logger) {
3790-
#[cfg(not(fuzzing))]
3791-
debug_assert!(commitment_tx_to_counterparty_output.is_none(),
3792-
"A commitment transaction matched as both a counterparty and local commitment tx?");
3793-
if !new_outputs.1.is_empty() {
3794-
watch_outputs.push(new_outputs);
3795-
}
3796-
claimable_outpoints.append(&mut new_outpoints);
3797-
balance_spendable_csv = Some(self.on_holder_tx_csv);
3772+
if let Some((mut new_outpoints, new_outputs)) = self.check_spend_holder_transaction(&tx, height, &block_hash, &logger) {
3773+
if !new_outputs.1.is_empty() {
3774+
watch_outputs.push(new_outputs);
3775+
}
3776+
3777+
claimable_outpoints.append(&mut new_outpoints);
3778+
balance_spendable_csv = Some(self.on_holder_tx_csv);
3779+
} else {
3780+
let mut new_watch_outputs = Vec::new();
3781+
for (idx, outp) in tx.output.iter().enumerate() {
3782+
new_watch_outputs.push((idx as u32, outp.clone()));
37983783
}
3784+
watch_outputs.push((txid, new_watch_outputs));
3785+
3786+
let (mut new_outpoints, counterparty_output_idx_sats) =
3787+
self.check_spend_counterparty_transaction(&tx, height, &block_hash, &logger);
3788+
commitment_tx_to_counterparty_output = counterparty_output_idx_sats;
3789+
3790+
claimable_outpoints.append(&mut new_outpoints);
37993791
}
38003792
}
38013793
self.onchain_events_awaiting_threshold_conf.push(OnchainEventEntry {

0 commit comments

Comments
 (0)