Skip to content

Commit 1eb4cf4

Browse files
committed
Allow nodes to be avoided during pathfinding
Users may want to - for whatever reasons - prevent payments to be routed over certain nodes. This change therefore allows to add `NodeId`s to a list of banned nodes, which then will be avoided during path finding.
1 parent 3676a05 commit 1eb4cf4

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

lightning/src/routing/scoring.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ where L::Target: Logger {
300300
logger: L,
301301
// TODO: Remove entries of closed channels.
302302
channel_liquidities: HashMap<u64, ChannelLiquidity<T>>,
303+
banned_nodes: HashSet<NodeId>,
303304
}
304305

305306
/// Parameters for configuring [`ProbabilisticScorer`].
@@ -399,6 +400,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerU
399400
network_graph,
400401
logger,
401402
channel_liquidities: HashMap::new(),
403+
banned_nodes: HashSet::new(),
402404
}
403405
}
404406

@@ -408,6 +410,22 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> ProbabilisticScorerU
408410
self
409411
}
410412

413+
/// Marks the node with the given `node_id` as banned, i.e.,
414+
/// it will be avoided during path finding.
415+
pub fn add_banned(&mut self, node_id: &NodeId) {
416+
self.banned_nodes.insert(*node_id);
417+
}
418+
419+
/// Removes the node with the given `node_id` from the list of nodes to avoid.
420+
pub fn remove_banned(&mut self, node_id: &NodeId) {
421+
self.banned_nodes.remove(node_id);
422+
}
423+
424+
/// Clears the list of nodes that are avoided during path finding.
425+
pub fn clear_banned(&mut self) {
426+
self.banned_nodes = HashSet::new();
427+
}
428+
411429
/// Dump the contents of this scorer into the configured logger.
412430
///
413431
/// Note that this writes roughly one line per channel for which we have a liquidity estimate,
@@ -672,6 +690,10 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Score for Probabilis
672690
fn channel_penalty_msat(
673691
&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage
674692
) -> u64 {
693+
if self.banned_nodes.contains(source) || self.banned_nodes.contains(target) {
694+
return u64::max_value();
695+
}
696+
675697
if let EffectiveCapacity::ExactLiquidity { liquidity_msat } = usage.effective_capacity {
676698
if usage.amount_msat > liquidity_msat {
677699
return u64::max_value();
@@ -1072,7 +1094,7 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Writeable for Probab
10721094
#[inline]
10731095
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
10741096
write_tlv_fields!(w, {
1075-
(0, self.channel_liquidities, required)
1097+
(0, self.channel_liquidities, required),
10761098
});
10771099
Ok(())
10781100
}
@@ -1087,13 +1109,14 @@ ReadableArgs<(ProbabilisticScoringParameters, G, L)> for ProbabilisticScorerUsin
10871109
let (params, network_graph, logger) = args;
10881110
let mut channel_liquidities = HashMap::new();
10891111
read_tlv_fields!(r, {
1090-
(0, channel_liquidities, required)
1112+
(0, channel_liquidities, required),
10911113
});
10921114
Ok(Self {
10931115
params,
10941116
network_graph,
10951117
logger,
10961118
channel_liquidities,
1119+
banned_nodes: HashSet::new(),
10971120
})
10981121
}
10991122
}

0 commit comments

Comments
 (0)