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