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