Skip to content

Commit b287cfd

Browse files
author
Antoine Riard
committed
Re-add test_max_dust_htlc_exposure
1 parent b650bf8 commit b287cfd

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

lightning/src/ln/functional_tests.rs

+129
Original file line numberDiff line numberDiff line change
@@ -9318,3 +9318,132 @@ fn test_keysend_payments_to_private_node() {
93189318
pass_along_path(&nodes[0], &path, 10000, payment_hash, None, event, true, Some(test_preimage));
93199319
claim_payment(&nodes[0], &path, test_preimage);
93209320
}
9321+
9322+
fn do_test_max_dust_htlc_exposure(dust_outbound_balance: bool, at_forward: bool, on_holder_tx: bool) {
9323+
// Test that we properly reject dust HTLC violating our `max_dust_htlc_exposure_msat` policy.
9324+
//
9325+
// At HTLC forward (`send_payment()`), if the sum of the trimmed-to-dust HTLC inbound and
9326+
// trimmed-to-dust HTLC outbound balance and this new payment as included on next counterparty
9327+
// commitment are above our `max_dust_htlc_exposure_msat`, we'll reject the update.
9328+
// At HTLC reception (`update_add_htlc()`), if the sum of the trimmed-to-dust HTLC inbound
9329+
// and trimmed-to-dust HTLC outbound balance and this new received HTLC as included on next
9330+
// counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll fail the update.
9331+
// Note, we return a `temporary_channel_failure` (0x1000 | 7), as the channel might be
9332+
// available again for HTLC processing once the dust bandwidth has cleared up.
9333+
9334+
let chanmon_cfgs = create_chanmon_cfgs(2);
9335+
let mut config = test_default_channel_config();
9336+
config.channel_options.max_dust_htlc_exposure_msat = 5_000_000; // default setting value
9337+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9338+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(config)]);
9339+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9340+
9341+
nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
9342+
let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9343+
open_channel.max_htlc_value_in_flight_msat = 50_000_000;
9344+
open_channel.max_accepted_htlcs = 60;
9345+
nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
9346+
let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9347+
if on_holder_tx {
9348+
accept_channel.dust_limit_satoshis = 546;
9349+
}
9350+
nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
9351+
9352+
let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], 1_000_000, 42);
9353+
9354+
if on_holder_tx {
9355+
if let Some(mut chan) = nodes[1].node.channel_state.lock().unwrap().by_id.get_mut(&temporary_channel_id) {
9356+
chan.holder_dust_limit_satoshis = 546;
9357+
}
9358+
}
9359+
9360+
nodes[0].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
9361+
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()));
9362+
check_added_monitors!(nodes[1], 1);
9363+
9364+
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()));
9365+
check_added_monitors!(nodes[0], 1);
9366+
9367+
let (funding_locked, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
9368+
let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
9369+
update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
9370+
9371+
if on_holder_tx {
9372+
if dust_outbound_balance {
9373+
// Outbound dust threshold: 2223 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9374+
// Outbound dust balance: 4372 sats
9375+
// Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2132 sats
9376+
for i in 0..2 {
9377+
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 2_186_000);
9378+
if let Err(_) = nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)) { panic!("Unexpected event at dust HTLC {}", i); }
9379+
}
9380+
} else {
9381+
// Inbound dust threshold: 2324 sats (`dust_buffer_feerate` * HTLC_SUCCESS_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9382+
// Inbound dust balance: 4372 sats
9383+
// Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2031 sats
9384+
for _ in 0..2 {
9385+
route_payment(&nodes[0], &[&nodes[1]], 2_186_000);
9386+
}
9387+
}
9388+
} else {
9389+
if dust_outbound_balance {
9390+
// Outbound dust threshold: 2132 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9391+
// Outbound dust balance: 5000 sats
9392+
for i in 0..25 {
9393+
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 200_000); // + 177_000 msat of HTLC-success tx at 253 sats/kWU
9394+
if let Err(_) = nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)) { panic!("Unexpected event at dust HTLC {}", i); }
9395+
}
9396+
} else {
9397+
// Inbound dust threshold: 2031 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9398+
// Inbound dust balance: 5000 sats
9399+
for _ in 0..25 {
9400+
route_payment(&nodes[0], &[&nodes[1]], 200_000);
9401+
}
9402+
}
9403+
}
9404+
9405+
if at_forward {
9406+
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], if on_holder_tx { 2_186_000 } else { 200_000 });
9407+
let mut config = UserConfig::default();
9408+
// With default dust exposure: 5000 sats
9409+
if on_holder_tx {
9410+
// Outbound dust balance: 6558 sats
9411+
unwrap_send_err!(nodes[1].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", 6_558_000, config.channel_options.max_dust_htlc_exposure_msat)));
9412+
} else {
9413+
// Outbound dust balance: 5200 sats
9414+
unwrap_send_err!(nodes[1].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", 5_200_000, config.channel_options.max_dust_htlc_exposure_msat)));
9415+
}
9416+
} else {
9417+
let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1 ], if on_holder_tx { 2_186_000 } else { 200_000 });
9418+
nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
9419+
check_added_monitors!(nodes[0], 1);
9420+
let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9421+
assert_eq!(events.len(), 1);
9422+
let payment_event = SendEvent::from_event(events.remove(0));
9423+
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9424+
// With default dust exposure: 5000 sats
9425+
if on_holder_tx {
9426+
// Outbound dust balance: 6558 sats
9427+
nodes[1].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", 6_558_000, config.channel_options.max_dust_htlc_exposure_msat), 1);
9428+
} else {
9429+
// Outbound dust balance: 5200 sats
9430+
nodes[1].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", 5_200_000, config.channel_options.max_dust_htlc_exposure_msat), 1);
9431+
}
9432+
}
9433+
9434+
let _ = nodes[1].node.get_and_clear_pending_msg_events();
9435+
let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
9436+
added_monitors.clear();
9437+
}
9438+
9439+
#[test]
9440+
fn test_max_dust_htlc_exposure() {
9441+
do_test_max_dust_htlc_exposure(true, true, true);
9442+
do_test_max_dust_htlc_exposure(false, true, true);
9443+
do_test_max_dust_htlc_exposure(false, false, true);
9444+
do_test_max_dust_htlc_exposure(false, false, false);
9445+
do_test_max_dust_htlc_exposure(true, true, false);
9446+
do_test_max_dust_htlc_exposure(true, false, false);
9447+
do_test_max_dust_htlc_exposure(true, false, true);
9448+
do_test_max_dust_htlc_exposure(false, true, false);
9449+
}

0 commit comments

Comments
 (0)