Skip to content

Commit e3ee0c0

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

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
@@ -78,7 +78,7 @@ use crate::ln::types::ChannelId;
7878
use crate::ln::LN_MAX_MSG_LEN;
7979
use crate::routing::gossip::NodeId;
8080
use crate::sign::ecdsa::EcdsaChannelSigner;
81-
use crate::sign::tx_builder::{HTLCAmountDirection, NextCommitmentStats, SpecTxBuilder, TxBuilder};
81+
use crate::sign::tx_builder::{HTLCAmountDirection, ChannelConstraints, NextCommitmentStats, SpecTxBuilder, TxBuilder};
8282
use crate::sign::{ChannelSigner, EntropySource, NodeSigner, Recipient, SignerProvider};
8383
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
8484
use crate::types::payment::{PaymentHash, PaymentPreimage};
@@ -5063,6 +5063,26 @@ where
50635063
outbound_details
50645064
}
50655065

5066+
fn get_holder_channel_constraints(&self, funding: &FundingScope) -> ChannelConstraints {
5067+
ChannelConstraints {
5068+
dust_limit_satoshis: self.holder_dust_limit_satoshis,
5069+
channel_reserve_satoshis: funding.counterparty_selected_channel_reserve_satoshis.unwrap_or(0),
5070+
htlc_minimum_msat: self.holder_htlc_minimum_msat,
5071+
max_accepted_htlcs: self.holder_max_accepted_htlcs as u64,
5072+
max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
5073+
}
5074+
}
5075+
5076+
fn get_counterparty_channel_constraints(&self, funding: &FundingScope) -> ChannelConstraints {
5077+
ChannelConstraints {
5078+
dust_limit_satoshis: self.counterparty_dust_limit_satoshis,
5079+
channel_reserve_satoshis: funding.holder_selected_channel_reserve_satoshis,
5080+
htlc_minimum_msat: self.counterparty_htlc_minimum_msat,
5081+
max_accepted_htlcs: self.counterparty_max_accepted_htlcs as u64,
5082+
max_htlc_value_in_flight_msat: self.counterparty_max_htlc_value_in_flight_msat,
5083+
}
5084+
}
5085+
50665086
#[rustfmt::skip]
50675087
fn get_available_balances_for_scope<F: Deref>(
50685088
&self, funding: &FundingScope, fee_estimator: &LowerBoundedFeeEstimator<F>,
@@ -5071,6 +5091,8 @@ where
50715091
F::Target: FeeEstimator,
50725092
{
50735093
let context = &self;
5094+
let holder_channel_constraints = self.get_holder_channel_constraints(funding);
5095+
let counterparty_channel_constraints = self.get_counterparty_channel_constraints(funding);
50745096
// Note that we have to handle overflow due to the case mentioned in the docs in general
50755097
// here.
50765098

@@ -5089,7 +5111,7 @@ where
50895111

50905112
let outbound_capacity_msat = local_balance_before_fee_msat
50915113
.saturating_sub(
5092-
funding.counterparty_selected_channel_reserve_satoshis.unwrap_or(0) * 1000);
5114+
holder_channel_constraints.channel_reserve_satoshis * 1000);
50935115

50945116
let mut available_capacity_msat = outbound_capacity_msat;
50955117
let (real_htlc_success_tx_fee_sat, real_htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
@@ -5110,7 +5132,7 @@ where
51105132
Some(())
51115133
};
51125134

5113-
let real_dust_limit_timeout_sat = real_htlc_timeout_tx_fee_sat + context.holder_dust_limit_satoshis;
5135+
let real_dust_limit_timeout_sat = real_htlc_timeout_tx_fee_sat + holder_channel_constraints.dust_limit_satoshis;
51145136
let htlc_above_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000, HTLCInitiator::LocalOffered);
51155137
let mut max_reserved_commit_tx_fee_msat = context.next_local_commit_tx_fee_msat(&funding, htlc_above_dust, fee_spike_buffer_htlc);
51165138
let htlc_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000 - 1, HTLCInitiator::LocalOffered);
@@ -5134,19 +5156,19 @@ where
51345156
} else {
51355157
// If the channel is inbound (i.e. counterparty pays the fee), we need to make sure
51365158
// sending a new HTLC won't reduce their balance below our reserve threshold.
5137-
let real_dust_limit_success_sat = real_htlc_success_tx_fee_sat + context.counterparty_dust_limit_satoshis;
5159+
let real_dust_limit_success_sat = real_htlc_success_tx_fee_sat + counterparty_channel_constraints.dust_limit_satoshis;
51385160
let htlc_above_dust = HTLCCandidate::new(real_dust_limit_success_sat * 1000, HTLCInitiator::LocalOffered);
51395161
let max_reserved_commit_tx_fee_msat = context.next_remote_commit_tx_fee_msat(funding, Some(htlc_above_dust), None);
51405162

5141-
let holder_selected_chan_reserve_msat = funding.holder_selected_channel_reserve_satoshis * 1000;
5163+
let holder_selected_chan_reserve_msat = counterparty_channel_constraints.channel_reserve_satoshis * 1000;
51425164
if remote_balance_before_fee_msat < max_reserved_commit_tx_fee_msat + holder_selected_chan_reserve_msat {
51435165
// If another HTLC's fee would reduce the remote's balance below the reserve limit
51445166
// we've selected for them, we can only send dust HTLCs.
51455167
available_capacity_msat = cmp::min(available_capacity_msat, real_dust_limit_success_sat * 1000 - 1);
51465168
}
51475169
}
51485170

