Skip to content

Commit ba8af22

Browse files
authored
Merge pull request #2403 from wpaulino/bump-transaction-event-handler-tests
Integrate BumpTransactionEventHandler into existing anchor tests
2 parents 8e2b70d + ff474ba commit ba8af22

9 files changed

+338
-209
lines changed

lightning/src/chain/chaininterface.rs

+9
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@
1414
//! disconnections, transaction broadcasting, and feerate information requests.
1515
1616
use core::{cmp, ops::Deref};
17+
use core::convert::TryInto;
1718

1819
use bitcoin::blockdata::transaction::Transaction;
1920

21+
// TODO: Define typed abstraction over feerates to handle their conversions.
22+
pub(crate) fn compute_feerate_sat_per_1000_weight(fee_sat: u64, weight: u64) -> u32 {
23+
(fee_sat * 1000 / weight).try_into().unwrap_or(u32::max_value())
24+
}
25+
pub(crate) const fn fee_for_weight(feerate_sat_per_1000_weight: u32, weight: u64) -> u64 {
26+
((feerate_sat_per_1000_weight as u64 * weight) + 1000 - 1) / 1000
27+
}
28+
2029
/// An interface to send a transaction to the Bitcoin network.
2130
pub trait BroadcasterInterface {
2231
/// Sends a list of transactions out to (hopefully) be mined.

lightning/src/chain/onchaintx.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use bitcoin::hash_types::{Txid, BlockHash};
2222
use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
2323
use bitcoin::secp256k1;
2424

25+
use crate::chain::chaininterface::compute_feerate_sat_per_1000_weight;
2526
use crate::sign::{ChannelSigner, EntropySource, SignerProvider};
2627
use crate::ln::msgs::DecodeError;
2728
use crate::ln::PaymentPreimage;
@@ -623,19 +624,31 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
623624
return inputs.find_map(|input| match input {
624625
// Commitment inputs with anchors support are the only untractable inputs supported
625626
// thus far that require external funding.
626-
PackageSolvingData::HolderFundingOutput(..) => {
627+
PackageSolvingData::HolderFundingOutput(output) => {
627628
debug_assert_eq!(tx.txid(), self.holder_commitment.trust().txid(),
628629
"Holder commitment transaction mismatch");
630+
631+
let conf_target = ConfirmationTarget::HighPriority;
632+
let package_target_feerate_sat_per_1000_weight = cached_request
633+
.compute_package_feerate(fee_estimator, conf_target, force_feerate_bump);
634+
if let Some(input_amount_sat) = output.funding_amount {
635+
let fee_sat = input_amount_sat - tx.output.iter().map(|output| output.value).sum::<u64>();
636+
if compute_feerate_sat_per_1000_weight(fee_sat, tx.weight() as u64) >=
637+
package_target_feerate_sat_per_1000_weight
638+
{
639+
log_debug!(logger, "Commitment transaction {} already meets required feerate {} sat/kW",
640+
tx.txid(), package_target_feerate_sat_per_1000_weight);
641+
return Some((new_timer, 0, OnchainClaim::Tx(tx.clone())));
642+
}
643+
}
644+
629645
// We'll locate an anchor output we can spend within the commitment transaction.
630646
let funding_pubkey = &self.channel_transaction_parameters.holder_pubkeys.funding_pubkey;
631647
match chan_utils::get_anchor_output(&tx, funding_pubkey) {
632648
// An anchor output was found, so we should yield a funding event externally.
633649
Some((idx, _)) => {
634650
// TODO: Use a lower confirmation target when both our and the
635651
// counterparty's latest commitment don't have any HTLCs present.
636-
let conf_target = ConfirmationTarget::HighPriority;
637-
let package_target_feerate_sat_per_1000_weight = cached_request
638-
.compute_package_feerate(fee_estimator, conf_target, force_feerate_bump);
639652
Some((
640653
new_timer,
641654
package_target_feerate_sat_per_1000_weight as u64,
@@ -739,6 +752,9 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
739752
) {
740753
req.set_timer(new_timer);
741754
req.set_feerate(new_feerate);
755+
// Once a pending claim has an id assigned, it remains fixed until the claim is
756+
// satisfied, regardless of whether the claim switches between different variants of
757+
// `OnchainClaim`.
742758
let claim_id = match claim {
743759
OnchainClaim::Tx(tx) => {
744760
log_info!(logger, "Broadcasting onchain {}", log_tx!(tx));

lightning/src/chain/package.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ impl Readable for HolderHTLCOutput {
429429
#[derive(Clone, PartialEq, Eq)]
430430
pub(crate) struct HolderFundingOutput {
431431
funding_redeemscript: Script,
432-
funding_amount: Option<u64>,
432+
pub(crate) funding_amount: Option<u64>,
433433
channel_type_features: ChannelTypeFeatures,
434434
}
435435

0 commit comments

Comments
 (0)