@@ -24,7 +24,7 @@ use crate::ln::types::ChannelId;
2424use crate::types::payment::{PaymentPreimage, PaymentSecret, PaymentHash};
2525use crate::ln::channel::{CONCURRENT_INBOUND_HTLC_FEE_BUFFER, FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE, MIN_AFFORDABLE_HTLC_COUNT, get_holder_selected_channel_reserve_satoshis, OutboundV1Channel, InboundV1Channel, COINBASE_MATURITY, Channel};
2626use crate::ln::channelmanager::{self, PaymentId, RAACommitmentOrder, RecipientOnionFields, BREAKDOWN_TIMEOUT, ENABLE_GOSSIP_TICKS, DISABLE_GOSSIP_TICKS, MIN_CLTV_EXPIRY_DELTA};
27- use crate::ln::channel::{DISCONNECT_PEER_AWAITING_RESPONSE_TICKS, ChannelError};
27+ use crate::ln::channel::{DISCONNECT_PEER_AWAITING_RESPONSE_TICKS, ChannelError, MIN_CHAN_DUST_LIMIT_SATOSHIS };
2828use crate::ln::{chan_utils, onion_utils};
2929use crate::ln::chan_utils::{commitment_tx_base_weight, COMMITMENT_TX_WEIGHT_PER_HTLC, OFFERED_HTLC_SCRIPT_WEIGHT, htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputInCommitment};
3030use crate::routing::gossip::{NetworkGraph, NetworkUpdate};
@@ -37,7 +37,7 @@ use crate::util::test_utils::{self, TestLogger, WatchtowerPersister};
3737use crate::util::errors::APIError;
3838use crate::util::ser::{Writeable, ReadableArgs};
3939use crate::util::string::UntrustedString;
40- use crate::util::config::{UserConfig, MaxDustHTLCExposure};
40+ use crate::util::config::{UserConfig, MaxDustHTLCExposure, ChannelConfigUpdate };
4141
4242use bitcoin::hash_types::BlockHash;
4343use bitcoin::locktime::absolute::LockTime;
@@ -10672,6 +10672,204 @@ fn test_nondust_htlc_excess_fees_are_dust() {
1067210672 expect_payment_failed_conditions(&nodes[2], payment_hash, false, PaymentFailedConditions::new());
1067310673}
1067410674
10675+ fn do_test_nondust_htlc_fees_dust_exposure_delta(features: ChannelTypeFeatures) {
10676+ // Tests the increase in htlc dust exposure due to the excess mining fees of a single non-dust
10677+ // HTLC on the counterparty commitment transaction, for both incoming and outgoing htlcs.
10678+ //
10679+ // Brings the dust exposure up to the base dust exposure using dust htlcs.
10680+ // Sets the max dust exposure to 1msat below the expected dust exposure given an additional non-dust htlc.
10681+ // Checks a failed payment for a non-dust htlc.
10682+ // Sets the max dust exposure equal to the expected dust exposure given an additional non-dust htlc.
10683+ // Checks a successful payment for a non-dust htlc.
10684+ //
10685+ // Runs this sequence for both directions.
10686+
10687+ let chanmon_cfgs = create_chanmon_cfgs(2);
10688+
10689+ const DEFAULT_FEERATE: u64 = 253;
10690+ const HIGH_FEERATE: u64 = 275;
10691+ const EXCESS_FEERATE: u64 = HIGH_FEERATE - DEFAULT_FEERATE;
10692+
10693+ const DUST_HTLC_COUNT: usize = 4;
10694+ // Set dust htlcs to a satoshi value plus a non-zero msat amount to assert that
10695+ // the dust accounting rounds transaction fees to the lower satoshi, but does not round dust htlc values.
10696+ const DUST_HTLC_MSAT: u64 = 125_123;
10697+ const BASE_DUST_EXPOSURE_MSAT: u64 = DUST_HTLC_COUNT as u64 * DUST_HTLC_MSAT;
10698+
10699+ const NON_DUST_HTLC_MSAT: u64 = 4_000_000;
10700+
10701+ {
10702+ // Set the feerate of the channel funder above the `dust_exposure_limiting_feerate` of
10703+ // the fundee. This delta means that the fundee will add the mining fees of the commitment and
10704+ // htlc transactions in excess of its `dust_exposure_limiting_feerate` to its total dust htlc
10705+ // exposure.
10706+ let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
10707+ *feerate_lock = HIGH_FEERATE as u32;
10708+ }
10709+
10710+ // Set `expected_dust_exposure_msat` to match the calculation in `FundedChannel::can_accept_incoming_htlc`
10711+ // only_static_remote_key: 500_492 + 22 * (724 + 172) / 1000 * 1000 + 22 * 663 / 1000 * 1000 = 533_492
10712+ // anchors_zero_htlc_fee: 500_492 + 22 * (1_124 + 172) / 1000 * 1000 = 528_492
10713+ let mut expected_dust_exposure_msat = BASE_DUST_EXPOSURE_MSAT + EXCESS_FEERATE * (commitment_tx_base_weight(&features) + COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000 * 1000;
10714+ if features == ChannelTypeFeatures::only_static_remote_key() {
10715+ expected_dust_exposure_msat += EXCESS_FEERATE * htlc_timeout_tx_weight(&features) / 1000 * 1000;
10716+ assert_eq!(expected_dust_exposure_msat, 533_492);
10717+ } else {
10718+ assert_eq!(expected_dust_exposure_msat, 528_492);
10719+ }
10720+
10721+ let mut default_config = test_default_channel_config();
10722+ if features == ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies() {
10723+ default_config.channel_handshake_config.negotiate_anchors_zero_fee_htlc_tx = true;
10724+ // in addition to the one above, this setting is also needed to create an anchor channel
10725+ default_config.manually_accept_inbound_channels = true;
10726+ }
10727+
10728+ // Set node 1's max dust htlc exposure to 1msat below `expected_dust_exposure_msat`
10729+ let mut fixed_limit_config = default_config.clone();
10730+ fixed_limit_config.channel_config.max_dust_htlc_exposure = MaxDustHTLCExposure::FixedLimitMsat(expected_dust_exposure_msat - 1);
10731+
10732+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
10733+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(default_config), Some(fixed_limit_config)]);
10734+ let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
10735+
10736+ let chan_id = create_chan_between_nodes_with_value(&nodes[0], &nodes[1], 100_000, 50_000_000).3;
10737+
10738+ let node_1_dust_buffer_feerate = {
10739+ let per_peer_state = nodes[1].node.per_peer_state.read().unwrap();
10740+ let chan_lock = per_peer_state.get(&nodes[0].node.get_our_node_id()).unwrap().lock().unwrap();
10741+ let chan = chan_lock.channel_by_id.get(&chan_id).unwrap();
10742+ chan.context().get_dust_buffer_feerate(None) as u64
10743+ };
10744+
10745+ // Skip the router complaint when node 1 will attempt to pay node 0
10746+ let (route_1_0, payment_hash_1_0, _, payment_secret_1_0) = get_route_and_payment_hash!(nodes[1], nodes[0], NON_DUST_HTLC_MSAT);
10747+
10748+ // Bring node 1's dust htlc exposure up to `BASE_DUST_EXPOSURE_MSAT`
10749+ for _ in 0..DUST_HTLC_COUNT {
10750+ route_payment(&nodes[0], &[&nodes[1]], DUST_HTLC_MSAT);
10751+ }
10752+
10753+ assert_eq!(nodes[0].node.list_channels().len(), 1);
10754+ assert_eq!(nodes[1].node.list_channels().len(), 1);
10755+
10756+ assert_eq!(nodes[0].node.list_channels()[0].pending_inbound_htlcs.len(), 0);
10757+ assert_eq!(nodes[1].node.list_channels()[0].pending_outbound_htlcs.len(), 0);
10758+ assert_eq!(nodes[0].node.list_channels()[0].pending_outbound_htlcs.len(), DUST_HTLC_COUNT);
10759+ assert_eq!(nodes[1].node.list_channels()[0].pending_inbound_htlcs.len(), DUST_HTLC_COUNT);
10760+
10761+ // Send an additional non-dust htlc from 0 to 1, and check the complaint
10762+ let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], NON_DUST_HTLC_MSAT);
10763+ nodes[0].node.send_payment_with_route(route, payment_hash,
10764+ RecipientOnionFields::secret_only(payment_secret), PaymentId(payment_hash.0)).unwrap();
10765+ check_added_monitors!(nodes[0], 1);
10766+ let mut events = nodes[0].node.get_and_clear_pending_msg_events();
10767+ assert_eq!(events.len(), 1);
10768+ let payment_event = SendEvent::from_event(events.remove(0));
10769+ nodes[1].node.handle_update_add_htlc(nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
10770+ commitment_signed_dance!(nodes[1], nodes[0], payment_event.commitment_msg, false);
10771+ expect_pending_htlcs_forwardable!(nodes[1]);
10772+ expect_htlc_handling_failed_destinations!(nodes[1].node.get_and_clear_pending_events(), &[HTLCDestination::FailedPayment { payment_hash }]);
10773+ nodes[1].logger.assert_log("lightning::ln::channel",
10774+ format!("Cannot accept value that would put our total dust exposure at {} over the limit {} on counterparty commitment tx",
10775+ expected_dust_exposure_msat, expected_dust_exposure_msat - 1), 1);
10776+ check_added_monitors!(nodes[1], 1);
10777+
10778+ // Clear the failed htlc
10779+ let updates = get_htlc_update_msgs!(nodes[1], nodes[0].node.get_our_node_id());
10780+ assert!(updates.update_add_htlcs.is_empty());
10781+ assert!(updates.update_fulfill_htlcs.is_empty());
10782+ assert_eq!(updates.update_fail_htlcs.len(), 1);
10783+ assert!(updates.update_fail_malformed_htlcs.is_empty());
10784+ assert!(updates.update_fee.is_none());
10785+ nodes[0].node.handle_update_fail_htlc(nodes[1].node.get_our_node_id(), &updates.update_fail_htlcs[0]);
10786+ commitment_signed_dance!(nodes[0], nodes[1], updates.commitment_signed, false);
10787+ expect_payment_failed!(nodes[0], payment_hash, false);
10788+
10789+ assert_eq!(nodes[0].node.list_channels().len(), 1);
10790+ assert_eq!(nodes[1].node.list_channels().len(), 1);
10791+
10792+ assert_eq!(nodes[0].node.list_channels()[0].pending_inbound_htlcs.len(), 0);
10793+ assert_eq!(nodes[1].node.list_channels()[0].pending_outbound_htlcs.len(), 0);
10794+ assert_eq!(nodes[0].node.list_channels()[0].pending_outbound_htlcs.len(), DUST_HTLC_COUNT);
10795+ assert_eq!(nodes[1].node.list_channels()[0].pending_inbound_htlcs.len(), DUST_HTLC_COUNT);
10796+
10797+ // Set node 1's max dust htlc exposure equal to the `expected_dust_exposure_msat`
10798+ nodes[1].node.update_partial_channel_config(&nodes[0].node.get_our_node_id(), &[chan_id], &ChannelConfigUpdate {
10799+ max_dust_htlc_exposure_msat: Some(MaxDustHTLCExposure::FixedLimitMsat(expected_dust_exposure_msat)),
10800+ ..ChannelConfigUpdate::default()
10801+ }).unwrap();
10802+
10803+ // Check a successful payment
10804+ send_payment(&nodes[0], &[&nodes[1]], NON_DUST_HTLC_MSAT);
10805+
10806+ assert_eq!(nodes[0].node.list_channels().len(), 1);
10807+ assert_eq!(nodes[1].node.list_channels().len(), 1);
10808+
10809+ assert_eq!(nodes[0].node.list_channels()[0].pending_inbound_htlcs.len(), 0);
10810+ assert_eq!(nodes[1].node.list_channels()[0].pending_outbound_htlcs.len(), 0);
10811+ assert_eq!(nodes[0].node.list_channels()[0].pending_outbound_htlcs.len(), DUST_HTLC_COUNT);
10812+ assert_eq!(nodes[1].node.list_channels()[0].pending_inbound_htlcs.len(), DUST_HTLC_COUNT);
10813+
10814+ // The `expected_dust_exposure_msat` for the outbound htlc changes in the non-anchor case, as the htlc success and timeout transactions have different weights
10815+ // only_static_remote_key: 500_492 + 22 * (724 + 172) / 1000 * 1000 + 22 * 703 / 1000 * 1000 = 534_492
10816+ if features == ChannelTypeFeatures::only_static_remote_key() {
10817+ expected_dust_exposure_msat = BASE_DUST_EXPOSURE_MSAT + EXCESS_FEERATE * (commitment_tx_base_weight(&features) + COMMITMENT_TX_WEIGHT_PER_HTLC) / 1000 * 1000 + EXCESS_FEERATE * htlc_success_tx_weight(&features) / 1000 * 1000;
10818+ assert_eq!(expected_dust_exposure_msat, 534_492);
10819+ } else {
10820+ assert_eq!(expected_dust_exposure_msat, 528_492);
10821+ }
10822+
10823+ // Set node 1's max dust htlc exposure to 1msat below `expected_dust_exposure_msat`
10824+ nodes[1].node.update_partial_channel_config(&nodes[0].node.get_our_node_id(), &[chan_id], &ChannelConfigUpdate {
10825+ max_dust_htlc_exposure_msat: Some(MaxDustHTLCExposure::FixedLimitMsat(expected_dust_exposure_msat - 1)),
10826+ ..ChannelConfigUpdate::default()
10827+ }).unwrap();
10828+
10829+ // Send an additional non-dust htlc from 1 to 0 using the pre-calculated route above, and check the immediate complaint
10830+ unwrap_send_err!(nodes[1], nodes[1].node.send_payment_with_route(route_1_0, payment_hash_1_0,
10831+ RecipientOnionFields::secret_only(payment_secret_1_0), PaymentId(payment_hash_1_0.0)
10832+ ), true, APIError::ChannelUnavailable { .. }, {});
10833+ let dust_limit = if features == ChannelTypeFeatures::only_static_remote_key() {
10834+ MIN_CHAN_DUST_LIMIT_SATOSHIS * 1000 + htlc_success_tx_weight(&features) * node_1_dust_buffer_feerate / 1000 * 1000
10835+ } else {
10836+ MIN_CHAN_DUST_LIMIT_SATOSHIS * 1000
10837+ };
10838+ nodes[1].logger.assert_log("lightning::ln::outbound_payment",
10839+ format!("Failed to send along path due to error: Channel unavailable: Cannot send more than our next-HTLC maximum - {} msat", dust_limit), 1);
10840+ assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
10841+
10842+ assert_eq!(nodes[0].node.list_channels().len(), 1);
10843+ assert_eq!(nodes[1].node.list_channels().len(), 1);
10844+
10845+ assert_eq!(nodes[0].node.list_channels()[0].pending_inbound_htlcs.len(), 0);
10846+ assert_eq!(nodes[1].node.list_channels()[0].pending_outbound_htlcs.len(), 0);
10847+ assert_eq!(nodes[0].node.list_channels()[0].pending_outbound_htlcs.len(), DUST_HTLC_COUNT);
10848+ assert_eq!(nodes[1].node.list_channels()[0].pending_inbound_htlcs.len(), DUST_HTLC_COUNT);
10849+
10850+ // Set node 1's max dust htlc exposure equal to `expected_dust_exposure_msat`
10851+ nodes[1].node.update_partial_channel_config(&nodes[0].node.get_our_node_id(), &[chan_id], &ChannelConfigUpdate {
10852+ max_dust_htlc_exposure_msat: Some(MaxDustHTLCExposure::FixedLimitMsat(expected_dust_exposure_msat)),
10853+ ..ChannelConfigUpdate::default()
10854+ }).unwrap();
10855+
10856+ // Check a successful payment
10857+ send_payment(&nodes[1], &[&nodes[0]], NON_DUST_HTLC_MSAT);
10858+
10859+ assert_eq!(nodes[0].node.list_channels().len(), 1);
10860+ assert_eq!(nodes[1].node.list_channels().len(), 1);
10861+
10862+ assert_eq!(nodes[0].node.list_channels()[0].pending_inbound_htlcs.len(), 0);
10863+ assert_eq!(nodes[1].node.list_channels()[0].pending_outbound_htlcs.len(), 0);
10864+ assert_eq!(nodes[0].node.list_channels()[0].pending_outbound_htlcs.len(), DUST_HTLC_COUNT);
10865+ assert_eq!(nodes[1].node.list_channels()[0].pending_inbound_htlcs.len(), DUST_HTLC_COUNT);
10866+ }
10867+
10868+ #[test]
10869+ fn test_nondust_htlc_fees_dust_exposure_delta() {
10870+ do_test_nondust_htlc_fees_dust_exposure_delta(ChannelTypeFeatures::only_static_remote_key());
10871+ do_test_nondust_htlc_fees_dust_exposure_delta(ChannelTypeFeatures::anchors_zero_htlc_fee_and_dependencies());
10872+ }
1067510873
1067610874#[test]
1067710875fn test_non_final_funding_tx() {
0 commit comments