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 //! [`ProbabilisticScorer`] may be given to [`find_route`] to score payment channels during path
12 //! finding when a custom [`ScoreLookUp`] implementation is not needed.
17 //! # extern crate bitcoin;
19 //! # use lightning::routing::gossip::NetworkGraph;
20 //! # use lightning::routing::router::{RouteParameters, find_route};
21 //! # use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringFeeParameters, ProbabilisticScoringDecayParameters};
22 //! # use lightning::sign::KeysManager;
23 //! # use lightning::util::logger::{Logger, Record};
24 //! # use bitcoin::secp256k1::PublicKey;
26 //! # struct FakeLogger {};
27 //! # impl Logger for FakeLogger {
28 //! # fn log(&self, record: &Record) { unimplemented!() }
30 //! # fn find_scored_route(payer: PublicKey, route_params: RouteParameters, network_graph: NetworkGraph<&FakeLogger>) {
31 //! # let logger = FakeLogger {};
33 //! // Use the default channel penalties.
34 //! let params = ProbabilisticScoringFeeParameters::default();
35 //! let decay_params = ProbabilisticScoringDecayParameters::default();
36 //! let scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
38 //! // Or use custom channel penalties.
39 //! let params = ProbabilisticScoringFeeParameters {
40 //! \tliquidity_penalty_multiplier_msat: 2 * 1000,
41 //! \t..ProbabilisticScoringFeeParameters::default()
43 //! let decay_params = ProbabilisticScoringDecayParameters::default();
44 //! let scorer = ProbabilisticScorer::new(decay_params, &network_graph, &logger);
45 //! # let random_seed_bytes = [42u8; 32];
47 //! let route = find_route(&payer, &route_params, &network_graph, None, &logger, &scorer, ¶ms, &random_seed_bytes);
53 //! Persisting when built with feature `no-std` and restoring without it, or vice versa, uses
54 //! different types and thus is undefined.
56 //! [`find_route`]: crate::routing::router::find_route
58 use alloc::str::FromStr;
59 use alloc::string::String;
60 use core::ffi::c_void;
61 use core::convert::Infallible;
62 use bitcoin::hashes::Hash;
63 use crate::c_types::*;
64 #[cfg(feature="no-std")]
65 use alloc::{vec::Vec, boxed::Box};
67 /// An interface used to score payment channels for path finding.
69 /// `ScoreLookUp` is used to determine the penalty for a given channel.
71 /// Scoring is in terms of fees willing to be paid in order to avoid routing through a channel.
73 pub struct ScoreLookUp {
74 /// An opaque pointer which is passed to your function implementations as an argument.
75 /// This has no meaning in the LDK, and can be NULL or any other value.
76 pub this_arg: *mut c_void,
77 /// Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
78 /// given channel in the direction from `source` to `target`.
80 /// The channel's capacity (less any other MPP parts that are also being considered for use in
81 /// the same payment) is given by `capacity_msat`. It may be determined from various sources
82 /// such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
83 /// [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
84 /// Thus, implementations should be overflow-safe.
85 pub channel_penalty_msat: extern "C" fn (this_arg: *const c_void, short_channel_id: u64, source: &crate::lightning::routing::gossip::NodeId, target: &crate::lightning::routing::gossip::NodeId, usage: crate::lightning::routing::scoring::ChannelUsage, score_params: &crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters) -> u64,
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)>,
90 unsafe impl Send for ScoreLookUp {}
91 unsafe impl Sync for ScoreLookUp {}
92 pub(crate) fn ScoreLookUp_clone_fields(orig: &ScoreLookUp) -> ScoreLookUp {
94 this_arg: orig.this_arg,
95 channel_penalty_msat: Clone::clone(&orig.channel_penalty_msat),
96 free: Clone::clone(&orig.free),
100 use lightning::routing::scoring::ScoreLookUp as rustScoreLookUp;
101 impl rustScoreLookUp for ScoreLookUp {
102 fn channel_penalty_msat(&self, mut short_channel_id: u64, mut source: &lightning::routing::gossip::NodeId, mut target: &lightning::routing::gossip::NodeId, mut usage: lightning::routing::scoring::ChannelUsage, mut score_params: &lightning::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
103 let mut ret = (self.channel_penalty_msat)(self.this_arg, short_channel_id, &crate::lightning::routing::gossip::NodeId { inner: unsafe { ObjOps::nonnull_ptr_to_inner((source as *const lightning::routing::gossip::NodeId<>) as *mut _) }, is_owned: false }, &crate::lightning::routing::gossip::NodeId { inner: unsafe { ObjOps::nonnull_ptr_to_inner((target as *const lightning::routing::gossip::NodeId<>) as *mut _) }, is_owned: false }, crate::lightning::routing::scoring::ChannelUsage { inner: ObjOps::heap_alloc(usage), is_owned: true }, &crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters { inner: unsafe { ObjOps::nonnull_ptr_to_inner((score_params as *const lightning::routing::scoring::ProbabilisticScoringFeeParameters<>) as *mut _) }, is_owned: false });
108 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
109 // directly as a Deref trait in higher-level structs:
110 impl core::ops::Deref for ScoreLookUp {
112 fn deref(&self) -> &Self {
116 impl core::ops::DerefMut for ScoreLookUp {
117 fn deref_mut(&mut self) -> &mut Self {
121 /// Calls the free function if one is set
123 pub extern "C" fn ScoreLookUp_free(this_ptr: ScoreLookUp) { }
124 impl Drop for ScoreLookUp {
126 if let Some(f) = self.free {
131 /// `ScoreUpdate` is used to update the scorer's internal state after a payment attempt.
133 pub struct ScoreUpdate {
134 /// An opaque pointer which is passed to your function implementations as an argument.
135 /// This has no meaning in the LDK, and can be NULL or any other value.
136 pub this_arg: *mut c_void,
137 /// Handles updating channel penalties after failing to route through a channel.
138 pub payment_path_failed: extern "C" fn (this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, short_channel_id: u64),
139 /// Handles updating channel penalties after successfully routing along a path.
140 pub payment_path_successful: extern "C" fn (this_arg: *mut c_void, path: &crate::lightning::routing::router::Path),
141 /// Handles updating channel penalties after a probe over the given path failed.
142 pub probe_failed: extern "C" fn (this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, short_channel_id: u64),
143 /// Handles updating channel penalties after a probe over the given path succeeded.
144 pub probe_successful: extern "C" fn (this_arg: *mut c_void, path: &crate::lightning::routing::router::Path),
145 /// Frees any resources associated with this object given its this_arg pointer.
146 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
147 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
149 unsafe impl Send for ScoreUpdate {}
150 unsafe impl Sync for ScoreUpdate {}
151 pub(crate) fn ScoreUpdate_clone_fields(orig: &ScoreUpdate) -> ScoreUpdate {
153 this_arg: orig.this_arg,
154 payment_path_failed: Clone::clone(&orig.payment_path_failed),
155 payment_path_successful: Clone::clone(&orig.payment_path_successful),
156 probe_failed: Clone::clone(&orig.probe_failed),
157 probe_successful: Clone::clone(&orig.probe_successful),
158 free: Clone::clone(&orig.free),
162 use lightning::routing::scoring::ScoreUpdate as rustScoreUpdate;
163 impl rustScoreUpdate for ScoreUpdate {
164 fn payment_path_failed(&mut self, mut path: &lightning::routing::router::Path, mut short_channel_id: u64) {
165 (self.payment_path_failed)(self.this_arg, &crate::lightning::routing::router::Path { inner: unsafe { ObjOps::nonnull_ptr_to_inner((path as *const lightning::routing::router::Path<>) as *mut _) }, is_owned: false }, short_channel_id)
167 fn payment_path_successful(&mut self, mut path: &lightning::routing::router::Path) {
168 (self.payment_path_successful)(self.this_arg, &crate::lightning::routing::router::Path { inner: unsafe { ObjOps::nonnull_ptr_to_inner((path as *const lightning::routing::router::Path<>) as *mut _) }, is_owned: false })
170 fn probe_failed(&mut self, mut path: &lightning::routing::router::Path, mut short_channel_id: u64) {
171 (self.probe_failed)(self.this_arg, &crate::lightning::routing::router::Path { inner: unsafe { ObjOps::nonnull_ptr_to_inner((path as *const lightning::routing::router::Path<>) as *mut _) }, is_owned: false }, short_channel_id)
173 fn probe_successful(&mut self, mut path: &lightning::routing::router::Path) {
174 (self.probe_successful)(self.this_arg, &crate::lightning::routing::router::Path { inner: unsafe { ObjOps::nonnull_ptr_to_inner((path as *const lightning::routing::router::Path<>) as *mut _) }, is_owned: false })
178 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
179 // directly as a Deref trait in higher-level structs:
180 impl core::ops::Deref for ScoreUpdate {
182 fn deref(&self) -> &Self {
186 impl core::ops::DerefMut for ScoreUpdate {
187 fn deref_mut(&mut self) -> &mut Self {
191 /// Calls the free function if one is set
193 pub extern "C" fn ScoreUpdate_free(this_ptr: ScoreUpdate) { }
194 impl Drop for ScoreUpdate {
196 if let Some(f) = self.free {
201 /// A trait which can both lookup and update routing channel penalty scores.
203 /// This is used in places where both bounds are required and implemented for all types which
204 /// implement [`ScoreLookUp`] and [`ScoreUpdate`].
206 /// Bindings users may need to manually implement this for their custom scoring implementations.
209 /// An opaque pointer which is passed to your function implementations as an argument.
210 /// This has no meaning in the LDK, and can be NULL or any other value.
211 pub this_arg: *mut c_void,
212 /// Implementation of ScoreLookUp for this object.
213 pub ScoreLookUp: crate::lightning::routing::scoring::ScoreLookUp,
214 /// Implementation of ScoreUpdate for this object.
215 pub ScoreUpdate: crate::lightning::routing::scoring::ScoreUpdate,
216 /// Serialize the object into a byte array
217 pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
218 /// Frees any resources associated with this object given its this_arg pointer.
219 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
220 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
222 unsafe impl Send for Score {}
223 unsafe impl Sync for Score {}
224 pub(crate) fn Score_clone_fields(orig: &Score) -> Score {
226 this_arg: orig.this_arg,
227 ScoreLookUp: crate::lightning::routing::scoring::ScoreLookUp_clone_fields(&orig.ScoreLookUp),
228 ScoreUpdate: crate::lightning::routing::scoring::ScoreUpdate_clone_fields(&orig.ScoreUpdate),
229 write: Clone::clone(&orig.write),
230 free: Clone::clone(&orig.free),
233 impl lightning::routing::scoring::ScoreLookUp for Score {
234 fn channel_penalty_msat(&self, mut short_channel_id: u64, mut source: &lightning::routing::gossip::NodeId, mut target: &lightning::routing::gossip::NodeId, mut usage: lightning::routing::scoring::ChannelUsage, mut score_params: &lightning::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
235 let mut ret = (self.ScoreLookUp.channel_penalty_msat)(self.ScoreLookUp.this_arg, short_channel_id, &crate::lightning::routing::gossip::NodeId { inner: unsafe { ObjOps::nonnull_ptr_to_inner((source as *const lightning::routing::gossip::NodeId<>) as *mut _) }, is_owned: false }, &crate::lightning::routing::gossip::NodeId { inner: unsafe { ObjOps::nonnull_ptr_to_inner((target as *const lightning::routing::gossip::NodeId<>) as *mut _) }, is_owned: false }, crate::lightning::routing::scoring::ChannelUsage { inner: ObjOps::heap_alloc(usage), is_owned: true }, &crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters { inner: unsafe { ObjOps::nonnull_ptr_to_inner((score_params as *const lightning::routing::scoring::ProbabilisticScoringFeeParameters<>) as *mut _) }, is_owned: false });
239 impl lightning::routing::scoring::ScoreUpdate for Score {
240 fn payment_path_failed(&mut self, mut path: &lightning::routing::router::Path, mut short_channel_id: u64) {
241 (self.ScoreUpdate.payment_path_failed)(self.ScoreUpdate.this_arg, &crate::lightning::routing::router::Path { inner: unsafe { ObjOps::nonnull_ptr_to_inner((path as *const lightning::routing::router::Path<>) as *mut _) }, is_owned: false }, short_channel_id)
243 fn payment_path_successful(&mut self, mut path: &lightning::routing::router::Path) {
244 (self.ScoreUpdate.payment_path_successful)(self.ScoreUpdate.this_arg, &crate::lightning::routing::router::Path { inner: unsafe { ObjOps::nonnull_ptr_to_inner((path as *const lightning::routing::router::Path<>) as *mut _) }, is_owned: false })
246 fn probe_failed(&mut self, mut path: &lightning::routing::router::Path, mut short_channel_id: u64) {
247 (self.ScoreUpdate.probe_failed)(self.ScoreUpdate.this_arg, &crate::lightning::routing::router::Path { inner: unsafe { ObjOps::nonnull_ptr_to_inner((path as *const lightning::routing::router::Path<>) as *mut _) }, is_owned: false }, short_channel_id)
249 fn probe_successful(&mut self, mut path: &lightning::routing::router::Path) {
250 (self.ScoreUpdate.probe_successful)(self.ScoreUpdate.this_arg, &crate::lightning::routing::router::Path { inner: unsafe { ObjOps::nonnull_ptr_to_inner((path as *const lightning::routing::router::Path<>) as *mut _) }, is_owned: false })
253 impl lightning::util::ser::Writeable for Score {
254 fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
255 let vec = (self.write)(self.this_arg);
256 w.write_all(vec.as_slice())
260 use lightning::routing::scoring::Score as rustScore;
261 impl rustScore for Score {
264 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
265 // directly as a Deref trait in higher-level structs:
266 impl core::ops::Deref for Score {
268 fn deref(&self) -> &Self {
272 impl core::ops::DerefMut for Score {
273 fn deref_mut(&mut self) -> &mut Self {
277 /// Calls the free function if one is set
279 pub extern "C" fn Score_free(this_ptr: Score) { }
280 impl Drop for Score {
282 if let Some(f) = self.free {
287 /// A scorer that is accessed under a lock.
289 /// Needed so that calls to [`ScoreLookUp::channel_penalty_msat`] in [`find_route`] can be made while
290 /// having shared ownership of a scorer but without requiring internal locking in [`ScoreUpdate`]
291 /// implementations. Internal locking would be detrimental to route finding performance and could
292 /// result in [`ScoreLookUp::channel_penalty_msat`] returning a different value for the same channel.
294 /// [`find_route`]: crate::routing::router::find_route
296 pub struct LockableScore {
297 /// An opaque pointer which is passed to your function implementations as an argument.
298 /// This has no meaning in the LDK, and can be NULL or any other value.
299 pub this_arg: *mut c_void,
300 /// Returns read locked scorer.
301 pub read_lock: extern "C" fn (this_arg: *const c_void) -> crate::lightning::routing::scoring::ScoreLookUp,
302 /// Returns write locked scorer.
303 pub write_lock: extern "C" fn (this_arg: *const c_void) -> crate::lightning::routing::scoring::ScoreUpdate,
304 /// Frees any resources associated with this object given its this_arg pointer.
305 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
306 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
308 unsafe impl Send for LockableScore {}
309 unsafe impl Sync for LockableScore {}
310 pub(crate) fn LockableScore_clone_fields(orig: &LockableScore) -> LockableScore {
312 this_arg: orig.this_arg,
313 read_lock: Clone::clone(&orig.read_lock),
314 write_lock: Clone::clone(&orig.write_lock),
315 free: Clone::clone(&orig.free),
319 use lightning::routing::scoring::LockableScore as rustLockableScore;
320 impl<'a> rustLockableScore<'a> for LockableScore {
321 type ScoreUpdate = crate::lightning::routing::scoring::ScoreUpdate;
322 type ScoreLookUp = crate::lightning::routing::scoring::ScoreLookUp;
323 type WriteLocked = crate::lightning::routing::scoring::ScoreUpdate;
324 type ReadLocked = crate::lightning::routing::scoring::ScoreLookUp;
325 fn read_lock(&'a self) -> crate::lightning::routing::scoring::ScoreLookUp {
326 let mut ret = (self.read_lock)(self.this_arg);
329 fn write_lock(&'a self) -> crate::lightning::routing::scoring::ScoreUpdate {
330 let mut ret = (self.write_lock)(self.this_arg);
335 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
336 // directly as a Deref trait in higher-level structs:
337 impl core::ops::Deref for LockableScore {
339 fn deref(&self) -> &Self {
343 impl core::ops::DerefMut for LockableScore {
344 fn deref_mut(&mut self) -> &mut Self {
348 /// Calls the free function if one is set
350 pub extern "C" fn LockableScore_free(this_ptr: LockableScore) { }
351 impl Drop for LockableScore {
353 if let Some(f) = self.free {
358 /// Refers to a scorer that is accessible under lock and also writeable to disk
360 /// We need this trait to be able to pass in a scorer to `lightning-background-processor` that will enable us to
361 /// use the Persister to persist it.
363 pub struct WriteableScore {
364 /// An opaque pointer which is passed to your function implementations as an argument.
365 /// This has no meaning in the LDK, and can be NULL or any other value.
366 pub this_arg: *mut c_void,
367 /// Implementation of LockableScore for this object.
368 pub LockableScore: crate::lightning::routing::scoring::LockableScore,
369 /// Serialize the object into a byte array
370 pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
371 /// Frees any resources associated with this object given its this_arg pointer.
372 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
373 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
375 unsafe impl Send for WriteableScore {}
376 unsafe impl Sync for WriteableScore {}
377 pub(crate) fn WriteableScore_clone_fields(orig: &WriteableScore) -> WriteableScore {
379 this_arg: orig.this_arg,
380 LockableScore: crate::lightning::routing::scoring::LockableScore_clone_fields(&orig.LockableScore),
381 write: Clone::clone(&orig.write),
382 free: Clone::clone(&orig.free),
385 impl<'a> lightning::routing::scoring::LockableScore<'a> for WriteableScore {
386 type ScoreUpdate = crate::lightning::routing::scoring::ScoreUpdate;
387 type ScoreLookUp = crate::lightning::routing::scoring::ScoreLookUp;
388 type WriteLocked = crate::lightning::routing::scoring::ScoreUpdate;
389 type ReadLocked = crate::lightning::routing::scoring::ScoreLookUp;
390 fn read_lock(&'a self) -> crate::lightning::routing::scoring::ScoreLookUp {
391 let mut ret = (self.LockableScore.read_lock)(self.LockableScore.this_arg);
394 fn write_lock(&'a self) -> crate::lightning::routing::scoring::ScoreUpdate {
395 let mut ret = (self.LockableScore.write_lock)(self.LockableScore.this_arg);
399 impl lightning::util::ser::Writeable for WriteableScore {
400 fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), crate::c_types::io::Error> {
401 let vec = (self.write)(self.this_arg);
402 w.write_all(vec.as_slice())
406 use lightning::routing::scoring::WriteableScore as rustWriteableScore;
407 impl<'a> rustWriteableScore<'a> for WriteableScore {
410 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
411 // directly as a Deref trait in higher-level structs:
412 impl core::ops::Deref for WriteableScore {
414 fn deref(&self) -> &Self {
418 impl core::ops::DerefMut for WriteableScore {
419 fn deref_mut(&mut self) -> &mut Self {
423 /// Calls the free function if one is set
425 pub extern "C" fn WriteableScore_free(this_ptr: WriteableScore) { }
426 impl Drop for WriteableScore {
428 if let Some(f) = self.free {
434 use lightning::routing::scoring::MultiThreadedLockableScore as nativeMultiThreadedLockableScoreImport;
435 pub(crate) type nativeMultiThreadedLockableScore = nativeMultiThreadedLockableScoreImport<crate::lightning::routing::scoring::Score>;
437 /// A concrete implementation of [`LockableScore`] which supports multi-threading.
440 pub struct MultiThreadedLockableScore {
441 /// A pointer to the opaque Rust object.
443 /// Nearly everywhere, inner must be non-null, however in places where
444 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
445 pub inner: *mut nativeMultiThreadedLockableScore,
446 /// Indicates that this is the only struct which contains the same pointer.
448 /// Rust functions which take ownership of an object provided via an argument require
449 /// this to be true and invalidate the object pointed to by inner.
453 impl Drop for MultiThreadedLockableScore {
455 if self.is_owned && !<*mut nativeMultiThreadedLockableScore>::is_null(self.inner) {
456 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
460 /// Frees any resources used by the MultiThreadedLockableScore, if is_owned is set and inner is non-NULL.
462 pub extern "C" fn MultiThreadedLockableScore_free(this_obj: MultiThreadedLockableScore) { }
464 /// Used only if an object of this type is returned as a trait impl by a method
465 pub(crate) extern "C" fn MultiThreadedLockableScore_free_void(this_ptr: *mut c_void) {
466 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeMultiThreadedLockableScore) };
469 impl MultiThreadedLockableScore {
470 pub(crate) fn get_native_ref(&self) -> &'static nativeMultiThreadedLockableScore {
471 unsafe { &*ObjOps::untweak_ptr(self.inner) }
473 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeMultiThreadedLockableScore {
474 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
476 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
477 pub(crate) fn take_inner(mut self) -> *mut nativeMultiThreadedLockableScore {
478 assert!(self.is_owned);
479 let ret = ObjOps::untweak_ptr(self.inner);
480 self.inner = core::ptr::null_mut();
484 impl From<nativeMultiThreadedLockableScore> for crate::lightning::routing::scoring::LockableScore {
485 fn from(obj: nativeMultiThreadedLockableScore) -> Self {
486 let mut rust_obj = MultiThreadedLockableScore { inner: ObjOps::heap_alloc(obj), is_owned: true };
487 let mut ret = MultiThreadedLockableScore_as_LockableScore(&rust_obj);
488 // 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
489 rust_obj.inner = core::ptr::null_mut();
490 ret.free = Some(MultiThreadedLockableScore_free_void);
494 /// Constructs a new LockableScore which calls the relevant methods on this_arg.
495 /// This copies the `inner` pointer in this_arg and thus the returned LockableScore must be freed before this_arg is
497 pub extern "C" fn MultiThreadedLockableScore_as_LockableScore(this_arg: &MultiThreadedLockableScore) -> crate::lightning::routing::scoring::LockableScore {
498 crate::lightning::routing::scoring::LockableScore {
499 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
501 read_lock: MultiThreadedLockableScore_LockableScore_read_lock,
502 write_lock: MultiThreadedLockableScore_LockableScore_write_lock,
507 extern "C" fn MultiThreadedLockableScore_LockableScore_read_lock(this_arg: *const c_void) -> crate::lightning::routing::scoring::ScoreLookUp {
508 let mut ret = <nativeMultiThreadedLockableScore as lightning::routing::scoring::LockableScore<>>::read_lock(unsafe { &mut *(this_arg as *mut nativeMultiThreadedLockableScore) }, );
512 extern "C" fn MultiThreadedLockableScore_LockableScore_write_lock(this_arg: *const c_void) -> crate::lightning::routing::scoring::ScoreUpdate {
513 let mut ret = <nativeMultiThreadedLockableScore as lightning::routing::scoring::LockableScore<>>::write_lock(unsafe { &mut *(this_arg as *mut nativeMultiThreadedLockableScore) }, );
518 /// Serialize the MultiThreadedLockableScore object into a byte array which can be read by MultiThreadedLockableScore_read
519 pub extern "C" fn MultiThreadedLockableScore_write(obj: &crate::lightning::routing::scoring::MultiThreadedLockableScore) -> crate::c_types::derived::CVec_u8Z {
520 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
523 pub(crate) extern "C" fn MultiThreadedLockableScore_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
524 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeMultiThreadedLockableScore) })
526 impl From<nativeMultiThreadedLockableScore> for crate::lightning::routing::scoring::WriteableScore {
527 fn from(obj: nativeMultiThreadedLockableScore) -> Self {
528 let mut rust_obj = MultiThreadedLockableScore { inner: ObjOps::heap_alloc(obj), is_owned: true };
529 let mut ret = MultiThreadedLockableScore_as_WriteableScore(&rust_obj);
530 // 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
531 rust_obj.inner = core::ptr::null_mut();
532 ret.free = Some(MultiThreadedLockableScore_free_void);
536 /// Constructs a new WriteableScore which calls the relevant methods on this_arg.
537 /// This copies the `inner` pointer in this_arg and thus the returned WriteableScore must be freed before this_arg is
539 pub extern "C" fn MultiThreadedLockableScore_as_WriteableScore(this_arg: &MultiThreadedLockableScore) -> crate::lightning::routing::scoring::WriteableScore {
540 crate::lightning::routing::scoring::WriteableScore {
541 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
543 LockableScore: crate::lightning::routing::scoring::LockableScore {
544 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
546 read_lock: MultiThreadedLockableScore_LockableScore_read_lock,
547 write_lock: MultiThreadedLockableScore_LockableScore_write_lock,
549 write: MultiThreadedLockableScore_write_void,
554 /// Creates a new [`MultiThreadedLockableScore`] given an underlying [`Score`].
557 pub extern "C" fn MultiThreadedLockableScore_new(mut score: crate::lightning::routing::scoring::Score) -> crate::lightning::routing::scoring::MultiThreadedLockableScore {
558 let mut ret = lightning::routing::scoring::MultiThreadedLockableScore::new(score);
559 crate::lightning::routing::scoring::MultiThreadedLockableScore { inner: ObjOps::heap_alloc(ret), is_owned: true }
563 use lightning::routing::scoring::MultiThreadedScoreLockRead as nativeMultiThreadedScoreLockReadImport;
564 pub(crate) type nativeMultiThreadedScoreLockRead = nativeMultiThreadedScoreLockReadImport<'static, crate::lightning::routing::scoring::Score>;
566 /// A locked `MultiThreadedLockableScore`.
569 pub struct MultiThreadedScoreLockRead {
570 /// A pointer to the opaque Rust object.
572 /// Nearly everywhere, inner must be non-null, however in places where
573 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
574 pub inner: *mut nativeMultiThreadedScoreLockRead,
575 /// Indicates that this is the only struct which contains the same pointer.
577 /// Rust functions which take ownership of an object provided via an argument require
578 /// this to be true and invalidate the object pointed to by inner.
582 impl Drop for MultiThreadedScoreLockRead {
584 if self.is_owned && !<*mut nativeMultiThreadedScoreLockRead>::is_null(self.inner) {
585 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
589 /// Frees any resources used by the MultiThreadedScoreLockRead, if is_owned is set and inner is non-NULL.
591 pub extern "C" fn MultiThreadedScoreLockRead_free(this_obj: MultiThreadedScoreLockRead) { }
593 /// Used only if an object of this type is returned as a trait impl by a method
594 pub(crate) extern "C" fn MultiThreadedScoreLockRead_free_void(this_ptr: *mut c_void) {
595 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeMultiThreadedScoreLockRead) };
598 impl MultiThreadedScoreLockRead {
599 pub(crate) fn get_native_ref(&self) -> &'static nativeMultiThreadedScoreLockRead {
600 unsafe { &*ObjOps::untweak_ptr(self.inner) }
602 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeMultiThreadedScoreLockRead {
603 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
605 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
606 pub(crate) fn take_inner(mut self) -> *mut nativeMultiThreadedScoreLockRead {
607 assert!(self.is_owned);
608 let ret = ObjOps::untweak_ptr(self.inner);
609 self.inner = core::ptr::null_mut();
614 use lightning::routing::scoring::MultiThreadedScoreLockWrite as nativeMultiThreadedScoreLockWriteImport;
615 pub(crate) type nativeMultiThreadedScoreLockWrite = nativeMultiThreadedScoreLockWriteImport<'static, crate::lightning::routing::scoring::Score>;
617 /// A locked `MultiThreadedLockableScore`.
620 pub struct MultiThreadedScoreLockWrite {
621 /// A pointer to the opaque Rust object.
623 /// Nearly everywhere, inner must be non-null, however in places where
624 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
625 pub inner: *mut nativeMultiThreadedScoreLockWrite,
626 /// Indicates that this is the only struct which contains the same pointer.
628 /// Rust functions which take ownership of an object provided via an argument require
629 /// this to be true and invalidate the object pointed to by inner.
633 impl Drop for MultiThreadedScoreLockWrite {
635 if self.is_owned && !<*mut nativeMultiThreadedScoreLockWrite>::is_null(self.inner) {
636 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
640 /// Frees any resources used by the MultiThreadedScoreLockWrite, if is_owned is set and inner is non-NULL.
642 pub extern "C" fn MultiThreadedScoreLockWrite_free(this_obj: MultiThreadedScoreLockWrite) { }
644 /// Used only if an object of this type is returned as a trait impl by a method
645 pub(crate) extern "C" fn MultiThreadedScoreLockWrite_free_void(this_ptr: *mut c_void) {
646 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeMultiThreadedScoreLockWrite) };
649 impl MultiThreadedScoreLockWrite {
650 pub(crate) fn get_native_ref(&self) -> &'static nativeMultiThreadedScoreLockWrite {
651 unsafe { &*ObjOps::untweak_ptr(self.inner) }
653 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeMultiThreadedScoreLockWrite {
654 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
656 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
657 pub(crate) fn take_inner(mut self) -> *mut nativeMultiThreadedScoreLockWrite {
658 assert!(self.is_owned);
659 let ret = ObjOps::untweak_ptr(self.inner);
660 self.inner = core::ptr::null_mut();
664 impl From<nativeMultiThreadedScoreLockRead> for crate::lightning::routing::scoring::ScoreLookUp {
665 fn from(obj: nativeMultiThreadedScoreLockRead) -> Self {
666 let mut rust_obj = MultiThreadedScoreLockRead { inner: ObjOps::heap_alloc(obj), is_owned: true };
667 let mut ret = MultiThreadedScoreLockRead_as_ScoreLookUp(&rust_obj);
668 // 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
669 rust_obj.inner = core::ptr::null_mut();
670 ret.free = Some(MultiThreadedScoreLockRead_free_void);
674 /// Constructs a new ScoreLookUp which calls the relevant methods on this_arg.
675 /// This copies the `inner` pointer in this_arg and thus the returned ScoreLookUp must be freed before this_arg is
677 pub extern "C" fn MultiThreadedScoreLockRead_as_ScoreLookUp(this_arg: &MultiThreadedScoreLockRead) -> crate::lightning::routing::scoring::ScoreLookUp {
678 crate::lightning::routing::scoring::ScoreLookUp {
679 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
681 channel_penalty_msat: MultiThreadedScoreLockRead_ScoreLookUp_channel_penalty_msat,
686 extern "C" fn MultiThreadedScoreLockRead_ScoreLookUp_channel_penalty_msat(this_arg: *const c_void, mut short_channel_id: u64, source: &crate::lightning::routing::gossip::NodeId, target: &crate::lightning::routing::gossip::NodeId, mut usage: crate::lightning::routing::scoring::ChannelUsage, score_params: &crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
687 let mut ret = <nativeMultiThreadedScoreLockRead as lightning::routing::scoring::ScoreLookUp<>>::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLockRead) }, short_channel_id, source.get_native_ref(), target.get_native_ref(), *unsafe { Box::from_raw(usage.take_inner()) }, score_params.get_native_ref());
692 /// Serialize the MultiThreadedScoreLockWrite object into a byte array which can be read by MultiThreadedScoreLockWrite_read
693 pub extern "C" fn MultiThreadedScoreLockWrite_write(obj: &crate::lightning::routing::scoring::MultiThreadedScoreLockWrite) -> crate::c_types::derived::CVec_u8Z {
694 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
697 pub(crate) extern "C" fn MultiThreadedScoreLockWrite_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
698 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeMultiThreadedScoreLockWrite) })
700 impl From<nativeMultiThreadedScoreLockWrite> for crate::lightning::routing::scoring::ScoreUpdate {
701 fn from(obj: nativeMultiThreadedScoreLockWrite) -> Self {
702 let mut rust_obj = MultiThreadedScoreLockWrite { inner: ObjOps::heap_alloc(obj), is_owned: true };
703 let mut ret = MultiThreadedScoreLockWrite_as_ScoreUpdate(&rust_obj);
704 // 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
705 rust_obj.inner = core::ptr::null_mut();
706 ret.free = Some(MultiThreadedScoreLockWrite_free_void);
710 /// Constructs a new ScoreUpdate which calls the relevant methods on this_arg.
711 /// This copies the `inner` pointer in this_arg and thus the returned ScoreUpdate must be freed before this_arg is
713 pub extern "C" fn MultiThreadedScoreLockWrite_as_ScoreUpdate(this_arg: &MultiThreadedScoreLockWrite) -> crate::lightning::routing::scoring::ScoreUpdate {
714 crate::lightning::routing::scoring::ScoreUpdate {
715 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
717 payment_path_failed: MultiThreadedScoreLockWrite_ScoreUpdate_payment_path_failed,
718 payment_path_successful: MultiThreadedScoreLockWrite_ScoreUpdate_payment_path_successful,
719 probe_failed: MultiThreadedScoreLockWrite_ScoreUpdate_probe_failed,
720 probe_successful: MultiThreadedScoreLockWrite_ScoreUpdate_probe_successful,
724 extern "C" fn MultiThreadedScoreLockWrite_ScoreUpdate_payment_path_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) {
725 <nativeMultiThreadedScoreLockWrite as lightning::routing::scoring::ScoreUpdate<>>::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLockWrite) }, path.get_native_ref(), short_channel_id)
727 extern "C" fn MultiThreadedScoreLockWrite_ScoreUpdate_payment_path_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) {
728 <nativeMultiThreadedScoreLockWrite as lightning::routing::scoring::ScoreUpdate<>>::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLockWrite) }, path.get_native_ref())
730 extern "C" fn MultiThreadedScoreLockWrite_ScoreUpdate_probe_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) {
731 <nativeMultiThreadedScoreLockWrite as lightning::routing::scoring::ScoreUpdate<>>::probe_failed(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLockWrite) }, path.get_native_ref(), short_channel_id)
733 extern "C" fn MultiThreadedScoreLockWrite_ScoreUpdate_probe_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) {
734 <nativeMultiThreadedScoreLockWrite as lightning::routing::scoring::ScoreUpdate<>>::probe_successful(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLockWrite) }, path.get_native_ref())
738 use lightning::routing::scoring::ChannelUsage as nativeChannelUsageImport;
739 pub(crate) type nativeChannelUsage = nativeChannelUsageImport;
741 /// Proposed use of a channel passed as a parameter to [`ScoreLookUp::channel_penalty_msat`].
744 pub struct ChannelUsage {
745 /// A pointer to the opaque Rust object.
747 /// Nearly everywhere, inner must be non-null, however in places where
748 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
749 pub inner: *mut nativeChannelUsage,
750 /// Indicates that this is the only struct which contains the same pointer.
752 /// Rust functions which take ownership of an object provided via an argument require
753 /// this to be true and invalidate the object pointed to by inner.
757 impl Drop for ChannelUsage {
759 if self.is_owned && !<*mut nativeChannelUsage>::is_null(self.inner) {
760 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
764 /// Frees any resources used by the ChannelUsage, if is_owned is set and inner is non-NULL.
766 pub extern "C" fn ChannelUsage_free(this_obj: ChannelUsage) { }
768 /// Used only if an object of this type is returned as a trait impl by a method
769 pub(crate) extern "C" fn ChannelUsage_free_void(this_ptr: *mut c_void) {
770 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeChannelUsage) };
774 pub(crate) fn get_native_ref(&self) -> &'static nativeChannelUsage {
775 unsafe { &*ObjOps::untweak_ptr(self.inner) }
777 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeChannelUsage {
778 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
780 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
781 pub(crate) fn take_inner(mut self) -> *mut nativeChannelUsage {
782 assert!(self.is_owned);
783 let ret = ObjOps::untweak_ptr(self.inner);
784 self.inner = core::ptr::null_mut();
788 /// The amount to send through the channel, denominated in millisatoshis.
790 pub extern "C" fn ChannelUsage_get_amount_msat(this_ptr: &ChannelUsage) -> u64 {
791 let mut inner_val = &mut this_ptr.get_native_mut_ref().amount_msat;
794 /// The amount to send through the channel, denominated in millisatoshis.
796 pub extern "C" fn ChannelUsage_set_amount_msat(this_ptr: &mut ChannelUsage, mut val: u64) {
797 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.amount_msat = val;
799 /// Total amount, denominated in millisatoshis, already allocated to send through the channel
800 /// as part of a multi-path payment.
802 pub extern "C" fn ChannelUsage_get_inflight_htlc_msat(this_ptr: &ChannelUsage) -> u64 {
803 let mut inner_val = &mut this_ptr.get_native_mut_ref().inflight_htlc_msat;
806 /// Total amount, denominated in millisatoshis, already allocated to send through the channel
807 /// as part of a multi-path payment.
809 pub extern "C" fn ChannelUsage_set_inflight_htlc_msat(this_ptr: &mut ChannelUsage, mut val: u64) {
810 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.inflight_htlc_msat = val;
812 /// The effective capacity of the channel.
814 pub extern "C" fn ChannelUsage_get_effective_capacity(this_ptr: &ChannelUsage) -> crate::lightning::routing::gossip::EffectiveCapacity {
815 let mut inner_val = &mut this_ptr.get_native_mut_ref().effective_capacity;
816 crate::lightning::routing::gossip::EffectiveCapacity::from_native(inner_val)
818 /// The effective capacity of the channel.
820 pub extern "C" fn ChannelUsage_set_effective_capacity(this_ptr: &mut ChannelUsage, mut val: crate::lightning::routing::gossip::EffectiveCapacity) {
821 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.effective_capacity = val.into_native();
823 /// Constructs a new ChannelUsage given each field
826 pub extern "C" fn ChannelUsage_new(mut amount_msat_arg: u64, mut inflight_htlc_msat_arg: u64, mut effective_capacity_arg: crate::lightning::routing::gossip::EffectiveCapacity) -> ChannelUsage {
827 ChannelUsage { inner: ObjOps::heap_alloc(nativeChannelUsage {
828 amount_msat: amount_msat_arg,
829 inflight_htlc_msat: inflight_htlc_msat_arg,
830 effective_capacity: effective_capacity_arg.into_native(),
833 impl Clone for ChannelUsage {
834 fn clone(&self) -> Self {
836 inner: if <*mut nativeChannelUsage>::is_null(self.inner) { core::ptr::null_mut() } else {
837 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
843 /// Used only if an object of this type is returned as a trait impl by a method
844 pub(crate) extern "C" fn ChannelUsage_clone_void(this_ptr: *const c_void) -> *mut c_void {
845 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeChannelUsage)).clone() })) as *mut c_void
848 /// Creates a copy of the ChannelUsage
849 pub extern "C" fn ChannelUsage_clone(orig: &ChannelUsage) -> ChannelUsage {
853 use lightning::routing::scoring::FixedPenaltyScorer as nativeFixedPenaltyScorerImport;
854 pub(crate) type nativeFixedPenaltyScorer = nativeFixedPenaltyScorerImport;
856 /// [`ScoreLookUp`] implementation that uses a fixed penalty.
859 pub struct FixedPenaltyScorer {
860 /// A pointer to the opaque Rust object.
862 /// Nearly everywhere, inner must be non-null, however in places where
863 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
864 pub inner: *mut nativeFixedPenaltyScorer,
865 /// Indicates that this is the only struct which contains the same pointer.
867 /// Rust functions which take ownership of an object provided via an argument require
868 /// this to be true and invalidate the object pointed to by inner.
872 impl Drop for FixedPenaltyScorer {
874 if self.is_owned && !<*mut nativeFixedPenaltyScorer>::is_null(self.inner) {
875 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
879 /// Frees any resources used by the FixedPenaltyScorer, if is_owned is set and inner is non-NULL.
881 pub extern "C" fn FixedPenaltyScorer_free(this_obj: FixedPenaltyScorer) { }
883 /// Used only if an object of this type is returned as a trait impl by a method
884 pub(crate) extern "C" fn FixedPenaltyScorer_free_void(this_ptr: *mut c_void) {
885 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeFixedPenaltyScorer) };
888 impl FixedPenaltyScorer {
889 pub(crate) fn get_native_ref(&self) -> &'static nativeFixedPenaltyScorer {
890 unsafe { &*ObjOps::untweak_ptr(self.inner) }
892 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeFixedPenaltyScorer {
893 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
895 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
896 pub(crate) fn take_inner(mut self) -> *mut nativeFixedPenaltyScorer {
897 assert!(self.is_owned);
898 let ret = ObjOps::untweak_ptr(self.inner);
899 self.inner = core::ptr::null_mut();
903 impl Clone for FixedPenaltyScorer {
904 fn clone(&self) -> Self {
906 inner: if <*mut nativeFixedPenaltyScorer>::is_null(self.inner) { core::ptr::null_mut() } else {
907 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
913 /// Used only if an object of this type is returned as a trait impl by a method
914 pub(crate) extern "C" fn FixedPenaltyScorer_clone_void(this_ptr: *const c_void) -> *mut c_void {
915 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeFixedPenaltyScorer)).clone() })) as *mut c_void
918 /// Creates a copy of the FixedPenaltyScorer
919 pub extern "C" fn FixedPenaltyScorer_clone(orig: &FixedPenaltyScorer) -> FixedPenaltyScorer {
922 /// Creates a new scorer using `penalty_msat`.
925 pub extern "C" fn FixedPenaltyScorer_with_penalty(mut penalty_msat: u64) -> crate::lightning::routing::scoring::FixedPenaltyScorer {
926 let mut ret = lightning::routing::scoring::FixedPenaltyScorer::with_penalty(penalty_msat);
927 crate::lightning::routing::scoring::FixedPenaltyScorer { inner: ObjOps::heap_alloc(ret), is_owned: true }
930 impl From<nativeFixedPenaltyScorer> for crate::lightning::routing::scoring::ScoreLookUp {
931 fn from(obj: nativeFixedPenaltyScorer) -> Self {
932 let mut rust_obj = FixedPenaltyScorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
933 let mut ret = FixedPenaltyScorer_as_ScoreLookUp(&rust_obj);
934 // 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
935 rust_obj.inner = core::ptr::null_mut();
936 ret.free = Some(FixedPenaltyScorer_free_void);
940 /// Constructs a new ScoreLookUp which calls the relevant methods on this_arg.
941 /// This copies the `inner` pointer in this_arg and thus the returned ScoreLookUp must be freed before this_arg is
943 pub extern "C" fn FixedPenaltyScorer_as_ScoreLookUp(this_arg: &FixedPenaltyScorer) -> crate::lightning::routing::scoring::ScoreLookUp {
944 crate::lightning::routing::scoring::ScoreLookUp {
945 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
947 channel_penalty_msat: FixedPenaltyScorer_ScoreLookUp_channel_penalty_msat,
952 extern "C" fn FixedPenaltyScorer_ScoreLookUp_channel_penalty_msat(this_arg: *const c_void, mut short_channel_id: u64, source: &crate::lightning::routing::gossip::NodeId, target: &crate::lightning::routing::gossip::NodeId, mut usage: crate::lightning::routing::scoring::ChannelUsage, score_params: &crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
953 let mut ret = <nativeFixedPenaltyScorer as lightning::routing::scoring::ScoreLookUp<>>::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, short_channel_id, source.get_native_ref(), target.get_native_ref(), *unsafe { Box::from_raw(usage.take_inner()) }, score_params.get_native_ref());
957 impl From<nativeFixedPenaltyScorer> for crate::lightning::routing::scoring::ScoreUpdate {
958 fn from(obj: nativeFixedPenaltyScorer) -> Self {
959 let mut rust_obj = FixedPenaltyScorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
960 let mut ret = FixedPenaltyScorer_as_ScoreUpdate(&rust_obj);
961 // 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
962 rust_obj.inner = core::ptr::null_mut();
963 ret.free = Some(FixedPenaltyScorer_free_void);
967 /// Constructs a new ScoreUpdate which calls the relevant methods on this_arg.
968 /// This copies the `inner` pointer in this_arg and thus the returned ScoreUpdate must be freed before this_arg is
970 pub extern "C" fn FixedPenaltyScorer_as_ScoreUpdate(this_arg: &FixedPenaltyScorer) -> crate::lightning::routing::scoring::ScoreUpdate {
971 crate::lightning::routing::scoring::ScoreUpdate {
972 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
974 payment_path_failed: FixedPenaltyScorer_ScoreUpdate_payment_path_failed,
975 payment_path_successful: FixedPenaltyScorer_ScoreUpdate_payment_path_successful,
976 probe_failed: FixedPenaltyScorer_ScoreUpdate_probe_failed,
977 probe_successful: FixedPenaltyScorer_ScoreUpdate_probe_successful,
981 extern "C" fn FixedPenaltyScorer_ScoreUpdate_payment_path_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) {
982 <nativeFixedPenaltyScorer as lightning::routing::scoring::ScoreUpdate<>>::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, path.get_native_ref(), short_channel_id)
984 extern "C" fn FixedPenaltyScorer_ScoreUpdate_payment_path_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) {
985 <nativeFixedPenaltyScorer as lightning::routing::scoring::ScoreUpdate<>>::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, path.get_native_ref())
987 extern "C" fn FixedPenaltyScorer_ScoreUpdate_probe_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) {
988 <nativeFixedPenaltyScorer as lightning::routing::scoring::ScoreUpdate<>>::probe_failed(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, path.get_native_ref(), short_channel_id)
990 extern "C" fn FixedPenaltyScorer_ScoreUpdate_probe_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) {
991 <nativeFixedPenaltyScorer as lightning::routing::scoring::ScoreUpdate<>>::probe_successful(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, path.get_native_ref())
995 /// Serialize the FixedPenaltyScorer object into a byte array which can be read by FixedPenaltyScorer_read
996 pub extern "C" fn FixedPenaltyScorer_write(obj: &crate::lightning::routing::scoring::FixedPenaltyScorer) -> crate::c_types::derived::CVec_u8Z {
997 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
1000 pub(crate) extern "C" fn FixedPenaltyScorer_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1001 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeFixedPenaltyScorer) })
1004 /// Read a FixedPenaltyScorer from a byte array, created by FixedPenaltyScorer_write
1005 pub extern "C" fn FixedPenaltyScorer_read(ser: crate::c_types::u8slice, arg: u64) -> crate::c_types::derived::CResult_FixedPenaltyScorerDecodeErrorZ {
1007 let res: Result<lightning::routing::scoring::FixedPenaltyScorer, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj_arg(ser, arg_conv);
1008 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::native_into(e) }).into() };
1012 use lightning::routing::scoring::ProbabilisticScorer as nativeProbabilisticScorerImport;
1013 pub(crate) type nativeProbabilisticScorer = nativeProbabilisticScorerImport<&'static lightning::routing::gossip::NetworkGraph<crate::lightning::util::logger::Logger>, crate::lightning::util::logger::Logger>;
1015 /// [`ScoreLookUp`] implementation using channel success probability distributions.
1017 /// Channels are tracked with upper and lower liquidity bounds - when an HTLC fails at a channel,
1018 /// we learn that the upper-bound on the available liquidity is lower than the amount of the HTLC.
1019 /// When a payment is forwarded through a channel (but fails later in the route), we learn the
1020 /// lower-bound on the channel's available liquidity must be at least the value of the HTLC.
1022 /// These bounds are then used to determine a success probability using the formula from
1023 /// *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
1024 /// and Stefan Richter [[1]] (i.e. `(upper_bound - payment_amount) / (upper_bound - lower_bound)`).
1026 /// This probability is combined with the [`liquidity_penalty_multiplier_msat`] and
1027 /// [`liquidity_penalty_amount_multiplier_msat`] parameters to calculate a concrete penalty in
1028 /// milli-satoshis. The penalties, when added across all hops, have the property of being linear in
1029 /// terms of the entire path's success probability. This allows the router to directly compare
1030 /// penalties for different paths. See the documentation of those parameters for the exact formulas.
1032 /// The liquidity bounds are decayed by halving them every [`liquidity_offset_half_life`].
1034 /// Further, we track the history of our upper and lower liquidity bounds for each channel,
1035 /// allowing us to assign a second penalty (using [`historical_liquidity_penalty_multiplier_msat`]
1036 /// and [`historical_liquidity_penalty_amount_multiplier_msat`]) based on the same probability
1037 /// formula, but using the history of a channel rather than our latest estimates for the liquidity
1042 /// Mixing the `no-std` feature between serialization and deserialization results in undefined
1045 /// [1]: https://arxiv.org/abs/2107.05322
1046 /// [`liquidity_penalty_multiplier_msat`]: ProbabilisticScoringFeeParameters::liquidity_penalty_multiplier_msat
1047 /// [`liquidity_penalty_amount_multiplier_msat`]: ProbabilisticScoringFeeParameters::liquidity_penalty_amount_multiplier_msat
1048 /// [`liquidity_offset_half_life`]: ProbabilisticScoringDecayParameters::liquidity_offset_half_life
1049 /// [`historical_liquidity_penalty_multiplier_msat`]: ProbabilisticScoringFeeParameters::historical_liquidity_penalty_multiplier_msat
1050 /// [`historical_liquidity_penalty_amount_multiplier_msat`]: ProbabilisticScoringFeeParameters::historical_liquidity_penalty_amount_multiplier_msat
1053 pub struct ProbabilisticScorer {
1054 /// A pointer to the opaque Rust object.
1056 /// Nearly everywhere, inner must be non-null, however in places where
1057 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1058 pub inner: *mut nativeProbabilisticScorer,
1059 /// Indicates that this is the only struct which contains the same pointer.
1061 /// Rust functions which take ownership of an object provided via an argument require
1062 /// this to be true and invalidate the object pointed to by inner.
1066 impl Drop for ProbabilisticScorer {
1067 fn drop(&mut self) {
1068 if self.is_owned && !<*mut nativeProbabilisticScorer>::is_null(self.inner) {
1069 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
1073 /// Frees any resources used by the ProbabilisticScorer, if is_owned is set and inner is non-NULL.
1075 pub extern "C" fn ProbabilisticScorer_free(this_obj: ProbabilisticScorer) { }
1077 /// Used only if an object of this type is returned as a trait impl by a method
1078 pub(crate) extern "C" fn ProbabilisticScorer_free_void(this_ptr: *mut c_void) {
1079 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeProbabilisticScorer) };
1082 impl ProbabilisticScorer {
1083 pub(crate) fn get_native_ref(&self) -> &'static nativeProbabilisticScorer {
1084 unsafe { &*ObjOps::untweak_ptr(self.inner) }
1086 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeProbabilisticScorer {
1087 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
1089 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1090 pub(crate) fn take_inner(mut self) -> *mut nativeProbabilisticScorer {
1091 assert!(self.is_owned);
1092 let ret = ObjOps::untweak_ptr(self.inner);
1093 self.inner = core::ptr::null_mut();
1098 use lightning::routing::scoring::ProbabilisticScoringFeeParameters as nativeProbabilisticScoringFeeParametersImport;
1099 pub(crate) type nativeProbabilisticScoringFeeParameters = nativeProbabilisticScoringFeeParametersImport;
1101 /// Parameters for configuring [`ProbabilisticScorer`].
1103 /// Used to configure base, liquidity, and amount penalties, the sum of which comprises the channel
1104 /// penalty (i.e., the amount in msats willing to be paid to avoid routing through the channel).
1106 /// The penalty applied to any channel by the [`ProbabilisticScorer`] is the sum of each of the
1107 /// parameters here.
1110 pub struct ProbabilisticScoringFeeParameters {
1111 /// A pointer to the opaque Rust object.
1113 /// Nearly everywhere, inner must be non-null, however in places where
1114 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1115 pub inner: *mut nativeProbabilisticScoringFeeParameters,
1116 /// Indicates that this is the only struct which contains the same pointer.
1118 /// Rust functions which take ownership of an object provided via an argument require
1119 /// this to be true and invalidate the object pointed to by inner.
1123 impl Drop for ProbabilisticScoringFeeParameters {
1124 fn drop(&mut self) {
1125 if self.is_owned && !<*mut nativeProbabilisticScoringFeeParameters>::is_null(self.inner) {
1126 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
1130 /// Frees any resources used by the ProbabilisticScoringFeeParameters, if is_owned is set and inner is non-NULL.
1132 pub extern "C" fn ProbabilisticScoringFeeParameters_free(this_obj: ProbabilisticScoringFeeParameters) { }
1134 /// Used only if an object of this type is returned as a trait impl by a method
1135 pub(crate) extern "C" fn ProbabilisticScoringFeeParameters_free_void(this_ptr: *mut c_void) {
1136 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeProbabilisticScoringFeeParameters) };
1139 impl ProbabilisticScoringFeeParameters {
1140 pub(crate) fn get_native_ref(&self) -> &'static nativeProbabilisticScoringFeeParameters {
1141 unsafe { &*ObjOps::untweak_ptr(self.inner) }
1143 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeProbabilisticScoringFeeParameters {
1144 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
1146 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1147 pub(crate) fn take_inner(mut self) -> *mut nativeProbabilisticScoringFeeParameters {
1148 assert!(self.is_owned);
1149 let ret = ObjOps::untweak_ptr(self.inner);
1150 self.inner = core::ptr::null_mut();
1154 /// A fixed penalty in msats to apply to each channel.
1156 /// Default value: 500 msat
1158 pub extern "C" fn ProbabilisticScoringFeeParameters_get_base_penalty_msat(this_ptr: &ProbabilisticScoringFeeParameters) -> u64 {
1159 let mut inner_val = &mut this_ptr.get_native_mut_ref().base_penalty_msat;
1162 /// A fixed penalty in msats to apply to each channel.
1164 /// Default value: 500 msat
1166 pub extern "C" fn ProbabilisticScoringFeeParameters_set_base_penalty_msat(this_ptr: &mut ProbabilisticScoringFeeParameters, mut val: u64) {
1167 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.base_penalty_msat = val;
1169 /// A multiplier used with the total amount flowing over a channel to calculate a fixed penalty
1170 /// applied to each channel, in excess of the [`base_penalty_msat`].
1172 /// The purpose of the amount penalty is to avoid having fees dominate the channel cost (i.e.,
1173 /// fees plus penalty) for large payments. The penalty is computed as the product of this
1174 /// multiplier and `2^30`ths of the total amount flowing over a channel (i.e. the payment
1175 /// amount plus the amount of any other HTLCs flowing we sent over the same channel).
1177 /// ie `base_penalty_amount_multiplier_msat * amount_msat / 2^30`
1179 /// Default value: 8,192 msat
1181 /// [`base_penalty_msat`]: Self::base_penalty_msat
1183 pub extern "C" fn ProbabilisticScoringFeeParameters_get_base_penalty_amount_multiplier_msat(this_ptr: &ProbabilisticScoringFeeParameters) -> u64 {
1184 let mut inner_val = &mut this_ptr.get_native_mut_ref().base_penalty_amount_multiplier_msat;
1187 /// A multiplier used with the total amount flowing over a channel to calculate a fixed penalty
1188 /// applied to each channel, in excess of the [`base_penalty_msat`].
1190 /// The purpose of the amount penalty is to avoid having fees dominate the channel cost (i.e.,
1191 /// fees plus penalty) for large payments. The penalty is computed as the product of this
1192 /// multiplier and `2^30`ths of the total amount flowing over a channel (i.e. the payment
1193 /// amount plus the amount of any other HTLCs flowing we sent over the same channel).
1195 /// ie `base_penalty_amount_multiplier_msat * amount_msat / 2^30`
1197 /// Default value: 8,192 msat
1199 /// [`base_penalty_msat`]: Self::base_penalty_msat
1201 pub extern "C" fn ProbabilisticScoringFeeParameters_set_base_penalty_amount_multiplier_msat(this_ptr: &mut ProbabilisticScoringFeeParameters, mut val: u64) {
1202 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.base_penalty_amount_multiplier_msat = val;
1204 /// A multiplier used in conjunction with the negative `log10` of the channel's success
1205 /// probability for a payment, as determined by our latest estimates of the channel's
1206 /// liquidity, to determine the liquidity penalty.
1208 /// The penalty is based in part on the knowledge learned from prior successful and unsuccessful
1209 /// payments. This knowledge is decayed over time based on [`liquidity_offset_half_life`]. The
1210 /// penalty is effectively limited to `2 * liquidity_penalty_multiplier_msat` (corresponding to
1211 /// lower bounding the success probability to `0.01`) when the amount falls within the
1212 /// uncertainty bounds of the channel liquidity balance. Amounts above the upper bound will
1213 /// result in a `u64::max_value` penalty, however.
1215 /// `-log10(success_probability) * liquidity_penalty_multiplier_msat`
1217 /// Default value: 30,000 msat
1219 /// [`liquidity_offset_half_life`]: ProbabilisticScoringDecayParameters::liquidity_offset_half_life
1221 pub extern "C" fn ProbabilisticScoringFeeParameters_get_liquidity_penalty_multiplier_msat(this_ptr: &ProbabilisticScoringFeeParameters) -> u64 {
1222 let mut inner_val = &mut this_ptr.get_native_mut_ref().liquidity_penalty_multiplier_msat;
1225 /// A multiplier used in conjunction with the negative `log10` of the channel's success
1226 /// probability for a payment, as determined by our latest estimates of the channel's
1227 /// liquidity, to determine the liquidity penalty.
1229 /// The penalty is based in part on the knowledge learned from prior successful and unsuccessful
1230 /// payments. This knowledge is decayed over time based on [`liquidity_offset_half_life`]. The
1231 /// penalty is effectively limited to `2 * liquidity_penalty_multiplier_msat` (corresponding to
1232 /// lower bounding the success probability to `0.01`) when the amount falls within the
1233 /// uncertainty bounds of the channel liquidity balance. Amounts above the upper bound will
1234 /// result in a `u64::max_value` penalty, however.
1236 /// `-log10(success_probability) * liquidity_penalty_multiplier_msat`
1238 /// Default value: 30,000 msat
1240 /// [`liquidity_offset_half_life`]: ProbabilisticScoringDecayParameters::liquidity_offset_half_life
1242 pub extern "C" fn ProbabilisticScoringFeeParameters_set_liquidity_penalty_multiplier_msat(this_ptr: &mut ProbabilisticScoringFeeParameters, mut val: u64) {
1243 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.liquidity_penalty_multiplier_msat = val;
1245 /// A multiplier used in conjunction with the total amount flowing over a channel and the
1246 /// negative `log10` of the channel's success probability for the payment, as determined by our
1247 /// latest estimates of the channel's liquidity, to determine the amount penalty.
1249 /// The purpose of the amount penalty is to avoid having fees dominate the channel cost (i.e.,
1250 /// fees plus penalty) for large payments. The penalty is computed as the product of this
1251 /// multiplier and `2^20`ths of the amount flowing over this channel, weighted by the negative
1252 /// `log10` of the success probability.
1254 /// `-log10(success_probability) * liquidity_penalty_amount_multiplier_msat * amount_msat / 2^20`
1256 /// In practice, this means for 0.1 success probability (`-log10(0.1) == 1`) each `2^20`th of
1257 /// the amount will result in a penalty of the multiplier. And, as the success probability
1258 /// decreases, the negative `log10` weighting will increase dramatically. For higher success
1259 /// probabilities, the multiplier will have a decreasing effect as the negative `log10` will
1262 /// Default value: 192 msat
1264 pub extern "C" fn ProbabilisticScoringFeeParameters_get_liquidity_penalty_amount_multiplier_msat(this_ptr: &ProbabilisticScoringFeeParameters) -> u64 {
1265 let mut inner_val = &mut this_ptr.get_native_mut_ref().liquidity_penalty_amount_multiplier_msat;
1268 /// A multiplier used in conjunction with the total amount flowing over a channel and the
1269 /// negative `log10` of the channel's success probability for the payment, as determined by our
1270 /// latest estimates of the channel's liquidity, to determine the amount penalty.
1272 /// The purpose of the amount penalty is to avoid having fees dominate the channel cost (i.e.,
1273 /// fees plus penalty) for large payments. The penalty is computed as the product of this
1274 /// multiplier and `2^20`ths of the amount flowing over this channel, weighted by the negative
1275 /// `log10` of the success probability.
1277 /// `-log10(success_probability) * liquidity_penalty_amount_multiplier_msat * amount_msat / 2^20`
1279 /// In practice, this means for 0.1 success probability (`-log10(0.1) == 1`) each `2^20`th of
1280 /// the amount will result in a penalty of the multiplier. And, as the success probability
1281 /// decreases, the negative `log10` weighting will increase dramatically. For higher success
1282 /// probabilities, the multiplier will have a decreasing effect as the negative `log10` will
1285 /// Default value: 192 msat
1287 pub extern "C" fn ProbabilisticScoringFeeParameters_set_liquidity_penalty_amount_multiplier_msat(this_ptr: &mut ProbabilisticScoringFeeParameters, mut val: u64) {
1288 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.liquidity_penalty_amount_multiplier_msat = val;
1290 /// A multiplier used in conjunction with the negative `log10` of the channel's success
1291 /// probability for the payment, as determined based on the history of our estimates of the
1292 /// channel's available liquidity, to determine a penalty.
1294 /// This penalty is similar to [`liquidity_penalty_multiplier_msat`], however, instead of using
1295 /// only our latest estimate for the current liquidity available in the channel, it estimates
1296 /// success probability based on the estimated liquidity available in the channel through
1297 /// history. Specifically, every time we update our liquidity bounds on a given channel, we
1298 /// track which of several buckets those bounds fall into, exponentially decaying the
1299 /// probability of each bucket as new samples are added.
1301 /// Default value: 10,000 msat
1303 /// [`liquidity_penalty_multiplier_msat`]: Self::liquidity_penalty_multiplier_msat
1305 pub extern "C" fn ProbabilisticScoringFeeParameters_get_historical_liquidity_penalty_multiplier_msat(this_ptr: &ProbabilisticScoringFeeParameters) -> u64 {
1306 let mut inner_val = &mut this_ptr.get_native_mut_ref().historical_liquidity_penalty_multiplier_msat;
1309 /// A multiplier used in conjunction with the negative `log10` of the channel's success
1310 /// probability for the payment, as determined based on the history of our estimates of the
1311 /// channel's available liquidity, to determine a penalty.
1313 /// This penalty is similar to [`liquidity_penalty_multiplier_msat`], however, instead of using
1314 /// only our latest estimate for the current liquidity available in the channel, it estimates
1315 /// success probability based on the estimated liquidity available in the channel through
1316 /// history. Specifically, every time we update our liquidity bounds on a given channel, we
1317 /// track which of several buckets those bounds fall into, exponentially decaying the
1318 /// probability of each bucket as new samples are added.
1320 /// Default value: 10,000 msat
1322 /// [`liquidity_penalty_multiplier_msat`]: Self::liquidity_penalty_multiplier_msat
1324 pub extern "C" fn ProbabilisticScoringFeeParameters_set_historical_liquidity_penalty_multiplier_msat(this_ptr: &mut ProbabilisticScoringFeeParameters, mut val: u64) {
1325 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.historical_liquidity_penalty_multiplier_msat = val;
1327 /// A multiplier used in conjunction with the total amount flowing over a channel and the
1328 /// negative `log10` of the channel's success probability for the payment, as determined based
1329 /// on the history of our estimates of the channel's available liquidity, to determine a
1332 /// The purpose of the amount penalty is to avoid having fees dominate the channel cost for
1333 /// large payments. The penalty is computed as the product of this multiplier and `2^20`ths
1334 /// of the amount flowing over this channel, weighted by the negative `log10` of the success
1337 /// This penalty is similar to [`liquidity_penalty_amount_multiplier_msat`], however, instead
1338 /// of using only our latest estimate for the current liquidity available in the channel, it
1339 /// estimates success probability based on the estimated liquidity available in the channel
1340 /// through history. Specifically, every time we update our liquidity bounds on a given
1341 /// channel, we track which of several buckets those bounds fall into, exponentially decaying
1342 /// the probability of each bucket as new samples are added.
1344 /// Default value: 64 msat
1346 /// [`liquidity_penalty_amount_multiplier_msat`]: Self::liquidity_penalty_amount_multiplier_msat
1348 pub extern "C" fn ProbabilisticScoringFeeParameters_get_historical_liquidity_penalty_amount_multiplier_msat(this_ptr: &ProbabilisticScoringFeeParameters) -> u64 {
1349 let mut inner_val = &mut this_ptr.get_native_mut_ref().historical_liquidity_penalty_amount_multiplier_msat;
1352 /// A multiplier used in conjunction with the total amount flowing over a channel and the
1353 /// negative `log10` of the channel's success probability for the payment, as determined based
1354 /// on the history of our estimates of the channel's available liquidity, to determine a
1357 /// The purpose of the amount penalty is to avoid having fees dominate the channel cost for
1358 /// large payments. The penalty is computed as the product of this multiplier and `2^20`ths
1359 /// of the amount flowing over this channel, weighted by the negative `log10` of the success
1362 /// This penalty is similar to [`liquidity_penalty_amount_multiplier_msat`], however, instead
1363 /// of using only our latest estimate for the current liquidity available in the channel, it
1364 /// estimates success probability based on the estimated liquidity available in the channel
1365 /// through history. Specifically, every time we update our liquidity bounds on a given
1366 /// channel, we track which of several buckets those bounds fall into, exponentially decaying
1367 /// the probability of each bucket as new samples are added.
1369 /// Default value: 64 msat
1371 /// [`liquidity_penalty_amount_multiplier_msat`]: Self::liquidity_penalty_amount_multiplier_msat
1373 pub extern "C" fn ProbabilisticScoringFeeParameters_set_historical_liquidity_penalty_amount_multiplier_msat(this_ptr: &mut ProbabilisticScoringFeeParameters, mut val: u64) {
1374 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.historical_liquidity_penalty_amount_multiplier_msat = val;
1376 /// This penalty is applied when `htlc_maximum_msat` is equal to or larger than half of the
1377 /// channel's capacity, (ie. htlc_maximum_msat >= 0.5 * channel_capacity) which makes us
1378 /// prefer nodes with a smaller `htlc_maximum_msat`. We treat such nodes preferentially
1379 /// as this makes balance discovery attacks harder to execute, thereby creating an incentive
1380 /// to restrict `htlc_maximum_msat` and improve privacy.
1382 /// Default value: 250 msat
1384 pub extern "C" fn ProbabilisticScoringFeeParameters_get_anti_probing_penalty_msat(this_ptr: &ProbabilisticScoringFeeParameters) -> u64 {
1385 let mut inner_val = &mut this_ptr.get_native_mut_ref().anti_probing_penalty_msat;
1388 /// This penalty is applied when `htlc_maximum_msat` is equal to or larger than half of the
1389 /// channel's capacity, (ie. htlc_maximum_msat >= 0.5 * channel_capacity) which makes us
1390 /// prefer nodes with a smaller `htlc_maximum_msat`. We treat such nodes preferentially
1391 /// as this makes balance discovery attacks harder to execute, thereby creating an incentive
1392 /// to restrict `htlc_maximum_msat` and improve privacy.
1394 /// Default value: 250 msat
1396 pub extern "C" fn ProbabilisticScoringFeeParameters_set_anti_probing_penalty_msat(this_ptr: &mut ProbabilisticScoringFeeParameters, mut val: u64) {
1397 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.anti_probing_penalty_msat = val;
1399 /// This penalty is applied when the total amount flowing over a channel exceeds our current
1400 /// estimate of the channel's available liquidity. The total amount is the amount of the
1401 /// current HTLC plus any HTLCs which we've sent over the same channel.
1403 /// Note that in this case all other penalties, including the
1404 /// [`liquidity_penalty_multiplier_msat`] and [`liquidity_penalty_amount_multiplier_msat`]-based
1405 /// penalties, as well as the [`base_penalty_msat`] and the [`anti_probing_penalty_msat`], if
1406 /// applicable, are still included in the overall penalty.
1408 /// If you wish to avoid creating paths with such channels entirely, setting this to a value of
1409 /// `u64::max_value()` will guarantee that.
1411 /// Default value: 1_0000_0000_000 msat (1 Bitcoin)
1413 /// [`liquidity_penalty_multiplier_msat`]: Self::liquidity_penalty_multiplier_msat
1414 /// [`liquidity_penalty_amount_multiplier_msat`]: Self::liquidity_penalty_amount_multiplier_msat
1415 /// [`base_penalty_msat`]: Self::base_penalty_msat
1416 /// [`anti_probing_penalty_msat`]: Self::anti_probing_penalty_msat
1418 pub extern "C" fn ProbabilisticScoringFeeParameters_get_considered_impossible_penalty_msat(this_ptr: &ProbabilisticScoringFeeParameters) -> u64 {
1419 let mut inner_val = &mut this_ptr.get_native_mut_ref().considered_impossible_penalty_msat;
1422 /// This penalty is applied when the total amount flowing over a channel exceeds our current
1423 /// estimate of the channel's available liquidity. The total amount is the amount of the
1424 /// current HTLC plus any HTLCs which we've sent over the same channel.
1426 /// Note that in this case all other penalties, including the
1427 /// [`liquidity_penalty_multiplier_msat`] and [`liquidity_penalty_amount_multiplier_msat`]-based
1428 /// penalties, as well as the [`base_penalty_msat`] and the [`anti_probing_penalty_msat`], if
1429 /// applicable, are still included in the overall penalty.
1431 /// If you wish to avoid creating paths with such channels entirely, setting this to a value of
1432 /// `u64::max_value()` will guarantee that.
1434 /// Default value: 1_0000_0000_000 msat (1 Bitcoin)
1436 /// [`liquidity_penalty_multiplier_msat`]: Self::liquidity_penalty_multiplier_msat
1437 /// [`liquidity_penalty_amount_multiplier_msat`]: Self::liquidity_penalty_amount_multiplier_msat
1438 /// [`base_penalty_msat`]: Self::base_penalty_msat
1439 /// [`anti_probing_penalty_msat`]: Self::anti_probing_penalty_msat
1441 pub extern "C" fn ProbabilisticScoringFeeParameters_set_considered_impossible_penalty_msat(this_ptr: &mut ProbabilisticScoringFeeParameters, mut val: u64) {
1442 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.considered_impossible_penalty_msat = val;
1444 /// In order to calculate most of the scores above, we must first convert a lower and upper
1445 /// bound on the available liquidity in a channel into the probability that we think a payment
1446 /// will succeed. That probability is derived from a Probability Density Function for where we
1447 /// think the liquidity in a channel likely lies, given such bounds.
1449 /// If this flag is set, that PDF is simply a constant - we assume that the actual available
1450 /// liquidity in a channel is just as likely to be at any point between our lower and upper
1453 /// If this flag is *not* set, that PDF is `(x - 0.5*capacity) ^ 2`. That is, we use an
1454 /// exponential curve which expects the liquidity of a channel to lie \"at the edges\". This
1455 /// matches experimental results - most routing nodes do not aggressively rebalance their
1456 /// channels and flows in the network are often unbalanced, leaving liquidity usually
1459 /// Thus, for the \"best\" routes, leave this flag `false`. However, the flag does imply a number
1460 /// of floating-point multiplications in the hottest routing code, which may lead to routing
1461 /// performance degradation on some machines.
1463 /// Default value: false
1465 pub extern "C" fn ProbabilisticScoringFeeParameters_get_linear_success_probability(this_ptr: &ProbabilisticScoringFeeParameters) -> bool {
1466 let mut inner_val = &mut this_ptr.get_native_mut_ref().linear_success_probability;
1469 /// In order to calculate most of the scores above, we must first convert a lower and upper
1470 /// bound on the available liquidity in a channel into the probability that we think a payment
1471 /// will succeed. That probability is derived from a Probability Density Function for where we
1472 /// think the liquidity in a channel likely lies, given such bounds.
1474 /// If this flag is set, that PDF is simply a constant - we assume that the actual available
1475 /// liquidity in a channel is just as likely to be at any point between our lower and upper
1478 /// If this flag is *not* set, that PDF is `(x - 0.5*capacity) ^ 2`. That is, we use an
1479 /// exponential curve which expects the liquidity of a channel to lie \"at the edges\". This
1480 /// matches experimental results - most routing nodes do not aggressively rebalance their
1481 /// channels and flows in the network are often unbalanced, leaving liquidity usually
1484 /// Thus, for the \"best\" routes, leave this flag `false`. However, the flag does imply a number
1485 /// of floating-point multiplications in the hottest routing code, which may lead to routing
1486 /// performance degradation on some machines.
1488 /// Default value: false
1490 pub extern "C" fn ProbabilisticScoringFeeParameters_set_linear_success_probability(this_ptr: &mut ProbabilisticScoringFeeParameters, mut val: bool) {
1491 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.linear_success_probability = val;
1493 impl Clone for ProbabilisticScoringFeeParameters {
1494 fn clone(&self) -> Self {
1496 inner: if <*mut nativeProbabilisticScoringFeeParameters>::is_null(self.inner) { core::ptr::null_mut() } else {
1497 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
1503 /// Used only if an object of this type is returned as a trait impl by a method
1504 pub(crate) extern "C" fn ProbabilisticScoringFeeParameters_clone_void(this_ptr: *const c_void) -> *mut c_void {
1505 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeProbabilisticScoringFeeParameters)).clone() })) as *mut c_void
1508 /// Creates a copy of the ProbabilisticScoringFeeParameters
1509 pub extern "C" fn ProbabilisticScoringFeeParameters_clone(orig: &ProbabilisticScoringFeeParameters) -> ProbabilisticScoringFeeParameters {
1512 /// Creates a "default" ProbabilisticScoringFeeParameters. See struct and individual field documentaiton for details on which values are used.
1515 pub extern "C" fn ProbabilisticScoringFeeParameters_default() -> ProbabilisticScoringFeeParameters {
1516 ProbabilisticScoringFeeParameters { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
1518 /// Marks the node with the given `node_id` as banned,
1519 /// i.e it will be avoided during path finding.
1521 pub extern "C" fn ProbabilisticScoringFeeParameters_add_banned(this_arg: &mut crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters, node_id: &crate::lightning::routing::gossip::NodeId) {
1522 unsafe { &mut (*ObjOps::untweak_ptr(this_arg.inner as *mut crate::lightning::routing::scoring::nativeProbabilisticScoringFeeParameters)) }.add_banned(node_id.get_native_ref())
1525 /// Marks all nodes in the given list as banned, i.e.,
1526 /// they will be avoided during path finding.
1528 pub extern "C" fn ProbabilisticScoringFeeParameters_add_banned_from_list(this_arg: &mut crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters, mut node_ids: crate::c_types::derived::CVec_NodeIdZ) {
1529 let mut local_node_ids = Vec::new(); for mut item in node_ids.into_rust().drain(..) { local_node_ids.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
1530 unsafe { &mut (*ObjOps::untweak_ptr(this_arg.inner as *mut crate::lightning::routing::scoring::nativeProbabilisticScoringFeeParameters)) }.add_banned_from_list(local_node_ids)
1533 /// Removes the node with the given `node_id` from the list of nodes to avoid.
1535 pub extern "C" fn ProbabilisticScoringFeeParameters_remove_banned(this_arg: &mut crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters, node_id: &crate::lightning::routing::gossip::NodeId) {
1536 unsafe { &mut (*ObjOps::untweak_ptr(this_arg.inner as *mut crate::lightning::routing::scoring::nativeProbabilisticScoringFeeParameters)) }.remove_banned(node_id.get_native_ref())
1539 /// Sets a manual penalty for the given node.
1541 pub extern "C" fn ProbabilisticScoringFeeParameters_set_manual_penalty(this_arg: &mut crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters, node_id: &crate::lightning::routing::gossip::NodeId, mut penalty: u64) {
1542 unsafe { &mut (*ObjOps::untweak_ptr(this_arg.inner as *mut crate::lightning::routing::scoring::nativeProbabilisticScoringFeeParameters)) }.set_manual_penalty(node_id.get_native_ref(), penalty)
1545 /// Removes the node with the given `node_id` from the list of manual penalties.
1547 pub extern "C" fn ProbabilisticScoringFeeParameters_remove_manual_penalty(this_arg: &mut crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters, node_id: &crate::lightning::routing::gossip::NodeId) {
1548 unsafe { &mut (*ObjOps::untweak_ptr(this_arg.inner as *mut crate::lightning::routing::scoring::nativeProbabilisticScoringFeeParameters)) }.remove_manual_penalty(node_id.get_native_ref())
1551 /// Clears the list of manual penalties that are applied during path finding.
1553 pub extern "C" fn ProbabilisticScoringFeeParameters_clear_manual_penalties(this_arg: &mut crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters) {
1554 unsafe { &mut (*ObjOps::untweak_ptr(this_arg.inner as *mut crate::lightning::routing::scoring::nativeProbabilisticScoringFeeParameters)) }.clear_manual_penalties()
1558 use lightning::routing::scoring::ProbabilisticScoringDecayParameters as nativeProbabilisticScoringDecayParametersImport;
1559 pub(crate) type nativeProbabilisticScoringDecayParameters = nativeProbabilisticScoringDecayParametersImport;
1561 /// Parameters for configuring [`ProbabilisticScorer`].
1563 /// Used to configure decay parameters that are static throughout the lifetime of the scorer.
1564 /// these decay parameters affect the score of the channel penalty and are not changed on a
1565 /// per-route penalty cost call.
1568 pub struct ProbabilisticScoringDecayParameters {
1569 /// A pointer to the opaque Rust object.
1571 /// Nearly everywhere, inner must be non-null, however in places where
1572 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1573 pub inner: *mut nativeProbabilisticScoringDecayParameters,
1574 /// Indicates that this is the only struct which contains the same pointer.
1576 /// Rust functions which take ownership of an object provided via an argument require
1577 /// this to be true and invalidate the object pointed to by inner.
1581 impl Drop for ProbabilisticScoringDecayParameters {
1582 fn drop(&mut self) {
1583 if self.is_owned && !<*mut nativeProbabilisticScoringDecayParameters>::is_null(self.inner) {
1584 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
1588 /// Frees any resources used by the ProbabilisticScoringDecayParameters, if is_owned is set and inner is non-NULL.
1590 pub extern "C" fn ProbabilisticScoringDecayParameters_free(this_obj: ProbabilisticScoringDecayParameters) { }
1592 /// Used only if an object of this type is returned as a trait impl by a method
1593 pub(crate) extern "C" fn ProbabilisticScoringDecayParameters_free_void(this_ptr: *mut c_void) {
1594 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeProbabilisticScoringDecayParameters) };
1597 impl ProbabilisticScoringDecayParameters {
1598 pub(crate) fn get_native_ref(&self) -> &'static nativeProbabilisticScoringDecayParameters {
1599 unsafe { &*ObjOps::untweak_ptr(self.inner) }
1601 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeProbabilisticScoringDecayParameters {
1602 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
1604 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1605 pub(crate) fn take_inner(mut self) -> *mut nativeProbabilisticScoringDecayParameters {
1606 assert!(self.is_owned);
1607 let ret = ObjOps::untweak_ptr(self.inner);
1608 self.inner = core::ptr::null_mut();
1612 /// If we aren't learning any new datapoints for a channel, the historical liquidity bounds
1613 /// tracking can simply live on with increasingly stale data. Instead, when a channel has not
1614 /// seen a liquidity estimate update for this amount of time, the historical datapoints are
1615 /// decayed by half.
1616 /// For an example of historical_no_updates_half_life being used see [`historical_estimated_channel_liquidity_probabilities`]
1618 /// Note that after 16 or more half lives all historical data will be completely gone.
1620 /// Default value: 14 days
1622 /// [`historical_estimated_channel_liquidity_probabilities`]: ProbabilisticScorerUsingTime::historical_estimated_channel_liquidity_probabilities
1624 pub extern "C" fn ProbabilisticScoringDecayParameters_get_historical_no_updates_half_life(this_ptr: &ProbabilisticScoringDecayParameters) -> u64 {
1625 let mut inner_val = &mut this_ptr.get_native_mut_ref().historical_no_updates_half_life;
1628 /// If we aren't learning any new datapoints for a channel, the historical liquidity bounds
1629 /// tracking can simply live on with increasingly stale data. Instead, when a channel has not
1630 /// seen a liquidity estimate update for this amount of time, the historical datapoints are
1631 /// decayed by half.
1632 /// For an example of historical_no_updates_half_life being used see [`historical_estimated_channel_liquidity_probabilities`]
1634 /// Note that after 16 or more half lives all historical data will be completely gone.
1636 /// Default value: 14 days
1638 /// [`historical_estimated_channel_liquidity_probabilities`]: ProbabilisticScorerUsingTime::historical_estimated_channel_liquidity_probabilities
1640 pub extern "C" fn ProbabilisticScoringDecayParameters_set_historical_no_updates_half_life(this_ptr: &mut ProbabilisticScoringDecayParameters, mut val: u64) {
1641 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.historical_no_updates_half_life = core::time::Duration::from_secs(val);
1643 /// Whenever this amount of time elapses since the last update to a channel's liquidity bounds,
1644 /// the distance from the bounds to \"zero\" is cut in half. In other words, the lower-bound on
1645 /// the available liquidity is halved and the upper-bound moves half-way to the channel's total
1648 /// Because halving the liquidity bounds grows the uncertainty on the channel's liquidity,
1649 /// the penalty for an amount within the new bounds may change. See the [`ProbabilisticScorer`]
1650 /// struct documentation for more info on the way the liquidity bounds are used.
1652 /// For example, if the channel's capacity is 1 million sats, and the current upper and lower
1653 /// liquidity bounds are 200,000 sats and 600,000 sats, after this amount of time the upper
1654 /// and lower liquidity bounds will be decayed to 100,000 and 800,000 sats.
1656 /// Default value: 6 hours
1660 /// When built with the `no-std` feature, time will never elapse. Therefore, the channel
1661 /// liquidity knowledge will never decay except when the bounds cross.
1663 pub extern "C" fn ProbabilisticScoringDecayParameters_get_liquidity_offset_half_life(this_ptr: &ProbabilisticScoringDecayParameters) -> u64 {
1664 let mut inner_val = &mut this_ptr.get_native_mut_ref().liquidity_offset_half_life;
1667 /// Whenever this amount of time elapses since the last update to a channel's liquidity bounds,
1668 /// the distance from the bounds to \"zero\" is cut in half. In other words, the lower-bound on
1669 /// the available liquidity is halved and the upper-bound moves half-way to the channel's total
1672 /// Because halving the liquidity bounds grows the uncertainty on the channel's liquidity,
1673 /// the penalty for an amount within the new bounds may change. See the [`ProbabilisticScorer`]
1674 /// struct documentation for more info on the way the liquidity bounds are used.
1676 /// For example, if the channel's capacity is 1 million sats, and the current upper and lower
1677 /// liquidity bounds are 200,000 sats and 600,000 sats, after this amount of time the upper
1678 /// and lower liquidity bounds will be decayed to 100,000 and 800,000 sats.
1680 /// Default value: 6 hours
1684 /// When built with the `no-std` feature, time will never elapse. Therefore, the channel
1685 /// liquidity knowledge will never decay except when the bounds cross.
1687 pub extern "C" fn ProbabilisticScoringDecayParameters_set_liquidity_offset_half_life(this_ptr: &mut ProbabilisticScoringDecayParameters, mut val: u64) {
1688 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.liquidity_offset_half_life = core::time::Duration::from_secs(val);
1690 /// Constructs a new ProbabilisticScoringDecayParameters given each field
1693 pub extern "C" fn ProbabilisticScoringDecayParameters_new(mut historical_no_updates_half_life_arg: u64, mut liquidity_offset_half_life_arg: u64) -> ProbabilisticScoringDecayParameters {
1694 ProbabilisticScoringDecayParameters { inner: ObjOps::heap_alloc(nativeProbabilisticScoringDecayParameters {
1695 historical_no_updates_half_life: core::time::Duration::from_secs(historical_no_updates_half_life_arg),
1696 liquidity_offset_half_life: core::time::Duration::from_secs(liquidity_offset_half_life_arg),
1697 }), is_owned: true }
1699 impl Clone for ProbabilisticScoringDecayParameters {
1700 fn clone(&self) -> Self {
1702 inner: if <*mut nativeProbabilisticScoringDecayParameters>::is_null(self.inner) { core::ptr::null_mut() } else {
1703 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
1709 /// Used only if an object of this type is returned as a trait impl by a method
1710 pub(crate) extern "C" fn ProbabilisticScoringDecayParameters_clone_void(this_ptr: *const c_void) -> *mut c_void {
1711 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeProbabilisticScoringDecayParameters)).clone() })) as *mut c_void
1714 /// Creates a copy of the ProbabilisticScoringDecayParameters
1715 pub extern "C" fn ProbabilisticScoringDecayParameters_clone(orig: &ProbabilisticScoringDecayParameters) -> ProbabilisticScoringDecayParameters {
1718 /// Creates a "default" ProbabilisticScoringDecayParameters. See struct and individual field documentaiton for details on which values are used.
1721 pub extern "C" fn ProbabilisticScoringDecayParameters_default() -> ProbabilisticScoringDecayParameters {
1722 ProbabilisticScoringDecayParameters { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
1724 /// Creates a new scorer using the given scoring parameters for sending payments from a node
1725 /// through a network graph.
1728 pub extern "C" fn ProbabilisticScorer_new(mut decay_params: crate::lightning::routing::scoring::ProbabilisticScoringDecayParameters, network_graph: &crate::lightning::routing::gossip::NetworkGraph, mut logger: crate::lightning::util::logger::Logger) -> crate::lightning::routing::scoring::ProbabilisticScorer {
1729 let mut ret = lightning::routing::scoring::ProbabilisticScorer::new(*unsafe { Box::from_raw(decay_params.take_inner()) }, network_graph.get_native_ref(), logger);
1730 crate::lightning::routing::scoring::ProbabilisticScorer { inner: ObjOps::heap_alloc(ret), is_owned: true }
1733 /// Dump the contents of this scorer into the configured logger.
1735 /// Note that this writes roughly one line per channel for which we have a liquidity estimate,
1736 /// which may be a substantial amount of log output.
1738 pub extern "C" fn ProbabilisticScorer_debug_log_liquidity_stats(this_arg: &crate::lightning::routing::scoring::ProbabilisticScorer) {
1739 unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.debug_log_liquidity_stats()
1742 /// Query the estimated minimum and maximum liquidity available for sending a payment over the
1743 /// channel with `scid` towards the given `target` node.
1746 pub extern "C" fn ProbabilisticScorer_estimated_channel_liquidity_range(this_arg: &crate::lightning::routing::scoring::ProbabilisticScorer, mut scid: u64, target: &crate::lightning::routing::gossip::NodeId) -> crate::c_types::derived::COption_C2Tuple_u64u64ZZ {
1747 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.estimated_channel_liquidity_range(scid, target.get_native_ref());
1748 let mut local_ret = if ret.is_none() { crate::c_types::derived::COption_C2Tuple_u64u64ZZ::None } else { crate::c_types::derived::COption_C2Tuple_u64u64ZZ::Some( { let (mut orig_ret_0_0, mut orig_ret_0_1) = (ret.unwrap()); let mut local_ret_0 = (orig_ret_0_0, orig_ret_0_1).into(); local_ret_0 }) };
1752 /// Query the historical estimated minimum and maximum liquidity available for sending a
1753 /// payment over the channel with `scid` towards the given `target` node.
1755 /// Returns two sets of 32 buckets. The first set describes the lower-bound liquidity history,
1756 /// the second set describes the upper-bound liquidity history. Each bucket describes the
1757 /// relative frequency at which we've seen a liquidity bound in the bucket's range relative to
1758 /// the channel's total capacity, on an arbitrary scale. Because the values are slowly decayed,
1759 /// more recent data points are weighted more heavily than older datapoints.
1761 /// Note that the range of each bucket varies by its location to provide more granular results
1762 /// at the edges of a channel's capacity, where it is more likely to sit.
1764 /// When scoring, the estimated probability that an upper-/lower-bound lies in a given bucket
1765 /// is calculated by dividing that bucket's value with the total value of all buckets.
1767 /// For example, using a lower bucket count for illustrative purposes, a value of
1768 /// `[0, 0, 0, ..., 0, 32]` indicates that we believe the probability of a bound being very
1769 /// close to the channel's capacity to be 100%, and have never (recently) seen it in any other
1770 /// bucket. A value of `[31, 0, 0, ..., 0, 0, 32]` indicates we've seen the bound being both
1771 /// in the top and bottom bucket, and roughly with similar (recent) frequency.
1773 /// Because the datapoints are decayed slowly over time, values will eventually return to
1774 /// `Some(([1; 32], [1; 32]))` and then to `None` once no datapoints remain.
1776 /// In order to fetch a single success probability from the buckets provided here, as used in
1777 /// the scoring model, see [`Self::historical_estimated_payment_success_probability`].
1780 pub extern "C" fn ProbabilisticScorer_historical_estimated_channel_liquidity_probabilities(this_arg: &crate::lightning::routing::scoring::ProbabilisticScorer, mut scid: u64, target: &crate::lightning::routing::gossip::NodeId) -> crate::c_types::derived::COption_C2Tuple_ThirtyTwoU16sThirtyTwoU16sZZ {
1781 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.historical_estimated_channel_liquidity_probabilities(scid, target.get_native_ref());
1782 let mut local_ret = if ret.is_none() { crate::c_types::derived::COption_C2Tuple_ThirtyTwoU16sThirtyTwoU16sZZ::None } else { crate::c_types::derived::COption_C2Tuple_ThirtyTwoU16sThirtyTwoU16sZZ::Some( { let (mut orig_ret_0_0, mut orig_ret_0_1) = (ret.unwrap()); let mut local_ret_0 = (crate::c_types::ThirtyTwoU16s { data: orig_ret_0_0 }, crate::c_types::ThirtyTwoU16s { data: orig_ret_0_1 }).into(); local_ret_0 }) };
1786 /// Query the probability of payment success sending the given `amount_msat` over the channel
1787 /// with `scid` towards the given `target` node, based on the historical estimated liquidity
1790 /// These are the same bounds as returned by
1791 /// [`Self::historical_estimated_channel_liquidity_probabilities`] (but not those returned by
1792 /// [`Self::estimated_channel_liquidity_range`]).
1795 pub extern "C" fn ProbabilisticScorer_historical_estimated_payment_success_probability(this_arg: &crate::lightning::routing::scoring::ProbabilisticScorer, mut scid: u64, target: &crate::lightning::routing::gossip::NodeId, mut amount_msat: u64, params: &crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters) -> crate::c_types::derived::COption_f64Z {
1796 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.historical_estimated_payment_success_probability(scid, target.get_native_ref(), amount_msat, params.get_native_ref());
1797 let mut local_ret = if ret.is_none() { crate::c_types::derived::COption_f64Z::None } else { crate::c_types::derived::COption_f64Z::Some( { ret.unwrap() }) };
1801 impl From<nativeProbabilisticScorer> for crate::lightning::routing::scoring::ScoreLookUp {
1802 fn from(obj: nativeProbabilisticScorer) -> Self {
1803 let mut rust_obj = ProbabilisticScorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
1804 let mut ret = ProbabilisticScorer_as_ScoreLookUp(&rust_obj);
1805 // 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
1806 rust_obj.inner = core::ptr::null_mut();
1807 ret.free = Some(ProbabilisticScorer_free_void);
1811 /// Constructs a new ScoreLookUp which calls the relevant methods on this_arg.
1812 /// This copies the `inner` pointer in this_arg and thus the returned ScoreLookUp must be freed before this_arg is
1814 pub extern "C" fn ProbabilisticScorer_as_ScoreLookUp(this_arg: &ProbabilisticScorer) -> crate::lightning::routing::scoring::ScoreLookUp {
1815 crate::lightning::routing::scoring::ScoreLookUp {
1816 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
1818 channel_penalty_msat: ProbabilisticScorer_ScoreLookUp_channel_penalty_msat,
1823 extern "C" fn ProbabilisticScorer_ScoreLookUp_channel_penalty_msat(this_arg: *const c_void, mut short_channel_id: u64, source: &crate::lightning::routing::gossip::NodeId, target: &crate::lightning::routing::gossip::NodeId, mut usage: crate::lightning::routing::scoring::ChannelUsage, score_params: &crate::lightning::routing::scoring::ProbabilisticScoringFeeParameters) -> u64 {
1824 let mut ret = <nativeProbabilisticScorer as lightning::routing::scoring::ScoreLookUp<>>::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, short_channel_id, source.get_native_ref(), target.get_native_ref(), *unsafe { Box::from_raw(usage.take_inner()) }, score_params.get_native_ref());
1828 impl From<nativeProbabilisticScorer> for crate::lightning::routing::scoring::ScoreUpdate {
1829 fn from(obj: nativeProbabilisticScorer) -> Self {
1830 let mut rust_obj = ProbabilisticScorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
1831 let mut ret = ProbabilisticScorer_as_ScoreUpdate(&rust_obj);
1832 // 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
1833 rust_obj.inner = core::ptr::null_mut();
1834 ret.free = Some(ProbabilisticScorer_free_void);
1838 /// Constructs a new ScoreUpdate which calls the relevant methods on this_arg.
1839 /// This copies the `inner` pointer in this_arg and thus the returned ScoreUpdate must be freed before this_arg is
1841 pub extern "C" fn ProbabilisticScorer_as_ScoreUpdate(this_arg: &ProbabilisticScorer) -> crate::lightning::routing::scoring::ScoreUpdate {
1842 crate::lightning::routing::scoring::ScoreUpdate {
1843 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
1845 payment_path_failed: ProbabilisticScorer_ScoreUpdate_payment_path_failed,
1846 payment_path_successful: ProbabilisticScorer_ScoreUpdate_payment_path_successful,
1847 probe_failed: ProbabilisticScorer_ScoreUpdate_probe_failed,
1848 probe_successful: ProbabilisticScorer_ScoreUpdate_probe_successful,
1852 extern "C" fn ProbabilisticScorer_ScoreUpdate_payment_path_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) {
1853 <nativeProbabilisticScorer as lightning::routing::scoring::ScoreUpdate<>>::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, path.get_native_ref(), short_channel_id)
1855 extern "C" fn ProbabilisticScorer_ScoreUpdate_payment_path_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) {
1856 <nativeProbabilisticScorer as lightning::routing::scoring::ScoreUpdate<>>::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, path.get_native_ref())
1858 extern "C" fn ProbabilisticScorer_ScoreUpdate_probe_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) {
1859 <nativeProbabilisticScorer as lightning::routing::scoring::ScoreUpdate<>>::probe_failed(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, path.get_native_ref(), short_channel_id)
1861 extern "C" fn ProbabilisticScorer_ScoreUpdate_probe_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) {
1862 <nativeProbabilisticScorer as lightning::routing::scoring::ScoreUpdate<>>::probe_successful(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, path.get_native_ref())
1865 impl From<nativeProbabilisticScorer> for crate::lightning::routing::scoring::Score {
1866 fn from(obj: nativeProbabilisticScorer) -> Self {
1867 let mut rust_obj = ProbabilisticScorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
1868 let mut ret = ProbabilisticScorer_as_Score(&rust_obj);
1869 // 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
1870 rust_obj.inner = core::ptr::null_mut();
1871 ret.free = Some(ProbabilisticScorer_free_void);
1875 /// Constructs a new Score which calls the relevant methods on this_arg.
1876 /// This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
1878 pub extern "C" fn ProbabilisticScorer_as_Score(this_arg: &ProbabilisticScorer) -> crate::lightning::routing::scoring::Score {
1879 crate::lightning::routing::scoring::Score {
1880 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
1882 ScoreLookUp: crate::lightning::routing::scoring::ScoreLookUp {
1883 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
1885 channel_penalty_msat: ProbabilisticScorer_ScoreLookUp_channel_penalty_msat,
1887 ScoreUpdate: crate::lightning::routing::scoring::ScoreUpdate {
1888 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
1890 payment_path_failed: ProbabilisticScorer_ScoreUpdate_payment_path_failed,
1891 payment_path_successful: ProbabilisticScorer_ScoreUpdate_payment_path_successful,
1892 probe_failed: ProbabilisticScorer_ScoreUpdate_probe_failed,
1893 probe_successful: ProbabilisticScorer_ScoreUpdate_probe_successful,
1895 write: ProbabilisticScorer_write_void,
1902 use alloc::str::FromStr;
1903 use alloc::string::String;
1904 use core::ffi::c_void;
1905 use core::convert::Infallible;
1906 use bitcoin::hashes::Hash;
1907 use crate::c_types::*;
1908 #[cfg(feature="no-std")]
1909 use alloc::{vec::Vec, boxed::Box};
1912 mod bucketed_history {
1914 use alloc::str::FromStr;
1915 use alloc::string::String;
1916 use core::ffi::c_void;
1917 use core::convert::Infallible;
1918 use bitcoin::hashes::Hash;
1919 use crate::c_types::*;
1920 #[cfg(feature="no-std")]
1921 use alloc::{vec::Vec, boxed::Box};
1925 /// Serialize the ProbabilisticScorer object into a byte array which can be read by ProbabilisticScorer_read
1926 pub extern "C" fn ProbabilisticScorer_write(obj: &crate::lightning::routing::scoring::ProbabilisticScorer) -> crate::c_types::derived::CVec_u8Z {
1927 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
1930 pub(crate) extern "C" fn ProbabilisticScorer_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1931 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeProbabilisticScorer) })
1934 /// Read a ProbabilisticScorer from a byte array, created by ProbabilisticScorer_write
1935 pub extern "C" fn ProbabilisticScorer_read(ser: crate::c_types::u8slice, arg_a: crate::lightning::routing::scoring::ProbabilisticScoringDecayParameters, arg_b: &crate::lightning::routing::gossip::NetworkGraph, arg_c: crate::lightning::util::logger::Logger) -> crate::c_types::derived::CResult_ProbabilisticScorerDecodeErrorZ {
1936 let arg_a_conv = *unsafe { Box::from_raw(arg_a.take_inner()) };
1937 let arg_b_conv = arg_b.get_native_ref();
1938 let arg_c_conv = arg_c;
1939 let arg_conv = (arg_a_conv, arg_b_conv, arg_c_conv);
1940 let res: Result<lightning::routing::scoring::ProbabilisticScorer<&lightning::routing::gossip::NetworkGraph<crate::lightning::util::logger::Logger>, crate::lightning::util::logger::Logger>, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj_arg(ser, arg_conv);
1941 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::native_into(e) }).into() };