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.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.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 and channel ID 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 TwoTuple_OutPointChannelIdZ[] list_monitors() {
116 long[] ret = bindings.ChainMonitor_list_monitors(this.ptr);
117 Reference.reachabilityFence(this);
118 int ret_conv_29_len = ret.length;
119 TwoTuple_OutPointChannelIdZ[] ret_conv_29_arr = new TwoTuple_OutPointChannelIdZ[ret_conv_29_len];
120 for (int d = 0; d < ret_conv_29_len; d++) {
121 long ret_conv_29 = ret[d];
122 TwoTuple_OutPointChannelIdZ ret_conv_29_hu_conv = new TwoTuple_OutPointChannelIdZ(null, ret_conv_29);
123 if (ret_conv_29_hu_conv != null) { ret_conv_29_hu_conv.ptrs_to.add(this); };
124 ret_conv_29_arr[d] = ret_conv_29_hu_conv;
126 return ret_conv_29_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.ptr, 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 * Triggers rebroadcasts of pending claims from force-closed channels after a transaction
206 * signature generation failure.
208 * `monitor_opt` can be used as a filter to only trigger them for a specific channel monitor.
210 * Note that monitor_opt (or a relevant inner pointer) may be NULL or all-0s to represent None
212 public void signer_unblocked(@Nullable org.ldk.structs.OutPoint monitor_opt) {
213 bindings.ChainMonitor_signer_unblocked(this.ptr, monitor_opt == null ? 0 : monitor_opt.ptr);
214 Reference.reachabilityFence(this);
215 Reference.reachabilityFence(monitor_opt);
216 if (this != null) { this.ptrs_to.add(monitor_opt); };
220 * Archives fully resolved channel monitors by calling [`Persist::archive_persisted_channel`].
222 * This is useful for pruning fully resolved monitors from the monitor set and primary
223 * storage so they are not kept in memory and reloaded on restart.
225 * Should be called occasionally (once every handful of blocks or on startup).
227 * Depending on the implementation of [`Persist::archive_persisted_channel`] the monitor
228 * data could be moved to an archive location or removed entirely.
230 public void archive_fully_resolved_channel_monitors() {
231 bindings.ChainMonitor_archive_fully_resolved_channel_monitors(this.ptr);
232 Reference.reachabilityFence(this);
236 * Constructs a new Listen which calls the relevant methods on this_arg.
237 * This copies the `inner` pointer in this_arg and thus the returned Listen must be freed before this_arg is
239 public Listen as_Listen() {
240 long ret = bindings.ChainMonitor_as_Listen(this.ptr);
241 Reference.reachabilityFence(this);
242 if (ret >= 0 && ret <= 4096) { return null; }
243 Listen ret_hu_conv = new Listen(null, ret);
244 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
249 * Constructs a new Confirm which calls the relevant methods on this_arg.
250 * This copies the `inner` pointer in this_arg and thus the returned Confirm must be freed before this_arg is
252 public Confirm as_Confirm() {
253 long ret = bindings.ChainMonitor_as_Confirm(this.ptr);
254 Reference.reachabilityFence(this);
255 if (ret >= 0 && ret <= 4096) { return null; }
256 Confirm ret_hu_conv = new Confirm(null, ret);
257 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
262 * Constructs a new Watch which calls the relevant methods on this_arg.
263 * This copies the `inner` pointer in this_arg and thus the returned Watch must be freed before this_arg is
265 public Watch as_Watch() {
266 long ret = bindings.ChainMonitor_as_Watch(this.ptr);
267 Reference.reachabilityFence(this);
268 if (ret >= 0 && ret <= 4096) { return null; }
269 Watch ret_hu_conv = new Watch(null, ret);
270 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
275 * Constructs a new EventsProvider which calls the relevant methods on this_arg.
276 * This copies the `inner` pointer in this_arg and thus the returned EventsProvider must be freed before this_arg is
278 public EventsProvider as_EventsProvider() {
279 long ret = bindings.ChainMonitor_as_EventsProvider(this.ptr);
280 Reference.reachabilityFence(this);
281 if (ret >= 0 && ret <= 4096) { return null; }
282 EventsProvider ret_hu_conv = new EventsProvider(null, ret);
283 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };