Skip to content

Commit 40f48de

Browse files
author
Antoine Riard
committed
Re-add test_max_dust_htlc_exposure
1 parent ab11f45 commit 40f48de

File tree

1 file changed

+173
-1
lines changed

1 file changed

+173
-1
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 173 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
2222
use ln::channelmanager::{ChannelManager, ChannelManagerReadArgs, PaymentId, RAACommitmentOrder, PaymentSendFailure, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA};
2323
use ln::channel::{Channel, ChannelError};
2424
use ln::{chan_utils, onion_utils};
25-
use ln::chan_utils::{HTLC_SUCCESS_TX_WEIGHT, HTLCOutputInCommitment};
25+
use ln::chan_utils::{HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, HTLCOutputInCommitment};
2626
use routing::network_graph::{NetworkUpdate, RoutingFees};
2727
use routing::router::{Payee, Route, RouteHop, RouteHint, RouteHintHop, RouteParameters, find_route, get_route};
2828
use ln::features::{ChannelFeatures, InitFeatures, InvoiceFeatures, NodeFeatures};
@@ -9202,3 +9202,175 @@ fn test_keysend_payments_to_private_node() {
92029202
pass_along_path(&nodes[0], &path, 10000, payment_hash, None, event, true, Some(test_preimage));
92039203
claim_payment(&nodes[0], &path, test_preimage);
92049204
}
9205+
9206+
/// The possible events which may trigger a `max_dust_htlc_exposure` breach
9207+
#[derive(Clone, Copy, PartialEq)]
9208+
enum ExposureEvent {
9209+
/// Breach occurs at HTLC forwarding (see `send_htlc`)
9210+
AtHTLCForward,
9211+
/// Breach occurs at HTLC reception (see `update_add_htlc`)
9212+
AtHTLCReception,
9213+
/// Breach occurs at outbound update_fee (see `send_update_fee`)
9214+
AtUpdateFeeOutbound,
9215+
}
9216+
9217+
fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, exposure_breach_event: ExposureEvent, on_holder_tx: bool) {
9218+
// Test that we properly reject dust HTLC violating our `max_dust_htlc_exposure_msat`
9219+
// policy.
9220+
//
9221+
// At HTLC forward (`send_payment()`), if the sum of the trimmed-to-dust HTLC inbound and
9222+
// trimmed-to-dust HTLC outbound balance and this new payment as included on next
9223+
// counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll reject the
9224+
// update. At HTLC reception (`update_add_htlc()`), if the sum of the trimmed-to-dust HTLC
9225+
// inbound and trimmed-to-dust HTLC outbound balance and this new received HTLC as included
9226+
// on next counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll fail
9227+
// the update. Note, we return a `temporary_channel_failure` (0x1000 | 7), as the channel
9228+
// might be available again for HTLC processing once the dust bandwidth has cleared up.
9229+
9230+
let chanmon_cfgs = create_chanmon_cfgs(2);
9231+
let mut config = test_default_channel_config();
9232+
config.channel_options.max_dust_htlc_exposure_msat = 5_000_000; // default setting value
9233+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9234+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[Some(config), None]);
9235+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9236+
9237+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
9238+
let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9239+
open_channel.max_htlc_value_in_flight_msat = 50_000_000;
9240+
open_channel.max_accepted_htlcs = 60;
9241+
if on_holder_tx {
9242+
open_channel.dust_limit_satoshis = 546;
9243+
}
9244+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
9245+
let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9246+
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
9247+
9248+
let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], 1_000_000, 42);
9249+
9250+
if on_holder_tx {
9251+
if let Some(mut chan) = nodes[0].node.channel_state.lock().unwrap().by_id.get_mut(&temporary_channel_id) {
9252+
chan.holder_dust_limit_satoshis = 546;
9253+
}
9254+
}
9255+
9256+
nodes[0].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
9257+
nodes[1].node.handle_funding_created(&nodes[0].node.get_our_node_id(), &get_event_msg!(nodes[0], MessageSendEvent::SendFundingCreated, nodes[1].node.get_our_node_id()));
9258+
check_added_monitors!(nodes[1], 1);
9259+
9260+
nodes[0].node.handle_funding_signed(&nodes[1].node.get_our_node_id(), &get_event_msg!(nodes[1], MessageSendEvent::SendFundingSigned, nodes[0].node.get_our_node_id()));
9261+
check_added_monitors!(nodes[0], 1);
9262+
9263+
let (funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
9264+
let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
9265+
update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
9266+
9267+
let dust_buffer_feerate = {
9268+
let chan_lock = nodes[0].node.channel_state.lock().unwrap();
9269+
let chan = chan_lock.by_id.get(&channel_id).unwrap();
9270+
chan.get_dust_buffer_feerate(None) as u64
9271+
};
9272+
let dust_outbound_htlc_on_holder_tx_msat: u64 = (dust_buffer_feerate * HTLC_TIMEOUT_TX_WEIGHT / 1000 + open_channel.dust_limit_satoshis - 1) * 1000;
9273+
let dust_outbound_htlc_on_holder_tx: u64 = config.channel_options.max_dust_htlc_exposure_msat / dust_outbound_htlc_on_holder_tx_msat;
9274+
9275+
let dust_inbound_htlc_on_holder_tx_msat: u64 = (dust_buffer_feerate * HTLC_SUCCESS_TX_WEIGHT / 1000 + open_channel.dust_limit_satoshis - 1) * 1000;
9276+
let dust_inbound_htlc_on_holder_tx: u64 = config.channel_options.max_dust_htlc_exposure_msat / dust_inbound_htlc_on_holder_tx_msat;
9277+
9278+
let dust_htlc_on_counterparty_tx: u64 = 25;
9279+
let dust_htlc_on_counterparty_tx_msat: u64 = config.channel_options.max_dust_htlc_exposure_msat / dust_htlc_on_counterparty_tx;
9280+
9281+
if on_holder_tx {
9282+
if dust_outbound_balance {
9283+
// Outbound dust threshold: 2223 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9284+
// Outbound dust balance: 4372 sats
9285+
// Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2132 sats
9286+
for i in 0..dust_outbound_htlc_on_holder_tx {
9287+
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], dust_outbound_htlc_on_holder_tx_msat);
9288+
if let Err(_) = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)) { panic!("Unexpected event at dust HTLC {}", i); }
9289+
}
9290+
} else {
9291+
// Inbound dust threshold: 2324 sats (`dust_buffer_feerate` * HTLC_SUCCESS_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9292+
// Inbound dust balance: 4372 sats
9293+
// Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2031 sats
9294+
for _ in 0..dust_inbound_htlc_on_holder_tx {
9295+
route_payment(&nodes[1], &[&nodes[0]], dust_inbound_htlc_on_holder_tx_msat);
9296+
}
9297+
}
9298+
} else {
9299+
if dust_outbound_balance {
9300+
// Outbound dust threshold: 2132 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9301+
// Outbound dust balance: 5000 sats
9302+
for i in 0..dust_htlc_on_counterparty_tx {
9303+
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], dust_htlc_on_counterparty_tx_msat);
9304+
if let Err(_) = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)) { panic!("Unexpected event at dust HTLC {}", i); }
9305+
}
9306+
} else {
9307+
// Inbound dust threshold: 2031 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9308+
// Inbound dust balance: 5000 sats
9309+
for _ in 0..dust_htlc_on_counterparty_tx {
9310+
route_payment(&nodes[1], &[&nodes[0]], dust_htlc_on_counterparty_tx_msat);
9311+
}
9312+
}
9313+
}
9314+
9315+
let dust_overflow = dust_htlc_on_counterparty_tx_msat * (dust_htlc_on_counterparty_tx + 1);
9316+
if exposure_breach_event == ExposureEvent::AtHTLCForward {
9317+
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], if on_holder_tx { dust_outbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat });
9318+
let mut config = UserConfig::default();
9319+
// With default dust exposure: 5000 sats
9320+
if on_holder_tx {
9321+
let dust_outbound_overflow = dust_outbound_htlc_on_holder_tx_msat * (dust_outbound_htlc_on_holder_tx + 1);
9322+
let dust_inbound_overflow = dust_inbound_htlc_on_holder_tx_msat * dust_inbound_htlc_on_holder_tx + dust_outbound_htlc_on_holder_tx_msat;
9323+
unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)), true, APIError::ChannelUnavailable { ref err }, assert_eq!(err, &format!("Cannot send value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx", if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow }, config.channel_options.max_dust_htlc_exposure_msat)));
9324+
} else {
9325+
unwrap_send_err!(nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)), true, APIError::ChannelUnavailable { ref err }, assert_eq!(err, &format!("Cannot send value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx", dust_overflow, config.channel_options.max_dust_htlc_exposure_msat)));
9326+
}
9327+
} else if exposure_breach_event == ExposureEvent::AtHTLCReception {
9328+
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], if on_holder_tx { dust_inbound_htlc_on_holder_tx_msat } else { dust_htlc_on_counterparty_tx_msat });
9329+
nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
9330+
check_added_monitors!(nodes[1], 1);
9331+
let mut events = nodes[1].node.get_and_clear_pending_msg_events();
9332+
assert_eq!(events.len(), 1);
9333+
let payment_event = SendEvent::from_event(events.remove(0));
9334+
nodes[0].node.handle_update_add_htlc(&nodes[1].node.get_our_node_id(), &payment_event.msgs[0]);
9335+
// With default dust exposure: 5000 sats
9336+
if on_holder_tx {
9337+
// Outbound dust balance: 6399 sats
9338+
let dust_inbound_overflow = dust_inbound_htlc_on_holder_tx_msat * (dust_inbound_htlc_on_holder_tx + 1);
9339+
let dust_outbound_overflow = dust_outbound_htlc_on_holder_tx_msat * dust_outbound_htlc_on_holder_tx + dust_inbound_htlc_on_holder_tx_msat;
9340+
nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on holder commitment tx", if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow }, config.channel_options.max_dust_htlc_exposure_msat), 1);
9341+
} else {
9342+
// Outbound dust balance: 5200 sats
9343+
nodes[0].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot accept value that would put our exposure to dust HTLCs at {} over the limit {} on counterparty commitment tx", dust_overflow, config.channel_options.max_dust_htlc_exposure_msat), 1);
9344+
}
9345+
} else if exposure_breach_event == ExposureEvent::AtUpdateFeeOutbound {
9346+
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1], 2_500_000);
9347+
if let Err(_) = nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)) { panic!("Unexpected event at update_fee-swallowed HTLC", ); }
9348+
{
9349+
let mut feerate_lock = chanmon_cfgs[0].fee_estimator.sat_per_kw.lock().unwrap();
9350+
*feerate_lock = *feerate_lock * 10;
9351+
}
9352+
nodes[0].node.timer_tick_occurred();
9353+
check_added_monitors!(nodes[0], 1);
9354+
nodes[0].logger.assert_log_contains("lightning::ln::channel".to_string(), "Cannot afford to send new feerate at 2530 without infringing max dust htlc exposure".to_string(), 1);
9355+
}
9356+
9357+
let _ = nodes[0].node.get_and_clear_pending_msg_events();
9358+
let mut added_monitors = nodes[0].chain_monitor.added_monitors.lock().unwrap();
9359+
added_monitors.clear();
9360+
}
9361+
9362+
#[test]
9363+
fn test_max_dust_htlc_exposure() {
9364+
do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, true);
9365+
do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, true);
9366+
do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, true);
9367+
do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCReception, false);
9368+
do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCForward, false);
9369+
do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, false);
9370+
do_test_max_dust_htlc_exposure(true, ExposureEvent::AtHTLCReception, true);
9371+
do_test_max_dust_htlc_exposure(false, ExposureEvent::AtHTLCForward, false);
9372+
do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, true);
9373+
do_test_max_dust_htlc_exposure(true, ExposureEvent::AtUpdateFeeOutbound, false);
9374+
do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, false);
9375+
do_test_max_dust_htlc_exposure(false, ExposureEvent::AtUpdateFeeOutbound, true);
9376+
}

0 commit comments

Comments
 (0)