Skip to content

Commit c271d78

Browse files
committed
Add TxBuilder::{commit_tx_fee_sat, balances_excluding_tx_fee}
1 parent e1fe0b8 commit c271d78

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

lightning/src/ln/channel.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ use crate::chain::chaininterface::{FeeEstimator, ConfirmationTarget, LowerBounde
5656
use crate::chain::channelmonitor::{ChannelMonitor, ChannelMonitorUpdate, ChannelMonitorUpdateStep, LATENCY_GRACE_PERIOD_BLOCKS};
5757
use crate::chain::transaction::{OutPoint, TransactionData};
5858
use crate::sign::ecdsa::EcdsaChannelSigner;
59+
use crate::sign::tx_builder::{SpecTxBuilder, TxBuilder};
5960
use crate::sign::{EntropySource, ChannelSigner, SignerProvider, NodeSigner, Recipient};
6061
use crate::events::{ClosureReason, Event};
6162
use crate::events::bump_transaction::BASE_INPUT_WEIGHT;
@@ -3869,7 +3870,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
38693870
#[inline]
38703871
fn build_commitment_stats(&self, funding: &FundingScope, local: bool, generated_by_local: bool) -> CommitmentStats {
38713872
let broadcaster_dust_limit_sat = if local { self.holder_dust_limit_satoshis } else { self.counterparty_dust_limit_satoshis };
3872-
let mut non_dust_htlc_count = 0;
3873+
let mut nondust_htlc_count = 0;
38733874
let mut remote_htlc_total_msat = 0;
38743875
let mut local_htlc_total_msat = 0;
38753876
let mut value_to_self_msat_offset = 0;
@@ -3879,7 +3880,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
38793880
for htlc in self.pending_inbound_htlcs.iter() {
38803881
if htlc.state.included_in_commitment(generated_by_local) {
38813882
if !htlc.is_dust(local, feerate_per_kw, broadcaster_dust_limit_sat, funding.get_channel_type()) {
3882-
non_dust_htlc_count += 1;
3883+
nondust_htlc_count += 1;
38833884
}
38843885
remote_htlc_total_msat += htlc.amount_msat;
38853886
} else {
@@ -3892,7 +3893,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
38923893
for htlc in self.pending_outbound_htlcs.iter() {
38933894
if htlc.state.included_in_commitment(generated_by_local) {
38943895
if !htlc.is_dust(local, feerate_per_kw, broadcaster_dust_limit_sat, funding.get_channel_type()) {
3895-
non_dust_htlc_count += 1;
3896+
nondust_htlc_count += 1;
38963897
}
38973898
local_htlc_total_msat += htlc.amount_msat;
38983899
} else {
@@ -3928,20 +3929,18 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
39283929
debug_assert!(broadcaster_max_commitment_tx_output.1 <= value_to_remote_msat || value_to_remote_msat / 1000 >= funding.holder_selected_channel_reserve_satoshis);
39293930
broadcaster_max_commitment_tx_output.1 = cmp::max(broadcaster_max_commitment_tx_output.1, value_to_remote_msat);
39303931
}
3931-
3932-
let total_fee_sat = commit_tx_fee_sat(feerate_per_kw, non_dust_htlc_count, &funding.channel_transaction_parameters.channel_type_features);
3933-
let total_anchors_sat = if funding.channel_transaction_parameters.channel_type_features.supports_anchors_zero_fee_htlc_tx() { ANCHOR_OUTPUT_VALUE_SATOSHI * 2 } else { 0 };
3934-
3935-
if funding.is_outbound() {
3936-
value_to_self_msat = value_to_self_msat.saturating_sub(total_anchors_sat * 1000);
3937-
} else {
3938-
value_to_remote_msat = value_to_remote_msat.saturating_sub(total_anchors_sat * 1000);
3939-
}
3932+
let builder = SpecTxBuilder {};
3933+
let (local_balance_before_fee_msat, remote_balance_before_fee_msat) = builder.balances_excluding_tx_fee(
3934+
funding.is_outbound(),
3935+
&funding.channel_transaction_parameters.channel_type_features,
3936+
value_to_self_msat,
3937+
value_to_remote_msat,
3938+
);
39403939

39413940
CommitmentStats {
3942-
total_fee_sat,
3943-
local_balance_before_fee_msat: value_to_self_msat,
3944-
remote_balance_before_fee_msat: value_to_remote_msat,
3941+
total_fee_sat: builder.commit_tx_fee_sat(feerate_per_kw, nondust_htlc_count, &funding.channel_transaction_parameters.channel_type_features),
3942+
local_balance_before_fee_msat,
3943+
remote_balance_before_fee_msat,
39453944
}
39463945
}
39473946

lightning/src/sign/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ pub(crate) mod type_resolver;
7676
pub mod ecdsa;
7777
#[cfg(taproot)]
7878
pub mod taproot;
79+
pub mod tx_builder;
7980

8081
/// Information about a spendable output to a P2WSH script.
8182
///

lightning/src/sign/tx_builder.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//! Defines the `TxBuilder` trait, and the `SpecTxBuilder` type
2+
3+
use crate::ln::chan_utils::commit_tx_fee_sat;
4+
use crate::ln::channel::ANCHOR_OUTPUT_VALUE_SATOSHI;
5+
use crate::prelude::*;
6+
use crate::types::features::ChannelTypeFeatures;
7+
8+
pub(crate) trait TxBuilder {
9+
fn commit_tx_fee_sat(
10+
&self, feerate_per_kw: u32, nondust_htlc_count: usize, channel_type: &ChannelTypeFeatures,
11+
) -> u64;
12+
fn balances_excluding_tx_fee(
13+
&self, is_outbound_from_holder: bool, channel_type: &ChannelTypeFeatures,
14+
value_to_self_after_htlcs: u64, value_to_remote_after_htlcs: u64,
15+
) -> (u64, u64);
16+
}
17+
18+
#[derive(Clone, Debug, Default)]
19+
pub(crate) struct SpecTxBuilder {}
20+
21+
impl TxBuilder for SpecTxBuilder {
22+
fn commit_tx_fee_sat(
23+
&self, feerate_per_kw: u32, nondust_htlc_count: usize, channel_type: &ChannelTypeFeatures,
24+
) -> u64 {
25+
commit_tx_fee_sat(feerate_per_kw, nondust_htlc_count, channel_type)
26+
}
27+
fn balances_excluding_tx_fee(
28+
&self, is_outbound_from_holder: bool, channel_type: &ChannelTypeFeatures,
29+
value_to_self_after_htlcs: u64, value_to_remote_after_htlcs: u64,
30+
) -> (u64, u64) {
31+
let total_anchors_sat = if channel_type.supports_anchors_zero_fee_htlc_tx() {
32+
ANCHOR_OUTPUT_VALUE_SATOSHI * 2
33+
} else {
34+
0
35+
};
36+
37+
let mut local_balance_before_fee_msat = value_to_self_after_htlcs;
38+
let mut remote_balance_before_fee_msat = value_to_remote_after_htlcs;
39+
40+
if is_outbound_from_holder {
41+
local_balance_before_fee_msat =
42+
local_balance_before_fee_msat.saturating_sub(total_anchors_sat * 1000);
43+
} else {
44+
remote_balance_before_fee_msat =
45+
remote_balance_before_fee_msat.saturating_sub(total_anchors_sat * 1000);
46+
}
47+
48+
(local_balance_before_fee_msat, remote_balance_before_fee_msat)
49+
}
50+
}

0 commit comments

Comments
 (0)