Skip to content

Commit 3f60d9a

Browse files
committed
DRY the get_route_and_payment_hash!() macro duplicated in tests
1 parent ebb7600 commit 3f60d9a

File tree

2 files changed

+23
-61
lines changed

2 files changed

+23
-61
lines changed

lightning/src/ln/functional_test_utils.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,6 +904,18 @@ macro_rules! get_payment_preimage_hash {
904904
}
905905
}
906906

907+
#[cfg(test)]
908+
macro_rules! get_route_and_payment_hash {
909+
($send_node: expr, $recv_node: expr, $recv_value: expr) => {{
910+
let (payment_preimage, payment_hash) = get_payment_preimage_hash!($recv_node);
911+
let net_graph_msg_handler = &$send_node.net_graph_msg_handler;
912+
let route = get_route(&$send_node.node.get_our_node_id(),
913+
&net_graph_msg_handler.network_graph.read().unwrap(),
914+
&$recv_node.node.get_our_node_id(), None, None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, $send_node.logger).unwrap();
915+
(route, payment_hash, payment_preimage)
916+
}}
917+
}
918+
907919
macro_rules! expect_pending_htlcs_forwardable_ignore {
908920
($node: expr) => {{
909921
let events = $node.node.get_and_clear_pending_events();

lightning/src/ln/functional_tests.rs

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,18 +1569,8 @@ fn test_fee_spike_violation_fails_htlc() {
15691569
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
15701570
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
15711571
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
1572-
let logger = test_utils::TestLogger::new();
1573-
1574-
macro_rules! get_route_and_payment_hash {
1575-
($recv_value: expr) => {{
1576-
let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[1]);
1577-
let net_graph_msg_handler = &nodes[0].net_graph_msg_handler.network_graph.read().unwrap();
1578-
let route = get_route(&nodes[0].node.get_our_node_id(), net_graph_msg_handler, &nodes.last().unwrap().node.get_our_node_id(), None, None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
1579-
(route, payment_hash, payment_preimage)
1580-
}}
1581-
}
15821572

1583-
let (route, payment_hash, _) = get_route_and_payment_hash!(3460001);
1573+
let (route, payment_hash, _) = get_route_and_payment_hash!(nodes[0], nodes[1], 3460001);
15841574
// Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
15851575
let secp_ctx = Secp256k1::new();
15861576
let session_priv = SecretKey::from_slice(&[42; 32]).expect("RNG is bad!");
@@ -1709,18 +1699,8 @@ fn test_chan_reserve_violation_outbound_htlc_inbound_chan() {
17091699
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
17101700
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
17111701
let _ = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
1712-
let logger = test_utils::TestLogger::new();
1713-
1714-
macro_rules! get_route_and_payment_hash {
1715-
($recv_value: expr) => {{
1716-
let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[1]);
1717-
let net_graph_msg_handler = &nodes[1].net_graph_msg_handler;
1718-
let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.first().unwrap().node.get_our_node_id(), None, None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
1719-
(route, payment_hash, payment_preimage)
1720-
}}
1721-
}
17221702

1723-
let (route, our_payment_hash, _) = get_route_and_payment_hash!(4843000);
1703+
let (route, our_payment_hash, _) = get_route_and_payment_hash!(nodes[1], nodes[0], 4843000);
17241704
unwrap_send_err!(nodes[1].node.send_payment(&route, our_payment_hash, &None), true, APIError::ChannelUnavailable { ref err },
17251705
assert_eq!(err, "Cannot send value that would put counterparty balance under holder-announced channel reserve value"));
17261706
assert!(nodes[1].node.get_and_clear_pending_msg_events().is_empty());
@@ -1741,18 +1721,8 @@ fn test_chan_reserve_violation_inbound_htlc_outbound_channel() {
17411721
let node_chanmgrs = create_node_chanmgrs(2, &node_cfgs, &[None, None]);
17421722
let mut nodes = create_network(2, &node_cfgs, &node_chanmgrs);
17431723
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
1744-
let logger = test_utils::TestLogger::new();
17451724

1746-
macro_rules! get_route_and_payment_hash {
1747-
($recv_value: expr) => {{
1748-
let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[1]);
1749-
let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
1750-
let route = get_route(&nodes[1].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.first().unwrap().node.get_our_node_id(), None, None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
1751-
(route, payment_hash, payment_preimage)
1752-
}}
1753-
}
1754-
1755-
let (route, payment_hash, _) = get_route_and_payment_hash!(1000);
1725+
let (route, payment_hash, _) = get_route_and_payment_hash!(nodes[1], nodes[0], 1000);
17561726
// Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
17571727
let secp_ctx = Secp256k1::new();
17581728
let session_priv = SecretKey::from_slice(&[42; 32]).unwrap();
@@ -1837,16 +1807,6 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
18371807
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
18381808
let chan = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
18391809
let _ = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 100000, 95000000, InitFeatures::known(), InitFeatures::known());
1840-
let logger = test_utils::TestLogger::new();
1841-
1842-
macro_rules! get_route_and_payment_hash {
1843-
($recv_value: expr) => {{
1844-
let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]);
1845-
let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
1846-
let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.last().unwrap().node.get_our_node_id(), None, None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
1847-
(route, payment_hash, payment_preimage)
1848-
}}
1849-
}
18501810

18511811
let feemsat = 239;
18521812
let total_routing_fee_msat = (nodes.len() - 2) as u64 * feemsat;
@@ -1859,7 +1819,7 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
18591819
let amt_msat_1 = recv_value_1 + total_routing_fee_msat;
18601820

18611821
// Add a pending HTLC.
1862-
let (route_1, our_payment_hash_1, _) = get_route_and_payment_hash!(amt_msat_1);
1822+
let (route_1, our_payment_hash_1, _) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat_1);
18631823
let payment_event_1 = {
18641824
nodes[0].node.send_payment(&route_1, our_payment_hash_1, &None).unwrap();
18651825
check_added_monitors!(nodes[0], 1);
@@ -1874,7 +1834,7 @@ fn test_chan_reserve_violation_inbound_htlc_inbound_chan() {
18741834
let commit_tx_fee_2_htlcs = commit_tx_fee_msat(feerate, 2);
18751835
let recv_value_2 = chan_stat.value_to_self_msat - amt_msat_1 - chan_stat.channel_reserve_msat - total_routing_fee_msat - commit_tx_fee_2_htlcs + 1;
18761836
let amt_msat_2 = recv_value_2 + total_routing_fee_msat;
1877-
let (route_2, _, _) = get_route_and_payment_hash!(amt_msat_2);
1837+
let (route_2, _, _) = get_route_and_payment_hash!(nodes[0], nodes[2], amt_msat_2);
18781838

18791839
// Need to manually create the update_add_htlc message to go around the channel reserve check in send_htlc()
18801840
let secp_ctx = Secp256k1::new();
@@ -1932,23 +1892,13 @@ fn test_channel_reserve_holding_cell_htlcs() {
19321892
let mut nodes = create_network(3, &node_cfgs, &node_chanmgrs);
19331893
let chan_1 = create_announced_chan_between_nodes_with_value(&nodes, 0, 1, 190000, 1001, InitFeatures::known(), InitFeatures::known());
19341894
let chan_2 = create_announced_chan_between_nodes_with_value(&nodes, 1, 2, 190000, 1001, InitFeatures::known(), InitFeatures::known());
1935-
let logger = test_utils::TestLogger::new();
19361895

19371896
let mut stat01 = get_channel_value_stat!(nodes[0], chan_1.2);
19381897
let mut stat11 = get_channel_value_stat!(nodes[1], chan_1.2);
19391898

19401899
let mut stat12 = get_channel_value_stat!(nodes[1], chan_2.2);
19411900
let mut stat22 = get_channel_value_stat!(nodes[2], chan_2.2);
19421901

1943-
macro_rules! get_route_and_payment_hash {
1944-
($recv_value: expr) => {{
1945-
let (payment_preimage, payment_hash) = get_payment_preimage_hash!(nodes[0]);
1946-
let net_graph_msg_handler = &nodes[0].net_graph_msg_handler;
1947-
let route = get_route(&nodes[0].node.get_our_node_id(), &net_graph_msg_handler.network_graph.read().unwrap(), &nodes.last().unwrap().node.get_our_node_id(), None, None, &Vec::new(), $recv_value, TEST_FINAL_CLTV, &logger).unwrap();
1948-
(route, payment_hash, payment_preimage)
1949-
}}
1950-
}
1951-
19521902
macro_rules! expect_forward {
19531903
($node: expr) => {{
19541904
let mut events = $node.node.get_and_clear_pending_msg_events();
@@ -1967,7 +1917,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
19671917

19681918
// attempt to send amt_msat > their_max_htlc_value_in_flight_msat
19691919
{
1970-
let (mut route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_0);
1920+
let (mut route, our_payment_hash, _) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_0);
19711921
route.paths[0].last_mut().unwrap().fee_msat += 1;
19721922
assert!(route.paths[0].iter().rev().skip(1).all(|h| h.fee_msat == feemsat));
19731923
unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &None), true, APIError::ChannelUnavailable { ref err },
@@ -2019,7 +1969,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
20191969
let recv_value_1 = (stat01.value_to_self_msat - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs)/2;
20201970
let amt_msat_1 = recv_value_1 + total_fee_msat;
20211971

2022-
let (route_1, our_payment_hash_1, our_payment_preimage_1) = get_route_and_payment_hash!(recv_value_1);
1972+
let (route_1, our_payment_hash_1, our_payment_preimage_1) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_1);
20231973
let payment_event_1 = {
20241974
nodes[0].node.send_payment(&route_1, our_payment_hash_1, &None).unwrap();
20251975
check_added_monitors!(nodes[0], 1);
@@ -2033,7 +1983,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
20331983
// channel reserve test with htlc pending output > 0
20341984
let recv_value_2 = stat01.value_to_self_msat - amt_msat_1 - stat01.channel_reserve_msat - total_fee_msat - commit_tx_fee_2_htlcs;
20351985
{
2036-
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_2 + 1);
1986+
let (route, our_payment_hash, _) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_2 + 1);
20371987
unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &None), true, APIError::ChannelUnavailable { ref err },
20381988
assert!(regex::Regex::new(r"Cannot send value that would put our balance under counterparty-announced channel reserve value \(\d+\)").unwrap().is_match(err)));
20391989
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
@@ -2050,7 +2000,7 @@ fn test_channel_reserve_holding_cell_htlcs() {
20502000
}
20512001

20522002
// now see if they go through on both sides
2053-
let (route_21, our_payment_hash_21, our_payment_preimage_21) = get_route_and_payment_hash!(recv_value_21);
2003+
let (route_21, our_payment_hash_21, our_payment_preimage_21) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_21);
20542004
// but this will stuck in the holding cell
20552005
nodes[0].node.send_payment(&route_21, our_payment_hash_21, &None).unwrap();
20562006
check_added_monitors!(nodes[0], 0);
@@ -2059,14 +2009,14 @@ fn test_channel_reserve_holding_cell_htlcs() {
20592009

20602010
// test with outbound holding cell amount > 0
20612011
{
2062-
let (route, our_payment_hash, _) = get_route_and_payment_hash!(recv_value_22+1);
2012+
let (route, our_payment_hash, _) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22+1);
20632013
unwrap_send_err!(nodes[0].node.send_payment(&route, our_payment_hash, &None), true, APIError::ChannelUnavailable { ref err },
20642014
assert!(regex::Regex::new(r"Cannot send value that would put our balance under counterparty-announced channel reserve value \(\d+\)").unwrap().is_match(err)));
20652015
assert!(nodes[0].node.get_and_clear_pending_msg_events().is_empty());
20662016
nodes[0].logger.assert_log_contains("lightning::ln::channelmanager".to_string(), "Cannot send value that would put our balance under counterparty-announced channel reserve value".to_string(), 2);
20672017
}
20682018

2069-
let (route_22, our_payment_hash_22, our_payment_preimage_22) = get_route_and_payment_hash!(recv_value_22);
2019+
let (route_22, our_payment_hash_22, our_payment_preimage_22) = get_route_and_payment_hash!(nodes[0], nodes[2], recv_value_22);
20702020
// this will also stuck in the holding cell
20712021
nodes[0].node.send_payment(&route_22, our_payment_hash_22, &None).unwrap();
20722022
check_added_monitors!(nodes[0], 0);

0 commit comments

Comments
 (0)