@@ -25,6 +25,7 @@ use crate::ln::outbound_payment::{IDEMPOTENCY_TIMEOUT_TICKS, Retry};
25
25
use crate :: routing:: gossip:: { EffectiveCapacity , RoutingFees } ;
26
26
use crate :: routing:: router:: { get_route, Path , PaymentParameters , Route , Router , RouteHint , RouteHintHop , RouteHop , RouteParameters , find_route} ;
27
27
use crate :: routing:: scoring:: ChannelUsage ;
28
+ use crate :: util:: config:: UserConfig ;
28
29
use crate :: util:: test_utils;
29
30
use crate :: util:: errors:: APIError ;
30
31
use crate :: util:: ser:: Writeable ;
@@ -1304,6 +1305,102 @@ fn onchain_failed_probe_yields_event() {
1304
1305
assert ! ( !nodes[ 0 ] . node. has_pending_payments( ) ) ;
1305
1306
}
1306
1307
1308
+ #[ test]
1309
+ fn preflight_probes_yield_event_and_skip ( ) {
1310
+ let chanmon_cfgs = create_chanmon_cfgs ( 5 ) ;
1311
+ let node_cfgs = create_node_cfgs ( 5 , & chanmon_cfgs) ;
1312
+
1313
+ // We alleviate the HTLC max-in-flight limit, as otherwise we'd always be limited through that.
1314
+ let mut no_htlc_limit_config = test_default_channel_config ( ) ;
1315
+ no_htlc_limit_config. channel_handshake_config . max_inbound_htlc_value_in_flight_percent_of_channel = 100 ;
1316
+
1317
+ let user_configs = std:: iter:: repeat ( no_htlc_limit_config) . take ( 5 ) . map ( |c| Some ( c) ) . collect :: < Vec < Option < UserConfig > > > ( ) ;
1318
+ let node_chanmgrs = create_node_chanmgrs ( 5 , & node_cfgs, & user_configs) ;
1319
+ let nodes = create_network ( 5 , & node_cfgs, & node_chanmgrs) ;
1320
+
1321
+ // Setup channel topology:
1322
+ // (30k:0)- N2 -(1M:0)
1323
+ // / \
1324
+ // N0 -(100k:0)-> N1 N4
1325
+ // \ /
1326
+ // (70k:0)- N3 -(1M:0)
1327
+ //
1328
+ let first_chan_update = create_announced_chan_between_nodes_with_value ( & nodes, 0 , 1 , 100_000 , 0 ) . 0 ;
1329
+ create_announced_chan_between_nodes_with_value ( & nodes, 1 , 2 , 30_000 , 0 ) ;
1330
+ create_announced_chan_between_nodes_with_value ( & nodes, 1 , 3 , 70_000 , 0 ) ;
1331
+ create_announced_chan_between_nodes_with_value ( & nodes, 2 , 4 , 1_000_000 , 0 ) ;
1332
+ create_announced_chan_between_nodes_with_value ( & nodes, 3 , 4 , 1_000_000 , 0 ) ;
1333
+
1334
+ let mut invoice_features = Bolt11InvoiceFeatures :: empty ( ) ;
1335
+ invoice_features. set_basic_mpp_optional ( ) ;
1336
+
1337
+ let mut payment_params = PaymentParameters :: from_node_id ( nodes[ 4 ] . node . get_our_node_id ( ) , TEST_FINAL_CLTV )
1338
+ . with_bolt11_features ( invoice_features) . unwrap ( ) ;
1339
+
1340
+ let route_params = RouteParameters { payment_params, final_value_msat : 80_000_000 } ;
1341
+ let res = nodes[ 0 ] . node . send_preflight_probes ( route_params, None ) . unwrap ( ) ;
1342
+
1343
+ // We check that only one probe was sent, the other one was skipped due to limited liquidity.
1344
+ assert_eq ! ( res. len( ) , 1 ) ;
1345
+ let log_msg = format ! ( "Skipped sending payment probe to avoid putting channel {} under the liquidity limit." ,
1346
+ first_chan_update. contents. short_channel_id) ;
1347
+ node_cfgs[ 0 ] . logger . assert_log_contains ( "lightning::ln::channelmanager" , & log_msg, 1 ) ;
1348
+
1349
+ let ( payment_hash, payment_id) = res. first ( ) . unwrap ( ) ;
1350
+
1351
+ // node[0] -- update_add_htlcs -> node[1]
1352
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1353
+ let probe_event = SendEvent :: from_node ( & nodes[ 0 ] ) ;
1354
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & probe_event. msgs [ 0 ] ) ;
1355
+ check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
1356
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , probe_event. commitment_msg, false ) ;
1357
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1358
+
1359
+ // node[1] -- update_add_htlcs -> node[2]
1360
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1361
+ let probe_event = SendEvent :: from_node ( & nodes[ 1 ] ) ;
1362
+ nodes[ 2 ] . node . handle_update_add_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & probe_event. msgs [ 0 ] ) ;
1363
+ check_added_monitors ! ( nodes[ 2 ] , 0 ) ;
1364
+ commitment_signed_dance ! ( nodes[ 2 ] , nodes[ 1 ] , probe_event. commitment_msg, false ) ;
1365
+ expect_pending_htlcs_forwardable ! ( nodes[ 2 ] ) ;
1366
+
1367
+ // node[2] -- update_add_htlcs -> node[4]
1368
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1369
+ let probe_event = SendEvent :: from_node ( & nodes[ 2 ] ) ;
1370
+ nodes[ 4 ] . node . handle_update_add_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & probe_event. msgs [ 0 ] ) ;
1371
+ check_added_monitors ! ( nodes[ 4 ] , 0 ) ;
1372
+ commitment_signed_dance ! ( nodes[ 4 ] , nodes[ 2 ] , probe_event. commitment_msg, true , true ) ;
1373
+
1374
+ // node[2] <- update_fail_htlcs -- node[4]
1375
+ let updates = get_htlc_update_msgs ! ( nodes[ 4 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1376
+ nodes[ 2 ] . node . handle_update_fail_htlc ( & nodes[ 4 ] . node . get_our_node_id ( ) , & updates. update_fail_htlcs [ 0 ] ) ;
1377
+ check_added_monitors ! ( nodes[ 2 ] , 0 ) ;
1378
+ commitment_signed_dance ! ( nodes[ 2 ] , nodes[ 4 ] , updates. commitment_signed, true ) ;
1379
+
1380
+ // node[1] <- update_fail_htlcs -- node[2]
1381
+ let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1382
+ nodes[ 1 ] . node . handle_update_fail_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & updates. update_fail_htlcs [ 0 ] ) ;
1383
+ check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
1384
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 2 ] , updates. commitment_signed, true ) ;
1385
+
1386
+ // node[0] <- update_fail_htlcs -- node[1]
1387
+ let updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1388
+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & updates. update_fail_htlcs [ 0 ] ) ;
1389
+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
1390
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , updates. commitment_signed, false ) ;
1391
+
1392
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1393
+ assert_eq ! ( events. len( ) , 1 ) ;
1394
+ match events. drain ( ..) . next ( ) . unwrap ( ) {
1395
+ crate :: events:: Event :: ProbeSuccessful { payment_id : ev_pid, payment_hash : ev_ph, .. } => {
1396
+ assert_eq ! ( * payment_id, ev_pid) ;
1397
+ assert_eq ! ( * payment_hash, ev_ph) ;
1398
+ } ,
1399
+ _ => panic ! ( ) ,
1400
+ } ;
1401
+ assert ! ( !nodes[ 0 ] . node. has_pending_payments( ) ) ;
1402
+ }
1403
+
1307
1404
#[ test]
1308
1405
fn claimed_send_payment_idempotent ( ) {
1309
1406
// Tests that `send_payment` (and friends) are (reasonably) idempotent.
0 commit comments