Skip to content

Commit f8f43c8

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

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
@@ -74,7 +74,7 @@ use crate::ln::script::{self, ShutdownScript};
7474
use crate::ln::types::ChannelId;
7575
use crate::routing::gossip::NodeId;
7676
use crate::sign::ecdsa::EcdsaChannelSigner;
77-
use crate::sign::tx_builder::{HTLCAmountDirection, NextCommitmentStats, SpecTxBuilder, TxBuilder};
77+
use crate::sign::tx_builder::{HTLCAmountDirection, ChannelConstraints, NextCommitmentStats, SpecTxBuilder, TxBuilder};
7878
use crate::sign::{ChannelSigner, EntropySource, NodeSigner, Recipient, SignerProvider};
7979
use crate::types::features::{ChannelTypeFeatures, InitFeatures};
8080
use crate::types::payment::{PaymentHash, PaymentPreimage};
@@ -5058,6 +5058,26 @@ where
50585058
outbound_details
50595059
}
50605060

5061+
fn get_holder_channel_constraints(&self, funding: &FundingScope) -> ChannelConstraints {
5062+
ChannelConstraints {
5063+
dust_limit_satoshis: self.holder_dust_limit_satoshis,
5064+
channel_reserve_satoshis: funding.counterparty_selected_channel_reserve_satoshis.unwrap_or(0),
5065+
htlc_minimum_msat: self.holder_htlc_minimum_msat,
5066+
max_accepted_htlcs: self.holder_max_accepted_htlcs as u64,
5067+
max_htlc_value_in_flight_msat: self.holder_max_htlc_value_in_flight_msat,
5068+
}
5069+
}
5070+
5071+
fn get_counterparty_channel_constraints(&self, funding: &FundingScope) -> ChannelConstraints {
5072+
ChannelConstraints {
5073+
dust_limit_satoshis: self.counterparty_dust_limit_satoshis,
5074+
channel_reserve_satoshis: funding.holder_selected_channel_reserve_satoshis,
5075+
htlc_minimum_msat: self.counterparty_htlc_minimum_msat,
5076+
max_accepted_htlcs: self.counterparty_max_accepted_htlcs as u64,
5077+
max_htlc_value_in_flight_msat: self.counterparty_max_htlc_value_in_flight_msat,
5078+
}
5079+
}
5080+
50615081
#[rustfmt::skip]
50625082
fn get_available_balances_for_scope<F: Deref>(
50635083
&self, funding: &FundingScope, fee_estimator: &LowerBoundedFeeEstimator<F>,
@@ -5066,6 +5086,8 @@ where
50665086
F::Target: FeeEstimator,
50675087
{
50685088
let context = &self;
5089+
let holder_channel_constraints = self.get_holder_channel_constraints(funding);
5090+
let counterparty_channel_constraints = self.get_counterparty_channel_constraints(funding);
50695091
// Note that we have to handle overflow due to the case mentioned in the docs in general
50705092
// here.
50715093

@@ -5084,7 +5106,7 @@ where
50845106

50855107
let outbound_capacity_msat = local_balance_before_fee_msat
50865108
.saturating_sub(
5087-
funding.counterparty_selected_channel_reserve_satoshis.unwrap_or(0) * 1000);
5109+
holder_channel_constraints.channel_reserve_satoshis * 1000);
50885110

50895111
let mut available_capacity_msat = outbound_capacity_msat;
50905112
let (real_htlc_success_tx_fee_sat, real_htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
@@ -5105,7 +5127,7 @@ where
51055127
Some(())
51065128
};
51075129

5108-
let real_dust_limit_timeout_sat = real_htlc_timeout_tx_fee_sat + context.holder_dust_limit_satoshis;
5130+
let real_dust_limit_timeout_sat = real_htlc_timeout_tx_fee_sat + holder_channel_constraints.dust_limit_satoshis;
51095131
let htlc_above_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000, HTLCInitiator::LocalOffered);
51105132
let mut max_reserved_commit_tx_fee_msat = context.next_local_commit_tx_fee_msat(&funding, htlc_above_dust, fee_spike_buffer_htlc);
51115133
let htlc_dust = HTLCCandidate::new(real_dust_limit_timeout_sat * 1000 - 1, HTLCInitiator::LocalOffered);
@@ -5129,19 +5151,19 @@ where
51295151
} else {
51305152
// If the channel is inbound (i.e. counterparty pays the fee), we need to make sure
51315153
// sending a new HTLC won't reduce their balance below our reserve threshold.
5132-
let real_dust_limit_success_sat = real_htlc_success_tx_fee_sat + context.counterparty_dust_limit_satoshis;
5154+
let real_dust_limit_success_sat = real_htlc_success_tx_fee_sat + counterparty_channel_constraints.dust_limit_satoshis;
51335155
let htlc_above_dust = HTLCCandidate::new(real_dust_limit_success_sat * 1000, HTLCInitiator::LocalOffered);
51345156
let max_reserved_commit_tx_fee_msat = context.next_remote_commit_tx_fee_msat(funding, Some(htlc_above_dust), None);
51355157

5136-
let holder_selected_chan_reserve_msat = funding.holder_selected_channel_reserve_satoshis * 1000;
5158+
let holder_selected_chan_reserve_msat = counterparty_channel_constraints.channel_reserve_satoshis * 1000;
51375159
if remote_balance_before_fee_msat < max_reserved_commit_tx_fee_msat + holder_selected_chan_reserve_msat {
51385160
// If another HTLC's fee would reduce the remote's balance below the reserve limit
51395161
// we've selected for them, we can only send dust HTLCs.
51405162
available_capacity_msat = cmp::min(available_capacity_msat, real_dust_limit_success_sat * 1000 - 1);
51415163
}
51425164
}
51435165

