Update auto-generated bindings
[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 //! [`Scorer`] may be given to [`find_route`] to score payment channels during path finding when a
12 //! 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::{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 //! # Note
47 //!
48 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
49 //! different types and thus is undefined.
50 //!
51 //! [`find_route`]: crate::routing::router::find_route
52
53 use alloc::str::FromStr;
54 use core::ffi::c_void;
55 use core::convert::Infallible;
56 use bitcoin::hashes::Hash;
57 use crate::c_types::*;
58 #[cfg(feature="no-std")]
59 use alloc::{vec::Vec, boxed::Box};
60
61 /// An interface used to score payment channels for path finding.
62 ///
63 ///\tScoring is in terms of fees willing to be paid in order to avoid routing through a channel.
64 #[repr(C)]
65 pub struct Score {
66         /// An opaque pointer which is passed to your function implementations as an argument.
67         /// This has no meaning in the LDK, and can be NULL or any other value.
68         pub this_arg: *mut c_void,
69         /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
70         /// given channel in the direction from `source` to `target`.
71         ///
72         /// The channel's capacity (less any other MPP parts which are also being considered for use in
73         /// the same payment) is given by `channel_capacity_msat`. It may be guessed from various
74         /// sources or assumed from no data at all.
75         ///
76         /// For hints provided in the invoice, we assume the channel has sufficient capacity to accept
77         /// the invoice's full amount, and provide a `channel_capacity_msat` of `None`. In all other
78         /// cases it is set to `Some`, even if we're guessing at the channel value.
79         ///
80         /// Your code should be overflow-safe through a `channel_capacity_msat` of 21 million BTC.
81         #[must_use]
82         pub channel_penalty_msat: extern "C" fn (this_arg: *const c_void, short_channel_id: u64, send_amt_msat: u64, channel_capacity_msat: crate::c_types::derived::COption_u64Z, source: &crate::lightning::routing::network_graph::NodeId, target: &crate::lightning::routing::network_graph::NodeId) -> u64,
83         /// Handles updating channel penalties after failing to route through a channel.
84         pub payment_path_failed: extern "C" fn (this_arg: *mut c_void, path: crate::c_types::derived::CVec_RouteHopZ, short_channel_id: u64),
85         /// Handles updating channel penalties after successfully routing along a path.
86         pub payment_path_successful: extern "C" fn (this_arg: *mut c_void, path: crate::c_types::derived::CVec_RouteHopZ),
87         /// Serialize the object into a byte array
88         pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
89         /// Frees any resources associated with this object given its this_arg pointer.
90         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
91         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
92 }
93 unsafe impl Send for Score {}
94 unsafe impl Sync for Score {}
95 #[no_mangle]
96 pub(crate) extern "C" fn Score_clone_fields(orig: &Score) -> Score {
97         Score {
98                 this_arg: orig.this_arg,
99                 channel_penalty_msat: Clone::clone(&orig.channel_penalty_msat),
100                 payment_path_failed: Clone::clone(&orig.payment_path_failed),
101                 payment_path_successful: Clone::clone(&orig.payment_path_successful),
102                 write: Clone::clone(&orig.write),
103                 free: Clone::clone(&orig.free),
104         }
105 }
106 impl lightning::util::ser::Writeable for Score {
107         fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
108                 let vec = (self.write)(self.this_arg);
109                 w.write_all(vec.as_slice())
110         }
111 }
112
113 use lightning::routing::scoring::Score as rustScore;
114 impl rustScore for Score {
115         fn channel_penalty_msat(&self, mut short_channel_id: u64, mut send_amt_msat: u64, mut channel_capacity_msat: Option<u64>, mut source: &lightning::routing::network_graph::NodeId, mut target: &lightning::routing::network_graph::NodeId) -> u64 {
116                 let mut local_channel_capacity_msat = if channel_capacity_msat.is_none() { crate::c_types::derived::COption_u64Z::None } else { crate::c_types::derived::COption_u64Z::Some( { channel_capacity_msat.unwrap() }) };
117                 let mut ret = (self.channel_penalty_msat)(self.this_arg, short_channel_id, send_amt_msat, local_channel_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 });
118                 ret
119         }
120         fn payment_path_failed(&mut self, mut path: &[&lightning::routing::router::RouteHop], mut short_channel_id: u64) {
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_failed)(self.this_arg, local_path.into(), short_channel_id)
123         }
124         fn payment_path_successful(&mut self, mut path: &[&lightning::routing::router::RouteHop]) {
125                 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 } }); };
126                 (self.payment_path_successful)(self.this_arg, local_path.into())
127         }
128 }
129
130 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
131 // directly as a Deref trait in higher-level structs:
132 impl core::ops::Deref for Score {
133         type Target = Self;
134         fn deref(&self) -> &Self {
135                 self
136         }
137 }
138 /// Calls the free function if one is set
139 #[no_mangle]
140 pub extern "C" fn Score_free(this_ptr: Score) { }
141 impl Drop for Score {
142         fn drop(&mut self) {
143                 if let Some(f) = self.free {
144                         f(self.this_arg);
145                 }
146         }
147 }
148 /// A scorer that is accessed under a lock.
149 ///
150 /// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
151 /// having shared ownership of a scorer but without requiring internal locking in [`Score`]
152 /// implementations. Internal locking would be detrimental to route finding performance and could
153 /// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
154 ///
155 /// [`find_route`]: crate::routing::router::find_route
156 #[repr(C)]
157 pub struct LockableScore {
158         /// An opaque pointer which is passed to your function implementations as an argument.
159         /// This has no meaning in the LDK, and can be NULL or any other value.
160         pub this_arg: *mut c_void,
161         /// Returns the locked scorer.
162         #[must_use]
163         pub lock: extern "C" fn (this_arg: *const c_void) -> crate::lightning::routing::scoring::Score,
164         /// Frees any resources associated with this object given its this_arg pointer.
165         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
166         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
167 }
168 unsafe impl Send for LockableScore {}
169 unsafe impl Sync for LockableScore {}
170 #[no_mangle]
171 pub(crate) extern "C" fn LockableScore_clone_fields(orig: &LockableScore) -> LockableScore {
172         LockableScore {
173                 this_arg: orig.this_arg,
174                 lock: Clone::clone(&orig.lock),
175                 free: Clone::clone(&orig.free),
176         }
177 }
178
179 use lightning::routing::scoring::LockableScore as rustLockableScore;
180 impl<'a> rustLockableScore<'a> for LockableScore {
181         type Locked = crate::lightning::routing::scoring::Score;
182         fn lock(&'a self) -> crate::lightning::routing::scoring::Score {
183                 let mut ret = (self.lock)(self.this_arg);
184                 ret
185         }
186 }
187
188 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
189 // directly as a Deref trait in higher-level structs:
190 impl core::ops::Deref for LockableScore {
191         type Target = Self;
192         fn deref(&self) -> &Self {
193                 self
194         }
195 }
196 /// Calls the free function if one is set
197 #[no_mangle]
198 pub extern "C" fn LockableScore_free(this_ptr: LockableScore) { }
199 impl Drop for LockableScore {
200         fn drop(&mut self) {
201                 if let Some(f) = self.free {
202                         f(self.this_arg);
203                 }
204         }
205 }
206
207 use lightning::routing::scoring::MultiThreadedLockableScore as nativeMultiThreadedLockableScoreImport;
208 pub(crate) type nativeMultiThreadedLockableScore = nativeMultiThreadedLockableScoreImport<crate::lightning::routing::scoring::Score>;
209
210 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
211 #[must_use]
212 #[repr(C)]
213 pub struct MultiThreadedLockableScore {
214         /// A pointer to the opaque Rust object.
215
216         /// Nearly everywhere, inner must be non-null, however in places where
217         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
218         pub inner: *mut nativeMultiThreadedLockableScore,
219         /// Indicates that this is the only struct which contains the same pointer.
220
221         /// Rust functions which take ownership of an object provided via an argument require
222         /// this to be true and invalidate the object pointed to by inner.
223         pub is_owned: bool,
224 }
225
226 impl Drop for MultiThreadedLockableScore {
227         fn drop(&mut self) {
228                 if self.is_owned && !<*mut nativeMultiThreadedLockableScore>::is_null(self.inner) {
229                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
230                 }
231         }
232 }
233 /// Frees any resources used by the MultiThreadedLockableScore, if is_owned is set and inner is non-NULL.
234 #[no_mangle]
235 pub extern "C" fn MultiThreadedLockableScore_free(this_obj: MultiThreadedLockableScore) { }
236 #[allow(unused)]
237 /// Used only if an object of this type is returned as a trait impl by a method
238 pub(crate) extern "C" fn MultiThreadedLockableScore_free_void(this_ptr: *mut c_void) {
239         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMultiThreadedLockableScore); }
240 }
241 #[allow(unused)]
242 impl MultiThreadedLockableScore {
243         pub(crate) fn get_native_ref(&self) -> &'static nativeMultiThreadedLockableScore {
244                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
245         }
246         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeMultiThreadedLockableScore {
247                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
248         }
249         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
250         pub(crate) fn take_inner(mut self) -> *mut nativeMultiThreadedLockableScore {
251                 assert!(self.is_owned);
252                 let ret = ObjOps::untweak_ptr(self.inner);
253                 self.inner = core::ptr::null_mut();
254                 ret
255         }
256 }
257 /// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
258 #[must_use]
259 #[no_mangle]
260 pub extern "C" fn MultiThreadedLockableScore_new(mut score: crate::lightning::routing::scoring::Score) -> MultiThreadedLockableScore {
261         let mut ret = lightning::routing::scoring::MultiThreadedLockableScore::new(score);
262         MultiThreadedLockableScore { inner: ObjOps::heap_alloc(ret), is_owned: true }
263 }
264
265
266 use lightning::routing::scoring::Scorer as nativeScorerImport;
267 pub(crate) type nativeScorer = nativeScorerImport;
268
269 /// [`Score`] implementation that provides reasonable default behavior.
270 ///
271 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
272 /// slightly higher fees are available. Will further penalize channels that fail to relay payments.
273 ///
274 /// See [module-level documentation] for usage.
275 ///
276 /// [module-level documentation]: crate::routing::scoring
277 #[must_use]
278 #[repr(C)]
279 pub struct Scorer {
280         /// A pointer to the opaque Rust object.
281
282         /// Nearly everywhere, inner must be non-null, however in places where
283         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
284         pub inner: *mut nativeScorer,
285         /// Indicates that this is the only struct which contains the same pointer.
286
287         /// Rust functions which take ownership of an object provided via an argument require
288         /// this to be true and invalidate the object pointed to by inner.
289         pub is_owned: bool,
290 }
291
292 impl Drop for Scorer {
293         fn drop(&mut self) {
294                 if self.is_owned && !<*mut nativeScorer>::is_null(self.inner) {
295                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
296                 }
297         }
298 }
299 /// Frees any resources used by the Scorer, if is_owned is set and inner is non-NULL.
300 #[no_mangle]
301 pub extern "C" fn Scorer_free(this_obj: Scorer) { }
302 #[allow(unused)]
303 /// Used only if an object of this type is returned as a trait impl by a method
304 pub(crate) extern "C" fn Scorer_free_void(this_ptr: *mut c_void) {
305         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScorer); }
306 }
307 #[allow(unused)]
308 impl Scorer {
309         pub(crate) fn get_native_ref(&self) -> &'static nativeScorer {
310                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
311         }
312         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScorer {
313                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
314         }
315         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
316         pub(crate) fn take_inner(mut self) -> *mut nativeScorer {
317                 assert!(self.is_owned);
318                 let ret = ObjOps::untweak_ptr(self.inner);
319                 self.inner = core::ptr::null_mut();
320                 ret
321         }
322 }
323
324 use lightning::routing::scoring::ScoringParameters as nativeScoringParametersImport;
325 pub(crate) type nativeScoringParameters = nativeScoringParametersImport;
326
327 /// Parameters for configuring [`Scorer`].
328 #[must_use]
329 #[repr(C)]
330 pub struct ScoringParameters {
331         /// A pointer to the opaque Rust object.
332
333         /// Nearly everywhere, inner must be non-null, however in places where
334         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
335         pub inner: *mut nativeScoringParameters,
336         /// Indicates that this is the only struct which contains the same pointer.
337
338         /// Rust functions which take ownership of an object provided via an argument require
339         /// this to be true and invalidate the object pointed to by inner.
340         pub is_owned: bool,
341 }
342
343 impl Drop for ScoringParameters {
344         fn drop(&mut self) {
345                 if self.is_owned && !<*mut nativeScoringParameters>::is_null(self.inner) {
346                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
347                 }
348         }
349 }
350 /// Frees any resources used by the ScoringParameters, if is_owned is set and inner is non-NULL.
351 #[no_mangle]
352 pub extern "C" fn ScoringParameters_free(this_obj: ScoringParameters) { }
353 #[allow(unused)]
354 /// Used only if an object of this type is returned as a trait impl by a method
355 pub(crate) extern "C" fn ScoringParameters_free_void(this_ptr: *mut c_void) {
356         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScoringParameters); }
357 }
358 #[allow(unused)]
359 impl ScoringParameters {
360         pub(crate) fn get_native_ref(&self) -> &'static nativeScoringParameters {
361                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
362         }
363         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScoringParameters {
364                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
365         }
366         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
367         pub(crate) fn take_inner(mut self) -> *mut nativeScoringParameters {
368                 assert!(self.is_owned);
369                 let ret = ObjOps::untweak_ptr(self.inner);
370                 self.inner = core::ptr::null_mut();
371                 ret
372         }
373 }
374 /// A fixed penalty in msats to apply to each channel.
375 ///
376 /// Default value: 500 msat
377 #[no_mangle]
378 pub extern "C" fn ScoringParameters_get_base_penalty_msat(this_ptr: &ScoringParameters) -> u64 {
379         let mut inner_val = &mut this_ptr.get_native_mut_ref().base_penalty_msat;
380         *inner_val
381 }
382 /// A fixed penalty in msats to apply to each channel.
383 ///
384 /// Default value: 500 msat
385 #[no_mangle]
386 pub extern "C" fn ScoringParameters_set_base_penalty_msat(this_ptr: &mut ScoringParameters, mut val: u64) {
387         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.base_penalty_msat = val;
388 }
389 /// A penalty in msats to apply to a channel upon failing to relay a payment.
390 ///
391 /// This accumulates for each failure but may be reduced over time based on
392 /// [`failure_penalty_half_life`] or when successfully routing through a channel.
393 ///
394 /// Default value: 1,024,000 msat
395 ///
396 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
397 #[no_mangle]
398 pub extern "C" fn ScoringParameters_get_failure_penalty_msat(this_ptr: &ScoringParameters) -> u64 {
399         let mut inner_val = &mut this_ptr.get_native_mut_ref().failure_penalty_msat;
400         *inner_val
401 }
402 /// A penalty in msats to apply to a channel upon failing to relay a payment.
403 ///
404 /// This accumulates for each failure but may be reduced over time based on
405 /// [`failure_penalty_half_life`] or when successfully routing through a channel.
406 ///
407 /// Default value: 1,024,000 msat
408 ///
409 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
410 #[no_mangle]
411 pub extern "C" fn ScoringParameters_set_failure_penalty_msat(this_ptr: &mut ScoringParameters, mut val: u64) {
412         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.failure_penalty_msat = val;
413 }
414 /// When the amount being sent over a channel is this many 1024ths of the total channel
415 /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
416 ///
417 /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
418 ///
419 /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
420 #[no_mangle]
421 pub extern "C" fn ScoringParameters_get_overuse_penalty_start_1024th(this_ptr: &ScoringParameters) -> u16 {
422         let mut inner_val = &mut this_ptr.get_native_mut_ref().overuse_penalty_start_1024th;
423         *inner_val
424 }
425 /// When the amount being sent over a channel is this many 1024ths of the total channel
426 /// capacity, we begin applying [`overuse_penalty_msat_per_1024th`].
427 ///
428 /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
429 ///
430 /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
431 #[no_mangle]
432 pub extern "C" fn ScoringParameters_set_overuse_penalty_start_1024th(this_ptr: &mut ScoringParameters, mut val: u16) {
433         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.overuse_penalty_start_1024th = val;
434 }
435 /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
436 /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
437 ///
438 /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
439 ///                to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
440 ///
441 /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
442 #[no_mangle]
443 pub extern "C" fn ScoringParameters_get_overuse_penalty_msat_per_1024th(this_ptr: &ScoringParameters) -> u64 {
444         let mut inner_val = &mut this_ptr.get_native_mut_ref().overuse_penalty_msat_per_1024th;
445         *inner_val
446 }
447 /// A penalty applied, per whole 1024ths of the channel capacity which the amount being sent
448 /// over the channel exceeds [`overuse_penalty_start_1024th`] by.
449 ///
450 /// Default value: 20 msat (i.e. 2560 msat penalty to use 1/4th of a channel, 7680 msat penalty
451 ///                to use half a channel, and 12,560 msat penalty to use 3/4ths of a channel)
452 ///
453 /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
454 #[no_mangle]
455 pub extern "C" fn ScoringParameters_set_overuse_penalty_msat_per_1024th(this_ptr: &mut ScoringParameters, mut val: u64) {
456         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.overuse_penalty_msat_per_1024th = val;
457 }
458 /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
459 /// cut in half.
460 ///
461 /// Successfully routing through a channel will immediately cut the penalty in half as well.
462 ///
463 /// # Note
464 ///
465 /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
466 /// never decay.
467 ///
468 /// [`failure_penalty_msat`]: Self::failure_penalty_msat
469 #[no_mangle]
470 pub extern "C" fn ScoringParameters_get_failure_penalty_half_life(this_ptr: &ScoringParameters) -> u64 {
471         let mut inner_val = &mut this_ptr.get_native_mut_ref().failure_penalty_half_life;
472         inner_val.as_secs()
473 }
474 /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
475 /// cut in half.
476 ///
477 /// Successfully routing through a channel will immediately cut the penalty in half as well.
478 ///
479 /// # Note
480 ///
481 /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
482 /// never decay.
483 ///
484 /// [`failure_penalty_msat`]: Self::failure_penalty_msat
485 #[no_mangle]
486 pub extern "C" fn ScoringParameters_set_failure_penalty_half_life(this_ptr: &mut ScoringParameters, mut val: u64) {
487         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.failure_penalty_half_life = core::time::Duration::from_secs(val);
488 }
489 /// Constructs a new ScoringParameters given each field
490 #[must_use]
491 #[no_mangle]
492 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 {
493         ScoringParameters { inner: ObjOps::heap_alloc(nativeScoringParameters {
494                 base_penalty_msat: base_penalty_msat_arg,
495                 failure_penalty_msat: failure_penalty_msat_arg,
496                 overuse_penalty_start_1024th: overuse_penalty_start_1024th_arg,
497                 overuse_penalty_msat_per_1024th: overuse_penalty_msat_per_1024th_arg,
498                 failure_penalty_half_life: core::time::Duration::from_secs(failure_penalty_half_life_arg),
499         }), is_owned: true }
500 }
501 #[no_mangle]
502 /// Serialize the ScoringParameters object into a byte array which can be read by ScoringParameters_read
503 pub extern "C" fn ScoringParameters_write(obj: &ScoringParameters) -> crate::c_types::derived::CVec_u8Z {
504         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
505 }
506 #[no_mangle]
507 pub(crate) extern "C" fn ScoringParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
508         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeScoringParameters) })
509 }
510 #[no_mangle]
511 /// Read a ScoringParameters from a byte array, created by ScoringParameters_write
512 pub extern "C" fn ScoringParameters_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ScoringParametersDecodeErrorZ {
513         let res: Result<lightning::routing::scoring::ScoringParameters, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
514         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() };
515         local_res
516 }
517 /// Creates a new scorer using the given scoring parameters.
518 #[must_use]
519 #[no_mangle]
520 pub extern "C" fn Scorer_new(mut params: crate::lightning::routing::scoring::ScoringParameters) -> Scorer {
521         let mut ret = lightning::routing::scoring::Scorer::new(*unsafe { Box::from_raw(params.take_inner()) });
522         Scorer { inner: ObjOps::heap_alloc(ret), is_owned: true }
523 }
524
525 /// Creates a "default" Scorer. See struct and individual field documentaiton for details on which values are used.
526 #[must_use]
527 #[no_mangle]
528 pub extern "C" fn Scorer_default() -> Scorer {
529         Scorer { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
530 }
531 /// Creates a "default" ScoringParameters. See struct and individual field documentaiton for details on which values are used.
532 #[must_use]
533 #[no_mangle]
534 pub extern "C" fn ScoringParameters_default() -> ScoringParameters {
535         ScoringParameters { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
536 }
537 impl From<nativeScorer> for crate::lightning::routing::scoring::Score {
538         fn from(obj: nativeScorer) -> Self {
539                 let mut rust_obj = Scorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
540                 let mut ret = Scorer_as_Score(&rust_obj);
541                 // 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
542                 rust_obj.inner = core::ptr::null_mut();
543                 ret.free = Some(Scorer_free_void);
544                 ret
545         }
546 }
547 /// Constructs a new Score which calls the relevant methods on this_arg.
548 /// This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
549 #[no_mangle]
550 pub extern "C" fn Scorer_as_Score(this_arg: &Scorer) -> crate::lightning::routing::scoring::Score {
551         crate::lightning::routing::scoring::Score {
552                 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
553                 free: None,
554                 channel_penalty_msat: Scorer_Score_channel_penalty_msat,
555                 payment_path_failed: Scorer_Score_payment_path_failed,
556                 payment_path_successful: Scorer_Score_payment_path_successful,
557                 write: Scorer_write_void,
558         }
559 }
560
561 #[must_use]
562 extern "C" fn Scorer_Score_channel_penalty_msat(this_arg: *const c_void, mut short_channel_id: u64, mut send_amt_msat: u64, mut chan_capacity_opt: crate::c_types::derived::COption_u64Z, _source: &crate::lightning::routing::network_graph::NodeId, _target: &crate::lightning::routing::network_graph::NodeId) -> u64 {
563         let mut local_chan_capacity_opt = if chan_capacity_opt.is_some() { Some( { chan_capacity_opt.take() }) } else { None };
564         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, local_chan_capacity_opt, _source.get_native_ref(), _target.get_native_ref());
565         ret
566 }
567 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) {
568         let mut local__path = Vec::new(); for mut item in _path.as_slice().iter() { local__path.push( { item.get_native_ref() }); };
569         <nativeScorer as lightning::routing::scoring::Score<>>::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeScorer) }, &local__path[..], short_channel_id)
570 }
571 extern "C" fn Scorer_Score_payment_path_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) {
572         let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); };
573         <nativeScorer as lightning::routing::scoring::Score<>>::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeScorer) }, &local_path[..])
574 }
575
576 #[no_mangle]
577 /// Serialize the Scorer object into a byte array which can be read by Scorer_read
578 pub extern "C" fn Scorer_write(obj: &Scorer) -> crate::c_types::derived::CVec_u8Z {
579         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
580 }
581 #[no_mangle]
582 pub(crate) extern "C" fn Scorer_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
583         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeScorer) })
584 }
585 #[no_mangle]
586 /// Read a Scorer from a byte array, created by Scorer_write
587 pub extern "C" fn Scorer_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_ScorerDecodeErrorZ {
588         let res: Result<lightning::routing::scoring::Scorer, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
589         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() };
590         local_res
591 }
592 mod time {
593
594 use alloc::str::FromStr;
595 use core::ffi::c_void;
596 use core::convert::Infallible;
597 use bitcoin::hashes::Hash;
598 use crate::c_types::*;
599 #[cfg(feature="no-std")]
600 use alloc::{vec::Vec, boxed::Box};
601
602 }