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 * Note that `ChainMonitor` should regularly trigger rebroadcasts/fee bumps of pending claims from
20 * a force-closed channel. This is crucial in preventing certain classes of pinning attacks,
21 * detecting substantial mempool feerate changes between blocks, and ensuring reliability if
22 * broadcasting fails. We recommend invoking this every 30 seconds, or lower if running in an
23 * environment with spotty connections, like on mobile.
25 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
26 * [module-level documentation]: crate::chain::chainmonitor
27 * [`rebroadcast_pending_claims`]: Self::rebroadcast_pending_claims
29 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
30 public class ChainMonitor extends CommonBase {
31 ChainMonitor(Object _dummy, long ptr) { super(ptr); }
32 @Override @SuppressWarnings("deprecation")
33 protected void finalize() throws Throwable {
35 if (ptr != 0) { bindings.ChainMonitor_free(ptr); }
39 * Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
41 * When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
42 * will call back to it indicating transactions and outputs of interest. This allows clients to
43 * pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may
44 * always need to fetch full blocks absent another means for determining which blocks contain
45 * transactions relevant to the watched channels.
47 public static ChainMonitor of(org.ldk.structs.Option_FilterZ chain_source, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.Logger logger, org.ldk.structs.FeeEstimator feeest, org.ldk.structs.Persist persister) {
48 long ret = bindings.ChainMonitor_new(chain_source.ptr, broadcaster.ptr, logger.ptr, feeest.ptr, persister.ptr);
49 Reference.reachabilityFence(chain_source);
50 Reference.reachabilityFence(broadcaster);
51 Reference.reachabilityFence(logger);
52 Reference.reachabilityFence(feeest);
53 Reference.reachabilityFence(persister);
54 if (ret >= 0 && ret <= 4096) { return null; }
55 org.ldk.structs.ChainMonitor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChainMonitor(null, ret); }
56 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
57 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(chain_source); };
58 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(broadcaster); };
59 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
60 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(feeest); };
61 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(persister); };
66 * Gets the balances in the contained [`ChannelMonitor`]s which are claimable on-chain or
67 * claims which are awaiting confirmation.
69 * Includes the balances from each [`ChannelMonitor`] *except* those included in
70 * `ignored_channels`, allowing you to filter out balances from channels which are still open
71 * (and whose balance should likely be pulled from the [`ChannelDetails`]).
73 * See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for
74 * inclusion in the return value.
76 public Balance[] get_claimable_balances(ChannelDetails[] ignored_channels) {
77 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).toArray() : null);
78 Reference.reachabilityFence(this);
79 Reference.reachabilityFence(ignored_channels);
80 int ret_conv_9_len = ret.length;
81 Balance[] ret_conv_9_arr = new Balance[ret_conv_9_len];
82 for (int j = 0; j < ret_conv_9_len; j++) {
83 long ret_conv_9 = ret[j];
84 org.ldk.structs.Balance ret_conv_9_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret_conv_9);
85 if (ret_conv_9_hu_conv != null) { ret_conv_9_hu_conv.ptrs_to.add(this); };
86 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
88 for (ChannelDetails ignored_channels_conv_16: ignored_channels) { if (this != null) { this.ptrs_to.add(ignored_channels_conv_16); }; };
89 return ret_conv_9_arr;
93 * Gets the [`LockedChannelMonitor`] for a given funding outpoint, returning an `Err` if no
94 * such [`ChannelMonitor`] is currently being monitored for.
96 * Note that the result holds a mutex over our monitor set, and should not be held
99 public Result_LockedChannelMonitorNoneZ get_monitor(org.ldk.structs.OutPoint funding_txo) {
100 long ret = bindings.ChainMonitor_get_monitor(this.ptr, funding_txo == null ? 0 : funding_txo.ptr);
101 Reference.reachabilityFence(this);
102 Reference.reachabilityFence(funding_txo);
103 if (ret >= 0 && ret <= 4096) { return null; }
104 Result_LockedChannelMonitorNoneZ ret_hu_conv = Result_LockedChannelMonitorNoneZ.constr_from_ptr(ret);
105 if (this != null) { this.ptrs_to.add(funding_txo); };
110 * Lists the funding outpoint of each [`ChannelMonitor`] being monitored.
112 * Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always
113 * monitoring for on-chain state resolutions.
115 public OutPoint[] list_monitors() {
116 long[] ret = bindings.ChainMonitor_list_monitors(this.ptr);
117 Reference.reachabilityFence(this);
118 int ret_conv_10_len = ret.length;
119 OutPoint[] ret_conv_10_arr = new OutPoint[ret_conv_10_len];
120 for (int k = 0; k < ret_conv_10_len; k++) {
121 long ret_conv_10 = ret[k];
122 org.ldk.structs.OutPoint ret_conv_10_hu_conv = null; if (ret_conv_10 < 0 || ret_conv_10 > 4096) { ret_conv_10_hu_conv = new org.ldk.structs.OutPoint(null, ret_conv_10); }
123 if (ret_conv_10_hu_conv != null) { ret_conv_10_hu_conv.ptrs_to.add(this); };
124 ret_conv_10_arr[k] = ret_conv_10_hu_conv;
126 return ret_conv_10_arr;
130 * Lists the pending updates for each [`ChannelMonitor`] (by `OutPoint` being monitored).
132 public TwoTuple_OutPointCVec_MonitorUpdateIdZZ[] list_pending_monitor_updates() {
133 long[] ret = bindings.ChainMonitor_list_pending_monitor_updates(this.ptr);
134 Reference.reachabilityFence(this);
135 int ret_conv_41_len = ret.length;
136 TwoTuple_OutPointCVec_MonitorUpdateIdZZ[] ret_conv_41_arr = new TwoTuple_OutPointCVec_MonitorUpdateIdZZ[ret_conv_41_len];
137 for (int p = 0; p < ret_conv_41_len; p++) {
138 long ret_conv_41 = ret[p];
139 TwoTuple_OutPointCVec_MonitorUpdateIdZZ ret_conv_41_hu_conv = new TwoTuple_OutPointCVec_MonitorUpdateIdZZ(null, ret_conv_41);
140 if (ret_conv_41_hu_conv != null) { ret_conv_41_hu_conv.ptrs_to.add(this); };
141 ret_conv_41_arr[p] = ret_conv_41_hu_conv;
143 return ret_conv_41_arr;
147 * Indicates the persistence of a [`ChannelMonitor`] has completed after
148 * [`ChannelMonitorUpdateStatus::InProgress`] was returned from an update operation.
150 * Thus, the anticipated use is, at a high level:
151 * 1) This [`ChainMonitor`] calls [`Persist::update_persisted_channel`] which stores the
152 * update to disk and begins updating any remote (e.g. watchtower/backup) copies,
153 * returning [`ChannelMonitorUpdateStatus::InProgress`],
154 * 2) once all remote copies are updated, you call this function with the
155 * `completed_update_id` that completed, and once all pending updates have completed the
156 * channel will be re-enabled.
158 * Returns an [`APIError::APIMisuseError`] if `funding_txo` does not match any currently
159 * registered [`ChannelMonitor`]s.
161 public Result_NoneAPIErrorZ channel_monitor_updated(org.ldk.structs.OutPoint funding_txo, org.ldk.structs.MonitorUpdateId completed_update_id) {
162 long ret = bindings.ChainMonitor_channel_monitor_updated(this.ptr, funding_txo == null ? 0 : funding_txo.ptr, completed_update_id == null ? 0 : completed_update_id.ptr);
163 Reference.reachabilityFence(this);
164 Reference.reachabilityFence(funding_txo);
165 Reference.reachabilityFence(completed_update_id);
166 if (ret >= 0 && ret <= 4096) { return null; }
167 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
168 if (this != null) { this.ptrs_to.add(funding_txo); };
169 if (this != null) { this.ptrs_to.add(completed_update_id); };
174 * Gets a [`Future`] that completes when an event is available either via
175 * [`chain::Watch::release_pending_monitor_events`] or
176 * [`EventsProvider::process_pending_events`].
178 * Note that callbacks registered on the [`Future`] MUST NOT call back into this
179 * [`ChainMonitor`] and should instead register actions to be taken later.
181 * [`EventsProvider::process_pending_events`]: crate::events::EventsProvider::process_pending_events
183 public Future get_update_future() {
184 long ret = bindings.ChainMonitor_get_update_future(this.ptr);
185 Reference.reachabilityFence(this);
186 if (ret >= 0 && ret <= 4096) { return null; }
187 org.ldk.structs.Future ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.Future(null, ret); }
188 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
193 * Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is
194 * crucial in preventing certain classes of pinning attacks, detecting substantial mempool
195 * feerate changes between blocks, and ensuring reliability if broadcasting fails. We recommend
196 * invoking this every 30 seconds, or lower if running in an environment with spotty
197 * connections, like on mobile.
199 public void rebroadcast_pending_claims() {
200 bindings.ChainMonitor_rebroadcast_pending_claims(this.ptr);
201 Reference.reachabilityFence(this);
205 * Constructs a new Listen which calls the relevant methods on this_arg.
206 * This copies the `inner` pointer in this_arg and thus the returned Listen must be freed before this_arg is
208 public Listen as_Listen() {
209 long ret = bindings.ChainMonitor_as_Listen(this.ptr);
210 Reference.reachabilityFence(this);
211 if (ret >= 0 && ret <= 4096) { return null; }
212 Listen ret_hu_conv = new Listen(null, ret);
213 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
218 * Constructs a new Confirm which calls the relevant methods on this_arg.
219 * This copies the `inner` pointer in this_arg and thus the returned Confirm must be freed before this_arg is
221 public Confirm as_Confirm() {
222 long ret = bindings.ChainMonitor_as_Confirm(this.ptr);
223 Reference.reachabilityFence(this);
224 if (ret >= 0 && ret <= 4096) { return null; }
225 Confirm ret_hu_conv = new Confirm(null, ret);
226 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
231 * Constructs a new Watch which calls the relevant methods on this_arg.
232 * This copies the `inner` pointer in this_arg and thus the returned Watch must be freed before this_arg is
234 public Watch as_Watch() {
235 long ret = bindings.ChainMonitor_as_Watch(this.ptr);
236 Reference.reachabilityFence(this);
237 if (ret >= 0 && ret <= 4096) { return null; }
238 Watch ret_hu_conv = new Watch(null, ret);
239 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
244 * Constructs a new EventsProvider which calls the relevant methods on this_arg.
245 * This copies the `inner` pointer in this_arg and thus the returned EventsProvider must be freed before this_arg is
247 public EventsProvider as_EventsProvider() {
248 long ret = bindings.ChainMonitor_as_EventsProvider(this.ptr);
249 Reference.reachabilityFence(this);
250 if (ret >= 0 && ret <= 4096) { return null; }
251 EventsProvider ret_hu_conv = new EventsProvider(null, ret);
252 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };