@@ -885,11 +885,11 @@ struct CommitmentData<'a> {
885
885
}
886
886
887
887
/// A struct gathering stats on commitment transaction, either local or remote.
888
- struct CommitmentStats {
889
- tx: CommitmentTransaction, // the transaction info
890
- total_fee_sat: u64, // the total fee included in the transaction
891
- local_balance_msat: u64, // local balance before fees *not* considering dust limits
892
- remote_balance_msat: u64, // remote balance before fees *not* considering dust limits
888
+ pub(crate) struct CommitmentStats {
889
+ pub(crate) tx: CommitmentTransaction, // the transaction info
890
+ pub(crate) total_fee_sat: u64, // the total fee included in the transaction
891
+ pub(crate) local_balance_msat: u64, // local balance before fees *not* considering dust limits
892
+ pub(crate) remote_balance_msat: u64, // remote balance before fees *not* considering dust limits
893
893
}
894
894
895
895
/// Used when calculating whether we or the remote can afford an additional HTLC.
@@ -3609,8 +3609,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3609
3609
let mut htlcs_in_tx: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::with_capacity(num_htlcs);
3610
3610
3611
3611
let broadcaster_dust_limit_satoshis = if local { self.holder_dust_limit_satoshis } else { self.counterparty_dust_limit_satoshis };
3612
- let mut remote_htlc_total_msat = 0;
3613
- let mut local_htlc_total_msat = 0;
3614
3612
let mut value_to_self_msat_offset = 0;
3615
3613
3616
3614
let mut feerate_per_kw = self.feerate_per_kw;
@@ -3710,93 +3708,12 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3710
3708
}
3711
3709
}
3712
3710
3713
- // Trim dust htlcs
3714
- let mut included_non_dust_htlcs: Vec<_> = htlcs_in_tx.iter_mut().map(|(htlc, _)| htlc).collect();
3715
- included_non_dust_htlcs.retain(|htlc| {
3716
- let outbound = local == htlc.offered;
3717
- if outbound {
3718
- local_htlc_total_msat += htlc.amount_msat;
3719
- } else {
3720
- remote_htlc_total_msat += htlc.amount_msat;
3721
- }
3722
- let htlc_tx_fee = if self.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
3723
- 0
3724
- } else {
3725
- feerate_per_kw as u64
3726
- * if htlc.offered {
3727
- chan_utils::htlc_timeout_tx_weight(self.get_channel_type())
3728
- } else {
3729
- chan_utils::htlc_success_tx_weight(self.get_channel_type())
3730
- } / 1000
3731
- };
3732
- if htlc.amount_msat / 1000 >= broadcaster_dust_limit_satoshis + htlc_tx_fee {
3733
- log_trace!(
3734
- logger,
3735
- " ...creating output for {} non-dust HTLC (hash {}) with value {}",
3736
- if outbound { "outbound" } else { "inbound" },
3737
- htlc.payment_hash,
3738
- htlc.amount_msat
3739
- );
3740
- true
3741
- } else {
3742
- log_trace!(
3743
- logger,
3744
- " ...trimming {} HTLC (hash {}) with value {} due to dust limit",
3745
- if outbound { "outbound" } else { "inbound" },
3746
- htlc.payment_hash,
3747
- htlc.amount_msat
3748
- );
3749
- false
3750
- }
3751
- });
3752
-
3753
3711
// TODO: When MSRV >= 1.66.0, use u64::checked_add_signed
3754
- let mut value_to_self_msat = u64::try_from(funding.value_to_self_msat as i64 + value_to_self_msat_offset).unwrap();
3755
- // Note that in case they have several just-awaiting-last-RAA fulfills in-progress (ie
3756
- // AwaitingRemoteRevokeToRemove or AwaitingRemovedRemoteRevoke) we may have allowed them to
3757
- // "violate" their reserve value by couting those against it. Thus, we have to do checked subtraction
3758
- // as otherwise we can overflow.
3759
- let mut value_to_remote_msat = u64::checked_sub(funding.get_value_satoshis() * 1000, value_to_self_msat).unwrap();
3760
- value_to_self_msat = u64::checked_sub(value_to_self_msat, local_htlc_total_msat).unwrap();
3761
- value_to_remote_msat = u64::checked_sub(value_to_remote_msat, remote_htlc_total_msat).unwrap();
3762
-
3763
- let total_fee_sat = commit_tx_fee_sat(feerate_per_kw, included_non_dust_htlcs.len(), &funding.channel_transaction_parameters.channel_type_features);
3764
- let anchors_val = if funding.channel_transaction_parameters.channel_type_features.supports_anchors_zero_fee_htlc_tx() { ANCHOR_OUTPUT_VALUE_SATOSHI * 2 } else { 0 };
3765
- let (value_to_self, value_to_remote) = if funding.is_outbound() {
3766
- ((value_to_self_msat / 1000).saturating_sub(anchors_val).saturating_sub(total_fee_sat), value_to_remote_msat / 1000)
3767
- } else {
3768
- (value_to_self_msat / 1000, (value_to_remote_msat / 1000).saturating_sub(anchors_val).saturating_sub(total_fee_sat))
3769
- };
3712
+ let value_to_self_with_offset_msat = u64::try_from(funding.value_to_self_msat as i64 + value_to_self_msat_offset).unwrap();
3770
3713
3771
- let mut value_to_a = if local { value_to_self } else { value_to_remote };
3772
- let mut value_to_b = if local { value_to_remote } else { value_to_self };
3773
-
3774
- if value_to_a >= broadcaster_dust_limit_satoshis {
3775
- log_trace!(logger, " ...creating {} output with value {}", if local { "to_local" } else { "to_remote" }, value_to_a);
3776
- } else {
3777
- log_trace!(logger, " ...trimming {} output with value {} due to dust limit", if local { "to_local" } else { "to_remote" }, value_to_a);
3778
- value_to_a = 0;
3779
- }
3780
-
3781
- if value_to_b >= broadcaster_dust_limit_satoshis {
3782
- log_trace!(logger, " ...creating {} output with value {}", if local { "to_remote" } else { "to_local" }, value_to_b);
3783
- } else {
3784
- log_trace!(logger, " ...trimming {} output with value {} due to dust limit", if local { "to_remote" } else { "to_local" }, value_to_b);
3785
- value_to_b = 0;
3786
- }
3787
-
3788
- let channel_parameters =
3789
- if local { funding.channel_transaction_parameters.as_holder_broadcastable() }
3790
- else { funding.channel_transaction_parameters.as_counterparty_broadcastable() };
3791
- let tx = CommitmentTransaction::new(commitment_number,
3792
- &per_commitment_point,
3793
- value_to_a,
3794
- value_to_b,
3795
- feerate_per_kw,
3796
- included_non_dust_htlcs,
3797
- &channel_parameters,
3798
- &self.secp_ctx,
3799
- );
3714
+ use crate::sign::tx_builder::{TxBuilder, SpecTxBuilder};
3715
+ let stats = TxBuilder::build_commitment_transaction(&SpecTxBuilder {}, local, commitment_number, per_commitment_point, &funding.channel_transaction_parameters, &self.secp_ctx,
3716
+ funding.get_value_satoshis(), value_to_self_with_offset_msat, htlcs_in_tx.iter_mut().map(|(htlc, _)| htlc).collect(), feerate_per_kw, broadcaster_dust_limit_satoshis, logger);
3800
3717
3801
3718
#[cfg(debug_assertions)]
3802
3719
{
@@ -3807,10 +3724,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3807
3724
} else {
3808
3725
funding.counterparty_max_commitment_tx_output.lock().unwrap()
3809
3726
};
3810
- debug_assert!(broadcaster_max_commitment_tx_output.0 <= value_to_self_msat || value_to_self_msat / 1000 >= funding.counterparty_selected_channel_reserve_satoshis.unwrap());
3811
- broadcaster_max_commitment_tx_output.0 = cmp::max(broadcaster_max_commitment_tx_output.0, value_to_self_msat );
3812
- debug_assert!(broadcaster_max_commitment_tx_output.1 <= value_to_remote_msat || value_to_remote_msat / 1000 >= funding.holder_selected_channel_reserve_satoshis);
3813
- broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, value_to_remote_msat );
3727
+ debug_assert!(broadcaster_max_commitment_tx_output.0 <= stats.local_balance_msat || stats.local_balance_msat / 1000 >= funding.counterparty_selected_channel_reserve_satoshis.unwrap());
3728
+ broadcaster_max_commitment_tx_output.0 = cmp::max(broadcaster_max_commitment_tx_output.0, stats.local_balance_msat );
3729
+ debug_assert!(broadcaster_max_commitment_tx_output.1 <= stats.remote_balance_msat || stats.remote_balance_msat / 1000 >= funding.holder_selected_channel_reserve_satoshis);
3730
+ broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, stats.remote_balance_msat );
3814
3731
}
3815
3732
3816
3733
htlcs_in_tx.sort_unstable_by(|(htlc_a, _), (htlc_b, _)| {
@@ -3822,13 +3739,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3822
3739
}
3823
3740
});
3824
3741
3825
- let stats = CommitmentStats {
3826
- tx,
3827
- total_fee_sat,
3828
- local_balance_msat: value_to_self_msat,
3829
- remote_balance_msat: value_to_remote_msat,
3830
- };
3831
-
3832
3742
CommitmentData {
3833
3743
stats,
3834
3744
htlcs_included: htlcs_in_tx,
0 commit comments