Skip to content

Commit 9d7bfa8

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 d2909e5 commit 9d7bfa8

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
@@ -1026,7 +1026,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
10261026
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
10271027
value_to_self_msat,
10281028

1029-
max_accepted_htlcs: 50,
1029+
max_accepted_htlcs: config.channel_handshake_config.max_accepted_htlcs,
10301030
pending_inbound_htlcs: Vec::new(),
10311031
pending_outbound_htlcs: Vec::new(),
10321032
holding_cell_htlc_updates: Vec::new(),
@@ -1373,7 +1373,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Channel<Signer> {
13731373
cur_counterparty_commitment_transaction_number: INITIAL_COMMITMENT_NUMBER,
13741374
value_to_self_msat: msg.push_msat,
13751375

1376-
max_accepted_htlcs: msg.max_accepted_htlcs,
1376+
max_accepted_htlcs: config.channel_handshake_config.max_accepted_htlcs,
13771377
pending_inbound_htlcs: Vec::new(),
13781378
pending_outbound_htlcs: Vec::new(),
13791379
holding_cell_htlc_updates: Vec::new(),
@@ -6276,7 +6276,6 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
62766276
self.cur_counterparty_commitment_transaction_number.write(writer)?;
62776277
self.value_to_self_msat.write(writer)?;
62786278

6279-
self.max_accepted_htlcs.write(writer)?;
62806279
let mut dropped_inbound_htlcs = 0;
62816280
for htlc in self.pending_inbound_htlcs.iter() {
62826281
if let InboundHTLCState::RemoteAnnounced(_) = htlc.state {
@@ -6498,6 +6497,8 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
64986497
// we write the high bytes as an option here.
64996498
let user_id_high_opt = Some((self.user_id >> 64) as u64);
65006499

6500+
let max_accepted_htlcs = if self.max_accepted_htlcs == 50 { None } else { Some(self.max_accepted_htlcs) };
6501+
65016502
write_tlv_fields!(writer, {
65026503
(0, self.announcement_sigs, option),
65036504
// minimum_depth and counterparty_selected_channel_reserve_satoshis used to have a
@@ -6523,7 +6524,7 @@ impl<Signer: WriteableEcdsaChannelSigner> Writeable for Channel<Signer> {
65236524
(23, channel_ready_event_emitted, option),
65246525
(25, user_id_high_opt, option),
65256526
(27, self.channel_keys_id, required),
6526-
(28, Some(self.max_accepted_htlcs), option),
6527+
(28, max_accepted_htlcs, option),
65276528
(29, self.temporary_channel_id, option),
65286529
(31, channel_pending_event_emitted, option),
65296530
});
@@ -6591,10 +6592,11 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
65916592
let cur_counterparty_commitment_transaction_number = Readable::read(reader)?;
65926593
let value_to_self_msat = Readable::read(reader)?;
65936594

6594-
let max_accepted_htlcs = Readable::read(reader)?;
65956595
let pending_inbound_htlc_count: u64 = Readable::read(reader)?;
65966596

6597-
let mut pending_inbound_htlcs = Vec::with_capacity(cmp::min(pending_inbound_htlc_count as usize, max_accepted_htlcs as usize));
6597+
let channel_handshake_config = ChannelHandshakeConfig::default();
6598+
6599+
let mut pending_inbound_htlcs = Vec::with_capacity(cmp::min(pending_inbound_htlc_count as usize, channel_handshake_config.max_accepted_htlcs as usize));
65986600
for _ in 0..pending_inbound_htlc_count {
65996601
pending_inbound_htlcs.push(InboundHTLCOutput {
66006602
htlc_id: Readable::read(reader)?,
@@ -6612,7 +6614,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
66126614
}
66136615

66146616
let pending_outbound_htlc_count: u64 = Readable::read(reader)?;
6615-
let mut pending_outbound_htlcs = Vec::with_capacity(cmp::min(pending_outbound_htlc_count as usize, max_accepted_htlcs as usize));
6617+
let mut pending_outbound_htlcs = Vec::with_capacity(cmp::min(pending_outbound_htlc_count as usize, channel_handshake_config.max_accepted_htlcs as usize));
66166618
for _ in 0..pending_outbound_htlc_count {
66176619
pending_outbound_htlcs.push(OutboundHTLCOutput {
66186620
htlc_id: Readable::read(reader)?,
@@ -6641,7 +6643,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
66416643
}
66426644

66436645
let holding_cell_htlc_update_count: u64 = Readable::read(reader)?;
6644-
let mut holding_cell_htlc_updates = Vec::with_capacity(cmp::min(holding_cell_htlc_update_count as usize, max_accepted_htlcs as usize*2));
6646+
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));
66456647
for _ in 0..holding_cell_htlc_update_count {
66466648
holding_cell_htlc_updates.push(match <u8 as Readable>::read(reader)? {
66476649
0 => HTLCUpdateAwaitingACK::AddHTLC {
@@ -6674,13 +6676,13 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
66746676
let monitor_pending_commitment_signed = Readable::read(reader)?;
66756677

66766678
let monitor_pending_forwards_count: u64 = Readable::read(reader)?;
6677-
let mut monitor_pending_forwards = Vec::with_capacity(cmp::min(monitor_pending_forwards_count as usize, max_accepted_htlcs as usize));
6679+
let mut monitor_pending_forwards = Vec::with_capacity(cmp::min(monitor_pending_forwards_count as usize, channel_handshake_config.max_accepted_htlcs as usize));
66786680
for _ in 0..monitor_pending_forwards_count {
66796681
monitor_pending_forwards.push((Readable::read(reader)?, Readable::read(reader)?));
66806682
}
66816683

66826684
let monitor_pending_failures_count: u64 = Readable::read(reader)?;
6683-
let mut monitor_pending_failures = Vec::with_capacity(cmp::min(monitor_pending_failures_count as usize, max_accepted_htlcs as usize));
6685+
let mut monitor_pending_failures = Vec::with_capacity(cmp::min(monitor_pending_failures_count as usize, channel_handshake_config.max_accepted_htlcs as usize));
66846686
for _ in 0..monitor_pending_failures_count {
66856687
monitor_pending_failures.push((Readable::read(reader)?, Readable::read(reader)?, Readable::read(reader)?));
66866688
}
@@ -6801,6 +6803,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68016803
let mut user_id_high_opt: Option<u64> = None;
68026804
let mut channel_keys_id: Option<[u8; 32]> = None;
68036805
let mut temporary_channel_id: Option<[u8; 32]> = None;
6806+
let mut max_accepted_htlcs: Option<u16> = None;
68046807

68056808
read_tlv_fields!(reader, {
68066809
(0, announcement_sigs, option),
@@ -6821,6 +6824,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68216824
(23, channel_ready_event_emitted, option),
68226825
(25, user_id_high_opt, option),
68236826
(27, channel_keys_id, option),
6827+
(28, max_accepted_htlcs, option),
68246828
(29, temporary_channel_id, option),
68256829
(31, channel_pending_event_emitted, option),
68266830
});
@@ -6875,6 +6879,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
68756879
// separate u64 values.
68766880
let user_id = user_id_low as u128 + ((user_id_high_opt.unwrap_or(0) as u128) << 64);
68776881

6882+
let max_accepted_htlcs = max_accepted_htlcs.unwrap_or(channel_handshake_config.max_accepted_htlcs);
6883+
68786884
Ok(Channel {
68796885
user_id,
68806886

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)