@@ -22,7 +22,7 @@ use ln::channel::{COMMITMENT_TX_BASE_WEIGHT, COMMITMENT_TX_WEIGHT_PER_HTLC};
22
22
use ln:: channelmanager:: { ChannelManager , ChannelManagerReadArgs , PaymentId , RAACommitmentOrder , PaymentSendFailure , BREAKDOWN_TIMEOUT , MIN_CLTV_EXPIRY_DELTA } ;
23
23
use ln:: channel:: { Channel , ChannelError } ;
24
24
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 } ;
26
26
use routing:: network_graph:: { NetworkUpdate , RoutingFees } ;
27
27
use routing:: router:: { Payee , Route , RouteHop , RouteHint , RouteHintHop , RouteParameters , find_route, get_route} ;
28
28
use ln:: features:: { ChannelFeatures , InitFeatures , InvoiceFeatures , NodeFeatures } ;
@@ -9202,3 +9202,152 @@ fn test_keysend_payments_to_private_node() {
9202
9202
pass_along_path ( & nodes[ 0 ] , & path, 10000 , payment_hash, None , event, true , Some ( test_preimage) ) ;
9203
9203
claim_payment ( & nodes[ 0 ] , & path, test_preimage) ;
9204
9204
}
9205
+
9206
+ fn do_test_max_dust_htlc_exposure ( dust_outbound_balance : bool , at_forward : bool , on_holder_tx : bool ) {
9207
+ // Test that we properly reject dust HTLC violating our `max_dust_htlc_exposure_msat`
9208
+ // policy.
9209
+ //
9210
+ // At HTLC forward (`send_payment()`), if the sum of the trimmed-to-dust HTLC inbound and
9211
+ // trimmed-to-dust HTLC outbound balance and this new payment as included on next
9212
+ // counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll reject the
9213
+ // update. At HTLC reception (`update_add_htlc()`), if the sum of the trimmed-to-dust HTLC
9214
+ // inbound and trimmed-to-dust HTLC outbound balance and this new received HTLC as included
9215
+ // on next counterparty commitment are above our `max_dust_htlc_exposure_msat`, we'll fail
9216
+ // the update. Note, we return a `temporary_channel_failure` (0x1000 | 7), as the channel
9217
+ // might be available again for HTLC processing once the dust bandwidth has cleared up.
9218
+
9219
+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
9220
+ let mut config = test_default_channel_config ( ) ;
9221
+ config. channel_options . max_dust_htlc_exposure_msat = 5_000_000 ; // default setting value
9222
+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
9223
+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , Some ( config) ] ) ;
9224
+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
9225
+
9226
+ nodes[ 0 ] . node . create_channel ( nodes[ 1 ] . node . get_our_node_id ( ) , 1_000_000 , 500_000_000 , 42 , None ) . unwrap ( ) ;
9227
+ let mut open_channel = get_event_msg ! ( nodes[ 0 ] , MessageSendEvent :: SendOpenChannel , nodes[ 1 ] . node. get_our_node_id( ) ) ;
9228
+ open_channel. max_htlc_value_in_flight_msat = 50_000_000 ;
9229
+ open_channel. max_accepted_htlcs = 60 ;
9230
+ nodes[ 1 ] . node . handle_open_channel ( & nodes[ 0 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & open_channel) ;
9231
+ let mut accept_channel = get_event_msg ! ( nodes[ 1 ] , MessageSendEvent :: SendAcceptChannel , nodes[ 0 ] . node. get_our_node_id( ) ) ;
9232
+ if on_holder_tx {
9233
+ accept_channel. dust_limit_satoshis = 546 ;
9234
+ }
9235
+ nodes[ 0 ] . node . handle_accept_channel ( & nodes[ 1 ] . node . get_our_node_id ( ) , InitFeatures :: known ( ) , & accept_channel) ;
9236
+
9237
+ let ( temporary_channel_id, tx, _) = create_funding_transaction ( & nodes[ 0 ] , 1_000_000 , 42 ) ;
9238
+
9239
+ if on_holder_tx {
9240
+ if let Some ( mut chan) = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) . by_id . get_mut ( & temporary_channel_id) {
9241
+ chan. holder_dust_limit_satoshis = 546 ;
9242
+ }
9243
+ }
9244
+
9245
+ nodes[ 0 ] . node . funding_transaction_generated ( & temporary_channel_id, tx. clone ( ) ) . unwrap ( ) ;
9246
+ 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( ) ) ) ;
9247
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
9248
+
9249
+ 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( ) ) ) ;
9250
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
9251
+
9252
+ let ( funding_locked, channel_id) = create_chan_between_nodes_with_value_confirm ( & nodes[ 0 ] , & nodes[ 1 ] , & tx) ;
9253
+ let ( announcement, as_update, bs_update) = create_chan_between_nodes_with_value_b ( & nodes[ 0 ] , & nodes[ 1 ] , & funding_locked) ;
9254
+ update_nodes_with_chan_announce ( & nodes, 0 , 1 , & announcement, & as_update, & bs_update) ;
9255
+
9256
+ let dust_buffer_feerate = {
9257
+ let chan_lock = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
9258
+ let chan = chan_lock. by_id . get ( & channel_id) . unwrap ( ) ;
9259
+ chan. get_dust_buffer_feerate ( None ) as u64
9260
+ } ;
9261
+ let dust_outbound_htlc_on_holder_tx_msat: u64 = ( dust_buffer_feerate * HTLC_SUCCESS_TX_WEIGHT / 1000 + open_channel. dust_limit_satoshis + 1 ) * 1000 ;
9262
+ println ! ( "dust outbound htlc on holder tx msat {}" , dust_outbound_htlc_on_holder_tx_msat) ;
9263
+ let dust_outbound_htlc_on_holder_tx: u64 = config. channel_options . max_dust_htlc_exposure_msat / dust_outbound_htlc_on_holder_tx_msat;
9264
+ println ! ( "dust outbound htlc on holder tx {}" , dust_outbound_htlc_on_holder_tx) ;
9265
+
9266
+ let dust_inbound_htlc_on_holder_tx_msat: u64 = ( dust_buffer_feerate * HTLC_TIMEOUT_TX_WEIGHT / 1000 + open_channel. dust_limit_satoshis + 1 ) * 1000 ;
9267
+ println ! ( "dust inbound htlc on holder tx msat {}" , dust_inbound_htlc_on_holder_tx_msat) ;
9268
+ let dust_inbound_htlc_on_holder_tx: u64 = config. channel_options . max_dust_htlc_exposure_msat / dust_inbound_htlc_on_holder_tx_msat;
9269
+ println ! ( "dust inbound htlc on holder tx {}" , dust_inbound_htlc_on_holder_tx) ;
9270
+
9271
+ let dust_htlc_on_counterparty_tx: u64 = 25 ;
9272
+ let dust_htlc_on_counterparty_tx_msat: u64 = config. channel_options . max_dust_htlc_exposure_msat / dust_htlc_on_counterparty_tx;
9273
+
9274
+ if on_holder_tx {
9275
+ if dust_outbound_balance {
9276
+ // Outbound dust threshold: 2223 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9277
+ // Outbound dust balance: 4372 sats
9278
+ // Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2132 sats
9279
+ for i in 0 ..dust_outbound_htlc_on_holder_tx {
9280
+ let ( route, payment_hash, _, payment_secret) = get_route_and_payment_hash ! ( nodes[ 1 ] , nodes[ 0 ] , dust_outbound_htlc_on_holder_tx_msat) ;
9281
+ if let Err ( _) = nodes[ 1 ] . node . send_payment ( & route, payment_hash, & Some ( payment_secret) ) { panic ! ( "Unexpected event at dust HTLC {}" , i) ; }
9282
+ }
9283
+ } else {
9284
+ // Inbound dust threshold: 2324 sats (`dust_buffer_feerate` * HTLC_SUCCESS_TX_WEIGHT / 1000 + holder's `dust_limit_satoshis`)
9285
+ // Inbound dust balance: 4372 sats
9286
+ // Note, we need sent payment to be above outbound dust threshold on counterparty_tx of 2031 sats
9287
+ for _ in 0 ..dust_inbound_htlc_on_holder_tx {
9288
+ route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , dust_inbound_htlc_on_holder_tx_msat) ;
9289
+ }
9290
+ }
9291
+ } else {
9292
+ if dust_outbound_balance {
9293
+ // Outbound dust threshold: 2132 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9294
+ // Outbound dust balance: 5000 sats
9295
+ for i in 0 ..dust_htlc_on_counterparty_tx {
9296
+ let ( route, payment_hash, _, payment_secret) = get_route_and_payment_hash ! ( nodes[ 1 ] , nodes[ 0 ] , dust_htlc_on_counterparty_tx_msat) ;
9297
+ if let Err ( _) = nodes[ 1 ] . node . send_payment ( & route, payment_hash, & Some ( payment_secret) ) { panic ! ( "Unexpected event at dust HTLC {}" , i) ; }
9298
+ }
9299
+ } else {
9300
+ // Inbound dust threshold: 2031 sats (`dust_buffer_feerate` * HTLC_TIMEOUT_TX_WEIGHT / 1000 + counteparty's `dust_limit_satoshis`)
9301
+ // Inbound dust balance: 5000 sats
9302
+ for _ in 0 ..dust_htlc_on_counterparty_tx {
9303
+ route_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , dust_htlc_on_counterparty_tx_msat) ;
9304
+ }
9305
+ }
9306
+ }
9307
+
9308
+ let dust_outbound_overflow = dust_outbound_htlc_on_holder_tx_msat * ( dust_outbound_htlc_on_holder_tx + 1 ) ;
9309
+ let dust_inbound_overflow = dust_inbound_htlc_on_holder_tx_msat * ( dust_inbound_htlc_on_holder_tx + 1 ) ;
9310
+ let dust_overflow = dust_htlc_on_counterparty_tx_msat * ( dust_htlc_on_counterparty_tx + 1 ) ;
9311
+ if at_forward {
9312
+ let ( route, payment_hash, _, payment_secret) = get_route_and_payment_hash ! ( nodes[ 1 ] , nodes[ 0 ] , if on_holder_tx { if dust_outbound_balance { dust_outbound_htlc_on_holder_tx_msat } else { dust_inbound_htlc_on_holder_tx_msat } } else { dust_htlc_on_counterparty_tx_msat } ) ;
9313
+ let mut config = UserConfig :: default ( ) ;
9314
+ // With default dust exposure: 5000 sats
9315
+ if on_holder_tx {
9316
+ 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" , if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow } , config. channel_options. max_dust_htlc_exposure_msat) ) ) ;
9317
+ } else {
9318
+ 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" , dust_overflow, config. channel_options. max_dust_htlc_exposure_msat) ) ) ;
9319
+ }
9320
+ } else {
9321
+ let ( route, payment_hash, _, payment_secret) = get_route_and_payment_hash ! ( nodes[ 0 ] , nodes[ 1 ] , if on_holder_tx { if dust_outbound_balance { dust_outbound_htlc_on_holder_tx_msat } else { dust_inbound_htlc_on_holder_tx_msat } } else { dust_htlc_on_counterparty_tx_msat } ) ;
9322
+ nodes[ 0 ] . node . send_payment ( & route, payment_hash, & Some ( payment_secret) ) . unwrap ( ) ;
9323
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
9324
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
9325
+ assert_eq ! ( events. len( ) , 1 ) ;
9326
+ let payment_event = SendEvent :: from_event ( events. remove ( 0 ) ) ;
9327
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & payment_event. msgs [ 0 ] ) ;
9328
+ // With default dust exposure: 5000 sats
9329
+ if on_holder_tx {
9330
+ // Outbound dust balance: 6399 sats
9331
+ 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" , if dust_outbound_balance { dust_outbound_overflow } else { dust_inbound_overflow } , config. channel_options. max_dust_htlc_exposure_msat) , 1 ) ;
9332
+ } else {
9333
+ // Outbound dust balance: 5200 sats
9334
+ 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" , dust_overflow, config. channel_options. max_dust_htlc_exposure_msat) , 1 ) ;
9335
+ }
9336
+ }
9337
+
9338
+ let _ = nodes[ 1 ] . node . get_and_clear_pending_msg_events ( ) ;
9339
+ let mut added_monitors = nodes[ 1 ] . chain_monitor . added_monitors . lock ( ) . unwrap ( ) ;
9340
+ added_monitors. clear ( ) ;
9341
+ }
9342
+
9343
+ #[ test]
9344
+ fn test_max_dust_htlc_exposure ( ) {
9345
+ do_test_max_dust_htlc_exposure ( true , true , true ) ;
9346
+ do_test_max_dust_htlc_exposure ( false , true , true ) ;
9347
+ do_test_max_dust_htlc_exposure ( false , false , true ) ;
9348
+ do_test_max_dust_htlc_exposure ( false , false , false ) ;
9349
+ do_test_max_dust_htlc_exposure ( true , true , false ) ;
9350
+ do_test_max_dust_htlc_exposure ( true , false , false ) ;
9351
+ do_test_max_dust_htlc_exposure ( true , false , true ) ;
9352
+ do_test_max_dust_htlc_exposure ( false , true , false ) ;
9353
+ }
0 commit comments