Skip to content

Commit cb2e36e

Browse files
committed
Implement DynamicPenaltyScorer.
1 parent 36817e0 commit cb2e36e

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

lightning/src/routing/scoring.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,62 @@ impl ReadableArgs<u64> for FixedPenaltyScorer {
241241
}
242242
}
243243

244+
/// [`Score`] implementation that uses a dynamic penalty.
245+
pub struct DynamicPenaltyScorer<F>
246+
where
247+
F: Fn(u64, u64, u64, &NodeId, &NodeId) -> u64,
248+
{
249+
penalty_func: F,
250+
}
251+
252+
impl<F> DynamicPenaltyScorer<F>
253+
where
254+
F: Fn(u64, u64, u64, &NodeId, &NodeId) -> u64,
255+
{
256+
/// Creates a new scorer using `penalty_func`.
257+
pub fn with_penalty_func(penalty_func: F) -> Self
258+
{
259+
Self { penalty_func }
260+
}
261+
}
262+
263+
impl<F> Score for DynamicPenaltyScorer<F>
264+
where
265+
F: Fn(u64, u64, u64, &NodeId, &NodeId) -> u64,
266+
{
267+
fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64,
268+
source: &NodeId, target: &NodeId) -> u64
269+
{
270+
(self.penalty_func)(short_channel_id, send_amt_msat, capacity_msat, source, target)
271+
}
272+
273+
fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
274+
275+
fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
276+
}
277+
278+
impl<F> Writeable for DynamicPenaltyScorer<F>
279+
where
280+
F: Fn(u64, u64, u64, &NodeId, &NodeId) -> u64,
281+
{
282+
#[inline]
283+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
284+
write_tlv_fields!(w, {});
285+
Ok(())
286+
}
287+
}
288+
289+
impl<F> ReadableArgs<F> for DynamicPenaltyScorer<F>
290+
where
291+
F: Fn(u64, u64, u64, &NodeId, &NodeId) -> u64,
292+
{
293+
#[inline]
294+
fn read<R: Read>(r: &mut R, penalty_func: F) -> Result<Self, DecodeError> {
295+
read_tlv_fields!(r, {});
296+
Ok(Self { penalty_func })
297+
}
298+
}
299+
244300
/// [`Score`] implementation that provides reasonable default behavior.
245301
///
246302
/// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with

0 commit comments

Comments
 (0)