1 // This file is Copyright its original authors, visible in version control
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
10 //! Structs and impls for receiving messages about the network and storing the topology live here.
12 pub mod network_graph;
16 use routing::network_graph::NodeId;
17 use routing::router::RouteHop;
20 use core::cell::{RefCell, RefMut};
21 use core::ops::DerefMut;
22 use sync::{Mutex, MutexGuard};
24 /// An interface used to score payment channels for path finding.
26 /// Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
28 /// Returns the fee in msats willing to be paid to avoid routing through the given channel
29 /// in the direction from `source` to `target`.
30 fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId) -> u64;
32 /// Handles updating channel penalties after failing to route through a channel.
33 fn payment_path_failed(&mut self, path: &Vec<RouteHop>, short_channel_id: u64);
36 /// A scorer that is accessed under a lock.
38 /// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
39 /// having shared ownership of a scorer but without requiring internal locking in [`Score`]
40 /// implementations. Internal locking would be detrimental to route finding performance and could
41 /// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
43 /// [`find_route`]: crate::routing::router::find_route
44 pub trait LockableScore<'a> {
45 /// The locked [`Score`] type.
46 type Locked: 'a + Score;
48 /// Returns the locked scorer.
49 fn lock(&'a self) -> Self::Locked;
52 impl<'a, T: 'a + Score> LockableScore<'a> for Mutex<T> {
53 type Locked = MutexGuard<'a, T>;
55 fn lock(&'a self) -> MutexGuard<'a, T> {
56 Mutex::lock(self).unwrap()
60 impl<'a, T: 'a + Score> LockableScore<'a> for RefCell<T> {
61 type Locked = RefMut<'a, T>;
63 fn lock(&'a self) -> RefMut<'a, T> {
68 impl<S: Score, T: DerefMut<Target=S>> Score for T {
69 fn channel_penalty_msat(&self, short_channel_id: u64, source: &NodeId, target: &NodeId) -> u64 {
70 self.deref().channel_penalty_msat(short_channel_id, source, target)
73 fn payment_path_failed(&mut self, path: &Vec<RouteHop>, short_channel_id: u64) {
74 self.deref_mut().payment_path_failed(path, short_channel_id)