Skip to content

Commit fe1a516

Browse files
committed
Use outbound_capacity_msat from first_hops for routing
1 parent 276d18c commit fe1a516

File tree

1 file changed

+71
-12
lines changed

1 file changed

+71
-12
lines changed

lightning/src/routing/router.rs

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
368368
} else if chan.remote_network_id == *our_node_id {
369369
return Err(LightningError{err: "First hop cannot have our_node_id as a destination.".to_owned(), action: ErrorAction::IgnoreError});
370370
}
371-
first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone()));
371+
first_hop_targets.insert(chan.remote_network_id, (short_channel_id, chan.counterparty_features.clone(), chan.outbound_capacity_msat));
372372
}
373373
if first_hop_targets.is_empty() {
374374
return Err(LightningError{err: "Cannot route when there are no outbound routes away from us".to_owned(), action: ErrorAction::IgnoreError});
@@ -378,8 +378,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
378378
// We don't want multiple paths (as per MPP) share liquidity of the same channels.
379379
// This map allows paths to be aware of the channel use by other paths in the same call.
380380
// This would help to make a better path finding decisions and not "overbook" channels.
381-
// It is unaware of the directions.
382-
// TODO: we could let a caller specify this. Definitely useful when considering our own channels.
381+
// It is unaware of the directions (except for `outbound_capacity_msat` in `first_hops`).
383382
let mut bookkeeped_channels_liquidity_available_msat = HashMap::new();
384383

385384
// Keeping track of how much value we already collected across other paths. Helps to decide:
@@ -576,8 +575,8 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
576575
macro_rules! add_entries_to_cheapest_to_target_node {
577576
( $node: expr, $node_id: expr, $fee_to_target_msat: expr, $next_hops_value_contribution: expr ) => {
578577
if first_hops.is_some() {
579-
if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&$node_id) {
580-
add_entry!(first_hop, *our_node_id, $node_id, dummy_directional_info, None::<u64>, features.to_context(), $fee_to_target_msat, $next_hops_value_contribution);
578+
if let Some(&(ref first_hop, ref features, ref outbound_capacity_msat)) = first_hop_targets.get(&$node_id) {
579+
add_entry!(first_hop, *our_node_id, $node_id, dummy_directional_info, Some(outbound_capacity_msat / 1000), features.to_context(), $fee_to_target_msat, $next_hops_value_contribution);
581580
}
582581
}
583582

@@ -646,7 +645,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
646645
// it matters only if the fees are exactly the same.
647646
for hop in last_hops.iter() {
648647
let have_hop_src_in_graph =
649-
if let Some(&(ref first_hop, ref features)) = first_hop_targets.get(&hop.src_node_id) {
648+
if let Some(&(ref first_hop, ref features, ref outbound_capacity_msat)) = first_hop_targets.get(&hop.src_node_id) {
650649
// If this hop connects to a node with which we have a direct channel, ignore
651650
// the network graph and add both the hop and our direct channel to
652651
// the candidate set.
@@ -655,7 +654,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
655654
// bit lazy here. In the future, we should pull them out via our
656655
// ChannelManager, but there's no reason to waste the space until we
657656
// need them.
658-
add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, None::<u64>, features.to_context(), 0, recommended_value_msat);
657+
add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, Some(outbound_capacity_msat / 1000), features.to_context(), 0, recommended_value_msat);
659658
true
660659
} else {
661660
// In any other case, only add the hop if the source is in the regular network
@@ -701,7 +700,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
701700
let mut ordered_hops = vec!(new_entry.clone());
702701

703702
'path_walk: loop {
704-
if let Some(&(_, ref features)) = first_hop_targets.get(&ordered_hops.last().unwrap().route_hop.pubkey) {
703+
if let Some(&(_, ref features, _)) = first_hop_targets.get(&ordered_hops.last().unwrap().route_hop.pubkey) {
705704
ordered_hops.last_mut().unwrap().route_hop.node_features = features.to_context();
706705
} else if let Some(node) = network.get_nodes().get(&ordered_hops.last().unwrap().route_hop.pubkey) {
707706
if let Some(node_info) = node.announcement_info.as_ref() {
@@ -1571,7 +1570,7 @@ mod tests {
15711570
counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
15721571
channel_value_satoshis: 0,
15731572
user_id: 0,
1574-
outbound_capacity_msat: 0,
1573+
outbound_capacity_msat: 250_000_000,
15751574
inbound_capacity_msat: 0,
15761575
is_live: true,
15771576
}];
@@ -1618,7 +1617,7 @@ mod tests {
16181617
counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
16191618
channel_value_satoshis: 0,
16201619
user_id: 0,
1621-
outbound_capacity_msat: 0,
1620+
outbound_capacity_msat: 250_000_000,
16221621
inbound_capacity_msat: 0,
16231622
is_live: true,
16241623
}];
@@ -1682,7 +1681,7 @@ mod tests {
16821681
counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
16831682
channel_value_satoshis: 0,
16841683
user_id: 0,
1685-
outbound_capacity_msat: 0,
1684+
outbound_capacity_msat: 250_000_000,
16861685
inbound_capacity_msat: 0,
16871686
is_live: true,
16881687
}];
@@ -1818,7 +1817,7 @@ mod tests {
18181817
counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
18191818
channel_value_satoshis: 0,
18201819
user_id: 0,
1821-
outbound_capacity_msat: 0,
1820+
outbound_capacity_msat: 250_000_000,
18221821
inbound_capacity_msat: 0,
18231822
is_live: true,
18241823
}];
@@ -2052,6 +2051,66 @@ mod tests {
20522051
assert_eq!(path.last().unwrap().fee_msat, 250_000_000);
20532052
}
20542053

2054+
// Check that setting outbound_capacity_msat in first_hops limits the channels.
2055+
// Disable channel #1 and use another first hop.
2056+
update_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
2057+
chain_hash: genesis_block(Network::Testnet).header.block_hash(),
2058+
short_channel_id: 1,
2059+
timestamp: 3,
2060+
flags: 2,
2061+
cltv_expiry_delta: 0,
2062+
htlc_minimum_msat: 0,
2063+
htlc_maximum_msat: OptionalField::Present(1_000_000_000),
2064+
fee_base_msat: 0,
2065+
fee_proportional_millionths: 0,
2066+
excess_data: Vec::new()
2067+
});
2068+
2069+
// Now, limit the first_hop by the outbound_capacity_msat of 200_000 sats.
2070+
let our_chans = vec![channelmanager::ChannelDetails {
2071+
channel_id: [0; 32],
2072+
short_channel_id: Some(42),
2073+
remote_network_id: nodes[0].clone(),
2074+
counterparty_features: InitFeatures::from_le_bytes(vec![0b11]),
2075+
channel_value_satoshis: 0,
2076+
user_id: 0,
2077+
outbound_capacity_msat: 200_000_000,
2078+
inbound_capacity_msat: 0,
2079+
is_live: true,
2080+
}];
2081+
2082+
{
2083+
// Attempt to route more than available results in a failure.
2084+
if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], Some(&our_chans.iter().collect::<Vec<_>>()), &Vec::new(), 200_000_001, 42, Arc::clone(&logger)) {
2085+
assert_eq!(err, "Failed to find a sufficient route to the given destination");
2086+
} else { panic!(); }
2087+
}
2088+
2089+
{
2090+
// Now, attempt to route an exact amount we have should be fine.
2091+
let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], Some(&our_chans.iter().collect::<Vec<_>>()), &Vec::new(), 200_000_000, 42, Arc::clone(&logger)).unwrap();
2092+
assert_eq!(route.paths.len(), 1);
2093+
let path = route.paths.last().unwrap();
2094+
assert_eq!(path.len(), 2);
2095+
assert_eq!(path.last().unwrap().pubkey, nodes[2]);
2096+
assert_eq!(path.last().unwrap().fee_msat, 200_000_000);
2097+
}
2098+
2099+
// Enable channel #1 back.
2100+
update_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
2101+
chain_hash: genesis_block(Network::Testnet).header.block_hash(),
2102+
short_channel_id: 1,
2103+
timestamp: 4,
2104+
flags: 0,
2105+
cltv_expiry_delta: 0,
2106+
htlc_minimum_msat: 0,
2107+
htlc_maximum_msat: OptionalField::Present(1_000_000_000),
2108+
fee_base_msat: 0,
2109+
fee_proportional_millionths: 0,
2110+
excess_data: Vec::new()
2111+
});
2112+
2113+
20552114
// Now let's see if routing works if we know only htlc_maximum_msat.
20562115
update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[0], UnsignedChannelUpdate {
20572116
chain_hash: genesis_block(Network::Testnet).header.block_hash(),

0 commit comments

Comments
 (0)