1 //! Traits and utility impls which allow other parts of rust-lightning to interact with the
4 //! Includes traits for monitoring and receiving notifications of new blocks and block
5 //! disconnections, transaction broadcasting, and feerate information requests.
8 use bitcoin::hashes::Hash;
11 /// An interface to send a transaction to the Bitcoin network.
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)>,
23 unsafe impl Sync for BroadcasterInterface {}
24 unsafe impl Send for BroadcasterInterface {}
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))
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 {
38 fn deref(&self) -> &Self {
42 /// Calls the free function if one is set
44 pub extern "C" fn BroadcasterInterface_free(this_ptr: BroadcasterInterface) { }
45 impl Drop for BroadcasterInterface {
47 if let Some(f) = self.free {
52 /// An enum that represents the speed at which we want a transaction to confirm used for feerate
57 pub enum ConfirmationTarget {
58 /// We are happy with this transaction confirming slowly when feerate drops some.
60 /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
62 /// We'd like this transaction to confirm in the next few blocks.
65 use lightning::chain::chaininterface::ConfirmationTarget as nativeConfirmationTarget;
66 impl ConfirmationTarget {
68 pub(crate) fn to_native(&self) -> nativeConfirmationTarget {
70 ConfirmationTarget::Background => nativeConfirmationTarget::Background,
71 ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
72 ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
76 pub(crate) fn into_native(self) -> nativeConfirmationTarget {
78 ConfirmationTarget::Background => nativeConfirmationTarget::Background,
79 ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
80 ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
84 pub(crate) fn from_native(native: &nativeConfirmationTarget) -> Self {
86 nativeConfirmationTarget::Background => ConfirmationTarget::Background,
87 nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
88 nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
92 pub(crate) fn native_into(native: nativeConfirmationTarget) -> Self {
94 nativeConfirmationTarget::Background => ConfirmationTarget::Background,
95 nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
96 nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
100 /// Creates a copy of the ConfirmationTarget
102 pub extern "C" fn ConfirmationTarget_clone(orig: &ConfirmationTarget) -> ConfirmationTarget {
105 /// A trait which should be implemented to provide feerate information on a number of time
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).
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.
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).
120 /// This translates to:
121 /// * satoshis-per-byte * 250
122 /// * ceil(satoshis-per-kbyte / 4)
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)>,
129 unsafe impl Sync for FeeEstimator {}
130 unsafe impl Send for FeeEstimator {}
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));
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 {
144 fn deref(&self) -> &Self {
148 /// Calls the free function if one is set
150 pub extern "C" fn FeeEstimator_free(this_ptr: FeeEstimator) { }
151 impl Drop for FeeEstimator {
153 if let Some(f) = self.free {
158 /// Minimum relay fee as required by bitcoin network mempool policy.
161 pub static MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = lightning::chain::chaininterface::MIN_RELAY_FEE_SAT_PER_1000_WEIGHT;