Skip to content

Commit addbe07

Browse files
committed
Create ChannelConstraints
These will be useful to pass these constraints to `TxBuilder` to calculate `AvailableBalances`.
1 parent f2bfd67 commit addbe07

File tree

2 files changed

+41
-11
lines changed

2 files changed

+41
-11
lines changed

lightning/src/ln/channel.rs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ use crate::ln::LN_MAX_MSG_LEN;
7373
use crate::offers::static_invoice::StaticInvoice;
7474
use crate::routing::gossip::NodeId;
7575
use crate::sign::ecdsa::EcdsaChannelSigner;
76-
use crate::sign::tx_builder::{HTLCAmountDirection, NextCommitmentStats, SpecTxBuilder, TxBuilder};
76+
use crate::sign::tx_builder::{HTLCAmountDirection, ChannelConstraints, NextCommitmentStats, SpecTxBuilder, TxBuilder};
7777
use crate::sign::{ChannelSigner, EntropySource, NodeSigner, Recipient, SignerProvider};
7878
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
7979
use crate::types::payment::{PaymentHash, PaymentPreimage};
@@ -5662,6 +5662,26 @@ where
56625662
outbound_details
56635663
}
56645664

5665+
fn get_holder_channel_constraints(&self, funding: &FundingScope) -> ChannelConstraints {
5666+
ChannelConstraints {
5667+
dust_limit_satoshis: self.holder_dust_limit_satoshis,
5668+
channel_reserve_satoshis: funding.counterparty_selected_channel_reserve_satoshis.unwrap_or(0),
5669+
htlc_minimum_msat: self.holder_htlc_minimum_msat,
5670+
max_accepted_htlcs: self.holder_max_accepted_htlcs as u64,
5671+
max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
5672+
}
5673+
}
5674+
5675+
fn get_counterparty_channel_constraints(&self, funding: &FundingScope) -> ChannelConstraints {
5676+
ChannelConstraints {
5677+
dust_limit_satoshis: self.counterparty_dust_limit_satoshis,
5678+
channel_reserve_satoshis: funding.holder_selected_channel_reserve_satoshis,
5679+
htlc_minimum_msat: self.counterparty_htlc_minimum_msat,
5680+
max_accepted_htlcs: self.counterparty_max_accepted_htlcs as u64,
5681+
max_htlc_value_in_flight_msat: self.counterparty_max_htlc_value_in_flight_msat,
5682+
}
5683+
}
5684+
56655685
#[rustfmt::skip]
56665686
fn get_available_balances_for_scope<F: Deref>(
56675687
&self, funding: &FundingScope, fee_estimator: &LowerBoundedFeeEstimator<F>,
@@ -5670,6 +5690,8 @@ where
56705690
F::Target: FeeEstimator,
56715691
{
56725692
let context = &self;
5693+
let holder_channel_constraints = self.get_holder_channel_constraints(funding);
5694+
let counterparty_channel_constraints = self.get_counterparty_channel_constraints(funding);
56735695
// Note that we have to handle overflow due to the case mentioned in the docs in general
56745696
// here.
56755697

@@ -5688,7 +5710,7 @@ where
56885710

56895711
let outbound_capacity_msat = local_balance_before_fee_msat
56905712
.saturating_sub(
5691-
funding.counterparty_selected_channel_reserve_satoshis.unwrap_or(0) * 1000);
5713+
holder_channel_constraints.channel_reserve_satoshis * 1000);
56925714

56935715
let mut available_capacity_msat = outbound_capacity_msat;
56945716
let (real_htlc_success_tx_fee_sat, real_htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
@@ -5709,7 +5731,7 @@ where
57095731
Some(())
57105732
};
57115733

5712-
let real_dust_limit_timeout_sat = real_htlc_timeout_tx_fee_sat + context.holder_dust_limit_satoshis;
5734+
let real_dust_limit_timeout_sat = real_htlc_timeout_tx_fee_sat + holder_channel_constraints.dust_limit_satoshis;
57135735
let htlc_above_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000, HTLCInitiator::LocalOffered);
57145736
let mut max_reserved_commit_tx_fee_msat = context.next_local_commit_tx_fee_msat(&funding, htlc_above_dust, fee_spike_buffer_htlc);
57155737
let htlc_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000 - 1, HTLCInitiator::LocalOffered);
@@ -5733,19 +5755,19 @@ where
57335755
} else {
57345756
// If the channel is inbound (i.e. counterparty pays the fee), we need to make sure
57355757
// sending a new HTLC won't reduce their balance below our reserve threshold.
5736-
let real_dust_limit_success_sat = real_htlc_success_tx_fee_sat + context.counterparty_dust_limit_satoshis;
5758+
let real_dust_limit_success_sat = real_htlc_success_tx_fee_sat + counterparty_channel_constraints.dust_limit_satoshis;
57375759
let htlc_above_dust = HTLCCandidate::new(real_dust_limit_success_sat * 1000, HTLCInitiator::LocalOffered);
57385760
let max_reserved_commit_tx_fee_msat = context.next_remote_commit_tx_fee_msat(funding, Some(htlc_above_dust), None);
57395761

5740-
let holder_selected_chan_reserve_msat = funding.holder_selected_channel_reserve_satoshis * 1000;
5762+
let holder_selected_chan_reserve_msat = counterparty_channel_constraints.channel_reserve_satoshis * 1000;
57415763
if remote_balance_before_fee_msat < max_reserved_commit_tx_fee_msat + holder_selected_chan_reserve_msat {
57425764
// If another HTLC's fee would reduce the remote's balance below the reserve limit
57435765
// we've selected for them, we can only send dust HTLCs.
57445766
available_capacity_msat = cmp::min(available_capacity_msat, real_dust_limit_success_sat * 1000 - 1);
57455767
}
57465768
}
57475769

