Unify route finding methods
[rust-lightning] / lightning / src / routing / scorer.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 //! Utilities for scoring payment channels.
11 //!
12 //! [`Scorer`] may be given to [`find_route`] to score payment channels during path finding when a
13 //! custom [`routing::Score`] implementation is not needed.
14 //!
15 //! # Example
16 //!
17 //! ```
18 //! # extern crate secp256k1;
19 //! #
20 //! # use lightning::routing::network_graph::NetworkGraph;
21 //! # use lightning::routing::router::{RouteParameters, find_route};
22 //! # use lightning::routing::scorer::Scorer;
23 //! # use lightning::util::logger::{Logger, Record};
24 //! # use secp256k1::key::PublicKey;
25 //! #
26 //! # struct FakeLogger {};
27 //! # impl Logger for FakeLogger {
28 //! #     fn log(&self, record: &Record) { unimplemented!() }
29 //! # }
30 //! # fn find_scored_route(payer: PublicKey, params: RouteParameters, network_graph: NetworkGraph) {
31 //! # let logger = FakeLogger {};
32 //! #
33 //! // Use the default channel penalty.
34 //! let scorer = Scorer::default();
35 //!
36 //! // Or use a custom channel penalty.
37 //! let scorer = Scorer::new(1_000);
38 //!
39 //! let route = find_route(&payer, &params, &network_graph, None, &logger, &scorer);
40 //! # }
41 //! ```
42 //!
43 //! [`find_route`]: crate::routing::router::find_route
44
45 use routing;
46
47 use routing::network_graph::NodeId;
48
49 /// [`routing::Score`] implementation that provides reasonable default behavior.
50 ///
51 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
52 /// slightly higher fees are available.
53 ///
54 /// See [module-level documentation] for usage.
55 ///
56 /// [module-level documentation]: crate::routing::scorer
57 pub struct Scorer {
58         base_penalty_msat: u64,
59 }
60
61 impl Scorer {
62         /// Creates a new scorer using `base_penalty_msat` as the channel penalty.
63         pub fn new(base_penalty_msat: u64) -> Self {
64                 Self { base_penalty_msat }
65         }
66 }
67
68 impl Default for Scorer {
69         /// Creates a new scorer using 500 msat as the channel penalty.
70         fn default() -> Self {
71                 Scorer::new(500)
72         }
73 }
74
75 impl routing::Score for Scorer {
76         fn channel_penalty_msat(
77                 &self, _short_channel_id: u64, _source: &NodeId, _target: &NodeId
78         ) -> u64 {
79                 self.base_penalty_msat
80         }
81 }