Skip to content

Commit 09b9a19

Browse files
committed
Replace OUR_MAX_HTLCS constant with config knob
holder_max_accepted_htlcs Writes an even TLV if the value isn't 50
1 parent e1e3819 commit 09b9a19

File tree

3 files changed

+39
-14
lines changed

3 files changed

+39
-14
lines changed

lightning/src/ln/channel.rs

+22-11
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ pub(super) struct Channel<Signer: ChannelSigner> {
653653
pub counterparty_max_accepted_htlcs: u16,
654654
#[cfg(not(test))]
655655
counterparty_max_accepted_htlcs: u16,
656-
//implied by OUR_MAX_HTLCS: max_accepted_htlcs: u16,
656+
holder_max_accepted_htlcs: u16,
657657
minimum_depth: Option<u32>,
658658

659659
counterparty_forwarding_info: Option<CounterpartyForwardingInfo>,
@@ -756,7 +756,7 @@ struct CommitmentTxInfoCached {
756756
feerate: u32,
757757
}
758758

759-
pub const OUR_MAX_HTLCS: u16 = 50; //TODO
759+
pub const DEFAULT_MAX_HTLCS: u16 = 50;
760760

761761
pub(crate) fn commitment_tx_base_weight(opt_anchors: bool) -> u64 {
762762
const COMMITMENT_TX_BASE_WEIGHT: u64 = 724;
@@ -1072,6 +1072,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
10721072
counterparty_htlc_minimum_msat: 0,
10731073
holder_htlc_minimum_msat: if config.channel_handshake_config.our_htlc_minimum_msat == 0 { 1 } else { config.channel_handshake_config.our_htlc_minimum_msat },
10741074
counterparty_max_accepted_htlcs: 0,
1075+
holder_max_accepted_htlcs: config.channel_handshake_config.our_max_accepted_htlcs,
10751076
minimum_depth: None, // Filled in in accept_channel
10761077

10771078
counterparty_forwarding_info: None,
@@ -1419,6 +1420,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
14191420
counterparty_htlc_minimum_msat: msg.htlc_minimum_msat,
14201421
holder_htlc_minimum_msat: if config.channel_handshake_config.our_htlc_minimum_msat == 0 { 1 } else { config.channel_handshake_config.our_htlc_minimum_msat },
14211422
counterparty_max_accepted_htlcs: msg.max_accepted_htlcs,
1423+
holder_max_accepted_htlcs: config.channel_handshake_config.our_max_accepted_htlcs,
14221424
minimum_depth: Some(cmp::max(config.channel_handshake_config.minimum_depth, 1)),
14231425

14241426
counterparty_forwarding_info: None,
@@ -2874,8 +2876,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
28742876

28752877
let inbound_stats = self.get_inbound_pending_htlc_stats(None);
28762878
let outbound_stats = self.get_outbound_pending_htlc_stats(None);
2877-
if inbound_stats.pending_htlcs + 1 > OUR_MAX_HTLCS as u32 {
2878-
return Err(ChannelError::Close(format!("Remote tried to push more than our max accepted HTLCs ({})", OUR_MAX_HTLCS)));
2879+
if inbound_stats.pending_htlcs + 1 > self.holder_max_accepted_htlcs as u32 {
2880+
return Err(ChannelError::Close(format!("Remote tried to push more than our max accepted HTLCs ({})", self.holder_max_accepted_htlcs)));
28792881
}
28802882
if inbound_stats.pending_htlcs_value_msat + msg.amount_msat > self.holder_max_htlc_value_in_flight_msat {
28812883
return Err(ChannelError::Close(format!("Remote HTLC add would put them over our max HTLC value ({})", self.holder_max_htlc_value_in_flight_msat)));
@@ -5313,7 +5315,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
53135315
htlc_minimum_msat: self.holder_htlc_minimum_msat,
53145316
feerate_per_kw: self.feerate_per_kw as u32,
53155317
to_self_delay: self.get_holder_selected_contest_delay(),
5316-
max_accepted_htlcs: OUR_MAX_HTLCS,
5318+
max_accepted_htlcs: self.holder_max_accepted_htlcs,
53175319
funding_pubkey: keys.funding_pubkey,
53185320
revocation_basepoint: keys.revocation_basepoint,
53195321
payment_point: keys.payment_point,
@@ -5380,7 +5382,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
53805382
htlc_minimum_msat: self.holder_htlc_minimum_msat,
53815383
minimum_depth: self.minimum_depth.unwrap(),
53825384
to_self_delay: self.get_holder_selected_contest_delay(),
5383-
max_accepted_htlcs: OUR_MAX_HTLCS,
5385+
max_accepted_htlcs: self.holder_max_accepted_htlcs,
53845386
funding_pubkey: keys.funding_pubkey,
53855387
revocation_basepoint: keys.revocation_basepoint,
53865388
payment_point: keys.payment_point,
@@ -6496,6 +6498,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
64966498
// we write the high bytes as an option here.
64976499
let user_id_high_opt = Some((self.user_id >> 64) as u64);
64986500

6501+
let holder_max_accepted_htlcs = if self.holder_max_accepted_htlcs == 50 { None } else { Some(self.holder_max_accepted_htlcs) };
6502+
64996503
write_tlv_fields!(writer, {
65006504
(0, self.announcement_sigs, option),
65016505
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -6521,6 +6525,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
65216525
(23, channel_ready_event_emitted, option),
65226526
(25, user_id_high_opt, option),
65236527
(27, self.channel_keys_id, required),
6528+
(28, holder_max_accepted_htlcs, option),
65246529
(29, self.temporary_channel_id, option),
65256530
(31, channel_pending_event_emitted, option),
65266531
});
@@ -6589,7 +6594,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
65896594
let value_to_self_msat = Readable::read(reader)?;
65906595

65916596
let pending_inbound_htlc_count: u64 = Readable::read(reader)?;
6592-
let mut pending_inbound_htlcs = Vec::with_capacity(cmp::min(pending_inbound_htlc_count as usize, OUR_MAX_HTLCS as usize));
6597+
6598+
let mut pending_inbound_htlcs = Vec::with_capacity(cmp::min(pending_inbound_htlc_count as usize, DEFAULT_MAX_HTLCS as usize));
65936599
for _ in 0..pending_inbound_htlc_count {
65946600
pending_inbound_htlcs.push(InboundHTLCOutput {
65956601
htlc_id: Readable::read(reader)?,
@@ -6607,7 +6613,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
66076613
}
66086614

66096615
let pending_outbound_htlc_count: u64 = Readable::read(reader)?;
6610-
let mut pending_outbound_htlcs = Vec::with_capacity(cmp::min(pending_outbound_htlc_count as usize, OUR_MAX_HTLCS as usize));
6616+
let mut pending_outbound_htlcs = Vec::with_capacity(cmp::min(pending_outbound_htlc_count as usize, DEFAULT_MAX_HTLCS as usize));
66116617
for _ in 0..pending_outbound_htlc_count {
66126618
pending_outbound_htlcs.push(OutboundHTLCOutput {
66136619
htlc_id: Readable::read(reader)?,
@@ -6636,7 +6642,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
66366642
}
66376643

66386644
let holding_cell_htlc_update_count: u64 = Readable::read(reader)?;
6639-
let mut holding_cell_htlc_updates = Vec::with_capacity(cmp::min(holding_cell_htlc_update_count as usize, OUR_MAX_HTLCS as usize*2));
6645+
let mut holding_cell_htlc_updates = Vec::with_capacity(cmp::min(holding_cell_htlc_update_count as usize, DEFAULT_MAX_HTLCS as usize*2));
66406646
for _ in 0..holding_cell_htlc_update_count {
66416647
holding_cell_htlc_updates.push(match <u8 as Readable>::read(reader)? {
66426648
0 => HTLCUpdateAwaitingACK::AddHTLC {
@@ -6669,13 +6675,13 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
66696675
let monitor_pending_commitment_signed = Readable::read(reader)?;
66706676

66716677
let monitor_pending_forwards_count: u64 = Readable::read(reader)?;
6672-
let mut monitor_pending_forwards = Vec::with_capacity(cmp::min(monitor_pending_forwards_count as usize, OUR_MAX_HTLCS as usize));
6678+
let mut monitor_pending_forwards = Vec::with_capacity(cmp::min(monitor_pending_forwards_count as usize, DEFAULT_MAX_HTLCS as usize));
66736679
for _ in 0..monitor_pending_forwards_count {
66746680
monitor_pending_forwards.push((Readable::read(reader)?, Readable::read(reader)?));
66756681
}
66766682

66776683
let monitor_pending_failures_count: u64 = Readable::read(reader)?;
6678-
let mut monitor_pending_failures = Vec::with_capacity(cmp::min(monitor_pending_failures_count as usize, OUR_MAX_HTLCS as usize));
6684+
let mut monitor_pending_failures = Vec::with_capacity(cmp::min(monitor_pending_failures_count as usize, DEFAULT_MAX_HTLCS as usize));
66796685
for _ in 0..monitor_pending_failures_count {
66806686
monitor_pending_failures.push((Readable::read(reader)?, Readable::read(reader)?, Readable::read(reader)?));
66816687
}
@@ -6796,6 +6802,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
67966802
let mut user_id_high_opt: Option<u64> = None;
67976803
let mut channel_keys_id: Option<[u8; 32]> = None;
67986804
let mut temporary_channel_id: Option<[u8; 32]> = None;
6805+
let mut holder_max_accepted_htlcs: Option<u16> = None;
67996806

68006807
read_tlv_fields!(reader, {
68016808
(0, announcement_sigs, option),
@@ -6816,6 +6823,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68166823
(23, channel_ready_event_emitted, option),
68176824
(25, user_id_high_opt, option),
68186825
(27, channel_keys_id, option),
6826+
(28, holder_max_accepted_htlcs, option),
68196827
(29, temporary_channel_id, option),
68206828
(31, channel_pending_event_emitted, option),
68216829
});
@@ -6870,6 +6878,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68706878
// separate u64 values.
68716879
let user_id = user_id_low as u128 + ((user_id_high_opt.unwrap_or(0) as u128) << 64);
68726880

6881+
let holder_max_accepted_htlcs = holder_max_accepted_htlcs.unwrap_or(DEFAULT_MAX_HTLCS);
6882+
68736883
Ok(Channel {
68746884
user_id,
68756885

@@ -6898,6 +6908,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68986908
cur_counterparty_commitment_transaction_number,
68996909
value_to_self_msat,
69006910

6911+
holder_max_accepted_htlcs,
69016912
pending_inbound_htlcs,
69026913
pending_outbound_htlcs,
69036914
holding_cell_htlc_updates,

lightning/src/ln/functional_tests.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ fn holding_cell_htlc_counting() {
11071107
let chan_2 = create_announced_chan_between_nodes(&nodes, 1, 2);
11081108

11091109
let mut payments = Vec::new();
1110-
for _ in 0..crate::ln::channel::OUR_MAX_HTLCS {
1110+
for _ in 0..50 {
11111111
let (route, payment_hash, payment_preimage, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[2], 100000);
11121112
nodes[1].node.send_payment_with_route(&route, payment_hash,
11131113
RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
@@ -6278,11 +6278,11 @@ fn test_update_add_htlc_bolt2_receiver_check_max_htlc_limit() {
62786278
onion_routing_packet: onion_packet.clone(),
62796279
};
62806280

6281-
for i in 0..super::channel::OUR_MAX_HTLCS {
6281+
for i in 0..50 {
62826282
msg.htlc_id = i as u64;
62836283
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
62846284
}
6285-
msg.htlc_id = (super::channel::OUR_MAX_HTLCS) as u64;
6285+
msg.htlc_id = (50) as u64;
62866286
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &msg);
62876287

62886288
assert!(nodes[1].node.list_channels().is_empty());

lightning/src/util/config.rs

+14
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,19 @@ pub struct ChannelHandshakeConfig {
170170
/// [`DecodeError::InvalidValue`]: crate::ln::msgs::DecodeError::InvalidValue
171171
/// [`SIGHASH_SINGLE + update_fee Considered Harmful`]: https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-September/002796.html
172172
pub negotiate_anchors_zero_fee_htlc_tx: bool,
173+
174+
/// Set to limit the number of HTLCs a remote can push.
175+
///
176+
/// If a counterparty exceeds the threshold, throw an error and close the channel.
177+
///
178+
/// Increasing the value can help improve liquidity and stability in
179+
/// routing at the cost of higher memory & compute consumption.
180+
///
181+
/// Note: Versions of LDK earlier than v0.0.115 will fail to read channels with a configuration
182+
/// other than the default value.
183+
///
184+
/// Default value: 50
185+
pub our_max_accepted_htlcs: u16,
173186
}
174187

175188
impl Default for ChannelHandshakeConfig {
@@ -185,6 +198,7 @@ impl Default for ChannelHandshakeConfig {
185198
their_channel_reserve_proportional_millionths: 10_000,
186199
#[cfg(anchors)]
187200
negotiate_anchors_zero_fee_htlc_tx: false,
201+
our_max_accepted_htlcs: 50,
188202
}
189203
}
190204
}

0 commit comments

Comments
 (0)