Skip to content

Commit 13104a0

Browse files
committed
Do not write outside TLV.
Do not write TLV if max htlcs is already 50. Read configured max accepted htlcs when opening channels
1 parent aad39a8 commit 13104a0

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lightning/src/ln/channel.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,7 +1019,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
10191019
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
10201020
value_to_self_msat,
10211021

1022-
max_accepted_htlcs: 50,
1022+
max_accepted_htlcs: config.channel_handshake_config.max_accepted_htlcs,
10231023
pending_inbound_htlcs: Vec::new(),
10241024
pending_outbound_htlcs: Vec::new(),
10251025
holding_cell_htlc_updates: Vec::new(),
@@ -1364,7 +1364,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
13641364
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
13651365
value_to_self_msat: msg.push_msat,
13661366

1367-
max_accepted_htlcs: msg.max_accepted_htlcs,
1367+
max_accepted_htlcs: config.channel_handshake_config.max_accepted_htlcs,
13681368
pending_inbound_htlcs: Vec::new(),
13691369
pending_outbound_htlcs: Vec::new(),
13701370
holding_cell_htlc_updates: Vec::new(),
@@ -6208,7 +6208,6 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
62086208
self.cur_counterparty_commitment_transaction_number.write(writer)?;
62096209
self.value_to_self_msat.write(writer)?;
62106210

