X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Frouting%2Fscoring.rs;h=820a7b167355f34e912da75d5d9da455ace083a4;hb=82601052fe2cac04a6249f5930495e987923303b;hp=3a3ad98848649fd512d4158970c08acb98eae1fb;hpb=c809fde8131b3ffdb6e5c73709e85d918a87c87d;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning/routing/scoring.rs b/lightning-c-bindings/src/lightning/routing/scoring.rs index 3a3ad98..820a7b1 100644 --- a/lightning-c-bindings/src/lightning/routing/scoring.rs +++ b/lightning-c-bindings/src/lightning/routing/scoring.rs @@ -19,7 +19,7 @@ //! # use lightning::routing::gossip::NetworkGraph; //! # use lightning::routing::router::{RouteParameters, find_route}; //! # use lightning::routing::scoring::{ProbabilisticScorer, ProbabilisticScoringParameters}; -//! # use lightning::chain::keysinterface::{KeysManager, KeysInterface}; +//! # use lightning::chain::keysinterface::KeysManager; //! # use lightning::util::logger::{Logger, Record}; //! # use bitcoin::secp256k1::PublicKey; //! # @@ -80,13 +80,13 @@ pub struct Score { #[must_use] 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) -> u64, /// Handles updating channel penalties after failing to route through a channel. - pub payment_path_failed: extern "C" fn (this_arg: *mut c_void, path: crate::c_types::derived::CVec_RouteHopZ, short_channel_id: u64), + pub payment_path_failed: extern "C" fn (this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, short_channel_id: u64), /// Handles updating channel penalties after successfully routing along a path. - pub payment_path_successful: extern "C" fn (this_arg: *mut c_void, path: crate::c_types::derived::CVec_RouteHopZ), + pub payment_path_successful: extern "C" fn (this_arg: *mut c_void, path: &crate::lightning::routing::router::Path), /// Handles updating channel penalties after a probe over the given path failed. - pub probe_failed: extern "C" fn (this_arg: *mut c_void, path: crate::c_types::derived::CVec_RouteHopZ, short_channel_id: u64), + pub probe_failed: extern "C" fn (this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, short_channel_id: u64), /// Handles updating channel penalties after a probe over the given path succeeded. - pub probe_successful: extern "C" fn (this_arg: *mut c_void, path: crate::c_types::derived::CVec_RouteHopZ), + pub probe_successful: extern "C" fn (this_arg: *mut c_void, path: &crate::lightning::routing::router::Path), /// Serialize the object into a byte array pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z, /// Frees any resources associated with this object given its this_arg pointer. @@ -121,21 +121,17 @@ impl rustScore for Score { 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 }); ret } - fn payment_path_failed(&mut self, mut path: &[&lightning::routing::router::RouteHop], mut short_channel_id: u64) { - let mut local_path = Vec::new(); for item in path.iter() { local_path.push( { crate::lightning::routing::router::RouteHop { inner: unsafe { ObjOps::nonnull_ptr_to_inner(((*item) as *const lightning::routing::router::RouteHop<>) as *mut _) }, is_owned: false } }); }; - (self.payment_path_failed)(self.this_arg, local_path.into(), short_channel_id) + fn payment_path_failed(&mut self, mut path: &lightning::routing::router::Path, mut short_channel_id: u64) { + (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) } - fn payment_path_successful(&mut self, mut path: &[&lightning::routing::router::RouteHop]) { - let mut local_path = Vec::new(); for item in path.iter() { local_path.push( { crate::lightning::routing::router::RouteHop { inner: unsafe { ObjOps::nonnull_ptr_to_inner(((*item) as *const lightning::routing::router::RouteHop<>) as *mut _) }, is_owned: false } }); }; - (self.payment_path_successful)(self.this_arg, local_path.into()) + fn payment_path_successful(&mut self, mut path: &lightning::routing::router::Path) { + (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 }) } - fn probe_failed(&mut self, mut path: &[&lightning::routing::router::RouteHop], mut short_channel_id: u64) { - let mut local_path = Vec::new(); for item in path.iter() { local_path.push( { crate::lightning::routing::router::RouteHop { inner: unsafe { ObjOps::nonnull_ptr_to_inner(((*item) as *const lightning::routing::router::RouteHop<>) as *mut _) }, is_owned: false } }); }; - (self.probe_failed)(self.this_arg, local_path.into(), short_channel_id) + fn probe_failed(&mut self, mut path: &lightning::routing::router::Path, mut short_channel_id: u64) { + (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) } - fn probe_successful(&mut self, mut path: &[&lightning::routing::router::RouteHop]) { - let mut local_path = Vec::new(); for item in path.iter() { local_path.push( { crate::lightning::routing::router::RouteHop { inner: unsafe { ObjOps::nonnull_ptr_to_inner(((*item) as *const lightning::routing::router::RouteHop<>) as *mut _) }, is_owned: false } }); }; - (self.probe_successful)(self.this_arg, local_path.into()) + fn probe_successful(&mut self, mut path: &lightning::routing::router::Path) { + (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 }) } } @@ -312,7 +308,7 @@ pub extern "C" fn MultiThreadedLockableScore_free(this_obj: MultiThreadedLockabl #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn MultiThreadedLockableScore_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMultiThreadedLockableScore); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeMultiThreadedLockableScore) }; } #[allow(unused)] impl MultiThreadedLockableScore { @@ -363,7 +359,7 @@ pub extern "C" fn MultiThreadedScoreLock_free(this_obj: MultiThreadedScoreLock) #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn MultiThreadedScoreLock_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeMultiThreadedScoreLock); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeMultiThreadedScoreLock) }; } #[allow(unused)] impl MultiThreadedScoreLock { @@ -412,21 +408,17 @@ extern "C" fn MultiThreadedScoreLock_Score_channel_penalty_msat(this_arg: *const let mut ret = >::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLock) }, short_channel_id, source.get_native_ref(), target.get_native_ref(), *unsafe { Box::from_raw(usage.take_inner()) }); ret } -extern "C" fn MultiThreadedScoreLock_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 nativeMultiThreadedScoreLock) }, &local_path[..], short_channel_id) +extern "C" fn MultiThreadedScoreLock_Score_payment_path_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) { + >::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLock) }, path.get_native_ref(), short_channel_id) } -extern "C" fn MultiThreadedScoreLock_Score_payment_path_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) { - let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); }; - >::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLock) }, &local_path[..]) +extern "C" fn MultiThreadedScoreLock_Score_payment_path_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) { + >::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLock) }, path.get_native_ref()) } -extern "C" fn MultiThreadedScoreLock_Score_probe_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() }); }; - >::probe_failed(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLock) }, &local_path[..], short_channel_id) +extern "C" fn MultiThreadedScoreLock_Score_probe_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) { + >::probe_failed(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLock) }, path.get_native_ref(), short_channel_id) } -extern "C" fn MultiThreadedScoreLock_Score_probe_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) { - let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); }; - >::probe_successful(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLock) }, &local_path[..]) +extern "C" fn MultiThreadedScoreLock_Score_probe_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) { + >::probe_successful(unsafe { &mut *(this_arg as *mut nativeMultiThreadedScoreLock) }, path.get_native_ref()) } #[no_mangle] @@ -542,7 +534,7 @@ pub extern "C" fn ChannelUsage_free(this_obj: ChannelUsage) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn ChannelUsage_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelUsage); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeChannelUsage) }; } #[allow(unused)] impl ChannelUsage { @@ -657,7 +649,7 @@ pub extern "C" fn FixedPenaltyScorer_free(this_obj: FixedPenaltyScorer) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn FixedPenaltyScorer_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeFixedPenaltyScorer); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeFixedPenaltyScorer) }; } #[allow(unused)] impl FixedPenaltyScorer { @@ -733,21 +725,17 @@ extern "C" fn FixedPenaltyScorer_Score_channel_penalty_msat(this_arg: *const c_v let mut ret = >::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()) }); ret } -extern "C" fn FixedPenaltyScorer_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 nativeFixedPenaltyScorer) }, &local_path[..], short_channel_id) +extern "C" fn FixedPenaltyScorer_Score_payment_path_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) { + >::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, path.get_native_ref(), short_channel_id) } -extern "C" fn FixedPenaltyScorer_Score_payment_path_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) { - let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); }; - >::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, &local_path[..]) +extern "C" fn FixedPenaltyScorer_Score_payment_path_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) { + >::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, path.get_native_ref()) } -extern "C" fn FixedPenaltyScorer_Score_probe_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() }); }; - >::probe_failed(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, &local_path[..], short_channel_id) +extern "C" fn FixedPenaltyScorer_Score_probe_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) { + >::probe_failed(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, path.get_native_ref(), short_channel_id) } -extern "C" fn FixedPenaltyScorer_Score_probe_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) { - let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); }; - >::probe_successful(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, &local_path[..]) +extern "C" fn FixedPenaltyScorer_Score_probe_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) { + >::probe_successful(unsafe { &mut *(this_arg as *mut nativeFixedPenaltyScorer) }, path.get_native_ref()) } #[no_mangle] @@ -835,7 +823,7 @@ pub extern "C" fn ProbabilisticScorer_free(this_obj: ProbabilisticScorer) { } #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn ProbabilisticScorer_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeProbabilisticScorer); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeProbabilisticScorer) }; } #[allow(unused)] impl ProbabilisticScorer { @@ -892,7 +880,7 @@ pub extern "C" fn ProbabilisticScoringParameters_free(this_obj: ProbabilisticSco #[allow(unused)] /// Used only if an object of this type is returned as a trait impl by a method pub(crate) extern "C" fn ProbabilisticScoringParameters_free_void(this_ptr: *mut c_void) { - unsafe { let _ = Box::from_raw(this_ptr as *mut nativeProbabilisticScoringParameters); } + let _ = unsafe { Box::from_raw(this_ptr as *mut nativeProbabilisticScoringParameters) }; } #[allow(unused)] impl ProbabilisticScoringParameters { @@ -1309,6 +1297,35 @@ pub extern "C" fn ProbabilisticScorer_estimated_channel_liquidity_range(this_arg local_ret } +/// Query the historical estimated minimum and maximum liquidity available for sending a +/// payment over the channel with `scid` towards the given `target` node. +/// +/// Returns two sets of 8 buckets. The first set describes the octiles for lower-bound +/// liquidity estimates, the second set describes the octiles for upper-bound liquidity +/// estimates. Each bucket describes the relative frequency at which we've seen a liquidity +/// bound in the octile relative to the channel's total capacity, on an arbitrary scale. +/// Because the values are slowly decayed, more recent data points are weighted more heavily +/// than older datapoints. +/// +/// When scoring, the estimated probability that an upper-/lower-bound lies in a given octile +/// relative to the channel's total capacity is calculated by dividing that bucket's value with +/// the total of all buckets for the given bound. +/// +/// For example, a value of `[0, 0, 0, 0, 0, 0, 32]` indicates that we believe the probability +/// of a bound being in the top octile to be 100%, and have never (recently) seen it in any +/// other octiles. A value of `[31, 0, 0, 0, 0, 0, 0, 32]` indicates we've seen the bound being +/// both in the top and bottom octile, and roughly with similar (recent) frequency. +/// +/// Because the datapoints are decayed slowly over time, values will eventually return to +/// `Some(([0; 8], [0; 8]))`. +#[must_use] +#[no_mangle] +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_EightU16sEightU16sZZ { + let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.historical_estimated_channel_liquidity_probabilities(scid, target.get_native_ref()); + let mut local_ret = if ret.is_none() { crate::c_types::derived::COption_C2Tuple_EightU16sEightU16sZZ::None } else { crate::c_types::derived::COption_C2Tuple_EightU16sEightU16sZZ::Some( { let (mut orig_ret_0_0, mut orig_ret_0_1) = (ret.unwrap()); let mut local_ret_0 = (crate::c_types::EightU16s { data: orig_ret_0_0 }, crate::c_types::EightU16s { data: orig_ret_0_1 }).into(); local_ret_0 }) }; + local_ret +} + /// Marks the node with the given `node_id` as banned, i.e., /// it will be avoided during path finding. #[no_mangle] @@ -1385,21 +1402,17 @@ extern "C" fn ProbabilisticScorer_Score_channel_penalty_msat(this_arg: *const c_ let mut ret = >::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()) }); ret } -extern "C" fn ProbabilisticScorer_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 nativeProbabilisticScorer) }, &local_path[..], short_channel_id) +extern "C" fn ProbabilisticScorer_Score_payment_path_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) { + >::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, path.get_native_ref(), short_channel_id) } -extern "C" fn ProbabilisticScorer_Score_payment_path_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) { - let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); }; - >::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, &local_path[..]) +extern "C" fn ProbabilisticScorer_Score_payment_path_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) { + >::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, path.get_native_ref()) } -extern "C" fn ProbabilisticScorer_Score_probe_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() }); }; - >::probe_failed(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, &local_path[..], short_channel_id) +extern "C" fn ProbabilisticScorer_Score_probe_failed(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path, mut short_channel_id: u64) { + >::probe_failed(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, path.get_native_ref(), short_channel_id) } -extern "C" fn ProbabilisticScorer_Score_probe_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) { - let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); }; - >::probe_successful(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, &local_path[..]) +extern "C" fn ProbabilisticScorer_Score_probe_successful(this_arg: *mut c_void, path: &crate::lightning::routing::router::Path) { + >::probe_successful(unsafe { &mut *(this_arg as *mut nativeProbabilisticScorer) }, path.get_native_ref()) } mod approx {