Merge pull request #39 from TheBlueMatt/main
[ldk-c-bindings] / lightning-c-bindings / src / lightning / ln / channelmanager.rs
index 62b675e076969162474168a514915172da21496f..0f5d3cc3ac2a7dc484385eaf85a1d483cf9a3fbe 100644 (file)
@@ -421,6 +421,8 @@ pub extern "C" fn ChannelDetails_set_counterparty(this_ptr: &mut ChannelDetails,
 ///
 /// Note that, if this has been set, `channel_id` will be equivalent to
 /// `funding_txo.unwrap().to_channel_id()`.
+///
+/// Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
 #[no_mangle]
 pub extern "C" fn ChannelDetails_get_funding_txo(this_ptr: &ChannelDetails) -> crate::lightning::chain::transaction::OutPoint {
        let mut inner_val = &mut unsafe { &mut *this_ptr.inner }.funding_txo;
@@ -432,6 +434,8 @@ pub extern "C" fn ChannelDetails_get_funding_txo(this_ptr: &ChannelDetails) -> c
 ///
 /// Note that, if this has been set, `channel_id` will be equivalent to
 /// `funding_txo.unwrap().to_channel_id()`.
+///
+/// Note that val (or a relevant inner pointer) may be NULL or all-0s to represent None
 #[no_mangle]
 pub extern "C" fn ChannelDetails_set_funding_txo(this_ptr: &mut ChannelDetails, mut val: crate::lightning::chain::transaction::OutPoint) {
        let mut local_val = if val.inner.is_null() { None } else { Some( { *unsafe { Box::from_raw(val.take_inner()) } }) };
@@ -889,6 +893,26 @@ pub extern "C" fn PaymentSendFailure_free(this_ptr: PaymentSendFailure) { }
 pub extern "C" fn PaymentSendFailure_clone(orig: &PaymentSendFailure) -> PaymentSendFailure {
        orig.clone()
 }
+#[no_mangle]
+/// Utility method to constructs a new ParameterError-variant PaymentSendFailure
+pub extern "C" fn PaymentSendFailure_parameter_error(a: crate::lightning::util::errors::APIError) -> PaymentSendFailure {
+       PaymentSendFailure::ParameterError(a, )
+}
+#[no_mangle]
+/// Utility method to constructs a new PathParameterError-variant PaymentSendFailure
+pub extern "C" fn PaymentSendFailure_path_parameter_error(a: crate::c_types::derived::CVec_CResult_NoneAPIErrorZZ) -> PaymentSendFailure {
+       PaymentSendFailure::PathParameterError(a, )
+}
+#[no_mangle]
+/// Utility method to constructs a new AllFailedRetrySafe-variant PaymentSendFailure
+pub extern "C" fn PaymentSendFailure_all_failed_retry_safe(a: crate::c_types::derived::CVec_APIErrorZ) -> PaymentSendFailure {
+       PaymentSendFailure::AllFailedRetrySafe(a, )
+}
+#[no_mangle]
+/// Utility method to constructs a new PartialFailure-variant PaymentSendFailure
+pub extern "C" fn PaymentSendFailure_partial_failure(a: crate::c_types::derived::CVec_CResult_NoneAPIErrorZZ) -> PaymentSendFailure {
+       PaymentSendFailure::PartialFailure(a, )
+}
 /// Constructs a new ChannelManager to hold several channels and route between them.
 ///
 /// This is the main \"logic hub\" for all channel-related actions, and implements
@@ -933,6 +957,8 @@ pub extern "C" fn ChannelManager_get_current_default_configuration(this_arg: &Ch
 /// Note that we do not check if you are currently connected to the given peer. If no
 /// connection is available, the outbound `open_channel` message may fail to send, resulting in
 /// the channel eventually being silently forgotten.
+///
+/// Note that override_config (or a relevant inner pointer) may be NULL or all-0s to represent None
 #[must_use]
 #[no_mangle]
 pub extern "C" fn ChannelManager_create_channel(this_arg: &ChannelManager, mut their_network_key: crate::c_types::PublicKey, mut channel_value_satoshis: u64, mut push_msat: u64, mut user_id: u64, mut override_config: crate::lightning::util::config::UserConfig) -> crate::c_types::derived::CResult_NoneAPIErrorZ {
@@ -970,7 +996,19 @@ pub extern "C" fn ChannelManager_list_usable_channels(this_arg: &ChannelManager)
 /// will be accepted on the given channel, and after additional timeout/the closing of all
 /// pending HTLCs, the channel will be closed on chain.
 ///
+///  * If we are the channel initiator, we will pay between our [`Background`] and
+///    [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`Normal`] fee
+///    estimate.
+///  * If our counterparty is the channel initiator, we will require a channel closing
+///    transaction feerate of at least our [`Background`] feerate or the feerate which
+///    would appear on a force-closure transaction, whichever is lower. We will allow our
+///    counterparty to pay as much fee as they'd like, however.
+///
 /// May generate a SendShutdown message event on success, which should be relayed.
+///
+/// [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis
+/// [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background
+/// [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal
 #[must_use]
 #[no_mangle]
 pub extern "C" fn ChannelManager_close_channel(this_arg: &ChannelManager, channel_id: *const [u8; 32]) -> crate::c_types::derived::CResult_NoneAPIErrorZ {
@@ -979,6 +1017,33 @@ pub extern "C" fn ChannelManager_close_channel(this_arg: &ChannelManager, channe
        local_ret
 }
 
+/// Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
+/// will be accepted on the given channel, and after additional timeout/the closing of all
+/// pending HTLCs, the channel will be closed on chain.
+///
+/// `target_feerate_sat_per_1000_weight` has different meanings depending on if we initiated
+/// the channel being closed or not:
+///  * If we are the channel initiator, we will pay at least this feerate on the closing
+///    transaction. The upper-bound is set by
+///    [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`Normal`] fee
+///    estimate (or `target_feerate_sat_per_1000_weight`, if it is greater).
+///  * If our counterparty is the channel initiator, we will refuse to accept a channel closure
+///    transaction feerate below `target_feerate_sat_per_1000_weight` (or the feerate which
+///    will appear on a force-closure transaction, whichever is lower).
+///
+/// May generate a SendShutdown message event on success, which should be relayed.
+///
+/// [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis
+/// [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background
+/// [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal
+#[must_use]
+#[no_mangle]
+pub extern "C" fn ChannelManager_close_channel_with_target_feerate(this_arg: &ChannelManager, channel_id: *const [u8; 32], mut target_feerate_sats_per_1000_weight: u32) -> crate::c_types::derived::CResult_NoneAPIErrorZ {
+       let mut ret = unsafe { &*this_arg.inner }.close_channel_with_target_feerate(unsafe { &*channel_id}, target_feerate_sats_per_1000_weight);
+       let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::util::errors::APIError::native_into(e) }).into() };
+       local_ret
+}
+
 /// Force closes a channel, immediately broadcasting the latest local commitment transaction to
 /// the chain and rejecting new HTLCs on the given channel. Fails if channel_id is unknown to the manager.
 #[must_use]
@@ -1035,6 +1100,8 @@ pub extern "C" fn ChannelManager_force_close_all_channels(this_arg: &ChannelMana
 /// If a payment_secret *is* provided, we assume that the invoice had the payment_secret feature
 /// bit set (either as required or as available). If multiple paths are present in the Route,
 /// we assume the invoice had the basic_mpp feature set.
+///
+/// Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
 #[must_use]
 #[no_mangle]
 pub extern "C" fn ChannelManager_send_payment(this_arg: &ChannelManager, route: &crate::lightning::routing::router::Route, mut payment_hash: crate::c_types::ThirtyTwoBytes, mut payment_secret: crate::c_types::ThirtyTwoBytes) -> crate::c_types::derived::CResult_NonePaymentSendFailureZ {
@@ -1044,6 +1111,31 @@ pub extern "C" fn ChannelManager_send_payment(this_arg: &ChannelManager, route:
        local_ret
 }
 
+/// Send a spontaneous payment, which is a payment that does not require the recipient to have
+/// generated an invoice. Optionally, you may specify the preimage. If you do choose to specify
+/// the preimage, it must be a cryptographically secure random value that no intermediate node
+/// would be able to guess -- otherwise, an intermediate node may claim the payment and it will
+/// never reach the recipient.
+///
+/// See [`send_payment`] documentation for more details on the return value of this function.
+///
+/// Similar to regular payments, you MUST NOT reuse a `payment_preimage` value. See
+/// [`send_payment`] for more information about the risks of duplicate preimage usage.
+///
+/// Note that `route` must have exactly one path.
+///
+/// [`send_payment`]: Self::send_payment
+///
+/// Note that payment_preimage (or a relevant inner pointer) may be NULL or all-0s to represent None
+#[must_use]
+#[no_mangle]
+pub extern "C" fn ChannelManager_send_spontaneous_payment(this_arg: &ChannelManager, route: &crate::lightning::routing::router::Route, mut payment_preimage: crate::c_types::ThirtyTwoBytes) -> crate::c_types::derived::CResult_PaymentHashPaymentSendFailureZ {
+       let mut local_payment_preimage = if payment_preimage.data == [0; 32] { None } else { Some( { ::lightning::ln::PaymentPreimage(payment_preimage.data) }) };
+       let mut ret = unsafe { &*this_arg.inner }.send_spontaneous_payment(unsafe { &*route.inner }, local_payment_preimage);
+       let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::ThirtyTwoBytes { data: o.0 } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning::ln::channelmanager::PaymentSendFailure::native_into(e) }).into() };
+       local_ret
+}
+
 /// Call this upon creation of a funding transaction for the given channel.
 ///
 /// Returns an [`APIError::APIMisuseError`] if the funding_transaction spent non-SegWit outputs
@@ -1105,13 +1197,16 @@ pub extern "C" fn ChannelManager_process_pending_htlc_forwards(this_arg: &Channe
        unsafe { &*this_arg.inner }.process_pending_htlc_forwards()
 }
 
-/// If a peer is disconnected we mark any channels with that peer as 'disabled'.
-/// After some time, if channels are still disabled we need to broadcast a ChannelUpdate
-/// to inform the network about the uselessness of these channels.
+/// Performs actions which should happen on startup and roughly once per minute thereafter.
 ///
-/// This method handles all the details, and must be called roughly once per minute.
+/// This currently includes:
+///  * Increasing or decreasing the on-chain feerate estimates for our outbound channels,
+///  * Broadcasting `ChannelUpdate` messages if we've been disconnected from our peer for more
+///    than a minute, informing the network that they should no longer attempt to route over
+///    the channel.
 ///
-/// Note that in some rare cases this may generate a `chain::Watch::update_channel` call.
+/// Note that this may cause reentrancy through `chain::Watch::update_channel` calls or feerate
+/// estimate fetches.
 #[no_mangle]
 pub extern "C" fn ChannelManager_timer_tick_occurred(this_arg: &ChannelManager) {
        unsafe { &*this_arg.inner }.timer_tick_occurred()
@@ -1217,7 +1312,7 @@ pub extern "C" fn ChannelManager_create_inbound_payment(this_arg: &ChannelManage
 /// The [`PaymentHash`] (and corresponding [`PaymentPreimage`]) must be globally unique. This
 /// method may return an Err if another payment with the same payment_hash is still pending.
 ///
-/// `user_payment_id` will be provided back in [`PaymentReceived::user_payment_id`] events to
+/// `user_payment_id` will be provided back in [`PaymentPurpose::InvoicePayment::user_payment_id`] events to
 /// allow tracking of which events correspond with which calls to this and
 /// [`create_inbound_payment`]. `user_payment_id` has no meaning inside of LDK, it is simply
 /// copied to events and otherwise ignored. It may be used to correlate PaymentReceived events
@@ -1251,7 +1346,7 @@ pub extern "C" fn ChannelManager_create_inbound_payment(this_arg: &ChannelManage
 ///
 /// [`create_inbound_payment`]: Self::create_inbound_payment
 /// [`PaymentReceived`]: events::Event::PaymentReceived
-/// [`PaymentReceived::user_payment_id`]: events::Event::PaymentReceived::user_payment_id
+/// [`PaymentPurpose::InvoicePayment::user_payment_id`]: events::PaymentPurpose::InvoicePayment::user_payment_id
 #[must_use]
 #[no_mangle]
 pub extern "C" fn ChannelManager_create_inbound_payment_for_hash(this_arg: &ChannelManager, mut payment_hash: crate::c_types::ThirtyTwoBytes, mut min_value_msat: crate::c_types::derived::COption_u64Z, mut invoice_expiry_delta_secs: u32, mut user_payment_id: u64) -> crate::c_types::derived::CResult_PaymentSecretAPIErrorZ {