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 //! The router finds paths within a [`NetworkGraph`] for a payment.
11 use alloc::str::FromStr;
12 use core::ffi::c_void;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
16 #[cfg(feature="no-std")]
17 use alloc::{vec::Vec, boxed::Box};
20 use lightning::routing::router::DefaultRouter as nativeDefaultRouterImport;
21 pub(crate) type nativeDefaultRouter = nativeDefaultRouterImport<&'static lightning::routing::gossip::NetworkGraph<crate::lightning::util::logger::Logger>, crate::lightning::util::logger::Logger, crate::lightning::routing::scoring::LockableScore>;
23 /// A [`Router`] implemented using [`find_route`].
26 pub struct DefaultRouter {
27 /// A pointer to the opaque Rust object.
29 /// Nearly everywhere, inner must be non-null, however in places where
30 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
31 pub inner: *mut nativeDefaultRouter,
32 /// Indicates that this is the only struct which contains the same pointer.
34 /// Rust functions which take ownership of an object provided via an argument require
35 /// this to be true and invalidate the object pointed to by inner.
39 impl Drop for DefaultRouter {
41 if self.is_owned && !<*mut nativeDefaultRouter>::is_null(self.inner) {
42 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
46 /// Frees any resources used by the DefaultRouter, if is_owned is set and inner is non-NULL.
48 pub extern "C" fn DefaultRouter_free(this_obj: DefaultRouter) { }
50 /// Used only if an object of this type is returned as a trait impl by a method
51 pub(crate) extern "C" fn DefaultRouter_free_void(this_ptr: *mut c_void) {
52 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeDefaultRouter) };
56 pub(crate) fn get_native_ref(&self) -> &'static nativeDefaultRouter {
57 unsafe { &*ObjOps::untweak_ptr(self.inner) }
59 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeDefaultRouter {
60 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
62 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
63 pub(crate) fn take_inner(mut self) -> *mut nativeDefaultRouter {
64 assert!(self.is_owned);
65 let ret = ObjOps::untweak_ptr(self.inner);
66 self.inner = core::ptr::null_mut();
70 /// Creates a new router.
73 pub extern "C" fn DefaultRouter_new(network_graph: &crate::lightning::routing::gossip::NetworkGraph, mut logger: crate::lightning::util::logger::Logger, mut random_seed_bytes: crate::c_types::ThirtyTwoBytes, mut scorer: crate::lightning::routing::scoring::LockableScore) -> crate::lightning::routing::router::DefaultRouter {
74 let mut ret = lightning::routing::router::DefaultRouter::new(network_graph.get_native_ref(), logger, random_seed_bytes.data, scorer);
75 crate::lightning::routing::router::DefaultRouter { inner: ObjOps::heap_alloc(ret), is_owned: true }
78 impl From<nativeDefaultRouter> for crate::lightning::routing::router::Router {
79 fn from(obj: nativeDefaultRouter) -> Self {
80 let mut rust_obj = DefaultRouter { inner: ObjOps::heap_alloc(obj), is_owned: true };
81 let mut ret = DefaultRouter_as_Router(&rust_obj);
82 // 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
83 rust_obj.inner = core::ptr::null_mut();
84 ret.free = Some(DefaultRouter_free_void);
88 /// Constructs a new Router which calls the relevant methods on this_arg.
89 /// This copies the `inner` pointer in this_arg and thus the returned Router must be freed before this_arg is
91 pub extern "C" fn DefaultRouter_as_Router(this_arg: &DefaultRouter) -> crate::lightning::routing::router::Router {
92 crate::lightning::routing::router::Router {
93 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
95 find_route: DefaultRouter_Router_find_route,
96 find_route_with_id: DefaultRouter_Router_find_route_with_id,
101 extern "C" fn DefaultRouter_Router_find_route(this_arg: *const c_void, mut payer: crate::c_types::PublicKey, route_params: &crate::lightning::routing::router::RouteParameters, first_hops: *mut crate::c_types::derived::CVec_ChannelDetailsZ, inflight_htlcs: &crate::lightning::routing::router::InFlightHtlcs) -> crate::c_types::derived::CResult_RouteLightningErrorZ {
102 let mut local_first_hops_base = if first_hops == core::ptr::null_mut() { None } else { Some( { let mut local_first_hops_0 = Vec::new(); for mut item in unsafe { &mut *first_hops }.as_slice().iter() { local_first_hops_0.push( { item.get_native_ref() }); }; local_first_hops_0 }) }; let mut local_first_hops = local_first_hops_base.as_ref().map(|a| &a[..]);
103 let mut ret = <nativeDefaultRouter as lightning::routing::router::Router<>>::find_route(unsafe { &mut *(this_arg as *mut nativeDefaultRouter) }, &payer.into_rust(), route_params.get_native_ref(), local_first_hops, inflight_htlcs.get_native_ref());
104 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::Route { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
108 extern "C" fn DefaultRouter_Router_find_route_with_id(this_arg: *const c_void, mut payer: crate::c_types::PublicKey, route_params: &crate::lightning::routing::router::RouteParameters, first_hops: *mut crate::c_types::derived::CVec_ChannelDetailsZ, inflight_htlcs: &crate::lightning::routing::router::InFlightHtlcs, mut _payment_hash: crate::c_types::ThirtyTwoBytes, mut _payment_id: crate::c_types::ThirtyTwoBytes) -> crate::c_types::derived::CResult_RouteLightningErrorZ {
109 let mut local_first_hops_base = if first_hops == core::ptr::null_mut() { None } else { Some( { let mut local_first_hops_0 = Vec::new(); for mut item in unsafe { &mut *first_hops }.as_slice().iter() { local_first_hops_0.push( { item.get_native_ref() }); }; local_first_hops_0 }) }; let mut local_first_hops = local_first_hops_base.as_ref().map(|a| &a[..]);
110 let mut ret = <nativeDefaultRouter as lightning::routing::router::Router<>>::find_route_with_id(unsafe { &mut *(this_arg as *mut nativeDefaultRouter) }, &payer.into_rust(), route_params.get_native_ref(), local_first_hops, inflight_htlcs.get_native_ref(), ::lightning::ln::PaymentHash(_payment_hash.data), ::lightning::ln::channelmanager::PaymentId(_payment_id.data));
111 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::Route { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
115 /// A trait defining behavior for routing a payment.
118 /// An opaque pointer which is passed to your function implementations as an argument.
119 /// This has no meaning in the LDK, and can be NULL or any other value.
120 pub this_arg: *mut c_void,
121 /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values.
123 /// Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
125 pub find_route: extern "C" fn (this_arg: *const c_void, payer: crate::c_types::PublicKey, route_params: &crate::lightning::routing::router::RouteParameters, first_hops: *mut crate::c_types::derived::CVec_ChannelDetailsZ, inflight_htlcs: &crate::lightning::routing::router::InFlightHtlcs) -> crate::c_types::derived::CResult_RouteLightningErrorZ,
126 /// Finds a [`Route`] between `payer` and `payee` for a payment with the given values. Includes
127 /// `PaymentHash` and `PaymentId` to be able to correlate the request with a specific payment.
129 /// Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
131 pub find_route_with_id: extern "C" fn (this_arg: *const c_void, payer: crate::c_types::PublicKey, route_params: &crate::lightning::routing::router::RouteParameters, first_hops: *mut crate::c_types::derived::CVec_ChannelDetailsZ, inflight_htlcs: &crate::lightning::routing::router::InFlightHtlcs, _payment_hash: crate::c_types::ThirtyTwoBytes, _payment_id: crate::c_types::ThirtyTwoBytes) -> crate::c_types::derived::CResult_RouteLightningErrorZ,
132 /// Frees any resources associated with this object given its this_arg pointer.
133 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
134 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
136 unsafe impl Send for Router {}
137 unsafe impl Sync for Router {}
139 pub(crate) extern "C" fn Router_clone_fields(orig: &Router) -> Router {
141 this_arg: orig.this_arg,
142 find_route: Clone::clone(&orig.find_route),
143 find_route_with_id: Clone::clone(&orig.find_route_with_id),
144 free: Clone::clone(&orig.free),
148 use lightning::routing::router::Router as rustRouter;
149 impl rustRouter for Router {
150 fn find_route(&self, mut payer: &bitcoin::secp256k1::PublicKey, mut route_params: &lightning::routing::router::RouteParameters, mut first_hops: Option<&[&lightning::ln::channelmanager::ChannelDetails]>, mut inflight_htlcs: &lightning::routing::router::InFlightHtlcs) -> Result<lightning::routing::router::Route, lightning::ln::msgs::LightningError> {
151 let mut local_first_hops_base = if first_hops.is_none() { SmartPtr::null() } else { SmartPtr::from_obj( { let mut local_first_hops_0 = Vec::new(); for item in (first_hops.unwrap()).iter() { local_first_hops_0.push( { crate::lightning::ln::channelmanager::ChannelDetails { inner: unsafe { ObjOps::nonnull_ptr_to_inner(((*item) as *const lightning::ln::channelmanager::ChannelDetails<>) as *mut _) }, is_owned: false } }); }; local_first_hops_0.into() }) }; let mut local_first_hops = *local_first_hops_base;
152 let mut ret = (self.find_route)(self.this_arg, crate::c_types::PublicKey::from_rust(&payer), &crate::lightning::routing::router::RouteParameters { inner: unsafe { ObjOps::nonnull_ptr_to_inner((route_params as *const lightning::routing::router::RouteParameters<>) as *mut _) }, is_owned: false }, local_first_hops, &crate::lightning::routing::router::InFlightHtlcs { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inflight_htlcs as *const lightning::routing::router::InFlightHtlcs<>) as *mut _) }, is_owned: false });
153 let mut local_ret = match ret.result_ok { true => Ok( { *unsafe { Box::from_raw((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).take_inner()) } }), false => Err( { *unsafe { Box::from_raw((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).take_inner()) } })};
156 fn find_route_with_id(&self, mut payer: &bitcoin::secp256k1::PublicKey, mut route_params: &lightning::routing::router::RouteParameters, mut first_hops: Option<&[&lightning::ln::channelmanager::ChannelDetails]>, mut inflight_htlcs: &lightning::routing::router::InFlightHtlcs, mut _payment_hash: lightning::ln::PaymentHash, mut _payment_id: lightning::ln::channelmanager::PaymentId) -> Result<lightning::routing::router::Route, lightning::ln::msgs::LightningError> {
157 let mut local_first_hops_base = if first_hops.is_none() { SmartPtr::null() } else { SmartPtr::from_obj( { let mut local_first_hops_0 = Vec::new(); for item in (first_hops.unwrap()).iter() { local_first_hops_0.push( { crate::lightning::ln::channelmanager::ChannelDetails { inner: unsafe { ObjOps::nonnull_ptr_to_inner(((*item) as *const lightning::ln::channelmanager::ChannelDetails<>) as *mut _) }, is_owned: false } }); }; local_first_hops_0.into() }) }; let mut local_first_hops = *local_first_hops_base;
158 let mut ret = (self.find_route_with_id)(self.this_arg, crate::c_types::PublicKey::from_rust(&payer), &crate::lightning::routing::router::RouteParameters { inner: unsafe { ObjOps::nonnull_ptr_to_inner((route_params as *const lightning::routing::router::RouteParameters<>) as *mut _) }, is_owned: false }, local_first_hops, &crate::lightning::routing::router::InFlightHtlcs { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inflight_htlcs as *const lightning::routing::router::InFlightHtlcs<>) as *mut _) }, is_owned: false }, crate::c_types::ThirtyTwoBytes { data: _payment_hash.0 }, crate::c_types::ThirtyTwoBytes { data: _payment_id.0 });
159 let mut local_ret = match ret.result_ok { true => Ok( { *unsafe { Box::from_raw((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.result)) }).take_inner()) } }), false => Err( { *unsafe { Box::from_raw((*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut ret.contents.err)) }).take_inner()) } })};
164 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
165 // directly as a Deref trait in higher-level structs:
166 impl core::ops::Deref for Router {
168 fn deref(&self) -> &Self {
172 /// Calls the free function if one is set
174 pub extern "C" fn Router_free(this_ptr: Router) { }
175 impl Drop for Router {
177 if let Some(f) = self.free {
183 use lightning::routing::router::ScorerAccountingForInFlightHtlcs as nativeScorerAccountingForInFlightHtlcsImport;
184 pub(crate) type nativeScorerAccountingForInFlightHtlcs = nativeScorerAccountingForInFlightHtlcsImport<'static, crate::lightning::routing::scoring::Score>;
186 /// [`Score`] implementation that factors in in-flight HTLC liquidity.
188 /// Useful for custom [`Router`] implementations to wrap their [`Score`] on-the-fly when calling
191 /// [`Score`]: crate::routing::scoring::Score
194 pub struct ScorerAccountingForInFlightHtlcs {
195 /// A pointer to the opaque Rust object.
197 /// Nearly everywhere, inner must be non-null, however in places where
198 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
199 pub inner: *mut nativeScorerAccountingForInFlightHtlcs,
200 /// Indicates that this is the only struct which contains the same pointer.
202 /// Rust functions which take ownership of an object provided via an argument require
203 /// this to be true and invalidate the object pointed to by inner.
207 impl Drop for ScorerAccountingForInFlightHtlcs {
209 if self.is_owned && !<*mut nativeScorerAccountingForInFlightHtlcs>::is_null(self.inner) {
210 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
214 /// Frees any resources used by the ScorerAccountingForInFlightHtlcs, if is_owned is set and inner is non-NULL.
216 pub extern "C" fn ScorerAccountingForInFlightHtlcs_free(this_obj: ScorerAccountingForInFlightHtlcs) { }
218 /// Used only if an object of this type is returned as a trait impl by a method
219 pub(crate) extern "C" fn ScorerAccountingForInFlightHtlcs_free_void(this_ptr: *mut c_void) {
220 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeScorerAccountingForInFlightHtlcs) };
223 impl ScorerAccountingForInFlightHtlcs {
224 pub(crate) fn get_native_ref(&self) -> &'static nativeScorerAccountingForInFlightHtlcs {
225 unsafe { &*ObjOps::untweak_ptr(self.inner) }
227 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeScorerAccountingForInFlightHtlcs {
228 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
230 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
231 pub(crate) fn take_inner(mut self) -> *mut nativeScorerAccountingForInFlightHtlcs {
232 assert!(self.is_owned);
233 let ret = ObjOps::untweak_ptr(self.inner);
234 self.inner = core::ptr::null_mut();
238 /// Initialize a new `ScorerAccountingForInFlightHtlcs`.
241 pub extern "C" fn ScorerAccountingForInFlightHtlcs_new(mut scorer: crate::lightning::routing::scoring::Score, inflight_htlcs: &crate::lightning::routing::router::InFlightHtlcs) -> crate::lightning::routing::router::ScorerAccountingForInFlightHtlcs {
242 let mut ret = lightning::routing::router::ScorerAccountingForInFlightHtlcs::new(scorer, inflight_htlcs.get_native_ref());
243 crate::lightning::routing::router::ScorerAccountingForInFlightHtlcs { inner: ObjOps::heap_alloc(ret), is_owned: true }
247 /// Serialize the ScorerAccountingForInFlightHtlcs object into a byte array which can be read by ScorerAccountingForInFlightHtlcs_read
248 pub extern "C" fn ScorerAccountingForInFlightHtlcs_write(obj: &crate::lightning::routing::router::ScorerAccountingForInFlightHtlcs) -> crate::c_types::derived::CVec_u8Z {
249 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
252 pub(crate) extern "C" fn ScorerAccountingForInFlightHtlcs_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
253 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeScorerAccountingForInFlightHtlcs) })
255 impl From<nativeScorerAccountingForInFlightHtlcs> for crate::lightning::routing::scoring::Score {
256 fn from(obj: nativeScorerAccountingForInFlightHtlcs) -> Self {
257 let mut rust_obj = ScorerAccountingForInFlightHtlcs { inner: ObjOps::heap_alloc(obj), is_owned: true };
258 let mut ret = ScorerAccountingForInFlightHtlcs_as_Score(&rust_obj);
259 // 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
260 rust_obj.inner = core::ptr::null_mut();
261 ret.free = Some(ScorerAccountingForInFlightHtlcs_free_void);
265 /// Constructs a new Score which calls the relevant methods on this_arg.
266 /// This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
268 pub extern "C" fn ScorerAccountingForInFlightHtlcs_as_Score(this_arg: &ScorerAccountingForInFlightHtlcs) -> crate::lightning::routing::scoring::Score {
269 crate::lightning::routing::scoring::Score {
270 this_arg: unsafe { ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void },
272 channel_penalty_msat: ScorerAccountingForInFlightHtlcs_Score_channel_penalty_msat,
273 payment_path_failed: ScorerAccountingForInFlightHtlcs_Score_payment_path_failed,
274 payment_path_successful: ScorerAccountingForInFlightHtlcs_Score_payment_path_successful,
275 probe_failed: ScorerAccountingForInFlightHtlcs_Score_probe_failed,
276 probe_successful: ScorerAccountingForInFlightHtlcs_Score_probe_successful,
277 write: ScorerAccountingForInFlightHtlcs_write_void,
282 extern "C" fn ScorerAccountingForInFlightHtlcs_Score_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) -> u64 {
283 let mut ret = <nativeScorerAccountingForInFlightHtlcs as lightning::routing::scoring::Score<>>::channel_penalty_msat(unsafe { &mut *(this_arg as *mut nativeScorerAccountingForInFlightHtlcs) }, short_channel_id, source.get_native_ref(), target.get_native_ref(), *unsafe { Box::from_raw(usage.take_inner()) });
286 extern "C" fn ScorerAccountingForInFlightHtlcs_Score_payment_path_failed(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ, mut short_channel_id: u64) {
287 let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); };
288 <nativeScorerAccountingForInFlightHtlcs as lightning::routing::scoring::Score<>>::payment_path_failed(unsafe { &mut *(this_arg as *mut nativeScorerAccountingForInFlightHtlcs) }, &local_path[..], short_channel_id)
290 extern "C" fn ScorerAccountingForInFlightHtlcs_Score_payment_path_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) {
291 let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); };
292 <nativeScorerAccountingForInFlightHtlcs as lightning::routing::scoring::Score<>>::payment_path_successful(unsafe { &mut *(this_arg as *mut nativeScorerAccountingForInFlightHtlcs) }, &local_path[..])
294 extern "C" fn ScorerAccountingForInFlightHtlcs_Score_probe_failed(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ, mut short_channel_id: u64) {
295 let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); };
296 <nativeScorerAccountingForInFlightHtlcs as lightning::routing::scoring::Score<>>::probe_failed(unsafe { &mut *(this_arg as *mut nativeScorerAccountingForInFlightHtlcs) }, &local_path[..], short_channel_id)
298 extern "C" fn ScorerAccountingForInFlightHtlcs_Score_probe_successful(this_arg: *mut c_void, mut path: crate::c_types::derived::CVec_RouteHopZ) {
299 let mut local_path = Vec::new(); for mut item in path.as_slice().iter() { local_path.push( { item.get_native_ref() }); };
300 <nativeScorerAccountingForInFlightHtlcs as lightning::routing::scoring::Score<>>::probe_successful(unsafe { &mut *(this_arg as *mut nativeScorerAccountingForInFlightHtlcs) }, &local_path[..])
304 use lightning::routing::router::InFlightHtlcs as nativeInFlightHtlcsImport;
305 pub(crate) type nativeInFlightHtlcs = nativeInFlightHtlcsImport;
307 /// A data structure for tracking in-flight HTLCs. May be used during pathfinding to account for
308 /// in-use channel liquidity.
311 pub struct InFlightHtlcs {
312 /// A pointer to the opaque Rust object.
314 /// Nearly everywhere, inner must be non-null, however in places where
315 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
316 pub inner: *mut nativeInFlightHtlcs,
317 /// Indicates that this is the only struct which contains the same pointer.
319 /// Rust functions which take ownership of an object provided via an argument require
320 /// this to be true and invalidate the object pointed to by inner.
324 impl Drop for InFlightHtlcs {
326 if self.is_owned && !<*mut nativeInFlightHtlcs>::is_null(self.inner) {
327 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
331 /// Frees any resources used by the InFlightHtlcs, if is_owned is set and inner is non-NULL.
333 pub extern "C" fn InFlightHtlcs_free(this_obj: InFlightHtlcs) { }
335 /// Used only if an object of this type is returned as a trait impl by a method
336 pub(crate) extern "C" fn InFlightHtlcs_free_void(this_ptr: *mut c_void) {
337 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeInFlightHtlcs) };
341 pub(crate) fn get_native_ref(&self) -> &'static nativeInFlightHtlcs {
342 unsafe { &*ObjOps::untweak_ptr(self.inner) }
344 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeInFlightHtlcs {
345 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
347 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
348 pub(crate) fn take_inner(mut self) -> *mut nativeInFlightHtlcs {
349 assert!(self.is_owned);
350 let ret = ObjOps::untweak_ptr(self.inner);
351 self.inner = core::ptr::null_mut();
355 impl Clone for InFlightHtlcs {
356 fn clone(&self) -> Self {
358 inner: if <*mut nativeInFlightHtlcs>::is_null(self.inner) { core::ptr::null_mut() } else {
359 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
365 /// Used only if an object of this type is returned as a trait impl by a method
366 pub(crate) extern "C" fn InFlightHtlcs_clone_void(this_ptr: *const c_void) -> *mut c_void {
367 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeInFlightHtlcs)).clone() })) as *mut c_void
370 /// Creates a copy of the InFlightHtlcs
371 pub extern "C" fn InFlightHtlcs_clone(orig: &InFlightHtlcs) -> InFlightHtlcs {
374 /// Constructs an empty `InFlightHtlcs`.
377 pub extern "C" fn InFlightHtlcs_new() -> crate::lightning::routing::router::InFlightHtlcs {
378 let mut ret = lightning::routing::router::InFlightHtlcs::new();
379 crate::lightning::routing::router::InFlightHtlcs { inner: ObjOps::heap_alloc(ret), is_owned: true }
382 /// Returns liquidity in msat given the public key of the HTLC source, target, and short channel
386 pub extern "C" fn InFlightHtlcs_used_liquidity_msat(this_arg: &crate::lightning::routing::router::InFlightHtlcs, source: &crate::lightning::routing::gossip::NodeId, target: &crate::lightning::routing::gossip::NodeId, mut channel_scid: u64) -> crate::c_types::derived::COption_u64Z {
387 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.used_liquidity_msat(source.get_native_ref(), target.get_native_ref(), channel_scid);
388 let mut local_ret = if ret.is_none() { crate::c_types::derived::COption_u64Z::None } else { crate::c_types::derived::COption_u64Z::Some( { ret.unwrap() }) };
393 /// Serialize the InFlightHtlcs object into a byte array which can be read by InFlightHtlcs_read
394 pub extern "C" fn InFlightHtlcs_write(obj: &crate::lightning::routing::router::InFlightHtlcs) -> crate::c_types::derived::CVec_u8Z {
395 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
398 pub(crate) extern "C" fn InFlightHtlcs_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
399 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeInFlightHtlcs) })
402 /// Read a InFlightHtlcs from a byte array, created by InFlightHtlcs_write
403 pub extern "C" fn InFlightHtlcs_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_InFlightHtlcsDecodeErrorZ {
404 let res: Result<lightning::routing::router::InFlightHtlcs, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
405 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::InFlightHtlcs { 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() };
409 use lightning::routing::router::RouteHop as nativeRouteHopImport;
410 pub(crate) type nativeRouteHop = nativeRouteHopImport;
415 pub struct RouteHop {
416 /// A pointer to the opaque Rust object.
418 /// Nearly everywhere, inner must be non-null, however in places where
419 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
420 pub inner: *mut nativeRouteHop,
421 /// Indicates that this is the only struct which contains the same pointer.
423 /// Rust functions which take ownership of an object provided via an argument require
424 /// this to be true and invalidate the object pointed to by inner.
428 impl Drop for RouteHop {
430 if self.is_owned && !<*mut nativeRouteHop>::is_null(self.inner) {
431 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
435 /// Frees any resources used by the RouteHop, if is_owned is set and inner is non-NULL.
437 pub extern "C" fn RouteHop_free(this_obj: RouteHop) { }
439 /// Used only if an object of this type is returned as a trait impl by a method
440 pub(crate) extern "C" fn RouteHop_free_void(this_ptr: *mut c_void) {
441 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRouteHop) };
445 pub(crate) fn get_native_ref(&self) -> &'static nativeRouteHop {
446 unsafe { &*ObjOps::untweak_ptr(self.inner) }
448 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRouteHop {
449 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
451 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
452 pub(crate) fn take_inner(mut self) -> *mut nativeRouteHop {
453 assert!(self.is_owned);
454 let ret = ObjOps::untweak_ptr(self.inner);
455 self.inner = core::ptr::null_mut();
459 /// The node_id of the node at this hop.
461 pub extern "C" fn RouteHop_get_pubkey(this_ptr: &RouteHop) -> crate::c_types::PublicKey {
462 let mut inner_val = &mut this_ptr.get_native_mut_ref().pubkey;
463 crate::c_types::PublicKey::from_rust(&inner_val)
465 /// The node_id of the node at this hop.
467 pub extern "C" fn RouteHop_set_pubkey(this_ptr: &mut RouteHop, mut val: crate::c_types::PublicKey) {
468 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.pubkey = val.into_rust();
470 /// The node_announcement features of the node at this hop. For the last hop, these may be
471 /// amended to match the features present in the invoice this node generated.
473 pub extern "C" fn RouteHop_get_node_features(this_ptr: &RouteHop) -> crate::lightning::ln::features::NodeFeatures {
474 let mut inner_val = &mut this_ptr.get_native_mut_ref().node_features;
475 crate::lightning::ln::features::NodeFeatures { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const lightning::ln::features::NodeFeatures<>) as *mut _) }, is_owned: false }
477 /// The node_announcement features of the node at this hop. For the last hop, these may be
478 /// amended to match the features present in the invoice this node generated.
480 pub extern "C" fn RouteHop_set_node_features(this_ptr: &mut RouteHop, mut val: crate::lightning::ln::features::NodeFeatures) {
481 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.node_features = *unsafe { Box::from_raw(val.take_inner()) };
483 /// The channel that should be used from the previous hop to reach this node.
485 pub extern "C" fn RouteHop_get_short_channel_id(this_ptr: &RouteHop) -> u64 {
486 let mut inner_val = &mut this_ptr.get_native_mut_ref().short_channel_id;
489 /// The channel that should be used from the previous hop to reach this node.
491 pub extern "C" fn RouteHop_set_short_channel_id(this_ptr: &mut RouteHop, mut val: u64) {
492 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.short_channel_id = val;
494 /// The channel_announcement features of the channel that should be used from the previous hop
495 /// to reach this node.
497 pub extern "C" fn RouteHop_get_channel_features(this_ptr: &RouteHop) -> crate::lightning::ln::features::ChannelFeatures {
498 let mut inner_val = &mut this_ptr.get_native_mut_ref().channel_features;
499 crate::lightning::ln::features::ChannelFeatures { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const lightning::ln::features::ChannelFeatures<>) as *mut _) }, is_owned: false }
501 /// The channel_announcement features of the channel that should be used from the previous hop
502 /// to reach this node.
504 pub extern "C" fn RouteHop_set_channel_features(this_ptr: &mut RouteHop, mut val: crate::lightning::ln::features::ChannelFeatures) {
505 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.channel_features = *unsafe { Box::from_raw(val.take_inner()) };
507 /// The fee taken on this hop (for paying for the use of the *next* channel in the path).
508 /// For the last hop, this should be the full value of the payment (might be more than
509 /// requested if we had to match htlc_minimum_msat).
511 pub extern "C" fn RouteHop_get_fee_msat(this_ptr: &RouteHop) -> u64 {
512 let mut inner_val = &mut this_ptr.get_native_mut_ref().fee_msat;
515 /// The fee taken on this hop (for paying for the use of the *next* channel in the path).
516 /// For the last hop, this should be the full value of the payment (might be more than
517 /// requested if we had to match htlc_minimum_msat).
519 pub extern "C" fn RouteHop_set_fee_msat(this_ptr: &mut RouteHop, mut val: u64) {
520 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.fee_msat = val;
522 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
523 /// expected at the destination, in excess of the current block height.
525 pub extern "C" fn RouteHop_get_cltv_expiry_delta(this_ptr: &RouteHop) -> u32 {
526 let mut inner_val = &mut this_ptr.get_native_mut_ref().cltv_expiry_delta;
529 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
530 /// expected at the destination, in excess of the current block height.
532 pub extern "C" fn RouteHop_set_cltv_expiry_delta(this_ptr: &mut RouteHop, mut val: u32) {
533 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.cltv_expiry_delta = val;
535 /// Constructs a new RouteHop given each field
538 pub extern "C" fn RouteHop_new(mut pubkey_arg: crate::c_types::PublicKey, mut node_features_arg: crate::lightning::ln::features::NodeFeatures, mut short_channel_id_arg: u64, mut channel_features_arg: crate::lightning::ln::features::ChannelFeatures, mut fee_msat_arg: u64, mut cltv_expiry_delta_arg: u32) -> RouteHop {
539 RouteHop { inner: ObjOps::heap_alloc(nativeRouteHop {
540 pubkey: pubkey_arg.into_rust(),
541 node_features: *unsafe { Box::from_raw(node_features_arg.take_inner()) },
542 short_channel_id: short_channel_id_arg,
543 channel_features: *unsafe { Box::from_raw(channel_features_arg.take_inner()) },
544 fee_msat: fee_msat_arg,
545 cltv_expiry_delta: cltv_expiry_delta_arg,
548 impl Clone for RouteHop {
549 fn clone(&self) -> Self {
551 inner: if <*mut nativeRouteHop>::is_null(self.inner) { core::ptr::null_mut() } else {
552 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
558 /// Used only if an object of this type is returned as a trait impl by a method
559 pub(crate) extern "C" fn RouteHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
560 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHop)).clone() })) as *mut c_void
563 /// Creates a copy of the RouteHop
564 pub extern "C" fn RouteHop_clone(orig: &RouteHop) -> RouteHop {
567 /// Checks if two RouteHops contain equal inner contents.
569 pub extern "C" fn RouteHop_hash(o: &RouteHop) -> u64 {
570 if o.inner.is_null() { return 0; }
571 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
573 let mut hasher = core::hash::SipHasher::new();
574 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
575 core::hash::Hasher::finish(&hasher)
577 /// Checks if two RouteHops contain equal inner contents.
578 /// This ignores pointers and is_owned flags and looks at the values in fields.
579 /// Two objects with NULL inner values will be considered "equal" here.
581 pub extern "C" fn RouteHop_eq(a: &RouteHop, b: &RouteHop) -> bool {
582 if a.inner == b.inner { return true; }
583 if a.inner.is_null() || b.inner.is_null() { return false; }
584 if a.get_native_ref() == b.get_native_ref() { true } else { false }
587 /// Serialize the RouteHop object into a byte array which can be read by RouteHop_read
588 pub extern "C" fn RouteHop_write(obj: &crate::lightning::routing::router::RouteHop) -> crate::c_types::derived::CVec_u8Z {
589 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
592 pub(crate) extern "C" fn RouteHop_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
593 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRouteHop) })
596 /// Read a RouteHop from a byte array, created by RouteHop_write
597 pub extern "C" fn RouteHop_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RouteHopDecodeErrorZ {
598 let res: Result<lightning::routing::router::RouteHop, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
599 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::RouteHop { 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() };
603 use lightning::routing::router::Route as nativeRouteImport;
604 pub(crate) type nativeRoute = nativeRouteImport;
606 /// A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
607 /// it can take multiple paths. Each path is composed of one or more hops through the network.
611 /// A pointer to the opaque Rust object.
613 /// Nearly everywhere, inner must be non-null, however in places where
614 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
615 pub inner: *mut nativeRoute,
616 /// Indicates that this is the only struct which contains the same pointer.
618 /// Rust functions which take ownership of an object provided via an argument require
619 /// this to be true and invalidate the object pointed to by inner.
623 impl Drop for Route {
625 if self.is_owned && !<*mut nativeRoute>::is_null(self.inner) {
626 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
630 /// Frees any resources used by the Route, if is_owned is set and inner is non-NULL.
632 pub extern "C" fn Route_free(this_obj: Route) { }
634 /// Used only if an object of this type is returned as a trait impl by a method
635 pub(crate) extern "C" fn Route_free_void(this_ptr: *mut c_void) {
636 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRoute) };
640 pub(crate) fn get_native_ref(&self) -> &'static nativeRoute {
641 unsafe { &*ObjOps::untweak_ptr(self.inner) }
643 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRoute {
644 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
646 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
647 pub(crate) fn take_inner(mut self) -> *mut nativeRoute {
648 assert!(self.is_owned);
649 let ret = ObjOps::untweak_ptr(self.inner);
650 self.inner = core::ptr::null_mut();
654 /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
655 /// last RouteHop in each path must be the same. Each entry represents a list of hops, NOT
656 /// INCLUDING our own, where the last hop is the destination. Thus, this must always be at
657 /// least length one. While the maximum length of any given path is variable, keeping the length
658 /// of any path less or equal to 19 should currently ensure it is viable.
660 pub extern "C" fn Route_get_paths(this_ptr: &Route) -> crate::c_types::derived::CVec_CVec_RouteHopZZ {
661 let mut inner_val = &mut this_ptr.get_native_mut_ref().paths;
662 let mut local_inner_val = Vec::new(); for item in inner_val.iter() { local_inner_val.push( { let mut local_inner_val_0 = Vec::new(); for item in item.iter() { local_inner_val_0.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 } }); }; local_inner_val_0.into() }); };
663 local_inner_val.into()
665 /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
666 /// last RouteHop in each path must be the same. Each entry represents a list of hops, NOT
667 /// INCLUDING our own, where the last hop is the destination. Thus, this must always be at
668 /// least length one. While the maximum length of any given path is variable, keeping the length
669 /// of any path less or equal to 19 should currently ensure it is viable.
671 pub extern "C" fn Route_set_paths(this_ptr: &mut Route, mut val: crate::c_types::derived::CVec_CVec_RouteHopZZ) {
672 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { let mut local_val_0 = Vec::new(); for mut item in item.into_rust().drain(..) { local_val_0.push( { *unsafe { Box::from_raw(item.take_inner()) } }); }; local_val_0 }); };
673 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.paths = local_val;
675 /// The `payment_params` parameter passed to [`find_route`].
676 /// This is used by `ChannelManager` to track information which may be required for retries,
677 /// provided back to you via [`Event::PaymentPathFailed`].
679 /// [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
681 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
683 pub extern "C" fn Route_get_payment_params(this_ptr: &Route) -> crate::lightning::routing::router::PaymentParameters {
684 let mut inner_val = &mut this_ptr.get_native_mut_ref().payment_params;
685 let mut local_inner_val = crate::lightning::routing::router::PaymentParameters { inner: unsafe { (if inner_val.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (inner_val.as_ref().unwrap()) }) } as *const lightning::routing::router::PaymentParameters<>) as *mut _ }, is_owned: false };
688 /// The `payment_params` parameter passed to [`find_route`].
689 /// This is used by `ChannelManager` to track information which may be required for retries,
690 /// provided back to you via [`Event::PaymentPathFailed`].
692 /// [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
694 /// Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None
696 pub extern "C" fn Route_set_payment_params(this_ptr: &mut Route, mut val: crate::lightning::routing::router::PaymentParameters) {
697 let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) };
698 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.payment_params = local_val;
700 /// Constructs a new Route given each field
703 pub extern "C" fn Route_new(mut paths_arg: crate::c_types::derived::CVec_CVec_RouteHopZZ, mut payment_params_arg: crate::lightning::routing::router::PaymentParameters) -> Route {
704 let mut local_paths_arg = Vec::new(); for mut item in paths_arg.into_rust().drain(..) { local_paths_arg.push( { let mut local_paths_arg_0 = Vec::new(); for mut item in item.into_rust().drain(..) { local_paths_arg_0.push( { *unsafe { Box::from_raw(item.take_inner()) } }); }; local_paths_arg_0 }); };
705 let mut local_payment_params_arg = if payment_params_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(payment_params_arg.take_inner()) } }) };
706 Route { inner: ObjOps::heap_alloc(nativeRoute {
707 paths: local_paths_arg,
708 payment_params: local_payment_params_arg,
711 impl Clone for Route {
712 fn clone(&self) -> Self {
714 inner: if <*mut nativeRoute>::is_null(self.inner) { core::ptr::null_mut() } else {
715 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
721 /// Used only if an object of this type is returned as a trait impl by a method
722 pub(crate) extern "C" fn Route_clone_void(this_ptr: *const c_void) -> *mut c_void {
723 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRoute)).clone() })) as *mut c_void
726 /// Creates a copy of the Route
727 pub extern "C" fn Route_clone(orig: &Route) -> Route {
730 /// Checks if two Routes contain equal inner contents.
732 pub extern "C" fn Route_hash(o: &Route) -> u64 {
733 if o.inner.is_null() { return 0; }
734 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
736 let mut hasher = core::hash::SipHasher::new();
737 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
738 core::hash::Hasher::finish(&hasher)
740 /// Checks if two Routes contain equal inner contents.
741 /// This ignores pointers and is_owned flags and looks at the values in fields.
742 /// Two objects with NULL inner values will be considered "equal" here.
744 pub extern "C" fn Route_eq(a: &Route, b: &Route) -> bool {
745 if a.inner == b.inner { return true; }
746 if a.inner.is_null() || b.inner.is_null() { return false; }
747 if a.get_native_ref() == b.get_native_ref() { true } else { false }
749 /// Returns the total amount of fees paid on this [`Route`].
751 /// This doesn't include any extra payment made to the recipient, which can happen in excess of
752 /// the amount passed to [`find_route`]'s `params.final_value_msat`.
755 pub extern "C" fn Route_get_total_fees(this_arg: &crate::lightning::routing::router::Route) -> u64 {
756 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_total_fees();
760 /// Returns the total amount paid on this [`Route`], excluding the fees.
763 pub extern "C" fn Route_get_total_amount(this_arg: &crate::lightning::routing::router::Route) -> u64 {
764 let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_total_amount();
769 /// Serialize the Route object into a byte array which can be read by Route_read
770 pub extern "C" fn Route_write(obj: &crate::lightning::routing::router::Route) -> crate::c_types::derived::CVec_u8Z {
771 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
774 pub(crate) extern "C" fn Route_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
775 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRoute) })
778 /// Read a Route from a byte array, created by Route_write
779 pub extern "C" fn Route_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RouteDecodeErrorZ {
780 let res: Result<lightning::routing::router::Route, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
781 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::Route { 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() };
785 use lightning::routing::router::RouteParameters as nativeRouteParametersImport;
786 pub(crate) type nativeRouteParameters = nativeRouteParametersImport;
788 /// Parameters needed to find a [`Route`].
790 /// Passed to [`find_route`] and [`build_route_from_hops`], but also provided in
791 /// [`Event::PaymentPathFailed`].
793 /// [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
796 pub struct RouteParameters {
797 /// A pointer to the opaque Rust object.
799 /// Nearly everywhere, inner must be non-null, however in places where
800 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
801 pub inner: *mut nativeRouteParameters,
802 /// Indicates that this is the only struct which contains the same pointer.
804 /// Rust functions which take ownership of an object provided via an argument require
805 /// this to be true and invalidate the object pointed to by inner.
809 impl Drop for RouteParameters {
811 if self.is_owned && !<*mut nativeRouteParameters>::is_null(self.inner) {
812 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
816 /// Frees any resources used by the RouteParameters, if is_owned is set and inner is non-NULL.
818 pub extern "C" fn RouteParameters_free(this_obj: RouteParameters) { }
820 /// Used only if an object of this type is returned as a trait impl by a method
821 pub(crate) extern "C" fn RouteParameters_free_void(this_ptr: *mut c_void) {
822 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRouteParameters) };
825 impl RouteParameters {
826 pub(crate) fn get_native_ref(&self) -> &'static nativeRouteParameters {
827 unsafe { &*ObjOps::untweak_ptr(self.inner) }
829 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRouteParameters {
830 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
832 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
833 pub(crate) fn take_inner(mut self) -> *mut nativeRouteParameters {
834 assert!(self.is_owned);
835 let ret = ObjOps::untweak_ptr(self.inner);
836 self.inner = core::ptr::null_mut();
840 /// The parameters of the failed payment path.
842 pub extern "C" fn RouteParameters_get_payment_params(this_ptr: &RouteParameters) -> crate::lightning::routing::router::PaymentParameters {
843 let mut inner_val = &mut this_ptr.get_native_mut_ref().payment_params;
844 crate::lightning::routing::router::PaymentParameters { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const lightning::routing::router::PaymentParameters<>) as *mut _) }, is_owned: false }
846 /// The parameters of the failed payment path.
848 pub extern "C" fn RouteParameters_set_payment_params(this_ptr: &mut RouteParameters, mut val: crate::lightning::routing::router::PaymentParameters) {
849 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.payment_params = *unsafe { Box::from_raw(val.take_inner()) };
851 /// The amount in msats sent on the failed payment path.
853 pub extern "C" fn RouteParameters_get_final_value_msat(this_ptr: &RouteParameters) -> u64 {
854 let mut inner_val = &mut this_ptr.get_native_mut_ref().final_value_msat;
857 /// The amount in msats sent on the failed payment path.
859 pub extern "C" fn RouteParameters_set_final_value_msat(this_ptr: &mut RouteParameters, mut val: u64) {
860 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.final_value_msat = val;
862 /// Constructs a new RouteParameters given each field
865 pub extern "C" fn RouteParameters_new(mut payment_params_arg: crate::lightning::routing::router::PaymentParameters, mut final_value_msat_arg: u64) -> RouteParameters {
866 RouteParameters { inner: ObjOps::heap_alloc(nativeRouteParameters {
867 payment_params: *unsafe { Box::from_raw(payment_params_arg.take_inner()) },
868 final_value_msat: final_value_msat_arg,
871 impl Clone for RouteParameters {
872 fn clone(&self) -> Self {
874 inner: if <*mut nativeRouteParameters>::is_null(self.inner) { core::ptr::null_mut() } else {
875 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
881 /// Used only if an object of this type is returned as a trait impl by a method
882 pub(crate) extern "C" fn RouteParameters_clone_void(this_ptr: *const c_void) -> *mut c_void {
883 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteParameters)).clone() })) as *mut c_void
886 /// Creates a copy of the RouteParameters
887 pub extern "C" fn RouteParameters_clone(orig: &RouteParameters) -> RouteParameters {
890 /// Checks if two RouteParameterss contain equal inner contents.
891 /// This ignores pointers and is_owned flags and looks at the values in fields.
892 /// Two objects with NULL inner values will be considered "equal" here.
894 pub extern "C" fn RouteParameters_eq(a: &RouteParameters, b: &RouteParameters) -> bool {
895 if a.inner == b.inner { return true; }
896 if a.inner.is_null() || b.inner.is_null() { return false; }
897 if a.get_native_ref() == b.get_native_ref() { true } else { false }
900 /// Serialize the RouteParameters object into a byte array which can be read by RouteParameters_read
901 pub extern "C" fn RouteParameters_write(obj: &crate::lightning::routing::router::RouteParameters) -> crate::c_types::derived::CVec_u8Z {
902 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
905 pub(crate) extern "C" fn RouteParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
906 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRouteParameters) })
909 /// Read a RouteParameters from a byte array, created by RouteParameters_write
910 pub extern "C" fn RouteParameters_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RouteParametersDecodeErrorZ {
911 let res: Result<lightning::routing::router::RouteParameters, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
912 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::RouteParameters { 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() };
915 /// Maximum total CTLV difference we allow for a full payment path.
918 pub static DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA: u32 = lightning::routing::router::DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA;
919 /// Maximum number of paths we allow an (MPP) payment to have.
922 pub static DEFAULT_MAX_PATH_COUNT: u8 = lightning::routing::router::DEFAULT_MAX_PATH_COUNT;
924 use lightning::routing::router::PaymentParameters as nativePaymentParametersImport;
925 pub(crate) type nativePaymentParameters = nativePaymentParametersImport;
927 /// The recipient of a payment.
930 pub struct PaymentParameters {
931 /// A pointer to the opaque Rust object.
933 /// Nearly everywhere, inner must be non-null, however in places where
934 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
935 pub inner: *mut nativePaymentParameters,
936 /// Indicates that this is the only struct which contains the same pointer.
938 /// Rust functions which take ownership of an object provided via an argument require
939 /// this to be true and invalidate the object pointed to by inner.
943 impl Drop for PaymentParameters {
945 if self.is_owned && !<*mut nativePaymentParameters>::is_null(self.inner) {
946 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
950 /// Frees any resources used by the PaymentParameters, if is_owned is set and inner is non-NULL.
952 pub extern "C" fn PaymentParameters_free(this_obj: PaymentParameters) { }
954 /// Used only if an object of this type is returned as a trait impl by a method
955 pub(crate) extern "C" fn PaymentParameters_free_void(this_ptr: *mut c_void) {
956 let _ = unsafe { Box::from_raw(this_ptr as *mut nativePaymentParameters) };
959 impl PaymentParameters {
960 pub(crate) fn get_native_ref(&self) -> &'static nativePaymentParameters {
961 unsafe { &*ObjOps::untweak_ptr(self.inner) }
963 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativePaymentParameters {
964 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
966 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
967 pub(crate) fn take_inner(mut self) -> *mut nativePaymentParameters {
968 assert!(self.is_owned);
969 let ret = ObjOps::untweak_ptr(self.inner);
970 self.inner = core::ptr::null_mut();
974 /// The node id of the payee.
976 pub extern "C" fn PaymentParameters_get_payee_pubkey(this_ptr: &PaymentParameters) -> crate::c_types::PublicKey {
977 let mut inner_val = &mut this_ptr.get_native_mut_ref().payee_pubkey;
978 crate::c_types::PublicKey::from_rust(&inner_val)
980 /// The node id of the payee.
982 pub extern "C" fn PaymentParameters_set_payee_pubkey(this_ptr: &mut PaymentParameters, mut val: crate::c_types::PublicKey) {
983 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.payee_pubkey = val.into_rust();
985 /// Features supported by the payee.
987 /// May be set from the payee's invoice or via [`for_keysend`]. May be `None` if the invoice
988 /// does not contain any features.
990 /// [`for_keysend`]: Self::for_keysend
992 /// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
994 pub extern "C" fn PaymentParameters_get_features(this_ptr: &PaymentParameters) -> crate::lightning::ln::features::InvoiceFeatures {
995 let mut inner_val = &mut this_ptr.get_native_mut_ref().features;
996 let mut local_inner_val = crate::lightning::ln::features::InvoiceFeatures { inner: unsafe { (if inner_val.is_none() { core::ptr::null() } else { ObjOps::nonnull_ptr_to_inner( { (inner_val.as_ref().unwrap()) }) } as *const lightning::ln::features::InvoiceFeatures<>) as *mut _ }, is_owned: false };
999 /// Features supported by the payee.
1001 /// May be set from the payee's invoice or via [`for_keysend`]. May be `None` if the invoice
1002 /// does not contain any features.
1004 /// [`for_keysend`]: Self::for_keysend
1006 /// Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None
1008 pub extern "C" fn PaymentParameters_set_features(this_ptr: &mut PaymentParameters, mut val: crate::lightning::ln::features::InvoiceFeatures) {
1009 let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) };
1010 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.features = local_val;
1012 /// Hints for routing to the payee, containing channels connecting the payee to public nodes.
1014 pub extern "C" fn PaymentParameters_get_route_hints(this_ptr: &PaymentParameters) -> crate::c_types::derived::CVec_RouteHintZ {
1015 let mut inner_val = &mut this_ptr.get_native_mut_ref().route_hints;
1016 let mut local_inner_val = Vec::new(); for item in inner_val.iter() { local_inner_val.push( { crate::lightning::routing::router::RouteHint { inner: unsafe { ObjOps::nonnull_ptr_to_inner((item as *const lightning::routing::router::RouteHint<>) as *mut _) }, is_owned: false } }); };
1017 local_inner_val.into()
1019 /// Hints for routing to the payee, containing channels connecting the payee to public nodes.
1021 pub extern "C" fn PaymentParameters_set_route_hints(this_ptr: &mut PaymentParameters, mut val: crate::c_types::derived::CVec_RouteHintZ) {
1022 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
1023 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.route_hints = local_val;
1025 /// Expiration of a payment to the payee, in seconds relative to the UNIX epoch.
1027 pub extern "C" fn PaymentParameters_get_expiry_time(this_ptr: &PaymentParameters) -> crate::c_types::derived::COption_u64Z {
1028 let mut inner_val = &mut this_ptr.get_native_mut_ref().expiry_time;
1029 let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u64Z::None } else { crate::c_types::derived::COption_u64Z::Some( { inner_val.unwrap() }) };
1032 /// Expiration of a payment to the payee, in seconds relative to the UNIX epoch.
1034 pub extern "C" fn PaymentParameters_set_expiry_time(this_ptr: &mut PaymentParameters, mut val: crate::c_types::derived::COption_u64Z) {
1035 let mut local_val = if val.is_some() { Some( { val.take() }) } else { None };
1036 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.expiry_time = local_val;
1038 /// The maximum total CLTV delta we accept for the route.
1039 /// Defaults to [`DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA`].
1041 pub extern "C" fn PaymentParameters_get_max_total_cltv_expiry_delta(this_ptr: &PaymentParameters) -> u32 {
1042 let mut inner_val = &mut this_ptr.get_native_mut_ref().max_total_cltv_expiry_delta;
1045 /// The maximum total CLTV delta we accept for the route.
1046 /// Defaults to [`DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA`].
1048 pub extern "C" fn PaymentParameters_set_max_total_cltv_expiry_delta(this_ptr: &mut PaymentParameters, mut val: u32) {
1049 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.max_total_cltv_expiry_delta = val;
1051 /// The maximum number of paths that may be used by (MPP) payments.
1052 /// Defaults to [`DEFAULT_MAX_PATH_COUNT`].
1054 pub extern "C" fn PaymentParameters_get_max_path_count(this_ptr: &PaymentParameters) -> u8 {
1055 let mut inner_val = &mut this_ptr.get_native_mut_ref().max_path_count;
1058 /// The maximum number of paths that may be used by (MPP) payments.
1059 /// Defaults to [`DEFAULT_MAX_PATH_COUNT`].
1061 pub extern "C" fn PaymentParameters_set_max_path_count(this_ptr: &mut PaymentParameters, mut val: u8) {
1062 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.max_path_count = val;
1064 /// Selects the maximum share of a channel's total capacity which will be sent over a channel,
1065 /// as a power of 1/2. A higher value prefers to send the payment using more MPP parts whereas
1066 /// a lower value prefers to send larger MPP parts, potentially saturating channels and
1067 /// increasing failure probability for those paths.
1069 /// Note that this restriction will be relaxed during pathfinding after paths which meet this
1070 /// restriction have been found. While paths which meet this criteria will be searched for, it
1071 /// is ultimately up to the scorer to select them over other paths.
1073 /// A value of 0 will allow payments up to and including a channel's total announced usable
1074 /// capacity, a value of one will only use up to half its capacity, two 1/4, etc.
1076 /// Default value: 2
1078 pub extern "C" fn PaymentParameters_get_max_channel_saturation_power_of_half(this_ptr: &PaymentParameters) -> u8 {
1079 let mut inner_val = &mut this_ptr.get_native_mut_ref().max_channel_saturation_power_of_half;
1082 /// Selects the maximum share of a channel's total capacity which will be sent over a channel,
1083 /// as a power of 1/2. A higher value prefers to send the payment using more MPP parts whereas
1084 /// a lower value prefers to send larger MPP parts, potentially saturating channels and
1085 /// increasing failure probability for those paths.
1087 /// Note that this restriction will be relaxed during pathfinding after paths which meet this
1088 /// restriction have been found. While paths which meet this criteria will be searched for, it
1089 /// is ultimately up to the scorer to select them over other paths.
1091 /// A value of 0 will allow payments up to and including a channel's total announced usable
1092 /// capacity, a value of one will only use up to half its capacity, two 1/4, etc.
1094 /// Default value: 2
1096 pub extern "C" fn PaymentParameters_set_max_channel_saturation_power_of_half(this_ptr: &mut PaymentParameters, mut val: u8) {
1097 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.max_channel_saturation_power_of_half = val;
1099 /// A list of SCIDs which this payment was previously attempted over and which caused the
1100 /// payment to fail. Future attempts for the same payment shouldn't be relayed through any of
1103 /// Returns a copy of the field.
1105 pub extern "C" fn PaymentParameters_get_previously_failed_channels(this_ptr: &PaymentParameters) -> crate::c_types::derived::CVec_u64Z {
1106 let mut inner_val = this_ptr.get_native_mut_ref().previously_failed_channels.clone();
1107 let mut local_inner_val = Vec::new(); for mut item in inner_val.drain(..) { local_inner_val.push( { item }); };
1108 local_inner_val.into()
1110 /// A list of SCIDs which this payment was previously attempted over and which caused the
1111 /// payment to fail. Future attempts for the same payment shouldn't be relayed through any of
1114 pub extern "C" fn PaymentParameters_set_previously_failed_channels(this_ptr: &mut PaymentParameters, mut val: crate::c_types::derived::CVec_u64Z) {
1115 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item }); };
1116 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.previously_failed_channels = local_val;
1118 /// The minimum CLTV delta at the end of the route. This value must not be zero.
1120 pub extern "C" fn PaymentParameters_get_final_cltv_expiry_delta(this_ptr: &PaymentParameters) -> u32 {
1121 let mut inner_val = &mut this_ptr.get_native_mut_ref().final_cltv_expiry_delta;
1124 /// The minimum CLTV delta at the end of the route. This value must not be zero.
1126 pub extern "C" fn PaymentParameters_set_final_cltv_expiry_delta(this_ptr: &mut PaymentParameters, mut val: u32) {
1127 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.final_cltv_expiry_delta = val;
1129 /// Constructs a new PaymentParameters given each field
1132 pub extern "C" fn PaymentParameters_new(mut payee_pubkey_arg: crate::c_types::PublicKey, mut features_arg: crate::lightning::ln::features::InvoiceFeatures, mut route_hints_arg: crate::c_types::derived::CVec_RouteHintZ, mut expiry_time_arg: crate::c_types::derived::COption_u64Z, mut max_total_cltv_expiry_delta_arg: u32, mut max_path_count_arg: u8, mut max_channel_saturation_power_of_half_arg: u8, mut previously_failed_channels_arg: crate::c_types::derived::CVec_u64Z, mut final_cltv_expiry_delta_arg: u32) -> PaymentParameters {
1133 let mut local_features_arg = if features_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(features_arg.take_inner()) } }) };
1134 let mut local_route_hints_arg = Vec::new(); for mut item in route_hints_arg.into_rust().drain(..) { local_route_hints_arg.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
1135 let mut local_expiry_time_arg = if expiry_time_arg.is_some() { Some( { expiry_time_arg.take() }) } else { None };
1136 let mut local_previously_failed_channels_arg = Vec::new(); for mut item in previously_failed_channels_arg.into_rust().drain(..) { local_previously_failed_channels_arg.push( { item }); };
1137 PaymentParameters { inner: ObjOps::heap_alloc(nativePaymentParameters {
1138 payee_pubkey: payee_pubkey_arg.into_rust(),
1139 features: local_features_arg,
1140 route_hints: local_route_hints_arg,
1141 expiry_time: local_expiry_time_arg,
1142 max_total_cltv_expiry_delta: max_total_cltv_expiry_delta_arg,
1143 max_path_count: max_path_count_arg,
1144 max_channel_saturation_power_of_half: max_channel_saturation_power_of_half_arg,
1145 previously_failed_channels: local_previously_failed_channels_arg,
1146 final_cltv_expiry_delta: final_cltv_expiry_delta_arg,
1147 }), is_owned: true }
1149 impl Clone for PaymentParameters {
1150 fn clone(&self) -> Self {
1152 inner: if <*mut nativePaymentParameters>::is_null(self.inner) { core::ptr::null_mut() } else {
1153 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
1159 /// Used only if an object of this type is returned as a trait impl by a method
1160 pub(crate) extern "C" fn PaymentParameters_clone_void(this_ptr: *const c_void) -> *mut c_void {
1161 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativePaymentParameters)).clone() })) as *mut c_void
1164 /// Creates a copy of the PaymentParameters
1165 pub extern "C" fn PaymentParameters_clone(orig: &PaymentParameters) -> PaymentParameters {
1168 /// Checks if two PaymentParameterss contain equal inner contents.
1170 pub extern "C" fn PaymentParameters_hash(o: &PaymentParameters) -> u64 {
1171 if o.inner.is_null() { return 0; }
1172 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
1173 #[allow(deprecated)]
1174 let mut hasher = core::hash::SipHasher::new();
1175 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
1176 core::hash::Hasher::finish(&hasher)
1178 /// Checks if two PaymentParameterss contain equal inner contents.
1179 /// This ignores pointers and is_owned flags and looks at the values in fields.
1180 /// Two objects with NULL inner values will be considered "equal" here.
1182 pub extern "C" fn PaymentParameters_eq(a: &PaymentParameters, b: &PaymentParameters) -> bool {
1183 if a.inner == b.inner { return true; }
1184 if a.inner.is_null() || b.inner.is_null() { return false; }
1185 if a.get_native_ref() == b.get_native_ref() { true } else { false }
1188 /// Serialize the PaymentParameters object into a byte array which can be read by PaymentParameters_read
1189 pub extern "C" fn PaymentParameters_write(obj: &crate::lightning::routing::router::PaymentParameters) -> crate::c_types::derived::CVec_u8Z {
1190 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
1193 pub(crate) extern "C" fn PaymentParameters_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1194 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativePaymentParameters) })
1197 /// Read a PaymentParameters from a byte array, created by PaymentParameters_write
1198 pub extern "C" fn PaymentParameters_read(ser: crate::c_types::u8slice, arg: u32) -> crate::c_types::derived::CResult_PaymentParametersDecodeErrorZ {
1200 let res: Result<lightning::routing::router::PaymentParameters, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj_arg(ser, arg_conv);
1201 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::PaymentParameters { 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() };
1204 /// Creates a payee with the node id of the given `pubkey`.
1206 /// The `final_cltv_expiry_delta` should match the expected final CLTV delta the recipient has
1210 pub extern "C" fn PaymentParameters_from_node_id(mut payee_pubkey: crate::c_types::PublicKey, mut final_cltv_expiry_delta: u32) -> crate::lightning::routing::router::PaymentParameters {
1211 let mut ret = lightning::routing::router::PaymentParameters::from_node_id(payee_pubkey.into_rust(), final_cltv_expiry_delta);
1212 crate::lightning::routing::router::PaymentParameters { inner: ObjOps::heap_alloc(ret), is_owned: true }
1215 /// Creates a payee with the node id of the given `pubkey` to use for keysend payments.
1217 /// The `final_cltv_expiry_delta` should match the expected final CLTV delta the recipient has
1221 pub extern "C" fn PaymentParameters_for_keysend(mut payee_pubkey: crate::c_types::PublicKey, mut final_cltv_expiry_delta: u32) -> crate::lightning::routing::router::PaymentParameters {
1222 let mut ret = lightning::routing::router::PaymentParameters::for_keysend(payee_pubkey.into_rust(), final_cltv_expiry_delta);
1223 crate::lightning::routing::router::PaymentParameters { inner: ObjOps::heap_alloc(ret), is_owned: true }
1227 use lightning::routing::router::RouteHint as nativeRouteHintImport;
1228 pub(crate) type nativeRouteHint = nativeRouteHintImport;
1230 /// A list of hops along a payment path terminating with a channel to the recipient.
1233 pub struct RouteHint {
1234 /// A pointer to the opaque Rust object.
1236 /// Nearly everywhere, inner must be non-null, however in places where
1237 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1238 pub inner: *mut nativeRouteHint,
1239 /// Indicates that this is the only struct which contains the same pointer.
1241 /// Rust functions which take ownership of an object provided via an argument require
1242 /// this to be true and invalidate the object pointed to by inner.
1246 impl Drop for RouteHint {
1247 fn drop(&mut self) {
1248 if self.is_owned && !<*mut nativeRouteHint>::is_null(self.inner) {
1249 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
1253 /// Frees any resources used by the RouteHint, if is_owned is set and inner is non-NULL.
1255 pub extern "C" fn RouteHint_free(this_obj: RouteHint) { }
1257 /// Used only if an object of this type is returned as a trait impl by a method
1258 pub(crate) extern "C" fn RouteHint_free_void(this_ptr: *mut c_void) {
1259 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRouteHint) };
1263 pub(crate) fn get_native_ref(&self) -> &'static nativeRouteHint {
1264 unsafe { &*ObjOps::untweak_ptr(self.inner) }
1266 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRouteHint {
1267 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
1269 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1270 pub(crate) fn take_inner(mut self) -> *mut nativeRouteHint {
1271 assert!(self.is_owned);
1272 let ret = ObjOps::untweak_ptr(self.inner);
1273 self.inner = core::ptr::null_mut();
1278 pub extern "C" fn RouteHint_get_a(this_ptr: &RouteHint) -> crate::c_types::derived::CVec_RouteHintHopZ {
1279 let mut inner_val = &mut this_ptr.get_native_mut_ref().0;
1280 let mut local_inner_val = Vec::new(); for item in inner_val.iter() { local_inner_val.push( { crate::lightning::routing::router::RouteHintHop { inner: unsafe { ObjOps::nonnull_ptr_to_inner((item as *const lightning::routing::router::RouteHintHop<>) as *mut _) }, is_owned: false } }); };
1281 local_inner_val.into()
1284 pub extern "C" fn RouteHint_set_a(this_ptr: &mut RouteHint, mut val: crate::c_types::derived::CVec_RouteHintHopZ) {
1285 let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
1286 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.0 = local_val;
1288 /// Constructs a new RouteHint given each field
1291 pub extern "C" fn RouteHint_new(mut a_arg: crate::c_types::derived::CVec_RouteHintHopZ) -> RouteHint {
1292 let mut local_a_arg = Vec::new(); for mut item in a_arg.into_rust().drain(..) { local_a_arg.push( { *unsafe { Box::from_raw(item.take_inner()) } }); };
1293 RouteHint { inner: ObjOps::heap_alloc(lightning::routing::router::RouteHint (
1295 )), is_owned: true }
1297 impl Clone for RouteHint {
1298 fn clone(&self) -> Self {
1300 inner: if <*mut nativeRouteHint>::is_null(self.inner) { core::ptr::null_mut() } else {
1301 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
1307 /// Used only if an object of this type is returned as a trait impl by a method
1308 pub(crate) extern "C" fn RouteHint_clone_void(this_ptr: *const c_void) -> *mut c_void {
1309 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHint)).clone() })) as *mut c_void
1312 /// Creates a copy of the RouteHint
1313 pub extern "C" fn RouteHint_clone(orig: &RouteHint) -> RouteHint {
1316 /// Checks if two RouteHints contain equal inner contents.
1318 pub extern "C" fn RouteHint_hash(o: &RouteHint) -> u64 {
1319 if o.inner.is_null() { return 0; }
1320 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
1321 #[allow(deprecated)]
1322 let mut hasher = core::hash::SipHasher::new();
1323 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
1324 core::hash::Hasher::finish(&hasher)
1326 /// Checks if two RouteHints contain equal inner contents.
1327 /// This ignores pointers and is_owned flags and looks at the values in fields.
1328 /// Two objects with NULL inner values will be considered "equal" here.
1330 pub extern "C" fn RouteHint_eq(a: &RouteHint, b: &RouteHint) -> bool {
1331 if a.inner == b.inner { return true; }
1332 if a.inner.is_null() || b.inner.is_null() { return false; }
1333 if a.get_native_ref() == b.get_native_ref() { true } else { false }
1336 /// Serialize the RouteHint object into a byte array which can be read by RouteHint_read
1337 pub extern "C" fn RouteHint_write(obj: &crate::lightning::routing::router::RouteHint) -> crate::c_types::derived::CVec_u8Z {
1338 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
1341 pub(crate) extern "C" fn RouteHint_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1342 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRouteHint) })
1345 /// Read a RouteHint from a byte array, created by RouteHint_write
1346 pub extern "C" fn RouteHint_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RouteHintDecodeErrorZ {
1347 let res: Result<lightning::routing::router::RouteHint, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
1348 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::RouteHint { 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() };
1352 use lightning::routing::router::RouteHintHop as nativeRouteHintHopImport;
1353 pub(crate) type nativeRouteHintHop = nativeRouteHintHopImport;
1355 /// A channel descriptor for a hop along a payment path.
1358 pub struct RouteHintHop {
1359 /// A pointer to the opaque Rust object.
1361 /// Nearly everywhere, inner must be non-null, however in places where
1362 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
1363 pub inner: *mut nativeRouteHintHop,
1364 /// Indicates that this is the only struct which contains the same pointer.
1366 /// Rust functions which take ownership of an object provided via an argument require
1367 /// this to be true and invalidate the object pointed to by inner.
1371 impl Drop for RouteHintHop {
1372 fn drop(&mut self) {
1373 if self.is_owned && !<*mut nativeRouteHintHop>::is_null(self.inner) {
1374 let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
1378 /// Frees any resources used by the RouteHintHop, if is_owned is set and inner is non-NULL.
1380 pub extern "C" fn RouteHintHop_free(this_obj: RouteHintHop) { }
1382 /// Used only if an object of this type is returned as a trait impl by a method
1383 pub(crate) extern "C" fn RouteHintHop_free_void(this_ptr: *mut c_void) {
1384 let _ = unsafe { Box::from_raw(this_ptr as *mut nativeRouteHintHop) };
1388 pub(crate) fn get_native_ref(&self) -> &'static nativeRouteHintHop {
1389 unsafe { &*ObjOps::untweak_ptr(self.inner) }
1391 pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRouteHintHop {
1392 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
1394 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
1395 pub(crate) fn take_inner(mut self) -> *mut nativeRouteHintHop {
1396 assert!(self.is_owned);
1397 let ret = ObjOps::untweak_ptr(self.inner);
1398 self.inner = core::ptr::null_mut();
1402 /// The node_id of the non-target end of the route
1404 pub extern "C" fn RouteHintHop_get_src_node_id(this_ptr: &RouteHintHop) -> crate::c_types::PublicKey {
1405 let mut inner_val = &mut this_ptr.get_native_mut_ref().src_node_id;
1406 crate::c_types::PublicKey::from_rust(&inner_val)
1408 /// The node_id of the non-target end of the route
1410 pub extern "C" fn RouteHintHop_set_src_node_id(this_ptr: &mut RouteHintHop, mut val: crate::c_types::PublicKey) {
1411 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.src_node_id = val.into_rust();
1413 /// The short_channel_id of this channel
1415 pub extern "C" fn RouteHintHop_get_short_channel_id(this_ptr: &RouteHintHop) -> u64 {
1416 let mut inner_val = &mut this_ptr.get_native_mut_ref().short_channel_id;
1419 /// The short_channel_id of this channel
1421 pub extern "C" fn RouteHintHop_set_short_channel_id(this_ptr: &mut RouteHintHop, mut val: u64) {
1422 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.short_channel_id = val;
1424 /// The fees which must be paid to use this channel
1426 pub extern "C" fn RouteHintHop_get_fees(this_ptr: &RouteHintHop) -> crate::lightning::routing::gossip::RoutingFees {
1427 let mut inner_val = &mut this_ptr.get_native_mut_ref().fees;
1428 crate::lightning::routing::gossip::RoutingFees { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const lightning::routing::gossip::RoutingFees<>) as *mut _) }, is_owned: false }
1430 /// The fees which must be paid to use this channel
1432 pub extern "C" fn RouteHintHop_set_fees(this_ptr: &mut RouteHintHop, mut val: crate::lightning::routing::gossip::RoutingFees) {
1433 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.fees = *unsafe { Box::from_raw(val.take_inner()) };
1435 /// The difference in CLTV values between this node and the next node.
1437 pub extern "C" fn RouteHintHop_get_cltv_expiry_delta(this_ptr: &RouteHintHop) -> u16 {
1438 let mut inner_val = &mut this_ptr.get_native_mut_ref().cltv_expiry_delta;
1441 /// The difference in CLTV values between this node and the next node.
1443 pub extern "C" fn RouteHintHop_set_cltv_expiry_delta(this_ptr: &mut RouteHintHop, mut val: u16) {
1444 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.cltv_expiry_delta = val;
1446 /// The minimum value, in msat, which must be relayed to the next hop.
1448 pub extern "C" fn RouteHintHop_get_htlc_minimum_msat(this_ptr: &RouteHintHop) -> crate::c_types::derived::COption_u64Z {
1449 let mut inner_val = &mut this_ptr.get_native_mut_ref().htlc_minimum_msat;
1450 let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u64Z::None } else { crate::c_types::derived::COption_u64Z::Some( { inner_val.unwrap() }) };
1453 /// The minimum value, in msat, which must be relayed to the next hop.
1455 pub extern "C" fn RouteHintHop_set_htlc_minimum_msat(this_ptr: &mut RouteHintHop, mut val: crate::c_types::derived::COption_u64Z) {
1456 let mut local_val = if val.is_some() { Some( { val.take() }) } else { None };
1457 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.htlc_minimum_msat = local_val;
1459 /// The maximum value in msat available for routing with a single HTLC.
1461 pub extern "C" fn RouteHintHop_get_htlc_maximum_msat(this_ptr: &RouteHintHop) -> crate::c_types::derived::COption_u64Z {
1462 let mut inner_val = &mut this_ptr.get_native_mut_ref().htlc_maximum_msat;
1463 let mut local_inner_val = if inner_val.is_none() { crate::c_types::derived::COption_u64Z::None } else { crate::c_types::derived::COption_u64Z::Some( { inner_val.unwrap() }) };
1466 /// The maximum value in msat available for routing with a single HTLC.
1468 pub extern "C" fn RouteHintHop_set_htlc_maximum_msat(this_ptr: &mut RouteHintHop, mut val: crate::c_types::derived::COption_u64Z) {
1469 let mut local_val = if val.is_some() { Some( { val.take() }) } else { None };
1470 unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.htlc_maximum_msat = local_val;
1472 /// Constructs a new RouteHintHop given each field
1475 pub extern "C" fn RouteHintHop_new(mut src_node_id_arg: crate::c_types::PublicKey, mut short_channel_id_arg: u64, mut fees_arg: crate::lightning::routing::gossip::RoutingFees, mut cltv_expiry_delta_arg: u16, mut htlc_minimum_msat_arg: crate::c_types::derived::COption_u64Z, mut htlc_maximum_msat_arg: crate::c_types::derived::COption_u64Z) -> RouteHintHop {
1476 let mut local_htlc_minimum_msat_arg = if htlc_minimum_msat_arg.is_some() { Some( { htlc_minimum_msat_arg.take() }) } else { None };
1477 let mut local_htlc_maximum_msat_arg = if htlc_maximum_msat_arg.is_some() { Some( { htlc_maximum_msat_arg.take() }) } else { None };
1478 RouteHintHop { inner: ObjOps::heap_alloc(nativeRouteHintHop {
1479 src_node_id: src_node_id_arg.into_rust(),
1480 short_channel_id: short_channel_id_arg,
1481 fees: *unsafe { Box::from_raw(fees_arg.take_inner()) },
1482 cltv_expiry_delta: cltv_expiry_delta_arg,
1483 htlc_minimum_msat: local_htlc_minimum_msat_arg,
1484 htlc_maximum_msat: local_htlc_maximum_msat_arg,
1485 }), is_owned: true }
1487 impl Clone for RouteHintHop {
1488 fn clone(&self) -> Self {
1490 inner: if <*mut nativeRouteHintHop>::is_null(self.inner) { core::ptr::null_mut() } else {
1491 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
1497 /// Used only if an object of this type is returned as a trait impl by a method
1498 pub(crate) extern "C" fn RouteHintHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
1499 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHintHop)).clone() })) as *mut c_void
1502 /// Creates a copy of the RouteHintHop
1503 pub extern "C" fn RouteHintHop_clone(orig: &RouteHintHop) -> RouteHintHop {
1506 /// Checks if two RouteHintHops contain equal inner contents.
1508 pub extern "C" fn RouteHintHop_hash(o: &RouteHintHop) -> u64 {
1509 if o.inner.is_null() { return 0; }
1510 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
1511 #[allow(deprecated)]
1512 let mut hasher = core::hash::SipHasher::new();
1513 core::hash::Hash::hash(o.get_native_ref(), &mut hasher);
1514 core::hash::Hasher::finish(&hasher)
1516 /// Checks if two RouteHintHops contain equal inner contents.
1517 /// This ignores pointers and is_owned flags and looks at the values in fields.
1518 /// Two objects with NULL inner values will be considered "equal" here.
1520 pub extern "C" fn RouteHintHop_eq(a: &RouteHintHop, b: &RouteHintHop) -> bool {
1521 if a.inner == b.inner { return true; }
1522 if a.inner.is_null() || b.inner.is_null() { return false; }
1523 if a.get_native_ref() == b.get_native_ref() { true } else { false }
1526 /// Serialize the RouteHintHop object into a byte array which can be read by RouteHintHop_read
1527 pub extern "C" fn RouteHintHop_write(obj: &crate::lightning::routing::router::RouteHintHop) -> crate::c_types::derived::CVec_u8Z {
1528 crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
1531 pub(crate) extern "C" fn RouteHintHop_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
1532 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRouteHintHop) })
1535 /// Read a RouteHintHop from a byte array, created by RouteHintHop_write
1536 pub extern "C" fn RouteHintHop_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RouteHintHopDecodeErrorZ {
1537 let res: Result<lightning::routing::router::RouteHintHop, lightning::ln::msgs::DecodeError> = crate::c_types::deserialize_obj(ser);
1538 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::RouteHintHop { 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() };
1541 /// Finds a route from us (payer) to the given target node (payee).
1543 /// If the payee provided features in their invoice, they should be provided via `params.payee`.
1544 /// Without this, MPP will only be used if the payee's features are available in the network graph.
1546 /// Private routing paths between a public node and the target may be included in `params.payee`.
1548 /// If some channels aren't announced, it may be useful to fill in `first_hops` with the results
1549 /// from [`ChannelManager::list_usable_channels`]. If it is filled in, the view of these channels
1550 /// from `network_graph` will be ignored, and only those in `first_hops` will be used.
1552 /// The fees on channels from us to the next hop are ignored as they are assumed to all be equal.
1553 /// However, the enabled/disabled bit on such channels as well as the `htlc_minimum_msat` /
1554 /// `htlc_maximum_msat` *are* checked as they may change based on the receiving node.
1558 /// May be used to re-compute a [`Route`] when handling a [`Event::PaymentPathFailed`]. Any
1559 /// adjustments to the [`NetworkGraph`] and channel scores should be made prior to calling this
1564 /// Panics if first_hops contains channels without short_channel_ids;
1565 /// [`ChannelManager::list_usable_channels`] will never include such channels.
1567 /// [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
1568 /// [`Event::PaymentPathFailed`]: crate::util::events::Event::PaymentPathFailed
1569 /// [`NetworkGraph`]: crate::routing::gossip::NetworkGraph
1571 /// Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
1573 pub extern "C" fn find_route(mut our_node_pubkey: crate::c_types::PublicKey, route_params: &crate::lightning::routing::router::RouteParameters, network_graph: &crate::lightning::routing::gossip::NetworkGraph, first_hops: *mut crate::c_types::derived::CVec_ChannelDetailsZ, mut logger: crate::lightning::util::logger::Logger, scorer: &crate::lightning::routing::scoring::Score, random_seed_bytes: *const [u8; 32]) -> crate::c_types::derived::CResult_RouteLightningErrorZ {
1574 let mut local_first_hops_base = if first_hops == core::ptr::null_mut() { None } else { Some( { let mut local_first_hops_0 = Vec::new(); for mut item in unsafe { &mut *first_hops }.as_slice().iter() { local_first_hops_0.push( { item.get_native_ref() }); }; local_first_hops_0 }) }; let mut local_first_hops = local_first_hops_base.as_ref().map(|a| &a[..]);
1575 let mut ret = lightning::routing::router::find_route::<crate::lightning::util::logger::Logger, crate::lightning::util::logger::Logger, crate::lightning::routing::scoring::Score>(&our_node_pubkey.into_rust(), route_params.get_native_ref(), network_graph.get_native_ref(), local_first_hops, logger, scorer, unsafe { &*random_seed_bytes});
1576 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::Route { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
1580 /// Construct a route from us (payer) to the target node (payee) via the given hops (which should
1581 /// exclude the payer, but include the payee). This may be useful, e.g., for probing the chosen path.
1583 /// Re-uses logic from `find_route`, so the restrictions described there also apply here.
1585 pub extern "C" fn build_route_from_hops(mut our_node_pubkey: crate::c_types::PublicKey, mut hops: crate::c_types::derived::CVec_PublicKeyZ, route_params: &crate::lightning::routing::router::RouteParameters, network_graph: &crate::lightning::routing::gossip::NetworkGraph, mut logger: crate::lightning::util::logger::Logger, random_seed_bytes: *const [u8; 32]) -> crate::c_types::derived::CResult_RouteLightningErrorZ {
1586 let mut local_hops = Vec::new(); for mut item in hops.into_rust().drain(..) { local_hops.push( { item.into_rust() }); };
1587 let mut ret = lightning::routing::router::build_route_from_hops::<crate::lightning::util::logger::Logger, crate::lightning::util::logger::Logger>(&our_node_pubkey.into_rust(), &local_hops[..], route_params.get_native_ref(), network_graph.get_native_ref(), logger, unsafe { &*random_seed_bytes});
1588 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::Route { inner: ObjOps::heap_alloc(o), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::LightningError { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };