From 9b9f4d58eb13dda7f903ae525efe37171849df79 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sat, 16 Apr 2022 20:26:39 +0000 Subject: [PATCH] xxx broken tests full htlc max capacity separation --- lightning/src/ln/channel.rs | 35 +++++++++++++++++++--------- lightning/src/ln/functional_tests.rs | 3 ++- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index cac6dfd2..3696f384 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -361,10 +361,10 @@ struct HTLCCandidate { impl HTLCCandidate { fn new(amount_msat: u64, origin: HTLCInitiator) -> Self { - Self { - amount_msat, - origin, - } + Self { amount_msat, origin } + } + fn non_dust(origin: HTLCInitiator) -> Self { + Self { amount_msat: 21_000_000 * 1_0000_0000, origin } } } @@ -2340,6 +2340,7 @@ impl Channel { pub fn get_inbound_outbound_available_balance_msat(&self) -> (u64, u64, u64) { // Note that we have to handle overflow due to the above case. let outbound_stats = self.get_outbound_pending_htlc_stats(None); + let extra_htlc_commitment_fee_msat = self.outbound_htlc_spike_buffer_commitment_tx_fee_msat(HTLCCandidate::non_dust(HTLCInitiator::LocalOffered)) as i64; let outbound_capacity_msat = cmp::max(self.value_to_self_msat as i64 - outbound_stats.pending_htlcs_value_msat as i64 - self.counterparty_selected_channel_reserve_satoshis.unwrap_or(0) as i64 * 1000, @@ -2351,9 +2352,17 @@ impl Channel { - self.holder_selected_channel_reserve_satoshis as i64 * 1000, 0) as u64, outbound_capacity_msat, - cmp::max(cmp::min(outbound_capacity_msat as i64, - self.counterparty_max_htlc_value_in_flight_msat as i64 - - outbound_stats.pending_htlcs_value_msat as i64), + cmp::max(cmp::min(cmp::min( + outbound_capacity_msat as i64, + self.counterparty_max_htlc_value_in_flight_msat as i64 + - outbound_stats.pending_htlcs_value_msat as i64 + - extra_htlc_commitment_fee_msat) + - extra_htlc_commitment_fee_msat, + if outbound_stats.pending_htlcs + 1 > self.counterparty_max_accepted_htlcs as u32 { i64::max_value() /*XXX0*/ } else { i64::max_value() } + ), + // TODO: self.counterparty_htlc_maximum_msat as i64), + // TODO: Capture feerate avoidance policy + // TODO: Capture HTLC count limit 0) as u64 ) } @@ -5099,6 +5108,12 @@ impl Channel { } + fn outbound_htlc_spike_buffer_commitment_tx_fee_msat(&self, htlc_candidate: HTLCCandidate) -> u64 { + if self.is_outbound() { + FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * self.next_local_commit_tx_fee_msat(htlc_candidate, Some(())) + } else { 0 } + } + // Send stuff to our remote peers: /// Adds a pending outbound HTLC to this channel, note that you probably want @@ -5189,10 +5204,8 @@ impl Channel { } // `2 *` and extra HTLC are for the fee spike buffer. - let commit_tx_fee_msat = if self.is_outbound() { - let htlc_candidate = HTLCCandidate::new(amount_msat, HTLCInitiator::LocalOffered); - FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE * self.next_local_commit_tx_fee_msat(htlc_candidate, Some(())) - } else { 0 }; + let htlc_candidate = HTLCCandidate::new(amount_msat, HTLCInitiator::LocalOffered); + let commit_tx_fee_msat = self.outbound_htlc_spike_buffer_commitment_tx_fee_msat(htlc_candidate); if holder_balance_msat - amount_msat < commit_tx_fee_msat { return Err(ChannelError::Ignore(format!("Cannot send value that would not leave enough to pay for fees. Pending value to self: {}. local_commit_tx_fee {}", holder_balance_msat, commit_tx_fee_msat))); } diff --git a/lightning/src/ln/functional_tests.rs b/lightning/src/ln/functional_tests.rs index 525b4f33..31d44125 100644 --- a/lightning/src/ln/functional_tests.rs +++ b/lightning/src/ln/functional_tests.rs @@ -6455,6 +6455,7 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 1000000, 0, InitFeatures::known(), InitFeatures::known()); let max_accepted_htlcs = nodes[1].node.channel_state.lock().unwrap().by_id.get(&chan.2).unwrap().counterparty_max_accepted_htlcs as u64; + let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000); for i in 0..max_accepted_htlcs { let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000); let payment_event = { @@ -6477,7 +6478,7 @@ fn test_update_add_htlc_bolt2_sender_exceed_max_htlc_num_and_htlc_id_increment() expect_pending_htlcs_forwardable!(nodes[1]); expect_payment_received!(nodes[1], our_payment_hash, our_payment_secret, 100000); } - let (route, our_payment_hash, _, our_payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 100000); + // XXX: check that we fail to get a route now! unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &Some(our_payment_secret)), true, APIError::ChannelUnavailable { ref err }, assert!(regex::Regex::new(r"Cannot push more than their max accepted HTLCs \(\d+\)").unwrap().is_match(err))); -- 2.30.2