@@ -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:: { IDEMPOTENCY_TIMEOUT_TICKS , 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 ;
@@ -1304,6 +1305,107 @@ 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 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
+ if let Payee :: Clear { features, .. } = & mut payment_params. payee {
1339
+ * features = Some ( invoice_features) ;
1340
+ }
1341
+
1342
+ let route_params = RouteParameters { payment_params, final_value_msat : 80_000_000 } ;
1343
+ let res = nodes[ 0 ] . node . send_preflight_probes ( route_params) . unwrap ( ) ;
1344
+
1345
+ // We check that only one probe was sent, the other one was skipped due to limited liquidity.
1346
+ assert_eq ! ( res. len( ) , 1 ) ;
1347
+ let log_msg = format ! ( "Skipped sending payment probe to avoid putting channel {} under the liquidity limit." ,
1348
+ first_chan_update. contents. short_channel_id) ;
1349
+ node_cfgs[ 0 ] . logger . assert_log_contains ( "lightning::ln::channelmanager" , & log_msg, 1 ) ;
1350
+
1351
+ let ( payment_hash, payment_id) = res. first ( ) . unwrap ( ) ;
1352
+
1353
+ // node[0] -- update_add_htlcs -> node[1]
1354
+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1355
+ let updates = get_htlc_update_msgs ! ( nodes[ 0 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1356
+ let probe_event = SendEvent :: from_commitment_update ( nodes[ 1 ] . node . get_our_node_id ( ) , updates) ;
1357
+ nodes[ 1 ] . node . handle_update_add_htlc ( & nodes[ 0 ] . node . get_our_node_id ( ) , & probe_event. msgs [ 0 ] ) ;
1358
+ check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
1359
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 0 ] , probe_event. commitment_msg, false ) ;
1360
+ expect_pending_htlcs_forwardable ! ( nodes[ 1 ] ) ;
1361
+
1362
+ // node[1] -- update_add_htlcs -> node[2]
1363
+ check_added_monitors ! ( nodes[ 1 ] , 1 ) ;
1364
+ let updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1365
+ let probe_event = SendEvent :: from_commitment_update ( nodes[ 2 ] . node . get_our_node_id ( ) , updates) ;
1366
+ nodes[ 2 ] . node . handle_update_add_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & probe_event. msgs [ 0 ] ) ;
1367
+ check_added_monitors ! ( nodes[ 2 ] , 0 ) ;
1368
+ commitment_signed_dance ! ( nodes[ 2 ] , nodes[ 1 ] , probe_event. commitment_msg, false ) ;
1369
+ expect_pending_htlcs_forwardable ! ( nodes[ 2 ] ) ;
1370
+
1371
+ // node[2] -- update_add_htlcs -> node[4]
1372
+ check_added_monitors ! ( nodes[ 2 ] , 1 ) ;
1373
+ let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 4 ] . node. get_our_node_id( ) ) ;
1374
+ let probe_event = SendEvent :: from_commitment_update ( nodes[ 2 ] . node . get_our_node_id ( ) , updates) ;
1375
+ nodes[ 4 ] . node . handle_update_add_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & probe_event. msgs [ 0 ] ) ;
1376
+ check_added_monitors ! ( nodes[ 4 ] , 0 ) ;
1377
+ commitment_signed_dance ! ( nodes[ 4 ] , nodes[ 2 ] , probe_event. commitment_msg, true , true ) ;
1378
+
1379
+ // node[2] <- update_fail_htlcs -- node[4]
1380
+ let updates = get_htlc_update_msgs ! ( nodes[ 4 ] , nodes[ 2 ] . node. get_our_node_id( ) ) ;
1381
+ nodes[ 2 ] . node . handle_update_fail_htlc ( & nodes[ 4 ] . node . get_our_node_id ( ) , & updates. update_fail_htlcs [ 0 ] ) ;
1382
+ check_added_monitors ! ( nodes[ 2 ] , 0 ) ;
1383
+ commitment_signed_dance ! ( nodes[ 2 ] , nodes[ 4 ] , updates. commitment_signed, true ) ;
1384
+
1385
+ // node[1] <- update_fail_htlcs -- node[2]
1386
+ let updates = get_htlc_update_msgs ! ( nodes[ 2 ] , nodes[ 1 ] . node. get_our_node_id( ) ) ;
1387
+ nodes[ 1 ] . node . handle_update_fail_htlc ( & nodes[ 2 ] . node . get_our_node_id ( ) , & updates. update_fail_htlcs [ 0 ] ) ;
1388
+ check_added_monitors ! ( nodes[ 1 ] , 0 ) ;
1389
+ commitment_signed_dance ! ( nodes[ 1 ] , nodes[ 2 ] , updates. commitment_signed, true ) ;
1390
+
1391
+ // node[0] <- update_fail_htlcs -- node[1]
1392
+ let updates = get_htlc_update_msgs ! ( nodes[ 1 ] , nodes[ 0 ] . node. get_our_node_id( ) ) ;
1393
+ nodes[ 0 ] . node . handle_update_fail_htlc ( & nodes[ 1 ] . node . get_our_node_id ( ) , & updates. update_fail_htlcs [ 0 ] ) ;
1394
+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
1395
+ commitment_signed_dance ! ( nodes[ 0 ] , nodes[ 1 ] , updates. commitment_signed, false ) ;
1396
+
1397
+ let mut events = nodes[ 0 ] . node . get_and_clear_pending_events ( ) ;
1398
+ assert_eq ! ( events. len( ) , 1 ) ;
1399
+ match events. drain ( ..) . next ( ) . unwrap ( ) {
1400
+ crate :: events:: Event :: ProbeSuccessful { payment_id : ev_pid, payment_hash : ev_ph, .. } => {
1401
+ assert_eq ! ( * payment_id, ev_pid) ;
1402
+ assert_eq ! ( * payment_hash, ev_ph) ;
1403
+ } ,
1404
+ _ => panic ! ( ) ,
1405
+ } ;
1406
+ assert ! ( !nodes[ 0 ] . node. has_pending_payments( ) ) ;
1407
+ }
1408
+
1307
1409
#[ test]
1308
1410
fn claimed_send_payment_idempotent ( ) {
1309
1411
// Tests that `send_payment` (and friends) are (reasonably) idempotent.
0 commit comments