1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
9 //! Utilities for scoring payment channels.
11 //! [`Scorer`] may be given to [`find_route`] to score payment channels during path finding when a
12 //! custom [`routing::Score`] implementation is not needed.
17 //! # extern crate secp256k1;
19 //! # use lightning::routing::network_graph::NetworkGraph;
20 //! # use lightning::routing::router::{RouteParameters, find_route};
21 //! # use lightning::routing::scorer::{Scorer, ScoringParameters};
22 //! # use lightning::util::logger::{Logger, Record};
23 //! # use secp256k1::key::PublicKey;
25 //! # struct FakeLogger {};
26 //! # impl Logger for FakeLogger {
27 //! # fn log(&self, record: &Record) { unimplemented!() }
29 //! # fn find_scored_route(payer: PublicKey, params: RouteParameters, network_graph: NetworkGraph) {
30 //! # let logger = FakeLogger {};
32 //! // Use the default channel penalties.
33 //! let scorer = Scorer::default();
35 //! // Or use custom channel penalties.
36 //! let scorer = Scorer::new(ScoringParameters {
37 //! base_penalty_msat: 1000,
38 //! failure_penalty_msat: 2 * 1024 * 1000,
39 //! ..ScoringParameters::default()
42 //! let route = find_route(&payer, ¶ms, &network_graph, None, &logger, &scorer);
46 //! [`find_route`]: crate::routing::router::find_route
48 use std::str::FromStr;
50 use core::convert::Infallible;
51 use bitcoin::hashes::Hash;
52 use crate::c_types::*;
55 use lightning::routing::scorer::Scorer as nativeScorerImport;
56 pub(crate) type nativeScorer = nativeScorerImport;
58 /// [`routing::Score`] implementation that provides reasonable default behavior.
60 /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with
61 /// slightly higher fees are available. May also further penalize failed channels.
63 /// See [module-level documentation] for usage.
65 /// [module-level documentation]: crate::routing::scorer
69 /// A pointer to the opaque Rust object.
71 /// Nearly everywhere, inner must be non-null, however in places where
72 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
73 pub inner: *mut nativeScorer,
74 /// Indicates that this is the only struct which contains the same pointer.
76 /// Rust functions which take ownership of an object provided via an argument require
77 /// this to be true and invalidate the object pointed to by inner.
81 impl Drop for Scorer {
83 if self.is_owned && !<*mut nativeScorer>::is_null(self.inner) {
84 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
88 /// Frees any resources used by the Scorer, if is_owned is set and inner is non-NULL.
90 pub extern "C" fn Scorer_free(this_obj: Scorer) { }
92 /// Used only if an object of this type is returned as a trait impl by a method
93 pub(crate) extern "C" fn Scorer_free_void(this_ptr: *mut c_void) {
94 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScorer); }
98 pub(crate) fn get_native_ref(&self) -> &'static nativeScorer {
99 unsafe { &*ObjOps::untweak_ptr(self.inner) }
101 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScorer {
102 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
104 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
105 pub(crate) fn take_inner(mut self) -> *mut nativeScorer {
106 assert!(self.is_owned);
107 let ret = ObjOps::untweak_ptr(self.inner);
108 self.inner = std::ptr::null_mut();
113 use lightning::routing::scorer::ScoringParameters as nativeScoringParametersImport;
114 pub(crate) type nativeScoringParameters = nativeScoringParametersImport;
116 /// Parameters for configuring [`Scorer`].
119 pub struct ScoringParameters {
120 /// A pointer to the opaque Rust object.
122 /// Nearly everywhere, inner must be non-null, however in places where
123 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
124 pub inner: *mut nativeScoringParameters,
125 /// Indicates that this is the only struct which contains the same pointer.
127 /// Rust functions which take ownership of an object provided via an argument require
128 /// this to be true and invalidate the object pointed to by inner.
132 impl Drop for ScoringParameters {
134 if self.is_owned && !<*mut nativeScoringParameters>::is_null(self.inner) {
135 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
139 /// Frees any resources used by the ScoringParameters, if is_owned is set and inner is non-NULL.
141 pub extern "C" fn ScoringParameters_free(this_obj: ScoringParameters) { }
143 /// Used only if an object of this type is returned as a trait impl by a method
144 pub(crate) extern "C" fn ScoringParameters_free_void(this_ptr: *mut c_void) {
145 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScoringParameters); }
148 impl ScoringParameters {
149 pub(crate) fn get_native_ref(&self) -> &'static nativeScoringParameters {
150 unsafe { &*ObjOps::untweak_ptr(self.inner) }
152 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScoringParameters {
153 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
155 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
156 pub(crate) fn take_inner(mut self) -> *mut nativeScoringParameters {
157 assert!(self.is_owned);
158 let ret = ObjOps::untweak_ptr(self.inner);
159 self.inner = std::ptr::null_mut();
163 /// A fixed penalty in msats to apply to each channel.
165 pub extern "C" fn ScoringParameters_get_base_penalty_msat(this_ptr: &ScoringParameters) -> u64 {
166 let mut inner_val = &mut this_ptr.get_native_mut_ref().base_penalty_msat;
169 /// A fixed penalty in msats to apply to each channel.
171 pub extern "C" fn ScoringParameters_set_base_penalty_msat(this_ptr: &mut ScoringParameters, mut val: u64) {
172 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.base_penalty_msat = val;
174 /// A penalty in msats to apply to a channel upon failure.
176 /// This may be reduced over time based on [`failure_penalty_half_life`].
178 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
180 pub extern "C" fn ScoringParameters_get_failure_penalty_msat(this_ptr: &ScoringParameters) -> u64 {
181 let mut inner_val = &mut this_ptr.get_native_mut_ref().failure_penalty_msat;
184 /// A penalty in msats to apply to a channel upon failure.
186 /// This may be reduced over time based on [`failure_penalty_half_life`].
188 /// [`failure_penalty_half_life`]: Self::failure_penalty_half_life
190 pub extern "C" fn ScoringParameters_set_failure_penalty_msat(this_ptr: &mut ScoringParameters, mut val: u64) {
191 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.failure_penalty_msat = val;
193 /// The time needed before any accumulated channel failure penalties are cut in half.
195 pub extern "C" fn ScoringParameters_get_failure_penalty_half_life(this_ptr: &ScoringParameters) -> u64 {
196 let mut inner_val = &mut this_ptr.get_native_mut_ref().failure_penalty_half_life;
199 /// The time needed before any accumulated channel failure penalties are cut in half.
201 pub extern "C" fn ScoringParameters_set_failure_penalty_half_life(this_ptr: &mut ScoringParameters, mut val: u64) {
202 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.failure_penalty_half_life = std::time::Duration::from_secs(val);
204 /// Constructs a new ScoringParameters given each field
207 pub extern "C" fn ScoringParameters_new(mut base_penalty_msat_arg: u64, mut failure_penalty_msat_arg: u64, mut failure_penalty_half_life_arg: u64) -> ScoringParameters {
208 ScoringParameters { inner: ObjOps::heap_alloc(nativeScoringParameters {
209 base_penalty_msat: base_penalty_msat_arg,
210 failure_penalty_msat: failure_penalty_msat_arg,
211 failure_penalty_half_life: std::time::Duration::from_secs(failure_penalty_half_life_arg),
214 /// Creates a new scorer using the given scoring parameters.
217 pub extern "C" fn Scorer_new(mut params: crate::lightning::routing::scorer::ScoringParameters) -> Scorer {
218 let mut ret = lightning::routing::scorer::Scorer::new(*unsafe { Box::from_raw(params.take_inner()) });
219 Scorer { inner: ObjOps::heap_alloc(ret), is_owned: true }
222 /// Creates a "default" Scorer. See struct and individual field documentaiton for details on which values are used.
225 pub extern "C" fn Scorer_default() -> Scorer {
226 Scorer { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
228 /// Creates a "default" ScoringParameters. See struct and individual field documentaiton for details on which values are used.
231 pub extern "C" fn ScoringParameters_default() -> ScoringParameters {
232 ScoringParameters { inner: ObjOps::heap_alloc(Default::default()), is_owned: true }
234 impl From<nativeScorer> for crate::lightning::routing::Score {
235 fn from(obj: nativeScorer) -> Self {
236 let mut rust_obj = Scorer { inner: ObjOps::heap_alloc(obj), is_owned: true };
237 let mut ret = Scorer_as_Score(&rust_obj);
238 // We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn
239 rust_obj.inner = std::ptr::null_mut();
240 ret.free = Some(Scorer_free_void);
244 /// Constructs a new Score which calls the relevant methods on this_arg.
245 /// This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
247 pub extern "C" fn Scorer_as_Score(this_arg: &Scorer) -> crate::lightning::routing::Score {
248 crate::lightning::routing::Score {
249 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
251 channel_penalty_msat: Scorer_Score_channel_penalty_msat,
252 payment_path_failed: Scorer_Score_payment_path_failed,
257 extern "C" fn Scorer_Score_channel_penalty_msat(this_arg: *const c_void, mut short_channel_id: u64, _source: &crate::lightning::routing::network_graph::NodeId, _target: &crate::lightning::routing::network_graph::NodeId) -> u64 {
258 let mut ret = <nativeScorer as lightning::routing::Score<>>::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeScorer) }, short_channel_id, _source.get_native_ref(), _target.get_native_ref());
261 extern "C" fn Scorer_Score_payment_path_failed(this_arg: *mut c_void, mut _path: crate::c_types::derived::CVec_RouteHopZ, mut short_channel_id: u64) {
262 let mut local__path = Vec::new(); for mut item in _path.as_slice().iter() { local__path.push( { item.get_native_ref() }); };
263 <nativeScorer as lightning::routing::Score<>>::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeScorer) }, &local__path[..], short_channel_id)