Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning / chain / chaininterface.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 //! Traits and utility impls which allow other parts of rust-lightning to interact with the
10 //! blockchain.
11 //!
12 //! Includes traits for monitoring and receiving notifications of new blocks and block
13 //! disconnections, transaction broadcasting, and feerate information requests.
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 interface to send a transaction to the Bitcoin network.
25 #[repr(C)]
26 pub struct BroadcasterInterface {
27         /// An opaque pointer which is passed to your function implementations as an argument.
28         /// This has no meaning in the LDK, and can be NULL or any other value.
29         pub this_arg: *mut c_void,
30         /// Sends a list of transactions out to (hopefully) be mined.
31         /// This only needs to handle the actual broadcasting of transactions, LDK will automatically
32         /// rebroadcast transactions that haven't made it into a block.
33         ///
34         /// In some cases LDK may attempt to broadcast a transaction which double-spends another
35         /// and this isn't a bug and can be safely ignored.
36         ///
37         /// If more than one transaction is given, these transactions should be considered to be a
38         /// package and broadcast together. Some of the transactions may or may not depend on each other,
39         /// be sure to manage both cases correctly.
40         ///
41         /// Bitcoin transaction packages are defined in BIP 331 and here:
42         /// https://github.com/bitcoin/bitcoin/blob/master/doc/policy/packages.md
43         pub broadcast_transactions: extern "C" fn (this_arg: *const c_void, txs: crate::c_types::derived::CVec_TransactionZ),
44         /// Frees any resources associated with this object given its this_arg pointer.
45         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
46         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
47 }
48 unsafe impl Send for BroadcasterInterface {}
49 unsafe impl Sync for BroadcasterInterface {}
50 pub(crate) fn BroadcasterInterface_clone_fields(orig: &BroadcasterInterface) -> BroadcasterInterface {
51         BroadcasterInterface {
52                 this_arg: orig.this_arg,
53                 broadcast_transactions: Clone::clone(&orig.broadcast_transactions),
54                 free: Clone::clone(&orig.free),
55         }
56 }
57
58 use lightning::chain::chaininterface::BroadcasterInterface as rustBroadcasterInterface;
59 impl rustBroadcasterInterface for BroadcasterInterface {
60         fn broadcast_transactions(&self, mut txs: &[&bitcoin::blockdata::transaction::Transaction]) {
61                 let mut local_txs = Vec::new(); for item in txs.iter() { local_txs.push( { crate::c_types::Transaction::from_bitcoin((*item)) }); };
62                 (self.broadcast_transactions)(self.this_arg, local_txs.into())
63         }
64 }
65
66 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
67 // directly as a Deref trait in higher-level structs:
68 impl core::ops::Deref for BroadcasterInterface {
69         type Target = Self;
70         fn deref(&self) -> &Self {
71                 self
72         }
73 }
74 impl core::ops::DerefMut for BroadcasterInterface {
75         fn deref_mut(&mut self) -> &mut Self {
76                 self
77         }
78 }
79 /// Calls the free function if one is set
80 #[no_mangle]
81 pub extern "C" fn BroadcasterInterface_free(this_ptr: BroadcasterInterface) { }
82 impl Drop for BroadcasterInterface {
83         fn drop(&mut self) {
84                 if let Some(f) = self.free {
85                         f(self.this_arg);
86                 }
87         }
88 }
89 /// An enum that represents the priority at which we want a transaction to confirm used for feerate
90 /// estimation.
91 #[derive(Clone)]
92 #[must_use]
93 #[repr(C)]
94 pub enum ConfirmationTarget {
95         /// We'd like a transaction to confirm in the future, but don't want to commit most of the fees
96         /// required to do so yet. The remaining fees will come via a Child-Pays-For-Parent (CPFP) fee
97         /// bump of the transaction.
98         ///
99         /// The feerate returned should be the absolute minimum feerate required to enter most node
100         /// mempools across the network. Note that if you are not able to obtain this feerate estimate,
101         /// you should likely use the furthest-out estimate allowed by your fee estimator.
102         MempoolMinimum,
103         /// We are happy with a transaction confirming slowly, at least within a day or so worth of
104         /// blocks.
105         Background,
106         /// We'd like a transaction to confirm without major delayed, i.e., within the next 12-24 blocks.
107         Normal,
108         /// We'd like a transaction to confirm in the next few blocks.
109         HighPriority,
110 }
111 use lightning::chain::chaininterface::ConfirmationTarget as ConfirmationTargetImport;
112 pub(crate) type nativeConfirmationTarget = ConfirmationTargetImport;
113
114 impl ConfirmationTarget {
115         #[allow(unused)]
116         pub(crate) fn to_native(&self) -> nativeConfirmationTarget {
117                 match self {
118                         ConfirmationTarget::MempoolMinimum => nativeConfirmationTarget::MempoolMinimum,
119                         ConfirmationTarget::Background => nativeConfirmationTarget::Background,
120                         ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
121                         ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
122                 }
123         }
124         #[allow(unused)]
125         pub(crate) fn into_native(self) -> nativeConfirmationTarget {
126                 match self {
127                         ConfirmationTarget::MempoolMinimum => nativeConfirmationTarget::MempoolMinimum,
128                         ConfirmationTarget::Background => nativeConfirmationTarget::Background,
129                         ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
130                         ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
131                 }
132         }
133         #[allow(unused)]
134         pub(crate) fn from_native(native: &nativeConfirmationTarget) -> Self {
135                 match native {
136                         nativeConfirmationTarget::MempoolMinimum => ConfirmationTarget::MempoolMinimum,
137                         nativeConfirmationTarget::Background => ConfirmationTarget::Background,
138                         nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
139                         nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
140                 }
141         }
142         #[allow(unused)]
143         pub(crate) fn native_into(native: nativeConfirmationTarget) -> Self {
144                 match native {
145                         nativeConfirmationTarget::MempoolMinimum => ConfirmationTarget::MempoolMinimum,
146                         nativeConfirmationTarget::Background => ConfirmationTarget::Background,
147                         nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
148                         nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
149                 }
150         }
151 }
152 /// Creates a copy of the ConfirmationTarget
153 #[no_mangle]
154 pub extern "C" fn ConfirmationTarget_clone(orig: &ConfirmationTarget) -> ConfirmationTarget {
155         orig.clone()
156 }
157 #[no_mangle]
158 /// Utility method to constructs a new MempoolMinimum-variant ConfirmationTarget
159 pub extern "C" fn ConfirmationTarget_mempool_minimum() -> ConfirmationTarget {
160         ConfirmationTarget::MempoolMinimum}
161 #[no_mangle]
162 /// Utility method to constructs a new Background-variant ConfirmationTarget
163 pub extern "C" fn ConfirmationTarget_background() -> ConfirmationTarget {
164         ConfirmationTarget::Background}
165 #[no_mangle]
166 /// Utility method to constructs a new Normal-variant ConfirmationTarget
167 pub extern "C" fn ConfirmationTarget_normal() -> ConfirmationTarget {
168         ConfirmationTarget::Normal}
169 #[no_mangle]
170 /// Utility method to constructs a new HighPriority-variant ConfirmationTarget
171 pub extern "C" fn ConfirmationTarget_high_priority() -> ConfirmationTarget {
172         ConfirmationTarget::HighPriority}
173 /// Generates a non-cryptographic 64-bit hash of the ConfirmationTarget.
174 #[no_mangle]
175 pub extern "C" fn ConfirmationTarget_hash(o: &ConfirmationTarget) -> u64 {
176         // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
177         #[allow(deprecated)]
178         let mut hasher = core::hash::SipHasher::new();
179         core::hash::Hash::hash(&o.to_native(), &mut hasher);
180         core::hash::Hasher::finish(&hasher)
181 }
182 /// Checks if two ConfirmationTargets contain equal inner contents.
183 /// This ignores pointers and is_owned flags and looks at the values in fields.
184 #[no_mangle]
185 pub extern "C" fn ConfirmationTarget_eq(a: &ConfirmationTarget, b: &ConfirmationTarget) -> bool {
186         if &a.to_native() == &b.to_native() { true } else { false }
187 }
188 /// A trait which should be implemented to provide feerate information on a number of time
189 /// horizons.
190 ///
191 /// If access to a local mempool is not feasible, feerate estimates should be fetched from a set of
192 /// third-parties hosting them. Note that this enables them to affect the propagation of your
193 /// pre-signed transactions at any time and therefore endangers the safety of channels funds. It
194 /// should be considered carefully as a deployment.
195 ///
196 /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
197 /// called from inside the library in response to chain events, P2P events, or timer events).
198 #[repr(C)]
199 pub struct FeeEstimator {
200         /// An opaque pointer which is passed to your function implementations as an argument.
201         /// This has no meaning in the LDK, and can be NULL or any other value.
202         pub this_arg: *mut c_void,
203         /// Gets estimated satoshis of fee required per 1000 Weight-Units.
204         ///
205         /// LDK will wrap this method and ensure that the value returned is no smaller than 253
206         /// (ie 1 satoshi-per-byte rounded up to ensure later round-downs don't put us below 1 satoshi-per-byte).
207         ///
208         /// The following unit conversions can be used to convert to sats/KW:
209         ///  * satoshis-per-byte * 250
210         ///  * satoshis-per-kbyte / 4
211         pub get_est_sat_per_1000_weight: extern "C" fn (this_arg: *const c_void, confirmation_target: crate::lightning::chain::chaininterface::ConfirmationTarget) -> u32,
212         /// Frees any resources associated with this object given its this_arg pointer.
213         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
214         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
215 }
216 unsafe impl Send for FeeEstimator {}
217 unsafe impl Sync for FeeEstimator {}
218 pub(crate) fn FeeEstimator_clone_fields(orig: &FeeEstimator) -> FeeEstimator {
219         FeeEstimator {
220                 this_arg: orig.this_arg,
221                 get_est_sat_per_1000_weight: Clone::clone(&orig.get_est_sat_per_1000_weight),
222                 free: Clone::clone(&orig.free),
223         }
224 }
225
226 use lightning::chain::chaininterface::FeeEstimator as rustFeeEstimator;
227 impl rustFeeEstimator for FeeEstimator {
228         fn get_est_sat_per_1000_weight(&self, mut confirmation_target: lightning::chain::chaininterface::ConfirmationTarget) -> u32 {
229                 let mut ret = (self.get_est_sat_per_1000_weight)(self.this_arg, crate::lightning::chain::chaininterface::ConfirmationTarget::native_into(confirmation_target));
230                 ret
231         }
232 }
233
234 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
235 // directly as a Deref trait in higher-level structs:
236 impl core::ops::Deref for FeeEstimator {
237         type Target = Self;
238         fn deref(&self) -> &Self {
239                 self
240         }
241 }
242 impl core::ops::DerefMut for FeeEstimator {
243         fn deref_mut(&mut self) -> &mut Self {
244                 self
245         }
246 }
247 /// Calls the free function if one is set
248 #[no_mangle]
249 pub extern "C" fn FeeEstimator_free(this_ptr: FeeEstimator) { }
250 impl Drop for FeeEstimator {
251         fn drop(&mut self) {
252                 if let Some(f) = self.free {
253                         f(self.this_arg);
254                 }
255         }
256 }
257 /// Minimum relay fee as required by bitcoin network mempool policy.
258
259 #[no_mangle]
260 pub static MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = lightning::chain::chaininterface::MIN_RELAY_FEE_SAT_PER_1000_WEIGHT;
261 /// Minimum feerate that takes a sane approach to bitcoind weight-to-vbytes rounding.
262 /// See the following Core Lightning commit for an explanation:
263 /// <https://github.com/ElementsProject/lightning/commit/2e687b9b352c9092b5e8bd4a688916ac50b44af0>
264
265 #[no_mangle]
266 pub static FEERATE_FLOOR_SATS_PER_KW: u32 = lightning::chain::chaininterface::FEERATE_FLOOR_SATS_PER_KW;