Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / routing / mod.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
8
9 //! Structs and impls for receiving messages about the network and storing the topology live here.
10
11 use std::str::FromStr;
12 use std::ffi::c_void;
13 use core::convert::Infallible;
14 use bitcoin::hashes::Hash;
15 use crate::c_types::*;
16
17 pub mod network_graph;
18 pub mod router;
19 pub mod scorer;
20 /// An interface used to score payment channels for path finding.
21 ///
22 ///\tScoring is in terms of fees willing to be paid in order to avoid routing through a channel.
23 #[repr(C)]
24 pub struct Score {
25         /// An opaque pointer which is passed to your function implementations as an argument.
26         /// This has no meaning in the LDK, and can be NULL or any other value.
27         pub this_arg: *mut c_void,
28         /// Returns the fee in msats willing to be paid to avoid routing through the given channel
29         /// in the direction from `source` to `target`.
30         #[must_use]
31         pub channel_penalty_msat: extern "C" fn (this_arg: *const c_void, short_channel_id: u64, source: &crate::lightning::routing::network_graph::NodeId, target: &crate::lightning::routing::network_graph::NodeId) -> u64,
32         /// Handles updating channel penalties after failing to route through a channel.
33         pub payment_path_failed: extern "C" fn (this_arg: *mut c_void, path: crate::c_types::derived::CVec_RouteHopZ, short_channel_id: u64),
34         /// Frees any resources associated with this object given its this_arg pointer.
35         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
36         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
37 }
38 unsafe impl Send for Score {}
39 unsafe impl Sync for Score {}
40 #[no_mangle]
41 pub(crate) extern "C" fn Score_clone_fields(orig: &Score) -> Score {
42         Score {
43                 this_arg: orig.this_arg,
44                 channel_penalty_msat: Clone::clone(&orig.channel_penalty_msat),
45                 payment_path_failed: Clone::clone(&orig.payment_path_failed),
46                 free: Clone::clone(&orig.free),
47         }
48 }
49
50 use lightning::routing::Score as rustScore;
51 impl rustScore for Score {
52         fn channel_penalty_msat(&self, mut short_channel_id: u64, mut source: &lightning::routing::network_graph::NodeId, mut target: &lightning::routing::network_graph::NodeId) -> u64 {
53                 let mut ret = (self.channel_penalty_msat)(self.this_arg, short_channel_id, &crate::lightning::routing::network_graph::NodeId { inner: unsafe { ObjOps::nonnull_ptr_to_inner((source as *const lightning::routing::network_graph::NodeId<>) as *mut _) }, is_owned: false }, &crate::lightning::routing::network_graph::NodeId { inner: unsafe { ObjOps::nonnull_ptr_to_inner((target as *const lightning::routing::network_graph::NodeId<>) as *mut _) }, is_owned: false });
54                 ret
55         }
56         fn payment_path_failed(&mut self, mut path: &[&lightning::routing::router::RouteHop], mut short_channel_id: u64) {
57                 let mut local_path = Vec::new(); for item in path.iter() { local_path.push( { crate::lightning::routing::router::RouteHop { inner: unsafe { ObjOps::nonnull_ptr_to_inner(((*item) as *const lightning::routing::router::RouteHop<>) as *mut _) }, is_owned: false } }); };
58                 (self.payment_path_failed)(self.this_arg, local_path.into(), short_channel_id)
59         }
60 }
61
62 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
63 // directly as a Deref trait in higher-level structs:
64 impl std::ops::Deref for Score {
65         type Target = Self;
66         fn deref(&self) -> &Self {
67                 self
68         }
69 }
70 /// Calls the free function if one is set
71 #[no_mangle]
72 pub extern "C" fn Score_free(this_ptr: Score) { }
73 impl Drop for Score {
74         fn drop(&mut self) {
75                 if let Some(f) = self.free {
76                         f(self.this_arg);
77                 }
78         }
79 }
80
81 use lightning::routing::LockableScore as nativeLockableScoreImport;
82 pub(crate) type nativeLockableScore = nativeLockableScoreImport<crate::lightning::routing::Score>;
83
84 /// A scorer that is accessed under a lock.
85 ///
86 /// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
87 /// having shared ownership of a scorer but without requiring internal locking in [`Score`]
88 /// implementations. Internal locking would be detrimental to route finding performance and could
89 /// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
90 ///
91 /// [`find_route`]: crate::routing::router::find_route
92 #[must_use]
93 #[repr(C)]
94 pub struct LockableScore {
95         /// A pointer to the opaque Rust object.
96
97         /// Nearly everywhere, 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 nativeLockableScore,
100         /// Indicates that this is the only struct which contains the same pointer.
101
102         /// Rust functions which take ownership of an object provided via an argument require
103         /// this to be true and invalidate the object pointed to by inner.
104         pub is_owned: bool,
105 }
106
107 impl Drop for LockableScore {
108         fn drop(&mut self) {
109                 if self.is_owned && !<*mut nativeLockableScore>::is_null(self.inner) {
110                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
111                 }
112         }
113 }
114 /// Frees any resources used by the LockableScore, if is_owned is set and inner is non-NULL.
115 #[no_mangle]
116 pub extern "C" fn LockableScore_free(this_obj: LockableScore) { }
117 #[allow(unused)]
118 /// Used only if an object of this type is returned as a trait impl by a method
119 pub(crate) extern "C" fn LockableScore_free_void(this_ptr: *mut c_void) {
120         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeLockableScore); }
121 }
122 #[allow(unused)]
123 impl LockableScore {
124         pub(crate) fn get_native_ref(&self) -> &'static nativeLockableScore {
125                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
126         }
127         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeLockableScore {
128                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
129         }
130         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
131         pub(crate) fn take_inner(mut self) -> *mut nativeLockableScore {
132                 assert!(self.is_owned);
133                 let ret = ObjOps::untweak_ptr(self.inner);
134                 self.inner = std::ptr::null_mut();
135                 ret
136         }
137 }
138 /// Constructs a new LockableScore from a Score
139 #[must_use]
140 #[no_mangle]
141 pub extern "C" fn LockableScore_new(mut score: crate::lightning::routing::Score) -> LockableScore {
142         let mut ret = lightning::routing::LockableScore::new(score);
143         LockableScore { inner: ObjOps::heap_alloc(ret), is_owned: true }
144 }
145