39a6f5d00ac52459717555e37235bc5590060e7e
[ldk-java] / c_sharp / src / org / ldk / structs / Watch.cs
1 using org.ldk.impl;
2 using org.ldk.enums;
3 using org.ldk.util;
4 using System;
5
6 namespace org { namespace ldk { namespace structs {
7
8 /**
9  * The `Watch` trait defines behavior for watching on-chain activity pertaining to channels as
10  * blocks are connected and disconnected.
11  * 
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
15  * requirements.
16  * 
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.
20  * 
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
25  * multiple instances.
26  * 
27  * [`PermanentFailure`]: ChannelMonitorUpdateStatus::PermanentFailure
28  */
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;
35         }
36         ~Watch() {
37                 if (ptr != 0) { bindings.Watch_free(ptr); }
38         }
39
40         public interface WatchInterface {
41                 /**
42                  * Watches a channel identified by `funding_txo` using `monitor`.
43                  * 
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.
47                  * 
48                  * Note: this interface MUST error with [`ChannelMonitorUpdateStatus::PermanentFailure`] if
49                  * the given `funding_txo` has previously been registered via `watch_channel`.
50                  * 
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
54                  */
55                 ChannelMonitorUpdateStatus watch_channel(OutPoint _funding_txo, ChannelMonitor _monitor);
56                 /**
57                  * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
58                  * 
59                  * Implementations must call [`update_monitor`] with the given update. See
60                  * [`ChannelMonitorUpdateStatus`] for invariants around returning an error.
61                  * 
62                  * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
63                  */
64                 ChannelMonitorUpdateStatus update_channel(OutPoint _funding_txo, ChannelMonitorUpdate _update);
65                 /**
66                  * Returns any monitor events since the last call. Subsequent calls must only return new
67                  * events.
68                  * 
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
71                  * to disk.
72                  * 
73                  * For details on asynchronous [`ChannelMonitor`] updating and returning
74                  * [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
75                  */
76                 ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] release_pending_monitor_events();
77         }
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);
89                                 GC.KeepAlive(arg);
90                         return ret;
91                 }
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);
98                                 GC.KeepAlive(arg);
99                         return ret;
100                 }
101                 public long[] release_pending_monitor_events() {
102                         ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] ret = arg.release_pending_monitor_events();
103                                 GC.KeepAlive(arg);
104                         long[] result = ret != null ? InternalUtils.mapArray(ret, ret_conv_49 => ret_conv_49 == null ? 0 : ret_conv_49.clone_ptr()) : null;
105                         return result;
106                 }
107         }
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;
112         }
113         /**
114          * Watches a channel identified by `funding_txo` using `monitor`.
115          * 
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.
119          * 
120          * Note: this interface MUST error with [`ChannelMonitorUpdateStatus::PermanentFailure`] if
121          * the given `funding_txo` has previously been registered via `watch_channel`.
122          * 
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
126          */
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);
129                 GC.KeepAlive(this);
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); };
134                 return ret;
135         }
136
137         /**
138          * Updates a channel identified by `funding_txo` by applying `update` to its monitor.
139          * 
140          * Implementations must call [`update_monitor`] with the given update. See
141          * [`ChannelMonitorUpdateStatus`] for invariants around returning an error.
142          * 
143          * [`update_monitor`]: channelmonitor::ChannelMonitor::update_monitor
144          */
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);
147                 GC.KeepAlive(this);
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); };
152                 return ret;
153         }
154
155         /**
156          * Returns any monitor events since the last call. Subsequent calls must only return new
157          * events.
158          * 
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
161          * to disk.
162          * 
163          * For details on asynchronous [`ChannelMonitor`] updating and returning
164          * [`MonitorEvent::Completed`] here, see [`ChannelMonitorUpdateStatus::InProgress`].
165          */
166         public ThreeTuple_OutPointCVec_MonitorEventZPublicKeyZ[] release_pending_monitor_events() {
167                 long[] ret = bindings.Watch_release_pending_monitor_events(this.ptr);
168                 GC.KeepAlive(this);
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;
176                 }
177                 return ret_conv_49_arr;
178         }
179
180 }
181 } } }