5149-
let mut next_outbound_htlc_minimum_msat = context.counterparty_htlc_minimum_msat;
5171+
let mut next_outbound_htlc_minimum_msat = counterparty_channel_constraints.htlc_minimum_msat;
51505172

51515173
// If we get close to our maximum dust exposure, we end up in a situation where we can send
51525174
// between zero and the remaining dust exposure limit remaining OR above the dust limit.
@@ -5160,8 +5182,8 @@ where
51605182
let (buffer_htlc_success_tx_fee_sat, buffer_htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
51615183
funding.get_channel_type(), dust_buffer_feerate,
51625184
);
5163-
let buffer_dust_limit_success_sat = buffer_htlc_success_tx_fee_sat + context.counterparty_dust_limit_satoshis;
5164-
let buffer_dust_limit_timeout_sat = buffer_htlc_timeout_tx_fee_sat + context.holder_dust_limit_satoshis;
5185+
let buffer_dust_limit_success_sat = buffer_htlc_success_tx_fee_sat + counterparty_channel_constraints.dust_limit_satoshis;
5186+
let buffer_dust_limit_timeout_sat = buffer_htlc_timeout_tx_fee_sat + holder_channel_constraints.dust_limit_satoshis;
51655187

51665188
if let Some(extra_htlc_dust_exposure) = htlc_stats.extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat {
51675189
if extra_htlc_dust_exposure > max_dust_htlc_exposure_msat {
@@ -5195,15 +5217,15 @@ where
51955217
}
51965218

51975219
available_capacity_msat = cmp::min(available_capacity_msat,
5198-
context.counterparty_max_htlc_value_in_flight_msat - htlc_stats.pending_outbound_htlcs_value_msat);
5220+
counterparty_channel_constraints.max_htlc_value_in_flight_msat - htlc_stats.pending_outbound_htlcs_value_msat);
51995221

5200-
if htlc_stats.pending_outbound_htlcs + 1 > context.counterparty_max_accepted_htlcs as usize {
5222+
if htlc_stats.pending_outbound_htlcs + 1 > counterparty_channel_constraints.max_accepted_htlcs as usize {
52015223
available_capacity_msat = 0;
52025224
}
52035225

52045226
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.
52055227
AvailableBalances {
5206-
inbound_capacity_msat: remote_balance_before_fee_msat.saturating_sub(funding.holder_selected_channel_reserve_satoshis * 1000),
5228+
inbound_capacity_msat: remote_balance_before_fee_msat.saturating_sub(counterparty_channel_constraints.channel_reserve_satoshis * 1000),
52075229
outbound_capacity_msat,
52085230
next_outbound_htlc_limit_msat: available_capacity_msat,
52095231
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
@@ -116,6 +116,14 @@ fn get_dust_buffer_feerate(feerate_per_kw: u32) -> u32 {
116116
cmp::max(feerate_per_kw.saturating_add(2530), feerate_plus_quarter.unwrap_or(u32::MAX))
117117
}
118118

119+
pub(crate) struct ChannelConstraints {
120+
pub dust_limit_satoshis: u64,
121+
pub channel_reserve_satoshis: u64,
122+
pub htlc_minimum_msat: u64,
123+
pub max_htlc_value_in_flight_msat: u64,
124+
pub max_accepted_htlcs: u64,
125+
}
126+
119127
pub(crate) trait TxBuilder {
120128
fn get_next_commitment_stats(
121129
&self, local: bool, is_outbound_from_holder: bool, channel_value_satoshis: u64,

0 commit comments

Comments
 (0)