1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! Traits and utility impls which allow other parts of rust-lightning to interact with the
12 //! Includes traits for monitoring and receiving notifications of new blocks and block
13 //! disconnections, transaction broadcasting, and feerate information requests.
15 use std::str::FromStr;
17 use bitcoin::hashes::Hash;
18 use crate::c_types::*;
20 /// An interface to send a transaction to the Bitcoin network.
22 pub struct BroadcasterInterface {
23 /// An opaque pointer which is passed to your function implementations as an argument.
24 /// This has no meaning in the LDK, and can be NULL or any other value.
25 pub this_arg: *mut c_void,
26 /// Sends a transaction out to (hopefully) be mined.
27 pub broadcast_transaction: extern "C" fn (this_arg: *const c_void, tx: crate::c_types::Transaction),
28 /// Frees any resources associated with this object given its this_arg pointer.
29 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
30 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
32 unsafe impl Send for BroadcasterInterface {}
33 unsafe impl Sync for BroadcasterInterface {}
35 use lightning::chain::chaininterface::BroadcasterInterface as rustBroadcasterInterface;
36 impl rustBroadcasterInterface for BroadcasterInterface {
37 fn broadcast_transaction(&self, mut tx: &bitcoin::blockdata::transaction::Transaction) {
38 (self.broadcast_transaction)(self.this_arg, crate::c_types::Transaction::from_bitcoin(tx))
42 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
43 // directly as a Deref trait in higher-level structs:
44 impl std::ops::Deref for BroadcasterInterface {
46 fn deref(&self) -> &Self {
50 /// Calls the free function if one is set
52 pub extern "C" fn BroadcasterInterface_free(this_ptr: BroadcasterInterface) { }
53 impl Drop for BroadcasterInterface {
55 if let Some(f) = self.free {
60 /// An enum that represents the speed at which we want a transaction to confirm used for feerate
65 pub enum ConfirmationTarget {
66 /// We are happy with this transaction confirming slowly when feerate drops some.
68 /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
70 /// We'd like this transaction to confirm in the next few blocks.
73 use lightning::chain::chaininterface::ConfirmationTarget as nativeConfirmationTarget;
74 impl ConfirmationTarget {
76 pub(crate) fn to_native(&self) -> nativeConfirmationTarget {
78 ConfirmationTarget::Background => nativeConfirmationTarget::Background,
79 ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
80 ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
84 pub(crate) fn into_native(self) -> nativeConfirmationTarget {
86 ConfirmationTarget::Background => nativeConfirmationTarget::Background,
87 ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
88 ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
92 pub(crate) fn from_native(native: &nativeConfirmationTarget) -> Self {
94 nativeConfirmationTarget::Background => ConfirmationTarget::Background,
95 nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
96 nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
100 pub(crate) fn native_into(native: nativeConfirmationTarget) -> Self {
102 nativeConfirmationTarget::Background => ConfirmationTarget::Background,
103 nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
104 nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
108 /// Creates a copy of the ConfirmationTarget
110 pub extern "C" fn ConfirmationTarget_clone(orig: &ConfirmationTarget) -> ConfirmationTarget {
113 /// A trait which should be implemented to provide feerate information on a number of time
116 /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
117 /// called from inside the library in response to chain events, P2P events, or timer events).
119 pub struct FeeEstimator {
120 /// An opaque pointer which is passed to your function implementations as an argument.
121 /// This has no meaning in the LDK, and can be NULL or any other value.
122 pub this_arg: *mut c_void,
123 /// Gets estimated satoshis of fee required per 1000 Weight-Units.
125 /// Must be no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later round-downs
126 /// don't put us below 1 satoshi-per-byte).
128 /// This translates to:
129 /// * satoshis-per-byte * 250
130 /// * ceil(satoshis-per-kbyte / 4)
132 pub get_est_sat_per_1000_weight: extern "C" fn (this_arg: *const c_void, confirmation_target: crate::lightning::chain::chaininterface::ConfirmationTarget) -> u32,
133 /// Frees any resources associated with this object given its this_arg pointer.
134 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
135 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
137 unsafe impl Send for FeeEstimator {}
138 unsafe impl Sync for FeeEstimator {}
140 use lightning::chain::chaininterface::FeeEstimator as rustFeeEstimator;
141 impl rustFeeEstimator for FeeEstimator {
142 fn get_est_sat_per_1000_weight(&self, mut confirmation_target: lightning::chain::chaininterface::ConfirmationTarget) -> u32 {
143 let mut ret = (self.get_est_sat_per_1000_weight)(self.this_arg, crate::lightning::chain::chaininterface::ConfirmationTarget::native_into(confirmation_target));
148 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
149 // directly as a Deref trait in higher-level structs:
150 impl std::ops::Deref for FeeEstimator {
152 fn deref(&self) -> &Self {
156 /// Calls the free function if one is set
158 pub extern "C" fn FeeEstimator_free(this_ptr: FeeEstimator) { }
159 impl Drop for FeeEstimator {
161 if let Some(f) = self.free {
166 /// Minimum relay fee as required by bitcoin network mempool policy.
169 pub static MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = lightning::chain::chaininterface::MIN_RELAY_FEE_SAT_PER_1000_WEIGHT;