|info| info.features.supports_basic_mpp()))
} else { false };
- log_trace!(logger, "Searching for a route from payer {} to {} {} MPP and {} first hops {}overriding the network graph", our_node_pubkey,
- LoggedPayeePubkey(payment_params.payee.node_id()), if allow_mpp { "with" } else { "without" },
- first_hops.map(|hops| hops.len()).unwrap_or(0), if first_hops.is_some() { "" } else { "not " });
+ let max_total_routing_fee_msat = route_params.max_total_routing_fee_msat.unwrap_or(u64::max_value());
+
+ log_trace!(logger, "Searching for a route from payer {} to {} {} MPP and {} first hops {}overriding the network graph with a fee limit of {} msat",
+ our_node_pubkey, LoggedPayeePubkey(payment_params.payee.node_id()),
+ if allow_mpp { "with" } else { "without" },
+ first_hops.map(|hops| hops.len()).unwrap_or(0), if first_hops.is_some() { "" } else { "not " },
+ max_total_routing_fee_msat);
// Step (1).
// Prepare the data we'll use for payee-to-payer search by
LoggedPayeePubkey(payment_params.payee.node_id()), our_node_pubkey, final_value_msat);
// Remember how many candidates we ignored to allow for some logging afterwards.
- let mut num_ignored_value_contribution = 0;
- let mut num_ignored_path_length_limit = 0;
- let mut num_ignored_cltv_delta_limit = 0;
- let mut num_ignored_previously_failed = 0;
- let mut num_ignored_total_fee_limit = 0;
+ let mut num_ignored_value_contribution: u32 = 0;
+ let mut num_ignored_path_length_limit: u32 = 0;
+ let mut num_ignored_cltv_delta_limit: u32 = 0;
+ let mut num_ignored_previously_failed: u32 = 0;
+ let mut num_ignored_total_fee_limit: u32 = 0;
+ let mut num_ignored_avoid_overpayment: u32 = 0;
+ let mut num_ignored_htlc_minimum_msat_limit: u32 = 0;
macro_rules! add_entry {
// Adds entry which goes from $src_node_id to $dest_node_id over the $candidate hop.
#[allow(unused_comparisons)] // $next_hops_path_htlc_minimum_msat is 0 in some calls so rustc complains
let may_overpay_to_meet_path_minimum_msat =
((amount_to_transfer_over_msat < $candidate.htlc_minimum_msat() &&
- recommended_value_msat > $candidate.htlc_minimum_msat()) ||
+ recommended_value_msat >= $candidate.htlc_minimum_msat()) ||
(amount_to_transfer_over_msat < $next_hops_path_htlc_minimum_msat &&
- recommended_value_msat > $next_hops_path_htlc_minimum_msat));
+ recommended_value_msat >= $next_hops_path_htlc_minimum_msat));
let payment_failed_on_this_channel = scid_opt.map_or(false,
|scid| payment_params.previously_failed_channels.contains(&scid));
}
num_ignored_previously_failed += 1;
} else if may_overpay_to_meet_path_minimum_msat {
+ if should_log_candidate {
+ log_trace!(logger,
+ "Ignoring {} to avoid overpaying to meet htlc_minimum_msat limit.",
+ LoggedCandidateHop(&$candidate));
+ }
+ num_ignored_avoid_overpayment += 1;
hit_minimum_limit = true;
} else if over_path_minimum_msat {
// Note that low contribution here (limited by available_liquidity_msat)
}
// Ignore hops if augmenting the current path to them would put us over `max_total_routing_fee_msat`
- let max_total_routing_fee_msat = route_params.max_total_routing_fee_msat.unwrap_or(u64::max_value());
if total_fee_msat > max_total_routing_fee_msat {
if should_log_candidate {
log_trace!(logger, "Ignoring {} due to exceeding max total routing fee limit.", LoggedCandidateHop(&$candidate));
}
}
}
+ } else {
+ if should_log_candidate {
+ log_trace!(logger,
+ "Ignoring {} due to its htlc_minimum_msat limit.",
+ LoggedCandidateHop(&$candidate));
+ }
+ num_ignored_htlc_minimum_msat_limit += 1;
}
}
}
for route in payment_params.payee.unblinded_route_hints().iter()
.filter(|route| !route.0.is_empty())
{
- let first_hop_in_route = &(route.0)[0];
- let have_hop_src_in_graph =
- // Only add the hops in this route to our candidate set if either
- // we have a direct channel to the first hop or the first hop is
- // in the regular network graph.
- first_hop_targets.get(&NodeId::from_pubkey(&first_hop_in_route.src_node_id)).is_some() ||
- network_nodes.get(&NodeId::from_pubkey(&first_hop_in_route.src_node_id)).is_some();
- if have_hop_src_in_graph {
+ let first_hop_src_id = NodeId::from_pubkey(&route.0.first().unwrap().src_node_id);
+ let first_hop_src_is_reachable =
+ // Only add the hops in this route to our candidate set if either we are part of
+ // the first hop, we have a direct channel to the first hop, or the first hop is in
+ // the regular network graph.
+ our_node_id == first_hop_src_id ||
+ first_hop_targets.get(&first_hop_src_id).is_some() ||
+ network_nodes.get(&first_hop_src_id).is_some();
+ if first_hop_src_is_reachable {
// We start building the path from reverse, i.e., from payee
// to the first RouteHintHop in the path.
let hop_iter = route.0.iter().rev();
for (idx, (hop, prev_hop_id)) in hop_iter.zip(prev_hop_iter).enumerate() {
let source = NodeId::from_pubkey(&hop.src_node_id);
let target = NodeId::from_pubkey(&prev_hop_id);
+
+ if let Some(first_channels) = first_hop_targets.get(&target) {
+ if first_channels.iter().any(|d| d.outbound_scid_alias == Some(hop.short_channel_id)) {
+ log_trace!(logger, "Ignoring route hint with SCID {} (and any previous) due to it being a direct channel of ours.",
+ hop.short_channel_id);
+ break;
+ }
+ }
+
let candidate = network_channels
.get(&hop.short_channel_id)
.and_then(|channel| channel.as_directed_to(&target))
.saturating_add(1);
// Searching for a direct channel between last checked hop and first_hop_targets
- if let Some(first_channels) = first_hop_targets.get_mut(&NodeId::from_pubkey(&prev_hop_id)) {
+ if let Some(first_channels) = first_hop_targets.get_mut(&target) {
sort_first_hop_channels(first_channels, &used_liquidities,
recommended_value_msat, our_node_pubkey);
for details in first_channels {
let first_hop_candidate = CandidateRouteHop::FirstHop { details };
- add_entry!(first_hop_candidate, our_node_id, NodeId::from_pubkey(&prev_hop_id),
+ add_entry!(first_hop_candidate, our_node_id, target,
aggregate_next_hops_fee_msat, aggregate_path_contribution_msat,
aggregate_next_hops_path_htlc_minimum_msat, aggregate_next_hops_path_penalty_msat,
aggregate_next_hops_cltv_delta, aggregate_next_hops_path_length);
// because we deterministically terminated the search due to low liquidity.
if !found_new_path && channel_saturation_pow_half != 0 {
channel_saturation_pow_half = 0;
+ } else if !found_new_path && hit_minimum_limit && already_collected_value_msat < final_value_msat && path_value_msat != recommended_value_msat {
+ log_trace!(logger, "Failed to collect enough value, but running again to collect extra paths with a potentially higher limit.");
+ path_value_msat = recommended_value_msat;
} else if already_collected_value_msat >= recommended_value_msat || !found_new_path {
log_trace!(logger, "Have now collected {} msat (seeking {} msat) in paths. Last path loop {} a new path.",
already_collected_value_msat, recommended_value_msat, if found_new_path { "found" } else { "did not find" });
log_trace!(logger, "Collected exactly our payment amount on the first pass, without hitting an htlc_minimum_msat limit, exiting.");
break 'paths_collection;
}
- log_trace!(logger, "Collected our payment amount on the first pass, but running again to collect extra paths with a potentially higher limit.");
+ log_trace!(logger, "Collected our payment amount on the first pass, but running again to collect extra paths with a potentially higher value to meet htlc_minimum_msat limit.");
path_value_msat = recommended_value_msat;
}
}
let num_ignored_total = num_ignored_value_contribution + num_ignored_path_length_limit +
- num_ignored_cltv_delta_limit + num_ignored_previously_failed + num_ignored_total_fee_limit;
+ num_ignored_cltv_delta_limit + num_ignored_previously_failed +
+ num_ignored_avoid_overpayment + num_ignored_htlc_minimum_msat_limit +
+ num_ignored_total_fee_limit;
if num_ignored_total > 0 {
- log_trace!(logger, "Ignored {} candidate hops due to insufficient value contribution, {} due to path length limit, {} due to CLTV delta limit, {} due to previous payment failure, {} due to maximum total fee limit. Total: {} ignored candidates.", num_ignored_value_contribution, num_ignored_path_length_limit, num_ignored_cltv_delta_limit, num_ignored_previously_failed, num_ignored_total_fee_limit, num_ignored_total);
+ log_trace!(logger,
+ "Ignored {} candidate hops due to insufficient value contribution, {} due to path length limit, {} due to CLTV delta limit, {} due to previous payment failure, {} due to htlc_minimum_msat limit, {} to avoid overpaying, {} due to maximum total fee limit. Total: {} ignored candidates.",
+ num_ignored_value_contribution, num_ignored_path_length_limit,
+ num_ignored_cltv_delta_limit, num_ignored_previously_failed,
+ num_ignored_htlc_minimum_msat_limit, num_ignored_avoid_overpayment,
+ num_ignored_total_fee_limit, num_ignored_total);
}
// Step (5).
// Make sure we would never create a route with more paths than we allow.
debug_assert!(paths.len() <= payment_params.max_path_count.into());
- // Make sure we would never create a route whose total fees exceed max_total_routing_fee_msat.
- if let Some(max_total_routing_fee_msat) = route_params.max_total_routing_fee_msat {
- if paths.iter().map(|p| p.fee_msat()).sum::<u64>() > max_total_routing_fee_msat {
- return Err(LightningError{err: format!("Failed to find route that adheres to the maximum total fee limit of {}msat",
- max_total_routing_fee_msat), action: ErrorAction::IgnoreError});
- }
- }
-
if let Some(node_features) = payment_params.payee.node_features() {
for path in paths.iter_mut() {
path.hops.last_mut().unwrap().node_features = node_features.clone();
}
let route = Route { paths, route_params: Some(route_params.clone()) };
+
+ // Make sure we would never create a route whose total fees exceed max_total_routing_fee_msat.
+ if let Some(max_total_routing_fee_msat) = route_params.max_total_routing_fee_msat {
+ if route.get_total_fees() > max_total_routing_fee_msat {
+ return Err(LightningError{err: format!("Failed to find route that adheres to the maximum total fee limit of {}msat",
+ max_total_routing_fee_msat), action: ErrorAction::IgnoreError});
+ }
+ }
+
log_info!(logger, "Got route: {}", log_route!(route));
Ok(route)
}
assert_eq!(fees, 5_000);
}
+ #[test]
+ fn htlc_minimum_recipient_overpay_test() {
+ let (secp_ctx, network_graph, gossip_sync, _, logger) = build_graph();
+ let (_, our_id, privkeys, nodes) = get_nodes(&secp_ctx);
+ let config = UserConfig::default();
+ let payment_params = PaymentParameters::from_node_id(nodes[2], 42).with_bolt11_features(channelmanager::provided_invoice_features(&config)).unwrap();
+ let scorer = ln_test_utils::TestScorer::new();
+ let keys_manager = ln_test_utils::TestKeysInterface::new(&[0u8; 32], Network::Testnet);
+ let random_seed_bytes = keys_manager.get_secure_random_bytes();
+
+ // Route to node2 over a single path which requires overpaying the recipient themselves.
+
+ // First disable all paths except the us -> node1 -> node2 path
+ update_channel(&gossip_sync, &secp_ctx, &privkeys[2], UnsignedChannelUpdate {
+ chain_hash: genesis_block(Network::Testnet).header.block_hash(),
+ short_channel_id: 13,
+ timestamp: 2,
+ flags: 3,
+ cltv_expiry_delta: 0,
+ htlc_minimum_msat: 0,
+ htlc_maximum_msat: 0,
+ fee_base_msat: 0,
+ fee_proportional_millionths: 0,
+ excess_data: Vec::new()
+ });
+
+ // Set channel 4 to free but with a high htlc_minimum_msat
+ update_channel(&gossip_sync, &secp_ctx, &privkeys[1], UnsignedChannelUpdate {
+ chain_hash: genesis_block(Network::Testnet).header.block_hash(),
+ short_channel_id: 4,
+ timestamp: 2,
+ flags: 0,
+ cltv_expiry_delta: 0,
+ htlc_minimum_msat: 15_000,
+ htlc_maximum_msat: MAX_VALUE_MSAT,
+ fee_base_msat: 0,
+ fee_proportional_millionths: 0,
+ excess_data: Vec::new()
+ });
+
+ // Now check that we'll fail to find a path if we fail to find a path if the htlc_minimum
+ // is overrun. Note that the fees are actually calculated on 3*payment amount as that's
+ // what we try to find a route for, so this test only just happens to work out to exactly
+ // the fee limit.
+ let mut route_params = RouteParameters::from_payment_params_and_value(
+ payment_params.clone(), 5_000);
+ route_params.max_total_routing_fee_msat = Some(9_999);
+ if let Err(LightningError{err, action: ErrorAction::IgnoreError}) = get_route(&our_id,
+ &route_params, &network_graph.read_only(), None, Arc::clone(&logger), &scorer,
+ &Default::default(), &random_seed_bytes) {
+ assert_eq!(err, "Failed to find route that adheres to the maximum total fee limit of 9999msat");
+ } else { panic!(); }
+
+ let mut route_params = RouteParameters::from_payment_params_and_value(
+ payment_params.clone(), 5_000);
+ route_params.max_total_routing_fee_msat = Some(10_000);
+ let route = get_route(&our_id, &route_params, &network_graph.read_only(), None,
+ Arc::clone(&logger), &scorer, &Default::default(), &random_seed_bytes).unwrap();
+ assert_eq!(route.get_total_fees(), 10_000);
+ }
+
#[test]
fn disable_channels_test() {
let (secp_ctx, network_graph, gossip_sync, _, logger) = build_graph();