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 alloc::str::FromStr;
16 use core::ffi::c_void;
17 use core::convert::Infallible;
18 use bitcoin::hashes::Hash;
19 use crate::c_types::*;
20 #[cfg(feature="no-std")]
21 use alloc::{vec::Vec, boxed::Box};
23 /// An interface to send a transaction to the Bitcoin network.
25 pub struct BroadcasterInterface {
26 /// An opaque pointer which is passed to your function implementations as an argument.
27 /// This has no meaning in the LDK, and can be NULL or any other value.
28 pub this_arg: *mut c_void,
29 /// Sends a transaction out to (hopefully) be mined.
30 pub broadcast_transaction: extern "C" fn (this_arg: *const c_void, tx: crate::c_types::Transaction),
31 /// Frees any resources associated with this object given its this_arg pointer.
32 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
33 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
35 unsafe impl Send for BroadcasterInterface {}
36 unsafe impl Sync for BroadcasterInterface {}
38 pub(crate) extern "C" fn BroadcasterInterface_clone_fields(orig: &BroadcasterInterface) -> BroadcasterInterface {
39 BroadcasterInterface {
40 this_arg: orig.this_arg,
41 broadcast_transaction: Clone::clone(&orig.broadcast_transaction),
42 free: Clone::clone(&orig.free),
46 use lightning::chain::chaininterface::BroadcasterInterface as rustBroadcasterInterface;
47 impl rustBroadcasterInterface for BroadcasterInterface {
48 fn broadcast_transaction(&self, mut tx: &bitcoin::blockdata::transaction::Transaction) {
49 (self.broadcast_transaction)(self.this_arg, crate::c_types::Transaction::from_bitcoin(tx))
53 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
54 // directly as a Deref trait in higher-level structs:
55 impl core::ops::Deref for BroadcasterInterface {
57 fn deref(&self) -> &Self {
61 /// Calls the free function if one is set
63 pub extern "C" fn BroadcasterInterface_free(this_ptr: BroadcasterInterface) { }
64 impl Drop for BroadcasterInterface {
66 if let Some(f) = self.free {
71 /// An enum that represents the speed at which we want a transaction to confirm used for feerate
76 pub enum ConfirmationTarget {
77 /// We are happy with this transaction confirming slowly when feerate drops some.
79 /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
81 /// We'd like this transaction to confirm in the next few blocks.
84 use lightning::chain::chaininterface::ConfirmationTarget as nativeConfirmationTarget;
85 impl ConfirmationTarget {
87 pub(crate) fn to_native(&self) -> nativeConfirmationTarget {
89 ConfirmationTarget::Background => nativeConfirmationTarget::Background,
90 ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
91 ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
95 pub(crate) fn into_native(self) -> nativeConfirmationTarget {
97 ConfirmationTarget::Background => nativeConfirmationTarget::Background,
98 ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
99 ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
103 pub(crate) fn from_native(native: &nativeConfirmationTarget) -> Self {
105 nativeConfirmationTarget::Background => ConfirmationTarget::Background,
106 nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
107 nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
111 pub(crate) fn native_into(native: nativeConfirmationTarget) -> Self {
113 nativeConfirmationTarget::Background => ConfirmationTarget::Background,
114 nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
115 nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
119 /// Creates a copy of the ConfirmationTarget
121 pub extern "C" fn ConfirmationTarget_clone(orig: &ConfirmationTarget) -> ConfirmationTarget {
125 /// Utility method to constructs a new Background-variant ConfirmationTarget
126 pub extern "C" fn ConfirmationTarget_background() -> ConfirmationTarget {
127 ConfirmationTarget::Background}
129 /// Utility method to constructs a new Normal-variant ConfirmationTarget
130 pub extern "C" fn ConfirmationTarget_normal() -> ConfirmationTarget {
131 ConfirmationTarget::Normal}
133 /// Utility method to constructs a new HighPriority-variant ConfirmationTarget
134 pub extern "C" fn ConfirmationTarget_high_priority() -> ConfirmationTarget {
135 ConfirmationTarget::HighPriority}
136 /// Checks if two ConfirmationTargets contain equal inner contents.
137 /// This ignores pointers and is_owned flags and looks at the values in fields.
139 pub extern "C" fn ConfirmationTarget_eq(a: &ConfirmationTarget, b: &ConfirmationTarget) -> bool {
140 if &a.to_native() == &b.to_native() { true } else { false }
142 /// A trait which should be implemented to provide feerate information on a number of time
145 /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
146 /// called from inside the library in response to chain events, P2P events, or timer events).
148 pub struct FeeEstimator {
149 /// An opaque pointer which is passed to your function implementations as an argument.
150 /// This has no meaning in the LDK, and can be NULL or any other value.
151 pub this_arg: *mut c_void,
152 /// Gets estimated satoshis of fee required per 1000 Weight-Units.
154 /// Must return a value no smaller than 253 (ie 1 satoshi-per-byte rounded up to ensure later
155 /// round-downs don't put us below 1 satoshi-per-byte).
157 /// This method can be implemented with the following unit conversions:
158 /// * max(satoshis-per-byte * 250, 253)
159 /// * max(satoshis-per-kbyte / 4, 253)
161 pub get_est_sat_per_1000_weight: extern "C" fn (this_arg: *const c_void, confirmation_target: crate::lightning::chain::chaininterface::ConfirmationTarget) -> u32,
162 /// Frees any resources associated with this object given its this_arg pointer.
163 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
164 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
166 unsafe impl Send for FeeEstimator {}
167 unsafe impl Sync for FeeEstimator {}
169 pub(crate) extern "C" fn FeeEstimator_clone_fields(orig: &FeeEstimator) -> FeeEstimator {
171 this_arg: orig.this_arg,
172 get_est_sat_per_1000_weight: Clone::clone(&orig.get_est_sat_per_1000_weight),
173 free: Clone::clone(&orig.free),
177 use lightning::chain::chaininterface::FeeEstimator as rustFeeEstimator;
178 impl rustFeeEstimator for FeeEstimator {
179 fn get_est_sat_per_1000_weight(&self, mut confirmation_target: lightning::chain::chaininterface::ConfirmationTarget) -> u32 {
180 let mut ret = (self.get_est_sat_per_1000_weight)(self.this_arg, crate::lightning::chain::chaininterface::ConfirmationTarget::native_into(confirmation_target));
185 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
186 // directly as a Deref trait in higher-level structs:
187 impl core::ops::Deref for FeeEstimator {
189 fn deref(&self) -> &Self {
193 /// Calls the free function if one is set
195 pub extern "C" fn FeeEstimator_free(this_ptr: FeeEstimator) { }
196 impl Drop for FeeEstimator {
198 if let Some(f) = self.free {
203 /// Minimum relay fee as required by bitcoin network mempool policy.
206 pub static MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = lightning::chain::chaininterface::MIN_RELAY_FEE_SAT_PER_1000_WEIGHT;