Merge pull request #1146 from jkczyz/2021-10-score-serialization
[rust-lightning] / lightning / src / routing / mod.rs
index d419c6e9300ace03868c8272b7a71492f7664431..d6c016468ce79f696483ccafcee03e0269b6267f 100644 (file)
@@ -1,4 +1,76 @@
+// This file is Copyright its original authors, visible in version control
+// history.
+//
+// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
+// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
+// You may not use this file except in accordance with one or both of these
+// licenses.
+
 //! Structs and impls for receiving messages about the network and storing the topology live here.
 
-pub mod router;
 pub mod network_graph;
+pub mod router;
+pub mod scorer;
+
+use routing::network_graph::NodeId;
+use routing::router::RouteHop;
+
+use prelude::*;
+use core::cell::{RefCell, RefMut};
+use core::ops::DerefMut;
+use sync::{Mutex, MutexGuard};
+
+/// An interface used to score payment channels for path finding.
+///
+///    Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
+pub trait Score {
+       /// Returns the fee in msats willing to be paid to avoid routing through the given channel
+       /// in the direction from `source` to `target`.
+       fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId) -> u64;
+
+       /// Handles updating channel penalties after failing to route through a channel.
+       fn payment_path_failed(&mut self, path: &Vec<RouteHop>, short_channel_id: u64);
+}
+
+/// A scorer that is accessed under a lock.
+///
+/// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
+/// having shared ownership of a scorer but without requiring internal locking in [`Score`]
+/// implementations. Internal locking would be detrimental to route finding performance and could
+/// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
+///
+/// [`find_route`]: crate::routing::router::find_route
+pub trait LockableScore<'a> {
+       /// The locked [`Score`] type.
+       type Locked: 'a + Score;
+
+       /// Returns the locked scorer.
+       fn lock(&'a self) -> Self::Locked;
+}
+
+impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
+       type Locked = MutexGuard<'a, T>;
+
+       fn lock(&'a self) -> MutexGuard<'a, T> {
+               Mutex::lock(self).unwrap()
+       }
+}
+
+impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
+       type Locked = RefMut<'a, T>;
+
+       fn lock(&'a self) -> RefMut<'a, T> {
+               self.borrow_mut()
+       }
+}
+
+impl<S: Score, T: DerefMut<Target=S>> Score for T {
+       fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId) -> u64 {
+               self.deref().channel_penalty_msat(short_channel_id, source, target)
+       }
+
+       fn payment_path_failed(&mut self, path: &Vec<RouteHop>, short_channel_id: u64) {
+               self.deref_mut().payment_path_failed(path, short_channel_id)
+       }
+}