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         /// Serialize the object into a byte array
35         pub write: extern "C" fn (this_arg: *const c_void) -> crate::c_types::derived::CVec_u8Z,
36         /// Frees any resources associated with this object given its this_arg pointer.
37         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
38         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
39 }
40 unsafe impl Send for Score {}
41 unsafe impl Sync for Score {}
42 #[no_mangle]
43 pub(crate) extern "C" fn Score_clone_fields(orig: &Score) -> Score {
44         Score {
45                 this_arg: orig.this_arg,
46                 channel_penalty_msat: Clone::clone(&orig.channel_penalty_msat),
47                 payment_path_failed: Clone::clone(&orig.payment_path_failed),
48                 write: Clone::clone(&orig.write),
49                 free: Clone::clone(&orig.free),
50         }
51 }
52 impl lightning::util::ser::Writeable for Score {
53         fn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
54                 let vec = (self.write)(self.this_arg);
55                 w.write_all(vec.as_slice())
56         }
57 }
58
59 use lightning::routing::Score as rustScore;
60 impl rustScore for Score {
61         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 {
62                 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 });
63                 ret
64         }
65         fn payment_path_failed(&mut self, mut path: &[&lightning::routing::router::RouteHop], mut short_channel_id: u64) {
66                 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 } }); };
67                 (self.payment_path_failed)(self.this_arg, local_path.into(), short_channel_id)
68         }
69 }
70
71 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
72 // directly as a Deref trait in higher-level structs:
73 impl std::ops::Deref for Score {
74         type Target = Self;
75         fn deref(&self) -> &Self {
76                 self
77         }
78 }
79 /// Calls the free function if one is set
80 #[no_mangle]
81 pub extern "C" fn Score_free(this_ptr: Score) { }
82 impl Drop for Score {
83         fn drop(&mut self) {
84                 if let Some(f) = self.free {
85                         f(self.this_arg);
86                 }
87         }
88 }
89
90 use lightning::routing::LockableScore as nativeLockableScoreImport;
91 pub(crate) type nativeLockableScore = nativeLockableScoreImport<crate::lightning::routing::Score>;
92
93 /// A scorer that is accessed under a lock.
94 ///
95 /// Needed so that calls to [`Score::channel_penalty_msat`] in [`find_route`] can be made while
96 /// having shared ownership of a scorer but without requiring internal locking in [`Score`]
97 /// implementations. Internal locking would be detrimental to route finding performance and could
98 /// result in [`Score::channel_penalty_msat`] returning a different value for the same channel.
99 ///
100 /// [`find_route`]: crate::routing::router::find_route
101 #[must_use]
102 #[repr(C)]
103 pub struct LockableScore {
104         /// A pointer to the opaque Rust object.
105
106         /// Nearly everywhere, inner must be non-null, however in places where
107         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
108         pub inner: *mut nativeLockableScore,
109         /// Indicates that this is the only struct which contains the same pointer.
110
111         /// Rust functions which take ownership of an object provided via an argument require
112         /// this to be true and invalidate the object pointed to by inner.
113         pub is_owned: bool,
114 }
115
116 impl Drop for LockableScore {
117         fn drop(&mut self) {
118                 if self.is_owned && !<*mut nativeLockableScore>::is_null(self.inner) {
119                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
120                 }
121         }
122 }
123 /// Frees any resources used by the LockableScore, if is_owned is set and inner is non-NULL.
124 #[no_mangle]
125 pub extern "C" fn LockableScore_free(this_obj: LockableScore) { }
126 #[allow(unused)]
127 /// Used only if an object of this type is returned as a trait impl by a method
128 pub(crate) extern "C" fn LockableScore_free_void(this_ptr: *mut c_void) {
129         unsafe { let _ = Box::from_raw(this_ptr as *mut nativeLockableScore); }
130 }
131 #[allow(unused)]
132 impl LockableScore {
133         pub(crate) fn get_native_ref(&self) -> &'static nativeLockableScore {
134                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
135         }
136         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeLockableScore {
137                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
138         }
139         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
140         pub(crate) fn take_inner(mut self) -> *mut nativeLockableScore {
141                 assert!(self.is_owned);
142                 let ret = ObjOps::untweak_ptr(self.inner);
143                 self.inner = std::ptr::null_mut();
144                 ret
145         }
146 }
147 /// Constructs a new LockableScore from a Score
148 #[must_use]
149 #[no_mangle]
150 pub extern "C" fn LockableScore_new(mut score: crate::lightning::routing::Score) -> LockableScore {
151         let mut ret = lightning::routing::LockableScore::new(score);
152         LockableScore { inner: ObjOps::heap_alloc(ret), is_owned: true }
153 }
154
155 #[no_mangle]
156 /// Serialize the LockableScore object into a byte array which can be read by LockableScore_read
157 pub extern "C" fn LockableScore_write(obj: &LockableScore) -> crate::c_types::derived::CVec_u8Z {
158         crate::c_types::serialize_obj(unsafe { &*obj }.get_native_ref())
159 }
160 #[no_mangle]
161 pub(crate) extern "C" fn LockableScore_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {
162         crate::c_types::serialize_obj(unsafe { &*(obj as *const nativeLockableScore) })
163 }