5144-
let mut next_outbound_htlc_minimum_msat = context.counterparty_htlc_minimum_msat;
5166+
let mut next_outbound_htlc_minimum_msat = counterparty_channel_constraints.htlc_minimum_msat;
51455167

51465168
// If we get close to our maximum dust exposure, we end up in a situation where we can send
51475169
// between zero and the remaining dust exposure limit remaining OR above the dust limit.
@@ -5155,8 +5177,8 @@ where
51555177
let (buffer_htlc_success_tx_fee_sat, buffer_htlc_timeout_tx_fee_sat) = second_stage_tx_fees_sat(
51565178
funding.get_channel_type(), dust_buffer_feerate,
51575179
);
5158-
let buffer_dust_limit_success_sat = buffer_htlc_success_tx_fee_sat + context.counterparty_dust_limit_satoshis;
5159-
let buffer_dust_limit_timeout_sat = buffer_htlc_timeout_tx_fee_sat + context.holder_dust_limit_satoshis;
5180+
let buffer_dust_limit_success_sat = buffer_htlc_success_tx_fee_sat + counterparty_channel_constraints.dust_limit_satoshis;
5181+
let buffer_dust_limit_timeout_sat = buffer_htlc_timeout_tx_fee_sat + holder_channel_constraints.dust_limit_satoshis;
51605182

51615183
if let Some(extra_htlc_dust_exposure) = htlc_stats.extra_nondust_htlc_on_counterparty_tx_dust_exposure_msat {
51625184
if extra_htlc_dust_exposure > max_dust_htlc_exposure_msat {
@@ -5190,15 +5212,15 @@ where
51905212
}
51915213

51925214
available_capacity_msat = cmp::min(available_capacity_msat,
5193-
context.counterparty_max_htlc_value_in_flight_msat - htlc_stats.pending_outbound_htlcs_value_msat);
5215+
counterparty_channel_constraints.max_htlc_value_in_flight_msat - htlc_stats.pending_outbound_htlcs_value_msat);
51945216

5195-
if htlc_stats.pending_outbound_htlcs + 1 > context.counterparty_max_accepted_htlcs as usize {
5217+
if htlc_stats.pending_outbound_htlcs + 1 > counterparty_channel_constraints.max_accepted_htlcs as usize {
51965218
available_capacity_msat = 0;
51975219
}
51985220

51995221
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.
52005222
AvailableBalances {
5201-
inbound_capacity_msat: remote_balance_before_fee_msat.saturating_sub(funding.holder_selected_channel_reserve_satoshis * 1000),
5223+
inbound_capacity_msat: remote_balance_before_fee_msat.saturating_sub(counterparty_channel_constraints.channel_reserve_satoshis * 1000),
52025224
outbound_capacity_msat,
52035225
next_outbound_htlc_limit_msat: available_capacity_msat,
52045226
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)