Skip to content

Commit ad2e92a

Browse files
authored
Merge pull request #1592 from tnull/2022-07-manual-penalty
Allow to set manual node penalties
2 parents 4e5f74a + 1ddc6f1 commit ad2e92a

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
@@ -1882,7 +1882,7 @@ fn build_route_from_hops_internal<L: Deref>(
18821882

18831883
#[cfg(test)]
18841884
mod tests {
1885-
use routing::gossip::{NetworkGraph, P2PGossipSync, NodeId};
1885+
use routing::gossip::{NetworkGraph, P2PGossipSync, NodeId, EffectiveCapacity};
18861886
use routing::router::{get_route, build_route_from_hops_internal, add_random_cltv_offset, default_node_features,
18871887
PaymentParameters, Route, RouteHint, RouteHintHop, RouteHop, RoutingFees,
18881888
DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA, MAX_PATH_LENGTH_ESTIMATE};
@@ -5727,7 +5727,7 @@ mod tests {
57275727
}
57285728

57295729
#[test]
5730-
fn avoids_banned_nodes() {
5730+
fn honors_manual_penalties() {
57315731
let (secp_ctx, network_graph, _, _, logger) = build_line_graph();
57325732
let (_, our_id, _, nodes) = get_nodes(&secp_ctx);
57335733

@@ -5737,7 +5737,17 @@ mod tests {
57375737
let scorer_params = ProbabilisticScoringParameters::default();
57385738
let mut scorer = ProbabilisticScorer::new(scorer_params, Arc::clone(&network_graph), Arc::clone(&logger));
57395739

5740-
// First check we can get a route.
5740+
// First check set manual penalties are returned by the scorer.
5741+
let usage = ChannelUsage {
5742+
amount_msat: 0,
5743+
inflight_htlc_msat: 0,
5744+
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000, htlc_maximum_msat: Some(1_000) },
5745+
};
5746+
scorer.set_manual_penalty(&NodeId::from_pubkey(&nodes[3]), 123);
5747+
scorer.set_manual_penalty(&NodeId::from_pubkey(&nodes[4]), 456);
5748+
assert_eq!(scorer.channel_penalty_msat(42, &NodeId::from_pubkey(&nodes[3]), &NodeId::from_pubkey(&nodes[4]), usage), 456);
5749+
5750+
// Then check we can get a normal route
57415751
let payment_params = PaymentParameters::from_node_id(nodes[10]);
57425752
let route = get_route(&our_id, &payment_params, &network_graph.read_only(), None, 100, 42, Arc::clone(&logger), &scorer, &random_seed_bytes);
57435753
assert!(route.is_ok());

lightning/src/routing/scoring.rs

+24-12
Original file line numberDiff line numberDiff line change
@@ -380,10 +380,12 @@ pub struct ProbabilisticScoringParameters {
380380
/// Default value: 256 msat
381381
pub amount_penalty_multiplier_msat: u64,
382382

383-
/// A list of nodes that won't be considered during path finding.
383+
/// Manual penalties used for the given nodes. Allows to set a particular penalty for a given
384+
/// node. Note that a manual penalty of `u64::max_value()` means the node would not ever be
385+
/// considered during path finding.
384386
///
385387
/// (C-not exported)
386-
pub banned_nodes: HashSet<NodeId>,
388+
pub manual_node_penalties: HashMap<NodeId, u64>,
387389

388390
/// This penalty is applied when `htlc_maximum_msat` is equal to or larger than half of the
389391
/// channel's capacity, which makes us prefer nodes with a smaller `htlc_maximum_msat`. We
@@ -486,17 +488,27 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerU
486488
/// Marks the node with the given `node_id` as banned, i.e.,
487489
/// it will be avoided during path finding.
488490
pub fn add_banned(&mut self, node_id: &NodeId) {
489-
self.params.banned_nodes.insert(*node_id);
491+
self.params.manual_node_penalties.insert(*node_id, u64::max_value());
490492
}
491493

492494
/// Removes the node with the given `node_id` from the list of nodes to avoid.
493495
pub fn remove_banned(&mut self, node_id: &NodeId) {
494-
self.params.banned_nodes.remove(node_id);
496+
self.params.manual_node_penalties.remove(node_id);
495497
}
496498

497-
/// Clears the list of nodes that are avoided during path finding.
498-
pub fn clear_banned(&mut self) {
499-
self.params.banned_nodes = HashSet::new();
499+
/// Sets a manual penalty for the given node.
500+
pub fn set_manual_penalty(&mut self, node_id: &NodeId, penalty: u64) {
501+
self.params.manual_node_penalties.insert(*node_id, penalty);
502+
}
503+
504+
/// Removes the node with the given `node_id` from the list of manual penalties.
505+
pub fn remove_manual_penalty(&mut self, node_id: &NodeId) {
506+
self.params.manual_node_penalties.remove(node_id);
507+
}
508+
509+
/// Clears the list of manual penalties that are applied during path finding.
510+
pub fn clear_manual_penalties(&mut self) {
511+
self.params.manual_node_penalties = HashMap::new();
500512
}
501513
}
502514

@@ -508,7 +520,7 @@ impl ProbabilisticScoringParameters {
508520
liquidity_penalty_multiplier_msat: 0,
509521
liquidity_offset_half_life: Duration::from_secs(3600),
510522
amount_penalty_multiplier_msat: 0,
511-
banned_nodes: HashSet::new(),
523+
manual_node_penalties: HashMap::new(),
512524
anti_probing_penalty_msat: 0,
513525
}
514526
}
@@ -517,7 +529,7 @@ impl ProbabilisticScoringParameters {
517529
/// they will be avoided during path finding.
518530
pub fn add_banned_from_list(&mut self, node_ids: Vec<NodeId>) {
519531
for id in node_ids {
520-
self.banned_nodes.insert(id);
532+
self.manual_node_penalties.insert(id, u64::max_value());
521533
}
522534
}
523535
}
@@ -529,7 +541,7 @@ impl Default for ProbabilisticScoringParameters {
529541
liquidity_penalty_multiplier_msat: 40_000,
530542
liquidity_offset_half_life: Duration::from_secs(3600),
531543
amount_penalty_multiplier_msat: 256,
532-
banned_nodes: HashSet::new(),
544+
manual_node_penalties: HashMap::new(),
533545
anti_probing_penalty_msat: 250,
534546
}
535547
}
@@ -731,8 +743,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Score for Probabilis
731743
fn channel_penalty_msat(
732744
&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage
733745
) -> u64 {
734-
if self.params.banned_nodes.contains(source) || self.params.banned_nodes.contains(target) {
735-
return u64::max_value();
746+
if let Some(penalty) = self.params.manual_node_penalties.get(target) {
747+
return *penalty;
736748
}
737749

738750
let mut anti_probing_penalty_msat = 0;

0 commit comments

Comments
 (0)