89f20fd4889487224864a0846ce22b2a70e0e6c0
[rust-lightning] / lightning-c-bindings / src / routing / network_graph.rs
1 //! The top-level network map tracking logic lives here.
2
3 use std::ffi::c_void;
4 use bitcoin::hashes::Hash;
5 use crate::c_types::*;
6
7
8 use lightning::routing::network_graph::NetworkGraph as nativeNetworkGraphImport;
9 type nativeNetworkGraph = nativeNetworkGraphImport;
10
11 /// Represents the network as nodes and channels between them
12 #[must_use]
13 #[repr(C)]
14 pub struct NetworkGraph {
15         /// Nearly everyhwere, inner must be non-null, however in places where
16         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
17         pub inner: *mut nativeNetworkGraph,
18         pub is_owned: bool,
19 }
20
21 impl Drop for NetworkGraph {
22         fn drop(&mut self) {
23                 if self.is_owned && !self.inner.is_null() {
24                         let _ = unsafe { Box::from_raw(self.inner) };
25                 }
26         }
27 }
28 #[no_mangle]
29 pub extern "C" fn NetworkGraph_free(this_ptr: NetworkGraph) { }
30 #[allow(unused)]
31 /// Used only if an object of this type is returned as a trait impl by a method
32 extern "C" fn NetworkGraph_free_void(this_ptr: *mut c_void) {
33         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeNetworkGraph); }
34 }
35 #[allow(unused)]
36 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
37 impl NetworkGraph {
38         pub(crate) fn take_ptr(mut self) -> *mut nativeNetworkGraph {
39                 assert!(self.is_owned);
40                 let ret = self.inner;
41                 self.inner = std::ptr::null_mut();
42                 ret
43         }
44 }
45
46 use lightning::routing::network_graph::LockedNetworkGraph as nativeLockedNetworkGraphImport;
47 type nativeLockedNetworkGraph = nativeLockedNetworkGraphImport<'static>;
48
49 /// A simple newtype for RwLockReadGuard<'a, NetworkGraph>.
50 /// This exists only to make accessing a RwLock<NetworkGraph> possible from
51 /// the C bindings, as it can be done directly in Rust code.
52 #[must_use]
53 #[repr(C)]
54 pub struct LockedNetworkGraph {
55         /// Nearly everyhwere, inner must be non-null, however in places where
56         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
57         pub inner: *mut nativeLockedNetworkGraph,
58         pub is_owned: bool,
59 }
60
61 impl Drop for LockedNetworkGraph {
62         fn drop(&mut self) {
63                 if self.is_owned && !self.inner.is_null() {
64                         let _ = unsafe { Box::from_raw(self.inner) };
65                 }
66         }
67 }
68 #[no_mangle]
69 pub extern "C" fn LockedNetworkGraph_free(this_ptr: LockedNetworkGraph) { }
70 #[allow(unused)]
71 /// Used only if an object of this type is returned as a trait impl by a method
72 extern "C" fn LockedNetworkGraph_free_void(this_ptr: *mut c_void) {
73         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeLockedNetworkGraph); }
74 }
75 #[allow(unused)]
76 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
77 impl LockedNetworkGraph {
78         pub(crate) fn take_ptr(mut self) -> *mut nativeLockedNetworkGraph {
79                 assert!(self.is_owned);
80                 let ret = self.inner;
81                 self.inner = std::ptr::null_mut();
82                 ret
83         }
84 }
85
86 use lightning::routing::network_graph::NetGraphMsgHandler as nativeNetGraphMsgHandlerImport;
87 type nativeNetGraphMsgHandler = nativeNetGraphMsgHandlerImport<crate::chain::chaininterface::ChainWatchInterface, crate::util::logger::Logger>;
88
89 /// Receives and validates network updates from peers,
90 /// stores authentic and relevant data as a network graph.
91 /// This network graph is then used for routing payments.
92 /// Provides interface to help with initial routing sync by
93 /// serving historical announcements.
94 #[must_use]
95 #[repr(C)]
96 pub struct NetGraphMsgHandler {
97         /// Nearly everyhwere, inner must be non-null, however in places where
98         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
99         pub inner: *mut nativeNetGraphMsgHandler,
100         pub is_owned: bool,
101 }
102
103 impl Drop for NetGraphMsgHandler {
104         fn drop(&mut self) {
105                 if self.is_owned && !self.inner.is_null() {
106                         let _ = unsafe { Box::from_raw(self.inner) };
107                 }
108         }
109 }
110 #[no_mangle]
111 pub extern "C" fn NetGraphMsgHandler_free(this_ptr: NetGraphMsgHandler) { }
112 #[allow(unused)]
113 /// Used only if an object of this type is returned as a trait impl by a method
114 extern "C" fn NetGraphMsgHandler_free_void(this_ptr: *mut c_void) {
115         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeNetGraphMsgHandler); }
116 }
117 #[allow(unused)]
118 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
119 impl NetGraphMsgHandler {
120         pub(crate) fn take_ptr(mut self) -> *mut nativeNetGraphMsgHandler {
121                 assert!(self.is_owned);
122                 let ret = self.inner;
123                 self.inner = std::ptr::null_mut();
124                 ret
125         }
126 }
127 /// Creates a new tracker of the actual state of the network of channels and nodes,
128 /// assuming a fresh network graph.
129 /// Chain monitor is used to make sure announced channels exist on-chain,
130 /// channel data is correct, and that the announcement is signed with
131 /// channel owners' keys.
132 #[must_use]
133 #[no_mangle]
134 pub extern "C" fn NetGraphMsgHandler_new(mut chain_monitor: crate::chain::chaininterface::ChainWatchInterface, mut logger: crate::util::logger::Logger) -> NetGraphMsgHandler {
135         let mut ret = lightning::routing::network_graph::NetGraphMsgHandler::new(chain_monitor, logger);
136         NetGraphMsgHandler { inner: Box::into_raw(Box::new(ret)), is_owned: true }
137 }
138
139 /// Creates a new tracker of the actual state of the network of channels and nodes,
140 /// assuming an existing Network Graph.
141 #[must_use]
142 #[no_mangle]
143 pub extern "C" fn NetGraphMsgHandler_from_net_graph(mut chain_monitor: crate::chain::chaininterface::ChainWatchInterface, mut logger: crate::util::logger::Logger, mut network_graph: crate::routing::network_graph::NetworkGraph) -> NetGraphMsgHandler {
144         let mut ret = lightning::routing::network_graph::NetGraphMsgHandler::from_net_graph(chain_monitor, logger, *unsafe { Box::from_raw(network_graph.take_ptr()) });
145         NetGraphMsgHandler { inner: Box::into_raw(Box::new(ret)), is_owned: true }
146 }
147
148 /// Take a read lock on the network_graph and return it in the C-bindings
149 /// newtype helper. This is likely only useful when called via the C
150 /// bindings as you can call `self.network_graph.read().unwrap()` in Rust
151 /// yourself.
152 #[must_use]
153 #[no_mangle]
154 pub extern "C" fn NetGraphMsgHandler_read_locked_graph(this_arg: &NetGraphMsgHandler) -> crate::routing::network_graph::LockedNetworkGraph {
155         let mut ret = unsafe { &*this_arg.inner }.read_locked_graph();
156         crate::routing::network_graph::LockedNetworkGraph { inner: Box::into_raw(Box::new(ret)), is_owned: true }
157 }
158
159 /// Get a reference to the NetworkGraph which this read-lock contains.
160 #[must_use]
161 #[no_mangle]
162 pub extern "C" fn LockedNetworkGraph_graph(this_arg: &LockedNetworkGraph) -> crate::routing::network_graph::NetworkGraph {
163         let mut ret = unsafe { &*this_arg.inner }.graph();
164         crate::routing::network_graph::NetworkGraph { inner: unsafe { ( (&(*ret) as *const _) as *mut _) }, is_owned: false }
165 }
166
167 #[no_mangle]
168 pub extern "C" fn NetGraphMsgHandler_as_RoutingMessageHandler(this_arg: *const NetGraphMsgHandler) -> crate::ln::msgs::RoutingMessageHandler {
169         crate::ln::msgs::RoutingMessageHandler {
170                 this_arg: unsafe { (*this_arg).inner as *mut c_void },
171                 free: None,
172                 handle_node_announcement: NetGraphMsgHandler_RoutingMessageHandler_handle_node_announcement,
173                 handle_channel_announcement: NetGraphMsgHandler_RoutingMessageHandler_handle_channel_announcement,
174                 handle_channel_update: NetGraphMsgHandler_RoutingMessageHandler_handle_channel_update,
175                 handle_htlc_fail_channel_update: NetGraphMsgHandler_RoutingMessageHandler_handle_htlc_fail_channel_update,
176                 get_next_channel_announcements: NetGraphMsgHandler_RoutingMessageHandler_get_next_channel_announcements,
177                 get_next_node_announcements: NetGraphMsgHandler_RoutingMessageHandler_get_next_node_announcements,
178                 should_request_full_sync: NetGraphMsgHandler_RoutingMessageHandler_should_request_full_sync,
179         }
180 }
181 use lightning::ln::msgs::RoutingMessageHandler as RoutingMessageHandlerTraitImport;
182 #[must_use]
183 extern "C" fn NetGraphMsgHandler_RoutingMessageHandler_handle_node_announcement(this_arg: *const c_void, msg: &crate::ln::msgs::NodeAnnouncement) -> crate::c_types::derived::CResult_boolLightningErrorZ {
184         let mut ret = unsafe { &mut *(this_arg as *mut nativeNetGraphMsgHandler) }.handle_node_announcement(unsafe { &*msg.inner });
185         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { o }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) };
186         local_ret
187 }
188 #[must_use]
189 extern "C" fn NetGraphMsgHandler_RoutingMessageHandler_handle_channel_announcement(this_arg: *const c_void, msg: &crate::ln::msgs::ChannelAnnouncement) -> crate::c_types::derived::CResult_boolLightningErrorZ {
190         let mut ret = unsafe { &mut *(this_arg as *mut nativeNetGraphMsgHandler) }.handle_channel_announcement(unsafe { &*msg.inner });
191         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { o }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) };
192         local_ret
193 }
194 extern "C" fn NetGraphMsgHandler_RoutingMessageHandler_handle_htlc_fail_channel_update(this_arg: *const c_void, update: &crate::ln::msgs::HTLCFailChannelUpdate) {
195         unsafe { &mut *(this_arg as *mut nativeNetGraphMsgHandler) }.handle_htlc_fail_channel_update(&update.to_native())
196 }
197 #[must_use]
198 extern "C" fn NetGraphMsgHandler_RoutingMessageHandler_handle_channel_update(this_arg: *const c_void, msg: &crate::ln::msgs::ChannelUpdate) -> crate::c_types::derived::CResult_boolLightningErrorZ {
199         let mut ret = unsafe { &mut *(this_arg as *mut nativeNetGraphMsgHandler) }.handle_channel_update(unsafe { &*msg.inner });
200         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { o }), Err(mut e) => crate::c_types::CResultTempl::err( { crate::ln::msgs::LightningError { inner: Box::into_raw(Box::new(e)), is_owned: true } }) };
201         local_ret
202 }
203 #[must_use]
204 extern "C" fn NetGraphMsgHandler_RoutingMessageHandler_get_next_channel_announcements(this_arg: *const c_void, mut starting_point: u64, mut batch_amount: u8) -> crate::c_types::derived::CVec_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ {
205         let mut ret = unsafe { &mut *(this_arg as *mut nativeNetGraphMsgHandler) }.get_next_channel_announcements(starting_point, batch_amount);
206         let mut local_ret = Vec::new(); for item in ret.drain(..) { local_ret.push( { let (mut orig_ret_0_0, mut orig_ret_0_1, mut orig_ret_0_2) = item; let mut local_orig_ret_0_1 = crate::ln::msgs::ChannelUpdate { inner: if orig_ret_0_1.is_none() { std::ptr::null_mut() } else {  { Box::into_raw(Box::new((orig_ret_0_1.unwrap()))) } }, is_owned: true }; let mut local_orig_ret_0_2 = crate::ln::msgs::ChannelUpdate { inner: if orig_ret_0_2.is_none() { std::ptr::null_mut() } else {  { Box::into_raw(Box::new((orig_ret_0_2.unwrap()))) } }, is_owned: true }; let mut local_ret_0 = (crate::ln::msgs::ChannelAnnouncement { inner: Box::into_raw(Box::new(orig_ret_0_0)), is_owned: true }, local_orig_ret_0_1, local_orig_ret_0_2).into(); local_ret_0 }); };
207         local_ret.into()
208 }
209 #[must_use]
210 extern "C" fn NetGraphMsgHandler_RoutingMessageHandler_get_next_node_announcements(this_arg: *const c_void, mut starting_point: crate::c_types::PublicKey, mut batch_amount: u8) -> crate::c_types::derived::CVec_NodeAnnouncementZ {
211         let mut local_starting_point_base = if starting_point.is_null() { None } else { Some( { starting_point.into_rust() }) }; let mut local_starting_point = local_starting_point_base.as_ref();
212         let mut ret = unsafe { &mut *(this_arg as *mut nativeNetGraphMsgHandler) }.get_next_node_announcements(local_starting_point, batch_amount);
213         let mut local_ret = Vec::new(); for item in ret.drain(..) { local_ret.push( { crate::ln::msgs::NodeAnnouncement { inner: Box::into_raw(Box::new(item)), is_owned: true } }); };
214         local_ret.into()
215 }
216 #[must_use]
217 extern "C" fn NetGraphMsgHandler_RoutingMessageHandler_should_request_full_sync(this_arg: *const c_void, _node_id: crate::c_types::PublicKey) -> bool {
218         let mut ret = unsafe { &mut *(this_arg as *mut nativeNetGraphMsgHandler) }.should_request_full_sync(&_node_id.into_rust());
219         ret
220 }
221
222
223 use lightning::routing::network_graph::DirectionalChannelInfo as nativeDirectionalChannelInfoImport;
224 type nativeDirectionalChannelInfo = nativeDirectionalChannelInfoImport;
225
226 /// Details about one direction of a channel. Received
227 /// within a channel update.
228 #[must_use]
229 #[repr(C)]
230 pub struct DirectionalChannelInfo {
231         /// Nearly everyhwere, inner must be non-null, however in places where
232         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
233         pub inner: *mut nativeDirectionalChannelInfo,
234         pub is_owned: bool,
235 }
236
237 impl Drop for DirectionalChannelInfo {
238         fn drop(&mut self) {
239                 if self.is_owned && !self.inner.is_null() {
240                         let _ = unsafe { Box::from_raw(self.inner) };
241                 }
242         }
243 }
244 #[no_mangle]
245 pub extern "C" fn DirectionalChannelInfo_free(this_ptr: DirectionalChannelInfo) { }
246 #[allow(unused)]
247 /// Used only if an object of this type is returned as a trait impl by a method
248 extern "C" fn DirectionalChannelInfo_free_void(this_ptr: *mut c_void) {
249         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeDirectionalChannelInfo); }
250 }
251 #[allow(unused)]
252 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
253 impl DirectionalChannelInfo {
254         pub(crate) fn take_ptr(mut self) -> *mut nativeDirectionalChannelInfo {
255                 assert!(self.is_owned);
256                 let ret = self.inner;
257                 self.inner = std::ptr::null_mut();
258                 ret
259         }
260 }
261 /// When the last update to the channel direction was issued.
262 /// Value is opaque, as set in the announcement.
263 #[no_mangle]
264 pub extern "C" fn DirectionalChannelInfo_get_last_update(this_ptr: &DirectionalChannelInfo) -> u32 {
265         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.last_update;
266         (*inner_val)
267 }
268 /// When the last update to the channel direction was issued.
269 /// Value is opaque, as set in the announcement.
270 #[no_mangle]
271 pub extern "C" fn DirectionalChannelInfo_set_last_update(this_ptr: &mut DirectionalChannelInfo, mut val: u32) {
272         unsafe { &mut *this_ptr.inner }.last_update = val;
273 }
274 /// Whether the channel can be currently used for payments (in this one direction).
275 #[no_mangle]
276 pub extern "C" fn DirectionalChannelInfo_get_enabled(this_ptr: &DirectionalChannelInfo) -> bool {
277         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.enabled;
278         (*inner_val)
279 }
280 /// Whether the channel can be currently used for payments (in this one direction).
281 #[no_mangle]
282 pub extern "C" fn DirectionalChannelInfo_set_enabled(this_ptr: &mut DirectionalChannelInfo, mut val: bool) {
283         unsafe { &mut *this_ptr.inner }.enabled = val;
284 }
285 /// The difference in CLTV values that you must have when routing through this channel.
286 #[no_mangle]
287 pub extern "C" fn DirectionalChannelInfo_get_cltv_expiry_delta(this_ptr: &DirectionalChannelInfo) -> u16 {
288         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.cltv_expiry_delta;
289         (*inner_val)
290 }
291 /// The difference in CLTV values that you must have when routing through this channel.
292 #[no_mangle]
293 pub extern "C" fn DirectionalChannelInfo_set_cltv_expiry_delta(this_ptr: &mut DirectionalChannelInfo, mut val: u16) {
294         unsafe { &mut *this_ptr.inner }.cltv_expiry_delta = val;
295 }
296 /// The minimum value, which must be relayed to the next hop via the channel
297 #[no_mangle]
298 pub extern "C" fn DirectionalChannelInfo_get_htlc_minimum_msat(this_ptr: &DirectionalChannelInfo) -> u64 {
299         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.htlc_minimum_msat;
300         (*inner_val)
301 }
302 /// The minimum value, which must be relayed to the next hop via the channel
303 #[no_mangle]
304 pub extern "C" fn DirectionalChannelInfo_set_htlc_minimum_msat(this_ptr: &mut DirectionalChannelInfo, mut val: u64) {
305         unsafe { &mut *this_ptr.inner }.htlc_minimum_msat = val;
306 }
307 /// Most recent update for the channel received from the network
308 /// Mostly redundant with the data we store in fields explicitly.
309 /// Everything else is useful only for sending out for initial routing sync.
310 /// Not stored if contains excess data to prevent DoS.
311 #[no_mangle]
312 pub extern "C" fn DirectionalChannelInfo_get_last_update_message(this_ptr: &DirectionalChannelInfo) -> crate::ln::msgs::ChannelUpdate {
313         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.last_update_message;
314         let mut local_inner_val = crate::ln::msgs::ChannelUpdate { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else {  { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false };
315         local_inner_val
316 }
317 /// Most recent update for the channel received from the network
318 /// Mostly redundant with the data we store in fields explicitly.
319 /// Everything else is useful only for sending out for initial routing sync.
320 /// Not stored if contains excess data to prevent DoS.
321 #[no_mangle]
322 pub extern "C" fn DirectionalChannelInfo_set_last_update_message(this_ptr: &mut DirectionalChannelInfo, mut val: crate::ln::msgs::ChannelUpdate) {
323         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_ptr()) } }) };
324         unsafe { &mut *this_ptr.inner }.last_update_message = local_val;
325 }
326 #[no_mangle]
327 pub extern "C" fn DirectionalChannelInfo_write(obj: *const DirectionalChannelInfo) -> crate::c_types::derived::CVec_u8Z {
328         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
329 }
330 #[no_mangle]
331 pub extern "C" fn DirectionalChannelInfo_read(ser: crate::c_types::u8slice) -> DirectionalChannelInfo {
332         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
333                 DirectionalChannelInfo { inner: Box::into_raw(Box::new(res)), is_owned: true }
334         } else {
335                 DirectionalChannelInfo { inner: std::ptr::null_mut(), is_owned: true }
336         }
337 }
338
339 use lightning::routing::network_graph::ChannelInfo as nativeChannelInfoImport;
340 type nativeChannelInfo = nativeChannelInfoImport;
341
342 /// Details about a channel (both directions).
343 /// Received within a channel announcement.
344 #[must_use]
345 #[repr(C)]
346 pub struct ChannelInfo {
347         /// Nearly everyhwere, inner must be non-null, however in places where
348         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
349         pub inner: *mut nativeChannelInfo,
350         pub is_owned: bool,
351 }
352
353 impl Drop for ChannelInfo {
354         fn drop(&mut self) {
355                 if self.is_owned && !self.inner.is_null() {
356                         let _ = unsafe { Box::from_raw(self.inner) };
357                 }
358         }
359 }
360 #[no_mangle]
361 pub extern "C" fn ChannelInfo_free(this_ptr: ChannelInfo) { }
362 #[allow(unused)]
363 /// Used only if an object of this type is returned as a trait impl by a method
364 extern "C" fn ChannelInfo_free_void(this_ptr: *mut c_void) {
365         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeChannelInfo); }
366 }
367 #[allow(unused)]
368 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
369 impl ChannelInfo {
370         pub(crate) fn take_ptr(mut self) -> *mut nativeChannelInfo {
371                 assert!(self.is_owned);
372                 let ret = self.inner;
373                 self.inner = std::ptr::null_mut();
374                 ret
375         }
376 }
377 /// Source node of the first direction of a channel
378 #[no_mangle]
379 pub extern "C" fn ChannelInfo_get_node_one(this_ptr: &ChannelInfo) -> crate::c_types::PublicKey {
380         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.node_one;
381         crate::c_types::PublicKey::from_rust(&(*inner_val))
382 }
383 /// Source node of the first direction of a channel
384 #[no_mangle]
385 pub extern "C" fn ChannelInfo_set_node_one(this_ptr: &mut ChannelInfo, mut val: crate::c_types::PublicKey) {
386         unsafe { &mut *this_ptr.inner }.node_one = val.into_rust();
387 }
388 /// Details about the first direction of a channel
389 #[no_mangle]
390 pub extern "C" fn ChannelInfo_get_one_to_two(this_ptr: &ChannelInfo) -> crate::routing::network_graph::DirectionalChannelInfo {
391         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.one_to_two;
392         let mut local_inner_val = crate::routing::network_graph::DirectionalChannelInfo { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else {  { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false };
393         local_inner_val
394 }
395 /// Details about the first direction of a channel
396 #[no_mangle]
397 pub extern "C" fn ChannelInfo_set_one_to_two(this_ptr: &mut ChannelInfo, mut val: crate::routing::network_graph::DirectionalChannelInfo) {
398         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_ptr()) } }) };
399         unsafe { &mut *this_ptr.inner }.one_to_two = local_val;
400 }
401 /// Source node of the second direction of a channel
402 #[no_mangle]
403 pub extern "C" fn ChannelInfo_get_node_two(this_ptr: &ChannelInfo) -> crate::c_types::PublicKey {
404         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.node_two;
405         crate::c_types::PublicKey::from_rust(&(*inner_val))
406 }
407 /// Source node of the second direction of a channel
408 #[no_mangle]
409 pub extern "C" fn ChannelInfo_set_node_two(this_ptr: &mut ChannelInfo, mut val: crate::c_types::PublicKey) {
410         unsafe { &mut *this_ptr.inner }.node_two = val.into_rust();
411 }
412 /// Details about the second direction of a channel
413 #[no_mangle]
414 pub extern "C" fn ChannelInfo_get_two_to_one(this_ptr: &ChannelInfo) -> crate::routing::network_graph::DirectionalChannelInfo {
415         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.two_to_one;
416         let mut local_inner_val = crate::routing::network_graph::DirectionalChannelInfo { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else {  { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false };
417         local_inner_val
418 }
419 /// Details about the second direction of a channel
420 #[no_mangle]
421 pub extern "C" fn ChannelInfo_set_two_to_one(this_ptr: &mut ChannelInfo, mut val: crate::routing::network_graph::DirectionalChannelInfo) {
422         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_ptr()) } }) };
423         unsafe { &mut *this_ptr.inner }.two_to_one = local_val;
424 }
425 /// An initial announcement of the channel
426 /// Mostly redundant with the data we store in fields explicitly.
427 /// Everything else is useful only for sending out for initial routing sync.
428 /// Not stored if contains excess data to prevent DoS.
429 #[no_mangle]
430 pub extern "C" fn ChannelInfo_get_announcement_message(this_ptr: &ChannelInfo) -> crate::ln::msgs::ChannelAnnouncement {
431         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.announcement_message;
432         let mut local_inner_val = crate::ln::msgs::ChannelAnnouncement { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else {  { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false };
433         local_inner_val
434 }
435 /// An initial announcement of the channel
436 /// Mostly redundant with the data we store in fields explicitly.
437 /// Everything else is useful only for sending out for initial routing sync.
438 /// Not stored if contains excess data to prevent DoS.
439 #[no_mangle]
440 pub extern "C" fn ChannelInfo_set_announcement_message(this_ptr: &mut ChannelInfo, mut val: crate::ln::msgs::ChannelAnnouncement) {
441         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_ptr()) } }) };
442         unsafe { &mut *this_ptr.inner }.announcement_message = local_val;
443 }
444 #[no_mangle]
445 pub extern "C" fn ChannelInfo_write(obj: *const ChannelInfo) -> crate::c_types::derived::CVec_u8Z {
446         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
447 }
448 #[no_mangle]
449 pub extern "C" fn ChannelInfo_read(ser: crate::c_types::u8slice) -> ChannelInfo {
450         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
451                 ChannelInfo { inner: Box::into_raw(Box::new(res)), is_owned: true }
452         } else {
453                 ChannelInfo { inner: std::ptr::null_mut(), is_owned: true }
454         }
455 }
456
457 use lightning::routing::network_graph::RoutingFees as nativeRoutingFeesImport;
458 type nativeRoutingFees = nativeRoutingFeesImport;
459
460 /// Fees for routing via a given channel or a node
461 #[must_use]
462 #[repr(C)]
463 pub struct RoutingFees {
464         /// Nearly everyhwere, inner must be non-null, however in places where
465         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
466         pub inner: *mut nativeRoutingFees,
467         pub is_owned: bool,
468 }
469
470 impl Drop for RoutingFees {
471         fn drop(&mut self) {
472                 if self.is_owned && !self.inner.is_null() {
473                         let _ = unsafe { Box::from_raw(self.inner) };
474                 }
475         }
476 }
477 #[no_mangle]
478 pub extern "C" fn RoutingFees_free(this_ptr: RoutingFees) { }
479 #[allow(unused)]
480 /// Used only if an object of this type is returned as a trait impl by a method
481 extern "C" fn RoutingFees_free_void(this_ptr: *mut c_void) {
482         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeRoutingFees); }
483 }
484 #[allow(unused)]
485 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
486 impl RoutingFees {
487         pub(crate) fn take_ptr(mut self) -> *mut nativeRoutingFees {
488                 assert!(self.is_owned);
489                 let ret = self.inner;
490                 self.inner = std::ptr::null_mut();
491                 ret
492         }
493 }
494 impl Clone for RoutingFees {
495         fn clone(&self) -> Self {
496                 Self {
497                         inner: Box::into_raw(Box::new(unsafe { &*self.inner }.clone())),
498                         is_owned: true,
499                 }
500         }
501 }
502 #[allow(unused)]
503 /// Used only if an object of this type is returned as a trait impl by a method
504 pub(crate) extern "C" fn RoutingFees_clone_void(this_ptr: *const c_void) -> *mut c_void {
505         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeRoutingFees)).clone() })) as *mut c_void
506 }
507 /// Flat routing fee in satoshis
508 #[no_mangle]
509 pub extern "C" fn RoutingFees_get_base_msat(this_ptr: &RoutingFees) -> u32 {
510         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.base_msat;
511         (*inner_val)
512 }
513 /// Flat routing fee in satoshis
514 #[no_mangle]
515 pub extern "C" fn RoutingFees_set_base_msat(this_ptr: &mut RoutingFees, mut val: u32) {
516         unsafe { &mut *this_ptr.inner }.base_msat = val;
517 }
518 /// Liquidity-based routing fee in millionths of a routed amount.
519 /// In other words, 10000 is 1%.
520 #[no_mangle]
521 pub extern "C" fn RoutingFees_get_proportional_millionths(this_ptr: &RoutingFees) -> u32 {
522         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.proportional_millionths;
523         (*inner_val)
524 }
525 /// Liquidity-based routing fee in millionths of a routed amount.
526 /// In other words, 10000 is 1%.
527 #[no_mangle]
528 pub extern "C" fn RoutingFees_set_proportional_millionths(this_ptr: &mut RoutingFees, mut val: u32) {
529         unsafe { &mut *this_ptr.inner }.proportional_millionths = val;
530 }
531 #[must_use]
532 #[no_mangle]
533 pub extern "C" fn RoutingFees_new(mut base_msat_arg: u32, mut proportional_millionths_arg: u32) -> RoutingFees {
534         RoutingFees { inner: Box::into_raw(Box::new(nativeRoutingFees {
535                 base_msat: base_msat_arg,
536                 proportional_millionths: proportional_millionths_arg,
537         })), is_owned: true }
538 }
539 #[no_mangle]
540 pub extern "C" fn RoutingFees_read(ser: crate::c_types::u8slice) -> RoutingFees {
541         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
542                 RoutingFees { inner: Box::into_raw(Box::new(res)), is_owned: true }
543         } else {
544                 RoutingFees { inner: std::ptr::null_mut(), is_owned: true }
545         }
546 }
547 #[no_mangle]
548 pub extern "C" fn RoutingFees_write(obj: *const RoutingFees) -> crate::c_types::derived::CVec_u8Z {
549         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
550 }
551
552 use lightning::routing::network_graph::NodeAnnouncementInfo as nativeNodeAnnouncementInfoImport;
553 type nativeNodeAnnouncementInfo = nativeNodeAnnouncementInfoImport;
554
555 /// Information received in the latest node_announcement from this node.
556 #[must_use]
557 #[repr(C)]
558 pub struct NodeAnnouncementInfo {
559         /// Nearly everyhwere, inner must be non-null, however in places where
560         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
561         pub inner: *mut nativeNodeAnnouncementInfo,
562         pub is_owned: bool,
563 }
564
565 impl Drop for NodeAnnouncementInfo {
566         fn drop(&mut self) {
567                 if self.is_owned && !self.inner.is_null() {
568                         let _ = unsafe { Box::from_raw(self.inner) };
569                 }
570         }
571 }
572 #[no_mangle]
573 pub extern "C" fn NodeAnnouncementInfo_free(this_ptr: NodeAnnouncementInfo) { }
574 #[allow(unused)]
575 /// Used only if an object of this type is returned as a trait impl by a method
576 extern "C" fn NodeAnnouncementInfo_free_void(this_ptr: *mut c_void) {
577         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeNodeAnnouncementInfo); }
578 }
579 #[allow(unused)]
580 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
581 impl NodeAnnouncementInfo {
582         pub(crate) fn take_ptr(mut self) -> *mut nativeNodeAnnouncementInfo {
583                 assert!(self.is_owned);
584                 let ret = self.inner;
585                 self.inner = std::ptr::null_mut();
586                 ret
587         }
588 }
589 /// When the last known update to the node state was issued.
590 /// Value is opaque, as set in the announcement.
591 #[no_mangle]
592 pub extern "C" fn NodeAnnouncementInfo_get_last_update(this_ptr: &NodeAnnouncementInfo) -> u32 {
593         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.last_update;
594         (*inner_val)
595 }
596 /// When the last known update to the node state was issued.
597 /// Value is opaque, as set in the announcement.
598 #[no_mangle]
599 pub extern "C" fn NodeAnnouncementInfo_set_last_update(this_ptr: &mut NodeAnnouncementInfo, mut val: u32) {
600         unsafe { &mut *this_ptr.inner }.last_update = val;
601 }
602 /// Color assigned to the node
603 #[no_mangle]
604 pub extern "C" fn NodeAnnouncementInfo_get_rgb(this_ptr: &NodeAnnouncementInfo) -> *const [u8; 3] {
605         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.rgb;
606         &(*inner_val)
607 }
608 /// Color assigned to the node
609 #[no_mangle]
610 pub extern "C" fn NodeAnnouncementInfo_set_rgb(this_ptr: &mut NodeAnnouncementInfo, mut val: crate::c_types::ThreeBytes) {
611         unsafe { &mut *this_ptr.inner }.rgb = val.data;
612 }
613 /// Moniker assigned to the node.
614 /// May be invalid or malicious (eg control chars),
615 /// should not be exposed to the user.
616 #[no_mangle]
617 pub extern "C" fn NodeAnnouncementInfo_get_alias(this_ptr: &NodeAnnouncementInfo) -> *const [u8; 32] {
618         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.alias;
619         &(*inner_val)
620 }
621 /// Moniker assigned to the node.
622 /// May be invalid or malicious (eg control chars),
623 /// should not be exposed to the user.
624 #[no_mangle]
625 pub extern "C" fn NodeAnnouncementInfo_set_alias(this_ptr: &mut NodeAnnouncementInfo, mut val: crate::c_types::ThirtyTwoBytes) {
626         unsafe { &mut *this_ptr.inner }.alias = val.data;
627 }
628 /// Internet-level addresses via which one can connect to the node
629 #[no_mangle]
630 pub extern "C" fn NodeAnnouncementInfo_set_addresses(this_ptr: &mut NodeAnnouncementInfo, mut val: crate::c_types::derived::CVec_NetAddressZ) {
631         let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item.into_native() }); };
632         unsafe { &mut *this_ptr.inner }.addresses = local_val;
633 }
634 /// An initial announcement of the node
635 /// Mostly redundant with the data we store in fields explicitly.
636 /// Everything else is useful only for sending out for initial routing sync.
637 /// Not stored if contains excess data to prevent DoS.
638 #[no_mangle]
639 pub extern "C" fn NodeAnnouncementInfo_get_announcement_message(this_ptr: &NodeAnnouncementInfo) -> crate::ln::msgs::NodeAnnouncement {
640         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.announcement_message;
641         let mut local_inner_val = crate::ln::msgs::NodeAnnouncement { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else {  { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false };
642         local_inner_val
643 }
644 /// An initial announcement of the node
645 /// Mostly redundant with the data we store in fields explicitly.
646 /// Everything else is useful only for sending out for initial routing sync.
647 /// Not stored if contains excess data to prevent DoS.
648 #[no_mangle]
649 pub extern "C" fn NodeAnnouncementInfo_set_announcement_message(this_ptr: &mut NodeAnnouncementInfo, mut val: crate::ln::msgs::NodeAnnouncement) {
650         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_ptr()) } }) };
651         unsafe { &mut *this_ptr.inner }.announcement_message = local_val;
652 }
653 #[no_mangle]
654 pub extern "C" fn NodeAnnouncementInfo_write(obj: *const NodeAnnouncementInfo) -> crate::c_types::derived::CVec_u8Z {
655         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
656 }
657 #[no_mangle]
658 pub extern "C" fn NodeAnnouncementInfo_read(ser: crate::c_types::u8slice) -> NodeAnnouncementInfo {
659         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
660                 NodeAnnouncementInfo { inner: Box::into_raw(Box::new(res)), is_owned: true }
661         } else {
662                 NodeAnnouncementInfo { inner: std::ptr::null_mut(), is_owned: true }
663         }
664 }
665
666 use lightning::routing::network_graph::NodeInfo as nativeNodeInfoImport;
667 type nativeNodeInfo = nativeNodeInfoImport;
668
669 /// Details about a node in the network, known from the network announcement.
670 #[must_use]
671 #[repr(C)]
672 pub struct NodeInfo {
673         /// Nearly everyhwere, inner must be non-null, however in places where
674         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
675         pub inner: *mut nativeNodeInfo,
676         pub is_owned: bool,
677 }
678
679 impl Drop for NodeInfo {
680         fn drop(&mut self) {
681                 if self.is_owned && !self.inner.is_null() {
682                         let _ = unsafe { Box::from_raw(self.inner) };
683                 }
684         }
685 }
686 #[no_mangle]
687 pub extern "C" fn NodeInfo_free(this_ptr: NodeInfo) { }
688 #[allow(unused)]
689 /// Used only if an object of this type is returned as a trait impl by a method
690 extern "C" fn NodeInfo_free_void(this_ptr: *mut c_void) {
691         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeNodeInfo); }
692 }
693 #[allow(unused)]
694 /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
695 impl NodeInfo {
696         pub(crate) fn take_ptr(mut self) -> *mut nativeNodeInfo {
697                 assert!(self.is_owned);
698                 let ret = self.inner;
699                 self.inner = std::ptr::null_mut();
700                 ret
701         }
702 }
703 /// All valid channels a node has announced
704 #[no_mangle]
705 pub extern "C" fn NodeInfo_set_channels(this_ptr: &mut NodeInfo, mut val: crate::c_types::derived::CVec_u64Z) {
706         let mut local_val = Vec::new(); for mut item in val.into_rust().drain(..) { local_val.push( { item }); };
707         unsafe { &mut *this_ptr.inner }.channels = local_val;
708 }
709 /// Lowest fees enabling routing via any of the enabled, known channels to a node.
710 /// The two fields (flat and proportional fee) are independent,
711 /// meaning they don't have to refer to the same channel.
712 #[no_mangle]
713 pub extern "C" fn NodeInfo_get_lowest_inbound_channel_fees(this_ptr: &NodeInfo) -> crate::routing::network_graph::RoutingFees {
714         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.lowest_inbound_channel_fees;
715         let mut local_inner_val = crate::routing::network_graph::RoutingFees { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else {  { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false };
716         local_inner_val
717 }
718 /// Lowest fees enabling routing via any of the enabled, known channels to a node.
719 /// The two fields (flat and proportional fee) are independent,
720 /// meaning they don't have to refer to the same channel.
721 #[no_mangle]
722 pub extern "C" fn NodeInfo_set_lowest_inbound_channel_fees(this_ptr: &mut NodeInfo, mut val: crate::routing::network_graph::RoutingFees) {
723         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_ptr()) } }) };
724         unsafe { &mut *this_ptr.inner }.lowest_inbound_channel_fees = local_val;
725 }
726 /// More information about a node from node_announcement.
727 /// Optional because we store a Node entry after learning about it from
728 /// a channel announcement, but before receiving a node announcement.
729 #[no_mangle]
730 pub extern "C" fn NodeInfo_get_announcement_info(this_ptr: &NodeInfo) -> crate::routing::network_graph::NodeAnnouncementInfo {
731         let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.announcement_info;
732         let mut local_inner_val = crate::routing::network_graph::NodeAnnouncementInfo { inner: unsafe { (if inner_val.is_none() { std::ptr::null() } else {  { (inner_val.as_ref().unwrap()) } } as *const _) as *mut _ }, is_owned: false };
733         local_inner_val
734 }
735 /// More information about a node from node_announcement.
736 /// Optional because we store a Node entry after learning about it from
737 /// a channel announcement, but before receiving a node announcement.
738 #[no_mangle]
739 pub extern "C" fn NodeInfo_set_announcement_info(this_ptr: &mut NodeInfo, mut val: crate::routing::network_graph::NodeAnnouncementInfo) {
740         let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_ptr()) } }) };
741         unsafe { &mut *this_ptr.inner }.announcement_info = local_val;
742 }
743 #[must_use]
744 #[no_mangle]
745 pub extern "C" fn NodeInfo_new(mut channels_arg: crate::c_types::derived::CVec_u64Z, mut lowest_inbound_channel_fees_arg: crate::routing::network_graph::RoutingFees, mut announcement_info_arg: crate::routing::network_graph::NodeAnnouncementInfo) -> NodeInfo {
746         let mut local_channels_arg = Vec::new(); for mut item in channels_arg.into_rust().drain(..) { local_channels_arg.push( { item }); };
747         let mut local_lowest_inbound_channel_fees_arg = if lowest_inbound_channel_fees_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(lowest_inbound_channel_fees_arg.take_ptr()) } }) };
748         let mut local_announcement_info_arg = if announcement_info_arg.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(announcement_info_arg.take_ptr()) } }) };
749         NodeInfo { inner: Box::into_raw(Box::new(nativeNodeInfo {
750                 channels: local_channels_arg,
751                 lowest_inbound_channel_fees: local_lowest_inbound_channel_fees_arg,
752                 announcement_info: local_announcement_info_arg,
753         })), is_owned: true }
754 }
755 #[no_mangle]
756 pub extern "C" fn NodeInfo_write(obj: *const NodeInfo) -> crate::c_types::derived::CVec_u8Z {
757         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
758 }
759 #[no_mangle]
760 pub extern "C" fn NodeInfo_read(ser: crate::c_types::u8slice) -> NodeInfo {
761         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
762                 NodeInfo { inner: Box::into_raw(Box::new(res)), is_owned: true }
763         } else {
764                 NodeInfo { inner: std::ptr::null_mut(), is_owned: true }
765         }
766 }
767 #[no_mangle]
768 pub extern "C" fn NetworkGraph_write(obj: *const NetworkGraph) -> crate::c_types::derived::CVec_u8Z {
769         crate::c_types::serialize_obj(unsafe { &(*(*obj).inner) })
770 }
771 #[no_mangle]
772 pub extern "C" fn NetworkGraph_read(ser: crate::c_types::u8slice) -> NetworkGraph {
773         if let Ok(res) = crate::c_types::deserialize_obj(ser) {
774                 NetworkGraph { inner: Box::into_raw(Box::new(res)), is_owned: true }
775         } else {
776                 NetworkGraph { inner: std::ptr::null_mut(), is_owned: true }
777         }
778 }
779 /// Creates a new, empty, network graph.
780 #[must_use]
781 #[no_mangle]
782 pub extern "C" fn NetworkGraph_new() -> crate::routing::network_graph::NetworkGraph {
783         let mut ret = lightning::routing::network_graph::NetworkGraph::new();
784         crate::routing::network_graph::NetworkGraph { inner: Box::into_raw(Box::new(ret)), is_owned: true }
785 }
786
787 /// Close a channel if a corresponding HTLC fail was sent.
788 /// If permanent, removes a channel from the local storage.
789 /// May cause the removal of nodes too, if this was their last channel.
790 /// If not permanent, makes channels unavailable for routing.
791 #[no_mangle]
792 pub extern "C" fn NetworkGraph_close_channel_from_update(this_arg: &mut NetworkGraph, mut short_channel_id: u64, mut is_permanent: bool) {
793         unsafe { &mut (*(this_arg.inner as *mut nativeNetworkGraph)) }.close_channel_from_update(short_channel_id, is_permanent)
794 }
795