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.
14 use std::str::FromStr;
16 use bitcoin::hashes::Hash;
17 use crate::c_types::*;
20 use lightning::routing::router::RouteHop as nativeRouteHopImport;
21 type nativeRouteHop = nativeRouteHopImport;
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 nativeRouteHop,
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 RouteHop {
41 if self.is_owned && !<*mut nativeRouteHop>::is_null(self.inner) {
42 let _ = unsafe { Box::from_raw(self.inner) };
46 /// Frees any resources used by the RouteHop, if is_owned is set and inner is non-NULL.
48 pub extern "C" fn RouteHop_free(this_obj: RouteHop) { }
50 /// Used only if an object of this type is returned as a trait impl by a method
51 extern "C" fn RouteHop_free_void(this_ptr: *mut c_void) {
52 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRouteHop); }
55 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
57 pub(crate) fn take_inner(mut self) -> *mut nativeRouteHop {
58 assert!(self.is_owned);
60 self.inner = std::ptr::null_mut();
64 /// The node_id of the node at this hop.
66 pub extern "C" fn RouteHop_get_pubkey(this_ptr: &RouteHop) -> crate::c_types::PublicKey {
67 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.pubkey;
68 crate::c_types::PublicKey::from_rust(&inner_val)
70 /// The node_id of the node at this hop.
72 pub extern "C" fn RouteHop_set_pubkey(this_ptr: &mut RouteHop, mut val: crate::c_types::PublicKey) {
73 unsafe { &mut *this_ptr.inner }.pubkey = val.into_rust();
75 /// The node_announcement features of the node at this hop. For the last hop, these may be
76 /// amended to match the features present in the invoice this node generated.
78 pub extern "C" fn RouteHop_get_node_features(this_ptr: &RouteHop) -> crate::lightning::ln::features::NodeFeatures {
79 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.node_features;
80 crate::lightning::ln::features::NodeFeatures { inner: unsafe { ( (&(*inner_val) as *const _) as *mut _) }, is_owned: false }
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.
85 pub extern "C" fn RouteHop_set_node_features(this_ptr: &mut RouteHop, mut val: crate::lightning::ln::features::NodeFeatures) {
86 unsafe { &mut *this_ptr.inner }.node_features = *unsafe { Box::from_raw(val.take_inner()) };
88 /// The channel that should be used from the previous hop to reach this node.
90 pub extern "C" fn RouteHop_get_short_channel_id(this_ptr: &RouteHop) -> u64 {
91 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.short_channel_id;
94 /// The channel that should be used from the previous hop to reach this node.
96 pub extern "C" fn RouteHop_set_short_channel_id(this_ptr: &mut RouteHop, mut val: u64) {
97 unsafe { &mut *this_ptr.inner }.short_channel_id = val;
99 /// The channel_announcement features of the channel that should be used from the previous hop
100 /// to reach this node.
102 pub extern "C" fn RouteHop_get_channel_features(this_ptr: &RouteHop) -> crate::lightning::ln::features::ChannelFeatures {
103 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.channel_features;
104 crate::lightning::ln::features::ChannelFeatures { inner: unsafe { ( (&(*inner_val) as *const _) as *mut _) }, is_owned: false }
106 /// The channel_announcement features of the channel that should be used from the previous hop
107 /// to reach this node.
109 pub extern "C" fn RouteHop_set_channel_features(this_ptr: &mut RouteHop, mut val: crate::lightning::ln::features::ChannelFeatures) {
110 unsafe { &mut *this_ptr.inner }.channel_features = *unsafe { Box::from_raw(val.take_inner()) };
112 /// The fee taken on this hop (for paying for the use of the *next* channel in the path).
113 /// For the last hop, this should be the full value of the payment (might be more than
114 /// requested if we had to match htlc_minimum_msat).
116 pub extern "C" fn RouteHop_get_fee_msat(this_ptr: &RouteHop) -> u64 {
117 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.fee_msat;
120 /// The fee taken on this hop (for paying for the use of the *next* channel in the path).
121 /// For the last hop, this should be the full value of the payment (might be more than
122 /// requested if we had to match htlc_minimum_msat).
124 pub extern "C" fn RouteHop_set_fee_msat(this_ptr: &mut RouteHop, mut val: u64) {
125 unsafe { &mut *this_ptr.inner }.fee_msat = val;
127 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
128 /// expected at the destination, in excess of the current block height.
130 pub extern "C" fn RouteHop_get_cltv_expiry_delta(this_ptr: &RouteHop) -> u32 {
131 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry_delta;
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.
137 pub extern "C" fn RouteHop_set_cltv_expiry_delta(this_ptr: &mut RouteHop, mut val: u32) {
138 unsafe { &mut *this_ptr.inner }.cltv_expiry_delta = val;
140 /// Constructs a new RouteHop given each field
143 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 {
144 RouteHop { inner: Box::into_raw(Box::new(nativeRouteHop {
145 pubkey: pubkey_arg.into_rust(),
146 node_features: *unsafe { Box::from_raw(node_features_arg.take_inner()) },
147 short_channel_id: short_channel_id_arg,
148 channel_features: *unsafe { Box::from_raw(channel_features_arg.take_inner()) },
149 fee_msat: fee_msat_arg,
150 cltv_expiry_delta: cltv_expiry_delta_arg,
151 })), is_owned: true }
153 impl Clone for RouteHop {
154 fn clone(&self) -> Self {
156 inner: if <*mut nativeRouteHop>::is_null(self.inner) { std::ptr::null_mut() } else {
157 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
163 /// Used only if an object of this type is returned as a trait impl by a method
164 pub(crate) extern "C" fn RouteHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
165 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHop)).clone() })) as *mut c_void
168 /// Creates a copy of the RouteHop
169 pub extern "C" fn RouteHop_clone(orig: &RouteHop) -> RouteHop {
173 use lightning::routing::router::Route as nativeRouteImport;
174 type nativeRoute = nativeRouteImport;
176 /// A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
177 /// it can take multiple paths. Each path is composed of one or more hops through the network.
181 /// A pointer to the opaque Rust object.
183 /// Nearly everywhere, inner must be non-null, however in places where
184 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
185 pub inner: *mut nativeRoute,
186 /// Indicates that this is the only struct which contains the same pointer.
188 /// Rust functions which take ownership of an object provided via an argument require
189 /// this to be true and invalidate the object pointed to by inner.
193 impl Drop for Route {
195 if self.is_owned && !<*mut nativeRoute>::is_null(self.inner) {
196 let _ = unsafe { Box::from_raw(self.inner) };
200 /// Frees any resources used by the Route, if is_owned is set and inner is non-NULL.
202 pub extern "C" fn Route_free(this_obj: Route) { }
204 /// Used only if an object of this type is returned as a trait impl by a method
205 extern "C" fn Route_free_void(this_ptr: *mut c_void) {
206 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRoute); }
209 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
211 pub(crate) fn take_inner(mut self) -> *mut nativeRoute {
212 assert!(self.is_owned);
213 let ret = self.inner;
214 self.inner = std::ptr::null_mut();
218 /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
219 /// last RouteHop in each path must be the same.
220 /// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
221 /// destination. Thus, this must always be at least length one. While the maximum length of any
222 /// given path is variable, keeping the length of any path to less than 20 should currently
223 /// ensure it is viable.
225 pub extern "C" fn Route_set_paths(this_ptr: &mut Route, mut val: crate::c_types::derived::CVec_CVec_RouteHopZZ) {
226 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 }); };
227 unsafe { &mut *this_ptr.inner }.paths = local_val;
229 /// Constructs a new Route given each field
232 pub extern "C" fn Route_new(mut paths_arg: crate::c_types::derived::CVec_CVec_RouteHopZZ) -> Route {
233 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 }); };
234 Route { inner: Box::into_raw(Box::new(nativeRoute {
235 paths: local_paths_arg,
236 })), is_owned: true }
238 impl Clone for Route {
239 fn clone(&self) -> Self {
241 inner: if <*mut nativeRoute>::is_null(self.inner) { std::ptr::null_mut() } else {
242 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
248 /// Used only if an object of this type is returned as a trait impl by a method
249 pub(crate) extern "C" fn Route_clone_void(this_ptr: *const c_void) -> *mut c_void {
250 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRoute)).clone() })) as *mut c_void
253 /// Creates a copy of the Route
254 pub extern "C" fn Route_clone(orig: &Route) -> Route {
258 /// Serialize the Route object into a byte array which can be read by Route_read
259 pub extern "C" fn Route_write(obj: &Route) -> crate::c_types::derived::CVec_u8Z {
260 crate::c_types::serialize_obj(unsafe { &*unsafe { &*obj }.inner })
263 pub(crate) extern "C" fn Route_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
264 crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeRoute) })
267 /// Read a Route from a byte array, created by Route_write
268 pub extern "C" fn Route_read(ser: crate::c_types::u8slice) -> crate::c_types::derived::CResult_RouteDecodeErrorZ {
269 let res = crate::c_types::deserialize_obj(ser);
270 let mut local_res = match res { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::Route { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::DecodeError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };
274 use lightning::routing::router::RouteHintHop as nativeRouteHintHopImport;
275 type nativeRouteHintHop = nativeRouteHintHopImport;
277 /// A channel descriptor which provides a last-hop route to get_route
280 pub struct RouteHintHop {
281 /// A pointer to the opaque Rust object.
283 /// Nearly everywhere, inner must be non-null, however in places where
284 /// the Rust equivalent takes an Option, it may be set to null to indicate None.
285 pub inner: *mut nativeRouteHintHop,
286 /// Indicates that this is the only struct which contains the same pointer.
288 /// Rust functions which take ownership of an object provided via an argument require
289 /// this to be true and invalidate the object pointed to by inner.
293 impl Drop for RouteHintHop {
295 if self.is_owned && !<*mut nativeRouteHintHop>::is_null(self.inner) {
296 let _ = unsafe { Box::from_raw(self.inner) };
300 /// Frees any resources used by the RouteHintHop, if is_owned is set and inner is non-NULL.
302 pub extern "C" fn RouteHintHop_free(this_obj: RouteHintHop) { }
304 /// Used only if an object of this type is returned as a trait impl by a method
305 extern "C" fn RouteHintHop_free_void(this_ptr: *mut c_void) {
306 unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRouteHintHop); }
309 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
311 pub(crate) fn take_inner(mut self) -> *mut nativeRouteHintHop {
312 assert!(self.is_owned);
313 let ret = self.inner;
314 self.inner = std::ptr::null_mut();
318 /// The node_id of the non-target end of the route
320 pub extern "C" fn RouteHintHop_get_src_node_id(this_ptr: &RouteHintHop) -> crate::c_types::PublicKey {
321 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.src_node_id;
322 crate::c_types::PublicKey::from_rust(&inner_val)
324 /// The node_id of the non-target end of the route
326 pub extern "C" fn RouteHintHop_set_src_node_id(this_ptr: &mut RouteHintHop, mut val: crate::c_types::PublicKey) {
327 unsafe { &mut *this_ptr.inner }.src_node_id = val.into_rust();
329 /// The short_channel_id of this channel
331 pub extern "C" fn RouteHintHop_get_short_channel_id(this_ptr: &RouteHintHop) -> u64 {
332 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.short_channel_id;
335 /// The short_channel_id of this channel
337 pub extern "C" fn RouteHintHop_set_short_channel_id(this_ptr: &mut RouteHintHop, mut val: u64) {
338 unsafe { &mut *this_ptr.inner }.short_channel_id = val;
340 /// The fees which must be paid to use this channel
342 pub extern "C" fn RouteHintHop_get_fees(this_ptr: &RouteHintHop) -> crate::lightning::routing::network_graph::RoutingFees {
343 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.fees;
344 crate::lightning::routing::network_graph::RoutingFees { inner: unsafe { ( (&(*inner_val) as *const _) as *mut _) }, is_owned: false }
346 /// The fees which must be paid to use this channel
348 pub extern "C" fn RouteHintHop_set_fees(this_ptr: &mut RouteHintHop, mut val: crate::lightning::routing::network_graph::RoutingFees) {
349 unsafe { &mut *this_ptr.inner }.fees = *unsafe { Box::from_raw(val.take_inner()) };
351 /// The difference in CLTV values between this node and the next node.
353 pub extern "C" fn RouteHintHop_get_cltv_expiry_delta(this_ptr: &RouteHintHop) -> u16 {
354 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry_delta;
357 /// The difference in CLTV values between this node and the next node.
359 pub extern "C" fn RouteHintHop_set_cltv_expiry_delta(this_ptr: &mut RouteHintHop, mut val: u16) {
360 unsafe { &mut *this_ptr.inner }.cltv_expiry_delta = val;
362 /// The minimum value, in msat, which must be relayed to the next hop.
364 pub extern "C" fn RouteHintHop_get_htlc_minimum_msat(this_ptr: &RouteHintHop) -> crate::c_types::derived::COption_u64Z {
365 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_minimum_msat;
366 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()) } };
369 /// The minimum value, in msat, which must be relayed to the next hop.
371 pub extern "C" fn RouteHintHop_set_htlc_minimum_msat(this_ptr: &mut RouteHintHop, mut val: crate::c_types::derived::COption_u64Z) {
372 let mut local_val = if val.is_some() { Some( { val.take() }) } else { None };
373 unsafe { &mut *this_ptr.inner }.htlc_minimum_msat = local_val;
375 /// The maximum value in msat available for routing with a single HTLC.
377 pub extern "C" fn RouteHintHop_get_htlc_maximum_msat(this_ptr: &RouteHintHop) -> crate::c_types::derived::COption_u64Z {
378 let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_maximum_msat;
379 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()) } };
382 /// The maximum value in msat available for routing with a single HTLC.
384 pub extern "C" fn RouteHintHop_set_htlc_maximum_msat(this_ptr: &mut RouteHintHop, mut val: crate::c_types::derived::COption_u64Z) {
385 let mut local_val = if val.is_some() { Some( { val.take() }) } else { None };
386 unsafe { &mut *this_ptr.inner }.htlc_maximum_msat = local_val;
388 /// Constructs a new RouteHintHop given each field
391 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 {
392 let mut local_htlc_minimum_msat_arg = if htlc_minimum_msat_arg.is_some() { Some( { htlc_minimum_msat_arg.take() }) } else { None };
393 let mut local_htlc_maximum_msat_arg = if htlc_maximum_msat_arg.is_some() { Some( { htlc_maximum_msat_arg.take() }) } else { None };
394 RouteHintHop { inner: Box::into_raw(Box::new(nativeRouteHintHop {
395 src_node_id: src_node_id_arg.into_rust(),
396 short_channel_id: short_channel_id_arg,
397 fees: *unsafe { Box::from_raw(fees_arg.take_inner()) },
398 cltv_expiry_delta: cltv_expiry_delta_arg,
399 htlc_minimum_msat: local_htlc_minimum_msat_arg,
400 htlc_maximum_msat: local_htlc_maximum_msat_arg,
401 })), is_owned: true }
403 /// Checks if two RouteHintHops contain equal inner contents.
404 /// This ignores pointers and is_owned flags and looks at the values in fields.
405 /// Two objects with NULL inner values will be considered "equal" here.
407 pub extern "C" fn RouteHintHop_eq(a: &RouteHintHop, b: &RouteHintHop) -> bool {
408 if a.inner == b.inner { return true; }
409 if a.inner.is_null() || b.inner.is_null() { return false; }
410 if unsafe { &*a.inner } == unsafe { &*b.inner } { true } else { false }
412 impl Clone for RouteHintHop {
413 fn clone(&self) -> Self {
415 inner: if <*mut nativeRouteHintHop>::is_null(self.inner) { std::ptr::null_mut() } else {
416 Box::into_raw(Box::new(unsafe { &*self.inner }.clone())) },
422 /// Used only if an object of this type is returned as a trait impl by a method
423 pub(crate) extern "C" fn RouteHintHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
424 Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHintHop)).clone() })) as *mut c_void
427 /// Creates a copy of the RouteHintHop
428 pub extern "C" fn RouteHintHop_clone(orig: &RouteHintHop) -> RouteHintHop {
431 /// Gets a route from us (payer) to the given target node (payee).
433 /// If the payee provided features in their invoice, they should be provided via payee_features.
434 /// Without this, MPP will only be used if the payee's features are available in the network graph.
436 /// Extra routing hops between known nodes and the target will be used if they are included in
439 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
440 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
441 /// view of our local channels (from net_graph_msg_handler) will be ignored, and only those
442 /// in first_hops will be used.
444 /// Panics if first_hops contains channels without short_channel_ids
445 /// (ChannelManager::list_usable_channels will never include such channels).
447 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
448 /// equal), however the enabled/disabled bit on such channels as well as the
449 /// htlc_minimum_msat/htlc_maximum_msat *are* checked as they may change based on the receiving node.
451 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_RouteHintHopZ, mut final_value_msat: u64, mut final_cltv: u32, mut logger: crate::lightning::util::logger::Logger) -> crate::c_types::derived::CResult_RouteLightningErrorZ {
452 let mut local_payee_features = if payee_features.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(payee_features.take_inner()) } }) };
453 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[..]);
454 let mut local_last_hops = Vec::new(); for mut item in last_hops.as_slice().iter() { local_last_hops.push( { unsafe { &*item.inner } }); };
455 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);
456 let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::lightning::routing::router::Route { inner: Box::into_raw(Box::new(o)), is_owned: true } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }).into() };