1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
12 * Manager which keeps track of a number of channels and sends messages to the appropriate
13 * channel, also tracking HTLC preimages and forwarding onion packets appropriately.
15 * Implements ChannelMessageHandler, handling the multi-channel parts and passing things through
16 * to individual Channels.
18 * Implements Writeable to write out all channel state to disk. Implies peer_disconnected() for
19 * all peers during write/read (though does not modify this instance, only the instance being
20 * serialized). This will result in any channels which have not yet exchanged funding_created (ie
21 * called funding_transaction_generated for outbound channels).
23 * Note that you can be a bit lazier about writing out ChannelManager than you can be with
24 * ChannelMonitors. With ChannelMonitors you MUST write each monitor update out to disk before
25 * returning from chain::Watch::watch_/update_channel, with ChannelManagers, writing updates
26 * happens out-of-band (and will prevent any other ChannelManager operations from occurring during
27 * the serialization process). If the deserialized version is out-of-date compared to the
28 * ChannelMonitors passed by reference to read(), those channels will be force-closed based on the
29 * ChannelMonitor state and no funds will be lost (mod on-chain transaction fees).
31 * Note that the deserializer is only implemented for (BlockHash, ChannelManager), which
32 * tells you the last block hash which was block_connect()ed. You MUST rescan any blocks along
33 * the \"reorg path\" (ie call block_disconnected() until you get to a common block and then call
34 * block_connected() to step towards your best block) upon deserialization before using the
37 * Note that ChannelManager is responsible for tracking liveness of its channels and generating
38 * ChannelUpdate messages informing peers that the channel is temporarily disabled. To avoid
39 * spam due to quick disconnection/reconnection, updates are not sent until the channel has been
40 * offline for a full minute. In order to track this, you must call
41 * timer_tick_occurred roughly once per minute, though it doesn't have to be perfect.
43 * Rather than using a plain ChannelManager, it is preferable to use either a SimpleArcChannelManager
44 * a SimpleRefChannelManager, for conciseness. See their documentation for more details, but
45 * essentially you should default to using a SimpleRefChannelManager, and use a
46 * SimpleArcChannelManager when you require a ChannelManager with a static lifetime, such as when
47 * you're using lightning-net-tokio.
49 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
50 public class ChannelManager extends CommonBase {
51 ChannelManager(Object _dummy, long ptr) { super(ptr); }
52 @Override @SuppressWarnings("deprecation")
53 protected void finalize() throws Throwable {
55 if (ptr != 0) { bindings.ChannelManager_free(ptr); }
59 * Constructs a new ChannelManager to hold several channels and route between them.
61 * This is the main \"logic hub\" for all channel-related actions, and implements
62 * ChannelMessageHandler.
64 * Non-proportional fees are fixed according to our risk using the provided fee estimator.
66 * panics if channel_value_satoshis is >= `MAX_FUNDING_SATOSHIS`!
68 * Users need to notify the new ChannelManager when a new block is connected or
69 * disconnected using its `block_connected` and `block_disconnected` methods, starting
70 * from after `params.latest_hash`.
72 public static ChannelManager of(FeeEstimator fee_est, Watch chain_monitor, BroadcasterInterface tx_broadcaster, Logger logger, KeysInterface keys_manager, UserConfig config, ChainParameters params) {
73 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);
74 Reference.reachabilityFence(fee_est);
75 Reference.reachabilityFence(chain_monitor);
76 Reference.reachabilityFence(tx_broadcaster);
77 Reference.reachabilityFence(logger);
78 Reference.reachabilityFence(keys_manager);
79 Reference.reachabilityFence(config);
80 Reference.reachabilityFence(params);
81 if (ret >= 0 && ret <= 4096) { return null; }
82 ChannelManager ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new ChannelManager(null, ret); }
83 ret_hu_conv.ptrs_to.add(ret_hu_conv);
84 ret_hu_conv.ptrs_to.add(fee_est);
85 ret_hu_conv.ptrs_to.add(chain_monitor);
86 ret_hu_conv.ptrs_to.add(tx_broadcaster);
87 ret_hu_conv.ptrs_to.add(logger);
88 ret_hu_conv.ptrs_to.add(keys_manager);
93 * Gets the current configuration applied to all new channels, as
95 public UserConfig get_current_default_configuration() {
96 long ret = bindings.ChannelManager_get_current_default_configuration(this.ptr);
97 Reference.reachabilityFence(this);
98 if (ret >= 0 && ret <= 4096) { return null; }
99 UserConfig ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new UserConfig(null, ret); }
100 ret_hu_conv.ptrs_to.add(this);
105 * Creates a new outbound channel to the given remote node and with the given value.
107 * `user_channel_id` will be provided back as in
108 * [`Event::FundingGenerationReady::user_channel_id`] to allow tracking of which events
109 * correspond with which `create_channel` call. Note that the `user_channel_id` defaults to 0
110 * for inbound channels, so you may wish to avoid using 0 for `user_channel_id` here.
111 * `user_channel_id` has no meaning inside of LDK, it is simply copied to events and otherwise
114 * Raises [`APIError::APIMisuseError`] when `channel_value_satoshis` > 2**24 or `push_msat` is
115 * greater than `channel_value_satoshis * 1k` or `channel_value_satoshis < 1000`.
117 * Note that we do not check if you are currently connected to the given peer. If no
118 * connection is available, the outbound `open_channel` message may fail to send, resulting in
119 * the channel eventually being silently forgotten (dropped on reload).
121 * Returns the new Channel's temporary `channel_id`. This ID will appear as
122 * [`Event::FundingGenerationReady::temporary_channel_id`] and in
123 * [`ChannelDetails::channel_id`] until after
124 * [`ChannelManager::funding_transaction_generated`] is called, swapping the Channel's ID for
125 * one derived from the funding transaction's TXID. If the counterparty rejects the channel
126 * immediately, this temporary ID will appear in [`Event::ChannelClosed::channel_id`].
128 * [`Event::FundingGenerationReady::user_channel_id`]: events::Event::FundingGenerationReady::user_channel_id
129 * [`Event::FundingGenerationReady::temporary_channel_id`]: events::Event::FundingGenerationReady::temporary_channel_id
130 * [`Event::ChannelClosed::channel_id`]: events::Event::ChannelClosed::channel_id
132 * Note that override_config (or a relevant inner pointer) may be NULL or all-0s to represent None
134 public Result__u832APIErrorZ create_channel(byte[] their_network_key, long channel_value_satoshis, long push_msat, long user_channel_id, @Nullable UserConfig override_config) {
135 long ret = bindings.ChannelManager_create_channel(this.ptr, InternalUtils.check_arr_len(their_network_key, 33), channel_value_satoshis, push_msat, user_channel_id, override_config == null ? 0 : override_config.ptr & ~1);
136 Reference.reachabilityFence(this);
137 Reference.reachabilityFence(their_network_key);
138 Reference.reachabilityFence(channel_value_satoshis);
139 Reference.reachabilityFence(push_msat);
140 Reference.reachabilityFence(user_channel_id);
141 Reference.reachabilityFence(override_config);
142 if (ret >= 0 && ret <= 4096) { return null; }
143 Result__u832APIErrorZ ret_hu_conv = Result__u832APIErrorZ.constr_from_ptr(ret);
148 * Gets the list of open channels, in random order. See ChannelDetail field documentation for
151 public ChannelDetails[] list_channels() {
152 long[] ret = bindings.ChannelManager_list_channels(this.ptr);
153 Reference.reachabilityFence(this);
154 int ret_conv_16_len = ret.length;
155 ChannelDetails[] ret_conv_16_arr = new ChannelDetails[ret_conv_16_len];
156 for (int q = 0; q < ret_conv_16_len; q++) {
157 long ret_conv_16 = ret[q];
158 ChannelDetails ret_conv_16_hu_conv = null; if (ret_conv_16 < 0 || ret_conv_16 > 4096) { ret_conv_16_hu_conv = new ChannelDetails(null, ret_conv_16); }
159 ret_conv_16_hu_conv.ptrs_to.add(this);
160 ret_conv_16_arr[q] = ret_conv_16_hu_conv;
162 return ret_conv_16_arr;
166 * Gets the list of usable channels, in random order. Useful as an argument to
167 * get_route to ensure non-announced channels are used.
169 * These are guaranteed to have their [`ChannelDetails::is_usable`] value set to true, see the
170 * documentation for [`ChannelDetails::is_usable`] for more info on exactly what the criteria
173 public ChannelDetails[] list_usable_channels() {
174 long[] ret = bindings.ChannelManager_list_usable_channels(this.ptr);
175 Reference.reachabilityFence(this);
176 int ret_conv_16_len = ret.length;
177 ChannelDetails[] ret_conv_16_arr = new ChannelDetails[ret_conv_16_len];
178 for (int q = 0; q < ret_conv_16_len; q++) {
179 long ret_conv_16 = ret[q];
180 ChannelDetails ret_conv_16_hu_conv = null; if (ret_conv_16 < 0 || ret_conv_16 > 4096) { ret_conv_16_hu_conv = new ChannelDetails(null, ret_conv_16); }
181 ret_conv_16_hu_conv.ptrs_to.add(this);
182 ret_conv_16_arr[q] = ret_conv_16_hu_conv;
184 return ret_conv_16_arr;
188 * Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
189 * will be accepted on the given channel, and after additional timeout/the closing of all
190 * pending HTLCs, the channel will be closed on chain.
192 * If we are the channel initiator, we will pay between our [`Background`] and
193 * [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`Normal`] fee
195 * If our counterparty is the channel initiator, we will require a channel closing
196 * transaction feerate of at least our [`Background`] feerate or the feerate which
197 * would appear on a force-closure transaction, whichever is lower. We will allow our
198 * counterparty to pay as much fee as they'd like, however.
200 * May generate a SendShutdown message event on success, which should be relayed.
202 * [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis
203 * [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background
204 * [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal
206 public Result_NoneAPIErrorZ close_channel(byte[] channel_id) {
207 long ret = bindings.ChannelManager_close_channel(this.ptr, InternalUtils.check_arr_len(channel_id, 32));
208 Reference.reachabilityFence(this);
209 Reference.reachabilityFence(channel_id);
210 if (ret >= 0 && ret <= 4096) { return null; }
211 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
216 * Begins the process of closing a channel. After this call (plus some timeout), no new HTLCs
217 * will be accepted on the given channel, and after additional timeout/the closing of all
218 * pending HTLCs, the channel will be closed on chain.
220 * `target_feerate_sat_per_1000_weight` has different meanings depending on if we initiated
221 * the channel being closed or not:
222 * If we are the channel initiator, we will pay at least this feerate on the closing
223 * transaction. The upper-bound is set by
224 * [`ChannelConfig::force_close_avoidance_max_fee_satoshis`] plus our [`Normal`] fee
225 * estimate (or `target_feerate_sat_per_1000_weight`, if it is greater).
226 * If our counterparty is the channel initiator, we will refuse to accept a channel closure
227 * transaction feerate below `target_feerate_sat_per_1000_weight` (or the feerate which
228 * will appear on a force-closure transaction, whichever is lower).
230 * May generate a SendShutdown message event on success, which should be relayed.
232 * [`ChannelConfig::force_close_avoidance_max_fee_satoshis`]: crate::util::config::ChannelConfig::force_close_avoidance_max_fee_satoshis
233 * [`Background`]: crate::chain::chaininterface::ConfirmationTarget::Background
234 * [`Normal`]: crate::chain::chaininterface::ConfirmationTarget::Normal
236 public Result_NoneAPIErrorZ close_channel_with_target_feerate(byte[] channel_id, int target_feerate_sats_per_1000_weight) {
237 long ret = bindings.ChannelManager_close_channel_with_target_feerate(this.ptr, InternalUtils.check_arr_len(channel_id, 32), target_feerate_sats_per_1000_weight);
238 Reference.reachabilityFence(this);
239 Reference.reachabilityFence(channel_id);
240 Reference.reachabilityFence(target_feerate_sats_per_1000_weight);
241 if (ret >= 0 && ret <= 4096) { return null; }
242 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
247 * Force closes a channel, immediately broadcasting the latest local commitment transaction to
248 * the chain and rejecting new HTLCs on the given channel. Fails if channel_id is unknown to the manager.
250 public Result_NoneAPIErrorZ force_close_channel(byte[] channel_id) {
251 long ret = bindings.ChannelManager_force_close_channel(this.ptr, InternalUtils.check_arr_len(channel_id, 32));
252 Reference.reachabilityFence(this);
253 Reference.reachabilityFence(channel_id);
254 if (ret >= 0 && ret <= 4096) { return null; }
255 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
260 * Force close all channels, immediately broadcasting the latest local commitment transaction
261 * for each to the chain and rejecting new HTLCs on each.
263 public void force_close_all_channels() {
264 bindings.ChannelManager_force_close_all_channels(this.ptr);
265 Reference.reachabilityFence(this);
269 * Sends a payment along a given route.
271 * Value parameters are provided via the last hop in route, see documentation for RouteHop
272 * fields for more info.
274 * Note that if the payment_hash already exists elsewhere (eg you're sending a duplicative
275 * payment), we don't do anything to stop you! We always try to ensure that if the provided
276 * next hop knows the preimage to payment_hash they can claim an additional amount as
277 * specified in the last hop in the route! Thus, you should probably do your own
278 * payment_preimage tracking (which you should already be doing as they represent \"proof of
279 * payment\") and prevent double-sends yourself.
281 * May generate SendHTLCs message(s) event on success, which should be relayed.
283 * Each path may have a different return value, and PaymentSendValue may return a Vec with
284 * each entry matching the corresponding-index entry in the route paths, see
285 * PaymentSendFailure for more info.
287 * In general, a path may raise:
288 * APIError::RouteError when an invalid route or forwarding parameter (cltv_delta, fee,
289 * node public key) is specified.
290 * APIError::ChannelUnavailable if the next-hop channel is not available for updates
291 * (including due to previous monitor update failure or new permanent monitor update
293 * APIError::MonitorUpdateFailed if a new monitor update failure prevented sending the
296 * Note that depending on the type of the PaymentSendFailure the HTLC may have been
297 * irrevocably committed to on our end. In such a case, do NOT retry the payment with a
298 * different route unless you intend to pay twice!
300 * payment_secret is unrelated to payment_hash (or PaymentPreimage) and exists to authenticate
301 * the sender to the recipient and prevent payment-probing (deanonymization) attacks. For
302 * newer nodes, it will be provided to you in the invoice. If you do not have one, the Route
303 * must not contain multiple paths as multi-path payments require a recipient-provided
305 * If a payment_secret *is* provided, we assume that the invoice had the payment_secret feature
306 * bit set (either as required or as available). If multiple paths are present in the Route,
307 * we assume the invoice had the basic_mpp feature set.
309 * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
311 public Result_PaymentIdPaymentSendFailureZ send_payment(Route route, byte[] payment_hash, @Nullable byte[] payment_secret) {
312 long ret = bindings.ChannelManager_send_payment(this.ptr, route == null ? 0 : route.ptr & ~1, InternalUtils.check_arr_len(payment_hash, 32), InternalUtils.check_arr_len(payment_secret, 32));
313 Reference.reachabilityFence(this);
314 Reference.reachabilityFence(route);
315 Reference.reachabilityFence(payment_hash);
316 Reference.reachabilityFence(payment_secret);
317 if (ret >= 0 && ret <= 4096) { return null; }
318 Result_PaymentIdPaymentSendFailureZ ret_hu_conv = Result_PaymentIdPaymentSendFailureZ.constr_from_ptr(ret);
319 this.ptrs_to.add(route);
324 * Retries a payment along the given [`Route`].
326 * Errors returned are a superset of those returned from [`send_payment`], so see
327 * [`send_payment`] documentation for more details on errors. This method will also error if the
328 * retry amount puts the payment more than 10% over the payment's total amount, if the payment
329 * for the given `payment_id` cannot be found (likely due to timeout or success), or if
330 * further retries have been disabled with [`abandon_payment`].
332 * [`send_payment`]: [`ChannelManager::send_payment`]
333 * [`abandon_payment`]: [`ChannelManager::abandon_payment`]
335 public Result_NonePaymentSendFailureZ retry_payment(Route route, byte[] payment_id) {
336 long ret = bindings.ChannelManager_retry_payment(this.ptr, route == null ? 0 : route.ptr & ~1, InternalUtils.check_arr_len(payment_id, 32));
337 Reference.reachabilityFence(this);
338 Reference.reachabilityFence(route);
339 Reference.reachabilityFence(payment_id);
340 if (ret >= 0 && ret <= 4096) { return null; }
341 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
342 this.ptrs_to.add(route);
347 * Signals that no further retries for the given payment will occur.
349 * After this method returns, any future calls to [`retry_payment`] for the given `payment_id`
350 * will fail with [`PaymentSendFailure::ParameterError`]. If no such event has been generated,
351 * an [`Event::PaymentFailed`] event will be generated as soon as there are no remaining
352 * pending HTLCs for this payment.
354 * Note that calling this method does *not* prevent a payment from succeeding. You must still
355 * wait until you receive either a [`Event::PaymentFailed`] or [`Event::PaymentSent`] event to
356 * determine the ultimate status of a payment.
358 * [`retry_payment`]: Self::retry_payment
359 * [`Event::PaymentFailed`]: events::Event::PaymentFailed
360 * [`Event::PaymentSent`]: events::Event::PaymentSent
362 public void abandon_payment(byte[] payment_id) {
363 bindings.ChannelManager_abandon_payment(this.ptr, InternalUtils.check_arr_len(payment_id, 32));
364 Reference.reachabilityFence(this);
365 Reference.reachabilityFence(payment_id);
369 * Send a spontaneous payment, which is a payment that does not require the recipient to have
370 * generated an invoice. Optionally, you may specify the preimage. If you do choose to specify
371 * the preimage, it must be a cryptographically secure random value that no intermediate node
372 * would be able to guess -- otherwise, an intermediate node may claim the payment and it will
373 * never reach the recipient.
375 * See [`send_payment`] documentation for more details on the return value of this function.
377 * Similar to regular payments, you MUST NOT reuse a `payment_preimage` value. See
378 * [`send_payment`] for more information about the risks of duplicate preimage usage.
380 * Note that `route` must have exactly one path.
382 * [`send_payment`]: Self::send_payment
384 * Note that payment_preimage (or a relevant inner pointer) may be NULL or all-0s to represent None
386 public Result_C2Tuple_PaymentHashPaymentIdZPaymentSendFailureZ send_spontaneous_payment(Route route, @Nullable byte[] payment_preimage) {
387 long ret = bindings.ChannelManager_send_spontaneous_payment(this.ptr, route == null ? 0 : route.ptr & ~1, InternalUtils.check_arr_len(payment_preimage, 32));
388 Reference.reachabilityFence(this);
389 Reference.reachabilityFence(route);
390 Reference.reachabilityFence(payment_preimage);
391 if (ret >= 0 && ret <= 4096) { return null; }
392 Result_C2Tuple_PaymentHashPaymentIdZPaymentSendFailureZ ret_hu_conv = Result_C2Tuple_PaymentHashPaymentIdZPaymentSendFailureZ.constr_from_ptr(ret);
393 this.ptrs_to.add(route);
398 * Call this upon creation of a funding transaction for the given channel.
400 * Returns an [`APIError::APIMisuseError`] if the funding_transaction spent non-SegWit outputs
401 * or if no output was found which matches the parameters in [`Event::FundingGenerationReady`].
403 * Returns [`APIError::ChannelUnavailable`] if a funding transaction has already been provided
404 * for the channel or if the channel has been closed as indicated by [`Event::ChannelClosed`].
406 * May panic if the output found in the funding transaction is duplicative with some other
407 * channel (note that this should be trivially prevented by using unique funding transaction
410 * Do NOT broadcast the funding transaction yourself. When we have safely received our
411 * counterparty's signature the funding transaction will automatically be broadcast via the
412 * [`BroadcasterInterface`] provided when this `ChannelManager` was constructed.
414 * Note that this includes RBF or similar transaction replacement strategies - lightning does
415 * not currently support replacing a funding transaction on an existing channel. Instead,
416 * create a new channel with a conflicting funding transaction.
418 * [`Event::FundingGenerationReady`]: crate::util::events::Event::FundingGenerationReady
419 * [`Event::ChannelClosed`]: crate::util::events::Event::ChannelClosed
421 public Result_NoneAPIErrorZ funding_transaction_generated(byte[] temporary_channel_id, byte[] funding_transaction) {
422 long ret = bindings.ChannelManager_funding_transaction_generated(this.ptr, InternalUtils.check_arr_len(temporary_channel_id, 32), funding_transaction);
423 Reference.reachabilityFence(this);
424 Reference.reachabilityFence(temporary_channel_id);
425 Reference.reachabilityFence(funding_transaction);
426 if (ret >= 0 && ret <= 4096) { return null; }
427 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
432 * Regenerates channel_announcements and generates a signed node_announcement from the given
433 * arguments, providing them in corresponding events via
434 * [`get_and_clear_pending_msg_events`], if at least one public channel has been confirmed
435 * on-chain. This effectively re-broadcasts all channel announcements and sends our node
436 * announcement to ensure that the lightning P2P network is aware of the channels we have and
437 * our network addresses.
439 * `rgb` is a node \"color\" and `alias` is a printable human-readable string to describe this
440 * node to humans. They carry no in-protocol meaning.
442 * `addresses` represent the set (possibly empty) of socket addresses on which this node
443 * accepts incoming connections. These will be included in the node_announcement, publicly
444 * tying these addresses together and to this node. If you wish to preserve user privacy,
445 * addresses should likely contain only Tor Onion addresses.
447 * Panics if `addresses` is absurdly large (more than 500).
449 * [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events
451 public void broadcast_node_announcement(byte[] rgb, byte[] alias, NetAddress[] addresses) {
452 bindings.ChannelManager_broadcast_node_announcement(this.ptr, InternalUtils.check_arr_len(rgb, 3), InternalUtils.check_arr_len(alias, 32), addresses != null ? Arrays.stream(addresses).mapToLong(addresses_conv_12 -> addresses_conv_12.ptr).toArray() : null);
453 Reference.reachabilityFence(this);
454 Reference.reachabilityFence(rgb);
455 Reference.reachabilityFence(alias);
456 Reference.reachabilityFence(addresses);
460 * Processes HTLCs which are pending waiting on random forward delay.
462 * Should only really ever be called in response to a PendingHTLCsForwardable event.
463 * Will likely generate further events.
465 public void process_pending_htlc_forwards() {
466 bindings.ChannelManager_process_pending_htlc_forwards(this.ptr);
467 Reference.reachabilityFence(this);
471 * Performs actions which should happen on startup and roughly once per minute thereafter.
473 * This currently includes:
474 * Increasing or decreasing the on-chain feerate estimates for our outbound channels,
475 * Broadcasting `ChannelUpdate` messages if we've been disconnected from our peer for more
476 * than a minute, informing the network that they should no longer attempt to route over
479 * Note that this may cause reentrancy through `chain::Watch::update_channel` calls or feerate
482 public void timer_tick_occurred() {
483 bindings.ChannelManager_timer_tick_occurred(this.ptr);
484 Reference.reachabilityFence(this);
488 * Indicates that the preimage for payment_hash is unknown or the received amount is incorrect
489 * after a PaymentReceived event, failing the HTLC back to its origin and freeing resources
490 * along the path (including in our own channel on which we received it).
491 * Returns false if no payment was found to fail backwards, true if the process of failing the
492 * HTLC backwards has been started.
494 public boolean fail_htlc_backwards(byte[] payment_hash) {
495 boolean ret = bindings.ChannelManager_fail_htlc_backwards(this.ptr, InternalUtils.check_arr_len(payment_hash, 32));
496 Reference.reachabilityFence(this);
497 Reference.reachabilityFence(payment_hash);
502 * Provides a payment preimage in response to [`Event::PaymentReceived`], generating any
503 * [`MessageSendEvent`]s needed to claim the payment.
505 * Note that if you did not set an `amount_msat` when calling [`create_inbound_payment`] or
506 * [`create_inbound_payment_for_hash`] you must check that the amount in the `PaymentReceived`
507 * event matches your expectation. If you fail to do so and call this method, you may provide
508 * the sender \"proof-of-payment\" when they did not fulfill the full expected payment.
510 * Returns whether any HTLCs were claimed, and thus if any new [`MessageSendEvent`]s are now
511 * pending for processing via [`get_and_clear_pending_msg_events`].
513 * [`Event::PaymentReceived`]: crate::util::events::Event::PaymentReceived
514 * [`create_inbound_payment`]: Self::create_inbound_payment
515 * [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
516 * [`get_and_clear_pending_msg_events`]: MessageSendEventsProvider::get_and_clear_pending_msg_events
518 public boolean claim_funds(byte[] payment_preimage) {
519 boolean ret = bindings.ChannelManager_claim_funds(this.ptr, InternalUtils.check_arr_len(payment_preimage, 32));
520 Reference.reachabilityFence(this);
521 Reference.reachabilityFence(payment_preimage);
526 * Gets the node_id held by this ChannelManager
528 public byte[] get_our_node_id() {
529 byte[] ret = bindings.ChannelManager_get_our_node_id(this.ptr);
530 Reference.reachabilityFence(this);
535 * Called to accept a request to open a channel after [`Event::OpenChannelRequest`] has been
538 * The `temporary_channel_id` parameter indicates which inbound channel should be accepted.
540 * For inbound channels, the `user_channel_id` parameter will be provided back in
541 * [`Event::ChannelClosed::user_channel_id`] to allow tracking of which events correspond
542 * with which `accept_inbound_channel` call.
544 * [`Event::OpenChannelRequest`]: events::Event::OpenChannelRequest
545 * [`Event::ChannelClosed::user_channel_id`]: events::Event::ChannelClosed::user_channel_id
547 public Result_NoneAPIErrorZ accept_inbound_channel(byte[] temporary_channel_id, long user_channel_id) {
548 long ret = bindings.ChannelManager_accept_inbound_channel(this.ptr, InternalUtils.check_arr_len(temporary_channel_id, 32), user_channel_id);
549 Reference.reachabilityFence(this);
550 Reference.reachabilityFence(temporary_channel_id);
551 Reference.reachabilityFence(user_channel_id);
552 if (ret >= 0 && ret <= 4096) { return null; }
553 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
558 * Gets a payment secret and payment hash for use in an invoice given to a third party wishing
561 * This differs from [`create_inbound_payment_for_hash`] only in that it generates the
562 * [`PaymentHash`] and [`PaymentPreimage`] for you.
564 * The [`PaymentPreimage`] will ultimately be returned to you in the [`PaymentReceived`], which
565 * will have the [`PaymentReceived::payment_preimage`] field filled in. That should then be
566 * passed directly to [`claim_funds`].
568 * See [`create_inbound_payment_for_hash`] for detailed documentation on behavior and requirements.
570 * Note that a malicious eavesdropper can intuit whether an inbound payment was created by
571 * `create_inbound_payment` or `create_inbound_payment_for_hash` based on runtime.
575 * If you register an inbound payment with this method, then serialize the `ChannelManager`, then
576 * deserialize it with a node running 0.0.103 and earlier, the payment will fail to be received.
578 * Errors if `min_value_msat` is greater than total bitcoin supply.
580 * [`claim_funds`]: Self::claim_funds
581 * [`PaymentReceived`]: events::Event::PaymentReceived
582 * [`PaymentReceived::payment_preimage`]: events::Event::PaymentReceived::payment_preimage
583 * [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
585 public Result_C2Tuple_PaymentHashPaymentSecretZNoneZ create_inbound_payment(Option_u64Z min_value_msat, int invoice_expiry_delta_secs) {
586 long ret = bindings.ChannelManager_create_inbound_payment(this.ptr, min_value_msat.ptr, invoice_expiry_delta_secs);
587 Reference.reachabilityFence(this);
588 Reference.reachabilityFence(min_value_msat);
589 Reference.reachabilityFence(invoice_expiry_delta_secs);
590 if (ret >= 0 && ret <= 4096) { return null; }
591 Result_C2Tuple_PaymentHashPaymentSecretZNoneZ ret_hu_conv = Result_C2Tuple_PaymentHashPaymentSecretZNoneZ.constr_from_ptr(ret);
596 * Legacy version of [`create_inbound_payment`]. Use this method if you wish to share
597 * serialized state with LDK node(s) running 0.0.103 and earlier.
599 * May panic if `invoice_expiry_delta_secs` is greater than one year.
602 * This method is deprecated and will be removed soon.
604 * [`create_inbound_payment`]: Self::create_inbound_payment
606 public Result_C2Tuple_PaymentHashPaymentSecretZAPIErrorZ create_inbound_payment_legacy(Option_u64Z min_value_msat, int invoice_expiry_delta_secs) {
607 long ret = bindings.ChannelManager_create_inbound_payment_legacy(this.ptr, min_value_msat.ptr, invoice_expiry_delta_secs);
608 Reference.reachabilityFence(this);
609 Reference.reachabilityFence(min_value_msat);
610 Reference.reachabilityFence(invoice_expiry_delta_secs);
611 if (ret >= 0 && ret <= 4096) { return null; }
612 Result_C2Tuple_PaymentHashPaymentSecretZAPIErrorZ ret_hu_conv = Result_C2Tuple_PaymentHashPaymentSecretZAPIErrorZ.constr_from_ptr(ret);
617 * Gets a [`PaymentSecret`] for a given [`PaymentHash`], for which the payment preimage is
618 * stored external to LDK.
620 * A [`PaymentReceived`] event will only be generated if the [`PaymentSecret`] matches a
621 * payment secret fetched via this method or [`create_inbound_payment`], and which is at least
622 * the `min_value_msat` provided here, if one is provided.
624 * The [`PaymentHash`] (and corresponding [`PaymentPreimage`]) should be globally unique, though
625 * note that LDK will not stop you from registering duplicate payment hashes for inbound
628 * `min_value_msat` should be set if the invoice being generated contains a value. Any payment
629 * received for the returned [`PaymentHash`] will be required to be at least `min_value_msat`
630 * before a [`PaymentReceived`] event will be generated, ensuring that we do not provide the
631 * sender \"proof-of-payment\" unless they have paid the required amount.
633 * `invoice_expiry_delta_secs` describes the number of seconds that the invoice is valid for
634 * in excess of the current time. This should roughly match the expiry time set in the invoice.
635 * After this many seconds, we will remove the inbound payment, resulting in any attempts to
636 * pay the invoice failing. The BOLT spec suggests 3,600 secs as a default validity time for
637 * invoices when no timeout is set.
639 * Note that we use block header time to time-out pending inbound payments (with some margin
640 * to compensate for the inaccuracy of block header timestamps). Thus, in practice we will
641 * accept a payment and generate a [`PaymentReceived`] event for some time after the expiry.
642 * If you need exact expiry semantics, you should enforce them upon receipt of
643 * [`PaymentReceived`].
645 * Note that invoices generated for inbound payments should have their `min_final_cltv_expiry`
646 * set to at least [`MIN_FINAL_CLTV_EXPIRY`].
648 * Note that a malicious eavesdropper can intuit whether an inbound payment was created by
649 * `create_inbound_payment` or `create_inbound_payment_for_hash` based on runtime.
653 * If you register an inbound payment with this method, then serialize the `ChannelManager`, then
654 * deserialize it with a node running 0.0.103 and earlier, the payment will fail to be received.
656 * Errors if `min_value_msat` is greater than total bitcoin supply.
658 * [`create_inbound_payment`]: Self::create_inbound_payment
659 * [`PaymentReceived`]: events::Event::PaymentReceived
661 public Result_PaymentSecretNoneZ create_inbound_payment_for_hash(byte[] payment_hash, Option_u64Z min_value_msat, int invoice_expiry_delta_secs) {
662 long ret = bindings.ChannelManager_create_inbound_payment_for_hash(this.ptr, InternalUtils.check_arr_len(payment_hash, 32), min_value_msat.ptr, invoice_expiry_delta_secs);
663 Reference.reachabilityFence(this);
664 Reference.reachabilityFence(payment_hash);
665 Reference.reachabilityFence(min_value_msat);
666 Reference.reachabilityFence(invoice_expiry_delta_secs);
667 if (ret >= 0 && ret <= 4096) { return null; }
668 Result_PaymentSecretNoneZ ret_hu_conv = Result_PaymentSecretNoneZ.constr_from_ptr(ret);
673 * Legacy version of [`create_inbound_payment_for_hash`]. Use this method if you wish to share
674 * serialized state with LDK node(s) running 0.0.103 and earlier.
676 * May panic if `invoice_expiry_delta_secs` is greater than one year.
679 * This method is deprecated and will be removed soon.
681 * [`create_inbound_payment_for_hash`]: Self::create_inbound_payment_for_hash
683 public Result_PaymentSecretAPIErrorZ create_inbound_payment_for_hash_legacy(byte[] payment_hash, Option_u64Z min_value_msat, int invoice_expiry_delta_secs) {
684 long ret = bindings.ChannelManager_create_inbound_payment_for_hash_legacy(this.ptr, InternalUtils.check_arr_len(payment_hash, 32), min_value_msat.ptr, invoice_expiry_delta_secs);
685 Reference.reachabilityFence(this);
686 Reference.reachabilityFence(payment_hash);
687 Reference.reachabilityFence(min_value_msat);
688 Reference.reachabilityFence(invoice_expiry_delta_secs);
689 if (ret >= 0 && ret <= 4096) { return null; }
690 Result_PaymentSecretAPIErrorZ ret_hu_conv = Result_PaymentSecretAPIErrorZ.constr_from_ptr(ret);
695 * Gets an LDK-generated payment preimage from a payment hash and payment secret that were
696 * previously returned from [`create_inbound_payment`].
698 * [`create_inbound_payment`]: Self::create_inbound_payment
700 public Result_PaymentPreimageAPIErrorZ get_payment_preimage(byte[] payment_hash, byte[] payment_secret) {
701 long ret = bindings.ChannelManager_get_payment_preimage(this.ptr, InternalUtils.check_arr_len(payment_hash, 32), InternalUtils.check_arr_len(payment_secret, 32));
702 Reference.reachabilityFence(this);
703 Reference.reachabilityFence(payment_hash);
704 Reference.reachabilityFence(payment_secret);
705 if (ret >= 0 && ret <= 4096) { return null; }
706 Result_PaymentPreimageAPIErrorZ ret_hu_conv = Result_PaymentPreimageAPIErrorZ.constr_from_ptr(ret);
711 * Gets a fake short channel id for use in receiving [phantom node payments]. These fake scids
712 * are used when constructing the phantom invoice's route hints.
714 * [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager
716 public long get_phantom_scid() {
717 long ret = bindings.ChannelManager_get_phantom_scid(this.ptr);
718 Reference.reachabilityFence(this);
723 * Gets route hints for use in receiving [phantom node payments].
725 * [phantom node payments]: crate::chain::keysinterface::PhantomKeysManager
727 public PhantomRouteHints get_phantom_route_hints() {
728 long ret = bindings.ChannelManager_get_phantom_route_hints(this.ptr);
729 Reference.reachabilityFence(this);
730 if (ret >= 0 && ret <= 4096) { return null; }
731 PhantomRouteHints ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new PhantomRouteHints(null, ret); }
732 ret_hu_conv.ptrs_to.add(this);
737 * Constructs a new MessageSendEventsProvider which calls the relevant methods on this_arg.
738 * This copies the `inner` pointer in this_arg and thus the returned MessageSendEventsProvider must be freed before this_arg is
740 public MessageSendEventsProvider as_MessageSendEventsProvider() {
741 long ret = bindings.ChannelManager_as_MessageSendEventsProvider(this.ptr);
742 Reference.reachabilityFence(this);
743 if (ret >= 0 && ret <= 4096) { return null; }
744 MessageSendEventsProvider ret_hu_conv = new MessageSendEventsProvider(null, ret);
745 ret_hu_conv.ptrs_to.add(this);
750 * Constructs a new EventsProvider which calls the relevant methods on this_arg.
751 * This copies the `inner` pointer in this_arg and thus the returned EventsProvider must be freed before this_arg is
753 public EventsProvider as_EventsProvider() {
754 long ret = bindings.ChannelManager_as_EventsProvider(this.ptr);
755 Reference.reachabilityFence(this);
756 if (ret >= 0 && ret <= 4096) { return null; }
757 EventsProvider ret_hu_conv = new EventsProvider(null, ret);
758 ret_hu_conv.ptrs_to.add(this);
763 * Constructs a new Listen which calls the relevant methods on this_arg.
764 * This copies the `inner` pointer in this_arg and thus the returned Listen must be freed before this_arg is
766 public Listen as_Listen() {
767 long ret = bindings.ChannelManager_as_Listen(this.ptr);
768 Reference.reachabilityFence(this);
769 if (ret >= 0 && ret <= 4096) { return null; }
770 Listen ret_hu_conv = new Listen(null, ret);
771 ret_hu_conv.ptrs_to.add(this);
776 * Constructs a new Confirm which calls the relevant methods on this_arg.
777 * This copies the `inner` pointer in this_arg and thus the returned Confirm must be freed before this_arg is
779 public Confirm as_Confirm() {
780 long ret = bindings.ChannelManager_as_Confirm(this.ptr);
781 Reference.reachabilityFence(this);
782 if (ret >= 0 && ret <= 4096) { return null; }
783 Confirm ret_hu_conv = new Confirm(null, ret);
784 ret_hu_conv.ptrs_to.add(this);
789 * Blocks until ChannelManager needs to be persisted or a timeout is reached. It returns a bool
790 * indicating whether persistence is necessary. Only one listener on
791 * `await_persistable_update` or `await_persistable_update_timeout` is guaranteed to be woken
794 * Note that this method is not available with the `no-std` feature.
796 public boolean await_persistable_update_timeout(long max_wait) {
797 boolean ret = bindings.ChannelManager_await_persistable_update_timeout(this.ptr, max_wait);
798 Reference.reachabilityFence(this);
799 Reference.reachabilityFence(max_wait);
804 * Blocks until ChannelManager needs to be persisted. Only one listener on
805 * `await_persistable_update` or `await_persistable_update_timeout` is guaranteed to be woken
808 public void await_persistable_update() {
809 bindings.ChannelManager_await_persistable_update(this.ptr);
810 Reference.reachabilityFence(this);
814 * Gets the latest best block which was connected either via the [`chain::Listen`] or
815 * [`chain::Confirm`] interfaces.
817 public BestBlock current_best_block() {
818 long ret = bindings.ChannelManager_current_best_block(this.ptr);
819 Reference.reachabilityFence(this);
820 if (ret >= 0 && ret <= 4096) { return null; }
821 BestBlock ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new BestBlock(null, ret); }
822 ret_hu_conv.ptrs_to.add(this);
827 * Constructs a new ChannelMessageHandler which calls the relevant methods on this_arg.
828 * This copies the `inner` pointer in this_arg and thus the returned ChannelMessageHandler must be freed before this_arg is
830 public ChannelMessageHandler as_ChannelMessageHandler() {
831 long ret = bindings.ChannelManager_as_ChannelMessageHandler(this.ptr);
832 Reference.reachabilityFence(this);
833 if (ret >= 0 && ret <= 4096) { return null; }
834 ChannelMessageHandler ret_hu_conv = new ChannelMessageHandler(null, ret);
835 ret_hu_conv.ptrs_to.add(this);
840 * Serialize the ChannelManager object into a byte array which can be read by ChannelManager_read
842 public byte[] write() {
843 byte[] ret = bindings.ChannelManager_write(this.ptr);
844 Reference.reachabilityFence(this);
849 * Constructs a new Payer which calls the relevant methods on this_arg.
850 * This copies the `inner` pointer in this_arg and thus the returned Payer must be freed before this_arg is
852 public Payer as_Payer() {
853 long ret = bindings.ChannelManager_as_Payer(this.ptr);
854 Reference.reachabilityFence(this);
855 if (ret >= 0 && ret <= 4096) { return null; }
856 Payer ret_hu_conv = new Payer(null, ret);
857 ret_hu_conv.ptrs_to.add(this);