X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning-c-bindings%2Fsrc%2Flightning%2Fchain%2Fchaininterface.rs;h=f4779bb45c2a34165279ef0172ac65bfacfd8fed;hb=HEAD;hp=e27203a87238bee65f1a8c0ee56b4c29720a76ed;hpb=48e3445a78b643c837e7d489d90588eb877d0319;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 e27203a..e85a87e 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; @@ -38,7 +39,7 @@ pub struct BroadcasterInterface { /// 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. @@ -46,8 +47,8 @@ pub struct BroadcasterInterface { } unsafe impl Send for BroadcasterInterface {} unsafe impl Sync for BroadcasterInterface {} -#[no_mangle] -pub(crate) extern "C" fn BroadcasterInterface_clone_fields(orig: &BroadcasterInterface) -> BroadcasterInterface { +#[allow(unused)] +pub(crate) fn BroadcasterInterface_clone_fields(orig: &BroadcasterInterface) -> BroadcasterInterface { BroadcasterInterface { this_arg: orig.this_arg, broadcast_transactions: Clone::clone(&orig.broadcast_transactions), @@ -71,6 +72,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) { } @@ -87,21 +93,92 @@ impl Drop for BroadcasterInterface { #[must_use] #[repr(C)] pub enum ConfirmationTarget { - /// 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. + /// We have some funds available on chain which we need to spend prior to some expiry time at + /// which point our counterparty may be able to steal them. Generally we have in the high tens + /// to low hundreds of blocks to get our transaction on-chain, but we shouldn't risk too low a + /// fee - this should be a relatively high priority feerate. + OnChainSweep, + /// This is the lowest feerate we will allow our channel counterparty to have in an anchor + /// channel in order to close the channel if a channel party goes away. + /// + /// This needs to be sufficient to get into the mempool when the channel needs to + /// be force-closed. Setting too high may result in force-closures if our counterparty attempts + /// to use a lower feerate. Because this is for anchor channels, we can always bump the feerate + /// later; the feerate here only needs to be sufficient to enter the mempool. + /// + /// A good estimate is the expected mempool minimum at the time of force-closure. Obviously this + /// is not an estimate which is very easy to calculate because we do not know the future. Using + /// a simple long-term fee estimate or tracking of the mempool minimum is a good approach to + /// ensure you can always close the channel. A future change to Bitcoin's P2P network + /// (package relay) may obviate the need for this entirely. + MinAllowedAnchorChannelRemoteFee, + /// The lowest feerate we will allow our channel counterparty to have in a non-anchor channel. + /// + /// This is the feerate on the transaction which we (or our counterparty) will broadcast in + /// order to close the channel if a channel party goes away. Setting this value too high will + /// cause immediate force-closures in order to avoid having an unbroadcastable state. + /// + /// This feerate represents the fee we pick now, which must be sufficient to enter a block at an + /// arbitrary time in the future. Obviously this is not an estimate which is very easy to + /// calculate. This can leave channels subject to being unable to close if feerates rise, and in + /// general you should prefer anchor channels to ensure you can increase the feerate when the + /// transactions need broadcasting. + /// + /// Do note some fee estimators round up to the next full sat/vbyte (ie 250 sats per kw), + /// causing occasional issues with feerate disagreements between an initiator that wants a + /// feerate of 1.1 sat/vbyte and a receiver that wants 1.1 rounded up to 2. If your fee + /// estimator rounds subtracting 250 to your desired feerate here can help avoid this issue. + /// + /// [`ChannelConfig::max_dust_htlc_exposure`]: crate::util::config::ChannelConfig::max_dust_htlc_exposure + MinAllowedNonAnchorChannelRemoteFee, + /// This is the feerate on the transaction which we (or our counterparty) will broadcast in + /// order to close the channel if a channel party goes away. + /// + /// This needs to be sufficient to get into the mempool when the channel needs to + /// be force-closed. Setting too low may result in force-closures. Because this is for anchor + /// channels, it can be a low value as we can always bump the feerate later. + /// + /// A good estimate is the expected mempool minimum at the time of force-closure. Obviously this + /// is not an estimate which is very easy to calculate because we do not know the future. Using + /// a simple long-term fee estimate or tracking of the mempool minimum is a good approach to + /// ensure you can always close the channel. A future change to Bitcoin's P2P network + /// (package relay) may obviate the need for this entirely. + AnchorChannelFee, + /// Lightning is built around the ability to broadcast a transaction in the future to close our + /// channel and claim all pending funds. In order to do so, non-anchor channels are built with + /// transactions which we need to be able to broadcast at some point in the future. + /// + /// This feerate represents the fee we pick now, which must be sufficient to enter a block at an + /// arbitrary time in the future. Obviously this is not an estimate which is very easy to + /// calculate, so most lightning nodes use some relatively high-priority feerate using the + /// current mempool. This leaves channels subject to being unable to close if feerates rise, and + /// in general you should prefer anchor channels to ensure you can increase the feerate when the + /// transactions need broadcasting. + /// + /// Since this should represent the feerate of a channel close that does not need fee + /// bumping, this is also used as an upper bound for our attempted feerate when doing cooperative + /// closure of any channel. + NonAnchorChannelFee, + /// When cooperatively closing a channel, this is the minimum feerate we will accept. + /// Recommended at least within a day or so worth of blocks. /// - /// 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 a transaction to confirm without major delayed, i.e., within the next 12-24 blocks. - Normal, - /// We'd like a transaction to confirm in the next few blocks. - HighPriority, + /// This will also be used when initiating a cooperative close of a channel. When closing a + /// channel you can override this fee by using + /// [`ChannelManager::close_channel_with_feerate_and_script`]. + /// + /// [`ChannelManager::close_channel_with_feerate_and_script`]: crate::ln::channelmanager::ChannelManager::close_channel_with_feerate_and_script + ChannelCloseMinimum, + /// The feerate [`OutputSweeper`] will use on transactions spending + /// [`SpendableOutputDescriptor`]s after a channel closure. + /// + /// Generally spending these outputs is safe as long as they eventually confirm, so a value + /// (slightly above) the mempool minimum should suffice. However, as this value will influence + /// how long funds will be unavailable after channel closure, [`FeeEstimator`] implementors + /// might want to choose a higher feerate to regain control over funds faster. + /// + /// [`OutputSweeper`]: crate::util::sweep::OutputSweeper + /// [`SpendableOutputDescriptor`]: crate::sign::SpendableOutputDescriptor + OutputSpendingFee, } use lightning::chain::chaininterface::ConfirmationTarget as ConfirmationTargetImport; pub(crate) type nativeConfirmationTarget = ConfirmationTargetImport; @@ -110,37 +187,50 @@ 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, + ConfirmationTarget::OnChainSweep => nativeConfirmationTarget::OnChainSweep, + ConfirmationTarget::MinAllowedAnchorChannelRemoteFee => nativeConfirmationTarget::MinAllowedAnchorChannelRemoteFee, + ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => nativeConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee, + ConfirmationTarget::AnchorChannelFee => nativeConfirmationTarget::AnchorChannelFee, + ConfirmationTarget::NonAnchorChannelFee => nativeConfirmationTarget::NonAnchorChannelFee, + ConfirmationTarget::ChannelCloseMinimum => nativeConfirmationTarget::ChannelCloseMinimum, + ConfirmationTarget::OutputSpendingFee => nativeConfirmationTarget::OutputSpendingFee, } } #[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, + ConfirmationTarget::OnChainSweep => nativeConfirmationTarget::OnChainSweep, + ConfirmationTarget::MinAllowedAnchorChannelRemoteFee => nativeConfirmationTarget::MinAllowedAnchorChannelRemoteFee, + ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => nativeConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee, + ConfirmationTarget::AnchorChannelFee => nativeConfirmationTarget::AnchorChannelFee, + ConfirmationTarget::NonAnchorChannelFee => nativeConfirmationTarget::NonAnchorChannelFee, + ConfirmationTarget::ChannelCloseMinimum => nativeConfirmationTarget::ChannelCloseMinimum, + ConfirmationTarget::OutputSpendingFee => nativeConfirmationTarget::OutputSpendingFee, } } #[allow(unused)] - pub(crate) fn from_native(native: &nativeConfirmationTarget) -> Self { + pub(crate) fn from_native(native: &ConfirmationTargetImport) -> Self { + let native = unsafe { &*(native as *const _ as *const c_void as *const nativeConfirmationTarget) }; match native { - nativeConfirmationTarget::MempoolMinimum => ConfirmationTarget::MempoolMinimum, - nativeConfirmationTarget::Background => ConfirmationTarget::Background, - nativeConfirmationTarget::Normal => ConfirmationTarget::Normal, - nativeConfirmationTarget::HighPriority => ConfirmationTarget::HighPriority, + nativeConfirmationTarget::OnChainSweep => ConfirmationTarget::OnChainSweep, + nativeConfirmationTarget::MinAllowedAnchorChannelRemoteFee => ConfirmationTarget::MinAllowedAnchorChannelRemoteFee, + nativeConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee, + nativeConfirmationTarget::AnchorChannelFee => ConfirmationTarget::AnchorChannelFee, + nativeConfirmationTarget::NonAnchorChannelFee => ConfirmationTarget::NonAnchorChannelFee, + nativeConfirmationTarget::ChannelCloseMinimum => ConfirmationTarget::ChannelCloseMinimum, + nativeConfirmationTarget::OutputSpendingFee => ConfirmationTarget::OutputSpendingFee, } } #[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, + nativeConfirmationTarget::OnChainSweep => ConfirmationTarget::OnChainSweep, + nativeConfirmationTarget::MinAllowedAnchorChannelRemoteFee => ConfirmationTarget::MinAllowedAnchorChannelRemoteFee, + nativeConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee => ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee, + nativeConfirmationTarget::AnchorChannelFee => ConfirmationTarget::AnchorChannelFee, + nativeConfirmationTarget::NonAnchorChannelFee => ConfirmationTarget::NonAnchorChannelFee, + nativeConfirmationTarget::ChannelCloseMinimum => ConfirmationTarget::ChannelCloseMinimum, + nativeConfirmationTarget::OutputSpendingFee => ConfirmationTarget::OutputSpendingFee, } } } @@ -149,22 +239,47 @@ impl ConfirmationTarget { pub extern "C" fn ConfirmationTarget_clone(orig: &ConfirmationTarget) -> ConfirmationTarget { orig.clone() } +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ConfirmationTarget_clone_void(this_ptr: *const c_void) -> *mut c_void { + Box::into_raw(Box::new(unsafe { (*(this_ptr as *const ConfirmationTarget)).clone() })) as *mut c_void +} +#[allow(unused)] +/// Used only if an object of this type is returned as a trait impl by a method +pub(crate) extern "C" fn ConfirmationTarget_free_void(this_ptr: *mut c_void) { + let _ = unsafe { Box::from_raw(this_ptr as *mut ConfirmationTarget) }; +} +#[no_mangle] +/// Utility method to constructs a new OnChainSweep-variant ConfirmationTarget +pub extern "C" fn ConfirmationTarget_on_chain_sweep() -> ConfirmationTarget { + ConfirmationTarget::OnChainSweep} +#[no_mangle] +/// Utility method to constructs a new MinAllowedAnchorChannelRemoteFee-variant ConfirmationTarget +pub extern "C" fn ConfirmationTarget_min_allowed_anchor_channel_remote_fee() -> ConfirmationTarget { + ConfirmationTarget::MinAllowedAnchorChannelRemoteFee} #[no_mangle] -/// Utility method to constructs a new MempoolMinimum-variant ConfirmationTarget -pub extern "C" fn ConfirmationTarget_mempool_minimum() -> ConfirmationTarget { - ConfirmationTarget::MempoolMinimum} +/// Utility method to constructs a new MinAllowedNonAnchorChannelRemoteFee-variant ConfirmationTarget +pub extern "C" fn ConfirmationTarget_min_allowed_non_anchor_channel_remote_fee() -> ConfirmationTarget { + ConfirmationTarget::MinAllowedNonAnchorChannelRemoteFee} #[no_mangle] -/// Utility method to constructs a new Background-variant ConfirmationTarget -pub extern "C" fn ConfirmationTarget_background() -> ConfirmationTarget { - ConfirmationTarget::Background} +/// Utility method to constructs a new AnchorChannelFee-variant ConfirmationTarget +pub extern "C" fn ConfirmationTarget_anchor_channel_fee() -> ConfirmationTarget { + ConfirmationTarget::AnchorChannelFee} #[no_mangle] -/// Utility method to constructs a new Normal-variant ConfirmationTarget -pub extern "C" fn ConfirmationTarget_normal() -> ConfirmationTarget { - ConfirmationTarget::Normal} +/// Utility method to constructs a new NonAnchorChannelFee-variant ConfirmationTarget +pub extern "C" fn ConfirmationTarget_non_anchor_channel_fee() -> ConfirmationTarget { + ConfirmationTarget::NonAnchorChannelFee} #[no_mangle] -/// Utility method to constructs a new HighPriority-variant ConfirmationTarget -pub extern "C" fn ConfirmationTarget_high_priority() -> ConfirmationTarget { - ConfirmationTarget::HighPriority} +/// Utility method to constructs a new ChannelCloseMinimum-variant ConfirmationTarget +pub extern "C" fn ConfirmationTarget_channel_close_minimum() -> ConfirmationTarget { + ConfirmationTarget::ChannelCloseMinimum} +#[no_mangle] +/// Utility method to constructs a new OutputSpendingFee-variant ConfirmationTarget +pub extern "C" fn ConfirmationTarget_output_spending_fee() -> ConfirmationTarget { + ConfirmationTarget::OutputSpendingFee} +/// Get a string which allows debug introspection of a ConfirmationTarget object +pub extern "C" fn ConfirmationTarget_debug_str_void(o: *const c_void) -> Str { + alloc::format!("{:?}", unsafe { o as *const crate::lightning::chain::chaininterface::ConfirmationTarget }).into()} /// Generates a non-cryptographic 64-bit hash of the ConfirmationTarget. #[no_mangle] pub extern "C" fn ConfirmationTarget_hash(o: &ConfirmationTarget) -> u64 { @@ -190,6 +305,10 @@ pub extern "C" fn ConfirmationTarget_eq(a: &ConfirmationTarget, b: &Confirmation /// /// 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). +/// +/// LDK may generate a substantial number of fee-estimation calls in some cases. You should +/// pre-calculate and cache the fee estimate results to ensure you don't substantially slow HTLC +/// handling. #[repr(C)] pub struct FeeEstimator { /// An opaque pointer which is passed to your function implementations as an argument. @@ -210,8 +329,8 @@ 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 { +#[allow(unused)] +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), @@ -235,6 +354,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) { }