Skip to content

Commit c024ea6

Browse files
committed
[routing] Track full-path htlc-minimum-msat while graph-walking
Previously, we'd happily send funds through a path where, while generating the path, in some middle hope we reduce the value being sent to meet an htlc_maximum, making a later hop invalid due to it no longer meeting its htlc_minimum. Instead, we need to track the path's htlc-minimum while we're transiting the graph.
1 parent f317215 commit c024ea6

File tree

1 file changed

+87
-15
lines changed

1 file changed

+87
-15
lines changed

lightning/src/routing/router.rs

Lines changed: 87 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ struct RouteGraphNode {
143143
// - how much is needed for a path being constructed
144144
// - how much value can channels following this node (up to the destination) can contribute,
145145
// considering their capacity and fees
146-
value_contribution_msat: u64
146+
value_contribution_msat: u64,
147+
/// The maximum htlc_minimum_msat along the path, taking into consideration the fees required
148+
/// to meet the minimum over the hops required to get there.
149+
path_htlc_minimum_msat: u64,
147150
}
148151

149152
impl cmp::Ord for RouteGraphNode {
@@ -473,7 +476,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
473476
// $next_hops_fee_msat represents the fees paid for using all the channel *after* this one,
474477
// since that value has to be transferred over this channel.
475478
( $chan_id: expr, $src_node_id: expr, $dest_node_id: expr, $directional_info: expr, $capacity_sats: expr, $chan_features: expr, $next_hops_fee_msat: expr,
476-
$next_hops_value_contribution: expr ) => {
479+
$next_hops_value_contribution: expr, $incl_fee_next_hops_htlc_minimum_msat: expr ) => {
477480
// Channels to self should not be used. This is more of belt-and-suspenders, because in
478481
// practice these cases should be caught earlier:
479482
// - for regular channels at channel announcement (TODO)
@@ -535,19 +538,27 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
535538
// Can't overflow due to how the values were computed right above.
536539
None => unreachable!(),
537540
};
541+
#[allow(unused_comparisons)] // $incl_fee_next_hops_htlc_minimum_msat is 0 in some calls so rustc complains
542+
let over_path_minimum_msat = amount_to_transfer_over_msat >= $directional_info.htlc_minimum_msat &&
543+
amount_to_transfer_over_msat >= $incl_fee_next_hops_htlc_minimum_msat;
538544

539545
// If HTLC minimum is larger than the amount we're going to transfer, we shouldn't
540546
// bother considering this channel.
541547
// Since we're choosing amount_to_transfer_over_msat as maximum possible, it can
542548
// be only reduced later (not increased), so this channel should just be skipped
543549
// as not sufficient.
544-
if amount_to_transfer_over_msat < $directional_info.htlc_minimum_msat {
550+
if !over_path_minimum_msat {
545551
hit_minimum_limit = true;
546552
} else if contributes_sufficient_value {
547553
// Note that low contribution here (limited by available_liquidity_msat)
548554
// might violate htlc_minimum_msat on the hops which are next along the
549555
// payment path (upstream to the payee). To avoid that, we recompute path
550556
// path fees knowing the final path contribution after constructing it.
557+
let path_htlc_minimum_msat = match compute_fees($incl_fee_next_hops_htlc_minimum_msat, $directional_info.fees)
558+
.map(|fee_msat| fee_msat.checked_add($incl_fee_next_hops_htlc_minimum_msat)) {
559+
Some(Some(value_msat)) => cmp::max(value_msat, $directional_info.htlc_minimum_msat),
560+
_ => u64::max_value()
561+
};
551562
let hm_entry = dist.entry(&$src_node_id);
552563
let old_entry = hm_entry.or_insert_with(|| {
553564
// If there was previously no known way to access
@@ -619,6 +630,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
619630
lowest_fee_to_peer_through_node: total_fee_msat,
620631
lowest_fee_to_node: $next_hops_fee_msat as u64 + hop_use_fee_msat,
621632
value_contribution_msat: value_contribution_msat,
633+
path_htlc_minimum_msat,
622634
};
623635

624636
// Update the way of reaching $src_node_id with the given $chan_id (from $dest_node_id),
@@ -678,10 +690,10 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
678690
// meaning how much will be paid in fees after this node (to the best of our knowledge).
679691
// This data can later be helpful to optimize routing (pay lower fees).
680692
macro_rules! add_entries_to_cheapest_to_target_node {
681-
( $node: expr, $node_id: expr, $fee_to_target_msat: expr, $next_hops_value_contribution: expr ) => {
693+
( $node: expr, $node_id: expr, $fee_to_target_msat: expr, $next_hops_value_contribution: expr, $incl_fee_next_hops_htlc_minimum_msat: expr ) => {
682694
if first_hops.is_some() {
683695
if let Some(&(ref first_hop, ref features, ref outbound_capacity_msat)) = first_hop_targets.get(&$node_id) {
684-
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);
696+
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, $incl_fee_next_hops_htlc_minimum_msat);
685697
}
686698
}
687699

@@ -701,15 +713,15 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
701713
if first_hops.is_none() || chan.node_two != *our_node_id {
702714
if let Some(two_to_one) = chan.two_to_one.as_ref() {
703715
if two_to_one.enabled {
704-
add_entry!(chan_id, chan.node_two, chan.node_one, two_to_one, chan.capacity_sats, chan.features, $fee_to_target_msat, $next_hops_value_contribution);
716+
add_entry!(chan_id, chan.node_two, chan.node_one, two_to_one, chan.capacity_sats, chan.features, $fee_to_target_msat, $next_hops_value_contribution, $incl_fee_next_hops_htlc_minimum_msat);
705717
}
706718
}
707719
}
708720
} else {
709721
if first_hops.is_none() || chan.node_one != *our_node_id {
710722
if let Some(one_to_two) = chan.one_to_two.as_ref() {
711723
if one_to_two.enabled {
712-
add_entry!(chan_id, chan.node_one, chan.node_two, one_to_two, chan.capacity_sats, chan.features, $fee_to_target_msat, $next_hops_value_contribution);
724+
add_entry!(chan_id, chan.node_one, chan.node_two, one_to_two, chan.capacity_sats, chan.features, $fee_to_target_msat, $next_hops_value_contribution, $incl_fee_next_hops_htlc_minimum_msat);
713725
}
714726
}
715727

@@ -736,7 +748,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
736748
// place where it could be added.
737749
if first_hops.is_some() {
738750
if let Some(&(ref first_hop, ref features, ref outbound_capacity_msat)) = first_hop_targets.get(&payee) {
739-
add_entry!(first_hop, *our_node_id, payee, dummy_directional_info, Some(outbound_capacity_msat / 1000), features.to_context(), 0, path_value_msat);
751+
add_entry!(first_hop, *our_node_id, payee, dummy_directional_info, Some(outbound_capacity_msat / 1000), features.to_context(), 0, path_value_msat, 0);
740752
}
741753
}
742754

@@ -749,7 +761,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
749761
// If not, targets.pop() will not even let us enter the loop in step 2.
750762
None => {},
751763
Some(node) => {
752-
add_entries_to_cheapest_to_target_node!(node, payee, 0, path_value_msat);
764+
add_entries_to_cheapest_to_target_node!(node, payee, 0, path_value_msat, 0);
753765
},
754766
}
755767

@@ -768,7 +780,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
768780
// bit lazy here. In the future, we should pull them out via our
769781
// ChannelManager, but there's no reason to waste the space until we
770782
// need them.
771-
add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, Some(outbound_capacity_msat / 1000), features.to_context(), 0, path_value_msat);
783+
add_entry!(first_hop, *our_node_id , hop.src_node_id, dummy_directional_info, Some(outbound_capacity_msat / 1000), features.to_context(), 0, path_value_msat, 0);
772784
true
773785
} else {
774786
// In any other case, only add the hop if the source is in the regular network
@@ -778,17 +790,17 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
778790
if have_hop_src_in_graph {
779791
// BOLT 11 doesn't allow inclusion of features for the last hop hints, which
780792
// really sucks, cause we're gonna need that eventually.
781-
let last_hop_htlc_minimum_msat: u64 = match hop.htlc_minimum_msat {
793+
let last_path_htlc_minimum_msat: u64 = match hop.htlc_minimum_msat {
782794
Some(htlc_minimum_msat) => htlc_minimum_msat,
783795
None => 0
784796
};
785797
let directional_info = DummyDirectionalChannelInfo {
786798
cltv_expiry_delta: hop.cltv_expiry_delta as u32,
787-
htlc_minimum_msat: last_hop_htlc_minimum_msat,
799+
htlc_minimum_msat: last_path_htlc_minimum_msat,
788800
htlc_maximum_msat: hop.htlc_maximum_msat,
789801
fees: hop.fees,
790802
};
791-
add_entry!(hop.short_channel_id, hop.src_node_id, payee, directional_info, None::<u64>, ChannelFeatures::empty(), 0, path_value_msat);
803+
add_entry!(hop.short_channel_id, hop.src_node_id, payee, directional_info, None::<u64>, ChannelFeatures::empty(), 0, path_value_msat, 0);
792804
}
793805
}
794806

@@ -805,7 +817,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
805817
// Both these cases (and other cases except reaching recommended_value_msat) mean that
806818
// paths_collection will be stopped because found_new_path==false.
807819
// This is not necessarily a routing failure.
808-
'path_construction: while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, value_contribution_msat, .. }) = targets.pop() {
820+
'path_construction: while let Some(RouteGraphNode { pubkey, lowest_fee_to_node, value_contribution_msat, path_htlc_minimum_msat, .. }) = targets.pop() {
809821

810822
// Since we're going payee-to-payer, hitting our node as a target means we should stop
811823
// traversing the graph and arrange the path out of what we found.
@@ -902,7 +914,7 @@ pub fn get_route<L: Deref>(our_node_id: &PublicKey, network: &NetworkGraph, paye
902914
match network.get_nodes().get(&pubkey) {
903915
None => {},
904916
Some(node) => {
905-
add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node, value_contribution_msat);
917+
add_entries_to_cheapest_to_target_node!(node, &pubkey, lowest_fee_to_node, value_contribution_msat, path_htlc_minimum_msat);
906918
},
907919
}
908920
}
@@ -3459,6 +3471,66 @@ mod tests {
34593471
assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
34603472
}
34613473
}
3474+
3475+
#[test]
3476+
fn htlc_max_reduction_below_min() {
3477+
// Test that if, while walking the graph, we reduce the value being sent to meet an
3478+
// htlc_maximum_msat, we don't end up undershooting a later htlc_minimum_msat. In the
3479+
// initial version of MPP we'd accept such routes but reject them while recalculating fees,
3480+
// resulting in us thinking there is no possible path, even if other paths exist.
3481+
let (secp_ctx, net_graph_msg_handler, _, logger) = build_graph();
3482+
let (our_privkey, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
3483+
3484+
// We modify the graph to set the htlc_minimum of channel 2 and 4 as needed - channel 2
3485+
// gets an htlc_minimum_msat of 80_000 and channel 4 an htlc_maximum_msat of 90_000. We
3486+
// then try to send 90_000.
3487+
update_channel(&net_graph_msg_handler, &secp_ctx, &our_privkey, UnsignedChannelUpdate {
3488+
chain_hash: genesis_block(Network::Testnet).header.block_hash(),
3489+
short_channel_id: 2,
3490+
timestamp: 2,
3491+
flags: 0,
3492+
cltv_expiry_delta: 0,
3493+
htlc_minimum_msat: 0,
3494+
htlc_maximum_msat: OptionalField::Present(80_000),
3495+
fee_base_msat: 0,
3496+
fee_proportional_millionths: 0,
3497+
excess_data: Vec::new()
3498+
});
3499+
update_channel(&net_graph_msg_handler, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
3500+
chain_hash: genesis_block(Network::Testnet).header.block_hash(),
3501+
short_channel_id: 4,
3502+
timestamp: 2,
3503+
flags: 0,
3504+
cltv_expiry_delta: (4 << 8) | 1,
3505+
htlc_minimum_msat: 90_000,
3506+
htlc_maximum_msat: OptionalField::Absent,
3507+
fee_base_msat: 0,
3508+
fee_proportional_millionths: 0,
3509+
excess_data: Vec::new()
3510+
});
3511+
3512+
{
3513+
// Now, attempt to route 125 sats (just a bit below the capacity of 3 channels).
3514+
// Our algorithm should provide us with these 3 paths.
3515+
let route = get_route(&our_id, &net_graph_msg_handler.network_graph.read().unwrap(), &nodes[2], None, &Vec::new(), 90_000, 42, Arc::clone(&logger)).unwrap();
3516+
assert_eq!(route.paths.len(), 1);
3517+
assert_eq!(route.paths[0].len(), 2);
3518+
3519+
assert_eq!(route.paths[0][0].pubkey, nodes[7]);
3520+
assert_eq!(route.paths[0][0].short_channel_id, 12);
3521+
assert_eq!(route.paths[0][0].fee_msat, 90_000*2);
3522+
assert_eq!(route.paths[0][0].cltv_expiry_delta, (13 << 8) | 1);
3523+
assert_eq!(route.paths[0][0].node_features.le_flags(), &id_to_feature_flags(8));
3524+
assert_eq!(route.paths[0][0].channel_features.le_flags(), &id_to_feature_flags(12));
3525+
3526+
assert_eq!(route.paths[0][1].pubkey, nodes[2]);
3527+
assert_eq!(route.paths[0][1].short_channel_id, 13);
3528+
assert_eq!(route.paths[0][1].fee_msat, 90_000);
3529+
assert_eq!(route.paths[0][1].cltv_expiry_delta, 42);
3530+
assert_eq!(route.paths[0][1].node_features.le_flags(), &id_to_feature_flags(3));
3531+
assert_eq!(route.paths[0][1].channel_features.le_flags(), &id_to_feature_flags(13));
3532+
}
3533+
}
34623534
}
34633535

34643536
#[cfg(all(test, feature = "unstable"))]

0 commit comments

Comments
 (0)