Update auto-generated bindings
[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 [`find_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::{RouteParameters, find_route};
21 //! # use lightning::routing::scorer::{Scorer, ScoringParameters};
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, params: RouteParameters, network_graph: NetworkGraph) {
30 //! # let logger = FakeLogger {};
31 //! #
32 //! // Use the default channel penalties.
33 //! let scorer = Scorer::default();
34 //!
35 //! // Or use custom channel penalties.
36 //! let scorer = Scorer::new(ScoringParameters {
37 //!     base_penalty_msat: 1000,
38 //!     failure_penalty_msat: 2 * 1024 * 1000,
39 //!     ..ScoringParameters::default()
40 //! });
41 //!
42 //! let route = find_route(&payer, &params, &network_graph, None, &logger, &scorer);
43 //! # }
44 //! ```
45 //!
46 //! [`find_route`]: crate::routing::router::find_route
47
48 use std::str::FromStr;
49 use std::ffi::c_void;
50 use core::convert::Infallible;
51 use bitcoin::hashes::Hash;
52 use crate::c_types::*;
53
54
55 use lightning::routing::scorer::Scorer as nativeScorerImport;
56 pub(crate) type nativeScorer = nativeScorerImport;
57
58 /// [`routing::Score`] implementation that provides reasonable default behavior.
59 ///
60 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
61 /// slightly higher fees are available. May also further penalize failed channels.
62 ///
63 /// See [module-level documentation] for usage.
64 ///
65 /// [module-level documentation]: crate::routing::scorer
66 #[must_use]
67 #[repr(C)]
68 pub struct Scorer {
69         /// A pointer to the opaque Rust object.
70
71         /// Nearly everywhere, inner must be non-null, however in places where
72         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
73         pub inner: *mut nativeScorer,
74         /// Indicates that this is the only struct which contains the same pointer.
75
76         /// Rust functions which take ownership of an object provided via an argument require
77         /// this to be true and invalidate the object pointed to by inner.
78         pub is_owned: bool,
79 }
80
81 impl Drop for Scorer {
82         fn drop(&mut self) {
83                 if self.is_owned && !<*mut nativeScorer>::is_null(self.inner) {
84                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
85                 }
86         }
87 }
88 /// Frees any resources used by the Scorer, if is_owned is set and inner is non-NULL.
89 #[no_mangle]
90 pub extern "C" fn Scorer_free(this_obj: Scorer) { }
91 #[allow(unused)]
92 /// Used only if an object of this type is returned as a trait impl by a method
93 pub(crate) extern "C" fn Scorer_free_void(this_ptr: *mut c_void) {
94         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScorer); }
95 }
96 #[allow(unused)]
97 impl Scorer {
98         pub(crate) fn get_native_ref(&self) -> &'static nativeScorer {
99                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
100         }
101         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScorer {
102                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
103         }
104         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
105         pub(crate) fn take_inner(mut self) -> *mut nativeScorer {
106                 assert!(self.is_owned);
107                 let ret = ObjOps::untweak_ptr(self.inner);
108                 self.inner = std::ptr::null_mut();
109                 ret
110         }
111 }
112
113 use lightning::routing::scorer::ScoringParameters as nativeScoringParametersImport;
114 pub(crate) type nativeScoringParameters = nativeScoringParametersImport;
115
116 /// Parameters for configuring [`Scorer`].
117 #[must_use]
118 #[repr(C)]
119 pub struct ScoringParameters {
120         /// A pointer to the opaque Rust object.
121
122         /// Nearly everywhere, inner must be non-null, however in places where
123         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
124         pub inner: *mut nativeScoringParameters,
125         /// Indicates that this is the only struct which contains the same pointer.
126
127         /// Rust functions which take ownership of an object provided via an argument require
128         /// this to be true and invalidate the object pointed to by inner.
129         pub is_owned: bool,
130 }
131
132 impl Drop for ScoringParameters {
133         fn drop(&mut self) {
134                 if self.is_owned && !<*mut nativeScoringParameters>::is_null(self.inner) {
135                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
136                 }
137         }
138 }
139 /// Frees any resources used by the ScoringParameters, if is_owned is set and inner is non-NULL.
140 #[no_mangle]
141 pub extern "C" fn ScoringParameters_free(this_obj: ScoringParameters) { }
142 #[allow(unused)]
143 /// Used only if an object of this type is returned as a trait impl by a method
144 pub(crate) extern "C" fn ScoringParameters_free_void(this_ptr: *mut c_void) {
145         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScoringParameters); }
146 }
147 #[allow(unused)]
148 impl ScoringParameters {
149         pub(crate) fn get_native_ref(&self) -> &'static nativeScoringParameters {
150                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
151         }
152         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScoringParameters {
153                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
154         }
155         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
156         pub(crate) fn take_inner(mut self) -> *mut nativeScoringParameters {
157                 assert!(self.is_owned);
158                 let ret = ObjOps::untweak_ptr(self.inner);
159                 self.inner = std::ptr::null_mut();
160                 ret
161         }
162 }
163 /// A fixed penalty in msats to apply to each channel.
164 #[no_mangle]
165 pub extern "C" fn ScoringParameters_get_base_penalty_msat(this_ptr: &ScoringParameters) -> u64 {
166         let mut inner_val = &mut this_ptr.get_native_mut_ref().base_penalty_msat;
167         *inner_val
168 }
169 /// A fixed penalty in msats to apply to each channel.
170 #[no_mangle]
171 pub extern "C" fn ScoringParameters_set_base_penalty_msat(this_ptr: &mut ScoringParameters, mut val: u64) {
172         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.base_penalty_msat = val;
173 }
174 /// A penalty in msats to apply to a channel upon failure.
175 ///
176 /// This may be reduced over time based on [`failure_penalty_half_life`].
177 ///
178 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
179 #[no_mangle]
180 pub extern "C" fn ScoringParameters_get_failure_penalty_msat(this_ptr: &ScoringParameters) -> u64 {
181         let mut inner_val = &mut this_ptr.get_native_mut_ref().failure_penalty_msat;
182         *inner_val
183 }
184 /// A penalty in msats to apply to a channel upon failure.
185 ///
186 /// This may be reduced over time based on [`failure_penalty_half_life`].
187 ///
188 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
189 #[no_mangle]
190 pub extern "C" fn ScoringParameters_set_failure_penalty_msat(this_ptr: &mut ScoringParameters, mut val: u64) {
191         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.failure_penalty_msat = val;
192 }
193 /// The time needed before any accumulated channel failure penalties are cut in half.
194 #[no_mangle]
195 pub extern "C" fn ScoringParameters_get_failure_penalty_half_life(this_ptr: &ScoringParameters) -> u64 {
196         let mut inner_val = &mut this_ptr.get_native_mut_ref().failure_penalty_half_life;
197         inner_val.as_secs()
198 }
199 /// The time needed before any accumulated channel failure penalties are cut in half.
200 #[no_mangle]
201 pub extern "C" fn ScoringParameters_set_failure_penalty_half_life(this_ptr: &mut ScoringParameters, mut val: u64) {
202         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.failure_penalty_half_life = std::time::Duration::from_secs(val);
203 }
204 /// Constructs a new ScoringParameters given each field
205 #[must_use]
206 #[no_mangle]
207 pub extern "C" fn ScoringParameters_new(mut base_penalty_msat_arg: u64, mut failure_penalty_msat_arg: u64, mut failure_penalty_half_life_arg: u64) -> ScoringParameters {
208         ScoringParameters { inner: ObjOps::heap_alloc(nativeScoringParameters {
209                 base_penalty_msat: base_penalty_msat_arg,
210                 failure_penalty_msat: failure_penalty_msat_arg,
211                 failure_penalty_half_life: std::time::Duration::from_secs(failure_penalty_half_life_arg),
212         }), is_owned: true }
213 }
214 /// Creates a new scorer using the given scoring parameters.
215 #[must_use]
216 #[no_mangle]
217 pub extern "C" fn Scorer_new(mut params: crate::lightning::routing::scorer::ScoringParameters) -> Scorer {
218         let mut ret = lightning::routing::scorer::Scorer::new(*unsafe { Box::from_raw(params.take_inner()) });
219         Scorer { inner: ObjOps::heap_alloc(ret), is_owned: true }
220 }
221
222 /// Creates a "default" Scorer. See struct and individual field documentaiton for details on which values are used.
223 #[must_use]
224 #[no_mangle]
225 pub extern "C" fn Scorer_default() -> Scorer {
226         Scorer { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
227 }
228 /// Creates a "default" ScoringParameters. See struct and individual field documentaiton for details on which values are used.
229 #[must_use]
230 #[no_mangle]
231 pub extern "C" fn ScoringParameters_default() -> ScoringParameters {
232         ScoringParameters { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
233 }
234 impl From<nativeScorer> for crate::lightning::routing::Score {
235         fn from(obj: nativeScorer) -> Self {
236                 let mut rust_obj = Scorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
237                 let mut ret = Scorer_as_Score(&rust_obj);
238                 // 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
239                 rust_obj.inner = std::ptr::null_mut();
240                 ret.free = Some(Scorer_free_void);
241                 ret
242         }
243 }
244 /// Constructs a new Score which calls the relevant methods on this_arg.
245 /// This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
246 #[no_mangle]
247 pub extern "C" fn Scorer_as_Score(this_arg: &Scorer) -> crate::lightning::routing::Score {
248         crate::lightning::routing::Score {
249                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
250                 free: None,
251                 channel_penalty_msat: Scorer_Score_channel_penalty_msat,
252                 payment_path_failed: Scorer_Score_payment_path_failed,
253         }
254 }
255
256 #[must_use]
257 extern "C" fn Scorer_Score_channel_penalty_msat(this_arg: *const c_void, mut short_channel_id: u64, _source: &crate::lightning::routing::network_graph::NodeId, _target: &crate::lightning::routing::network_graph::NodeId) -> u64 {
258         let mut ret = <nativeScorer as lightning::routing::Score<>>::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeScorer) }, short_channel_id, _source.get_native_ref(), _target.get_native_ref());
259         ret
260 }
261 extern "C" fn Scorer_Score_payment_path_failed(this_arg: *mut c_void, mut _path: crate::c_types::derived::CVec_RouteHopZ, mut short_channel_id: u64) {
262         let mut local__path = Vec::new(); for mut item in _path.as_slice().iter() { local__path.push( { item.get_native_ref() }); };
263         <nativeScorer as lightning::routing::Score<>>::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeScorer) }, &local__path[..], short_channel_id)
264 }
265