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, ignored_channels != null ? InternalUtils.mapArray(ignored_channels, ignored_channels_conv_16 => ignored_channels_conv_16 == null ? 0 : ignored_channels_conv_16.ptr) : null);
74 GC.KeepAlive(ignored_channels);
75 int ret_conv_9_len = ret.Length;
76 Balance[] ret_conv_9_arr = new Balance[ret_conv_9_len];
77 for (int j = 0; j < ret_conv_9_len; j++) {
78 long ret_conv_9 = ret[j];
79 org.ldk.structs.Balance ret_conv_9_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret_conv_9);
80 if (ret_conv_9_hu_conv != null) { ret_conv_9_hu_conv.ptrs_to.AddLast(this); };
81 ret_conv_9_arr[j] = ret_conv_9_hu_conv;
83 foreach (ChannelDetails ignored_channels_conv_16 in ignored_channels) { if (this != null) { this.ptrs_to.AddLast(ignored_channels_conv_16); }; };
84 return ret_conv_9_arr;
88 * Gets the [`LockedChannelMonitor`] for a given funding outpoint, returning an `Err` if no
89 * such [`ChannelMonitor`] is currently being monitored for.
91 * Note that the result holds a mutex over our monitor set, and should not be held
94 public Result_LockedChannelMonitorNoneZ get_monitor(org.ldk.structs.OutPoint funding_txo) {
95 long ret = bindings.ChainMonitor_get_monitor(this.ptr, funding_txo == null ? 0 : funding_txo.ptr);
97 GC.KeepAlive(funding_txo);
98 if (ret >= 0 && ret <= 4096) { return null; }
99 Result_LockedChannelMonitorNoneZ ret_hu_conv = Result_LockedChannelMonitorNoneZ.constr_from_ptr(ret);
100 if (this != null) { this.ptrs_to.AddLast(funding_txo); };
105 * Lists the funding outpoint of each [`ChannelMonitor`] being monitored.
107 * Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always
108 * monitoring for on-chain state resolutions.
110 public OutPoint[] list_monitors() {
111 long[] ret = bindings.ChainMonitor_list_monitors(this.ptr);
113 int ret_conv_10_len = ret.Length;
114 OutPoint[] ret_conv_10_arr = new OutPoint[ret_conv_10_len];
115 for (int k = 0; k < ret_conv_10_len; k++) {
116 long ret_conv_10 = ret[k];
117 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); }
118 if (ret_conv_10_hu_conv != null) { ret_conv_10_hu_conv.ptrs_to.AddLast(this); };
119 ret_conv_10_arr[k] = ret_conv_10_hu_conv;
121 return ret_conv_10_arr;
125 * Lists the pending updates for each [`ChannelMonitor`] (by `OutPoint` being monitored).
127 public TwoTuple_OutPointCVec_MonitorUpdateIdZZ[] list_pending_monitor_updates() {
128 long[] ret = bindings.ChainMonitor_list_pending_monitor_updates(this.ptr);
130 int ret_conv_41_len = ret.Length;
131 TwoTuple_OutPointCVec_MonitorUpdateIdZZ[] ret_conv_41_arr = new TwoTuple_OutPointCVec_MonitorUpdateIdZZ[ret_conv_41_len];
132 for (int p = 0; p < ret_conv_41_len; p++) {
133 long ret_conv_41 = ret[p];
134 TwoTuple_OutPointCVec_MonitorUpdateIdZZ ret_conv_41_hu_conv = new TwoTuple_OutPointCVec_MonitorUpdateIdZZ(null, ret_conv_41);
135 if (ret_conv_41_hu_conv != null) { ret_conv_41_hu_conv.ptrs_to.AddLast(this); };
136 ret_conv_41_arr[p] = ret_conv_41_hu_conv;
138 return ret_conv_41_arr;
142 * Indicates the persistence of a [`ChannelMonitor`] has completed after
143 * [`ChannelMonitorUpdateStatus::InProgress`] was returned from an update operation.
145 * Thus, the anticipated use is, at a high level:
146 * 1) This [`ChainMonitor`] calls [`Persist::update_persisted_channel`] which stores the
147 * update to disk and begins updating any remote (e.g. watchtower/backup) copies,
148 * returning [`ChannelMonitorUpdateStatus::InProgress`],
149 * 2) once all remote copies are updated, you call this function with the
150 * `completed_update_id` that completed, and once all pending updates have completed the
151 * channel will be re-enabled.
153 * Returns an [`APIError::APIMisuseError`] if `funding_txo` does not match any currently
154 * registered [`ChannelMonitor`]s.
156 public Result_NoneAPIErrorZ channel_monitor_updated(org.ldk.structs.OutPoint funding_txo, org.ldk.structs.MonitorUpdateId completed_update_id) {
157 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);
159 GC.KeepAlive(funding_txo);
160 GC.KeepAlive(completed_update_id);
161 if (ret >= 0 && ret <= 4096) { return null; }
162 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
163 if (this != null) { this.ptrs_to.AddLast(funding_txo); };
164 if (this != null) { this.ptrs_to.AddLast(completed_update_id); };
169 * Gets a [`Future`] that completes when an event is available either via
170 * [`chain::Watch::release_pending_monitor_events`] or
171 * [`EventsProvider::process_pending_events`].
173 * Note that callbacks registered on the [`Future`] MUST NOT call back into this
174 * [`ChainMonitor`] and should instead register actions to be taken later.
176 * [`EventsProvider::process_pending_events`]: crate::events::EventsProvider::process_pending_events
178 public Future get_update_future() {
179 long ret = bindings.ChainMonitor_get_update_future(this.ptr);
181 if (ret >= 0 && ret <= 4096) { return null; }
182 org.ldk.structs.Future ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.Future(null, ret); }
183 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
188 * Triggers rebroadcasts/fee-bumps of pending claims from a force-closed channel. This is
189 * crucial in preventing certain classes of pinning attacks, detecting substantial mempool
190 * feerate changes between blocks, and ensuring reliability if broadcasting fails. We recommend
191 * invoking this every 30 seconds, or lower if running in an environment with spotty
192 * connections, like on mobile.
194 public void rebroadcast_pending_claims() {
195 bindings.ChainMonitor_rebroadcast_pending_claims(this.ptr);
200 * Constructs a new Listen which calls the relevant methods on this_arg.
201 * This copies the `inner` pointer in this_arg and thus the returned Listen must be freed before this_arg is
203 public Listen as_Listen() {
204 long ret = bindings.ChainMonitor_as_Listen(this.ptr);
206 if (ret >= 0 && ret <= 4096) { return null; }
207 Listen ret_hu_conv = new Listen(null, ret);
208 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
213 * Constructs a new Confirm which calls the relevant methods on this_arg.
214 * This copies the `inner` pointer in this_arg and thus the returned Confirm must be freed before this_arg is
216 public Confirm as_Confirm() {
217 long ret = bindings.ChainMonitor_as_Confirm(this.ptr);
219 if (ret >= 0 && ret <= 4096) { return null; }
220 Confirm ret_hu_conv = new Confirm(null, ret);
221 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
226 * Constructs a new Watch which calls the relevant methods on this_arg.
227 * This copies the `inner` pointer in this_arg and thus the returned Watch must be freed before this_arg is
229 public Watch as_Watch() {
230 long ret = bindings.ChainMonitor_as_Watch(this.ptr);
232 if (ret >= 0 && ret <= 4096) { return null; }
233 Watch ret_hu_conv = new Watch(null, ret);
234 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };
239 * Constructs a new EventsProvider which calls the relevant methods on this_arg.
240 * This copies the `inner` pointer in this_arg and thus the returned EventsProvider must be freed before this_arg is
242 public EventsProvider as_EventsProvider() {
243 long ret = bindings.ChainMonitor_as_EventsProvider(this.ptr);
245 if (ret >= 0 && ret <= 4096) { return null; }
246 EventsProvider ret_hu_conv = new EventsProvider(null, ret);
247 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.AddLast(this); };