3a48ffe93ddf42c77fb3b3c9b17d727dba14c8f0
[rust-lightning] / lightning / src / routing / mod.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Structs and impls for receiving messages about the network and storing the topology live here.
11
12 pub mod network_graph;
13 pub mod router;
14 pub mod scorer;
15
16 use routing::network_graph::NodeId;
17 use routing::router::RouteHop;
18
19 use core::cell::{RefCell, RefMut};
20 use core::ops::DerefMut;
21 use sync::{Mutex, MutexGuard};
22
23 /// An interface used to score payment channels for path finding.
24 ///
25 ///     Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
26 pub trait Score {
27         /// Returns the fee in msats willing to be paid to avoid routing through the given channel
28         /// in the direction from `source` to `target`.
29         fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId) -> u64;
30
31         /// Handles updating channel penalties after failing to route through a channel.
32         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64);
33 }
34
35 /// A scorer that is accessed under a lock.
36 ///
37 /// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
38 /// having shared ownership of a scorer but without requiring internal locking in [`Score`]
39 /// implementations. Internal locking would be detrimental to route finding performance and could
40 /// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
41 ///
42 /// [`find_route`]: crate::routing::router::find_route
43 pub trait LockableScore<'a> {
44         /// The locked [`Score`] type.
45         type Locked: 'a + Score;
46
47         /// Returns the locked scorer.
48         fn lock(&'a self) -> Self::Locked;
49 }
50
51 impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
52         type Locked = MutexGuard<'a, T>;
53
54         fn lock(&'a self) -> MutexGuard<'a, T> {
55                 Mutex::lock(self).unwrap()
56         }
57 }
58
59 impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
60         type Locked = RefMut<'a, T>;
61
62         fn lock(&'a self) -> RefMut<'a, T> {
63                 self.borrow_mut()
64         }
65 }
66
67 impl<S: Score, T: DerefMut<Target=S>> Score for T {
68         fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId) -> u64 {
69                 self.deref().channel_penalty_msat(short_channel_id, source, target)
70         }
71
72         fn payment_path_failed(&mut self, path: &[&RouteHop], short_channel_id: u64) {
73                 self.deref_mut().payment_path_failed(path, short_channel_id)
74         }
75 }