5748-
let mut next_outbound_htlc_minimum_msat = context.counterparty_htlc_minimum_msat;
5770+
let mut next_outbound_htlc_minimum_msat = counterparty_channel_constraints.htlc_minimum_msat;
57495771

57505772
// If we get close to our maximum dust exposure, we end up in a situation where we can send
57515773
// between zero and the remaining dust exposure limit remaining OR above the dust limit.
@@ -5759,8 +5781,8 @@ where
57595781
let (buffer_htlc_success_tx_fee_sat, buffer_htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
57605782
funding.get_channel_type(), dust_buffer_feerate,
57615783
);
5762-
let buffer_dust_limit_success_sat = buffer_htlc_success_tx_fee_sat + context.counterparty_dust_limit_satoshis;
5763-
let buffer_dust_limit_timeout_sat = buffer_htlc_timeout_tx_fee_sat + context.holder_dust_limit_satoshis;
5784+
let buffer_dust_limit_success_sat = buffer_htlc_success_tx_fee_sat + counterparty_channel_constraints.dust_limit_satoshis;
5785+
let buffer_dust_limit_timeout_sat = buffer_htlc_timeout_tx_fee_sat + holder_channel_constraints.dust_limit_satoshis;
57645786

57655787
if let Some(extra_htlc_dust_exposure) = htlc_stats.extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat {
57665788
if extra_htlc_dust_exposure > max_dust_htlc_exposure_msat {
@@ -5794,15 +5816,15 @@ where
57945816
}
57955817

57965818
available_capacity_msat = cmp::min(available_capacity_msat,
5797-
context.counterparty_max_htlc_value_in_flight_msat - htlc_stats.pending_outbound_htlcs_value_msat);
5819+
counterparty_channel_constraints.max_htlc_value_in_flight_msat - htlc_stats.pending_outbound_htlcs_value_msat);
57985820

5799-
if htlc_stats.pending_outbound_htlcs + 1 > context.counterparty_max_accepted_htlcs as usize {
5821+
if htlc_stats.pending_outbound_htlcs + 1 > counterparty_channel_constraints.max_accepted_htlcs as usize {
58005822
available_capacity_msat = 0;
58015823
}
58025824

58035825
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.
58045826
AvailableBalances {
5805-
inbound_capacity_msat: remote_balance_before_fee_msat.saturating_sub(funding.holder_selected_channel_reserve_satoshis * 1000),
5827+
inbound_capacity_msat: remote_balance_before_fee_msat.saturating_sub(counterparty_channel_constraints.channel_reserve_satoshis * 1000),
58065828
outbound_capacity_msat,
58075829
next_outbound_htlc_limit_msat: available_capacity_msat,
58085830
next_outbound_htlc_minimum_msat,

lightning/src/sign/tx_builder.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,14 @@ fn get_dust_buffer_feerate(feerate_per_kw: u32) -> u32 {
154154
cmp::max(feerate_per_kw.saturating_add(2530), feerate_plus_quarter.unwrap_or(u32::MAX))
155155
}
156156

157+
pub(crate) struct ChannelConstraints {
158+
pub dust_limit_satoshis: u64,
159+
pub channel_reserve_satoshis: u64,
160+
pub htlc_minimum_msat: u64,
161+
pub max_htlc_value_in_flight_msat: u64,
162+
pub max_accepted_htlcs: u64,
163+
}
164+
157165
pub(crate) trait TxBuilder {
158166
fn get_next_commitment_stats(
159167
&self, local: bool, is_outbound_from_holder: bool, channel_value_satoshis: u64,

0 commit comments

Comments
 (0)