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 * An implementation of [`chain::Watch`] for monitoring channels.
14 * Connected and disconnected blocks must be provided to `ChainMonitor` as documented by
15 * [`chain::Watch`]. May be used in conjunction with [`ChannelManager`] to monitor channels locally
16 * or used independently to monitor channels remotely. See the [module-level documentation] for
19 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
20 * [module-level documentation]: crate::chain::chainmonitor
22 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
23 public class ChainMonitor extends CommonBase {
24 ChainMonitor(Object _dummy, long ptr) { super(ptr); }
25 @Override @SuppressWarnings("deprecation")
26 protected void finalize() throws Throwable {
28 if (ptr != 0) { bindings.ChainMonitor_free(ptr); }
32 * Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
34 * When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
35 * will call back to it indicating transactions and outputs of interest. This allows clients to
36 * pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may
37 * always need to fetch full blocks absent another means for determining which blocks contain
38 * transactions relevant to the watched channels.
40 public static ChainMonitor of(Option_FilterZ chain_source, BroadcasterInterface broadcaster, Logger logger, FeeEstimator feeest, Persist persister) {
41 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);
42 Reference.reachabilityFence(chain_source);
43 Reference.reachabilityFence(broadcaster);
44 Reference.reachabilityFence(logger);
45 Reference.reachabilityFence(feeest);
46 Reference.reachabilityFence(persister);
47 if (ret >= 0 && ret <= 4096) { return null; }
48 ChainMonitor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new ChainMonitor(null, ret); }
49 ret_hu_conv.ptrs_to.add(ret_hu_conv);
50 ret_hu_conv.ptrs_to.add(chain_source);
51 ret_hu_conv.ptrs_to.add(broadcaster);
52 ret_hu_conv.ptrs_to.add(logger);
53 ret_hu_conv.ptrs_to.add(feeest);
54 ret_hu_conv.ptrs_to.add(persister);
59 * Gets the balances in the contained [`ChannelMonitor`]s which are claimable on-chain or
60 * claims which are awaiting confirmation.
62 * Includes the balances from each [`ChannelMonitor`] *except* those included in
63 * `ignored_channels`, allowing you to filter out balances from channels which are still open
64 * (and whose balance should likely be pulled from the [`ChannelDetails`]).
66 * See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for
67 * inclusion in the return value.
69 public Balance[] get_claimable_balances(ChannelDetails[] ignored_channels) {
70 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);
71 Reference.reachabilityFence(this);
72 Reference.reachabilityFence(ignored_channels);
73 Balance[] ret_conv_9_arr = new Balance[ret.length];
74 for (int j = 0; j < ret.length; j++) {
75 long ret_conv_9 = ret[j];
76 Balance ret_conv_9_hu_conv = Balance.constr_from_ptr(ret_conv_9);
77 ret_conv_9_hu_conv.ptrs_to.add(this);
78 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
80 return ret_conv_9_arr;
84 * Gets the [`LockedChannelMonitor`] for a given funding outpoint, returning an `Err` if no
85 * such [`ChannelMonitor`] is currently being monitored for.
87 * Note that the result holds a mutex over our monitor set, and should not be held
90 public Result_LockedChannelMonitorNoneZ get_monitor(OutPoint funding_txo) {
91 long ret = bindings.ChainMonitor_get_monitor(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1);
92 Reference.reachabilityFence(this);
93 Reference.reachabilityFence(funding_txo);
94 if (ret >= 0 && ret <= 4096) { return null; }
95 Result_LockedChannelMonitorNoneZ ret_hu_conv = Result_LockedChannelMonitorNoneZ.constr_from_ptr(ret);
100 * Lists the funding outpoint of each [`ChannelMonitor`] being monitored.
102 * Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always
103 * monitoring for on-chain state resolutions.
105 public OutPoint[] list_monitors() {
106 long[] ret = bindings.ChainMonitor_list_monitors(this.ptr);
107 Reference.reachabilityFence(this);
108 OutPoint[] ret_conv_10_arr = new OutPoint[ret.length];
109 for (int k = 0; k < ret.length; k++) {
110 long ret_conv_10 = ret[k];
111 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); }
112 ret_conv_10_hu_conv.ptrs_to.add(this);
113 ret_conv_10_arr[k] = ret_conv_10_hu_conv;
115 return ret_conv_10_arr;
119 * Indicates the persistence of a [`ChannelMonitor`] has completed after
120 * [`ChannelMonitorUpdateErr::TemporaryFailure`] was returned from an update operation.
122 * Thus, the anticipated use is, at a high level:
123 * 1) This [`ChainMonitor`] calls [`Persist::update_persisted_channel`] which stores the
124 * update to disk and begins updating any remote (e.g. watchtower/backup) copies,
125 * returning [`ChannelMonitorUpdateErr::TemporaryFailure`],
126 * 2) once all remote copies are updated, you call this function with the
127 * `completed_update_id` that completed, and once all pending updates have completed the
128 * channel will be re-enabled.
130 * Returns an [`APIError::APIMisuseError`] if `funding_txo` does not match any currently
131 * registered [`ChannelMonitor`]s.
133 public Result_NoneAPIErrorZ channel_monitor_updated(OutPoint funding_txo, MonitorUpdateId completed_update_id) {
134 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);
135 Reference.reachabilityFence(this);
136 Reference.reachabilityFence(funding_txo);
137 Reference.reachabilityFence(completed_update_id);
138 if (ret >= 0 && ret <= 4096) { return null; }
139 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
144 * Constructs a new Listen which calls the relevant methods on this_arg.
145 * This copies the `inner` pointer in this_arg and thus the returned Listen must be freed before this_arg is
147 public Listen as_Listen() {
148 long ret = bindings.ChainMonitor_as_Listen(this.ptr);
149 Reference.reachabilityFence(this);
150 if (ret >= 0 && ret <= 4096) { return null; }
151 Listen ret_hu_conv = new Listen(null, ret);
152 ret_hu_conv.ptrs_to.add(this);
157 * Constructs a new Confirm which calls the relevant methods on this_arg.
158 * This copies the `inner` pointer in this_arg and thus the returned Confirm must be freed before this_arg is
160 public Confirm as_Confirm() {
161 long ret = bindings.ChainMonitor_as_Confirm(this.ptr);
162 Reference.reachabilityFence(this);
163 if (ret >= 0 && ret <= 4096) { return null; }
164 Confirm ret_hu_conv = new Confirm(null, ret);
165 ret_hu_conv.ptrs_to.add(this);
170 * Constructs a new Watch which calls the relevant methods on this_arg.
171 * This copies the `inner` pointer in this_arg and thus the returned Watch must be freed before this_arg is
173 public Watch as_Watch() {
174 long ret = bindings.ChainMonitor_as_Watch(this.ptr);
175 Reference.reachabilityFence(this);
176 if (ret >= 0 && ret <= 4096) { return null; }
177 Watch ret_hu_conv = new Watch(null, ret);
178 ret_hu_conv.ptrs_to.add(this);
183 * Constructs a new EventsProvider which calls the relevant methods on this_arg.
184 * This copies the `inner` pointer in this_arg and thus the returned EventsProvider must be freed before this_arg is
186 public EventsProvider as_EventsProvider() {
187 long ret = bindings.ChainMonitor_as_EventsProvider(this.ptr);
188 Reference.reachabilityFence(this);
189 if (ret >= 0 && ret <= 4096) { return null; }
190 EventsProvider ret_hu_conv = new EventsProvider(null, ret);
191 ret_hu_conv.ptrs_to.add(this);