X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Frouting%2Fmod.rs;h=8508d5dde1af4373ee7f8d4bf35bfcf066550d85;hb=7428d63475aee8fa920405fd14eda47113337798;hp=9cc0eb411ac8ab5dc0fc24416c8c775531e48bcb;hpb=5a2c2e157fd16d664a28bf3f7fe58d2216dd43d7;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning/routing/mod.rs b/lightning-c-bindings/src/lightning/routing/mod.rs index 9cc0eb4..8508d5d 100644 --- a/lightning-c-bindings/src/lightning/routing/mod.rs +++ b/lightning-c-bindings/src/lightning/routing/mod.rs @@ -10,8 +10,62 @@ use std::str::FromStr; use std::ffi::c_void; +use core::convert::Infallible; use bitcoin::hashes::Hash; use crate::c_types::*; -pub mod router; pub mod network_graph; +pub mod router; +pub mod scorer; +/// An interface used to score payment channels for path finding. +/// +///\tScoring is in terms of fees willing to be paid in order to avoid routing through a channel. +#[repr(C)] +pub struct Score { + /// An opaque pointer which is passed to your function implementations as an argument. + /// This has no meaning in the LDK, and can be NULL or any other value. + pub this_arg: *mut c_void, + /// Returns the fee in msats willing to be paid to avoid routing through the given channel. + #[must_use] + pub channel_penalty_msat: extern "C" fn (this_arg: *const c_void, short_channel_id: u64) -> u64, + /// Frees any resources associated with this object given its this_arg pointer. + /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed. + pub free: Option, +} +unsafe impl Send for Score {} +unsafe impl Sync for Score {} +#[no_mangle] +pub(crate) extern "C" fn Score_clone_fields(orig: &Score) -> Score { + Score { + this_arg: orig.this_arg, + channel_penalty_msat: Clone::clone(&orig.channel_penalty_msat), + free: Clone::clone(&orig.free), + } +} + +use lightning::routing::Score as rustScore; +impl rustScore for Score { + fn channel_penalty_msat(&self, mut short_channel_id: u64) -> u64 { + let mut ret = (self.channel_penalty_msat)(self.this_arg, short_channel_id); + ret + } +} + +// We're essentially a pointer already, or at least a set of pointers, so allow us to be used +// directly as a Deref trait in higher-level structs: +impl std::ops::Deref for Score { + type Target = Self; + fn deref(&self) -> &Self { + self + } +} +/// Calls the free function if one is set +#[no_mangle] +pub extern "C" fn Score_free(this_ptr: Score) { } +impl Drop for Score { + fn drop(&mut self) { + if let Some(f) = self.free { + f(self.this_arg); + } + } +}