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