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