X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Frouting%2Fscorer.rs;fp=lightning-c-bindings%2Fsrc%2Flightning%2Frouting%2Fscorer.rs;h=dea61efe588effa57accf99df7bd556a769a09cf;hb=50b79596ea879102808034e44061a7e12e75209c;hp=a25a489ff410cf97c4a4eca648ee9ad57a122425;hpb=ddc4ec4c3006b232c49c8f34965a548d37e35f22;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning/routing/scorer.rs b/lightning-c-bindings/src/lightning/routing/scorer.rs index a25a489..dea61ef 100644 --- a/lightning-c-bindings/src/lightning/routing/scorer.rs +++ b/lightning-c-bindings/src/lightning/routing/scorer.rs @@ -8,7 +8,7 @@ //! Utilities for scoring payment channels. //! -//! [`Scorer`] may be given to [`get_route`] to score payment channels during path finding when a +//! [`Scorer`] may be given to [`find_route`] to score payment channels during path finding when a //! custom [`routing::Score`] implementation is not needed. //! //! # Example @@ -17,8 +17,8 @@ //! # extern crate secp256k1; //! # //! # use lightning::routing::network_graph::NetworkGraph; -//! # use lightning::routing::router::get_route; -//! # use lightning::routing::scorer::Scorer; +//! # use lightning::routing::router::{RouteParameters, find_route}; +//! # use lightning::routing::scorer::{Scorer, ScoringParameters}; //! # use lightning::util::logger::{Logger, Record}; //! # use secp256k1::key::PublicKey; //! # @@ -26,20 +26,24 @@ //! # impl Logger for FakeLogger { //! # fn log(&self, record: &Record) { unimplemented!() } //! # } -//! # fn find_scored_route(payer: PublicKey, payee: PublicKey, network_graph: NetworkGraph) { +//! # fn find_scored_route(payer: PublicKey, params: RouteParameters, network_graph: NetworkGraph) { //! # let logger = FakeLogger {}; //! # -//! // Use the default channel penalty. +//! // Use the default channel penalties. //! let scorer = Scorer::default(); //! -//! // Or use a custom channel penalty. -//! let scorer = Scorer::new(1_000); +//! // Or use custom channel penalties. +//! let scorer = Scorer::new(ScoringParameters { +//! base_penalty_msat: 1000, +//! failure_penalty_msat: 2 * 1024 * 1000, +//! ..ScoringParameters::default() +//! }); //! -//! let route = get_route(&payer, &network_graph, &payee, None, None, &vec![], 1_000, 42, &logger, &scorer); +//! let route = find_route(&payer, ¶ms, &network_graph, None, &logger, &scorer); //! # } //! ``` //! -//! [`get_route`]: crate::routing::router::get_route +//! [`find_route`]: crate::routing::router::find_route use std::str::FromStr; use std::ffi::c_void; @@ -49,12 +53,12 @@ use crate::c_types::*; use lightning::routing::scorer::Scorer as nativeScorerImport; -type nativeScorer = nativeScorerImport; +pub(crate) type nativeScorer = nativeScorerImport; /// [`routing::Score`] implementation that provides reasonable default behavior. /// /// Used to apply a fixed penalty to each channel, thus avoiding long paths when shorter paths with -/// slightly higher fees are available. +/// slightly higher fees are available. May also further penalize failed channels. /// /// See [module-level documentation] for usage. /// @@ -86,7 +90,7 @@ impl Drop for Scorer { pub extern "C" fn Scorer_free(this_obj: Scorer) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method -extern "C" fn Scorer_free_void(this_ptr: *mut c_void) { +pub(crate) extern "C" fn Scorer_free_void(this_ptr: *mut c_void) { unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScorer); } } #[allow(unused)] @@ -105,11 +109,113 @@ impl Scorer { ret } } -/// Creates a new scorer using `base_penalty_msat` as the channel penalty. + +use lightning::routing::scorer::ScoringParameters as nativeScoringParametersImport; +pub(crate) type nativeScoringParameters = nativeScoringParametersImport; + +/// Parameters for configuring [`Scorer`]. #[must_use] +#[repr(C)] +pub struct ScoringParameters { + /// A pointer to the opaque Rust object. + + /// Nearly everywhere, inner must be non-null, however in places where + /// the Rust equivalent takes an Option, it may be set to null to indicate None. + pub inner: *mut nativeScoringParameters, + /// Indicates that this is the only struct which contains the same pointer. + + /// Rust functions which take ownership of an object provided via an argument require + /// this to be true and invalidate the object pointed to by inner. + pub is_owned: bool, +} + +impl Drop for ScoringParameters { + fn drop(&mut self) { + if self.is_owned && !<*mut nativeScoringParameters>::is_null(self.inner) { + let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) }; + } + } +} +/// Frees any resources used by the ScoringParameters, if is_owned is set and inner is non-NULL. #[no_mangle] -pub extern "C" fn Scorer_new(mut base_penalty_msat: u64) -> Scorer { - let mut ret = lightning::routing::scorer::Scorer::new(base_penalty_msat); +pub extern "C" fn ScoringParameters_free(this_obj: ScoringParameters) { } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ScoringParameters_free_void(this_ptr: *mut c_void) { + unsafe { let _ = Box::from_raw(this_ptr as *mut nativeScoringParameters); } +} +#[allow(unused)] +impl ScoringParameters { + pub(crate) fn get_native_ref(&self) -> &'static nativeScoringParameters { + unsafe { &*ObjOps::untweak_ptr(self.inner) } + } + pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScoringParameters { + unsafe { &mut *ObjOps::untweak_ptr(self.inner) } + } + /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy + pub(crate) fn take_inner(mut self) -> *mut nativeScoringParameters { + assert!(self.is_owned); + let ret = ObjOps::untweak_ptr(self.inner); + self.inner = std::ptr::null_mut(); + ret + } +} +/// A fixed penalty in msats to apply to each channel. +#[no_mangle] +pub extern "C" fn ScoringParameters_get_base_penalty_msat(this_ptr: &ScoringParameters) -> u64 { + let mut inner_val = &mut this_ptr.get_native_mut_ref().base_penalty_msat; + *inner_val +} +/// A fixed penalty in msats to apply to each channel. +#[no_mangle] +pub extern "C" fn ScoringParameters_set_base_penalty_msat(this_ptr: &mut ScoringParameters, mut val: u64) { + unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.base_penalty_msat = val; +} +/// A penalty in msats to apply to a channel upon failure. +/// +/// This may be reduced over time based on [`failure_penalty_half_life`]. +/// +/// [`failure_penalty_half_life`]: Self::failure_penalty_half_life +#[no_mangle] +pub extern "C" fn ScoringParameters_get_failure_penalty_msat(this_ptr: &ScoringParameters) -> u64 { + let mut inner_val = &mut this_ptr.get_native_mut_ref().failure_penalty_msat; + *inner_val +} +/// A penalty in msats to apply to a channel upon failure. +/// +/// This may be reduced over time based on [`failure_penalty_half_life`]. +/// +/// [`failure_penalty_half_life`]: Self::failure_penalty_half_life +#[no_mangle] +pub extern "C" fn ScoringParameters_set_failure_penalty_msat(this_ptr: &mut ScoringParameters, mut val: u64) { + unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.failure_penalty_msat = val; +} +/// The time needed before any accumulated channel failure penalties are cut in half. +#[no_mangle] +pub extern "C" fn ScoringParameters_get_failure_penalty_half_life(this_ptr: &ScoringParameters) -> u64 { + let mut inner_val = &mut this_ptr.get_native_mut_ref().failure_penalty_half_life; + inner_val.as_secs() +} +/// The time needed before any accumulated channel failure penalties are cut in half. +#[no_mangle] +pub extern "C" fn ScoringParameters_set_failure_penalty_half_life(this_ptr: &mut ScoringParameters, mut val: u64) { + unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.failure_penalty_half_life = std::time::Duration::from_secs(val); +} +/// Constructs a new ScoringParameters given each field +#[must_use] +#[no_mangle] +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 { + ScoringParameters { inner: ObjOps::heap_alloc(nativeScoringParameters { + base_penalty_msat: base_penalty_msat_arg, + failure_penalty_msat: failure_penalty_msat_arg, + failure_penalty_half_life: std::time::Duration::from_secs(failure_penalty_half_life_arg), + }), is_owned: true } +} +/// Creates a new scorer using the given scoring parameters. +#[must_use] +#[no_mangle] +pub extern "C" fn Scorer_new(mut params: crate::lightning::routing::scorer::ScoringParameters) -> Scorer { + let mut ret = lightning::routing::scorer::Scorer::new(*unsafe { Box::from_raw(params.take_inner()) }); Scorer { inner: ObjOps::heap_alloc(ret), is_owned: true } } @@ -119,6 +225,12 @@ pub extern "C" fn Scorer_new(mut base_penalty_msat: u64) -> Scorer { pub extern "C" fn Scorer_default() -> Scorer { Scorer { inner: ObjOps::heap_alloc(Default::default()), is_owned: true } } +/// Creates a "default" ScoringParameters. See struct and individual field documentaiton for details on which values are used. +#[must_use] +#[no_mangle] +pub extern "C" fn ScoringParameters_default() -> ScoringParameters { + ScoringParameters { inner: ObjOps::heap_alloc(Default::default()), is_owned: true } +} impl From for crate::lightning::routing::Score { fn from(obj: nativeScorer) -> Self { let mut rust_obj = Scorer { inner: ObjOps::heap_alloc(obj), is_owned: true }; @@ -137,12 +249,17 @@ pub extern "C" fn Scorer_as_Score(this_arg: &Scorer) -> crate::lightning::routin this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }, free: None, channel_penalty_msat: Scorer_Score_channel_penalty_msat, + payment_path_failed: Scorer_Score_payment_path_failed, } } #[must_use] -extern "C" fn Scorer_Score_channel_penalty_msat(this_arg: *const c_void, mut _short_channel_id: u64) -> u64 { - let mut ret = >::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeScorer) }, _short_channel_id); +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 { + let mut ret = >::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeScorer) }, short_channel_id, _source.get_native_ref(), _target.get_native_ref()); ret } +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) { + let mut local__path = Vec::new(); for mut item in _path.as_slice().iter() { local__path.push( { item.get_native_ref() }); }; + >::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeScorer) }, &local__path[..], short_channel_id) +}