Skip to content

Commit cdf262f

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 abf6564 commit cdf262f

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

lightning/src/routing/scoring.rs

Lines changed: 23 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: Vec<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: Vec::new(),
402404
}
403405
}
404406

@@ -408,6 +410,17 @@ 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 set_banned(&mut self, node_id: &NodeId) {
416+
self.banned_nodes.push(*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.retain(|id| *id != *node_id);
422+
}
423+
411424
/// Dump the contents of this scorer into the configured logger.
412425
///
413426
/// Note that this writes roughly one line per channel for which we have a liquidity estimate,
@@ -655,6 +668,10 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Score for Probabilis
655668
fn channel_penalty_msat(
656669
&self, short_channel_id: u64, source: &NodeId, target: &NodeId, usage: ChannelUsage
657670
) -> u64 {
671+
if self.banned_nodes.contains(source) || self.banned_nodes.contains(target) {
672+
return u64::max_value();
673+
}
674+
658675
if let EffectiveCapacity::ExactLiquidity { liquidity_msat } = usage.effective_capacity {
659676
if usage.amount_msat > liquidity_msat {
660677
return u64::max_value();
@@ -1055,7 +1072,8 @@ impl<G: Deref<Target = NetworkGraph<L>>, L: Deref, T: Time> Writeable for Probab
10551072
#[inline]
10561073
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
10571074
write_tlv_fields!(w, {
1058-
(0, self.channel_liquidities, required)
1075+
(0, self.channel_liquidities, required),
1076+
(1, self.banned_nodes, vec_type),
10591077
});
10601078
Ok(())
10611079
}
@@ -1069,14 +1087,17 @@ ReadableArgs<(ProbabilisticScoringParameters, G, L)> for ProbabilisticScorerUsin
10691087
) -> Result<Self, DecodeError> {
10701088
let (params, network_graph, logger) = args;
10711089
let mut channel_liquidities = HashMap::new();
1090+
init_tlv_field_var!(banned_nodes, vec_type);
10721091
read_tlv_fields!(r, {
1073-
(0, channel_liquidities, required)
1092+
(0, channel_liquidities, required),
1093+
(1, banned_nodes, vec_type),
10741094
});
10751095
Ok(Self {
10761096
params,
10771097
network_graph,
10781098
logger,
10791099
channel_liquidities,
1100+
banned_nodes: init_tlv_based_struct_field!(banned_nodes, vec_type),
10801101
})
10811102
}
10821103
}

0 commit comments

Comments
 (0)