Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / routing / utxo.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 //! This module contains traits for LDK to access UTXOs to check gossip data is correct.
10 //!
11 //! When lightning nodes gossip channel information, they resist DoS attacks by checking that each
12 //! channel matches a UTXO on-chain, requiring at least some marginal on-chain transacting in
13 //! order to announce a channel. This module handles that checking.
14
15 use alloc::str::FromStr;
16 use core::ffi::c_void;
17 use core::convert::Infallible;
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
20 #[cfg(feature="no-std")]
21 use alloc::{vec::Vec, boxed::Box};
22
23 /// An error when accessing the chain via [`UtxoLookup`].
24 #[derive(Clone)]
25 #[must_use]
26 #[repr(C)]
27 pub enum UtxoLookupError {
28         /// The requested chain is unknown.
29         UnknownChain,
30         /// The requested transaction doesn't exist or hasn't confirmed.
31         UnknownTx,
32 }
33 use lightning::routing::utxo::UtxoLookupError as UtxoLookupErrorImport;
34 pub(crate) type nativeUtxoLookupError = UtxoLookupErrorImport;
35
36 impl UtxoLookupError {
37         #[allow(unused)]
38         pub(crate) fn to_native(&self) -> nativeUtxoLookupError {
39                 match self {
40                         UtxoLookupError::UnknownChain => nativeUtxoLookupError::UnknownChain,
41                         UtxoLookupError::UnknownTx => nativeUtxoLookupError::UnknownTx,
42                 }
43         }
44         #[allow(unused)]
45         pub(crate) fn into_native(self) -> nativeUtxoLookupError {
46                 match self {
47                         UtxoLookupError::UnknownChain => nativeUtxoLookupError::UnknownChain,
48                         UtxoLookupError::UnknownTx => nativeUtxoLookupError::UnknownTx,
49                 }
50         }
51         #[allow(unused)]
52         pub(crate) fn from_native(native: &nativeUtxoLookupError) -> Self {
53                 match native {
54                         nativeUtxoLookupError::UnknownChain => UtxoLookupError::UnknownChain,
55                         nativeUtxoLookupError::UnknownTx => UtxoLookupError::UnknownTx,
56                 }
57         }
58         #[allow(unused)]
59         pub(crate) fn native_into(native: nativeUtxoLookupError) -> Self {
60                 match native {
61                         nativeUtxoLookupError::UnknownChain => UtxoLookupError::UnknownChain,
62                         nativeUtxoLookupError::UnknownTx => UtxoLookupError::UnknownTx,
63                 }
64         }
65 }
66 /// Creates a copy of the UtxoLookupError
67 #[no_mangle]
68 pub extern "C" fn UtxoLookupError_clone(orig: &UtxoLookupError) -> UtxoLookupError {
69         orig.clone()
70 }
71 #[no_mangle]
72 /// Utility method to constructs a new UnknownChain-variant UtxoLookupError
73 pub extern "C" fn UtxoLookupError_unknown_chain() -> UtxoLookupError {
74         UtxoLookupError::UnknownChain}
75 #[no_mangle]
76 /// Utility method to constructs a new UnknownTx-variant UtxoLookupError
77 pub extern "C" fn UtxoLookupError_unknown_tx() -> UtxoLookupError {
78         UtxoLookupError::UnknownTx}
79 /// The result of a [`UtxoLookup::get_utxo`] call. A call may resolve either synchronously,
80 /// returning the `Sync` variant, or asynchronously, returning an [`UtxoFuture`] in the `Async`
81 /// variant.
82 #[derive(Clone)]
83 #[must_use]
84 #[repr(C)]
85 pub enum UtxoResult {
86         /// A result which was resolved synchronously. It either includes a [`TxOut`] for the output
87         /// requested or a [`UtxoLookupError`].
88         Sync(
89                 crate::c_types::derived::CResult_TxOutUtxoLookupErrorZ),
90         /// A result which will be resolved asynchronously. It includes a [`UtxoFuture`], a `clone` of
91         /// which you must keep locally and call [`UtxoFuture::resolve`] on once the lookup completes.
92         ///
93         /// Note that in order to avoid runaway memory usage, the number of parallel checks is limited,
94         /// but only fairly loosely. Because a pending checks block all message processing, leaving
95         /// checks pending for an extended time may cause DoS of other functions. It is recommended you
96         /// keep a tight timeout on lookups, on the order of a few seconds.
97         Async(
98                 crate::lightning::routing::utxo::UtxoFuture),
99 }
100 use lightning::routing::utxo::UtxoResult as UtxoResultImport;
101 pub(crate) type nativeUtxoResult = UtxoResultImport;
102
103 impl UtxoResult {
104         #[allow(unused)]
105         pub(crate) fn to_native(&self) -> nativeUtxoResult {
106                 match self {
107                         UtxoResult::Sync (ref a, ) => {
108                                 let mut a_nonref = Clone::clone(a);
109                                 let mut local_a_nonref = match a_nonref.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut a_nonref.contents.result)) }).into_rust() }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut a_nonref.contents.err)) }).into_native() })};
110                                 nativeUtxoResult::Sync (
111                                         local_a_nonref,
112                                 )
113                         },
114                         UtxoResult::Async (ref a, ) => {
115                                 let mut a_nonref = Clone::clone(a);
116                                 nativeUtxoResult::Async (
117                                         *unsafe { Box::from_raw(a_nonref.take_inner()) },
118                                 )
119                         },
120                 }
121         }
122         #[allow(unused)]
123         pub(crate) fn into_native(self) -> nativeUtxoResult {
124                 match self {
125                         UtxoResult::Sync (mut a, ) => {
126                                 let mut local_a = match a.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut a.contents.result)) }).into_rust() }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut a.contents.err)) }).into_native() })};
127                                 nativeUtxoResult::Sync (
128                                         local_a,
129                                 )
130                         },
131                         UtxoResult::Async (mut a, ) => {
132                                 nativeUtxoResult::Async (
133                                         *unsafe { Box::from_raw(a.take_inner()) },
134                                 )
135                         },
136                 }
137         }
138         #[allow(unused)]
139         pub(crate) fn from_native(native: &nativeUtxoResult) -> Self {
140                 match native {
141                         nativeUtxoResult::Sync (ref a, ) => {
142                                 let mut a_nonref = Clone::clone(a);
143                                 let mut local_a_nonref = match a_nonref { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::TxOut::from_rust(o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::routing::utxo::UtxoLookupError::native_into(e) }).into() };
144                                 UtxoResult::Sync (
145                                         local_a_nonref,
146                                 )
147                         },
148                         nativeUtxoResult::Async (ref a, ) => {
149                                 let mut a_nonref = Clone::clone(a);
150                                 UtxoResult::Async (
151                                         crate::lightning::routing::utxo::UtxoFuture { inner: ObjOps::heap_alloc(a_nonref), is_owned: true },
152                                 )
153                         },
154                 }
155         }
156         #[allow(unused)]
157         pub(crate) fn native_into(native: nativeUtxoResult) -> Self {
158                 match native {
159                         nativeUtxoResult::Sync (mut a, ) => {
160                                 let mut local_a = match a { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::TxOut::from_rust(o) }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::routing::utxo::UtxoLookupError::native_into(e) }).into() };
161                                 UtxoResult::Sync (
162                                         local_a,
163                                 )
164                         },
165                         nativeUtxoResult::Async (mut a, ) => {
166                                 UtxoResult::Async (
167                                         crate::lightning::routing::utxo::UtxoFuture { inner: ObjOps::heap_alloc(a), is_owned: true },
168                                 )
169                         },
170                 }
171         }
172 }
173 /// Frees any resources used by the UtxoResult
174 #[no_mangle]
175 pub extern "C" fn UtxoResult_free(this_ptr: UtxoResult) { }
176 /// Creates a copy of the UtxoResult
177 #[no_mangle]
178 pub extern "C" fn UtxoResult_clone(orig: &UtxoResult) -> UtxoResult {
179         orig.clone()
180 }
181 #[no_mangle]
182 /// Utility method to constructs a new Sync-variant UtxoResult
183 pub extern "C" fn UtxoResult_sync(a: crate::c_types::derived::CResult_TxOutUtxoLookupErrorZ) -> UtxoResult {
184         UtxoResult::Sync(a, )
185 }
186 #[no_mangle]
187 /// Utility method to constructs a new Async-variant UtxoResult
188 pub extern "C" fn UtxoResult_async(a: crate::lightning::routing::utxo::UtxoFuture) -> UtxoResult {
189         UtxoResult::Async(a, )
190 }
191 /// The `UtxoLookup` trait defines behavior for accessing on-chain UTXOs.
192 #[repr(C)]
193 pub struct UtxoLookup {
194         /// An opaque pointer which is passed to your function implementations as an argument.
195         /// This has no meaning in the LDK, and can be NULL or any other value.
196         pub this_arg: *mut c_void,
197         /// Returns the transaction output of a funding transaction encoded by [`short_channel_id`].
198         /// Returns an error if `genesis_hash` is for a different chain or if such a transaction output
199         /// is unknown.
200         ///
201         /// [`short_channel_id`]: https://github.com/lightning/bolts/blob/master/07-routing-gossip.md#definition-of-short_channel_id
202         #[must_use]
203         pub get_utxo: extern "C" fn (this_arg: *const c_void, genesis_hash: *const [u8; 32], short_channel_id: u64) -> crate::lightning::routing::utxo::UtxoResult,
204         /// Frees any resources associated with this object given its this_arg pointer.
205         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
206         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
207 }
208 unsafe impl Send for UtxoLookup {}
209 unsafe impl Sync for UtxoLookup {}
210 #[no_mangle]
211 pub(crate) extern "C" fn UtxoLookup_clone_fields(orig: &UtxoLookup) -> UtxoLookup {
212         UtxoLookup {
213                 this_arg: orig.this_arg,
214                 get_utxo: Clone::clone(&orig.get_utxo),
215                 free: Clone::clone(&orig.free),
216         }
217 }
218
219 use lightning::routing::utxo::UtxoLookup as rustUtxoLookup;
220 impl rustUtxoLookup for UtxoLookup {
221         fn get_utxo(&self, mut genesis_hash: &bitcoin::BlockHash, mut short_channel_id: u64) -> lightning::routing::utxo::UtxoResult {
222                 let mut ret = (self.get_utxo)(self.this_arg, genesis_hash.as_inner(), short_channel_id);
223                 ret.into_native()
224         }
225 }
226
227 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
228 // directly as a Deref trait in higher-level structs:
229 impl core::ops::Deref for UtxoLookup {
230         type Target = Self;
231         fn deref(&self) -> &Self {
232                 self
233         }
234 }
235 /// Calls the free function if one is set
236 #[no_mangle]
237 pub extern "C" fn UtxoLookup_free(this_ptr: UtxoLookup) { }
238 impl Drop for UtxoLookup {
239         fn drop(&mut self) {
240                 if let Some(f) = self.free {
241                         f(self.this_arg);
242                 }
243         }
244 }
245
246 use lightning::routing::utxo::UtxoFuture as nativeUtxoFutureImport;
247 pub(crate) type nativeUtxoFuture = nativeUtxoFutureImport;
248
249 /// Represents a future resolution of a [`UtxoLookup::get_utxo`] query resolving async.
250 ///
251 /// See [`UtxoResult::Async`] and [`UtxoFuture::resolve`] for more info.
252 #[must_use]
253 #[repr(C)]
254 pub struct UtxoFuture {
255         /// A pointer to the opaque Rust object.
256
257         /// Nearly everywhere, inner must be non-null, however in places where
258         /// the Rust equivalent takes an Option, it may be set to null to indicate None.
259         pub inner: *mut nativeUtxoFuture,
260         /// Indicates that this is the only struct which contains the same pointer.
261
262         /// Rust functions which take ownership of an object provided via an argument require
263         /// this to be true and invalidate the object pointed to by inner.
264         pub is_owned: bool,
265 }
266
267 impl Drop for UtxoFuture {
268         fn drop(&mut self) {
269                 if self.is_owned && !<*mut nativeUtxoFuture>::is_null(self.inner) {
270                         let _ = unsafe { Box::from_raw(ObjOps::untweak_ptr(self.inner)) };
271                 }
272         }
273 }
274 /// Frees any resources used by the UtxoFuture, if is_owned is set and inner is non-NULL.
275 #[no_mangle]
276 pub extern "C" fn UtxoFuture_free(this_obj: UtxoFuture) { }
277 #[allow(unused)]
278 /// Used only if an object of this type is returned as a trait impl by a method
279 pub(crate) extern "C" fn UtxoFuture_free_void(this_ptr: *mut c_void) {
280         let _ = unsafe { Box::from_raw(this_ptr as *mut nativeUtxoFuture) };
281 }
282 #[allow(unused)]
283 impl UtxoFuture {
284         pub(crate) fn get_native_ref(&self) -> &'static nativeUtxoFuture {
285                 unsafe { &*ObjOps::untweak_ptr(self.inner) }
286         }
287         pub(crate) fn get_native_mut_ref(&self) -> &'static mut nativeUtxoFuture {
288                 unsafe { &mut *ObjOps::untweak_ptr(self.inner) }
289         }
290         /// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy
291         pub(crate) fn take_inner(mut self) -> *mut nativeUtxoFuture {
292                 assert!(self.is_owned);
293                 let ret = ObjOps::untweak_ptr(self.inner);
294                 self.inner = core::ptr::null_mut();
295                 ret
296         }
297 }
298 impl Clone for UtxoFuture {
299         fn clone(&self) -> Self {
300                 Self {
301                         inner: if <*mut nativeUtxoFuture>::is_null(self.inner) { core::ptr::null_mut() } else {
302                                 ObjOps::heap_alloc(unsafe { &*ObjOps::untweak_ptr(self.inner) }.clone()) },
303                         is_owned: true,
304                 }
305         }
306 }
307 #[allow(unused)]
308 /// Used only if an object of this type is returned as a trait impl by a method
309 pub(crate) extern "C" fn UtxoFuture_clone_void(this_ptr: *const c_void) -> *mut c_void {
310         Box::into_raw(Box::new(unsafe { (*(this_ptr as *mut nativeUtxoFuture)).clone() })) as *mut c_void
311 }
312 #[no_mangle]
313 /// Creates a copy of the UtxoFuture
314 pub extern "C" fn UtxoFuture_clone(orig: &UtxoFuture) -> UtxoFuture {
315         orig.clone()
316 }
317 /// Builds a new future for later resolution.
318 #[must_use]
319 #[no_mangle]
320 pub extern "C" fn UtxoFuture_new() -> crate::lightning::routing::utxo::UtxoFuture {
321         let mut ret = lightning::routing::utxo::UtxoFuture::new();
322         crate::lightning::routing::utxo::UtxoFuture { inner: ObjOps::heap_alloc(ret), is_owned: true }
323 }
324
325 /// Resolves this future against the given `graph` and with the given `result`.
326 ///
327 /// This is identical to calling [`UtxoFuture::resolve`] with a dummy `gossip`, disabling
328 /// forwarding the validated gossip message onwards to peers.
329 ///
330 /// Because this may cause the [`NetworkGraph`]'s [`processing_queue_high`] to flip, in order
331 /// to allow us to interact with peers again, you should call [`PeerManager::process_events`]
332 /// after this.
333 ///
334 /// [`processing_queue_high`]: crate::ln::msgs::RoutingMessageHandler::processing_queue_high
335 /// [`PeerManager::process_events`]: crate::ln::peer_handler::PeerManager::process_events
336 #[no_mangle]
337 pub extern "C" fn UtxoFuture_resolve_without_forwarding(this_arg: &crate::lightning::routing::utxo::UtxoFuture, graph: &crate::lightning::routing::gossip::NetworkGraph, mut result: crate::c_types::derived::CResult_TxOutUtxoLookupErrorZ) {
338         let mut local_result = match result.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut result.contents.result)) }).into_rust() }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut result.contents.err)) }).into_native() })};
339         unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.resolve_without_forwarding(graph.get_native_ref(), local_result)
340 }
341
342 /// Resolves this future against the given `graph` and with the given `result`.
343 ///
344 /// The given `gossip` is used to broadcast any validated messages onwards to all peers which
345 /// have available buffer space.
346 ///
347 /// Because this may cause the [`NetworkGraph`]'s [`processing_queue_high`] to flip, in order
348 /// to allow us to interact with peers again, you should call [`PeerManager::process_events`]
349 /// after this.
350 ///
351 /// [`processing_queue_high`]: crate::ln::msgs::RoutingMessageHandler::processing_queue_high
352 /// [`PeerManager::process_events`]: crate::ln::peer_handler::PeerManager::process_events
353 #[no_mangle]
354 pub extern "C" fn UtxoFuture_resolve(this_arg: &crate::lightning::routing::utxo::UtxoFuture, graph: &crate::lightning::routing::gossip::NetworkGraph, gossip: &crate::lightning::routing::gossip::P2PGossipSync, mut result: crate::c_types::derived::CResult_TxOutUtxoLookupErrorZ) {
355         let mut local_result = match result.result_ok { true => Ok( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut result.contents.result)) }).into_rust() }), false => Err( { (*unsafe { Box::from_raw(<*mut _>::take_ptr(&mut result.contents.err)) }).into_native() })};
356         unsafe { &*ObjOps::untweak_ptr(this_arg.inner) }.resolve(graph.get_native_ref(), gossip.get_native_ref(), local_result)
357 }
358