Skip to content

Commit b9baa45

Browse files
Make our in-flight limit percentage configurable
1 parent 637fb88 commit b9baa45

File tree

2 files changed

+50
-9
lines changed

2 files changed

+50
-9
lines changed

lightning/src/ln/channel.rs

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -789,8 +789,13 @@ macro_rules! secp_check {
789789

790790
impl<Signer: Sign> Channel<Signer> {
791791
// Convert constants + channel value to limits:
792-
fn get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis: u64) -> u64 {
793-
channel_value_satoshis * 1000 / 10 //TODO
792+
fn get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis: u64, config: ChannelConfig) -> u64 {
793+
channel_value_satoshis * 10 * u64::from(config.holder_max_htlc_value_in_flight_msat_channel_value_percent)
794+
}
795+
796+
fn config_htlc_in_flight_value_is_valid(config: ChannelConfig) -> bool {
797+
let htlc_in_flight_value = config.holder_max_htlc_value_in_flight_msat_channel_value_percent;
798+
htlc_in_flight_value >= 1 && htlc_in_flight_value <= 100
794799
}
795800

796801
/// Returns a minimum channel reserve value the remote needs to maintain,
@@ -865,6 +870,13 @@ impl<Signer: Sign> Channel<Signer> {
865870
return Err(APIError::APIMisuseError { err: format!("Holder selected channel reserve below implemention limit dust_limit_satoshis {}", holder_selected_channel_reserve_satoshis) });
866871
}
867872

873+
// Check that config has valid values set
874+
if !Self::config_htlc_in_flight_value_is_valid(config.channel_options){
875+
return Err(APIError::APIMisuseError { err:format!(
876+
"UserConfig::channel_options::holder_max_htlc_value_in_flight_msat_channel_value_percent must be set to a value between 1-100. Current value set ({})",
877+
config.channel_options.holder_max_htlc_value_in_flight_msat_channel_value_percent)});
878+
}
879+
868880
let feerate = fee_estimator.get_est_sat_per_1000_weight(ConfirmationTarget::Normal);
869881

870882
let value_to_self_msat = channel_value_satoshis * 1000 - push_msat;
@@ -946,7 +958,7 @@ impl<Signer: Sign> Channel<Signer> {
946958
counterparty_dust_limit_satoshis: 0,
947959
holder_dust_limit_satoshis: MIN_CHAN_DUST_LIMIT_SATOSHIS,
948960
counterparty_max_htlc_value_in_flight_msat: 0,
949-
holder_max_htlc_value_in_flight_msat: Self::get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis),
961+
holder_max_htlc_value_in_flight_msat: Self::get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis, config.channel_options),
950962
counterparty_selected_channel_reserve_satoshis: None, // Filled in in accept_channel
951963
holder_selected_channel_reserve_satoshis,
952964
counterparty_htlc_minimum_msat: 0,
@@ -1129,6 +1141,13 @@ impl<Signer: Sign> Channel<Signer> {
11291141
return Err(ChannelError::Close(format!("dust_limit_satoshis ({}) is greater than the implementation limit ({})", msg.dust_limit_satoshis, MAX_CHAN_DUST_LIMIT_SATOSHIS)));
11301142
}
11311143

1144+
// Check that config has valid values set
1145+
if !Self::config_htlc_in_flight_value_is_valid(config.channel_options){
1146+
return Err(ChannelError::Close(format!(
1147+
"UserConfig::channel_options::holder_max_htlc_value_in_flight_msat_channel_value_percent must be set to a value between 1-100. Current value set ({})",
1148+
config.channel_options.holder_max_htlc_value_in_flight_msat_channel_value_percent)));
1149+
}
1150+
11321151
// Convert things into internal flags and prep our state:
11331152

