X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fchain%2Fchaininterface.rs;h=0dc8d6c52926fd16390787229d4816bbd14bdb85;hb=3e46e1794f14640e35f09cc6da4169c152de34ce;hp=687262be8a16c16be001fbaf1b813233cb382888;hpb=27a0520e39d348f9949016d4d1219c34d8450da6;p=ldk-c-bindings diff --git a/lightning-c-bindings/src/lightning/chain/chaininterface.rs b/lightning-c-bindings/src/lightning/chain/chaininterface.rs index 687262b..0dc8d6c 100644 --- a/lightning-c-bindings/src/lightning/chain/chaininterface.rs +++ b/lightning-c-bindings/src/lightning/chain/chaininterface.rs @@ -13,6 +13,7 @@ //! disconnections, transaction broadcasting, and feerate information requests. use alloc::str::FromStr; +use alloc::string::String; use core::ffi::c_void; use core::convert::Infallible; use bitcoin::hashes::Hash; @@ -26,27 +27,39 @@ pub struct BroadcasterInterface { /// An opaque pointer which is passed to your function implementations as an argument. /// This has no meaning in the LDK, and can be NULL or any other value. pub this_arg: *mut c_void, - /// Sends a transaction out to (hopefully) be mined. - pub broadcast_transaction: extern "C" fn (this_arg: *const c_void, tx: crate::c_types::Transaction), + /// Sends a list of transactions out to (hopefully) be mined. + /// This only needs to handle the actual broadcasting of transactions, LDK will automatically + /// rebroadcast transactions that haven't made it into a block. + /// + /// In some cases LDK may attempt to broadcast a transaction which double-spends another + /// and this isn't a bug and can be safely ignored. + /// + /// If more than one transaction is given, these transactions should be considered to be a + /// package and broadcast together. Some of the transactions may or may not depend on each other, + /// be sure to manage both cases correctly. + /// + /// Bitcoin transaction packages are defined in BIP 331 and here: + /// https://github.com/bitcoin/bitcoin/blob/master/doc/policy/packages.md + pub broadcast_transactions: extern "C" fn (this_arg: *const c_void, txs: crate::c_types::derived::CVec_TransactionZ), /// Frees any resources associated with this object given its this_arg pointer. /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed. pub free: Option, } unsafe impl Send for BroadcasterInterface {} unsafe impl Sync for BroadcasterInterface {} -#[no_mangle] -pub(crate) extern "C" fn BroadcasterInterface_clone_fields(orig: &BroadcasterInterface) -> BroadcasterInterface { +pub(crate) fn BroadcasterInterface_clone_fields(orig: &BroadcasterInterface) -> BroadcasterInterface { BroadcasterInterface { this_arg: orig.this_arg, - broadcast_transaction: Clone::clone(&orig.broadcast_transaction), + broadcast_transactions: Clone::clone(&orig.broadcast_transactions), free: Clone::clone(&orig.free), } } use lightning::chain::chaininterface::BroadcasterInterface as rustBroadcasterInterface; impl rustBroadcasterInterface for BroadcasterInterface { - fn broadcast_transaction(&self, mut tx: &bitcoin::blockdata::transaction::Transaction) { - (self.broadcast_transaction)(self.this_arg, crate::c_types::Transaction::from_bitcoin(tx)) + fn broadcast_transactions(&self, mut txs: &[&bitcoin::blockdata::transaction::Transaction]) { + let mut local_txs = Vec::new(); for item in txs.iter() { local_txs.push( { crate::c_types::Transaction::from_bitcoin((*item)) }); }; + (self.broadcast_transactions)(self.this_arg, local_txs.into()) } } @@ -58,6 +71,11 @@ impl core::ops::Deref for BroadcasterInterface { self } } +impl core::ops::DerefMut for BroadcasterInterface { + fn deref_mut(&mut self) -> &mut Self { + self + } +} /// Calls the free function if one is set #[no_mangle] pub extern "C" fn BroadcasterInterface_free(this_ptr: BroadcasterInterface) { } @@ -68,17 +86,26 @@ impl Drop for BroadcasterInterface { } } } -/// An enum that represents the speed at which we want a transaction to confirm used for feerate +/// An enum that represents the priority at which we want a transaction to confirm used for feerate /// estimation. #[derive(Clone)] #[must_use] #[repr(C)] pub enum ConfirmationTarget { - /// We are happy with this transaction confirming slowly when feerate drops some. + /// We'd like a transaction to confirm in the future, but don't want to commit most of the fees + /// required to do so yet. The remaining fees will come via a Child-Pays-For-Parent (CPFP) fee + /// bump of the transaction. + /// + /// The feerate returned should be the absolute minimum feerate required to enter most node + /// mempools across the network. Note that if you are not able to obtain this feerate estimate, + /// you should likely use the furthest-out estimate allowed by your fee estimator. + MempoolMinimum, + /// We are happy with a transaction confirming slowly, at least within a day or so worth of + /// blocks. Background, - /// We'd like this transaction to confirm without major delay, but 12-18 blocks is fine. + /// We'd like a transaction to confirm without major delayed, i.e., within the next 12-24 blocks. Normal, - /// We'd like this transaction to confirm in the next few blocks. + /// We'd like a transaction to confirm in the next few blocks. HighPriority, } use lightning::chain::chaininterface::ConfirmationTarget as ConfirmationTargetImport; @@ -88,6 +115,7 @@ impl ConfirmationTarget { #[allow(unused)] pub(crate) fn to_native(&self) -> nativeConfirmationTarget { match self { + ConfirmationTarget::MempoolMinimum => nativeConfirmationTarget::MempoolMinimum, ConfirmationTarget::Background => nativeConfirmationTarget::Background, ConfirmationTarget::Normal => nativeConfirmationTarget::Normal, ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority, @@ -96,6 +124,7 @@ impl ConfirmationTarget { #[allow(unused)] pub(crate) fn into_native(self) -> nativeConfirmationTarget { match self { + ConfirmationTarget::MempoolMinimum => nativeConfirmationTarget::MempoolMinimum, ConfirmationTarget::Background => nativeConfirmationTarget::Background, ConfirmationTarget::Normal => nativeConfirmationTarget::Normal, ConfirmationTarget::HighPriority => nativeConfirmationTarget::HighPriority, @@ -104,6 +133,7 @@ impl ConfirmationTarget { #[allow(unused)] pub(crate) fn from_native(native: &nativeConfirmationTarget) -> Self { match native { + nativeConfirmationTarget::MempoolMinimum => ConfirmationTarget::MempoolMinimum, nativeConfirmationTarget::Background => ConfirmationTarget::Background, nativeConfirmationTarget::Normal => ConfirmationTarget::Normal, nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority, @@ -112,6 +142,7 @@ impl ConfirmationTarget { #[allow(unused)] pub(crate) fn native_into(native: nativeConfirmationTarget) -> Self { match native { + nativeConfirmationTarget::MempoolMinimum => ConfirmationTarget::MempoolMinimum, nativeConfirmationTarget::Background => ConfirmationTarget::Background, nativeConfirmationTarget::Normal => ConfirmationTarget::Normal, nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority, @@ -124,6 +155,10 @@ pub extern "C" fn ConfirmationTarget_clone(orig: &ConfirmationTarget) -> Confirm orig.clone() } #[no_mangle] +/// Utility method to constructs a new MempoolMinimum-variant ConfirmationTarget +pub extern "C" fn ConfirmationTarget_mempool_minimum() -> ConfirmationTarget { + ConfirmationTarget::MempoolMinimum} +#[no_mangle] /// Utility method to constructs a new Background-variant ConfirmationTarget pub extern "C" fn ConfirmationTarget_background() -> ConfirmationTarget { ConfirmationTarget::Background} @@ -153,6 +188,11 @@ pub extern "C" fn ConfirmationTarget_eq(a: &ConfirmationTarget, b: &Confirmation /// A trait which should be implemented to provide feerate information on a number of time /// horizons. /// +/// If access to a local mempool is not feasible, feerate estimates should be fetched from a set of +/// third-parties hosting them. Note that this enables them to affect the propagation of your +/// pre-signed transactions at any time and therefore endangers the safety of channels funds. It +/// should be considered carefully as a deployment. +/// /// Note that all of the functions implemented here *must* be reentrant-safe (obviously - they're /// called from inside the library in response to chain events, P2P events, or timer events). #[repr(C)] @@ -168,7 +208,6 @@ pub struct FeeEstimator { /// The following unit conversions can be used to convert to sats/KW: /// * satoshis-per-byte * 250 /// * satoshis-per-kbyte / 4 - #[must_use] pub get_est_sat_per_1000_weight: extern "C" fn (this_arg: *const c_void, confirmation_target: crate::lightning::chain::chaininterface::ConfirmationTarget) -> u32, /// Frees any resources associated with this object given its this_arg pointer. /// Does not need to free the outer struct containing function pointers and may be NULL is no resources need to be freed. @@ -176,8 +215,7 @@ pub struct FeeEstimator { } unsafe impl Send for FeeEstimator {} unsafe impl Sync for FeeEstimator {} -#[no_mangle] -pub(crate) extern "C" fn FeeEstimator_clone_fields(orig: &FeeEstimator) -> FeeEstimator { +pub(crate) fn FeeEstimator_clone_fields(orig: &FeeEstimator) -> FeeEstimator { FeeEstimator { this_arg: orig.this_arg, get_est_sat_per_1000_weight: Clone::clone(&orig.get_est_sat_per_1000_weight), @@ -201,6 +239,11 @@ impl core::ops::Deref for FeeEstimator { self } } +impl core::ops::DerefMut for FeeEstimator { + fn deref_mut(&mut self) -> &mut Self { + self + } +} /// Calls the free function if one is set #[no_mangle] pub extern "C" fn FeeEstimator_free(this_ptr: FeeEstimator) { }