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