11341153
if config.peer_channel_config_limits.force_announced_channel_preference {
@@ -1259,7 +1278,7 @@ impl<Signer: Sign> Channel<Signer> {
12591278
counterparty_dust_limit_satoshis: msg.dust_limit_satoshis,
12601279
holder_dust_limit_satoshis: MIN_CHAN_DUST_LIMIT_SATOSHIS,
12611280
counterparty_max_htlc_value_in_flight_msat: cmp::min(msg.max_htlc_value_in_flight_msat, msg.funding_satoshis * 1000),
1262-
holder_max_htlc_value_in_flight_msat: Self::get_holder_max_htlc_value_in_flight_msat(msg.funding_satoshis),
1281+
holder_max_htlc_value_in_flight_msat: Self::get_holder_max_htlc_value_in_flight_msat(msg.funding_satoshis, config.channel_options),
12631282
counterparty_selected_channel_reserve_satoshis: Some(msg.channel_reserve_satoshis),
12641283
holder_selected_channel_reserve_satoshis,
12651284
counterparty_htlc_minimum_msat: msg.htlc_minimum_msat,
@@ -5855,13 +5874,14 @@ impl<Signer: Sign> Writeable for Channel<Signer> {
58555874
let chan_type = if self.channel_type != ChannelTypeFeatures::only_static_remote_key() {
58565875
Some(&self.channel_type) } else { None };
58575876

5858-
// The same logic applies for `holder_selected_channel_reserve_satoshis` and
5859-
// `holder_max_htlc_value_in_flight_msat` values other than the defaults.
5877+
// The same logic applies for `holder_selected_channel_reserve_satoshis` values other than
5878+
// the default, and when `holder_max_htlc_value_in_flight_msat` has a different value then
5879+
// the result of `get_holder_max_htlc_value_in_flight_msat` with the set channel `config`.
58605880
let serialized_holder_selected_reserve =
58615881
if self.holder_selected_channel_reserve_satoshis != Self::get_holder_selected_channel_reserve_satoshis(self.channel_value_satoshis)
58625882
{ Some(self.holder_selected_channel_reserve_satoshis) } else { None };
58635883
let serialized_holder_htlc_max_in_flight =
5864-
if self.holder_max_htlc_value_in_flight_msat != Self::get_holder_max_htlc_value_in_flight_msat(self.channel_value_satoshis)
5884+
if self.holder_max_htlc_value_in_flight_msat != Self::get_holder_max_htlc_value_in_flight_msat(self.channel_value_satoshis, self.config)
58655885
{ Some(self.holder_max_htlc_value_in_flight_msat) } else { None };
58665886

58675887
write_tlv_fields!(writer, {
@@ -6131,7 +6151,6 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
61316151
let mut target_closing_feerate_sats_per_kw = None;
61326152
let mut monitor_pending_finalized_fulfills = Some(Vec::new());
61336153
let mut holder_selected_channel_reserve_satoshis = Some(Self::get_holder_selected_channel_reserve_satoshis(channel_value_satoshis));
6134-
let mut holder_max_htlc_value_in_flight_msat = Some(Self::get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis));
61356154
// Prior to supporting channel type negotiation, all of our channels were static_remotekey
61366155
// only, so we default to that if none was written.
61376156
let mut channel_type = Some(ChannelTypeFeatures::only_static_remote_key());
@@ -6143,6 +6162,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
61436162
let mut announcement_sigs_state = Some(AnnouncementSigsState::NotSent);
61446163
let mut latest_inbound_scid_alias = None;
61456164
let mut outbound_scid_alias = None;
6165+
let mut tlv_holder_max_htlc_value_in_flight_msat = None;
61466166

61476167
read_tlv_fields!(reader, {
61486168
(0, announcement_sigs, option),
@@ -6151,7 +6171,7 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
61516171
(3, counterparty_selected_channel_reserve_satoshis, option),
61526172
(4, holder_selected_channel_reserve_satoshis, option),
61536173
(5, config, option), // Note that if none is provided we will *not* overwrite the existing one.
6154-
(6, holder_max_htlc_value_in_flight_msat, option),
6174+
(6, tlv_holder_max_htlc_value_in_flight_msat, option),
61556175
(7, shutdown_scriptpubkey, option),
61566176
(9, target_closing_feerate_sats_per_kw, option),
61576177
(11, monitor_pending_finalized_fulfills, vec_type),
@@ -6162,6 +6182,18 @@ impl<'a, Signer: Sign, K: Deref> ReadableArgs<(&'a K, u32)> for Channel<Signer>
61626182
(21, outbound_scid_alias, option),
61636183
});
61646184

6185+
// Note that we need to call `get_holder_max_htlc_value_in_flight_msat` after the tlv
6186+
// fields have been read, as
6187+
// `config.holder_max_htlc_value_in_flight_msat_channel_value_percent` always will have the
6188+
// default value prior to that.
6189+
// If `tlv_holder_max_htlc_value_in_flight_msat` was set in the tlv fields, the
6190+
// `holder_max_htlc_value_in_flight_msat` was written with a different value then result of
6191+
// `get_holder_max_htlc_value_in_flight_msat` with the channel `config`, and the written
6192+
// value should therefore be used.
6193+
let holder_max_htlc_value_in_flight_msat = tlv_holder_max_htlc_value_in_flight_msat
6194+
.map(|max_htlc_value_in_flight| max_htlc_value_in_flight)
6195+
.or(Some(Self::get_holder_max_htlc_value_in_flight_msat(channel_value_satoshis, config.unwrap())));
6196+
61656197
if let Some(preimages) = preimages_opt {
61666198
let mut iter = preimages.into_iter();
61676199
for htlc in pending_outbound_htlcs.iter_mut() {

lightning/src/util/config.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,13 @@ pub struct ChannelConfig {
269269
/// [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal
270270
/// [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background
271271
pub force_close_avoidance_max_fee_satoshis: u64,
272+
/// Sets how many the percent of the channel value the holder_max_htlc_value_in_flight_msat
273+
/// will be assigned as.
274+
///
275+
/// Should be set to a value between 1-100, where the value signals the percantage to be used.
276+
///
277+
/// Default value: 10.
278+
pub holder_max_htlc_value_in_flight_msat_channel_value_percent: u8,
272279
}
273280

274281
impl Default for ChannelConfig {
@@ -282,6 +289,7 @@ impl Default for ChannelConfig {
282289
commit_upfront_shutdown_pubkey: true,
283290
max_dust_htlc_exposure_msat: 5_000_000,
284291
force_close_avoidance_max_fee_satoshis: 1000,
292+
holder_max_htlc_value_in_flight_msat_channel_value_percent: 10,
285293
}
286294
}
287295
}
@@ -292,6 +300,7 @@ impl_writeable_tlv_based!(ChannelConfig, {
292300
(2, cltv_expiry_delta, required),
293301
(3, force_close_avoidance_max_fee_satoshis, (default_value, 1000)),
294302
(4, announced_channel, required),
303+
(5, holder_max_htlc_value_in_flight_msat_channel_value_percent, (default_value, 10)),
295304
(6, commit_upfront_shutdown_pubkey, required),
296305
(8, forwarding_fee_base_msat, required),
297306
});

0 commit comments

Comments
 (0)