Skip to content

Commit c5be7aa

Browse files
authored
Merge pull request #3297 from TheBlueMatt/2024-09-cleanup-package
Cleanup `PackageTemplate`a bit
2 parents 81923c7 + 20db790 commit c5be7aa

File tree

6 files changed

+190
-128
lines changed

6 files changed

+190
-128
lines changed

lightning/src/chain/channelmonitor.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -3018,7 +3018,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
30183018
let commitment_package = PackageTemplate::build_package(
30193019
self.funding_info.0.txid.clone(), self.funding_info.0.index as u32,
30203020
PackageSolvingData::HolderFundingOutput(funding_outp),
3021-
self.best_block.height, self.best_block.height
3021+
self.best_block.height,
30223022
);
30233023
let mut claimable_outpoints = vec![commitment_package];
30243024
let event = MonitorEvent::HolderForceClosedWithInfo {
@@ -3041,7 +3041,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
30413041
// assuming it gets confirmed in the next block. Sadly, we have code which considers
30423042
// "not yet confirmed" things as discardable, so we cannot do that here.
30433043
let (mut new_outpoints, _) = self.get_broadcasted_holder_claims(
3044-
&self.current_holder_commitment_tx, self.best_block.height
3044+
&self.current_holder_commitment_tx, self.best_block.height,
30453045
);
30463046
let unsigned_commitment_tx = self.onchain_tx_handler.get_unsigned_holder_commitment_tx();
30473047
let new_outputs = self.get_broadcasted_holder_watch_outputs(
@@ -3459,7 +3459,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34593459
for (idx, outp) in tx.output.iter().enumerate() {
34603460
if outp.script_pubkey == revokeable_p2wsh {
34613461
let revk_outp = RevokedOutput::build(per_commitment_point, self.counterparty_commitment_params.counterparty_delayed_payment_base_key, self.counterparty_commitment_params.counterparty_htlc_base_key, per_commitment_key, outp.value, self.counterparty_commitment_params.on_counterparty_tx_csv, self.onchain_tx_handler.channel_type_features().supports_anchors_zero_fee_htlc_tx());
3462-
let justice_package = PackageTemplate::build_package(commitment_txid, idx as u32, PackageSolvingData::RevokedOutput(revk_outp), height + self.counterparty_commitment_params.on_counterparty_tx_csv as u32, height);
3462+
let justice_package = PackageTemplate::build_package(
3463+
commitment_txid, idx as u32,
3464+
PackageSolvingData::RevokedOutput(revk_outp),
3465+
height + self.counterparty_commitment_params.on_counterparty_tx_csv as u32,
3466+
);
34633467
claimable_outpoints.push(justice_package);
34643468
to_counterparty_output_info =
34653469
Some((idx.try_into().expect("Txn can't have more than 2^32 outputs"), outp.value));
@@ -3476,7 +3480,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
34763480
return (claimable_outpoints, to_counterparty_output_info);
34773481
}
34783482
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);
3479-
let justice_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp), htlc.cltv_expiry, height);
3483+
let justice_package = PackageTemplate::build_package(
3484+
commitment_txid,
3485+
transaction_output_index,
3486+
PackageSolvingData::RevokedHTLCOutput(revk_htlc_outp),
3487+
htlc.cltv_expiry,
3488+
);
34803489
claimable_outpoints.push(justice_package);
34813490
}
34823491
}
@@ -3598,7 +3607,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
35983607
self.counterparty_commitment_params.counterparty_htlc_base_key,
35993608
htlc.clone(), self.onchain_tx_handler.channel_type_features().clone()))
36003609
};
3601-
let counterparty_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc.cltv_expiry, 0);
3610+
let counterparty_package = PackageTemplate::build_package(commitment_txid, transaction_output_index, counterparty_htlc_outp, htlc.cltv_expiry);
36023611
claimable_outpoints.push(counterparty_package);
36033612
}
36043613
}
@@ -3642,7 +3651,7 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36423651
);
36433652
let justice_package = PackageTemplate::build_package(
36443653
htlc_txid, idx as u32, PackageSolvingData::RevokedOutput(revk_outp),
3645-
height + self.counterparty_commitment_params.on_counterparty_tx_csv as u32, height
3654+
height + self.counterparty_commitment_params.on_counterparty_tx_csv as u32,
36463655
);
36473656
claimable_outpoints.push(justice_package);
36483657
if outputs_to_watch.is_none() {
@@ -3665,11 +3674,11 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36653674

36663675
for &(ref htlc, _, _) in holder_tx.htlc_outputs.iter() {
36673676
if let Some(transaction_output_index) = htlc.transaction_output_index {
3668-
let htlc_output = if htlc.offered {
3677+
let (htlc_output, counterparty_spendable_height) = if htlc.offered {
36693678
let htlc_output = HolderHTLCOutput::build_offered(
36703679
htlc.amount_msat, htlc.cltv_expiry, self.onchain_tx_handler.channel_type_features().clone()
36713680
);
3672-
htlc_output
3681+
(htlc_output, conf_height)
36733682
} else {
36743683
let payment_preimage = if let Some(preimage) = self.payment_preimages.get(&htlc.payment_hash) {
36753684
preimage.clone()
@@ -3680,12 +3689,12 @@ impl<Signer: EcdsaChannelSigner> ChannelMonitorImpl<Signer> {
36803689
let htlc_output = HolderHTLCOutput::build_accepted(
36813690
payment_preimage, htlc.amount_msat, self.onchain_tx_handler.channel_type_features().clone()
36823691
);
3683-
htlc_output
3692+
(htlc_output, htlc.cltv_expiry)
36843693
};
36853694
let htlc_package = PackageTemplate::build_package(
36863695
holder_tx.txid, transaction_output_index,
36873696
PackageSolvingData::HolderHTLCOutput(htlc_output),
3688-
htlc.cltv_expiry, conf_height
3697+
counterparty_spendable_height,
36893698
);
36903699
claim_requests.push(htlc_package);
36913700
}

lightning/src/chain/onchaintx.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -765,9 +765,8 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
765765
continue;
766766
}
767767

768-
log_trace!(logger, "Test if outpoint can be aggregated with expiration {} against {}", req.timelock(), cur_height + CLTV_SHARED_CLAIM_BUFFER);
769-
if req.timelock() <= cur_height + CLTV_SHARED_CLAIM_BUFFER || !req.aggregable() {
770-
// Don't aggregate if outpoint package timelock is soon or marked as non-aggregable
768+
log_trace!(logger, "Test if outpoint which our counterparty can spend at {} can be aggregated based on aggregation limit {}", req.counterparty_spendable_height(), cur_height + CLTV_SHARED_CLAIM_BUFFER);
769+
if req.counterparty_spendable_height() <= cur_height + CLTV_SHARED_CLAIM_BUFFER || !req.aggregable() {
771770
preprocessed_requests.push(req);
772771
} else if aggregated_request.is_none() {
773772
aggregated_request = Some(req);
@@ -885,15 +884,16 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
885884
if let Some((claim_id, _)) = self.claimable_outpoints.get(&inp.previous_output) {
886885
// If outpoint has claim request pending on it...
887886
if let Some(request) = self.pending_claim_requests.get_mut(claim_id) {
888-
//... we need to verify equality between transaction outpoints and claim request
889-
// outpoints to know if transaction is the original claim or a bumped one issued
890-
// by us.
891-
let mut are_sets_equal = true;
887+
//... we need to check if the pending claim was for a subset of the outputs
888+
// spent by the confirmed transaction. If so, we can drop the pending claim
889+
// after ANTI_REORG_DELAY blocks, otherwise we need to split it and retry
890+
// claiming the remaining outputs.
891+
let mut is_claim_subset_of_tx = true;
892892
let mut tx_inputs = tx.input.iter().map(|input| &input.previous_output).collect::<Vec<_>>();
893893
tx_inputs.sort_unstable();
894894
for request_input in request.outpoints() {
895895
if tx_inputs.binary_search(&request_input).is_err() {
896-
are_sets_equal = false;
896+
is_claim_subset_of_tx = false;
897897
break;
898898
}
899899
}
@@ -915,7 +915,7 @@ impl<ChannelSigner: EcdsaChannelSigner> OnchainTxHandler<ChannelSigner> {
915915
// If this is our transaction (or our counterparty spent all the outputs
916916
// before we could anyway with same inputs order than us), wait for
917917
// ANTI_REORG_DELAY and clean the RBF tracking map.
918-
if are_sets_equal {
918+
if is_claim_subset_of_tx {
919919
clean_claim_request_after_safety_delay!();
920920
} else { // If false, generate new claim request with update outpoint set
921921
let mut at_least_one_drop = false;

0 commit comments

Comments
 (0)