1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! Utilities for scoring payment channels.
11 //! [`Scorer`] may be given to [`find_route`] to score payment channels during path finding when a
12 //! custom [`Score`] implementation is not needed.
17 //! # extern crate secp256k1;
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;
25 //! # struct FakeLogger {};
26 //! # impl Logger for FakeLogger {
27 //! # fn log(&self, record: &Record) { unimplemented!() }
29 //! # fn find_scored_route(payer: PublicKey, params: RouteParameters, network_graph: NetworkGraph) {
30 //! # let logger = FakeLogger {};
32 //! // Use the default channel penalties.
33 //! let scorer = Scorer::default();
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()
42 //! let route = find_route(&payer, ¶ms, &network_graph, None, &logger, &scorer);
48 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
49 //! different types and thus is undefined.
51 //! [`find_route`]: crate::routing::router::find_route
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};
61 /// An interface used to score payment channels for path finding.
63 ///\tScoring is in terms of fees willing to be paid in order to avoid routing through a channel.
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`.
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.
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.
80 /// Your code should be overflow-safe through a `channel_capacity_msat` of 21 million BTC.
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)>,
93 unsafe impl Send for Score {}
94 unsafe impl Sync for Score {}
96 pub(crate) extern "C" fn Score_clone_fields(orig: &Score) -> 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),
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())
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 });
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)
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())
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 {
134 fn deref(&self) -> &Self {
138 /// Calls the free function if one is set
140 pub extern "C" fn Score_free(this_ptr: Score) { }
141 impl Drop for Score {
143 if let Some(f) = self.free {
148 /// A scorer that is accessed under a lock.
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.
155 /// [`find_route`]: crate::routing::router::find_route
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.
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)>,
168 unsafe impl Send for LockableScore {}
169 unsafe impl Sync for LockableScore {}
171 pub(crate) extern "C" fn LockableScore_clone_fields(orig: &LockableScore) -> LockableScore {
173 this_arg: orig.this_arg,
174 lock: Clone::clone(&orig.lock),
175 free: Clone::clone(&orig.free),
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);
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 {
192 fn deref(&self) -> &Self {
196 /// Calls the free function if one is set
198 pub extern "C" fn LockableScore_free(this_ptr: LockableScore) { }
199 impl Drop for LockableScore {
201 if let Some(f) = self.free {
207 use lightning::routing::scoring::MultiThreadedLockableScore as nativeMultiThreadedLockableScoreImport;
208 pub(crate) type nativeMultiThreadedLockableScore = nativeMultiThreadedLockableScoreImport<crate::lightning::routing::scoring::Score>;
210 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
213 pub struct MultiThreadedLockableScore {
214 /// A pointer to the opaque Rust object.
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.
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.
226 impl Drop for MultiThreadedLockableScore {
228 if self.is_owned && !<*mut nativeMultiThreadedLockableScore>::is_null(self.inner) {
229 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
233 /// Frees any resources used by the MultiThreadedLockableScore, if is_owned is set and inner is non-NULL.
235 pub extern "C" fn MultiThreadedLockableScore_free(this_obj: MultiThreadedLockableScore) { }
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); }
242 impl MultiThreadedLockableScore {
243 pub(crate) fn get_native_ref(&self) -> &'static nativeMultiThreadedLockableScore {
244 unsafe { &*ObjOps::untweak_ptr(self.inner) }
246 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeMultiThreadedLockableScore {
247 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
257 /// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
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 }
266 use lightning::routing::scoring::Scorer as nativeScorerImport;
267 pub(crate) type nativeScorer = nativeScorerImport;
269 /// [`Score`] implementation that provides reasonable default behavior.
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.
274 /// See [module-level documentation] for usage.
276 /// [module-level documentation]: crate::routing::scoring
280 /// A pointer to the opaque Rust object.
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.
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.
292 impl Drop for Scorer {
294 if self.is_owned && !<*mut nativeScorer>::is_null(self.inner) {
295 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
299 /// Frees any resources used by the Scorer, if is_owned is set and inner is non-NULL.
301 pub extern "C" fn Scorer_free(this_obj: Scorer) { }
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); }
309 pub(crate) fn get_native_ref(&self) -> &'static nativeScorer {
310 unsafe { &*ObjOps::untweak_ptr(self.inner) }
312 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScorer {
313 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
324 use lightning::routing::scoring::ScoringParameters as nativeScoringParametersImport;
325 pub(crate) type nativeScoringParameters = nativeScoringParametersImport;
327 /// Parameters for configuring [`Scorer`].
330 pub struct ScoringParameters {
331 /// A pointer to the opaque Rust object.
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.
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.
343 impl Drop for ScoringParameters {
345 if self.is_owned && !<*mut nativeScoringParameters>::is_null(self.inner) {
346 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
350 /// Frees any resources used by the ScoringParameters, if is_owned is set and inner is non-NULL.
352 pub extern "C" fn ScoringParameters_free(this_obj: ScoringParameters) { }
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); }
359 impl ScoringParameters {
360 pub(crate) fn get_native_ref(&self) -> &'static nativeScoringParameters {
361 unsafe { &*ObjOps::untweak_ptr(self.inner) }
363 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScoringParameters {
364 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
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();
374 /// A fixed penalty in msats to apply to each channel.
376 /// Default value: 500 msat
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;
382 /// A fixed penalty in msats to apply to each channel.
384 /// Default value: 500 msat
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;
389 /// A penalty in msats to apply to a channel upon failing to relay a payment.
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.
394 /// Default value: 1,024,000 msat
396 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
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;
402 /// A penalty in msats to apply to a channel upon failing to relay a payment.
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.
407 /// Default value: 1,024,000 msat
409 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
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;
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`].
417 /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
419 /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
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;
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`].
428 /// Default value: 128 1024ths (i.e. begin penalizing when an HTLC uses 1/8th of a channel)
430 /// [`overuse_penalty_msat_per_1024th`]: Self::overuse_penalty_msat_per_1024th
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;
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.
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)
441 /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
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;
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.
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)
453 /// [`overuse_penalty_start_1024th`]: Self::overuse_penalty_start_1024th
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;
458 /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
461 /// Successfully routing through a channel will immediately cut the penalty in half as well.
465 /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
468 /// [`failure_penalty_msat`]: Self::failure_penalty_msat
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;
474 /// The time required to elapse before any accumulated [`failure_penalty_msat`] penalties are
477 /// Successfully routing through a channel will immediately cut the penalty in half as well.
481 /// When built with the `no-std` feature, time will never elapse. Therefore, this penalty will
484 /// [`failure_penalty_msat`]: Self::failure_penalty_msat
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);
489 /// Constructs a new ScoringParameters given each field
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),
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())
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) })
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() };
517 /// Creates a new scorer using the given scoring parameters.
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 }
525 /// Creates a "default" Scorer. See struct and individual field documentaiton for details on which values are used.
528 pub extern "C" fn Scorer_default() -> Scorer {
529 Scorer { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
531 /// Creates a "default" ScoringParameters. See struct and individual field documentaiton for details on which values are used.
534 pub extern "C" fn ScoringParameters_default() -> ScoringParameters {
535 ScoringParameters { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
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);
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
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 },
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,
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());
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)
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[..])
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())
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) })
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() };
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};