Merge pull request #46 from TheBlueMatt/main
[ldk-c-bindings] / lightning-c-bindings / src / lightning / routing / scorer.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
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.
8
9 //! Utilities for scoring payment channels.
10 //!
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.
13 //!
14 //! # Example
15 //!
16 //! ```
17 //! # extern crate secp256k1;
18 //! #
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;
24 //! #
25 //! # struct FakeLogger {};
26 //! # impl Logger for FakeLogger {
27 //! #     fn log(&self, record: &Record) { unimplemented!() }
28 //! # }
29 //! # fn find_scored_route(payer: PublicKey, payee: PublicKey, network_graph: NetworkGraph) {
30 //! # let logger = FakeLogger {};
31 //! #
32 //! // Use the default channel penalty.
33 //! let scorer = Scorer::default();
34 //!
35 //! // Or use a custom channel penalty.
36 //! let scorer = Scorer::new(1_000);
37 //!
38 //! let route = get_route(&payer, &network_graph, &payee, None, None, &vec![], 1_000, 42, &logger, &scorer);
39 //! # }
40 //! ```
41 //!
42 //! [`get_route`]: crate::routing::router::get_route
43
44 use std::str::FromStr;
45 use std::ffi::c_void;
46 use core::convert::Infallible;
47 use bitcoin::hashes::Hash;
48 use crate::c_types::*;
49
50
51 use lightning::routing::scorer::Scorer as nativeScorerImport;
52 type nativeScorer = nativeScorerImport;
53
54 /// [`routing::Score`] implementation that provides reasonable default behavior.
55 ///
56 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
57 /// slightly higher fees are available.
58 ///
59 /// See [module-level documentation] for usage.
60 ///
61 /// [module-level documentation]: crate::routing::scorer
62 #[must_use]
63 #[repr(C)]
64 pub struct Scorer {
65         /// A pointer to the opaque Rust object.
66
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.
71
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.
74         pub is_owned: bool,
75 }
76
77 impl Drop for Scorer {
78         fn drop(&mut self) {
79                 if self.is_owned && !<*mut nativeScorer>::is_null(self.inner) {
80                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
81                 }
82         }
83 }
84 /// Frees any resources used by the Scorer, if is_owned is set and inner is non-NULL.
85 #[no_mangle]
86 pub extern "C" fn Scorer_free(this_obj: Scorer) { }
87 #[allow(unused)]
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); }
91 }
92 #[allow(unused)]
93 impl Scorer {
94         pub(crate) fn get_native_ref(&self) -> &'static nativeScorer {
95                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
96         }
97         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScorer {
98                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
99         }
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();
105                 ret
106         }
107 }
108 /// Creates a new scorer using `base_penalty_msat` as the channel penalty.
109 #[must_use]
110 #[no_mangle]
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 }
114 }
115
116 /// Creates a "default" Scorer. See struct and individual field documentaiton for details on which values are used.
117 #[must_use]
118 #[no_mangle]
119 pub extern "C" fn Scorer_default() -> Scorer {
120         Scorer { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
121 }
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);
129                 ret
130         }
131 }
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
134 #[no_mangle]
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 },
138                 free: None,
139                 channel_penalty_msat: Scorer_Score_channel_penalty_msat,
140         }
141 }
142
143 #[must_use]
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);
146         ret
147 }
148