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 top-level routing/network map tracking logic lives here.
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.
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
19 use lightning::routing::router::RouteHop as nativeRouteHopImport;
20 type nativeRouteHop = nativeRouteHopImport;
26 /// A pointer to the opaque Rust object.
28 /// Nearly everywhere, inner must be non-null, however in places where
29 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
30 pub inner: *mut nativeRouteHop,
31 /// Indicates that this is the only struct which contains the same pointer.
33 /// Rust functions which take ownership of an object provided via an argument require
34 /// this to be true and invalidate the object pointed to by inner.
38 impl Drop for RouteHop {
40 if self.is_owned && !<*mut nativeRouteHop>::is_null(self.inner) {
41 let _ = unsafe { Box::from_raw(self.inner) };
45 /// Frees any resources used by the RouteHop, if is_owned is set and inner is non-NULL.
47 pub extern "C" fn RouteHop_free(this_obj: RouteHop) { }
49 /// Used only if an object of this type is returned as a trait impl by a method
50 extern "C" fn RouteHop_free_void(this_ptr: *mut c_void) {
51 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRouteHop); }
54 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
56 pub(crate) fn take_inner(mut self) -> *mut nativeRouteHop {
57 assert!(self.is_owned);
59 self.inner = std::ptr::null_mut();
63 /// The node_id of the node at this hop.
65 pub extern "C" fn RouteHop_get_pubkey(this_ptr: &RouteHop) -> crate::c_types::PublicKey {
66 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.pubkey;
67 crate::c_types::PublicKey::from_rust(&(*inner_val))
69 /// The node_id of the node at this hop.
71 pub extern "C" fn RouteHop_set_pubkey(this_ptr: &mut RouteHop, mut val: crate::c_types::PublicKey) {
72 unsafe { &mut *this_ptr.inner }.pubkey = val.into_rust();
74 /// The node_announcement features of the node at this hop. For the last hop, these may be
75 /// amended to match the features present in the invoice this node generated.
77 pub extern "C" fn RouteHop_get_node_features(this_ptr: &RouteHop) -> crate::ln::features::NodeFeatures {
78 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.node_features;
79 crate::ln::features::NodeFeatures { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
81 /// The node_announcement features of the node at this hop. For the last hop, these may be
82 /// amended to match the features present in the invoice this node generated.
84 pub extern "C" fn RouteHop_set_node_features(this_ptr: &mut RouteHop, mut val: crate::ln::features::NodeFeatures) {
85 unsafe { &mut *this_ptr.inner }.node_features = *unsafe { Box::from_raw(val.take_inner()) };
87 /// The channel that should be used from the previous hop to reach this node.
89 pub extern "C" fn RouteHop_get_short_channel_id(this_ptr: &RouteHop) -> u64 {
90 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.short_channel_id;
93 /// The channel that should be used from the previous hop to reach this node.
95 pub extern "C" fn RouteHop_set_short_channel_id(this_ptr: &mut RouteHop, mut val: u64) {
96 unsafe { &mut *this_ptr.inner }.short_channel_id = val;
98 /// The channel_announcement features of the channel that should be used from the previous hop
99 /// to reach this node.
101 pub extern "C" fn RouteHop_get_channel_features(this_ptr: &RouteHop) -> crate::ln::features::ChannelFeatures {
102 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.channel_features;
103 crate::ln::features::ChannelFeatures { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
105 /// The channel_announcement features of the channel that should be used from the previous hop
106 /// to reach this node.
108 pub extern "C" fn RouteHop_set_channel_features(this_ptr: &mut RouteHop, mut val: crate::ln::features::ChannelFeatures) {
109 unsafe { &mut *this_ptr.inner }.channel_features = *unsafe { Box::from_raw(val.take_inner()) };
111 /// The fee taken on this hop (for paying for the use of the *next* channel in the path).
112 /// For the last hop, this should be the full value of the payment (might be more than
113 /// requested if we had to match htlc_minimum_msat).
115 pub extern "C" fn RouteHop_get_fee_msat(this_ptr: &RouteHop) -> u64 {
116 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.fee_msat;
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).
123 pub extern "C" fn RouteHop_set_fee_msat(this_ptr: &mut RouteHop, mut val: u64) {
124 unsafe { &mut *this_ptr.inner }.fee_msat = val;
126 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
127 /// expected at the destination, in excess of the current block height.
129 pub extern "C" fn RouteHop_get_cltv_expiry_delta(this_ptr: &RouteHop) -> u32 {
130 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry_delta;
133 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
134 /// expected at the destination, in excess of the current block height.
136 pub extern "C" fn RouteHop_set_cltv_expiry_delta(this_ptr: &mut RouteHop, mut val: u32) {
137 unsafe { &mut *this_ptr.inner }.cltv_expiry_delta = val;
139 /// Constructs a new RouteHop given each field
142 pub extern "C" fn RouteHop_new(mut pubkey_arg: crate::c_types::PublicKey, mut node_features_arg: crate::ln::features::NodeFeatures, mut short_channel_id_arg: u64, mut channel_features_arg: crate::ln::features::ChannelFeatures, mut fee_msat_arg: u64, mut cltv_expiry_delta_arg: u32) -> RouteHop {
143 RouteHop { inner: Box::into_raw(Box::new(nativeRouteHop {
144 pubkey: pubkey_arg.into_rust(),
145 node_features: *unsafe { Box::from_raw(node_features_arg.take_inner()) },
146 short_channel_id: short_channel_id_arg,
147 channel_features: *unsafe { Box::from_raw(channel_features_arg.take_inner()) },
148 fee_msat: fee_msat_arg,
149 cltv_expiry_delta: cltv_expiry_delta_arg,
150 })), is_owned: true }
152 impl Clone for RouteHop {
153 fn clone(&self) -> Self {
155 inner: if <*mut nativeRouteHop>::is_null(self.inner) { std::ptr::null_mut() } else {
156 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
162 /// Used only if an object of this type is returned as a trait impl by a method
163 pub(crate) extern "C" fn RouteHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
164 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHop)).clone() })) as *mut c_void
167 /// Creates a copy of the RouteHop
168 pub extern "C" fn RouteHop_clone(orig: &RouteHop) -> RouteHop {
172 use lightning::routing::router::Route as nativeRouteImport;
173 type nativeRoute = nativeRouteImport;
175 /// A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
176 /// it can take multiple paths. Each path is composed of one or more hops through the network.
180 /// A pointer to the opaque Rust object.
182 /// Nearly everywhere, inner must be non-null, however in places where
183 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
184 pub inner: *mut nativeRoute,
185 /// Indicates that this is the only struct which contains the same pointer.
187 /// Rust functions which take ownership of an object provided via an argument require
188 /// this to be true and invalidate the object pointed to by inner.
192 impl Drop for Route {
194 if self.is_owned && !<*mut nativeRoute>::is_null(self.inner) {
195 let _ = unsafe { Box::from_raw(self.inner) };
199 /// Frees any resources used by the Route, if is_owned is set and inner is non-NULL.
201 pub extern "C" fn Route_free(this_obj: Route) { }
203 /// Used only if an object of this type is returned as a trait impl by a method
204 extern "C" fn Route_free_void(this_ptr: *mut c_void) {
205 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRoute); }
208 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
210 pub(crate) fn take_inner(mut self) -> *mut nativeRoute {
211 assert!(self.is_owned);
212 let ret = self.inner;
213 self.inner = std::ptr::null_mut();
217 /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
218 /// last RouteHop in each path must be the same.
219 /// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
220 /// destination. Thus, this must always be at least length one. While the maximum length of any
221 /// given path is variable, keeping the length of any path to less than 20 should currently
222 /// ensure it is viable.
224 pub extern "C" fn Route_set_paths(this_ptr: &mut Route, mut val: crate::c_types::derived::CVec_CVec_RouteHopZZ) {
225 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 }); };
226 unsafe { &mut *this_ptr.inner }.paths = local_val;
228 /// Constructs a new Route given each field
231 pub extern "C" fn Route_new(mut paths_arg: crate::c_types::derived::CVec_CVec_RouteHopZZ) -> Route {
232 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 }); };
233 Route { inner: Box::into_raw(Box::new(nativeRoute {
234 paths: local_paths_arg,
235 })), is_owned: true }
237 impl Clone for Route {
238 fn clone(&self) -> Self {
240 inner: if <*mut nativeRoute>::is_null(self.inner) { std::ptr::null_mut() } else {
241 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
247 /// Used only if an object of this type is returned as a trait impl by a method
248 pub(crate) extern "C" fn Route_clone_void(this_ptr: *const c_void) -> *mut c_void {
249 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRoute)).clone() })) as *mut c_void
252 /// Creates a copy of the Route
253 pub extern "C" fn Route_clone(orig: &Route) -> Route {
257 /// Serialize the Route object into a byte array which can be read by Route_read
258 pub extern "C" fn Route_write(obj: &Route) -> crate::c_types::derived::CVec_u8Z {
259 crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
262 pub(crate) extern "C" fn Route_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
263 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRoute) })
266 /// Read a Route from a byte array, created by Route_write
267 pub extern "C" fn Route_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RouteDecodeErrorZ {
268 let res = crate::c_types::deserialize_obj(ser);
269 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::routing::router::Route { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
273 use lightning::routing::router::RouteHint as nativeRouteHintImport;
274 type nativeRouteHint = nativeRouteHintImport;
276 /// A channel descriptor which provides a last-hop route to get_route
279 pub struct RouteHint {
280 /// A pointer to the opaque Rust object.
282 /// Nearly everywhere, inner must be non-null, however in places where
283 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
284 pub inner: *mut nativeRouteHint,
285 /// Indicates that this is the only struct which contains the same pointer.
287 /// Rust functions which take ownership of an object provided via an argument require
288 /// this to be true and invalidate the object pointed to by inner.
292 impl Drop for RouteHint {
294 if self.is_owned && !<*mut nativeRouteHint>::is_null(self.inner) {
295 let _ = unsafe { Box::from_raw(self.inner) };
299 /// Frees any resources used by the RouteHint, if is_owned is set and inner is non-NULL.
301 pub extern "C" fn RouteHint_free(this_obj: RouteHint) { }
303 /// Used only if an object of this type is returned as a trait impl by a method
304 extern "C" fn RouteHint_free_void(this_ptr: *mut c_void) {
305 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRouteHint); }
308 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
310 pub(crate) fn take_inner(mut self) -> *mut nativeRouteHint {
311 assert!(self.is_owned);
312 let ret = self.inner;
313 self.inner = std::ptr::null_mut();
317 /// The node_id of the non-target end of the route
319 pub extern "C" fn RouteHint_get_src_node_id(this_ptr: &RouteHint) -> crate::c_types::PublicKey {
320 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.src_node_id;
321 crate::c_types::PublicKey::from_rust(&(*inner_val))
323 /// The node_id of the non-target end of the route
325 pub extern "C" fn RouteHint_set_src_node_id(this_ptr: &mut RouteHint, mut val: crate::c_types::PublicKey) {
326 unsafe { &mut *this_ptr.inner }.src_node_id = val.into_rust();
328 /// The short_channel_id of this channel
330 pub extern "C" fn RouteHint_get_short_channel_id(this_ptr: &RouteHint) -> u64 {
331 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.short_channel_id;
334 /// The short_channel_id of this channel
336 pub extern "C" fn RouteHint_set_short_channel_id(this_ptr: &mut RouteHint, mut val: u64) {
337 unsafe { &mut *this_ptr.inner }.short_channel_id = val;
339 /// The fees which must be paid to use this channel
341 pub extern "C" fn RouteHint_get_fees(this_ptr: &RouteHint) -> crate::routing::network_graph::RoutingFees {
342 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.fees;
343 crate::routing::network_graph::RoutingFees { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
345 /// The fees which must be paid to use this channel
347 pub extern "C" fn RouteHint_set_fees(this_ptr: &mut RouteHint, mut val: crate::routing::network_graph::RoutingFees) {
348 unsafe { &mut *this_ptr.inner }.fees = *unsafe { Box::from_raw(val.take_inner()) };
350 /// The difference in CLTV values between this node and the next node.
352 pub extern "C" fn RouteHint_get_cltv_expiry_delta(this_ptr: &RouteHint) -> u16 {
353 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry_delta;
356 /// The difference in CLTV values between this node and the next node.
358 pub extern "C" fn RouteHint_set_cltv_expiry_delta(this_ptr: &mut RouteHint, mut val: u16) {
359 unsafe { &mut *this_ptr.inner }.cltv_expiry_delta = val;
361 /// The minimum value, in msat, which must be relayed to the next hop.
363 pub extern "C" fn RouteHint_get_htlc_minimum_msat(this_ptr: &RouteHint) -> crate::c_types::derived::COption_u64Z {
364 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_minimum_msat;
365 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()) } };
368 /// The minimum value, in msat, which must be relayed to the next hop.
370 pub extern "C" fn RouteHint_set_htlc_minimum_msat(this_ptr: &mut RouteHint, mut val: crate::c_types::derived::COption_u64Z) {
371 let mut local_val = if val.is_some() { Some( { val.take() }) } else { None };
372 unsafe { &mut *this_ptr.inner }.htlc_minimum_msat = local_val;
374 /// The maximum value in msat available for routing with a single HTLC.
376 pub extern "C" fn RouteHint_get_htlc_maximum_msat(this_ptr: &RouteHint) -> crate::c_types::derived::COption_u64Z {
377 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_maximum_msat;
378 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()) } };
381 /// The maximum value in msat available for routing with a single HTLC.
383 pub extern "C" fn RouteHint_set_htlc_maximum_msat(this_ptr: &mut RouteHint, mut val: crate::c_types::derived::COption_u64Z) {
384 let mut local_val = if val.is_some() { Some( { val.take() }) } else { None };
385 unsafe { &mut *this_ptr.inner }.htlc_maximum_msat = local_val;
387 /// Constructs a new RouteHint given each field
390 pub extern "C" fn RouteHint_new(mut src_node_id_arg: crate::c_types::PublicKey, mut short_channel_id_arg: u64, mut fees_arg: crate::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) -> RouteHint {
391 let mut local_htlc_minimum_msat_arg = if htlc_minimum_msat_arg.is_some() { Some( { htlc_minimum_msat_arg.take() }) } else { None };
392 let mut local_htlc_maximum_msat_arg = if htlc_maximum_msat_arg.is_some() { Some( { htlc_maximum_msat_arg.take() }) } else { None };
393 RouteHint { inner: Box::into_raw(Box::new(nativeRouteHint {
394 src_node_id: src_node_id_arg.into_rust(),
395 short_channel_id: short_channel_id_arg,
396 fees: *unsafe { Box::from_raw(fees_arg.take_inner()) },
397 cltv_expiry_delta: cltv_expiry_delta_arg,
398 htlc_minimum_msat: local_htlc_minimum_msat_arg,
399 htlc_maximum_msat: local_htlc_maximum_msat_arg,
400 })), is_owned: true }
402 impl Clone for RouteHint {
403 fn clone(&self) -> Self {
405 inner: if <*mut nativeRouteHint>::is_null(self.inner) { std::ptr::null_mut() } else {
406 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
412 /// Used only if an object of this type is returned as a trait impl by a method
413 pub(crate) extern "C" fn RouteHint_clone_void(this_ptr: *const c_void) -> *mut c_void {
414 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHint)).clone() })) as *mut c_void
417 /// Creates a copy of the RouteHint
418 pub extern "C" fn RouteHint_clone(orig: &RouteHint) -> RouteHint {
421 /// Gets a route from us (payer) to the given target node (payee).
423 /// If the payee provided features in their invoice, they should be provided via payee_features.
424 /// Without this, MPP will only be used if the payee's features are available in the network graph.
426 /// Extra routing hops between known nodes and the target will be used if they are included in
429 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
430 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
431 /// view of our local channels (from net_graph_msg_handler) will be ignored, and only those
432 /// in first_hops will be used.
434 /// Panics if first_hops contains channels without short_channel_ids
435 /// (ChannelManager::list_usable_channels will never include such channels).
437 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
438 /// equal), however the enabled/disabled bit on such channels as well as the
439 /// htlc_minimum_msat/htlc_maximum_msat *are* checked as they may change based on the receiving node.
441 pub extern "C" fn get_route(mut our_node_id: crate::c_types::PublicKey, network: &crate::routing::network_graph::NetworkGraph, mut payee: crate::c_types::PublicKey, mut payee_features: crate::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::util::logger::Logger) -> crate::c_types::derived::CResult_RouteLightningErrorZ {
442 let mut local_payee_features = if payee_features.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(payee_features.take_inner()) } }) };
443 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( { unsafe { &*item.inner } }); }; local_first_hops_0 }) }; let mut local_first_hops = local_first_hops_base.as_ref().map(|a| &a[..]);
444 let mut local_last_hops = Vec::new(); for mut item in last_hops.as_slice().iter() { local_last_hops.push( { unsafe { &*item.inner } }); };
445 let mut ret = lightning::routing::router::get_route(&our_node_id.into_rust(), unsafe { &*network.inner }, &payee.into_rust(), local_payee_features, local_first_hops, &local_last_hops[..], final_value_msat, final_cltv, logger);
446 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::routing::router::Route { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };