@@ -32,6 +32,7 @@ use bitcoin::network::constants::Network;
3232use crate :: prelude:: * ;
3333
3434use crate :: ln:: functional_test_utils:: * ;
35+ use crate :: routing:: gossip:: NodeId ;
3536
3637#[ test]
3738fn retry_single_path_payment ( ) {
@@ -1239,3 +1240,133 @@ fn abandoned_send_payment_idempotent() {
12391240 pass_along_route ( & nodes[ 0 ] , & [ & [ & nodes[ 1 ] ] ] , 100_000 , second_payment_hash, second_payment_secret) ;
12401241 claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] ] , second_payment_preimage) ;
12411242}
1243+
1244+ #[ test]
1245+ fn test_trivial_inflight_htlc_tracking ( ) {
1246+ // In this test, we test three scenarios:
1247+ // (1) Sending + claiming a payment successfully should return `None` when querying InFlightHtlcs
1248+ // (2) Sending a payment without claiming it should return the payment's value (500000) when querying InFlightHtlcs
1249+ // (3) After we claim the payment sent in (2), InFlightHtlcs should return `None` for the query.
1250+ let chanmon_cfgs = create_chanmon_cfgs ( 3 ) ;
1251+ let node_cfgs = create_node_cfgs ( 3 , & chanmon_cfgs) ;
1252+ let node_chanmgrs = create_node_chanmgrs ( 3 , & node_cfgs, & [ None , None , None ] ) ;
1253+ let nodes = create_network ( 3 , & node_cfgs, & node_chanmgrs) ;
1254+
1255+ let ( _, _, chan_1_id, _) = create_announced_chan_between_nodes ( & nodes, 0 , 1 , channelmanager:: provided_init_features ( ) , channelmanager:: provided_init_features ( ) ) ;
1256+ let ( _, _, chan_2_id, _) = create_announced_chan_between_nodes ( & nodes, 1 , 2 , channelmanager:: provided_init_features ( ) , channelmanager:: provided_init_features ( ) ) ;
1257+
1258+ // Send and claim the payment. Inflight HTLCs should be empty.
1259+ send_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 500000 ) ;
1260+ {
1261+ let inflight_htlcs = node_chanmgrs[ 0 ] . compute_inflight_htlcs ( ) ;
1262+
1263+ let node_0_channel_lock = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
1264+ let node_1_channel_lock = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) ;
1265+ let channel_1 = node_0_channel_lock. by_id . get ( & chan_1_id) . unwrap ( ) ;
1266+ let channel_2 = node_1_channel_lock. by_id . get ( & chan_2_id) . unwrap ( ) ;
1267+
1268+ let chan_1_used_liquidity = inflight_htlcs. used_liquidity_msat (
1269+ & NodeId :: from_pubkey ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ,
1270+ & NodeId :: from_pubkey ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ,
1271+ channel_1. get_short_channel_id ( ) . unwrap ( )
1272+ ) ;
1273+ let chan_2_used_liquidity = inflight_htlcs. used_liquidity_msat (
1274+ & NodeId :: from_pubkey ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ,
1275+ & NodeId :: from_pubkey ( & nodes[ 2 ] . node . get_our_node_id ( ) ) ,
1276+ channel_2. get_short_channel_id ( ) . unwrap ( )
1277+ ) ;
1278+
1279+ assert_eq ! ( chan_1_used_liquidity, None ) ;
1280+ assert_eq ! ( chan_2_used_liquidity, None ) ;
1281+ }
1282+
1283+ // Send the payment, but do not claim it. Our inflight HTLCs should contain the pending payment.
1284+ let ( payment_preimage, _, _) = route_payment ( & nodes[ 0 ] , & vec ! ( & nodes[ 1 ] , & nodes[ 2 ] ) [ ..] , 500000 ) ;
1285+ {
1286+ let inflight_htlcs = node_chanmgrs[ 0 ] . compute_inflight_htlcs ( ) ;
1287+
1288+ let node_0_channel_lock = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
1289+ let node_1_channel_lock = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) ;
1290+ let channel_1 = node_0_channel_lock. by_id . get ( & chan_1_id) . unwrap ( ) ;
1291+ let channel_2 = node_1_channel_lock. by_id . get ( & chan_2_id) . unwrap ( ) ;
1292+
1293+ let chan_1_used_liquidity = inflight_htlcs. used_liquidity_msat (
1294+ & NodeId :: from_pubkey ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ,
1295+ & NodeId :: from_pubkey ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ,
1296+ channel_1. get_short_channel_id ( ) . unwrap ( )
1297+ ) ;
1298+ let chan_2_used_liquidity = inflight_htlcs. used_liquidity_msat (
1299+ & NodeId :: from_pubkey ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ,
1300+ & NodeId :: from_pubkey ( & nodes[ 2 ] . node . get_our_node_id ( ) ) ,
1301+ channel_2. get_short_channel_id ( ) . unwrap ( )
1302+ ) ;
1303+
1304+ // First hop accounts for expected 1000 msat fee
1305+ assert_eq ! ( chan_1_used_liquidity, Some ( 501000 ) ) ;
1306+ assert_eq ! ( chan_2_used_liquidity, Some ( 500000 ) ) ;
1307+ }
1308+
1309+ // Now, let's claim the payment. This should result in the used liquidity to return `None`.
1310+ claim_payment ( & nodes[ 0 ] , & [ & nodes[ 1 ] , & nodes[ 2 ] ] , payment_preimage) ;
1311+ {
1312+ let inflight_htlcs = node_chanmgrs[ 0 ] . compute_inflight_htlcs ( ) ;
1313+
1314+ let node_0_channel_lock = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
1315+ let node_1_channel_lock = nodes[ 1 ] . node . channel_state . lock ( ) . unwrap ( ) ;
1316+ let channel_1 = node_0_channel_lock. by_id . get ( & chan_1_id) . unwrap ( ) ;
1317+ let channel_2 = node_1_channel_lock. by_id . get ( & chan_2_id) . unwrap ( ) ;
1318+
1319+ let chan_1_used_liquidity = inflight_htlcs. used_liquidity_msat (
1320+ & NodeId :: from_pubkey ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ,
1321+ & NodeId :: from_pubkey ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ,
1322+ channel_1. get_short_channel_id ( ) . unwrap ( )
1323+ ) ;
1324+ let chan_2_used_liquidity = inflight_htlcs. used_liquidity_msat (
1325+ & NodeId :: from_pubkey ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ,
1326+ & NodeId :: from_pubkey ( & nodes[ 2 ] . node . get_our_node_id ( ) ) ,
1327+ channel_2. get_short_channel_id ( ) . unwrap ( )
1328+ ) ;
1329+
1330+ assert_eq ! ( chan_1_used_liquidity, None ) ;
1331+ assert_eq ! ( chan_2_used_liquidity, None ) ;
1332+ }
1333+ }
1334+
1335+ #[ test]
1336+ fn test_holding_cell_inflight_htlcs ( ) {
1337+ let chanmon_cfgs = create_chanmon_cfgs ( 2 ) ;
1338+ let node_cfgs = create_node_cfgs ( 2 , & chanmon_cfgs) ;
1339+ let node_chanmgrs = create_node_chanmgrs ( 2 , & node_cfgs, & [ None , None ] ) ;
1340+ let mut nodes = create_network ( 2 , & node_cfgs, & node_chanmgrs) ;
1341+ let channel_id = create_announced_chan_between_nodes ( & nodes, 0 , 1 , channelmanager:: provided_init_features ( ) , channelmanager:: provided_init_features ( ) ) . 2 ;
1342+
1343+ let ( route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash ! ( nodes[ 0 ] , nodes[ 1 ] , 1000000 ) ;
1344+ let ( _, payment_hash_2, payment_secret_2) = get_payment_preimage_hash ! ( nodes[ 1 ] ) ;
1345+
1346+ // Queue up two payments - one will be delivered right away, one immediately goes into the
1347+ // holding cell as nodes[0] is AwaitingRAA.
1348+ {
1349+ nodes[ 0 ] . node . send_payment ( & route, payment_hash_1, & Some ( payment_secret_1) , PaymentId ( payment_hash_1. 0 ) ) . unwrap ( ) ;
1350+ check_added_monitors ! ( nodes[ 0 ] , 1 ) ;
1351+ nodes[ 0 ] . node . send_payment ( & route, payment_hash_2, & Some ( payment_secret_2) , PaymentId ( payment_hash_2. 0 ) ) . unwrap ( ) ;
1352+ check_added_monitors ! ( nodes[ 0 ] , 0 ) ;
1353+ }
1354+
1355+ let inflight_htlcs = node_chanmgrs[ 0 ] . compute_inflight_htlcs ( ) ;
1356+
1357+ {
1358+ let channel_lock = nodes[ 0 ] . node . channel_state . lock ( ) . unwrap ( ) ;
1359+ let channel = channel_lock. by_id . get ( & channel_id) . unwrap ( ) ;
1360+
1361+ let used_liquidity = inflight_htlcs. used_liquidity_msat (
1362+ & NodeId :: from_pubkey ( & nodes[ 0 ] . node . get_our_node_id ( ) ) ,
1363+ & NodeId :: from_pubkey ( & nodes[ 1 ] . node . get_our_node_id ( ) ) ,
1364+ channel. get_short_channel_id ( ) . unwrap ( )
1365+ ) ;
1366+
1367+ assert_eq ! ( used_liquidity, Some ( 2000000 ) ) ;
1368+ }
1369+
1370+ // Clear pending events so test doesn't throw a "Had excess message on node..." error
1371+ nodes[ 0 ] . node . get_and_clear_pending_msg_events ( ) ;
1372+ }
0 commit comments