Skip to content

Commit 1ddc6f1

Browse files
committed
Allow to set manual node penalties
A user might want to explicitly penalize or prioritize a particular node. We now allow them to do so by specifying a manual penalty override for a given node that is then returned by the scorer.
1 parent f3d5b94 commit 1ddc6f1

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

lightning/src/routing/router.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,7 @@ fn build_route_from_hops_internal<L: Deref>(
18761876

18771877
#[cfg(test)]
18781878
mod tests {
1879-
use routing::gossip::{NetworkGraph, P2PGossipSync, NodeId};
1879+
use routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, EffectiveCapacity};
18801880
use routing::router::{get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
18811881
PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
18821882
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE};
@@ -5717,7 +5717,7 @@ mod tests {
57175717
}
57185718

57195719
#[test]
5720-
fn avoids_banned_nodes() {
5720+
fn honors_manual_penalties() {
57215721
let (secp_ctx, network_graph, _, _, logger) = build_line_graph();
57225722
let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
57235723

@@ -5727,7 +5727,17 @@ mod tests {
57275727
let scorer_params = ProbabilisticScoringParameters::default();
57285728
let mut scorer = ProbabilisticScorer::new(scorer_params, Arc::clone(&network_graph), Arc::clone(&logger));
57295729

5730-
// First check we can get a route.
5730+
// First check set manual penalties are returned by the scorer.
5731+
let usage = ChannelUsage {
5732+
amount_msat: 0,
5733+
inflight_htlc_msat: 0,
5734+
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000, htlc_maximum_msat: Some(1_000) },
5735+
};
5736+
scorer.set_manual_penalty(&NodeId::from_pubkey(&nodes[3]), 123);
5737+
scorer.set_manual_penalty(&NodeId::from_pubkey(&nodes[4]), 456);
5738+
assert_eq!(scorer.channel_penalty_msat(42, &NodeId::from_pubkey(&nodes[3]), &NodeId::from_pubkey(&nodes[4]), usage), 456);
5739+
5740+
// Then check we can get a normal route
57315741
let payment_params = PaymentParameters::from_node_id(nodes[10]);
57325742
let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes);
57335743
assert!(route.is_ok());

lightning/src/routing/scoring.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -362,10 +362,12 @@ pub struct ProbabilisticScoringParameters {
362362
/// Default value: 256 msat
363363
pub amount_penalty_multiplier_msat: u64,
364364

365-
/// A list of nodes that won't be considered during path finding.
365+
/// Manual penalties used for the given nodes. Allows to set a particular penalty for a given
366+
/// node. Note that a manual penalty of `u64::max_value()` means the node would not ever be
367+
/// considered during path finding.
366368
///
367369
/// (C-not exported)
368-
pub banned_nodes: HashSet<NodeId>,
370+
pub manual_node_penalties: HashMap<NodeId, u64>,
369371

370372
/// This penalty is applied when `htlc_maximum_msat` is equal to or larger than half of the
371373
/// channel's capacity, which makes us prefer nodes with a smaller `htlc_maximum_msat`. We
@@ -468,17 +470,27 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerU
468470
/// Marks the node with the given `node_id` as banned, i.e.,
469471
/// it will be avoided during path finding.
470472
pub fn add_banned(&mut self, node_id: &NodeId) {
471-
self.params.banned_nodes.insert(*node_id);
473+
self.params.manual_node_penalties.insert(*node_id, u64::max_value());
472474
}
473475

474476
/// Removes the node with the given `node_id` from the list of nodes to avoid.
475477
pub fn remove_banned(&mut self, node_id: &NodeId) {
476-
self.params.banned_nodes.remove(node_id);
478+
self.params.manual_node_penalties.remove(node_id);
477479
}
478480

479-
/// Clears the list of nodes that are avoided during path finding.
480-
pub fn clear_banned(&mut self) {
481-
self.params.banned_nodes = HashSet::new();
481+
/// Sets a manual penalty for the given node.
482+
pub fn set_manual_penalty(&mut self, node_id: &NodeId, penalty: u64) {
483+
self.params.manual_node_penalties.insert(*node_id, penalty);
484+
}
485+
486+
/// Removes the node with the given `node_id` from the list of manual penalties.
487+
pub fn remove_manual_penalty(&mut self, node_id: &NodeId) {
488+
self.params.manual_node_penalties.remove(node_id);
489+
}
490+
491+
/// Clears the list of manual penalties that are applied during path finding.
492+
pub fn clear_manual_penalties(&mut self) {
493+
self.params.manual_node_penalties = HashMap::new();
482494
}
483495
}
484496

@@ -490,7 +502,7 @@ impl ProbabilisticScoringParameters {
490502
liquidity_penalty_multiplier_msat: 0,
491503
liquidity_offset_half_life: Duration::from_secs(3600),
492504
amount_penalty_multiplier_msat: 0,
493-
banned_nodes: HashSet::new(),
505+
manual_node_penalties: HashMap::new(),
494506
anti_probing_penalty_msat: 0,
495507
}
496508
}
@@ -499,7 +511,7 @@ impl ProbabilisticScoringParameters {
499511
/// they will be avoided during path finding.
500512
pub fn add_banned_from_list(&mut self, node_ids: Vec<NodeId>) {
501513
for id in node_ids {
502-
self.banned_nodes.insert(id);
514+
self.manual_node_penalties.insert(id, u64::max_value());
503515
}
504516
}
505517
}
@@ -511,7 +523,7 @@ impl Default for ProbabilisticScoringParameters {
511523
liquidity_penalty_multiplier_msat: 40_000,
512524
liquidity_offset_half_life: Duration::from_secs(3600),
513525
amount_penalty_multiplier_msat: 256,
514-
banned_nodes: HashSet::new(),
526+
manual_node_penalties: HashMap::new(),
515527
anti_probing_penalty_msat: 250,
516528
}
517529
}
@@ -713,8 +725,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Score for Probabilis
713725
fn channel_penalty_msat(
714726
&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage
715727
) -> u64 {
716-
if self.params.banned_nodes.contains(source) || self.params.banned_nodes.contains(target) {
717-
return u64::max_value();
728+
if let Some(penalty) = self.params.manual_node_penalties.get(target) {
729+
return *penalty;
718730
}
719731

720732
let mut anti_probing_penalty_msat = 0;

0 commit comments

Comments
 (0)