6211-
self.max_accepted_htlcs.write(writer)?;
62126211
let mut dropped_inbound_htlcs = 0;
62136212
for htlc in self.pending_inbound_htlcs.iter() {
62146213
if let InboundHTLCState::RemoteAnnounced(_) = htlc.state {
@@ -6429,6 +6428,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
64296428
// we write the high bytes as an option here.
64306429
let user_id_high_opt = Some((self.user_id >> 64) as u64);
64316430

6431+
let max_accepted_htlcs = if self.max_accepted_htlcs == 50 { None } else { Some(self.max_accepted_htlcs) };
6432+
64326433
write_tlv_fields!(writer, {
64336434
(0, self.announcement_sigs, option),
64346435
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -6454,7 +6455,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
64546455
(23, channel_ready_event_emitted, option),
64556456
(25, user_id_high_opt, option),
64566457
(27, self.channel_keys_id, required),
6457-
(28, Some(self.max_accepted_htlcs), option),
6458+
(28, max_accepted_htlcs, option),
64586459
});
64596460

64606461
Ok(())
@@ -6520,10 +6521,11 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
65206521
let cur_counterparty_commitment_transaction_number = Readable::read(reader)?;
65216522
let value_to_self_msat = Readable::read(reader)?;
65226523

6523-
let max_accepted_htlcs = Readable::read(reader)?;
65246524
let pending_inbound_htlc_count: u64 = Readable::read(reader)?;
65256525

6526-
let mut pending_inbound_htlcs = Vec::with_capacity(cmp::min(pending_inbound_htlc_count as usize, max_accepted_htlcs as usize));
6526+
let channel_handshake_config = ChannelHandshakeConfig::default();
6527+
6528+
let mut pending_inbound_htlcs = Vec::with_capacity(cmp::min(pending_inbound_htlc_count as usize, channel_handshake_config.max_accepted_htlcs as usize));
65276529
for _ in 0..pending_inbound_htlc_count {
65286530
pending_inbound_htlcs.push(InboundHTLCOutput {
65296531
htlc_id: Readable::read(reader)?,
@@ -6541,7 +6543,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
65416543
}
65426544

65436545
let pending_outbound_htlc_count: u64 = Readable::read(reader)?;
6544-
let mut pending_outbound_htlcs = Vec::with_capacity(cmp::min(pending_outbound_htlc_count as usize, max_accepted_htlcs as usize));
6546+
let mut pending_outbound_htlcs = Vec::with_capacity(cmp::min(pending_outbound_htlc_count as usize, channel_handshake_config.max_accepted_htlcs as usize));
65456547
for _ in 0..pending_outbound_htlc_count {
65466548
pending_outbound_htlcs.push(OutboundHTLCOutput {
65476549
htlc_id: Readable::read(reader)?,
@@ -6570,7 +6572,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
65706572
}
65716573

65726574
let holding_cell_htlc_update_count: u64 = Readable::read(reader)?;
6573-
let mut holding_cell_htlc_updates = Vec::with_capacity(cmp::min(holding_cell_htlc_update_count as usize, max_accepted_htlcs as usize*2));
6575+
let mut holding_cell_htlc_updates = Vec::with_capacity(cmp::min(holding_cell_htlc_update_count as usize, channel_handshake_config.max_accepted_htlcs as usize*2));
65746576
for _ in 0..holding_cell_htlc_update_count {
65756577
holding_cell_htlc_updates.push(match <u8 as Readable>::read(reader)? {
65766578
0 => HTLCUpdateAwaitingACK::AddHTLC {
@@ -6603,13 +6605,13 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
66036605
let monitor_pending_commitment_signed = Readable::read(reader)?;
66046606

66056607
let monitor_pending_forwards_count: u64 = Readable::read(reader)?;
6606-
let mut monitor_pending_forwards = Vec::with_capacity(cmp::min(monitor_pending_forwards_count as usize, max_accepted_htlcs as usize));
6608+
let mut monitor_pending_forwards = Vec::with_capacity(cmp::min(monitor_pending_forwards_count as usize, channel_handshake_config.max_accepted_htlcs as usize));
66076609
for _ in 0..monitor_pending_forwards_count {
66086610
monitor_pending_forwards.push((Readable::read(reader)?, Readable::read(reader)?));
66096611
}
66106612

66116613
let monitor_pending_failures_count: u64 = Readable::read(reader)?;
6612-
let mut monitor_pending_failures = Vec::with_capacity(cmp::min(monitor_pending_failures_count as usize, max_accepted_htlcs as usize));
6614+
let mut monitor_pending_failures = Vec::with_capacity(cmp::min(monitor_pending_failures_count as usize, channel_handshake_config.max_accepted_htlcs as usize));
66136615
for _ in 0..monitor_pending_failures_count {
66146616
monitor_pending_failures.push((Readable::read(reader)?, Readable::read(reader)?, Readable::read(reader)?));
66156617
}
@@ -6728,6 +6730,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
67286730

67296731
let mut user_id_high_opt: Option<u64> = None;
67306732
let mut channel_keys_id: Option<[u8; 32]> = None;
6733+
let mut max_accepted_htlcs: Option<u16> = None;
67316734

67326735
read_tlv_fields!(reader, {
67336736
(0, announcement_sigs, option),
@@ -6748,6 +6751,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
67486751
(23, channel_ready_event_emitted, option),
67496752
(25, user_id_high_opt, option),
67506753
(27, channel_keys_id, option),
6754+
(28, max_accepted_htlcs, option),
67516755
});
67526756

67536757
let (channel_keys_id, holder_signer) = if let Some(channel_keys_id) = channel_keys_id {
@@ -6800,6 +6804,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68006804
// separate u64 values.
68016805
let user_id = user_id_low as u128 + ((user_id_high_opt.unwrap_or(0) as u128) << 64);
68026806

6807+
let max_accepted_htlcs = max_accepted_htlcs.unwrap_or(channel_handshake_config.max_accepted_htlcs);
6808+
68036809
Ok(Channel {
68046810
user_id,
68056811

lightning/src/util/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ 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+
/// Default value: 50
174+
pub max_accepted_htlcs: u16,
173175
}
174176

175177
impl Default for ChannelHandshakeConfig {
@@ -185,6 +187,7 @@ impl Default for ChannelHandshakeConfig {
185187
their_channel_reserve_proportional_millionths: 10_000,
186188
#[cfg(anchors)]
187189
negotiate_anchors_zero_fee_htlc_tx: false,
190+
max_accepted_htlcs: 50,
188191
}
189192
}
190193
}

0 commit comments

Comments
 (0)