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