Update auto-generated bindings with the ProbabilisticScorer
[ldk-c-bindings] / lightning-c-bindings / src / lightning / routing / scoring.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 //! [`ProbabilisticScorer`] may be given to [`find_route`] to score payment channels during path
12 //! finding when a custom [`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::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters, 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, route_params: RouteParameters, network_graph: NetworkGraph) {
30 //! # let logger = FakeLogger {};
31 //! #
32 //! // Use the default channel penalties.
33 //! let params = ProbabilisticScoringParameters::default();
34 //! let scorer = ProbabilisticScorer::new(params, &network_graph);
35 //!
36 //! // Or use custom channel penalties.
37 //! let params = ProbabilisticScoringParameters {
38 //!     liquidity_penalty_multiplier_msat: 2 * 1000,
39 //!     ..ProbabilisticScoringParameters::default()
40 //! };
41 //! let scorer = ProbabilisticScorer::new(params, &network_graph);
42 //!
43 //! let route = find_route(&payer, &route_params, &network_graph, None, &logger, &scorer);
44 //! # }
45 //! ```
46 //!
47 //! # Note
48 //!
49 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
50 //! different types and thus is undefined.
51 //!
52 //! [`find_route`]: crate::routing::router::find_route
53
54 use alloc::str::FromStr;
55 use core::ffi::c_void;
56 use core::convert::Infallible;
57 use bitcoin::hashes::Hash;
58 use crate::c_types::*;
59 #[cfg(feature="no-std")]
60 use alloc::{vec::Vec, boxed::Box};
61
62 /// An interface used to score payment channels for path finding.
63 ///
64 ///\tScoring is in terms of fees willing to be paid in order to avoid routing through a channel.
65 #[repr(C)]
66 pub struct Score {
67         /// An opaque pointer which is passed to your function implementations as an argument.
68         /// This has no meaning in the LDK, and can be NULL or any other value.
69         pub this_arg: *mut c_void,
70         /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
71         /// given channel in the direction from `source` to `target`.
72         ///
73         /// The channel's capacity (less any other MPP parts that are also being considered for use in
74         /// the same payment) is given by `capacity_msat`. It may be determined from various sources
75         /// such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
76         /// [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
77         /// Thus, implementations should be overflow-safe.
78         #[must_use]
79         pub channel_penalty_msat: extern "C" fn (this_arg: *const c_void, short_channel_id: u64, send_amt_msat: u64, capacity_msat: u64, source: &crate::lightning::routing::network_graph::NodeId, target: &crate::lightning::routing::network_graph::NodeId) -> u64,
80         /// Handles updating channel penalties after failing to route through a channel.
81         pub payment_path_failed: extern "C" fn (this_arg: *mut c_void, path: crate::c_types::derived::CVec_RouteHopZ, short_channel_id: u64),
82         /// Handles updating channel penalties after successfully routing along a path.
83         pub payment_path_successful: extern "C" fn (this_arg: *mut c_void, path: crate::c_types::derived::CVec_RouteHopZ),
84         /// Serialize the object into a byte array
85         pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
86         /// Frees any resources associated with this object given its this_arg pointer.
87         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
88         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
89 }
90 unsafe impl Send for Score {}
91 unsafe impl Sync for Score {}
92 #[no_mangle]
93 pub(crate) extern "C" fn Score_clone_fields(orig: &Score) -> Score {
94         Score {
95                 this_arg: orig.this_arg,
96                 channel_penalty_msat: Clone::clone(&orig.channel_penalty_msat),
97                 payment_path_failed: Clone::clone(&orig.payment_path_failed),
98                 payment_path_successful: Clone::clone(&orig.payment_path_successful),
99                 write: Clone::clone(&orig.write),
100                 free: Clone::clone(&orig.free),
101         }
102 }
103 impl lightning::util::ser::Writeable for Score {
104         fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
105                 let vec = (self.write)(self.this_arg);
106                 w.write_all(vec.as_slice())
107         }
108 }
109
110 use lightning::routing::scoring::Score as rustScore;
111 impl rustScore for Score {
112         fn channel_penalty_msat(&self, mut short_channel_id: u64, mut send_amt_msat: u64, mut capacity_msat: u64, mut source: &lightning::routing::network_graph::NodeId, mut target: &lightning::routing::network_graph::NodeId) -> u64 {
113                 let mut ret = (self.channel_penalty_msat)(self.this_arg, short_channel_id, send_amt_msat, capacity_msat, &crate::lightning::routing::network_graph::NodeId { inner: unsafe { ObjOps::nonnull_ptr_to_inner((source as *const lightning::routing::network_graph::NodeId<>) as *mut _) }, is_owned: false }, &crate::lightning::routing::network_graph::NodeId { inner: unsafe { ObjOps::nonnull_ptr_to_inner((target as *const lightning::routing::network_graph::NodeId<>) as *mut _) }, is_owned: false });
114                 ret
115         }
116         fn payment_path_failed(&mut self, mut path: &[&lightning::routing::router::RouteHop], mut short_channel_id: u64) {
117                 let mut local_path = Vec::new(); for item in path.iter() { local_path.push( { crate::lightning::routing::router::RouteHop { inner: unsafe { ObjOps::nonnull_ptr_to_inner(((*item) as *const lightning::routing::router::RouteHop<>) as *mut _) }, is_owned: false } }); };
118                 (self.payment_path_failed)(self.this_arg, local_path.into(), short_channel_id)
119         }
120         fn payment_path_successful(&mut self, mut path: &[&lightning::routing::router::RouteHop]) {
121                 let mut local_path = Vec::new(); for item in path.iter() { local_path.push( { crate::lightning::routing::router::RouteHop { inner: unsafe { ObjOps::nonnull_ptr_to_inner(((*item) as *const lightning::routing::router::RouteHop<>) as *mut _) }, is_owned: false } }); };
122                 (self.payment_path_successful)(self.this_arg, local_path.into())
123         }
124 }
125
126 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
127 // directly as a Deref trait in higher-level structs:
128 impl core::ops::Deref for Score {
129         type Target = Self;
130         fn deref(&self) -> &Self {
131                 self
132         }
133 }
134 /// Calls the free function if one is set
135 #[no_mangle]
136 pub extern "C" fn Score_free(this_ptr: Score) { }
137 impl Drop for Score {
138         fn drop(&mut self) {
139                 if let Some(f) = self.free {
140                         f(self.this_arg);
141                 }
142         }
143 }
144 /// A scorer that is accessed under a lock.
145 ///
146 /// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
147 /// having shared ownership of a scorer but without requiring internal locking in [`Score`]
148 /// implementations. Internal locking would be detrimental to route finding performance and could
149 /// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
150 ///
151 /// [`find_route`]: crate::routing::router::find_route
152 #[repr(C)]
153 pub struct LockableScore {
154         /// An opaque pointer which is passed to your function implementations as an argument.
155         /// This has no meaning in the LDK, and can be NULL or any other value.
156         pub this_arg: *mut c_void,
157         /// Returns the locked scorer.
158         #[must_use]
159         pub lock: extern "C" fn (this_arg: *const c_void) -> crate::lightning::routing::scoring::Score,
160         /// Frees any resources associated with this object given its this_arg pointer.
161         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
162         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
163 }
164 unsafe impl Send for LockableScore {}
165 unsafe impl Sync for LockableScore {}
166 #[no_mangle]
167 pub(crate) extern "C" fn LockableScore_clone_fields(orig: &LockableScore) -> LockableScore {
168         LockableScore {
169                 this_arg: orig.this_arg,
170                 lock: Clone::clone(&orig.lock),
171                 free: Clone::clone(&orig.free),
172         }
173 }
174
175 use lightning::routing::scoring::LockableScore as rustLockableScore;
176 impl<'a> rustLockableScore<'a> for LockableScore {
177         type Locked = crate::lightning::routing::scoring::Score;
178         fn lock(&'a self) -> crate::lightning::routing::scoring::Score {
179                 let mut ret = (self.lock)(self.this_arg);
180                 ret
181         }
182 }
183
184 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
185 // directly as a Deref trait in higher-level structs:
186 impl core::ops::Deref for LockableScore {
187         type Target = Self;
188         fn deref(&self) -> &Self {
189                 self
190         }
191 }
192 /// Calls the free function if one is set
193 #[no_mangle]
194 pub extern "C" fn LockableScore_free(this_ptr: LockableScore) { }
195 impl Drop for LockableScore {
196         fn drop(&mut self) {
197                 if let Some(f) = self.free {
198                         f(self.this_arg);
199                 }
200         }
201 }
202
203 use lightning::routing::scoring::MultiThreadedLockableScore as nativeMultiThreadedLockableScoreImport;
204 pub(crate) type nativeMultiThreadedLockableScore = nativeMultiThreadedLockableScoreImport<crate::lightning::routing::scoring::Score>;
205
206 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
207 #[must_use]
208 #[repr(C)]
209 pub struct MultiThreadedLockableScore {
210         /// A pointer to the opaque Rust object.
211
212         /// Nearly everywhere, inner must be non-null, however in places where
213         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
214         pub inner: *mut nativeMultiThreadedLockableScore,
215         /// Indicates that this is the only struct which contains the same pointer.
216
217         /// Rust functions which take ownership of an object provided via an argument require
218         /// this to be true and invalidate the object pointed to by inner.
219         pub is_owned: bool,
220 }
221
222 impl Drop for MultiThreadedLockableScore {
223         fn drop(&mut self) {
224                 if self.is_owned && !<*mut nativeMultiThreadedLockableScore>::is_null(self.inner) {
225                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
226                 }
227         }
228 }
229 /// Frees any resources used by the MultiThreadedLockableScore, if is_owned is set and inner is non-NULL.
230 #[no_mangle]
231 pub extern "C" fn MultiThreadedLockableScore_free(this_obj: MultiThreadedLockableScore) { }
232 #[allow(unused)]
233 /// Used only if an object of this type is returned as a trait impl by a method
234 pub(crate) extern "C" fn MultiThreadedLockableScore_free_void(this_ptr: *mut c_void) {
235         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMultiThreadedLockableScore); }
236 }
237 #[allow(unused)]
238 impl MultiThreadedLockableScore {
239         pub(crate) fn get_native_ref(&self) -> &'static nativeMultiThreadedLockableScore {
240                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
241         }
242         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeMultiThreadedLockableScore {
243                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
244         }
245         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
246         pub(crate) fn take_inner(mut self) -> *mut nativeMultiThreadedLockableScore {
247                 assert!(self.is_owned);
248                 let ret = ObjOps::untweak_ptr(self.inner);
249                 self.inner = core::ptr::null_mut();
250                 ret
251         }
252 }
253 /// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
254 #[must_use]
255 #[no_mangle]
256 pub extern "C" fn MultiThreadedLockableScore_new(mut score: crate::lightning::routing::scoring::Score) -> MultiThreadedLockableScore {
257         let mut ret = lightning::routing::scoring::MultiThreadedLockableScore::new(score);
258         MultiThreadedLockableScore { inner: ObjOps::heap_alloc(ret), is_owned: true }
259 }
260
261
262 use lightning::routing::scoring::FixedPenaltyScorer as nativeFixedPenaltyScorerImport;
263 pub(crate) type nativeFixedPenaltyScorer = nativeFixedPenaltyScorerImport;
264
265 /// [`Score`] implementation that uses a fixed penalty.
266 #[must_use]
267 #[repr(C)]
268 pub struct FixedPenaltyScorer {
269         /// A pointer to the opaque Rust object.
270
271         /// Nearly everywhere, inner must be non-null, however in places where
272         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
273         pub inner: *mut nativeFixedPenaltyScorer,
274         /// Indicates that this is the only struct which contains the same pointer.
275
276         /// Rust functions which take ownership of an object provided via an argument require
277         /// this to be true and invalidate the object pointed to by inner.
278         pub is_owned: bool,
279 }
280
281 impl Drop for FixedPenaltyScorer {
282         fn drop(&mut self) {
283                 if self.is_owned && !<*mut nativeFixedPenaltyScorer>::is_null(self.inner) {
284                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
285                 }
286         }
287 }
288 /// Frees any resources used by the FixedPenaltyScorer, if is_owned is set and inner is non-NULL.
289 #[no_mangle]
290 pub extern "C" fn FixedPenaltyScorer_free(this_obj: FixedPenaltyScorer) { }
291 #[allow(unused)]
292 /// Used only if an object of this type is returned as a trait impl by a method
293 pub(crate) extern "C" fn FixedPenaltyScorer_free_void(this_ptr: *mut c_void) {
294         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeFixedPenaltyScorer); }
295 }
296 #[allow(unused)]
297 impl FixedPenaltyScorer {
298         pub(crate) fn get_native_ref(&self) -> &'static nativeFixedPenaltyScorer {
299                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
300         }
301         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeFixedPenaltyScorer {
302                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
303         }
304         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
305         pub(crate) fn take_inner(mut self) -> *mut nativeFixedPenaltyScorer {
306                 assert!(self.is_owned);
307                 let ret = ObjOps::untweak_ptr(self.inner);
308                 self.inner = core::ptr::null_mut();
309                 ret
310         }
311 }
312 #[no_mangle]
313 /// Serialize the FixedPenaltyScorer object into a byte array which can be read by FixedPenaltyScorer_read
314 pub extern "C" fn FixedPenaltyScorer_write(obj: &FixedPenaltyScorer) -> crate::c_types::derived::CVec_u8Z {
315         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
316 }
317 #[no_mangle]
318 pub(crate) extern "C" fn FixedPenaltyScorer_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
319         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeFixedPenaltyScorer) })
320 }
321 #[no_mangle]
322 /// Read a FixedPenaltyScorer from a byte array, created by FixedPenaltyScorer_write
323 pub extern "C" fn FixedPenaltyScorer_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_FixedPenaltyScorerDecodeErrorZ {
324         let res: Result<lightning::routing::scoring::FixedPenaltyScorer, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
325         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::scoring::FixedPenaltyScorer { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
326         local_res
327 }
328 /// Creates a new scorer using `penalty_msat`.
329 #[must_use]
330 #[no_mangle]
331 pub extern "C" fn FixedPenaltyScorer_with_penalty(mut penalty_msat: u64) -> FixedPenaltyScorer {
332         let mut ret = lightning::routing::scoring::FixedPenaltyScorer::with_penalty(penalty_msat);
333         FixedPenaltyScorer { inner: ObjOps::heap_alloc(ret), is_owned: true }
334 }
335
336 impl From<nativeFixedPenaltyScorer> for crate::lightning::routing::scoring::Score {
337         fn from(obj: nativeFixedPenaltyScorer) -> Self {
338                 let mut rust_obj = FixedPenaltyScorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
339                 let mut ret = FixedPenaltyScorer_as_Score(&rust_obj);
340                 // 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
341                 rust_obj.inner = core::ptr::null_mut();
342                 ret.free = Some(FixedPenaltyScorer_free_void);
343                 ret
344         }
345 }
346 /// Constructs a new Score which calls the relevant methods on this_arg.
347 /// This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
348 #[no_mangle]
349 pub extern "C" fn FixedPenaltyScorer_as_Score(this_arg: &FixedPenaltyScorer) -> crate::lightning::routing::scoring::Score {
350         crate::lightning::routing::scoring::Score {
351                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
352                 free: None,
353                 channel_penalty_msat: FixedPenaltyScorer_Score_channel_penalty_msat,
354                 payment_path_failed: FixedPenaltyScorer_Score_payment_path_failed,
355                 payment_path_successful: FixedPenaltyScorer_Score_payment_path_successful,
356                 write: FixedPenaltyScorer_write_void,
357         }
358 }
359
360 #[must_use]
361 extern "C" fn FixedPenaltyScorer_Score_channel_penalty_msat(this_arg: *const c_void, unused_0: u64, unused_1: u64, unused_2: u64, unused_3: &crate::lightning::routing::network_graph::NodeId, unused_4: &crate::lightning::routing::network_graph::NodeId) -> u64 {
362         let mut ret = <nativeFixedPenaltyScorer as lightning::routing::scoring::Score<>>::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, unused_0, unused_1, unused_2, unused_3.get_native_ref(), unused_4.get_native_ref());
363         ret
364 }
365 extern "C" fn FixedPenaltyScorer_Score_payment_path_failed(this_arg: *mut c_void, mut _path: crate::c_types::derived::CVec_RouteHopZ, mut _short_channel_id: u64) {
366         let mut local__path = Vec::new(); for mut item in _path.as_slice().iter() { local__path.push( { item.get_native_ref() }); };
367         <nativeFixedPenaltyScorer as lightning::routing::scoring::Score<>>::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, &local__path[..], _short_channel_id)
368 }
369 extern "C" fn FixedPenaltyScorer_Score_payment_path_successful(this_arg: *mut c_void, mut _path: crate::c_types::derived::CVec_RouteHopZ) {
370         let mut local__path = Vec::new(); for mut item in _path.as_slice().iter() { local__path.push( { item.get_native_ref() }); };
371         <nativeFixedPenaltyScorer as lightning::routing::scoring::Score<>>::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, &local__path[..])
372 }
373
374
375 use lightning::routing::scoring::Scorer as nativeScorerImport;
376 pub(crate) type nativeScorer = nativeScorerImport;
377
378 /// [`Score`] implementation that provides reasonable default behavior.
379 ///
380 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
381 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
382 ///
383 /// See [module-level documentation] for usage and [`ScoringParameters`] for customization.
384 ///
385 /// # Note
386 ///
387 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
388 /// behavior.
389 ///
390 /// [module-level documentation]: crate::routing::scoring
391 #[must_use]
392 #[repr(C)]
393 pub struct Scorer {
394         /// A pointer to the opaque Rust object.
395
396         /// Nearly everywhere, inner must be non-null, however in places where
397         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
398         pub inner: *mut nativeScorer,
399         /// Indicates that this is the only struct which contains the same pointer.
400
401         /// Rust functions which take ownership of an object provided via an argument require
402         /// this to be true and invalidate the object pointed to by inner.
403         pub is_owned: bool,
404 }
405
406 impl Drop for Scorer {
407         fn drop(&mut self) {
408                 if self.is_owned && !<*mut nativeScorer>::is_null(self.inner) {
409                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
410                 }
411         }
412 }
413 /// Frees any resources used by the Scorer, if is_owned is set and inner is non-NULL.
414 #[no_mangle]
415 pub extern "C" fn Scorer_free(this_obj: Scorer) { }
416 #[allow(unused)]
417 /// Used only if an object of this type is returned as a trait impl by a method
418 pub(crate) extern "C" fn Scorer_free_void(this_ptr: *mut c_void) {
419         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScorer); }
420 }
421 #[allow(unused)]
422 impl Scorer {
423         pub(crate) fn get_native_ref(&self) -> &'static nativeScorer {
424                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
425         }
426         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScorer {
427                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
428         }
429         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
430         pub(crate) fn take_inner(mut self) -> *mut nativeScorer {
431                 assert!(self.is_owned);
432                 let ret = ObjOps::untweak_ptr(self.inner);
433                 self.inner = core::ptr::null_mut();
434                 ret
435         }
436 }
437
438 use lightning::routing::scoring::ScoringParameters as nativeScoringParametersImport;
439 pub(crate) type nativeScoringParameters = nativeScoringParametersImport;
440
441 /// Parameters for configuring [`Scorer`].
442 #[must_use]
443 #[repr(C)]
444 pub struct ScoringParameters {
445         /// A pointer to the opaque Rust object.
446
447         /// Nearly everywhere, inner must be non-null, however in places where
448         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
449         pub inner: *mut nativeScoringParameters,
450         /// Indicates that this is the only struct which contains the same pointer.
451
452         /// Rust functions which take ownership of an object provided via an argument require
453         /// this to be true and invalidate the object pointed to by inner.
454         pub is_owned: bool,
455 }
456
457 impl Drop for ScoringParameters {
458         fn drop(&mut self) {
459                 if self.is_owned && !<*mut nativeScoringParameters>::is_null(self.inner) {
460                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
461                 }
462         }
463 }
464 /// Frees any resources used by the ScoringParameters, if is_owned is set and inner is non-NULL.
465 #[no_mangle]
466 pub extern "C" fn ScoringParameters_free(this_obj: ScoringParameters) { }
467 #[allow(unused)]
468 /// Used only if an object of this type is returned as a trait impl by a method
469 pub(crate) extern "C" fn ScoringParameters_free_void(this_ptr: *mut c_void) {
470         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScoringParameters); }
471 }
472 #[allow(unused)]
473 impl ScoringParameters {
474         pub(crate) fn get_native_ref(&self) -> &'static nativeScoringParameters {
475                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
476         }
477         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScoringParameters {
478                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
479         }
480         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
481         pub(crate) fn take_inner(mut self) -> *mut nativeScoringParameters {
482                 assert!(self.is_owned);
483                 let ret = ObjOps::untweak_ptr(self.inner);
484                 self.inner = core::ptr::null_mut();
485                 ret
486         }
487 }
488 /// A fixed penalty in msats to apply to each channel.
489 ///
490 /// Default value: 500 msat
491 #[no_mangle]
492 pub extern "C" fn ScoringParameters_get_base_penalty_msat(this_ptr: &ScoringParameters) -> u64 {
493         let mut inner_val = &mut this_ptr.get_native_mut_ref().base_penalty_msat;
494         *inner_val
495 }
496 /// A fixed penalty in msats to apply to each channel.
497 ///
498 /// Default value: 500 msat
499 #[no_mangle]
500 pub extern "C" fn ScoringParameters_set_base_penalty_msat(this_ptr: &mut ScoringParameters, mut val: u64) {
501         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.base_penalty_msat = val;
502 }
503 /// A penalty in msats to apply to a channel upon failing to relay a payment.
504 ///
505 /// This accumulates for each failure but may be reduced over time based on
506 /// [`failure_penalty_half_life`] or when successfully routing through a channel.
507 ///
508 /// Default value: 1,024,000 msat
509 ///
510 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
511 #[no_mangle]
512 pub extern "C" fn ScoringParameters_get_failure_penalty_msat(this_ptr: &ScoringParameters) -> u64 {
513         let mut inner_val = &mut this_ptr.get_native_mut_ref().failure_penalty_msat;
514         *inner_val
515 }
516 /// A penalty in msats to apply to a channel upon failing to relay a payment.
517 ///
518 /// This accumulates for each failure but may be reduced over time based on
519 /// [`failure_penalty_half_life`] or when successfully routing through a channel.
520 ///
521 /// Default value: 1,024,000 msat
522 ///
523 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
524 #[no_mangle]
525 pub extern "C" fn ScoringParameters_set_failure_penalty_msat(this_ptr: &mut ScoringParameters, mut val: u64) {
526         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.failure_penalty_msat = val;
527 }
528 /// When the amount being sent over a channel is this many 1024ths of the total channel
529 /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
530 ///
531 /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
532 ///
533 /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
534 #[no_mangle]
535 pub extern "C" fn ScoringParameters_get_overuse_penalty_start_1024th(this_ptr: &ScoringParameters) -> u16 {
536         let mut inner_val = &mut this_ptr.get_native_mut_ref().overuse_penalty_start_1024th;
537         *inner_val
538 }
539 /// When the amount being sent over a channel is this many 1024ths of the total channel
540 /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
541 ///
542 /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
543 ///
544 /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
545 #[no_mangle]
546 pub extern "C" fn ScoringParameters_set_overuse_penalty_start_1024th(this_ptr: &mut ScoringParameters, mut val: u16) {
547         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.overuse_penalty_start_1024th = val;
548 }
549 /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
550 /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
551 ///
552 /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
553 ///                to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
554 ///
555 /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
556 #[no_mangle]
557 pub extern "C" fn ScoringParameters_get_overuse_penalty_msat_per_1024th(this_ptr: &ScoringParameters) -> u64 {
558         let mut inner_val = &mut this_ptr.get_native_mut_ref().overuse_penalty_msat_per_1024th;
559         *inner_val
560 }
561 /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
562 /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
563 ///
564 /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
565 ///                to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
566 ///
567 /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
568 #[no_mangle]
569 pub extern "C" fn ScoringParameters_set_overuse_penalty_msat_per_1024th(this_ptr: &mut ScoringParameters, mut val: u64) {
570         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.overuse_penalty_msat_per_1024th = val;
571 }
572 /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
573 /// cut in half.
574 ///
575 /// Successfully routing through a channel will immediately cut the penalty in half as well.
576 ///
577 /// Default value: 1 hour
578 ///
579 /// # Note
580 ///
581 /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
582 /// never decay.
583 ///
584 /// [`failure_penalty_msat`]: Self::failure_penalty_msat
585 #[no_mangle]
586 pub extern "C" fn ScoringParameters_get_failure_penalty_half_life(this_ptr: &ScoringParameters) -> u64 {
587         let mut inner_val = &mut this_ptr.get_native_mut_ref().failure_penalty_half_life;
588         inner_val.as_secs()
589 }
590 /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
591 /// cut in half.
592 ///
593 /// Successfully routing through a channel will immediately cut the penalty in half as well.
594 ///
595 /// Default value: 1 hour
596 ///
597 /// # Note
598 ///
599 /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
600 /// never decay.
601 ///
602 /// [`failure_penalty_msat`]: Self::failure_penalty_msat
603 #[no_mangle]
604 pub extern "C" fn ScoringParameters_set_failure_penalty_half_life(this_ptr: &mut ScoringParameters, mut val: u64) {
605         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.failure_penalty_half_life = core::time::Duration::from_secs(val);
606 }
607 /// Constructs a new ScoringParameters given each field
608 #[must_use]
609 #[no_mangle]
610 pub extern "C" fn ScoringParameters_new(mut base_penalty_msat_arg: u64, mut failure_penalty_msat_arg: u64, mut overuse_penalty_start_1024th_arg: u16, mut overuse_penalty_msat_per_1024th_arg: u64, mut failure_penalty_half_life_arg: u64) -> ScoringParameters {
611         ScoringParameters { inner: ObjOps::heap_alloc(nativeScoringParameters {
612                 base_penalty_msat: base_penalty_msat_arg,
613                 failure_penalty_msat: failure_penalty_msat_arg,
614                 overuse_penalty_start_1024th: overuse_penalty_start_1024th_arg,
615                 overuse_penalty_msat_per_1024th: overuse_penalty_msat_per_1024th_arg,
616                 failure_penalty_half_life: core::time::Duration::from_secs(failure_penalty_half_life_arg),
617         }), is_owned: true }
618 }
619 #[no_mangle]
620 /// Serialize the ScoringParameters object into a byte array which can be read by ScoringParameters_read
621 pub extern "C" fn ScoringParameters_write(obj: &ScoringParameters) -> crate::c_types::derived::CVec_u8Z {
622         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
623 }
624 #[no_mangle]
625 pub(crate) extern "C" fn ScoringParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
626         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeScoringParameters) })
627 }
628 #[no_mangle]
629 /// Read a ScoringParameters from a byte array, created by ScoringParameters_write
630 pub extern "C" fn ScoringParameters_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ScoringParametersDecodeErrorZ {
631         let res: Result<lightning::routing::scoring::ScoringParameters, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
632         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::scoring::ScoringParameters { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
633         local_res
634 }
635 /// Creates a new scorer using the given scoring parameters.
636 #[must_use]
637 #[no_mangle]
638 pub extern "C" fn Scorer_new(mut params: crate::lightning::routing::scoring::ScoringParameters) -> Scorer {
639         let mut ret = lightning::routing::scoring::Scorer::new(*unsafe { Box::from_raw(params.take_inner()) });
640         Scorer { inner: ObjOps::heap_alloc(ret), is_owned: true }
641 }
642
643 /// Creates a "default" Scorer. See struct and individual field documentaiton for details on which values are used.
644 #[must_use]
645 #[no_mangle]
646 pub extern "C" fn Scorer_default() -> Scorer {
647         Scorer { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
648 }
649 /// Creates a "default" ScoringParameters. See struct and individual field documentaiton for details on which values are used.
650 #[must_use]
651 #[no_mangle]
652 pub extern "C" fn ScoringParameters_default() -> ScoringParameters {
653         ScoringParameters { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
654 }
655 impl From<nativeScorer> for crate::lightning::routing::scoring::Score {
656         fn from(obj: nativeScorer) -> Self {
657                 let mut rust_obj = Scorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
658                 let mut ret = Scorer_as_Score(&rust_obj);
659                 // 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
660                 rust_obj.inner = core::ptr::null_mut();
661                 ret.free = Some(Scorer_free_void);
662                 ret
663         }
664 }
665 /// Constructs a new Score which calls the relevant methods on this_arg.
666 /// This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
667 #[no_mangle]
668 pub extern "C" fn Scorer_as_Score(this_arg: &Scorer) -> crate::lightning::routing::scoring::Score {
669         crate::lightning::routing::scoring::Score {
670                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
671                 free: None,
672                 channel_penalty_msat: Scorer_Score_channel_penalty_msat,
673                 payment_path_failed: Scorer_Score_payment_path_failed,
674                 payment_path_successful: Scorer_Score_payment_path_successful,
675                 write: Scorer_write_void,
676         }
677 }
678
679 #[must_use]
680 extern "C" fn Scorer_Score_channel_penalty_msat(this_arg: *const c_void, mut short_channel_id: u64, mut send_amt_msat: u64, mut capacity_msat: u64, _source: &crate::lightning::routing::network_graph::NodeId, _target: &crate::lightning::routing::network_graph::NodeId) -> u64 {
681         let mut ret = <nativeScorer as lightning::routing::scoring::Score<>>::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeScorer) }, short_channel_id, send_amt_msat, capacity_msat, _source.get_native_ref(), _target.get_native_ref());
682         ret
683 }
684 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) {
685         let mut local__path = Vec::new(); for mut item in _path.as_slice().iter() { local__path.push( { item.get_native_ref() }); };
686         <nativeScorer as lightning::routing::scoring::Score<>>::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeScorer) }, &local__path[..], short_channel_id)
687 }
688 extern "C" fn Scorer_Score_payment_path_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) {
689         let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); };
690         <nativeScorer as lightning::routing::scoring::Score<>>::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeScorer) }, &local_path[..])
691 }
692
693 #[no_mangle]
694 /// Serialize the Scorer object into a byte array which can be read by Scorer_read
695 pub extern "C" fn Scorer_write(obj: &Scorer) -> crate::c_types::derived::CVec_u8Z {
696         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
697 }
698 #[no_mangle]
699 pub(crate) extern "C" fn Scorer_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
700         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeScorer) })
701 }
702 #[no_mangle]
703 /// Read a Scorer from a byte array, created by Scorer_write
704 pub extern "C" fn Scorer_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ScorerDecodeErrorZ {
705         let res: Result<lightning::routing::scoring::Scorer, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
706         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::scoring::Scorer { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
707         local_res
708 }
709
710 use lightning::routing::scoring::ProbabilisticScorer as nativeProbabilisticScorerImport;
711 pub(crate) type nativeProbabilisticScorer = nativeProbabilisticScorerImport<&'static lightning::routing::network_graph::NetworkGraph>;
712
713 /// [`Score`] implementation using channel success probability distributions.
714 ///
715 /// Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
716 /// and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability
717 /// distributions are defined based on knowledge learned from successful and unsuccessful attempts.
718 /// Then the negative `log10` of the success probability is used to determine the cost of routing a
719 /// specific HTLC amount through a channel.
720 ///
721 /// Knowledge about channel liquidity balances takes the form of upper and lower bounds on the
722 /// possible liquidity. Certainty of the bounds is decreased over time using a decay function. See
723 /// [`ProbabilisticScoringParameters`] for details.
724 ///
725 /// Since the scorer aims to learn the current channel liquidity balances, it works best for nodes
726 /// with high payment volume or that actively probe the [`NetworkGraph`]. Nodes with low payment
727 /// volume are more likely to experience failed payment paths, which would need to be retried.
728 ///
729 /// # Note
730 ///
731 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
732 /// behavior.
733 ///
734 /// [1]: https://arxiv.org/abs/2107.05322
735 #[must_use]
736 #[repr(C)]
737 pub struct ProbabilisticScorer {
738         /// A pointer to the opaque Rust object.
739
740         /// Nearly everywhere, inner must be non-null, however in places where
741         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
742         pub inner: *mut nativeProbabilisticScorer,
743         /// Indicates that this is the only struct which contains the same pointer.
744
745         /// Rust functions which take ownership of an object provided via an argument require
746         /// this to be true and invalidate the object pointed to by inner.
747         pub is_owned: bool,
748 }
749
750 impl Drop for ProbabilisticScorer {
751         fn drop(&mut self) {
752                 if self.is_owned && !<*mut nativeProbabilisticScorer>::is_null(self.inner) {
753                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
754                 }
755         }
756 }
757 /// Frees any resources used by the ProbabilisticScorer, if is_owned is set and inner is non-NULL.
758 #[no_mangle]
759 pub extern "C" fn ProbabilisticScorer_free(this_obj: ProbabilisticScorer) { }
760 #[allow(unused)]
761 /// Used only if an object of this type is returned as a trait impl by a method
762 pub(crate) extern "C" fn ProbabilisticScorer_free_void(this_ptr: *mut c_void) {
763         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeProbabilisticScorer); }
764 }
765 #[allow(unused)]
766 impl ProbabilisticScorer {
767         pub(crate) fn get_native_ref(&self) -> &'static nativeProbabilisticScorer {
768                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
769         }
770         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeProbabilisticScorer {
771                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
772         }
773         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
774         pub(crate) fn take_inner(mut self) -> *mut nativeProbabilisticScorer {
775                 assert!(self.is_owned);
776                 let ret = ObjOps::untweak_ptr(self.inner);
777                 self.inner = core::ptr::null_mut();
778                 ret
779         }
780 }
781
782 use lightning::routing::scoring::ProbabilisticScoringParameters as nativeProbabilisticScoringParametersImport;
783 pub(crate) type nativeProbabilisticScoringParameters = nativeProbabilisticScoringParametersImport;
784
785 /// Parameters for configuring [`ProbabilisticScorer`].
786 #[must_use]
787 #[repr(C)]
788 pub struct ProbabilisticScoringParameters {
789         /// A pointer to the opaque Rust object.
790
791         /// Nearly everywhere, inner must be non-null, however in places where
792         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
793         pub inner: *mut nativeProbabilisticScoringParameters,
794         /// Indicates that this is the only struct which contains the same pointer.
795
796         /// Rust functions which take ownership of an object provided via an argument require
797         /// this to be true and invalidate the object pointed to by inner.
798         pub is_owned: bool,
799 }
800
801 impl Drop for ProbabilisticScoringParameters {
802         fn drop(&mut self) {
803                 if self.is_owned && !<*mut nativeProbabilisticScoringParameters>::is_null(self.inner) {
804                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
805                 }
806         }
807 }
808 /// Frees any resources used by the ProbabilisticScoringParameters, if is_owned is set and inner is non-NULL.
809 #[no_mangle]
810 pub extern "C" fn ProbabilisticScoringParameters_free(this_obj: ProbabilisticScoringParameters) { }
811 #[allow(unused)]
812 /// Used only if an object of this type is returned as a trait impl by a method
813 pub(crate) extern "C" fn ProbabilisticScoringParameters_free_void(this_ptr: *mut c_void) {
814         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeProbabilisticScoringParameters); }
815 }
816 #[allow(unused)]
817 impl ProbabilisticScoringParameters {
818         pub(crate) fn get_native_ref(&self) -> &'static nativeProbabilisticScoringParameters {
819                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
820         }
821         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeProbabilisticScoringParameters {
822                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
823         }
824         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
825         pub(crate) fn take_inner(mut self) -> *mut nativeProbabilisticScoringParameters {
826                 assert!(self.is_owned);
827                 let ret = ObjOps::untweak_ptr(self.inner);
828                 self.inner = core::ptr::null_mut();
829                 ret
830         }
831 }
832 /// A multiplier used to determine the amount in msats willing to be paid to avoid routing
833 /// through a channel, as per multiplying by the negative `log10` of the channel's success
834 /// probability for a payment.
835 ///
836 /// The success probability is determined by the effective channel capacity, the payment amount,
837 /// and knowledge learned from prior successful and unsuccessful payments. The lower bound of
838 /// the success probability is 0.01, effectively limiting the penalty to the range
839 /// `0..=2*liquidity_penalty_multiplier_msat`. The knowledge learned is decayed over time based
840 /// on [`liquidity_offset_half_life`].
841 ///
842 /// Default value: 10,000 msat
843 ///
844 /// [`liquidity_offset_half_life`]: Self::liquidity_offset_half_life
845 #[no_mangle]
846 pub extern "C" fn ProbabilisticScoringParameters_get_liquidity_penalty_multiplier_msat(this_ptr: &ProbabilisticScoringParameters) -> u64 {
847         let mut inner_val = &mut this_ptr.get_native_mut_ref().liquidity_penalty_multiplier_msat;
848         *inner_val
849 }
850 /// A multiplier used to determine the amount in msats willing to be paid to avoid routing
851 /// through a channel, as per multiplying by the negative `log10` of the channel's success
852 /// probability for a payment.
853 ///
854 /// The success probability is determined by the effective channel capacity, the payment amount,
855 /// and knowledge learned from prior successful and unsuccessful payments. The lower bound of
856 /// the success probability is 0.01, effectively limiting the penalty to the range
857 /// `0..=2*liquidity_penalty_multiplier_msat`. The knowledge learned is decayed over time based
858 /// on [`liquidity_offset_half_life`].
859 ///
860 /// Default value: 10,000 msat
861 ///
862 /// [`liquidity_offset_half_life`]: Self::liquidity_offset_half_life
863 #[no_mangle]
864 pub extern "C" fn ProbabilisticScoringParameters_set_liquidity_penalty_multiplier_msat(this_ptr: &mut ProbabilisticScoringParameters, mut val: u64) {
865         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.liquidity_penalty_multiplier_msat = val;
866 }
867 /// The time required to elapse before any knowledge learned about channel liquidity balances is
868 /// cut in half.
869 ///
870 /// The bounds are defined in terms of offsets and are initially zero. Increasing the offsets
871 /// gives tighter bounds on the channel liquidity balance. Thus, halving the offsets decreases
872 /// the certainty of the channel liquidity balance.
873 ///
874 /// Default value: 1 hour
875 ///
876 /// # Note
877 ///
878 /// When built with the `no-std` feature, time will never elapse. Therefore, the channel
879 /// liquidity knowledge will never decay except when the bounds cross.
880 #[no_mangle]
881 pub extern "C" fn ProbabilisticScoringParameters_get_liquidity_offset_half_life(this_ptr: &ProbabilisticScoringParameters) -> u64 {
882         let mut inner_val = &mut this_ptr.get_native_mut_ref().liquidity_offset_half_life;
883         inner_val.as_secs()
884 }
885 /// The time required to elapse before any knowledge learned about channel liquidity balances is
886 /// cut in half.
887 ///
888 /// The bounds are defined in terms of offsets and are initially zero. Increasing the offsets
889 /// gives tighter bounds on the channel liquidity balance. Thus, halving the offsets decreases
890 /// the certainty of the channel liquidity balance.
891 ///
892 /// Default value: 1 hour
893 ///
894 /// # Note
895 ///
896 /// When built with the `no-std` feature, time will never elapse. Therefore, the channel
897 /// liquidity knowledge will never decay except when the bounds cross.
898 #[no_mangle]
899 pub extern "C" fn ProbabilisticScoringParameters_set_liquidity_offset_half_life(this_ptr: &mut ProbabilisticScoringParameters, mut val: u64) {
900         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.liquidity_offset_half_life = core::time::Duration::from_secs(val);
901 }
902 /// Constructs a new ProbabilisticScoringParameters given each field
903 #[must_use]
904 #[no_mangle]
905 pub extern "C" fn ProbabilisticScoringParameters_new(mut liquidity_penalty_multiplier_msat_arg: u64, mut liquidity_offset_half_life_arg: u64) -> ProbabilisticScoringParameters {
906         ProbabilisticScoringParameters { inner: ObjOps::heap_alloc(nativeProbabilisticScoringParameters {
907                 liquidity_penalty_multiplier_msat: liquidity_penalty_multiplier_msat_arg,
908                 liquidity_offset_half_life: core::time::Duration::from_secs(liquidity_offset_half_life_arg),
909         }), is_owned: true }
910 }
911 impl Clone for ProbabilisticScoringParameters {
912         fn clone(&self) -> Self {
913                 Self {
914                         inner: if <*mut nativeProbabilisticScoringParameters>::is_null(self.inner) { core::ptr::null_mut() } else {
915                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
916                         is_owned: true,
917                 }
918         }
919 }
920 #[allow(unused)]
921 /// Used only if an object of this type is returned as a trait impl by a method
922 pub(crate) extern "C" fn ProbabilisticScoringParameters_clone_void(this_ptr: *const c_void) -> *mut c_void {
923         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeProbabilisticScoringParameters)).clone() })) as *mut c_void
924 }
925 #[no_mangle]
926 /// Creates a copy of the ProbabilisticScoringParameters
927 pub extern "C" fn ProbabilisticScoringParameters_clone(orig: &ProbabilisticScoringParameters) -> ProbabilisticScoringParameters {
928         orig.clone()
929 }
930 #[no_mangle]
931 /// Serialize the ProbabilisticScoringParameters object into a byte array which can be read by ProbabilisticScoringParameters_read
932 pub extern "C" fn ProbabilisticScoringParameters_write(obj: &ProbabilisticScoringParameters) -> crate::c_types::derived::CVec_u8Z {
933         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
934 }
935 #[no_mangle]
936 pub(crate) extern "C" fn ProbabilisticScoringParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
937         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeProbabilisticScoringParameters) })
938 }
939 #[no_mangle]
940 /// Read a ProbabilisticScoringParameters from a byte array, created by ProbabilisticScoringParameters_write
941 pub extern "C" fn ProbabilisticScoringParameters_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ProbabilisticScoringParametersDecodeErrorZ {
942         let res: Result<lightning::routing::scoring::ProbabilisticScoringParameters, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
943         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::scoring::ProbabilisticScoringParameters { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
944         local_res
945 }
946 /// Creates a new scorer using the given scoring parameters for sending payments from a node
947 /// through a network graph.
948 #[must_use]
949 #[no_mangle]
950 pub extern "C" fn ProbabilisticScorer_new(mut params: crate::lightning::routing::scoring::ProbabilisticScoringParameters, network_graph: &crate::lightning::routing::network_graph::NetworkGraph) -> ProbabilisticScorer {
951         let mut ret = lightning::routing::scoring::ProbabilisticScorer::new(*unsafe { Box::from_raw(params.take_inner()) }, network_graph.get_native_ref());
952         ProbabilisticScorer { inner: ObjOps::heap_alloc(ret), is_owned: true }
953 }
954
955 /// Creates a "default" ProbabilisticScoringParameters. See struct and individual field documentaiton for details on which values are used.
956 #[must_use]
957 #[no_mangle]
958 pub extern "C" fn ProbabilisticScoringParameters_default() -> ProbabilisticScoringParameters {
959         ProbabilisticScoringParameters { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
960 }
961 impl From<nativeProbabilisticScorer> for crate::lightning::routing::scoring::Score {
962         fn from(obj: nativeProbabilisticScorer) -> Self {
963                 let mut rust_obj = ProbabilisticScorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
964                 let mut ret = ProbabilisticScorer_as_Score(&rust_obj);
965                 // 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
966                 rust_obj.inner = core::ptr::null_mut();
967                 ret.free = Some(ProbabilisticScorer_free_void);
968                 ret
969         }
970 }
971 /// Constructs a new Score which calls the relevant methods on this_arg.
972 /// This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
973 #[no_mangle]
974 pub extern "C" fn ProbabilisticScorer_as_Score(this_arg: &ProbabilisticScorer) -> crate::lightning::routing::scoring::Score {
975         crate::lightning::routing::scoring::Score {
976                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
977                 free: None,
978                 channel_penalty_msat: ProbabilisticScorer_Score_channel_penalty_msat,
979                 payment_path_failed: ProbabilisticScorer_Score_payment_path_failed,
980                 payment_path_successful: ProbabilisticScorer_Score_payment_path_successful,
981                 write: ProbabilisticScorer_write_void,
982         }
983 }
984
985 #[must_use]
986 extern "C" fn ProbabilisticScorer_Score_channel_penalty_msat(this_arg: *const c_void, mut short_channel_id: u64, mut amount_msat: u64, mut capacity_msat: u64, source: &crate::lightning::routing::network_graph::NodeId, target: &crate::lightning::routing::network_graph::NodeId) -> u64 {
987         let mut ret = <nativeProbabilisticScorer as lightning::routing::scoring::Score<>>::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, short_channel_id, amount_msat, capacity_msat, source.get_native_ref(), target.get_native_ref());
988         ret
989 }
990 extern "C" fn ProbabilisticScorer_Score_payment_path_failed(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ, mut short_channel_id: u64) {
991         let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); };
992         <nativeProbabilisticScorer as lightning::routing::scoring::Score<>>::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, &local_path[..], short_channel_id)
993 }
994 extern "C" fn ProbabilisticScorer_Score_payment_path_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) {
995         let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); };
996         <nativeProbabilisticScorer as lightning::routing::scoring::Score<>>::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, &local_path[..])
997 }
998
999 #[no_mangle]
1000 /// Serialize the ProbabilisticScorer object into a byte array which can be read by ProbabilisticScorer_read
1001 pub extern "C" fn ProbabilisticScorer_write(obj: &ProbabilisticScorer) -> crate::c_types::derived::CVec_u8Z {
1002         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
1003 }
1004 #[no_mangle]
1005 pub(crate) extern "C" fn ProbabilisticScorer_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1006         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeProbabilisticScorer) })
1007 }
1008 #[no_mangle]
1009 /// Read a ProbabilisticScorer from a byte array, created by ProbabilisticScorer_write
1010 pub extern "C" fn ProbabilisticScorer_read(ser: crate::c_types::u8slice, arg: crate::c_types::derived::C2Tuple_ProbabilisticScoringParametersNetworkGraphZ) -> crate::c_types::derived::CResult_ProbabilisticScorerDecodeErrorZ {
1011         let (mut orig_arg_0, mut orig_arg_1) = arg.to_rust(); let mut local_arg = (*unsafe { Box::from_raw(orig_arg_0.take_inner()) }, orig_arg_1.get_native_ref());
1012         let arg_conv = local_arg;
1013         let res: Result<lightning::routing::scoring::ProbabilisticScorer<&lightning::routing::network_graph::NetworkGraph>, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj_arg(ser, arg_conv);
1014         let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::scoring::ProbabilisticScorer { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
1015         local_res
1016 }
1017 mod time {
1018
1019 use alloc::str::FromStr;
1020 use core::ffi::c_void;
1021 use core::convert::Infallible;
1022 use bitcoin::hashes::Hash;
1023 use crate::c_types::*;
1024 #[cfg(feature="no-std")]
1025 use alloc::{vec::Vec, boxed::Box};
1026
1027 }