Skip to content

Commit 2c8fdd3

Browse files
committed
Add functional test for inflight HTLC tracking with ChanManager
1 parent 83ef350 commit 2c8fdd3

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::ln::channelmanager::{self, ChannelManager, ChannelManagerReadArgs, Pa
2424
use crate::ln::channel::{Channel, ChannelError};
2525
use crate::ln::{chan_utils, onion_utils};
2626
use crate::ln::chan_utils::{OFFERED_HTLC_SCRIPT_WEIGHT, htlc_success_tx_weight, htlc_timeout_tx_weight, HTLCOutputInCommitment};
27-
use crate::routing::gossip::{NetworkGraph, NetworkUpdate};
27+
use crate::routing::gossip::{NetworkGraph, NetworkUpdate, NodeId};
2828
use crate::routing::router::{PaymentParameters, Route, RouteHop, RouteParameters, find_route, get_route};
2929
use crate::ln::features::{ChannelFeatures, NodeFeatures};
3030
use crate::ln::msgs;

lightning/src/ln/payment_tests.rs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use bitcoin::network::constants::Network;
3434
use crate::prelude::*;
3535

3636
use crate::ln::functional_test_utils::*;
37+
use crate::routing::gossip::NodeId;
3738

3839
#[test]
3940
fn retry_single_path_payment() {
@@ -1385,3 +1386,74 @@ fn abandoned_send_payment_idempotent() {
13851386
pass_along_route(&nodes[0], &[&[&nodes[1]]], 100_000, second_payment_hash, second_payment_secret);
13861387
claim_payment(&nodes[0], &[&nodes[1]], second_payment_preimage);
13871388
}
1389+
1390+
1391+
#[test]
1392+
fn test_trivial_inflight_htlc_tracking() {
1393+
let chanmon_cfgs = create_chanmon_cfgs(2);
1394+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1395+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1396+
let nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1397+
1398+
let (_, _, chan_id, _) = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features());
1399+
1400+
// Send and claim the payment. Inflight HTLCs should be empty.
1401+
send_payment(&nodes[0], &vec!(&nodes[1])[..], 500000);
1402+
assert_eq!(node_chanmgrs[0].compute_inflight_htlcs().0.len(), 0);
1403+
1404+
// Send the payment, but do not claim it. Our inflight HTLCs should contain the pending payment
1405+
route_payment(&nodes[0], &vec!(&nodes[1])[..], 500000);
1406+
let inflight_htlcs = node_chanmgrs[0].compute_inflight_htlcs();
1407+
1408+
{
1409+
let channel_lock = nodes[0].node.channel_state.lock().unwrap();
1410+
let channel = channel_lock.by_id.get(&chan_id).unwrap();
1411+
1412+
let used_liquidity = inflight_htlcs.used_liquidity_msat(
1413+
&NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
1414+
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1415+
channel.get_short_channel_id().unwrap()
1416+
);
1417+
1418+
assert_eq!(used_liquidity, Some(500000));
1419+
}
1420+
}
1421+
1422+
#[test]
1423+
fn test_holding_cell_inflight_htlcs() {
1424+
let chanmon_cfgs = create_chanmon_cfgs(2);
1425+
let node_cfgs = create_node_cfgs(2, &chanmon_cfgs);
1426+
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
1427+
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
1428+
let channel_id = create_announced_chan_between_nodes(&nodes, 0, 1, channelmanager::provided_init_features(), channelmanager::provided_init_features()).2;
1429+
1430+
let (route, payment_hash_1, _, payment_secret_1) = get_route_and_payment_hash!(nodes[0], nodes[1], 1000000);
1431+
let (_, payment_hash_2, payment_secret_2) = get_payment_preimage_hash!(nodes[1]);
1432+
1433+
// Queue up two payments - one will be delivered right away, one immediately goes into the
1434+
// holding cell as nodes[0] is AwaitingRAA.
1435+
{
1436+
nodes[0].node.send_payment(&route, payment_hash_1, &Some(payment_secret_1), PaymentId(payment_hash_1.0)).unwrap();
1437+
check_added_monitors!(nodes[0], 1);
1438+
nodes[0].node.send_payment(&route, payment_hash_2, &Some(payment_secret_2), PaymentId(payment_hash_2.0)).unwrap();
1439+
check_added_monitors!(nodes[0], 0);
1440+
}
1441+
1442+
let inflight_htlcs = node_chanmgrs[0].compute_inflight_htlcs();
1443+
1444+
{
1445+
let channel_lock = nodes[0].node.channel_state.lock().unwrap();
1446+
let channel = channel_lock.by_id.get(&channel_id).unwrap();
1447+
1448+
let used_liquidity = inflight_htlcs.used_liquidity_msat(
1449+
&NodeId::from_pubkey(&nodes[0].node.get_our_node_id()) ,
1450+
&NodeId::from_pubkey(&nodes[1].node.get_our_node_id()),
1451+
channel.get_short_channel_id().unwrap()
1452+
);
1453+
1454+
assert_eq!(used_liquidity, Some(2000000));
1455+
}
1456+
1457+
// Clear pending events so test doesn't throw a "Had excess message on node..." error
1458+
nodes[0].node.get_and_clear_pending_msg_events();
1459+
}

0 commit comments

Comments
 (0)