6 namespace org { namespace ldk { namespace structs {
10 * An implementation of [`chain::Watch`] for monitoring channels.
12 * Connected and disconnected blocks must be provided to `ChainMonitor` as documented by
13 * [`chain::Watch`]. May be used in conjunction with [`ChannelManager`] to monitor channels locally
14 * or used independently to monitor channels remotely. See the [module-level documentation] for
17 * Note that `ChainMonitor` should regularly trigger rebroadcasts/fee bumps of pending claims from
18 * a force-closed channel. This is crucial in preventing certain classes of pinning attacks,
19 * detecting substantial mempool feerate changes between blocks, and ensuring reliability if
20 * broadcasting fails. We recommend invoking this every 30 seconds, or lower if running in an
21 * environment with spotty connections, like on mobile.
23 * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
24 * [module-level documentation]: crate::chain::chainmonitor
25 * [`rebroadcast_pending_claims`]: Self::rebroadcast_pending_claims
27 public class ChainMonitor : CommonBase {
28 internal ChainMonitor(object _dummy, long ptr) : base(ptr) { }
30 if (ptr != 0) { bindings.ChainMonitor_free(ptr); }
34 * Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
36 * When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
37 * will call back to it indicating transactions and outputs of interest. This allows clients to
38 * pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may
39 * always need to fetch full blocks absent another means for determining which blocks contain
40 * transactions relevant to the watched channels.
42 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) {
43 long ret = bindings.ChainMonitor_new(chain_source.ptr, broadcaster.ptr, logger.ptr, feeest.ptr, persister.ptr);
44 GC.KeepAlive(chain_source);
45 GC.KeepAlive(broadcaster);
48 GC.KeepAlive(persister);
49 if (ret >= 0 && ret <= 4096) { return null; }
50 org.ldk.structs.ChainMonitor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ChainMonitor(null, ret); }
51 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(ret_hu_conv); };
52 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(chain_source); };
53 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(broadcaster); };
54 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(logger); };
55 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(feeest); };
56 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(persister); };
61 * Gets the balances in the contained [`ChannelMonitor`]s which are claimable on-chain or
62 * claims which are awaiting confirmation.
64 * Includes the balances from each [`ChannelMonitor`] *except* those included in
65 * `ignored_channels`, allowing you to filter out balances from channels which are still open
66 * (and whose balance should likely be pulled from the [`ChannelDetails`]).
68 * See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for
69 * inclusion in the return value.
71 public Balance[] get_claimable_balances(ChannelDetails[] ignored_channels) {
72 long ret = bindings.ChainMonitor_get_claimable_balances(this.ptr, InternalUtils.encodeUint64Array(InternalUtils.mapArray(ignored_channels, ignored_channels_conv_16 => ignored_channels_conv_16.ptr)));
74 GC.KeepAlive(ignored_channels);
75 if (ret >= 0 && ret <= 4096) { return null; }
76 int ret_conv_9_len = InternalUtils.getArrayLength(ret);
77 Balance[] ret_conv_9_arr = new Balance[ret_conv_9_len];
78 for (int j = 0; j < ret_conv_9_len; j++) {
79 long ret_conv_9 = InternalUtils.getU64ArrayElem(ret, j);
80 org.ldk.structs.Balance ret_conv_9_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret_conv_9);
81 if (ret_conv_9_hu_conv != null) { ret_conv_9_hu_conv.ptrs_to.AddLast(this); };
82 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
84 bindings.free_buffer(ret);
85 foreach (ChannelDetails ignored_channels_conv_16 in ignored_channels) { if (this != null) { this.ptrs_to.AddLast(ignored_channels_conv_16); }; };
86 return ret_conv_9_arr;
90 * Gets the [`LockedChannelMonitor`] for a given funding outpoint, returning an `Err` if no
91 * such [`ChannelMonitor`] is currently being monitored for.
93 * Note that the result holds a mutex over our monitor set, and should not be held
96 public Result_LockedChannelMonitorNoneZ get_monitor(org.ldk.structs.OutPoint funding_txo) {
97 long ret = bindings.ChainMonitor_get_monitor(this.ptr, funding_txo.ptr);
99 GC.KeepAlive(funding_txo);
100 if (ret >= 0 && ret <= 4096) { return null; }
101 Result_LockedChannelMonitorNoneZ ret_hu_conv = Result_LockedChannelMonitorNoneZ.constr_from_ptr(ret);
102 if (this != null) { this.ptrs_to.AddLast(funding_txo); };
107 * Lists the funding outpoint and channel ID of each [`ChannelMonitor`] being monitored.
109 * Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always
110 * monitoring for on-chain state resolutions.
112 public TwoTuple_OutPointChannelIdZ[] list_monitors() {
113 long ret = bindings.ChainMonitor_list_monitors(this.ptr);
115 if (ret >= 0 && ret <= 4096) { return null; }
116 int ret_conv_29_len = InternalUtils.getArrayLength(ret);
117 TwoTuple_OutPointChannelIdZ[] ret_conv_29_arr = new TwoTuple_OutPointChannelIdZ[ret_conv_29_len];
118 for (int d = 0; d < ret_conv_29_len; d++) {
119 long ret_conv_29 = InternalUtils.getU64ArrayElem(ret, d);
120 TwoTuple_OutPointChannelIdZ ret_conv_29_hu_conv = new TwoTuple_OutPointChannelIdZ(null, ret_conv_29);
121 if (ret_conv_29_hu_conv != null) { ret_conv_29_hu_conv.ptrs_to.AddLast(this); };
122 ret_conv_29_arr[d] = ret_conv_29_hu_conv;
124 bindings.free_buffer(ret);
125 return ret_conv_29_arr;
129 * Lists the pending updates for each [`ChannelMonitor`] (by `OutPoint` being monitored).
131 public TwoTuple_OutPointCVec_MonitorUpdateIdZZ[] list_pending_monitor_updates() {
132 long ret = bindings.ChainMonitor_list_pending_monitor_updates(this.ptr);
134 if (ret >= 0 && ret <= 4096) { return null; }
135 int ret_conv_41_len = InternalUtils.getArrayLength(ret);
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 = InternalUtils.getU64ArrayElem(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.AddLast(this); };
141 ret_conv_41_arr[p] = ret_conv_41_hu_conv;
143 bindings.free_buffer(ret);
144 return ret_conv_41_arr;
148 * Indicates the persistence of a [`ChannelMonitor`] has completed after
149 * [`ChannelMonitorUpdateStatus::InProgress`] was returned from an update operation.
151 * Thus, the anticipated use is, at a high level:
152 * 1) This [`ChainMonitor`] calls [`Persist::update_persisted_channel`] which stores the
153 * update to disk and begins updating any remote (e.g. watchtower/backup) copies,
154 * returning [`ChannelMonitorUpdateStatus::InProgress`],
155 * 2) once all remote copies are updated, you call this function with the
156 * `completed_update_id` that completed, and once all pending updates have completed the
157 * channel will be re-enabled.
159 * Returns an [`APIError::APIMisuseError`] if `funding_txo` does not match any currently
160 * registered [`ChannelMonitor`]s.
162 public Result_NoneAPIErrorZ channel_monitor_updated(org.ldk.structs.OutPoint funding_txo, org.ldk.structs.MonitorUpdateId completed_update_id) {
163 long ret = bindings.ChainMonitor_channel_monitor_updated(this.ptr, funding_txo.ptr, completed_update_id.ptr);
165 GC.KeepAlive(funding_txo);
166 GC.KeepAlive(completed_update_id);
167 if (ret >= 0 && ret <= 4096) { return null; }
168 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
169 if (this != null) { this.ptrs_to.AddLast(funding_txo); };
170 if (this != null) { this.ptrs_to.AddLast(completed_update_id); };
175 * Gets a [`Future`] that completes when an event is available either via
176 * [`chain::Watch::release_pending_monitor_events`] or
177 * [`EventsProvider::process_pending_events`].
179 * Note that callbacks registered on the [`Future`] MUST NOT call back into this
180 * [`ChainMonitor`] and should instead register actions to be taken later.
182 * [`EventsProvider::process_pending_events`]: crate::events::EventsProvider::process_pending_events
184 public Future get_update_future() {
185 long ret = bindings.ChainMonitor_get_update_future(this.ptr);
187 if (ret >= 0 && ret <= 4096) { return null; }
188 org.ldk.structs.Future ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.Future(null, ret); }
189 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
194 * Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is
195 * crucial in preventing certain classes of pinning attacks, detecting substantial mempool
196 * feerate changes between blocks, and ensuring reliability if broadcasting fails. We recommend
197 * invoking this every 30 seconds, or lower if running in an environment with spotty
198 * connections, like on mobile.
200 public void rebroadcast_pending_claims() {
201 bindings.ChainMonitor_rebroadcast_pending_claims(this.ptr);
206 * Triggers rebroadcasts of pending claims from force-closed channels after a transaction
207 * signature generation failure.
209 * `monitor_opt` can be used as a filter to only trigger them for a specific channel monitor.
211 * Note that monitor_opt (or a relevant inner pointer) may be NULL or all-0s to represent None
213 public void signer_unblocked(org.ldk.structs.OutPoint monitor_opt) {
214 bindings.ChainMonitor_signer_unblocked(this.ptr, monitor_opt == null ? 0 : monitor_opt.ptr);
216 GC.KeepAlive(monitor_opt);
217 if (this != null) { this.ptrs_to.AddLast(monitor_opt); };
221 * Archives fully resolved channel monitors by calling [`Persist::archive_persisted_channel`].
223 * This is useful for pruning fully resolved monitors from the monitor set and primary
224 * storage so they are not kept in memory and reloaded on restart.
226 * Should be called occasionally (once every handful of blocks or on startup).
228 * Depending on the implementation of [`Persist::archive_persisted_channel`] the monitor
229 * data could be moved to an archive location or removed entirely.
231 public void archive_fully_resolved_channel_monitors() {
232 bindings.ChainMonitor_archive_fully_resolved_channel_monitors(this.ptr);
237 * Constructs a new Listen which calls the relevant methods on this_arg.
238 * This copies the `inner` pointer in this_arg and thus the returned Listen must be freed before this_arg is
240 public Listen as_Listen() {
241 long ret = bindings.ChainMonitor_as_Listen(this.ptr);
243 if (ret >= 0 && ret <= 4096) { return null; }
244 Listen ret_hu_conv = new Listen(null, ret);
245 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
250 * Constructs a new Confirm which calls the relevant methods on this_arg.
251 * This copies the `inner` pointer in this_arg and thus the returned Confirm must be freed before this_arg is
253 public Confirm as_Confirm() {
254 long ret = bindings.ChainMonitor_as_Confirm(this.ptr);
256 if (ret >= 0 && ret <= 4096) { return null; }
257 Confirm ret_hu_conv = new Confirm(null, ret);
258 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
263 * Constructs a new Watch which calls the relevant methods on this_arg.
264 * This copies the `inner` pointer in this_arg and thus the returned Watch must be freed before this_arg is
266 public Watch as_Watch() {
267 long ret = bindings.ChainMonitor_as_Watch(this.ptr);
269 if (ret >= 0 && ret <= 4096) { return null; }
270 Watch ret_hu_conv = new Watch(null, ret);
271 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
276 * Constructs a new EventsProvider which calls the relevant methods on this_arg.
277 * This copies the `inner` pointer in this_arg and thus the returned EventsProvider must be freed before this_arg is
279 public EventsProvider as_EventsProvider() {
280 long ret = bindings.ChainMonitor_as_EventsProvider(this.ptr);
282 if (ret >= 0 && ret <= 4096) { return null; }
283 EventsProvider ret_hu_conv = new EventsProvider(null, ret);
284 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };