1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! Utilities for scoring payment channels.
11 //! [`Scorer`] may be given to [`get_route`] to score payment channels during path finding when a
12 //! custom [`routing::Score`] implementation is not needed.
17 //! # extern crate secp256k1;
19 //! # use lightning::routing::network_graph::NetworkGraph;
20 //! # use lightning::routing::router::get_route;
21 //! # use lightning::routing::scorer::Scorer;
22 //! # use lightning::util::logger::{Logger, Record};
23 //! # use secp256k1::key::PublicKey;
25 //! # struct FakeLogger {};
26 //! # impl Logger for FakeLogger {
27 //! # fn log(&self, record: &Record) { unimplemented!() }
29 //! # fn find_scored_route(payer: PublicKey, payee: PublicKey, network_graph: NetworkGraph) {
30 //! # let logger = FakeLogger {};
32 //! // Use the default channel penalty.
33 //! let scorer = Scorer::default();
35 //! // Or use a custom channel penalty.
36 //! let scorer = Scorer::new(1_000);
38 //! let route = get_route(&payer, &network_graph, &payee, None, None, &vec![], 1_000, 42, &logger, &scorer);
42 //! [`get_route`]: crate::routing::router::get_route
44 use std::str::FromStr;
46 use core::convert::Infallible;
47 use bitcoin::hashes::Hash;
48 use crate::c_types::*;
51 use lightning::routing::scorer::Scorer as nativeScorerImport;
52 type nativeScorer = nativeScorerImport;
54 /// [`routing::Score`] implementation that provides reasonable default behavior.
56 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
57 /// slightly higher fees are available.
59 /// See [module-level documentation] for usage.
61 /// [module-level documentation]: crate::routing::scorer
65 /// A pointer to the opaque Rust object.
67 /// Nearly everywhere, inner must be non-null, however in places where
68 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
69 pub inner: *mut nativeScorer,
70 /// Indicates that this is the only struct which contains the same pointer.
72 /// Rust functions which take ownership of an object provided via an argument require
73 /// this to be true and invalidate the object pointed to by inner.
77 impl Drop for Scorer {
79 if self.is_owned && !<*mut nativeScorer>::is_null(self.inner) {
80 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
84 /// Frees any resources used by the Scorer, if is_owned is set and inner is non-NULL.
86 pub extern "C" fn Scorer_free(this_obj: Scorer) { }
88 /// Used only if an object of this type is returned as a trait impl by a method
89 extern "C" fn Scorer_free_void(this_ptr: *mut c_void) {
90 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScorer); }
94 pub(crate) fn get_native_ref(&self) -> &'static nativeScorer {
95 unsafe { &*ObjOps::untweak_ptr(self.inner) }
97 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScorer {
98 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
100 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
101 pub(crate) fn take_inner(mut self) -> *mut nativeScorer {
102 assert!(self.is_owned);
103 let ret = ObjOps::untweak_ptr(self.inner);
104 self.inner = std::ptr::null_mut();
108 /// Creates a new scorer using `base_penalty_msat` as the channel penalty.
111 pub extern "C" fn Scorer_new(mut base_penalty_msat: u64) -> Scorer {
112 let mut ret = lightning::routing::scorer::Scorer::new(base_penalty_msat);
113 Scorer { inner: ObjOps::heap_alloc(ret), is_owned: true }
116 /// Creates a "default" Scorer. See struct and individual field documentaiton for details on which values are used.
119 pub extern "C" fn Scorer_default() -> Scorer {
120 Scorer { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
122 impl From<nativeScorer> for crate::lightning::routing::Score {
123 fn from(obj: nativeScorer) -> Self {
124 let mut rust_obj = Scorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
125 let mut ret = Scorer_as_Score(&rust_obj);
126 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn
127 rust_obj.inner = std::ptr::null_mut();
128 ret.free = Some(Scorer_free_void);
132 /// Constructs a new Score which calls the relevant methods on this_arg.
133 /// This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
135 pub extern "C" fn Scorer_as_Score(this_arg: &Scorer) -> crate::lightning::routing::Score {
136 crate::lightning::routing::Score {
137 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
139 channel_penalty_msat: Scorer_Score_channel_penalty_msat,
144 extern "C" fn Scorer_Score_channel_penalty_msat(this_arg: *const c_void, mut _short_channel_id: u64) -> u64 {
145 let mut ret = <nativeScorer as lightning::routing::Score<>>::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeScorer) }, _short_channel_id);