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 list of transactions out to (hopefully) be mined.
30 /// This only needs to handle the actual broadcasting of transactions, LDK will automatically
31 /// rebroadcast transactions that haven't made it into a block.
33 /// In some cases LDK may attempt to broadcast a transaction which double-spends another
34 /// and this isn't a bug and can be safely ignored.
36 /// If more than one transaction is given, these transactions should be considered to be a
37 /// package and broadcast together. Some of the transactions may or may not depend on each other,
38 /// be sure to manage both cases correctly.
40 /// Bitcoin transaction packages are defined in BIP 331 and here:
41 /// https://github.com/bitcoin/bitcoin/blob/master/doc/policy/packages.md
42 pub broadcast_transactions: extern "C" fn (this_arg: *const c_void, txs: crate::c_types::derived::CVec_TransactionZ),
43 /// Frees any resources associated with this object given its this_arg pointer.
44 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
45 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
47 unsafe impl Send for BroadcasterInterface {}
48 unsafe impl Sync for BroadcasterInterface {}
50 pub(crate) extern "C" fn BroadcasterInterface_clone_fields(orig: &BroadcasterInterface) -> BroadcasterInterface {
51 BroadcasterInterface {
52 this_arg: orig.this_arg,
53 broadcast_transactions: Clone::clone(&orig.broadcast_transactions),
54 free: Clone::clone(&orig.free),
58 use lightning::chain::chaininterface::BroadcasterInterface as rustBroadcasterInterface;
59 impl rustBroadcasterInterface for BroadcasterInterface {
60 fn broadcast_transactions(&self, mut txs: &[&bitcoin::blockdata::transaction::Transaction]) {
61 let mut local_txs = Vec::new(); for item in txs.iter() { local_txs.push( { crate::c_types::Transaction::from_bitcoin((*item)) }); };
62 (self.broadcast_transactions)(self.this_arg, local_txs.into())
66 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
67 // directly as a Deref trait in higher-level structs:
68 impl core::ops::Deref for BroadcasterInterface {
70 fn deref(&self) -> &Self {
74 /// Calls the free function if one is set
76 pub extern "C" fn BroadcasterInterface_free(this_ptr: BroadcasterInterface) { }
77 impl Drop for BroadcasterInterface {
79 if let Some(f) = self.free {
84 /// An enum that represents the priority at which we want a transaction to confirm used for feerate
89 pub enum ConfirmationTarget {
90 /// We'd like a transaction to confirm in the future, but don't want to commit most of the fees
91 /// required to do so yet. The remaining fees will come via a Child-Pays-For-Parent (CPFP) fee
92 /// bump of the transaction.
94 /// The feerate returned should be the absolute minimum feerate required to enter most node
95 /// mempools across the network. Note that if you are not able to obtain this feerate estimate,
96 /// you should likely use the furthest-out estimate allowed by your fee estimator.
98 /// We are happy with a transaction confirming slowly, at least within a day or so worth of
101 /// We'd like a transaction to confirm without major delayed, i.e., within the next 12-24 blocks.
103 /// We'd like a transaction to confirm in the next few blocks.
106 use lightning::chain::chaininterface::ConfirmationTarget as ConfirmationTargetImport;
107 pub(crate) type nativeConfirmationTarget = ConfirmationTargetImport;
109 impl ConfirmationTarget {
111 pub(crate) fn to_native(&self) -> nativeConfirmationTarget {
113 ConfirmationTarget::MempoolMinimum => nativeConfirmationTarget::MempoolMinimum,
114 ConfirmationTarget::Background => nativeConfirmationTarget::Background,
115 ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
116 ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
120 pub(crate) fn into_native(self) -> nativeConfirmationTarget {
122 ConfirmationTarget::MempoolMinimum => nativeConfirmationTarget::MempoolMinimum,
123 ConfirmationTarget::Background => nativeConfirmationTarget::Background,
124 ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
125 ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
129 pub(crate) fn from_native(native: &nativeConfirmationTarget) -> Self {
131 nativeConfirmationTarget::MempoolMinimum => ConfirmationTarget::MempoolMinimum,
132 nativeConfirmationTarget::Background => ConfirmationTarget::Background,
133 nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
134 nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
138 pub(crate) fn native_into(native: nativeConfirmationTarget) -> Self {
140 nativeConfirmationTarget::MempoolMinimum => ConfirmationTarget::MempoolMinimum,
141 nativeConfirmationTarget::Background => ConfirmationTarget::Background,
142 nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
143 nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
147 /// Creates a copy of the ConfirmationTarget
149 pub extern "C" fn ConfirmationTarget_clone(orig: &ConfirmationTarget) -> ConfirmationTarget {
153 /// Utility method to constructs a new MempoolMinimum-variant ConfirmationTarget
154 pub extern "C" fn ConfirmationTarget_mempool_minimum() -> ConfirmationTarget {
155 ConfirmationTarget::MempoolMinimum}
157 /// Utility method to constructs a new Background-variant ConfirmationTarget
158 pub extern "C" fn ConfirmationTarget_background() -> ConfirmationTarget {
159 ConfirmationTarget::Background}
161 /// Utility method to constructs a new Normal-variant ConfirmationTarget
162 pub extern "C" fn ConfirmationTarget_normal() -> ConfirmationTarget {
163 ConfirmationTarget::Normal}
165 /// Utility method to constructs a new HighPriority-variant ConfirmationTarget
166 pub extern "C" fn ConfirmationTarget_high_priority() -> ConfirmationTarget {
167 ConfirmationTarget::HighPriority}
168 /// Generates a non-cryptographic 64-bit hash of the ConfirmationTarget.
170 pub extern "C" fn ConfirmationTarget_hash(o: &ConfirmationTarget) -> u64 {
171 // Note that we'd love to use alloc::collections::hash_map::DefaultHasher but it's not in core
173 let mut hasher = core::hash::SipHasher::new();
174 core::hash::Hash::hash(&o.to_native(), &mut hasher);
175 core::hash::Hasher::finish(&hasher)
177 /// Checks if two ConfirmationTargets contain equal inner contents.
178 /// This ignores pointers and is_owned flags and looks at the values in fields.
180 pub extern "C" fn ConfirmationTarget_eq(a: &ConfirmationTarget, b: &ConfirmationTarget) -> bool {
181 if &a.to_native() == &b.to_native() { true } else { false }
183 /// A trait which should be implemented to provide feerate information on a number of time
186 /// If access to a local mempool is not feasible, feerate estimates should be fetched from a set of
187 /// third-parties hosting them. Note that this enables them to affect the propagation of your
188 /// pre-signed transactions at any time and therefore endangers the safety of channels funds. It
189 /// should be considered carefully as a deployment.
191 /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're
192 /// called from inside the library in response to chain events, P2P events, or timer events).
194 pub struct FeeEstimator {
195 /// An opaque pointer which is passed to your function implementations as an argument.
196 /// This has no meaning in the LDK, and can be NULL or any other value.
197 pub this_arg: *mut c_void,
198 /// Gets estimated satoshis of fee required per 1000 Weight-Units.
200 /// LDK will wrap this method and ensure that the value returned is no smaller than 253
201 /// (ie 1 satoshi-per-byte rounded up to ensure later round-downs don't put us below 1 satoshi-per-byte).
203 /// The following unit conversions can be used to convert to sats/KW:
204 /// * satoshis-per-byte * 250
205 /// * satoshis-per-kbyte / 4
206 pub get_est_sat_per_1000_weight: extern "C" fn (this_arg: *const c_void, confirmation_target: crate::lightning::chain::chaininterface::ConfirmationTarget) -> u32,
207 /// Frees any resources associated with this object given its this_arg pointer.
208 /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed.
209 pub free: Option<extern "C" fn(this_arg: *mut c_void)>,
211 unsafe impl Send for FeeEstimator {}
212 unsafe impl Sync for FeeEstimator {}
214 pub(crate) extern "C" fn FeeEstimator_clone_fields(orig: &FeeEstimator) -> FeeEstimator {
216 this_arg: orig.this_arg,
217 get_est_sat_per_1000_weight: Clone::clone(&orig.get_est_sat_per_1000_weight),
218 free: Clone::clone(&orig.free),
222 use lightning::chain::chaininterface::FeeEstimator as rustFeeEstimator;
223 impl rustFeeEstimator for FeeEstimator {
224 fn get_est_sat_per_1000_weight(&self, mut confirmation_target: lightning::chain::chaininterface::ConfirmationTarget) -> u32 {
225 let mut ret = (self.get_est_sat_per_1000_weight)(self.this_arg, crate::lightning::chain::chaininterface::ConfirmationTarget::native_into(confirmation_target));
230 // We're essentially a pointer already, or at least a set of pointers, so allow us to be used
231 // directly as a Deref trait in higher-level structs:
232 impl core::ops::Deref for FeeEstimator {
234 fn deref(&self) -> &Self {
238 /// Calls the free function if one is set
240 pub extern "C" fn FeeEstimator_free(this_ptr: FeeEstimator) { }
241 impl Drop for FeeEstimator {
243 if let Some(f) = self.free {
248 /// Minimum relay fee as required by bitcoin network mempool policy.
251 pub static MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = lightning::chain::chaininterface::MIN_RELAY_FEE_SAT_PER_1000_WEIGHT;
252 /// Minimum feerate that takes a sane approach to bitcoind weight-to-vbytes rounding.
253 /// See the following Core Lightning commit for an explanation:
254 /// <https://github.com/ElementsProject/lightning/commit/2e687b9b352c9092b5e8bd4a688916ac50b44af0>
257 pub static FEERATE_FLOOR_SATS_PER_KW: u32 = lightning::chain::chaininterface::FEERATE_FLOOR_SATS_PER_KW;