@@ -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.
@@ -3478,8 +3478,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3478
3478
let mut htlcs_in_tx: Vec<(HTLCOutputInCommitment, Option<&HTLCSource>)> = Vec::with_capacity(num_htlcs);
3479
3479
3480
3480
let broadcaster_dust_limit_satoshis = if local { self.holder_dust_limit_satoshis } else { self.counterparty_dust_limit_satoshis };
3481
- let mut remote_htlc_total_msat = 0;
3482
- let mut local_htlc_total_msat = 0;
3483
3481
let mut value_to_self_msat_offset = 0;
3484
3482
3485
3483
let mut feerate_per_kw = self.feerate_per_kw;
@@ -3579,93 +3577,12 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3579
3577
}
3580
3578
}
3581
3579
3582
- // Trim dust htlcs
3583
- let mut included_non_dust_htlcs: Vec<_> = htlcs_in_tx.iter_mut().map(|(htlc, _)| htlc).collect();
3584
- included_non_dust_htlcs.retain(|htlc| {
3585
- let outbound = local == htlc.offered;
3586
- if outbound {
3587
- local_htlc_total_msat += htlc.amount_msat;
3588
- } else {
3589
- remote_htlc_total_msat += htlc.amount_msat;
3590
- }
3591
- let htlc_tx_fee = if self.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
3592
- 0
3593
- } else {
3594
- feerate_per_kw as u64
3595
- * if htlc.offered {
3596
- chan_utils::htlc_timeout_tx_weight(self.get_channel_type())
3597
- } else {
3598
- chan_utils::htlc_success_tx_weight(self.get_channel_type())
3599
- } / 1000
3600
- };
3601
- if htlc.amount_msat / 1000 >= broadcaster_dust_limit_satoshis + htlc_tx_fee {
3602
- log_trace!(
3603
- logger,
3604
- " ...creating output for {} non-dust HTLC (hash {}) with value {}",
3605
- if outbound { "outbound" } else { "inbound" },
3606
- htlc.payment_hash,
3607
- htlc.amount_msat
3608
- );
3609
- true
3610
- } else {
3611
- log_trace!(
3612
- logger,
3613
- " ...trimming {} HTLC (hash {}) with value {} due to dust limit",
3614
- if outbound { "outbound" } else { "inbound" },
3615
- htlc.payment_hash,
3616
- htlc.amount_msat
3617
- );
3618
- false
3619
- }
3620
- });
3621
-
3622
3580
// TODO: When MSRV >= 1.66.0, use u64::checked_add_signed
3623
- let mut value_to_self_msat = u64::try_from(funding.value_to_self_msat as i64 + value_to_self_msat_offset).unwrap();
3624
- // Note that in case they have several just-awaiting-last-RAA fulfills in-progress (ie
3625
- // AwaitingRemoteRevokeToRemove or AwaitingRemovedRemoteRevoke) we may have allowed them to
3626
- // "violate" their reserve value by couting those against it. Thus, we have to do checked subtraction
3627
- // as otherwise we can overflow.
3628
- let mut value_to_remote_msat = u64::checked_sub(funding.get_value_satoshis() * 1000, value_to_self_msat).unwrap();
3629
- value_to_self_msat = u64::checked_sub(value_to_self_msat, local_htlc_total_msat).unwrap();
3630
- value_to_remote_msat = u64::checked_sub(value_to_remote_msat, remote_htlc_total_msat).unwrap();
3631
-
3632
- let total_fee_sat = commit_tx_fee_sat(feerate_per_kw, included_non_dust_htlcs.len(), &funding.channel_transaction_parameters.channel_type_features);
3633
- let anchors_val = if funding.channel_transaction_parameters.channel_type_features.supports_anchors_zero_fee_htlc_tx() { ANCHOR_OUTPUT_VALUE_SATOSHI * 2 } else { 0 };
3634
- let (value_to_self, value_to_remote) = if funding.is_outbound() {
3635
- ((value_to_self_msat / 1000).saturating_sub(anchors_val).saturating_sub(total_fee_sat), value_to_remote_msat / 1000)
3636
- } else {
3637
- (value_to_self_msat / 1000, (value_to_remote_msat / 1000).saturating_sub(anchors_val).saturating_sub(total_fee_sat))
3638
- };
3581
+ let value_to_self_with_offset_msat = u64::try_from(funding.value_to_self_msat as i64 + value_to_self_msat_offset).unwrap();
3639
3582
3640
- let mut value_to_a = if local { value_to_self } else { value_to_remote };
3641
- let mut value_to_b = if local { value_to_remote } else { value_to_self };
3642
-
3643
- if value_to_a >= broadcaster_dust_limit_satoshis {
3644
- log_trace!(logger, " ...creating {} output with value {}", if local { "to_local" } else { "to_remote" }, value_to_a);
3645
- } else {
3646
- log_trace!(logger, " ...trimming {} output with value {} due to dust limit", if local { "to_local" } else { "to_remote" }, value_to_a);
3647
- value_to_a = 0;
3648
- }
3649
-
3650
- if value_to_b >= broadcaster_dust_limit_satoshis {
3651
- log_trace!(logger, " ...creating {} output with value {}", if local { "to_remote" } else { "to_local" }, value_to_b);
3652
- } else {
3653
- log_trace!(logger, " ...trimming {} output with value {} due to dust limit", if local { "to_remote" } else { "to_local" }, value_to_b);
3654
- value_to_b = 0;
3655
- }
3656
-
3657
- let channel_parameters =
3658
- if local { funding.channel_transaction_parameters.as_holder_broadcastable() }
3659
- else { funding.channel_transaction_parameters.as_counterparty_broadcastable() };
3660
- let tx = CommitmentTransaction::new(commitment_number,
3661
- &per_commitment_point,
3662
- value_to_a,
3663
- value_to_b,
3664
- feerate_per_kw,
3665
- included_non_dust_htlcs,
3666
- &channel_parameters,
3667
- &self.secp_ctx,
3668
- );
3583
+ use crate::sign::tx_builder::{TxBuilder, SpecTxBuilder};
3584
+ let stats = TxBuilder::build_commitment_transaction(&SpecTxBuilder {}, local, commitment_number, per_commitment_point, &funding.channel_transaction_parameters, &self.secp_ctx,
3585
+ 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);
3669
3586
3670
3587
#[cfg(debug_assertions)]
3671
3588
{
@@ -3676,10 +3593,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3676
3593
} else {
3677
3594
funding.counterparty_max_commitment_tx_output.lock().unwrap()
3678
3595
};
3679
- debug_assert!(broadcaster_max_commitment_tx_output.0 <= value_to_self_msat || value_to_self_msat / 1000 >= funding.counterparty_selected_channel_reserve_satoshis.unwrap());
3680
- broadcaster_max_commitment_tx_output.0 = cmp::max(broadcaster_max_commitment_tx_output.0, value_to_self_msat );
3681
- debug_assert!(broadcaster_max_commitment_tx_output.1 <= value_to_remote_msat || value_to_remote_msat / 1000 >= funding.holder_selected_channel_reserve_satoshis);
3682
- broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, value_to_remote_msat );
3596
+ debug_assert!(broadcaster_max_commitment_tx_output.0 <= stats.local_balance_msat || stats.local_balance_msat / 1000 >= funding.counterparty_selected_channel_reserve_satoshis.unwrap());
3597
+ broadcaster_max_commitment_tx_output.0 = cmp::max(broadcaster_max_commitment_tx_output.0, stats.local_balance_msat );
3598
+ debug_assert!(broadcaster_max_commitment_tx_output.1 <= stats.remote_balance_msat || stats.remote_balance_msat / 1000 >= funding.holder_selected_channel_reserve_satoshis);
3599
+ broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, stats.remote_balance_msat );
3683
3600
}
3684
3601
3685
3602
htlcs_in_tx.sort_unstable_by(|(htlc_a, _), (htlc_b, _)| {
@@ -3691,13 +3608,6 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
3691
3608
}
3692
3609
});
3693
3610
3694
- let stats = CommitmentStats {
3695
- tx,
3696
- total_fee_sat,
3697
- local_balance_msat: value_to_self_msat,
3698
- remote_balance_msat: value_to_remote_msat,
3699
- };
3700
-
3701
3611
CommitmentData {
3702
3612
stats,
3703
3613
htlcs_included: htlcs_in_tx,
0 commit comments