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 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)>,
19 unsafe impl Sync for BroadcasterInterface {}
20 unsafe impl Send for BroadcasterInterface {}
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))
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 {
34 fn deref(&self) -> &Self {
38 /// Calls the free function if one is set
40 pub extern "C" fn BroadcasterInterface_free(this_ptr: BroadcasterInterface) { }
41 impl Drop for BroadcasterInterface {
43 if let Some(f) = self.free {
48 /// An enum that represents the speed at which we want a transaction to confirm used for feerate
53 pub enum ConfirmationTarget {
54 /// We are happy with this transaction confirming slowly when feerate drops some.
56 /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine.
58 /// We'd like this transaction to confirm in the next few blocks.
61 use lightning::chain::chaininterface::ConfirmationTarget as nativeConfirmationTarget;
62 impl ConfirmationTarget {
64 pub(crate) fn to_native(&self) -> nativeConfirmationTarget {
66 ConfirmationTarget::Background => nativeConfirmationTarget::Background,
67 ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
68 ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
72 pub(crate) fn into_native(self) -> nativeConfirmationTarget {
74 ConfirmationTarget::Background => nativeConfirmationTarget::Background,
75 ConfirmationTarget::Normal => nativeConfirmationTarget::Normal,
76 ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority,
80 pub(crate) fn from_native(native: &nativeConfirmationTarget) -> Self {
82 nativeConfirmationTarget::Background => ConfirmationTarget::Background,
83 nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
84 nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
88 pub(crate) fn native_into(native: nativeConfirmationTarget) -> Self {
90 nativeConfirmationTarget::Background => ConfirmationTarget::Background,
91 nativeConfirmationTarget::Normal => ConfirmationTarget::Normal,
92 nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority,
97 pub extern "C" fn ConfirmationTarget_clone(orig: &ConfirmationTarget) -> ConfirmationTarget {
100 /// A trait which should be implemented to provide feerate information on a number of time
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).
106 pub struct FeeEstimator {
107 pub this_arg: *mut c_void,
108 /// Gets estimated satoshis of fee required per 1000 Weight-Units.
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).
113 /// This translates to:
114 /// * satoshis-per-byte * 250
115 /// * ceil(satoshis-per-kbyte / 4)
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)>,
120 unsafe impl Sync for FeeEstimator {}
121 unsafe impl Send for FeeEstimator {}
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));
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 {
135 fn deref(&self) -> &Self {
139 /// Calls the free function if one is set
141 pub extern "C" fn FeeEstimator_free(this_ptr: FeeEstimator) { }
142 impl Drop for FeeEstimator {
144 if let Some(f) = self.free {
151 pub static MIN_RELAY_FEE_SAT_PER_1000_WEIGHT: u64 = lightning::chain::chaininterface::MIN_RELAY_FEE_SAT_PER_1000_WEIGHT;