1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
11 * An implementation of [`chain::Watch`] for monitoring channels.
13 * Connected and disconnected blocks must be provided to `ChainMonitor` as documented by
14 * [`chain::Watch`]. May be used in conjunction with [`ChannelManager`] to monitor channels locally
15 * or used independently to monitor channels remotely. See the [module-level documentation] for
18 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
19 * [module-level documentation]: crate::chain::chainmonitor
21 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
22 public class ChainMonitor extends CommonBase {
23 ChainMonitor(Object _dummy, long ptr) { super(ptr); }
24 @Override @SuppressWarnings("deprecation")
25 protected void finalize() throws Throwable {
27 if (ptr != 0) { bindings.ChainMonitor_free(ptr); }
31 * Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
33 * When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
34 * will call back to it indicating transactions and outputs of interest. This allows clients to
35 * pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may
36 * always need to fetch full blocks absent another means for determining which blocks contain
37 * transactions relevant to the watched channels.
39 public static ChainMonitor of(Option_FilterZ chain_source, BroadcasterInterface broadcaster, Logger logger, FeeEstimator feeest, Persist persister) {
40 long ret = bindings.ChainMonitor_new(chain_source.ptr, broadcaster == null ? 0 : broadcaster.ptr, logger == null ? 0 : logger.ptr, feeest == null ? 0 : feeest.ptr, persister == null ? 0 : persister.ptr);
41 if (ret >= 0 && ret <= 4096) { return null; }
42 ChainMonitor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new ChainMonitor(null, ret); }
43 ret_hu_conv.ptrs_to.add(ret_hu_conv);
44 ret_hu_conv.ptrs_to.add(chain_source);
45 ret_hu_conv.ptrs_to.add(broadcaster);
46 ret_hu_conv.ptrs_to.add(logger);
47 ret_hu_conv.ptrs_to.add(feeest);
48 ret_hu_conv.ptrs_to.add(persister);
53 * Gets the balances in the contained [`ChannelMonitor`]s which are claimable on-chain or
54 * claims which are awaiting confirmation.
56 * Includes the balances from each [`ChannelMonitor`] *except* those included in
57 * `ignored_channels`, allowing you to filter out balances from channels which are still open
58 * (and whose balance should likely be pulled from the [`ChannelDetails`]).
60 * See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for
61 * inclusion in the return value.
63 public Balance[] get_claimable_balances(ChannelDetails[] ignored_channels) {
64 long[] ret = bindings.ChainMonitor_get_claimable_balances(this.ptr, ignored_channels != null ? Arrays.stream(ignored_channels).mapToLong(ignored_channels_conv_16 -> ignored_channels_conv_16 == null ? 0 : ignored_channels_conv_16.ptr & ~1).toArray() : null);
65 Balance[] ret_conv_9_arr = new Balance[ret.length];
66 for (int j = 0; j < ret.length; j++) {
67 long ret_conv_9 = ret[j];
68 Balance ret_conv_9_hu_conv = Balance.constr_from_ptr(ret_conv_9);
69 ret_conv_9_hu_conv.ptrs_to.add(this);
70 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
72 return ret_conv_9_arr;
76 * Gets the [`LockedChannelMonitor`] for a given funding outpoint, returning an `Err` if no
77 * such [`ChannelMonitor`] is currently being monitored for.
79 * Note that the result holds a mutex over our monitor set, and should not be held
82 public Result_LockedChannelMonitorNoneZ get_monitor(OutPoint funding_txo) {
83 long ret = bindings.ChainMonitor_get_monitor(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1);
84 if (ret >= 0 && ret <= 4096) { return null; }
85 Result_LockedChannelMonitorNoneZ ret_hu_conv = Result_LockedChannelMonitorNoneZ.constr_from_ptr(ret);
90 * Lists the funding outpoint of each [`ChannelMonitor`] being monitored.
92 * Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always
93 * monitoring for on-chain state resolutions.
95 public OutPoint[] list_monitors() {
96 long[] ret = bindings.ChainMonitor_list_monitors(this.ptr);
97 OutPoint[] ret_conv_10_arr = new OutPoint[ret.length];
98 for (int k = 0; k < ret.length; k++) {
99 long ret_conv_10 = ret[k];
100 OutPoint ret_conv_10_hu_conv = null; if (ret_conv_10 < 0 || ret_conv_10 > 4096) { ret_conv_10_hu_conv = new OutPoint(null, ret_conv_10); }
101 ret_conv_10_hu_conv.ptrs_to.add(this);
102 ret_conv_10_arr[k] = ret_conv_10_hu_conv;
104 return ret_conv_10_arr;
108 * Indicates the persistence of a [`ChannelMonitor`] has completed after
109 * [`ChannelMonitorUpdateErr::TemporaryFailure`] was returned from an update operation.
111 * Thus, the anticipated use is, at a high level:
112 * 1) This [`ChainMonitor`] calls [`Persist::update_persisted_channel`] which stores the
113 * update to disk and begins updating any remote (e.g. watchtower/backup) copies,
114 * returning [`ChannelMonitorUpdateErr::TemporaryFailure`],
115 * 2) once all remote copies are updated, you call this function with the
116 * `completed_update_id` that completed, and once all pending updates have completed the
117 * channel will be re-enabled.
119 * Returns an [`APIError::APIMisuseError`] if `funding_txo` does not match any currently
120 * registered [`ChannelMonitor`]s.
122 public Result_NoneAPIErrorZ channel_monitor_updated(OutPoint funding_txo, MonitorUpdateId completed_update_id) {
123 long ret = bindings.ChainMonitor_channel_monitor_updated(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1, completed_update_id == null ? 0 : completed_update_id.ptr & ~1);
124 if (ret >= 0 && ret <= 4096) { return null; }
125 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
130 * Constructs a new Listen which calls the relevant methods on this_arg.
131 * This copies the `inner` pointer in this_arg and thus the returned Listen must be freed before this_arg is
133 public Listen as_Listen() {
134 long ret = bindings.ChainMonitor_as_Listen(this.ptr);
135 if (ret >= 0 && ret <= 4096) { return null; }
136 Listen ret_hu_conv = new Listen(null, ret);
137 ret_hu_conv.ptrs_to.add(this);
142 * Constructs a new Confirm which calls the relevant methods on this_arg.
143 * This copies the `inner` pointer in this_arg and thus the returned Confirm must be freed before this_arg is
145 public Confirm as_Confirm() {
146 long ret = bindings.ChainMonitor_as_Confirm(this.ptr);
147 if (ret >= 0 && ret <= 4096) { return null; }
148 Confirm ret_hu_conv = new Confirm(null, ret);
149 ret_hu_conv.ptrs_to.add(this);
154 * Constructs a new Watch which calls the relevant methods on this_arg.
155 * This copies the `inner` pointer in this_arg and thus the returned Watch must be freed before this_arg is
157 public Watch as_Watch() {
158 long ret = bindings.ChainMonitor_as_Watch(this.ptr);
159 if (ret >= 0 && ret <= 4096) { return null; }
160 Watch ret_hu_conv = new Watch(null, ret);
161 ret_hu_conv.ptrs_to.add(this);
166 * Constructs a new EventsProvider which calls the relevant methods on this_arg.
167 * This copies the `inner` pointer in this_arg and thus the returned EventsProvider must be freed before this_arg is
169 public EventsProvider as_EventsProvider() {
170 long ret = bindings.ChainMonitor_as_EventsProvider(this.ptr);
171 if (ret >= 0 && ret <= 4096) { return null; }
172 EventsProvider ret_hu_conv = new EventsProvider(null, ret);
173 ret_hu_conv.ptrs_to.add(this);