Merge pull request #750 from TheBlueMatt/2020-11-dup-chan-id-crash
[rust-lightning] / lightning-c-bindings / src / routing / router.rs
1 //! The top-level routing/network map tracking logic lives here.
2 //!
3 //! You probably want to create a NetGraphMsgHandler and use that as your RoutingMessageHandler and then
4 //! interrogate it to get routes for your own payments.
5
6 use std::ffi::c_void;
7 use bitcoin::hashes::Hash;
8 use crate::c_types::*;
9
10
11 use lightning::routing::router::RouteHop as nativeRouteHopImport;
12 type nativeRouteHop = nativeRouteHopImport;
13
14 /// A hop in a route
15 #[must_use]
16 #[repr(C)]
17 pub struct RouteHop {
18         /// Nearly everywhere, inner must be non-null, however in places where
19         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
20         pub inner: *mut nativeRouteHop,
21         pub is_owned: bool,
22 }
23
24 impl Drop for RouteHop {
25         fn drop(&mut self) {
26                 if self.is_owned && !self.inner.is_null() {
27                         let _ = unsafe { Box::from_raw(self.inner) };
28                 }
29         }
30 }
31 #[no_mangle]
32 pub extern "C" fn RouteHop_free(this_ptr: RouteHop) { }
33 #[allow(unused)]
34 /// Used only if an object of this type is returned as a trait impl by a method
35 extern "C" fn RouteHop_free_void(this_ptr: *mut c_void) {
36         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRouteHop); }
37 }
38 #[allow(unused)]
39 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
40 impl RouteHop {
41         pub(crate) fn take_ptr(mut self) -> *mut nativeRouteHop {
42                 assert!(self.is_owned);
43                 let ret = self.inner;
44                 self.inner = std::ptr::null_mut();
45                 ret
46         }
47 }
48 impl Clone for RouteHop {
49         fn clone(&self) -> Self {
50                 Self {
51                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
52                         is_owned: true,
53                 }
54         }
55 }
56 #[allow(unused)]
57 /// Used only if an object of this type is returned as a trait impl by a method
58 pub(crate) extern "C" fn RouteHop_clone_void(this_ptr: *const c_void) -> *mut c_void {
59         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHop)).clone() })) as *mut c_void
60 }
61 #[no_mangle]
62 pub extern "C" fn RouteHop_clone(orig: &RouteHop) -> RouteHop {
63         RouteHop { inner: Box::into_raw(Box::new(unsafe { &*orig.inner }.clone())), is_owned: true }
64 }
65 /// The node_id of the node at this hop.
66 #[no_mangle]
67 pub extern "C" fn RouteHop_get_pubkey(this_ptr: &RouteHop) -> crate::c_types::PublicKey {
68         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.pubkey;
69         crate::c_types::PublicKey::from_rust(&(*inner_val))
70 }
71 /// The node_id of the node at this hop.
72 #[no_mangle]
73 pub extern "C" fn RouteHop_set_pubkey(this_ptr: &mut RouteHop, mut val: crate::c_types::PublicKey) {
74         unsafe { &mut *this_ptr.inner }.pubkey = val.into_rust();
75 }
76 /// The node_announcement features of the node at this hop. For the last hop, these may be
77 /// amended to match the features present in the invoice this node generated.
78 #[no_mangle]
79 pub extern "C" fn RouteHop_get_node_features(this_ptr: &RouteHop) -> crate::ln::features::NodeFeatures {
80         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.node_features;
81         crate::ln::features::NodeFeatures { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
82 }
83 /// The node_announcement features of the node at this hop. For the last hop, these may be
84 /// amended to match the features present in the invoice this node generated.
85 #[no_mangle]
86 pub extern "C" fn RouteHop_set_node_features(this_ptr: &mut RouteHop, mut val: crate::ln::features::NodeFeatures) {
87         unsafe { &mut *this_ptr.inner }.node_features = *unsafe { Box::from_raw(val.take_ptr()) };
88 }
89 /// The channel that should be used from the previous hop to reach this node.
90 #[no_mangle]
91 pub extern "C" fn RouteHop_get_short_channel_id(this_ptr: &RouteHop) -> u64 {
92         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.short_channel_id;
93         (*inner_val)
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_set_short_channel_id(this_ptr: &mut RouteHop, mut val: u64) {
98         unsafe { &mut *this_ptr.inner }.short_channel_id = val;
99 }
100 /// The channel_announcement features of the channel that should be used from the previous hop
101 /// to reach this node.
102 #[no_mangle]
103 pub extern "C" fn RouteHop_get_channel_features(this_ptr: &RouteHop) -> crate::ln::features::ChannelFeatures {
104         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.channel_features;
105         crate::ln::features::ChannelFeatures { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
106 }
107 /// The channel_announcement features of the channel that should be used from the previous hop
108 /// to reach this node.
109 #[no_mangle]
110 pub extern "C" fn RouteHop_set_channel_features(this_ptr: &mut RouteHop, mut val: crate::ln::features::ChannelFeatures) {
111         unsafe { &mut *this_ptr.inner }.channel_features = *unsafe { Box::from_raw(val.take_ptr()) };
112 }
113 /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
114 #[no_mangle]
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;
117         (*inner_val)
118 }
119 /// The fee taken on this hop. For the last hop, this should be the full value of the payment.
120 #[no_mangle]
121 pub extern "C" fn RouteHop_set_fee_msat(this_ptr: &mut RouteHop, mut val: u64) {
122         unsafe { &mut *this_ptr.inner }.fee_msat = val;
123 }
124 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
125 /// expected at the destination, in excess of the current block height.
126 #[no_mangle]
127 pub extern "C" fn RouteHop_get_cltv_expiry_delta(this_ptr: &RouteHop) -> u32 {
128         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry_delta;
129         (*inner_val)
130 }
131 /// The CLTV delta added for this hop. For the last hop, this should be the full CLTV value
132 /// expected at the destination, in excess of the current block height.
133 #[no_mangle]
134 pub extern "C" fn RouteHop_set_cltv_expiry_delta(this_ptr: &mut RouteHop, mut val: u32) {
135         unsafe { &mut *this_ptr.inner }.cltv_expiry_delta = val;
136 }
137 #[must_use]
138 #[no_mangle]
139 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 {
140         RouteHop { inner: Box::into_raw(Box::new(nativeRouteHop {
141                 pubkey: pubkey_arg.into_rust(),
142                 node_features: *unsafe { Box::from_raw(node_features_arg.take_ptr()) },
143                 short_channel_id: short_channel_id_arg,
144                 channel_features: *unsafe { Box::from_raw(channel_features_arg.take_ptr()) },
145                 fee_msat: fee_msat_arg,
146                 cltv_expiry_delta: cltv_expiry_delta_arg,
147         })), is_owned: true }
148 }
149
150 use lightning::routing::router::Route as nativeRouteImport;
151 type nativeRoute = nativeRouteImport;
152
153 /// A route directs a payment from the sender (us) to the recipient. If the recipient supports MPP,
154 /// it can take multiple paths. Each path is composed of one or more hops through the network.
155 #[must_use]
156 #[repr(C)]
157 pub struct Route {
158         /// Nearly everywhere, inner must be non-null, however in places where
159         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
160         pub inner: *mut nativeRoute,
161         pub is_owned: bool,
162 }
163
164 impl Drop for Route {
165         fn drop(&mut self) {
166                 if self.is_owned && !self.inner.is_null() {
167                         let _ = unsafe { Box::from_raw(self.inner) };
168                 }
169         }
170 }
171 #[no_mangle]
172 pub extern "C" fn Route_free(this_ptr: Route) { }
173 #[allow(unused)]
174 /// Used only if an object of this type is returned as a trait impl by a method
175 extern "C" fn Route_free_void(this_ptr: *mut c_void) {
176         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRoute); }
177 }
178 #[allow(unused)]
179 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
180 impl Route {
181         pub(crate) fn take_ptr(mut self) -> *mut nativeRoute {
182                 assert!(self.is_owned);
183                 let ret = self.inner;
184                 self.inner = std::ptr::null_mut();
185                 ret
186         }
187 }
188 impl Clone for Route {
189         fn clone(&self) -> Self {
190                 Self {
191                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
192                         is_owned: true,
193                 }
194         }
195 }
196 #[allow(unused)]
197 /// Used only if an object of this type is returned as a trait impl by a method
198 pub(crate) extern "C" fn Route_clone_void(this_ptr: *const c_void) -> *mut c_void {
199         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRoute)).clone() })) as *mut c_void
200 }
201 #[no_mangle]
202 pub extern "C" fn Route_clone(orig: &Route) -> Route {
203         Route { inner: Box::into_raw(Box::new(unsafe { &*orig.inner }.clone())), is_owned: true }
204 }
205 /// The list of routes taken for a single (potentially-)multi-part payment. The pubkey of the
206 /// last RouteHop in each path must be the same.
207 /// Each entry represents a list of hops, NOT INCLUDING our own, where the last hop is the
208 /// destination. Thus, this must always be at least length one. While the maximum length of any
209 /// given path is variable, keeping the length of any path to less than 20 should currently
210 /// ensure it is viable.
211 #[no_mangle]
212 pub extern "C" fn Route_set_paths(this_ptr: &mut Route, mut val: crate::c_types::derived::CVec_CVec_RouteHopZZ) {
213         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_ptr()) } }); }; local_val_0 }); };
214         unsafe { &mut *this_ptr.inner }.paths = local_val;
215 }
216 #[must_use]
217 #[no_mangle]
218 pub extern "C" fn Route_new(mut paths_arg: crate::c_types::derived::CVec_CVec_RouteHopZZ) -> Route {
219         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_ptr()) } }); }; local_paths_arg_0 }); };
220         Route { inner: Box::into_raw(Box::new(nativeRoute {
221                 paths: local_paths_arg,
222         })), is_owned: true }
223 }
224 #[no_mangle]
225 pub extern "C" fn Route_write(obj: *const Route) -> crate::c_types::derived::CVec_u8Z {
226         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
227 }
228 #[no_mangle]
229 pub extern "C" fn Route_read(ser: crate::c_types::u8slice) -> Route {
230         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
231                 Route { inner: Box::into_raw(Box::new(res)), is_owned: true }
232         } else {
233                 Route { inner: std::ptr::null_mut(), is_owned: true }
234         }
235 }
236
237 use lightning::routing::router::RouteHint as nativeRouteHintImport;
238 type nativeRouteHint = nativeRouteHintImport;
239
240 /// A channel descriptor which provides a last-hop route to get_route
241 #[must_use]
242 #[repr(C)]
243 pub struct RouteHint {
244         /// Nearly everywhere, inner must be non-null, however in places where
245         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
246         pub inner: *mut nativeRouteHint,
247         pub is_owned: bool,
248 }
249
250 impl Drop for RouteHint {
251         fn drop(&mut self) {
252                 if self.is_owned && !self.inner.is_null() {
253                         let _ = unsafe { Box::from_raw(self.inner) };
254                 }
255         }
256 }
257 #[no_mangle]
258 pub extern "C" fn RouteHint_free(this_ptr: RouteHint) { }
259 #[allow(unused)]
260 /// Used only if an object of this type is returned as a trait impl by a method
261 extern "C" fn RouteHint_free_void(this_ptr: *mut c_void) {
262         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRouteHint); }
263 }
264 #[allow(unused)]
265 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
266 impl RouteHint {
267         pub(crate) fn take_ptr(mut self) -> *mut nativeRouteHint {
268                 assert!(self.is_owned);
269                 let ret = self.inner;
270                 self.inner = std::ptr::null_mut();
271                 ret
272         }
273 }
274 impl Clone for RouteHint {
275         fn clone(&self) -> Self {
276                 Self {
277                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
278                         is_owned: true,
279                 }
280         }
281 }
282 #[allow(unused)]
283 /// Used only if an object of this type is returned as a trait impl by a method
284 pub(crate) extern "C" fn RouteHint_clone_void(this_ptr: *const c_void) -> *mut c_void {
285         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRouteHint)).clone() })) as *mut c_void
286 }
287 #[no_mangle]
288 pub extern "C" fn RouteHint_clone(orig: &RouteHint) -> RouteHint {
289         RouteHint { inner: Box::into_raw(Box::new(unsafe { &*orig.inner }.clone())), is_owned: true }
290 }
291 /// The node_id of the non-target end of the route
292 #[no_mangle]
293 pub extern "C" fn RouteHint_get_src_node_id(this_ptr: &RouteHint) -> crate::c_types::PublicKey {
294         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.src_node_id;
295         crate::c_types::PublicKey::from_rust(&(*inner_val))
296 }
297 /// The node_id of the non-target end of the route
298 #[no_mangle]
299 pub extern "C" fn RouteHint_set_src_node_id(this_ptr: &mut RouteHint, mut val: crate::c_types::PublicKey) {
300         unsafe { &mut *this_ptr.inner }.src_node_id = val.into_rust();
301 }
302 /// The short_channel_id of this channel
303 #[no_mangle]
304 pub extern "C" fn RouteHint_get_short_channel_id(this_ptr: &RouteHint) -> u64 {
305         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.short_channel_id;
306         (*inner_val)
307 }
308 /// The short_channel_id of this channel
309 #[no_mangle]
310 pub extern "C" fn RouteHint_set_short_channel_id(this_ptr: &mut RouteHint, mut val: u64) {
311         unsafe { &mut *this_ptr.inner }.short_channel_id = val;
312 }
313 /// The fees which must be paid to use this channel
314 #[no_mangle]
315 pub extern "C" fn RouteHint_get_fees(this_ptr: &RouteHint) -> crate::routing::network_graph::RoutingFees {
316         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.fees;
317         crate::routing::network_graph::RoutingFees { inner: unsafe { ( (&((*inner_val)) as *const _) as *mut _) }, is_owned: false }
318 }
319 /// The fees which must be paid to use this channel
320 #[no_mangle]
321 pub extern "C" fn RouteHint_set_fees(this_ptr: &mut RouteHint, mut val: crate::routing::network_graph::RoutingFees) {
322         unsafe { &mut *this_ptr.inner }.fees = *unsafe { Box::from_raw(val.take_ptr()) };
323 }
324 /// The difference in CLTV values between this node and the next node.
325 #[no_mangle]
326 pub extern "C" fn RouteHint_get_cltv_expiry_delta(this_ptr: &RouteHint) -> u16 {
327         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry_delta;
328         (*inner_val)
329 }
330 /// The difference in CLTV values between this node and the next node.
331 #[no_mangle]
332 pub extern "C" fn RouteHint_set_cltv_expiry_delta(this_ptr: &mut RouteHint, mut val: u16) {
333         unsafe { &mut *this_ptr.inner }.cltv_expiry_delta = val;
334 }
335 /// The minimum value, in msat, which must be relayed to the next hop.
336 #[no_mangle]
337 pub extern "C" fn RouteHint_get_htlc_minimum_msat(this_ptr: &RouteHint) -> u64 {
338         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_minimum_msat;
339         (*inner_val)
340 }
341 /// The minimum value, in msat, which must be relayed to the next hop.
342 #[no_mangle]
343 pub extern "C" fn RouteHint_set_htlc_minimum_msat(this_ptr: &mut RouteHint, mut val: u64) {
344         unsafe { &mut *this_ptr.inner }.htlc_minimum_msat = val;
345 }
346 #[must_use]
347 #[no_mangle]
348 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: u64) -> RouteHint {
349         RouteHint { inner: Box::into_raw(Box::new(nativeRouteHint {
350                 src_node_id: src_node_id_arg.into_rust(),
351                 short_channel_id: short_channel_id_arg,
352                 fees: *unsafe { Box::from_raw(fees_arg.take_ptr()) },
353                 cltv_expiry_delta: cltv_expiry_delta_arg,
354                 htlc_minimum_msat: htlc_minimum_msat_arg,
355         })), is_owned: true }
356 }
357 /// Gets a route from us to the given target node.
358 ///
359 /// Extra routing hops between known nodes and the target will be used if they are included in
360 /// last_hops.
361 ///
362 /// If some channels aren't announced, it may be useful to fill in a first_hops with the
363 /// results from a local ChannelManager::list_usable_channels() call. If it is filled in, our
364 /// view of our local channels (from net_graph_msg_handler) will be ignored, and only those in first_hops
365 /// will be used.
366 ///
367 /// Panics if first_hops contains channels without short_channel_ids
368 /// (ChannelManager::list_usable_channels will never include such channels).
369 ///
370 /// The fees on channels from us to next-hops are ignored (as they are assumed to all be
371 /// equal), however the enabled/disabled bit on such channels as well as the htlc_minimum_msat
372 /// *is* checked as they may change based on the receiving node.
373 #[no_mangle]
374 pub extern "C" fn get_route(mut our_node_id: crate::c_types::PublicKey, network: &crate::routing::network_graph::NetworkGraph, mut target: 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::util::logger::Logger) -> crate::c_types::derived::CResult_RouteLightningErrorZ {
375         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[..]);
376         let mut local_last_hops = Vec::new(); for mut item in last_hops.as_slice().iter() { local_last_hops.push( { unsafe { &*item.inner } }); };
377         let mut ret = lightning::routing::router::get_route(&our_node_id.into_rust(), unsafe { &*network.inner }, &target.into_rust(), local_first_hops, &local_last_hops[..], final_value_msat, final_cltv, logger);
378         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 } }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) };
379         local_ret
380 }
381