Skip to content

Commit a9a01e6

Browse files
committed
Test whether path length is acutally limited.
1 parent 951ba5d commit a9a01e6

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

lightning/src/routing/router.rs

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1891,7 +1891,7 @@ mod tests {
18911891
}
18921892

18931893
fn get_nodes(secp_ctx: &Secp256k1<All>) -> (SecretKey, PublicKey, Vec<SecretKey>, Vec<PublicKey>) {
1894-
let privkeys: Vec<SecretKey> = (2..10).map(|i| {
1894+
let privkeys: Vec<SecretKey> = (2..23).map(|i| {
18951895
SecretKey::from_slice(&hex::decode(format!("{:02x}", i).repeat(32)).unwrap()[..]).unwrap()
18961896
}).collect();
18971897

@@ -1918,6 +1918,56 @@ mod tests {
19181918
}
19191919
}
19201920

1921+
fn build_line_graph() -> (
1922+
Secp256k1<All>,
1923+
sync::Arc<NetworkGraph>,
1924+
NetGraphMsgHandler<sync::Arc<NetworkGraph>, sync::Arc<test_utils::TestChainSource>, sync::Arc<crate::util::test_utils::TestLogger>>,
1925+
sync::Arc<test_utils::TestChainSource>,
1926+
sync::Arc<test_utils::TestLogger>,
1927+
) {
1928+
let secp_ctx = Secp256k1::new();
1929+
let logger = Arc::new(test_utils::TestLogger::new());
1930+
let chain_monitor = Arc::new(test_utils::TestChainSource::new(Network::Testnet));
1931+
let network_graph = Arc::new(NetworkGraph::new(genesis_block(Network::Testnet).header.block_hash()));
1932+
let net_graph_msg_handler = NetGraphMsgHandler::new(Arc::clone(&network_graph), None, Arc::clone(&logger));
1933+
1934+
// Build network from our_id to node 20:
1935+
// our_id -1(1)2- node0 -1(2)2- node1 - ... - node20
1936+
let (our_privkey, _, privkeys, _) = get_nodes(&secp_ctx);
1937+
1938+
for (idx, (cur_privkey, next_privkey)) in core::iter::once(&our_privkey).chain(privkeys.iter()).zip(privkeys.iter()).enumerate() {
1939+
let cur_short_channel_id = (idx as u64) + 1;
1940+
add_channel(&net_graph_msg_handler, &secp_ctx, &cur_privkey, &next_privkey, ChannelFeatures::from_le_bytes(id_to_feature_flags(1)), cur_short_channel_id);
1941+
update_channel(&net_graph_msg_handler, &secp_ctx, &cur_privkey, UnsignedChannelUpdate {
1942+
chain_hash: genesis_block(Network::Testnet).header.block_hash(),
1943+
short_channel_id: cur_short_channel_id,
1944+
timestamp: idx as u32,
1945+
flags: 0,
1946+
cltv_expiry_delta: 0,
1947+
htlc_minimum_msat: 0,
1948+
htlc_maximum_msat: OptionalField::Absent,
1949+
fee_base_msat: 0,
1950+
fee_proportional_millionths: 0,
1951+
excess_data: Vec::new()
1952+
});
1953+
update_channel(&net_graph_msg_handler, &secp_ctx, &next_privkey, UnsignedChannelUpdate {
1954+
chain_hash: genesis_block(Network::Testnet).header.block_hash(),
1955+
short_channel_id: cur_short_channel_id,
1956+
timestamp: (idx as u32)+1,
1957+
flags: 1,
1958+
cltv_expiry_delta: 0,
1959+
htlc_minimum_msat: 0,
1960+
htlc_maximum_msat: OptionalField::Absent,
1961+
fee_base_msat: 0,
1962+
fee_proportional_millionths: 0,
1963+
excess_data: Vec::new()
1964+
});
1965+
add_or_update_node(&net_graph_msg_handler, &secp_ctx, next_privkey, NodeFeatures::from_le_bytes(id_to_feature_flags(1)), 0);
1966+
}
1967+
1968+
(secp_ctx, network_graph, net_graph_msg_handler, chain_monitor, logger)
1969+
}
1970+
19211971
fn build_graph() -> (
19221972
Secp256k1<All>,
19231973
sync::Arc<NetworkGraph>,
@@ -5268,6 +5318,33 @@ mod tests {
52685318
}
52695319
}
52705320

5321+
#[test]
5322+
fn limits_path_length() {
5323+
let (secp_ctx, network, _, _, logger) = build_line_graph();
5324+
let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
5325+
let network_graph = network.read_only();
5326+
5327+
let scorer = test_utils::TestScorer::with_penalty(0);
5328+
let keys_manager = test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
5329+
let random_seed_bytes = keys_manager.get_secure_random_bytes();
5330+
5331+
// First check we can actually create a long route on this graph.
5332+
let feasible_payment_params = PaymentParameters::from_node_id(nodes[19]);
5333+
let route = get_route(&our_id, &feasible_payment_params, &network_graph, None, 100, 0, Arc::clone(&logger), &scorer, &random_seed_bytes).unwrap();
5334+
let path = route.paths[0].iter().map(|hop| hop.short_channel_id).collect::<Vec<_>>();
5335+
assert!(path.len() == 20);
5336+
5337+
// But we can't create a path surpassing the 20-hop MAX_PATH_LENGTH_ESTIMATE limit.
5338+
let fail_payment_params = PaymentParameters::from_node_id(nodes[20]);
5339+
match get_route(&our_id, &fail_payment_params, &network_graph, None, 100, 0, Arc::clone(&logger), &scorer, &random_seed_bytes)
5340+
{
5341+
Err(LightningError { err, .. } ) => {
5342+
assert_eq!(err, "Failed to find a path to the given destination");
5343+
},
5344+
Ok(_) => panic!("Expected error"),
5345+
}
5346+
}
5347+
52715348
#[test]
52725349
fn adds_and_limits_cltv_offset() {
52735350
let (secp_ctx, network_graph, _, _, logger) = build_graph();

0 commit comments

Comments
 (0)