Merge pull request #45 from TheBlueMatt/main
[ldk-c-bindings] / lightning-c-bindings / src / lightning / routing / router.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
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.
8
9 //! The top-level routing/network map tracking logic lives here.
10 //!
11 //! You probably want to create a NetGraphMsgHandler and use that as your RoutingMessageHandler and then
12 //! interrogate it to get routes for your own payments.
13
14 use std::str::FromStr;
15 use std::ffi::c_void;
16 use core::convert::Infallible;
17 use bitcoin::hashes::Hash;
18 use crate::c_types::*;
19
20
21 use lightning::routing::router::RouteHop as nativeRouteHopImport;
22 type nativeRouteHop = nativeRouteHopImport;
23
24 /// A hop in a route
25 #[must_use]
26 #[repr(C)]
27 pub struct RouteHop {
28         /// A pointer to the opaque Rust object.
29
30         /// Nearly everywhere, inner must be non-null, however in places where
31         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
32         pub inner: *mut nativeRouteHop,
33         /// Indicates that this is the only struct which contains the same pointer.
34
35         /// Rust functions which take ownership of an object provided via an argument require
36         /// this to be true and invalidate the object pointed to by inner.
37         pub is_owned: bool,
38 }
39
40 impl Drop for RouteHop {
41         fn drop(&mut self) {
42                 if self.is_owned && !<*mut nativeRouteHop>::is_null(self.inner) {
43                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
44                 }
45         }
46 }
47 /// Frees any resources used by the RouteHop, if is_owned is set and inner is non-NULL.
48 #[no_mangle]
49 pub extern "C" fn RouteHop_free(this_obj: RouteHop) { }
50 #[allow(unused)]
51 /// Used only if an object of this type is returned as a trait impl by a method
52 extern "C" fn RouteHop_free_void(this_ptr: *mut c_void) {
53         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRouteHop); }
54 }
55 #[allow(unused)]
56 impl RouteHop {
57         pub(crate) fn get_native_ref(&self) -> &'static nativeRouteHop {
58                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
59         }
60         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRouteHop {
61                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
62         }
63         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
64         pub(crate) fn take_inner(mut self) -> *mut nativeRouteHop {
65                 assert!(self.is_owned);
66                 let ret = ObjOps::untweak_ptr(self.inner);
67                 self.inner = std::ptr::null_mut();
68                 ret
69         }
70 }
71 /// The node_id of the node at this hop.
72 #[no_mangle]
73 pub extern "C" fn RouteHop_get_pubkey(this_ptr: &RouteHop) -> crate::c_types::PublicKey {
74         let mut inner_val = &mut this_ptr.get_native_mut_ref().pubkey;
75         crate::c_types::PublicKey::from_rust(&inner_val)
76 }
77 /// The node_id of the node at this hop.
78 #[no_mangle]
79 pub extern "C" fn RouteHop_set_pubkey(this_ptr: &mut RouteHop, mut val: crate::c_types::PublicKey) {
80         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.pubkey = val.into_rust();
81 }
82 /// The node_announcement features of the node at this hop. For the last hop, these may be
83 /// amended to match the features present in the invoice this node generated.
84 #[no_mangle]
85 pub extern "C" fn RouteHop_get_node_features(this_ptr: &RouteHop) -> crate::lightning::ln::features::NodeFeatures {
86         let mut inner_val = &mut this_ptr.get_native_mut_ref().node_features;
87         crate::lightning::ln::features::NodeFeatures { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const _) as *mut _) }, is_owned: false }
88 }
89 /// The node_announcement features of the node at this hop. For the last hop, these may be
90 /// amended to match the features present in the invoice this node generated.
91 #[no_mangle]
92 pub extern "C" fn RouteHop_set_node_features(this_ptr: &mut RouteHop, mut val: crate::lightning::ln::features::NodeFeatures) {
93         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.node_features = *unsafe { Box::from_raw(val.take_inner()) };
94 }
95 /// The channel that should be used from the previous hop to reach this node.
96 #[no_mangle]
97 pub extern "C" fn RouteHop_get_short_channel_id(this_ptr: &RouteHop) -> u64 {
98         let mut inner_val = &mut this_ptr.get_native_mut_ref().short_channel_id;
99         *inner_val
100 }
101 /// The channel that should be used from the previous hop to reach this node.
102 #[no_mangle]
103 pub extern "C" fn RouteHop_set_short_channel_id(this_ptr: &mut RouteHop, mut val: u64) {
104         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.short_channel_id = val;
105 }
106 /// The channel_announcement features of the channel that should be used from the previous hop
107 /// to reach this node.
108 #[no_mangle]
109 pub extern "C" fn RouteHop_get_channel_features(this_ptr: &RouteHop) -> crate::lightning::ln::features::ChannelFeatures {
110         let mut inner_val = &mut this_ptr.get_native_mut_ref().channel_features;
111         crate::lightning::ln::features::ChannelFeatures { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const _) as *mut _) }, is_owned: false }
112 }
113 /// The channel_announcement features of the channel that should be used from the previous hop
114 /// to reach this node.
115 #[no_mangle]
116 pub extern "C" fn RouteHop_set_channel_features(this_ptr: &mut RouteHop, mut val: crate::lightning::ln::features::ChannelFeatures) {
117         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.channel_features = *unsafe { Box::from_raw(val.take_inner()) };
118 }
119 /// The fee taken on this hop (for paying for the use of the *next* channel in the path).
120 /// For the last hop, this should be the full value of the payment (might be more than
121 /// requested if we had to match htlc_minimum_msat).
122 #[no_mangle]
123 pub extern "C" fn RouteHop_get_fee_msat(this_ptr: &RouteHop) -> u64 {
124         let mut inner_val = &mut this_ptr.get_native_mut_ref().fee_msat;
125         *inner_val
126 }
127 /// The fee taken on this hop (for paying for the use of the *next* channel in the path).
128 /// For the last hop, this should be the full value of the payment (might be more than
129 /// requested if we had to match htlc_minimum_msat).
130 #[no_mangle]
131 pub extern "C" fn RouteHop_set_fee_msat(this_ptr: &mut RouteHop, mut val: u64) {
132         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.fee_msat = val;
133 }
134 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
135 /// expected at the destination, in excess of the current block height.
136 #[no_mangle]
137 pub extern "C" fn RouteHop_get_cltv_expiry_delta(this_ptr: &RouteHop) -> u32 {
138         let mut inner_val = &mut this_ptr.get_native_mut_ref().cltv_expiry_delta;
139         *inner_val
140 }
141 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
142 /// expected at the destination, in excess of the current block height.
143 #[no_mangle]
144 pub extern "C" fn RouteHop_set_cltv_expiry_delta(this_ptr: &mut RouteHop, mut val: u32) {
145         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.cltv_expiry_delta = val;
146 }
147 /// Constructs a new RouteHop given each field
148 #[must_use]
149 #[no_mangle]
150 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 {
151         RouteHop { inner: ObjOps::heap_alloc(nativeRouteHop {
152                 pubkey: pubkey_arg.into_rust(),
153                 node_features: *unsafe { Box::from_raw(node_features_arg.take_inner()) },
154                 short_channel_id: short_channel_id_arg,
155                 channel_features: *unsafe { Box::from_raw(channel_features_arg.take_inner()) },
156                 fee_msat: fee_msat_arg,
157                 cltv_expiry_delta: cltv_expiry_delta_arg,
158         }), is_owned: true }
159 }
160 impl Clone for RouteHop {
161         fn clone(&self) -> Self {
162                 Self {
163                         inner: if <*mut nativeRouteHop>::is_null(self.inner) { std::ptr::null_mut() } else {
164                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
165                         is_owned: true,
166                 }
167         }
168 }
169 #[allow(unused)]
170 /// Used only if an object of this type is returned as a trait impl by a method
171 pub(crate) extern "C" fn RouteHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
172         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHop)).clone() })) as *mut c_void
173 }
174 #[no_mangle]
175 /// Creates a copy of the RouteHop
176 pub extern "C" fn RouteHop_clone(orig: &RouteHop) -> RouteHop {
177         orig.clone()
178 }
179 /// Checks if two RouteHops contain equal inner contents.
180 #[no_mangle]
181 pub extern "C" fn RouteHop_hash(o: &RouteHop) -> u64 {
182         if o.inner.is_null() { return 0; }
183         // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core
184         #[allow(deprecated)]
185         let mut hasher = core::hash::SipHasher::new();
186         std::hash::Hash::hash(o.get_native_ref(), &mut hasher);
187         std::hash::Hasher::finish(&hasher)
188 }
189 /// Checks if two RouteHops contain equal inner contents.
190 /// This ignores pointers and is_owned flags and looks at the values in fields.
191 /// Two objects with NULL inner values will be considered "equal" here.
192 #[no_mangle]
193 pub extern "C" fn RouteHop_eq(a: &RouteHop, b: &RouteHop) -> bool {
194         if a.inner == b.inner { return true; }
195         if a.inner.is_null() || b.inner.is_null() { return false; }
196         if a.get_native_ref() == b.get_native_ref() { true } else { false }
197 }
198 #[no_mangle]
199 /// Serialize the RouteHop object into a byte array which can be read by RouteHop_read
200 pub extern "C" fn RouteHop_write(obj: &RouteHop) -> crate::c_types::derived::CVec_u8Z {
201         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
202 }
203 #[no_mangle]
204 pub(crate) extern "C" fn RouteHop_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
205         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRouteHop) })
206 }
207 #[no_mangle]
208 /// Read a RouteHop from a byte array, created by RouteHop_write
209 pub extern "C" fn RouteHop_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RouteHopDecodeErrorZ {
210         let res = crate::c_types::deserialize_obj(ser);
211         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 { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
212         local_res
213 }
214
215 use lightning::routing::router::Route as nativeRouteImport;
216 type nativeRoute = nativeRouteImport;
217
218 /// A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
219 /// it can take multiple paths. Each path is composed of one or more hops through the network.
220 #[must_use]
221 #[repr(C)]
222 pub struct Route {
223         /// A pointer to the opaque Rust object.
224
225         /// Nearly everywhere, inner must be non-null, however in places where
226         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
227         pub inner: *mut nativeRoute,
228         /// Indicates that this is the only struct which contains the same pointer.
229
230         /// Rust functions which take ownership of an object provided via an argument require
231         /// this to be true and invalidate the object pointed to by inner.
232         pub is_owned: bool,
233 }
234
235 impl Drop for Route {
236         fn drop(&mut self) {
237                 if self.is_owned && !<*mut nativeRoute>::is_null(self.inner) {
238                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
239                 }
240         }
241 }
242 /// Frees any resources used by the Route, if is_owned is set and inner is non-NULL.
243 #[no_mangle]
244 pub extern "C" fn Route_free(this_obj: Route) { }
245 #[allow(unused)]
246 /// Used only if an object of this type is returned as a trait impl by a method
247 extern "C" fn Route_free_void(this_ptr: *mut c_void) {
248         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRoute); }
249 }
250 #[allow(unused)]
251 impl Route {
252         pub(crate) fn get_native_ref(&self) -> &'static nativeRoute {
253                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
254         }
255         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRoute {
256                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
257         }
258         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
259         pub(crate) fn take_inner(mut self) -> *mut nativeRoute {
260                 assert!(self.is_owned);
261                 let ret = ObjOps::untweak_ptr(self.inner);
262                 self.inner = std::ptr::null_mut();
263                 ret
264         }
265 }
266 /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
267 /// last RouteHop in each path must be the same.
268 /// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
269 /// destination. Thus, this must always be at least length one. While the maximum length of any
270 /// given path is variable, keeping the length of any path to less than 20 should currently
271 /// ensure it is viable.
272 #[no_mangle]
273 pub extern "C" fn Route_get_paths(this_ptr: &Route) -> crate::c_types::derived::CVec_CVec_RouteHopZZ {
274         let mut inner_val = &mut this_ptr.get_native_mut_ref().paths;
275         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 _) as *mut _) }, is_owned: false } }); }; local_inner_val_0.into() }); };
276         local_inner_val.into()
277 }
278 /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
279 /// last RouteHop in each path must be the same.
280 /// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
281 /// destination. Thus, this must always be at least length one. While the maximum length of any
282 /// given path is variable, keeping the length of any path to less than 20 should currently
283 /// ensure it is viable.
284 #[no_mangle]
285 pub extern "C" fn Route_set_paths(this_ptr: &mut Route, mut val: crate::c_types::derived::CVec_CVec_RouteHopZZ) {
286         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 }); };
287         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.paths = local_val;
288 }
289 /// Constructs a new Route given each field
290 #[must_use]
291 #[no_mangle]
292 pub extern "C" fn Route_new(mut paths_arg: crate::c_types::derived::CVec_CVec_RouteHopZZ) -> Route {
293         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 }); };
294         Route { inner: ObjOps::heap_alloc(nativeRoute {
295                 paths: local_paths_arg,
296         }), is_owned: true }
297 }
298 impl Clone for Route {
299         fn clone(&self) -> Self {
300                 Self {
301                         inner: if <*mut nativeRoute>::is_null(self.inner) { std::ptr::null_mut() } else {
302                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
303                         is_owned: true,
304                 }
305         }
306 }
307 #[allow(unused)]
308 /// Used only if an object of this type is returned as a trait impl by a method
309 pub(crate) extern "C" fn Route_clone_void(this_ptr: *const c_void) -> *mut c_void {
310         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRoute)).clone() })) as *mut c_void
311 }
312 #[no_mangle]
313 /// Creates a copy of the Route
314 pub extern "C" fn Route_clone(orig: &Route) -> Route {
315         orig.clone()
316 }
317 /// Checks if two Routes contain equal inner contents.
318 #[no_mangle]
319 pub extern "C" fn Route_hash(o: &Route) -> u64 {
320         if o.inner.is_null() { return 0; }
321         // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core
322         #[allow(deprecated)]
323         let mut hasher = core::hash::SipHasher::new();
324         std::hash::Hash::hash(o.get_native_ref(), &mut hasher);
325         std::hash::Hasher::finish(&hasher)
326 }
327 /// Checks if two Routes contain equal inner contents.
328 /// This ignores pointers and is_owned flags and looks at the values in fields.
329 /// Two objects with NULL inner values will be considered "equal" here.
330 #[no_mangle]
331 pub extern "C" fn Route_eq(a: &Route, b: &Route) -> bool {
332         if a.inner == b.inner { return true; }
333         if a.inner.is_null() || b.inner.is_null() { return false; }
334         if a.get_native_ref() == b.get_native_ref() { true } else { false }
335 }
336 /// Returns the total amount of fees paid on this [`Route`].
337 ///
338 /// This doesn't include any extra payment made to the recipient, which can happen in excess of
339 /// the amount passed to [`get_route`]'s `final_value_msat`.
340 #[must_use]
341 #[no_mangle]
342 pub extern "C" fn Route_get_total_fees(this_arg: &Route) -> u64 {
343         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_total_fees();
344         ret
345 }
346
347 /// Returns the total amount paid on this [`Route`], excluding the fees.
348 #[must_use]
349 #[no_mangle]
350 pub extern "C" fn Route_get_total_amount(this_arg: &Route) -> u64 {
351         let mut ret = unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.get_total_amount();
352         ret
353 }
354
355 #[no_mangle]
356 /// Serialize the Route object into a byte array which can be read by Route_read
357 pub extern "C" fn Route_write(obj: &Route) -> crate::c_types::derived::CVec_u8Z {
358         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
359 }
360 #[no_mangle]
361 pub(crate) extern "C" fn Route_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
362         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRoute) })
363 }
364 #[no_mangle]
365 /// Read a Route from a byte array, created by Route_write
366 pub extern "C" fn Route_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RouteDecodeErrorZ {
367         let res = crate::c_types::deserialize_obj(ser);
368         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 { inner: ObjOps::heap_alloc(e), is_owned: true } }).into() };
369         local_res
370 }
371
372 use lightning::routing::router::RouteHint as nativeRouteHintImport;
373 type nativeRouteHint = nativeRouteHintImport;
374
375 /// A list of hops along a payment path terminating with a channel to the recipient.
376 #[must_use]
377 #[repr(C)]
378 pub struct RouteHint {
379         /// A pointer to the opaque Rust object.
380
381         /// Nearly everywhere, inner must be non-null, however in places where
382         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
383         pub inner: *mut nativeRouteHint,
384         /// Indicates that this is the only struct which contains the same pointer.
385
386         /// Rust functions which take ownership of an object provided via an argument require
387         /// this to be true and invalidate the object pointed to by inner.
388         pub is_owned: bool,
389 }
390
391 impl Drop for RouteHint {
392         fn drop(&mut self) {
393                 if self.is_owned && !<*mut nativeRouteHint>::is_null(self.inner) {
394                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
395                 }
396         }
397 }
398 /// Frees any resources used by the RouteHint, if is_owned is set and inner is non-NULL.
399 #[no_mangle]
400 pub extern "C" fn RouteHint_free(this_obj: RouteHint) { }
401 #[allow(unused)]
402 /// Used only if an object of this type is returned as a trait impl by a method
403 extern "C" fn RouteHint_free_void(this_ptr: *mut c_void) {
404         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRouteHint); }
405 }
406 #[allow(unused)]
407 impl RouteHint {
408         pub(crate) fn get_native_ref(&self) -> &'static nativeRouteHint {
409                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
410         }
411         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRouteHint {
412                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
413         }
414         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
415         pub(crate) fn take_inner(mut self) -> *mut nativeRouteHint {
416                 assert!(self.is_owned);
417                 let ret = ObjOps::untweak_ptr(self.inner);
418                 self.inner = std::ptr::null_mut();
419                 ret
420         }
421 }
422 impl Clone for RouteHint {
423         fn clone(&self) -> Self {
424                 Self {
425                         inner: if <*mut nativeRouteHint>::is_null(self.inner) { std::ptr::null_mut() } else {
426                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
427                         is_owned: true,
428                 }
429         }
430 }
431 #[allow(unused)]
432 /// Used only if an object of this type is returned as a trait impl by a method
433 pub(crate) extern "C" fn RouteHint_clone_void(this_ptr: *const c_void) -> *mut c_void {
434         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHint)).clone() })) as *mut c_void
435 }
436 #[no_mangle]
437 /// Creates a copy of the RouteHint
438 pub extern "C" fn RouteHint_clone(orig: &RouteHint) -> RouteHint {
439         orig.clone()
440 }
441 /// Checks if two RouteHints contain equal inner contents.
442 #[no_mangle]
443 pub extern "C" fn RouteHint_hash(o: &RouteHint) -> u64 {
444         if o.inner.is_null() { return 0; }
445         // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core
446         #[allow(deprecated)]
447         let mut hasher = core::hash::SipHasher::new();
448         std::hash::Hash::hash(o.get_native_ref(), &mut hasher);
449         std::hash::Hasher::finish(&hasher)
450 }
451 /// Checks if two RouteHints contain equal inner contents.
452 /// This ignores pointers and is_owned flags and looks at the values in fields.
453 /// Two objects with NULL inner values will be considered "equal" here.
454 #[no_mangle]
455 pub extern "C" fn RouteHint_eq(a: &RouteHint, b: &RouteHint) -> bool {
456         if a.inner == b.inner { return true; }
457         if a.inner.is_null() || b.inner.is_null() { return false; }
458         if a.get_native_ref() == b.get_native_ref() { true } else { false }
459 }
460
461 use lightning::routing::router::RouteHintHop as nativeRouteHintHopImport;
462 type nativeRouteHintHop = nativeRouteHintHopImport;
463
464 /// A channel descriptor for a hop along a payment path.
465 #[must_use]
466 #[repr(C)]
467 pub struct RouteHintHop {
468         /// A pointer to the opaque Rust object.
469
470         /// Nearly everywhere, inner must be non-null, however in places where
471         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
472         pub inner: *mut nativeRouteHintHop,
473         /// Indicates that this is the only struct which contains the same pointer.
474
475         /// Rust functions which take ownership of an object provided via an argument require
476         /// this to be true and invalidate the object pointed to by inner.
477         pub is_owned: bool,
478 }
479
480 impl Drop for RouteHintHop {
481         fn drop(&mut self) {
482                 if self.is_owned && !<*mut nativeRouteHintHop>::is_null(self.inner) {
483                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
484                 }
485         }
486 }
487 /// Frees any resources used by the RouteHintHop, if is_owned is set and inner is non-NULL.
488 #[no_mangle]
489 pub extern "C" fn RouteHintHop_free(this_obj: RouteHintHop) { }
490 #[allow(unused)]
491 /// Used only if an object of this type is returned as a trait impl by a method
492 extern "C" fn RouteHintHop_free_void(this_ptr: *mut c_void) {
493         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRouteHintHop); }
494 }
495 #[allow(unused)]
496 impl RouteHintHop {
497         pub(crate) fn get_native_ref(&self) -> &'static nativeRouteHintHop {
498                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
499         }
500         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeRouteHintHop {
501                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
502         }
503         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
504         pub(crate) fn take_inner(mut self) -> *mut nativeRouteHintHop {
505                 assert!(self.is_owned);
506                 let ret = ObjOps::untweak_ptr(self.inner);
507                 self.inner = std::ptr::null_mut();
508                 ret
509         }
510 }
511 /// The node_id of the non-target end of the route
512 #[no_mangle]
513 pub extern "C" fn RouteHintHop_get_src_node_id(this_ptr: &RouteHintHop) -> crate::c_types::PublicKey {
514         let mut inner_val = &mut this_ptr.get_native_mut_ref().src_node_id;
515         crate::c_types::PublicKey::from_rust(&inner_val)
516 }
517 /// The node_id of the non-target end of the route
518 #[no_mangle]
519 pub extern "C" fn RouteHintHop_set_src_node_id(this_ptr: &mut RouteHintHop, mut val: crate::c_types::PublicKey) {
520         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.src_node_id = val.into_rust();
521 }
522 /// The short_channel_id of this channel
523 #[no_mangle]
524 pub extern "C" fn RouteHintHop_get_short_channel_id(this_ptr: &RouteHintHop) -> u64 {
525         let mut inner_val = &mut this_ptr.get_native_mut_ref().short_channel_id;
526         *inner_val
527 }
528 /// The short_channel_id of this channel
529 #[no_mangle]
530 pub extern "C" fn RouteHintHop_set_short_channel_id(this_ptr: &mut RouteHintHop, mut val: u64) {
531         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.short_channel_id = val;
532 }
533 /// The fees which must be paid to use this channel
534 #[no_mangle]
535 pub extern "C" fn RouteHintHop_get_fees(this_ptr: &RouteHintHop) -> crate::lightning::routing::network_graph::RoutingFees {
536         let mut inner_val = &mut this_ptr.get_native_mut_ref().fees;
537         crate::lightning::routing::network_graph::RoutingFees { inner: unsafe { ObjOps::nonnull_ptr_to_inner((inner_val as *const _) as *mut _) }, is_owned: false }
538 }
539 /// The fees which must be paid to use this channel
540 #[no_mangle]
541 pub extern "C" fn RouteHintHop_set_fees(this_ptr: &mut RouteHintHop, mut val: crate::lightning::routing::network_graph::RoutingFees) {
542         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.fees = *unsafe { Box::from_raw(val.take_inner()) };
543 }
544 /// The difference in CLTV values between this node and the next node.
545 #[no_mangle]
546 pub extern "C" fn RouteHintHop_get_cltv_expiry_delta(this_ptr: &RouteHintHop) -> u16 {
547         let mut inner_val = &mut this_ptr.get_native_mut_ref().cltv_expiry_delta;
548         *inner_val
549 }
550 /// The difference in CLTV values between this node and the next node.
551 #[no_mangle]
552 pub extern "C" fn RouteHintHop_set_cltv_expiry_delta(this_ptr: &mut RouteHintHop, mut val: u16) {
553         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.cltv_expiry_delta = val;
554 }
555 /// The minimum value, in msat, which must be relayed to the next hop.
556 #[no_mangle]
557 pub extern "C" fn RouteHintHop_get_htlc_minimum_msat(this_ptr: &RouteHintHop) -> crate::c_types::derived::COption_u64Z {
558         let mut inner_val = &mut this_ptr.get_native_mut_ref().htlc_minimum_msat;
559         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() }) };
560         local_inner_val
561 }
562 /// The minimum value, in msat, which must be relayed to the next hop.
563 #[no_mangle]
564 pub extern "C" fn RouteHintHop_set_htlc_minimum_msat(this_ptr: &mut RouteHintHop, mut val: crate::c_types::derived::COption_u64Z) {
565         let mut local_val = if val.is_some() { Some( { val.take() }) } else { None };
566         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.htlc_minimum_msat = local_val;
567 }
568 /// The maximum value in msat available for routing with a single HTLC.
569 #[no_mangle]
570 pub extern "C" fn RouteHintHop_get_htlc_maximum_msat(this_ptr: &RouteHintHop) -> crate::c_types::derived::COption_u64Z {
571         let mut inner_val = &mut this_ptr.get_native_mut_ref().htlc_maximum_msat;
572         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() }) };
573         local_inner_val
574 }
575 /// The maximum value in msat available for routing with a single HTLC.
576 #[no_mangle]
577 pub extern "C" fn RouteHintHop_set_htlc_maximum_msat(this_ptr: &mut RouteHintHop, mut val: crate::c_types::derived::COption_u64Z) {
578         let mut local_val = if val.is_some() { Some( { val.take() }) } else { None };
579         unsafe { &mut *ObjOps::untweak_ptr(this_ptr.inner) }.htlc_maximum_msat = local_val;
580 }
581 /// Constructs a new RouteHintHop given each field
582 #[must_use]
583 #[no_mangle]
584 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::network_graph::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 {
585         let mut local_htlc_minimum_msat_arg = if htlc_minimum_msat_arg.is_some() { Some( { htlc_minimum_msat_arg.take() }) } else { None };
586         let mut local_htlc_maximum_msat_arg = if htlc_maximum_msat_arg.is_some() { Some( { htlc_maximum_msat_arg.take() }) } else { None };
587         RouteHintHop { inner: ObjOps::heap_alloc(nativeRouteHintHop {
588                 src_node_id: src_node_id_arg.into_rust(),
589                 short_channel_id: short_channel_id_arg,
590                 fees: *unsafe { Box::from_raw(fees_arg.take_inner()) },
591                 cltv_expiry_delta: cltv_expiry_delta_arg,
592                 htlc_minimum_msat: local_htlc_minimum_msat_arg,
593                 htlc_maximum_msat: local_htlc_maximum_msat_arg,
594         }), is_owned: true }
595 }
596 impl Clone for RouteHintHop {
597         fn clone(&self) -> Self {
598                 Self {
599                         inner: if <*mut nativeRouteHintHop>::is_null(self.inner) { std::ptr::null_mut() } else {
600                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
601                         is_owned: true,
602                 }
603         }
604 }
605 #[allow(unused)]
606 /// Used only if an object of this type is returned as a trait impl by a method
607 pub(crate) extern "C" fn RouteHintHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
608         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHintHop)).clone() })) as *mut c_void
609 }
610 #[no_mangle]
611 /// Creates a copy of the RouteHintHop
612 pub extern "C" fn RouteHintHop_clone(orig: &RouteHintHop) -> RouteHintHop {
613         orig.clone()
614 }
615 /// Checks if two RouteHintHops contain equal inner contents.
616 #[no_mangle]
617 pub extern "C" fn RouteHintHop_hash(o: &RouteHintHop) -> u64 {
618         if o.inner.is_null() { return 0; }
619         // Note that we'd love to use std::collections::hash_map::DefaultHasher but it's not in core
620         #[allow(deprecated)]
621         let mut hasher = core::hash::SipHasher::new();
622         std::hash::Hash::hash(o.get_native_ref(), &mut hasher);
623         std::hash::Hasher::finish(&hasher)
624 }
625 /// Checks if two RouteHintHops contain equal inner contents.
626 /// This ignores pointers and is_owned flags and looks at the values in fields.
627 /// Two objects with NULL inner values will be considered "equal" here.
628 #[no_mangle]
629 pub extern "C" fn RouteHintHop_eq(a: &RouteHintHop, b: &RouteHintHop) -> bool {
630         if a.inner == b.inner { return true; }
631         if a.inner.is_null() || b.inner.is_null() { return false; }
632         if a.get_native_ref() == b.get_native_ref() { true } else { false }
633 }
634 /// Gets a keysend route from us (payer) to the given target node (payee). This is needed because
635 /// keysend payments do not have an invoice from which to pull the payee's supported features, which
636 /// makes it tricky to otherwise supply the `payee_features` parameter of `get_route`.
637 ///
638 /// Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
639 #[no_mangle]
640 pub extern "C" fn get_keysend_route(mut our_node_id: crate::c_types::PublicKey, network: &crate::lightning::routing::network_graph::NetworkGraph, mut payee: crate::c_types::PublicKey, first_hops: *mut crate::c_types::derived::CVec_ChannelDetailsZ, mut last_hops: crate::c_types::derived::CVec_RouteHintZ, mut final_value_msat: u64, mut final_cltv: u32, mut logger: crate::lightning::util::logger::Logger) -> crate::c_types::derived::CResult_RouteLightningErrorZ {
641         let mut local_first_hops_base = if first_hops == std::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[..]);
642         let mut local_last_hops = Vec::new(); for mut item in last_hops.as_slice().iter() { local_last_hops.push( { item.get_native_ref() }); };
643         let mut ret = lightning::routing::router::get_keysend_route(&our_node_id.into_rust(), network.get_native_ref(), &payee.into_rust(), local_first_hops, &local_last_hops[..], final_value_msat, final_cltv, logger);
644         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() };
645         local_ret
646 }
647
648 /// Gets a route from us (payer) to the given target node (payee).
649 ///
650 /// If the payee provided features in their invoice, they should be provided via payee_features.
651 /// Without this, MPP will only be used if the payee's features are available in the network graph.
652 ///
653 /// Private routing paths between a public node and the target may be included in `last_hops`.
654 /// Currently, only the last hop in each path is considered.
655 ///
656 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
657 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
658 /// view of our local channels (from net_graph_msg_handler) will be ignored, and only those
659 /// in first_hops will be used.
660 ///
661 /// Panics if first_hops contains channels without short_channel_ids
662 /// (ChannelManager::list_usable_channels will never include such channels).
663 ///
664 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
665 /// equal), however the enabled/disabled bit on such channels as well as the
666 /// htlc_minimum_msat/htlc_maximum_msat *are* checked as they may change based on the receiving node.
667 ///
668 /// Note that payee_features (or a relevant inner pointer) may be NULL or all-0s to represent None
669 /// Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
670 #[no_mangle]
671 pub extern "C" fn get_route(mut our_node_id: crate::c_types::PublicKey, network: &crate::lightning::routing::network_graph::NetworkGraph, mut payee: crate::c_types::PublicKey, mut payee_features: crate::lightning::ln::features::InvoiceFeatures, first_hops: *mut crate::c_types::derived::CVec_ChannelDetailsZ, mut last_hops: crate::c_types::derived::CVec_RouteHintZ, mut final_value_msat: u64, mut final_cltv: u32, mut logger: crate::lightning::util::logger::Logger) -> crate::c_types::derived::CResult_RouteLightningErrorZ {
672         let mut local_payee_features = if payee_features.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(payee_features.take_inner()) } }) };
673         let mut local_first_hops_base = if first_hops == std::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[..]);
674         let mut local_last_hops = Vec::new(); for mut item in last_hops.as_slice().iter() { local_last_hops.push( { item.get_native_ref() }); };
675         let mut ret = lightning::routing::router::get_route(&our_node_id.into_rust(), network.get_native_ref(), &payee.into_rust(), local_payee_features, local_first_hops, &local_last_hops[..], final_value_msat, final_cltv, logger);
676         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() };
677         local_ret
678 }
679