X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Forg%2Fldk%2Fstructs%2FChannelManager.java;h=e3ce61382a98ac6be58f9434692a44106dbe4105;hb=8c0814bf345323be8f16e075a9af0bd43d9400ee;hp=734cca5ac3d5257a89dec72ccb1d91cd2cd7deb8;hpb=b6cc0960eaee5a5bbc7fde79a56152d6dcd649d8;p=ldk-java diff --git a/src/main/java/org/ldk/structs/ChannelManager.java b/src/main/java/org/ldk/structs/ChannelManager.java index 734cca5a..e3ce6138 100644 --- a/src/main/java/org/ldk/structs/ChannelManager.java +++ b/src/main/java/org/ldk/structs/ChannelManager.java @@ -4,6 +4,7 @@ import org.ldk.impl.bindings; import org.ldk.enums.*; import org.ldk.util.*; import java.util.Arrays; +import javax.annotation.Nullable; /** @@ -69,6 +70,7 @@ public class ChannelManager extends CommonBase { */ public static ChannelManager of(FeeEstimator fee_est, Watch chain_monitor, BroadcasterInterface tx_broadcaster, Logger logger, KeysInterface keys_manager, UserConfig config, ChainParameters params) { long ret = bindings.ChannelManager_new(fee_est == null ? 0 : fee_est.ptr, chain_monitor == null ? 0 : chain_monitor.ptr, tx_broadcaster == null ? 0 : tx_broadcaster.ptr, logger == null ? 0 : logger.ptr, keys_manager == null ? 0 : keys_manager.ptr, config == null ? 0 : config.ptr & ~1, params == null ? 0 : params.ptr & ~1); + if (ret < 1024) { return null; } ChannelManager ret_hu_conv = new ChannelManager(null, ret); ret_hu_conv.ptrs_to.add(ret_hu_conv); ret_hu_conv.ptrs_to.add(fee_est); @@ -76,8 +78,6 @@ public class ChannelManager extends CommonBase { ret_hu_conv.ptrs_to.add(tx_broadcaster); ret_hu_conv.ptrs_to.add(logger); ret_hu_conv.ptrs_to.add(keys_manager); - ret_hu_conv.ptrs_to.add(config); - ret_hu_conv.ptrs_to.add(params); return ret_hu_conv; } @@ -86,6 +86,7 @@ public class ChannelManager extends CommonBase { */ public UserConfig get_current_default_configuration() { long ret = bindings.ChannelManager_get_current_default_configuration(this.ptr); + if (ret < 1024) { return null; } UserConfig ret_hu_conv = new UserConfig(null, ret); ret_hu_conv.ptrs_to.add(this); return ret_hu_conv; @@ -105,11 +106,17 @@ public class ChannelManager extends CommonBase { * * Raises APIError::APIMisuseError when channel_value_satoshis > 2**24 or push_msat is * greater than channel_value_satoshis * 1k or channel_value_satoshis is < 1000. + * + * 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 */ - public Result_NoneAPIErrorZ create_channel(byte[] their_network_key, long channel_value_satoshis, long push_msat, long user_id, UserConfig override_config) { + public Result_NoneAPIErrorZ create_channel(byte[] their_network_key, long channel_value_satoshis, long push_msat, long user_id, @Nullable UserConfig override_config) { long ret = bindings.ChannelManager_create_channel(this.ptr, their_network_key, channel_value_satoshis, push_msat, user_id, override_config == null ? 0 : override_config.ptr & ~1); + if (ret < 1024) { return null; } Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret); - this.ptrs_to.add(override_config); return ret_hu_conv; } @@ -154,10 +161,51 @@ public class ChannelManager extends CommonBase { * 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 */ public Result_NoneAPIErrorZ close_channel(byte[] channel_id) { long ret = bindings.ChannelManager_close_channel(this.ptr, channel_id); + if (ret < 1024) { return null; } + Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret); + return ret_hu_conv; + } + + /** + * 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 + */ + public Result_NoneAPIErrorZ close_channel_with_target_feerate(byte[] channel_id, int target_feerate_sats_per_1000_weight) { + long ret = bindings.ChannelManager_close_channel_with_target_feerate(this.ptr, channel_id, target_feerate_sats_per_1000_weight); + if (ret < 1024) { return null; } Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret); return ret_hu_conv; } @@ -168,6 +216,7 @@ public class ChannelManager extends CommonBase { */ public Result_NoneAPIErrorZ force_close_channel(byte[] channel_id) { long ret = bindings.ChannelManager_force_close_channel(this.ptr, channel_id); + if (ret < 1024) { return null; } Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret); return ret_hu_conv; } @@ -220,14 +269,43 @@ public class ChannelManager extends CommonBase { * 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 */ - public Result_NonePaymentSendFailureZ send_payment(Route route, byte[] payment_hash, byte[] payment_secret) { + public Result_NonePaymentSendFailureZ send_payment(Route route, byte[] payment_hash, @Nullable byte[] payment_secret) { long ret = bindings.ChannelManager_send_payment(this.ptr, route == null ? 0 : route.ptr & ~1, payment_hash, payment_secret); + if (ret < 1024) { return null; } Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret); this.ptrs_to.add(route); return ret_hu_conv; } + /** + * 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 + */ + public Result_PaymentHashPaymentSendFailureZ send_spontaneous_payment(Route route, @Nullable byte[] payment_preimage) { + long ret = bindings.ChannelManager_send_spontaneous_payment(this.ptr, route == null ? 0 : route.ptr & ~1, payment_preimage); + if (ret < 1024) { return null; } + Result_PaymentHashPaymentSendFailureZ ret_hu_conv = Result_PaymentHashPaymentSendFailureZ.constr_from_ptr(ret); + this.ptrs_to.add(route); + return ret_hu_conv; + } + /** * Call this upon creation of a funding transaction for the given channel. * @@ -252,6 +330,7 @@ public class ChannelManager extends CommonBase { */ public Result_NoneAPIErrorZ funding_transaction_generated(byte[] temporary_channel_id, byte[] funding_transaction) { long ret = bindings.ChannelManager_funding_transaction_generated(this.ptr, temporary_channel_id, funding_transaction); + if (ret < 1024) { return null; } Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret); return ret_hu_conv; } @@ -277,8 +356,7 @@ public class ChannelManager extends CommonBase { * [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events */ public void broadcast_node_announcement(byte[] rgb, byte[] alias, NetAddress[] addresses) { - bindings.ChannelManager_broadcast_node_announcement(this.ptr, rgb, alias, Arrays.stream(addresses).mapToLong(addresses_conv_12 -> addresses_conv_12.ptr).toArray()); - /* TODO 2 NetAddress */; + bindings.ChannelManager_broadcast_node_announcement(this.ptr, rgb, alias, addresses != null ? Arrays.stream(addresses).mapToLong(addresses_conv_12 -> addresses_conv_12.ptr).toArray() : null); } /** @@ -292,13 +370,16 @@ public class ChannelManager extends CommonBase { } /** - * 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. */ public void timer_tick_occurred() { bindings.ChannelManager_timer_tick_occurred(this.ptr); @@ -389,14 +470,12 @@ public class ChannelManager extends CommonBase { * [`PaymentReceived::payment_preimage`]: events::Event::PaymentReceived::payment_preimage * [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash */ - public TwoTuple create_inbound_payment(Option_u64Z min_value_msat, int invoice_expiry_delta_secs, long user_payment_id) { + public TwoTuple_PaymentHashPaymentSecretZ create_inbound_payment(Option_u64Z min_value_msat, int invoice_expiry_delta_secs, long user_payment_id) { long ret = bindings.ChannelManager_create_inbound_payment(this.ptr, min_value_msat.ptr, invoice_expiry_delta_secs, user_payment_id); - byte[] ret_a = bindings.LDKC2Tuple_PaymentHashPaymentSecretZ_get_a(ret); - byte[] ret_b = bindings.LDKC2Tuple_PaymentHashPaymentSecretZ_get_b(ret); - TwoTuple ret_conv = new TwoTuple(ret_a, ret_b, () -> { - bindings.C2Tuple_PaymentHashPaymentSecretZ_free(ret); - }); - return ret_conv; + if (ret < 1024) { return null; } + TwoTuple_PaymentHashPaymentSecretZ ret_hu_conv = new TwoTuple_PaymentHashPaymentSecretZ(null, ret); + ret_hu_conv.ptrs_to.add(this); + return ret_hu_conv; } /** @@ -410,7 +489,7 @@ public class ChannelManager extends CommonBase { * 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 @@ -444,10 +523,11 @@ public class ChannelManager extends CommonBase { * * [`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 */ public Result_PaymentSecretAPIErrorZ create_inbound_payment_for_hash(byte[] payment_hash, Option_u64Z min_value_msat, int invoice_expiry_delta_secs, long user_payment_id) { long ret = bindings.ChannelManager_create_inbound_payment_for_hash(this.ptr, payment_hash, min_value_msat.ptr, invoice_expiry_delta_secs, user_payment_id); + if (ret < 1024) { return null; } Result_PaymentSecretAPIErrorZ ret_hu_conv = Result_PaymentSecretAPIErrorZ.constr_from_ptr(ret); return ret_hu_conv; } @@ -458,6 +538,7 @@ public class ChannelManager extends CommonBase { */ public MessageSendEventsProvider as_MessageSendEventsProvider() { long ret = bindings.ChannelManager_as_MessageSendEventsProvider(this.ptr); + if (ret < 1024) { return null; } MessageSendEventsProvider ret_hu_conv = new MessageSendEventsProvider(null, ret); ret_hu_conv.ptrs_to.add(this); return ret_hu_conv; @@ -469,6 +550,7 @@ public class ChannelManager extends CommonBase { */ public EventsProvider as_EventsProvider() { long ret = bindings.ChannelManager_as_EventsProvider(this.ptr); + if (ret < 1024) { return null; } EventsProvider ret_hu_conv = new EventsProvider(null, ret); ret_hu_conv.ptrs_to.add(this); return ret_hu_conv; @@ -480,6 +562,7 @@ public class ChannelManager extends CommonBase { */ public Listen as_Listen() { long ret = bindings.ChannelManager_as_Listen(this.ptr); + if (ret < 1024) { return null; } Listen ret_hu_conv = new Listen(null, ret); ret_hu_conv.ptrs_to.add(this); return ret_hu_conv; @@ -491,6 +574,7 @@ public class ChannelManager extends CommonBase { */ public Confirm as_Confirm() { long ret = bindings.ChannelManager_as_Confirm(this.ptr); + if (ret < 1024) { return null; } Confirm ret_hu_conv = new Confirm(null, ret); ret_hu_conv.ptrs_to.add(this); return ret_hu_conv; @@ -517,12 +601,25 @@ public class ChannelManager extends CommonBase { bindings.ChannelManager_await_persistable_update(this.ptr); } + /** + * Gets the latest best block which was connected either via the [`chain::Listen`] or + * [`chain::Confirm`] interfaces. + */ + public BestBlock current_best_block() { + long ret = bindings.ChannelManager_current_best_block(this.ptr); + if (ret < 1024) { return null; } + BestBlock ret_hu_conv = new BestBlock(null, ret); + ret_hu_conv.ptrs_to.add(this); + return ret_hu_conv; + } + /** * Constructs a new ChannelMessageHandler which calls the relevant methods on this_arg. * This copies the `inner` pointer in this_arg and thus the returned ChannelMessageHandler must be freed before this_arg is */ public ChannelMessageHandler as_ChannelMessageHandler() { long ret = bindings.ChannelManager_as_ChannelMessageHandler(this.ptr); + if (ret < 1024) { return null; } ChannelMessageHandler ret_hu_conv = new ChannelMessageHandler(null, ret); ret_hu_conv.ptrs_to.add(this); return ret_hu_conv;