@@ -9550,3 +9550,116 @@ fn test_tx_confirmed_skipping_blocks_immediate_broadcast() {
95509550 do_test_tx_confirmed_skipping_blocks_immediate_broadcast(false);
95519551 do_test_tx_confirmed_skipping_blocks_immediate_broadcast(true);
95529552}
9553+
9554+ fn do_test_max_balance_dust_htlc(dust_outbound_balance: bool, at_forward: bool, on_holder_tx: bool) {
9555+ // Test that we properly reject dust HTLC violating our `max_balance_dust_htlc_msat` policy.
9556+ //
9557+ // At HTLC forward (`send_payment()`), if the sum of the trimmed-to-dust HTLC inbound and
9558+ // trimmed-to-dust HTLC outbound balance and this new payment as included on next counterparty
9559+ // commitment are above our `max_balance_dust_htlc_msat`, we'll reject the update.
9560+ // At HTLC reception (`update_add_htlc()`), if the sum of the trimmed-to-dust HTLC inbound
9561+ // and trimmed-to-dust HTLC outbound balance and this new received HTLC as included on next
9562+ // counterparty commitment are above our `max_balance_dust_htlc_msat`, we'll fail the update.
9563+ // Note, we return a `temporary_channel_failure` (0x1000 | 7), as the channel might be
9564+ // available again for HTLC processing once the dust bandwidth has cleared up.
9565+
9566+ let chanmon_cfgs = create_chanmon_cfgs(2);
9567+ let mut config = test_default_channel_config();
9568+ config.channel_options.max_balance_dust_htlc_msat = 5_000_000; // default setting value
9569+ let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
9570+ let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, Some(config)]);
9571+ let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
9572+
9573+ nodes[0].node.create_channel(nodes[1].node.get_our_node_id(), 1_000_000, 500_000_000, 42, None).unwrap();
9574+ let mut open_channel = get_event_msg!(nodes[0], MessageSendEvent::SendOpenChannel, nodes[1].node.get_our_node_id());
9575+ open_channel.max_htlc_value_in_flight_msat = 50_000_000;
9576+ open_channel.max_accepted_htlcs = 60;
9577+ nodes[1].node.handle_open_channel(&nodes[0].node.get_our_node_id(), InitFeatures::known(), &open_channel);
9578+ let mut accept_channel = get_event_msg!(nodes[1], MessageSendEvent::SendAcceptChannel, nodes[0].node.get_our_node_id());
9579+ if on_holder_tx {
9580+ accept_channel.dust_limit_satoshis = 660;
9581+ }
9582+ nodes[0].node.handle_accept_channel(&nodes[1].node.get_our_node_id(), InitFeatures::known(), &accept_channel);
9583+
9584+ let (temporary_channel_id, tx, _) = create_funding_transaction(&nodes[0], 1_000_000, 42);
9585+
9586+ if on_holder_tx {
9587+ if let Some(mut chan) = nodes[1].node.channel_state.lock().unwrap().by_id.get_mut(&temporary_channel_id) {
9588+ chan.holder_dust_limit_satoshis = 660;
9589+ }
9590+ }
9591+
9592+ nodes[0].node.funding_transaction_generated(&temporary_channel_id, tx.clone()).unwrap();
9593+ 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()));
9594+ check_added_monitors!(nodes[1], 1);
9595+
9596+ 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()));
9597+ check_added_monitors!(nodes[0], 1);
9598+
9599+ let (funding_locked, _) = create_chan_between_nodes_with_value_confirm(&nodes[0], &nodes[1], &tx);
9600+ let (announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b(&nodes[0], &nodes[1], &funding_locked);
9601+ update_nodes_with_chan_announce(&nodes, 0, 1, &announcement, &as_update, &bs_update);
9602+
9603+ if on_holder_tx {
9604+ if dust_outbound_balance {
9605+ for i in 0..10 {
9606+ let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 508_000);
9607+ if let Err(_) = nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)) { panic!("Unexpected event at dust HTLC {}", i); }
9608+ }
9609+ } else {
9610+ for _ in 0..18 {
9611+ route_payment(&nodes[0], &[&nodes[1]], 508_000);
9612+ }
9613+ }
9614+ } else {
9615+ if dust_outbound_balance {
9616+ for i in 0..18 {
9617+ let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 100_000); // + 177_000 msat of HTLC-success tx at 253 sats/kWU
9618+ if let Err(_) = nodes[1].node.send_payment(&route, payment_hash, &Some(payment_secret)) { panic!("Unexpected event at dust HTLC {}", i); }
9619+ }
9620+ } else {
9621+ for _ in 0..18 {
9622+ route_payment(&nodes[0], &[&nodes[1]], 100_000); // + 167_000 msat of HTLC-timeout tx at 253 sats/kWU
9623+ }
9624+ }
9625+ }
9626+
9627+ if at_forward {
9628+ let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[1], nodes[0], 100_000);
9629+ let mut config = UserConfig::default();
9630+ if on_holder_tx {
9631+ 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 holder dusted balance {} on holder commitment over limit {}", if dust_outbound_balance { 6_750_000 } else { 12_330_000 }, config.channel_options.max_balance_dust_htlc_msat)));
9632+ } else {
9633+ 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 holder dusted balance {} on counterparty commitment over limit {}", if dust_outbound_balance { 4_986_000 } else { 4_806_000 }, config.channel_options.max_balance_dust_htlc_msat)));
9634+ }
9635+ } else {
9636+ let (route, payment_hash, _, payment_secret) = get_route_and_payment_hash!(nodes[0], nodes[1 ], 100_000);
9637+ nodes[0].node.send_payment(&route, payment_hash, &Some(payment_secret)).unwrap();
9638+ check_added_monitors!(nodes[0], 1);
9639+ let mut events = nodes[0].node.get_and_clear_pending_msg_events();
9640+ assert_eq!(events.len(), 1);
9641+ let payment_event = SendEvent::from_event(events.remove(0));
9642+ nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &payment_event.msgs[0]);
9643+ if on_holder_tx {
9644+ nodes[1].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot accept value that would put holder dusted balance {} on holder commitment over limit {}", if dust_outbound_balance { 6_750_000 } else { 12_330_000 }, config.channel_options.max_balance_dust_htlc_msat), 1);
9645+ } else {
9646+ nodes[1].logger.assert_log("lightning::ln::channel".to_string(), format!("Cannot accept value that would put holder dusted balance {} on counterparty commitment over limit {}", if dust_outbound_balance { 4_986_000 } else { 4_806_000 }, config.channel_options.max_balance_dust_htlc_msat), 1);
9647+ }
9648+ }
9649+
9650+ let _ = nodes[1].node.get_and_clear_pending_msg_events();
9651+ let mut added_monitors = nodes[1].chain_monitor.added_monitors.lock().unwrap();
9652+ added_monitors.clear();
9653+ }
9654+
9655+ #[test]
9656+ fn test_max_balance_dust_htlc() {
9657+ do_test_max_balance_dust_htlc(true, true, true);
9658+ do_test_max_balance_dust_htlc(false, true, true);
9659+ do_test_max_balance_dust_htlc(false, false, true);
9660+ do_test_max_balance_dust_htlc(false, false, false);
9661+ do_test_max_balance_dust_htlc(true, true, false);
9662+ do_test_max_balance_dust_htlc(true, false, false);
9663+ do_test_max_balance_dust_htlc(true, false, true);
9664+ do_test_max_balance_dust_htlc(false, true, false);
9665+ }
0 commit comments