Skip to content

Commit a8070e3

Browse files
committed
Implement DynamicPenaltyScorer.
1 parent 95182fb commit a8070e3

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
@@ -259,6 +259,62 @@ impl ReadableArgs<u64> for FixedPenaltyScorer {
259259
}
260260
}
261261

262+
/// [`Score`] implementation that uses a dynamic penalty.
263+
pub struct DynamicPenaltyScorer<F>
264+
where
265+
F: Fn(u64, u64, u64, &NodeId, &NodeId) -> u64,
266+
{
267+
penalty_func: F,
268+
}
269+
270+
impl<F> DynamicPenaltyScorer<F>
271+
where
272+
F: Fn(u64, u64, u64, &NodeId, &NodeId) -> u64,
273+
{
274+
/// Creates a new scorer using `penalty_func`.
275+
pub fn with_penalty_func(penalty_func: F) -> Self
276+
{
277+
Self { penalty_func }
278+
}
279+
}
280+
281+
impl<F> Score for DynamicPenaltyScorer<F>
282+
where
283+
F: Fn(u64, u64, u64, &NodeId, &NodeId) -> u64,
284+
{
285+
fn channel_penalty_msat(&self, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64,
286+
source: &NodeId, target: &NodeId) -> u64
287+
{
288+
(self.penalty_func)(short_channel_id, send_amt_msat, capacity_msat, source, target)
289+
}
290+
291+
fn payment_path_failed(&mut self, _path: &[&RouteHop], _short_channel_id: u64) {}
292+
293+
fn payment_path_successful(&mut self, _path: &[&RouteHop]) {}
294+
}
295+
296+
impl<F> Writeable for DynamicPenaltyScorer<F>
297+
where
298+
F: Fn(u64, u64, u64, &NodeId, &NodeId) -> u64,
299+
{
300+
#[inline]
301+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
302+
write_tlv_fields!(w, {});
303+
Ok(())
304+
}
305+
}
306+
307+
impl<F> ReadableArgs<F> for DynamicPenaltyScorer<F>
308+
where
309+
F: Fn(u64, u64, u64, &NodeId, &NodeId) -> u64,
310+
{
311+
#[inline]
312+
fn read<R: Read>(r: &mut R, penalty_func: F) -> Result<Self, DecodeError> {
313+
read_tlv_fields!(r, {});
314+
Ok(Self { penalty_func })
315+
}
316+
}
317+
262318
/// [`Score`] implementation that provides reasonable default behavior.
263319
///
264320
/// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with

0 commit comments

Comments
 (0)