6 namespace org { namespace ldk { namespace structs {
9 * The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
10 * blocks are connected and disconnected.
12 * Each channel is associated with a [`ChannelMonitor`]. Implementations of this trait are
13 * responsible for maintaining a set of monitors such that they can be updated accordingly as
14 * channel state changes and HTLCs are resolved. See method documentation for specific
17 * Implementations **must** ensure that updates are successfully applied and persisted upon method
18 * completion. If an update fails with a [`PermanentFailure`], then it must immediately shut down
19 * without taking any further action such as persisting the current state.
21 * If an implementation maintains multiple instances of a channel's monitor (e.g., by storing
22 * backup copies), then it must ensure that updates are applied across all instances. Otherwise, it
23 * could result in a revoked transaction being broadcast, allowing the counterparty to claim all
24 * funds in the channel. See [`ChannelMonitorUpdateStatus`] for more details about how to handle
27 * [`PermanentFailure`]: ChannelMonitorUpdateStatus::PermanentFailure
29 public class Watch : CommonBase {
30 internal readonly bindings.LDKWatch bindings_instance;
31 internal Watch(object _dummy, long ptr) : base(ptr) { bindings_instance = null; }
32 private Watch(bindings.LDKWatch arg) : base(bindings.LDKWatch_new(arg)) {
33 this.ptrs_to.AddLast(arg);
34 this.bindings_instance = arg;
37 if (ptr != 0) { bindings.Watch_free(ptr); }
40 public interface WatchInterface {
42 * Watches a channel identified by `funding_txo` using `monitor`.
44 * Implementations are responsible for watching the chain for the funding transaction along
45 * with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
46 * calling [`block_connected`] and [`block_disconnected`] on the monitor.
48 * Note: this interface MUST error with [`ChannelMonitorUpdateStatus::PermanentFailure`] if
49 * the given `funding_txo` has previously been registered via `watch_channel`.
51 * [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
52 * [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
53 * [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
55 ChannelMonitorUpdateStatus watch_channel(OutPoint _funding_txo, ChannelMonitor _monitor);
57 * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
59 * Implementations must call [`update_monitor`] with the given update. See
60 * [`ChannelMonitorUpdateStatus`] for invariants around returning an error.
62 * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
64 ChannelMonitorUpdateStatus update_channel(OutPoint _funding_txo, ChannelMonitorUpdate _update);
66 * Returns any monitor events since the last call. Subsequent calls must only return new
69 * Note that after any block- or transaction-connection calls to a [`ChannelMonitor`], no
70 * further events may be returned here until the [`ChannelMonitor`] has been fully persisted
73 * For details on asynchronous [`ChannelMonitor`] updating and returning
74 * [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
76 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] release_pending_monitor_events();
78 private class LDKWatchHolder { internal Watch held; }
79 private class LDKWatchImpl : bindings.LDKWatch {
80 internal LDKWatchImpl(WatchInterface arg, LDKWatchHolder impl_holder) { this.arg = arg; this.impl_holder = impl_holder; }
81 private WatchInterface arg;
82 private LDKWatchHolder impl_holder;
83 public ChannelMonitorUpdateStatus watch_channel(long _funding_txo, long _monitor) {
84 org.ldk.structs.OutPoint _funding_txo_hu_conv = null; if (_funding_txo < 0 || _funding_txo > 4096) { _funding_txo_hu_conv = new org.ldk.structs.OutPoint(null, _funding_txo); }
85 if (_funding_txo_hu_conv != null) { _funding_txo_hu_conv.ptrs_to.AddLast(this); };
86 org.ldk.structs.ChannelMonitor _monitor_hu_conv = null; if (_monitor < 0 || _monitor > 4096) { _monitor_hu_conv = new org.ldk.structs.ChannelMonitor(null, _monitor); }
87 if (_monitor_hu_conv != null) { _monitor_hu_conv.ptrs_to.AddLast(this); };
88 ChannelMonitorUpdateStatus ret = arg.watch_channel(_funding_txo_hu_conv, _monitor_hu_conv);
92 public ChannelMonitorUpdateStatus update_channel(long _funding_txo, long _update) {
93 org.ldk.structs.OutPoint _funding_txo_hu_conv = null; if (_funding_txo < 0 || _funding_txo > 4096) { _funding_txo_hu_conv = new org.ldk.structs.OutPoint(null, _funding_txo); }
94 if (_funding_txo_hu_conv != null) { _funding_txo_hu_conv.ptrs_to.AddLast(this); };
95 org.ldk.structs.ChannelMonitorUpdate _update_hu_conv = null; if (_update < 0 || _update > 4096) { _update_hu_conv = new org.ldk.structs.ChannelMonitorUpdate(null, _update); }
96 if (_update_hu_conv != null) { _update_hu_conv.ptrs_to.AddLast(this); };
97 ChannelMonitorUpdateStatus ret = arg.update_channel(_funding_txo_hu_conv, _update_hu_conv);
101 public long[] release_pending_monitor_events() {
102 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] ret = arg.release_pending_monitor_events();
104 long[] result = ret != null ? InternalUtils.mapArray(ret, ret_conv_49 => ret_conv_49 == null ? 0 : ret_conv_49.clone_ptr()) : null;
108 public static Watch new_impl(WatchInterface arg) {
109 LDKWatchHolder impl_holder = new LDKWatchHolder();
110 impl_holder.held = new Watch(new LDKWatchImpl(arg, impl_holder));
111 return impl_holder.held;
114 * Watches a channel identified by `funding_txo` using `monitor`.
116 * Implementations are responsible for watching the chain for the funding transaction along
117 * with any spends of outputs returned by [`get_outputs_to_watch`]. In practice, this means
118 * calling [`block_connected`] and [`block_disconnected`] on the monitor.
120 * Note: this interface MUST error with [`ChannelMonitorUpdateStatus::PermanentFailure`] if
121 * the given `funding_txo` has previously been registered via `watch_channel`.
123 * [`get_outputs_to_watch`]: channelmonitor::ChannelMonitor::get_outputs_to_watch
124 * [`block_connected`]: channelmonitor::ChannelMonitor::block_connected
125 * [`block_disconnected`]: channelmonitor::ChannelMonitor::block_disconnected
127 public ChannelMonitorUpdateStatus watch_channel(org.ldk.structs.OutPoint funding_txo, org.ldk.structs.ChannelMonitor monitor) {
128 ChannelMonitorUpdateStatus ret = bindings.Watch_watch_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr, monitor == null ? 0 : monitor.ptr);
130 GC.KeepAlive(funding_txo);
131 GC.KeepAlive(monitor);
132 if (this != null) { this.ptrs_to.AddLast(funding_txo); };
133 if (this != null) { this.ptrs_to.AddLast(monitor); };
138 * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
140 * Implementations must call [`update_monitor`] with the given update. See
141 * [`ChannelMonitorUpdateStatus`] for invariants around returning an error.
143 * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
145 public ChannelMonitorUpdateStatus update_channel(org.ldk.structs.OutPoint funding_txo, org.ldk.structs.ChannelMonitorUpdate update) {
146 ChannelMonitorUpdateStatus ret = bindings.Watch_update_channel(this.ptr, funding_txo == null ? 0 : funding_txo.ptr, update == null ? 0 : update.ptr);
148 GC.KeepAlive(funding_txo);
149 GC.KeepAlive(update);
150 if (this != null) { this.ptrs_to.AddLast(funding_txo); };
151 if (this != null) { this.ptrs_to.AddLast(update); };
156 * Returns any monitor events since the last call. Subsequent calls must only return new
159 * Note that after any block- or transaction-connection calls to a [`ChannelMonitor`], no
160 * further events may be returned here until the [`ChannelMonitor`] has been fully persisted
163 * For details on asynchronous [`ChannelMonitor`] updating and returning
164 * [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
166 public ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] release_pending_monitor_events() {
167 long[] ret = bindings.Watch_release_pending_monitor_events(this.ptr);
169 int ret_conv_49_len = ret.Length;
170 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] ret_conv_49_arr = new ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[ret_conv_49_len];
171 for (int x = 0; x < ret_conv_49_len; x++) {
172 long ret_conv_49 = ret[x];
173 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ ret_conv_49_hu_conv = new ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ(null, ret_conv_49);
174 if (ret_conv_49_hu_conv != null) { ret_conv_49_hu_conv.ptrs_to.AddLast(this); };
175 ret_conv_49_arr[x] = ret_conv_49_hu_conv;
177 return ret_conv_49_arr;