Merge pull request #51 from TheBlueMatt/main
[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 std::str::FromStr;
16 use std::ffi::c_void;
17 use core::convert::Infallible;
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
20
21 /// An interface to send a transaction to the Bitcoin network.
22 #[repr(C)]
23 pub struct BroadcasterInterface {
24         /// An opaque pointer which is passed to your function implementations as an argument.
25         /// This has no meaning in the LDK, and can be NULL or any other value.
26         pub this_arg: *mut c_void,
27         /// Sends a transaction out to (hopefully) be mined.
28         pub broadcast_transaction: extern "C" fn (this_arg: *const c_void, tx: crate::c_types::Transaction),
29         /// Frees any resources associated with this object given its this_arg pointer.
30         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
31         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
32 }
33 unsafe impl Send for BroadcasterInterface {}
34 unsafe impl Sync for BroadcasterInterface {}
35 #[no_mangle]
36 pub(crate) extern "C" fn BroadcasterInterface_clone_fields(orig: &BroadcasterInterface) -> BroadcasterInterface {
37         BroadcasterInterface {
38                 this_arg: orig.this_arg,
39                 broadcast_transaction: Clone::clone(&orig.broadcast_transaction),
40                 free: Clone::clone(&orig.free),
41         }
42 }
43
44 use lightning::chain::chaininterface::BroadcasterInterface as rustBroadcasterInterface;
45 impl rustBroadcasterInterface for BroadcasterInterface {
46         fn broadcast_transaction(&self, mut tx: &bitcoin::blockdata::transaction::Transaction) {
47                 (self.broadcast_transaction)(self.this_arg, crate::c_types::Transaction::from_bitcoin(tx))
48         }
49 }
50
51 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
52 // directly as a Deref trait in higher-level structs:
53 impl std::ops::Deref for BroadcasterInterface {
54         type Target = Self;
55         fn deref(&self) -> &Self {
56                 self
57         }
58 }
59 /// Calls the free function if one is set
60 #[no_mangle]
61 pub extern "C" fn BroadcasterInterface_free(this_ptr: BroadcasterInterface) { }
62 impl Drop for BroadcasterInterface {
63         fn drop(&mut self) {
64                 if let Some(f) = self.free {
65                         f(self.this_arg);
66                 }
67         }
68 }
69 /// An enum that represents the speed at which we want a transaction to confirm used for feerate
70 /// estimation.
71 #[must_use]
72 #[derive(Clone)]
73 #[repr(C)]
74 pub enum ConfirmationTarget {
75         /// We are happy with this transaction confirming slowly when feerate drops some.
76         Background,
77         /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
78         Normal,
79         /// We'd like this transaction to confirm in the next few blocks.
80         HighPriority,
81 }
82 use lightning::chain::chaininterface::ConfirmationTarget as nativeConfirmationTarget;
83 impl ConfirmationTarget {
84         #[allow(unused)]
85         pub(crate) fn to_native(&self) -> nativeConfirmationTarget {
86                 match self {
87                         ConfirmationTarget::Background => nativeConfirmationTarget::Background,
88                         ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
89                         ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
90                 }
91         }
92         #[allow(unused)]
93         pub(crate) fn into_native(self) -> nativeConfirmationTarget {
94                 match self {
95                         ConfirmationTarget::Background => nativeConfirmationTarget::Background,
96                         ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
97                         ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
98                 }
99         }
100         #[allow(unused)]
101         pub(crate) fn from_native(native: &nativeConfirmationTarget) -> Self {
102                 match native {
103                         nativeConfirmationTarget::Background => ConfirmationTarget::Background,
104                         nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
105                         nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
106                 }
107         }
108         #[allow(unused)]
109         pub(crate) fn native_into(native: nativeConfirmationTarget) -> Self {
110                 match native {
111                         nativeConfirmationTarget::Background => ConfirmationTarget::Background,
112                         nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
113                         nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
114                 }
115         }
116 }
117 /// Creates a copy of the ConfirmationTarget
118 #[no_mangle]
119 pub extern "C" fn ConfirmationTarget_clone(orig: &ConfirmationTarget) -> ConfirmationTarget {
120         orig.clone()
121 }
122 #[no_mangle]
123 /// Utility method to constructs a new Background-variant ConfirmationTarget
124 pub extern "C" fn ConfirmationTarget_background() -> ConfirmationTarget {
125         ConfirmationTarget::Background}
126 #[no_mangle]
127 /// Utility method to constructs a new Normal-variant ConfirmationTarget
128 pub extern "C" fn ConfirmationTarget_normal() -> ConfirmationTarget {
129         ConfirmationTarget::Normal}
130 #[no_mangle]
131 /// Utility method to constructs a new HighPriority-variant ConfirmationTarget
132 pub extern "C" fn ConfirmationTarget_high_priority() -> ConfirmationTarget {
133         ConfirmationTarget::HighPriority}
134 /// Checks if two ConfirmationTargets contain equal inner contents.
135 /// This ignores pointers and is_owned flags and looks at the values in fields.
136 #[no_mangle]
137 pub extern "C" fn ConfirmationTarget_eq(a: &ConfirmationTarget, b: &ConfirmationTarget) -> bool {
138         if &a.to_native() == &b.to_native() { true } else { false }
139 }
140 /// A trait which should be implemented to provide feerate information on a number of time
141 /// horizons.
142 ///
143 /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
144 /// called from inside the library in response to chain events, P2P events, or timer events).
145 #[repr(C)]
146 pub struct FeeEstimator {
147         /// An opaque pointer which is passed to your function implementations as an argument.
148         /// This has no meaning in the LDK, and can be NULL or any other value.
149         pub this_arg: *mut c_void,
150         /// Gets estimated satoshis of fee required per 1000 Weight-Units.
151         ///
152         /// Must return a value no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later
153         /// round-downs don't put us below 1 satoshi-per-byte).
154         ///
155         /// This method can be implemented with the following unit conversions:
156         ///  * max(satoshis-per-byte * 250, 253)
157         ///  * max(satoshis-per-kbyte / 4, 253)
158         #[must_use]
159         pub get_est_sat_per_1000_weight: extern "C" fn (this_arg: *const c_void, confirmation_target: crate::lightning::chain::chaininterface::ConfirmationTarget) -> u32,
160         /// Frees any resources associated with this object given its this_arg pointer.
161         /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
162         pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
163 }
164 unsafe impl Send for FeeEstimator {}
165 unsafe impl Sync for FeeEstimator {}
166 #[no_mangle]
167 pub(crate) extern "C" fn FeeEstimator_clone_fields(orig: &FeeEstimator) -> FeeEstimator {
168         FeeEstimator {
169                 this_arg: orig.this_arg,
170                 get_est_sat_per_1000_weight: Clone::clone(&orig.get_est_sat_per_1000_weight),
171                 free: Clone::clone(&orig.free),
172         }
173 }
174
175 use lightning::chain::chaininterface::FeeEstimator as rustFeeEstimator;
176 impl rustFeeEstimator for FeeEstimator {
177         fn get_est_sat_per_1000_weight(&self, mut confirmation_target: lightning::chain::chaininterface::ConfirmationTarget) -> u32 {
178                 let mut ret = (self.get_est_sat_per_1000_weight)(self.this_arg, crate::lightning::chain::chaininterface::ConfirmationTarget::native_into(confirmation_target));
179                 ret
180         }
181 }
182
183 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
184 // directly as a Deref trait in higher-level structs:
185 impl std::ops::Deref for FeeEstimator {
186         type Target = Self;
187         fn deref(&self) -> &Self {
188                 self
189         }
190 }
191 /// Calls the free function if one is set
192 #[no_mangle]
193 pub extern "C" fn FeeEstimator_free(this_ptr: FeeEstimator) { }
194 impl Drop for FeeEstimator {
195         fn drop(&mut self) {
196                 if let Some(f) = self.free {
197                         f(self.this_arg);
198                 }
199         }
200 }
201 /// Minimum relay fee as required by bitcoin network mempool policy.
202
203 #[no_mangle]
204 pub static MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = lightning::chain::chaininterface::MIN_RELAY_FEE_SAT_PER_1000_WEIGHT;