Merge pull request #119 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / RoutingMessageHandler.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
11  * A trait to describe an object which can receive routing messages.
12  * 
13  * # Implementor DoS Warnings
14  * 
15  * For `gossip_queries` messages there are potential DoS vectors when handling
16  * inbound queries. Implementors using an on-disk network graph should be aware of
17  * repeated disk I/O for queries accessing different parts of the network graph.
18  */
19 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
20 public class RoutingMessageHandler extends CommonBase {
21         final bindings.LDKRoutingMessageHandler bindings_instance;
22         RoutingMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
23         private RoutingMessageHandler(bindings.LDKRoutingMessageHandler arg, bindings.LDKMessageSendEventsProvider MessageSendEventsProvider) {
24                 super(bindings.LDKRoutingMessageHandler_new(arg, MessageSendEventsProvider));
25                 this.ptrs_to.add(arg);
26                 this.ptrs_to.add(MessageSendEventsProvider);
27                 this.bindings_instance = arg;
28         }
29         @Override @SuppressWarnings("deprecation")
30         protected void finalize() throws Throwable {
31                 if (ptr != 0) { bindings.RoutingMessageHandler_free(ptr); } super.finalize();
32         }
33
34         public static interface RoutingMessageHandlerInterface {
35                 /**
36                  * Handle an incoming node_announcement message, returning true if it should be forwarded on,
37                  * false or returning an Err otherwise.
38                  */
39                 Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg);
40                 /**
41                  * Handle a channel_announcement message, returning true if it should be forwarded on, false
42                  * or returning an Err otherwise.
43                  */
44                 Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg);
45                 /**
46                  * Handle an incoming channel_update message, returning true if it should be forwarded on,
47                  * false or returning an Err otherwise.
48                  */
49                 Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg);
50                 /**
51                  * Gets channel announcements and updates required to dump our routing table to a remote node,
52                  * starting at the short_channel_id indicated by starting_point and including announcements
53                  * for a single channel.
54                  */
55                 Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ get_next_channel_announcement(long starting_point);
56                 /**
57                  * Gets a node announcement required to dump our routing table to a remote node, starting at
58                  * the node *after* the provided pubkey and including up to one announcement immediately
59                  * higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
60                  * If None is provided for starting_point, we start at the first node.
61                  * 
62                  * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
63                  * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
64                  */
65                 NodeAnnouncement get_next_node_announcement(byte[] starting_point);
66                 /**
67                  * Called when a connection is established with a peer. This can be used to
68                  * perform routing table synchronization using a strategy defined by the
69                  * implementor.
70                  */
71                 void peer_connected(byte[] their_node_id, Init init);
72                 /**
73                  * Handles the reply of a query we initiated to learn about channels
74                  * for a given range of blocks. We can expect to receive one or more
75                  * replies to a single query.
76                  */
77                 Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg);
78                 /**
79                  * Handles the reply of a query we initiated asking for routing gossip
80                  * messages for a list of channels. We should receive this message when
81                  * a node has completed its best effort to send us the pertaining routing
82                  * gossip messages.
83                  */
84                 Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg);
85                 /**
86                  * Handles when a peer asks us to send a list of short_channel_ids
87                  * for the requested range of blocks.
88                  */
89                 Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg);
90                 /**
91                  * Handles when a peer asks us to send routing gossip messages for a
92                  * list of short_channel_ids.
93                  */
94                 Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg);
95                 /**
96                  * Gets the node feature flags which this handler itself supports. All available handlers are
97                  * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
98                  * which are broadcasted in our [`NodeAnnouncement`] message.
99                  */
100                 NodeFeatures provided_node_features();
101                 /**
102                  * Gets the init feature flags which should be sent to the given peer. All available handlers
103                  * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
104                  * which are sent in our [`Init`] message.
105                  * 
106                  * Note that this method is called before [`Self::peer_connected`].
107                  */
108                 InitFeatures provided_init_features(byte[] their_node_id);
109         }
110         private static class LDKRoutingMessageHandlerHolder { RoutingMessageHandler held; }
111         public static RoutingMessageHandler new_impl(RoutingMessageHandlerInterface arg, MessageSendEventsProvider.MessageSendEventsProviderInterface MessageSendEventsProvider_impl) {
112                 final LDKRoutingMessageHandlerHolder impl_holder = new LDKRoutingMessageHandlerHolder();
113                 impl_holder.held = new RoutingMessageHandler(new bindings.LDKRoutingMessageHandler() {
114                         @Override public long handle_node_announcement(long msg) {
115                                 org.ldk.structs.NodeAnnouncement msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.NodeAnnouncement(null, msg); }
116                                 Result_boolLightningErrorZ ret = arg.handle_node_announcement(msg_hu_conv);
117                                 Reference.reachabilityFence(arg);
118                                 long result = ret == null ? 0 : ret.clone_ptr();
119                                 return result;
120                         }
121                         @Override public long handle_channel_announcement(long msg) {
122                                 org.ldk.structs.ChannelAnnouncement msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.ChannelAnnouncement(null, msg); }
123                                 Result_boolLightningErrorZ ret = arg.handle_channel_announcement(msg_hu_conv);
124                                 Reference.reachabilityFence(arg);
125                                 long result = ret == null ? 0 : ret.clone_ptr();
126                                 return result;
127                         }
128                         @Override public long handle_channel_update(long msg) {
129                                 org.ldk.structs.ChannelUpdate msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.ChannelUpdate(null, msg); }
130                                 Result_boolLightningErrorZ ret = arg.handle_channel_update(msg_hu_conv);
131                                 Reference.reachabilityFence(arg);
132                                 long result = ret == null ? 0 : ret.clone_ptr();
133                                 return result;
134                         }
135                         @Override public long get_next_channel_announcement(long starting_point) {
136                                 Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ ret = arg.get_next_channel_announcement(starting_point);
137                                 Reference.reachabilityFence(arg);
138                                 long result = ret == null ? 0 : ret.clone_ptr();
139                                 return result;
140                         }
141                         @Override public long get_next_node_announcement(byte[] starting_point) {
142                                 NodeAnnouncement ret = arg.get_next_node_announcement(starting_point);
143                                 Reference.reachabilityFence(arg);
144                                 long result = ret == null ? 0 : ret.clone_ptr();
145                                 return result;
146                         }
147                         @Override public void peer_connected(byte[] their_node_id, long init) {
148                                 org.ldk.structs.Init init_hu_conv = null; if (init < 0 || init > 4096) { init_hu_conv = new org.ldk.structs.Init(null, init); }
149                                 arg.peer_connected(their_node_id, init_hu_conv);
150                                 Reference.reachabilityFence(arg);
151                         }
152                         @Override public long handle_reply_channel_range(byte[] their_node_id, long msg) {
153                                 org.ldk.structs.ReplyChannelRange msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.ReplyChannelRange(null, msg); }
154                                 if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
155                                 Result_NoneLightningErrorZ ret = arg.handle_reply_channel_range(their_node_id, msg_hu_conv);
156                                 Reference.reachabilityFence(arg);
157                                 long result = ret == null ? 0 : ret.clone_ptr();
158                                 return result;
159                         }
160                         @Override public long handle_reply_short_channel_ids_end(byte[] their_node_id, long msg) {
161                                 org.ldk.structs.ReplyShortChannelIdsEnd msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.ReplyShortChannelIdsEnd(null, msg); }
162                                 if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
163                                 Result_NoneLightningErrorZ ret = arg.handle_reply_short_channel_ids_end(their_node_id, msg_hu_conv);
164                                 Reference.reachabilityFence(arg);
165                                 long result = ret == null ? 0 : ret.clone_ptr();
166                                 return result;
167                         }
168                         @Override public long handle_query_channel_range(byte[] their_node_id, long msg) {
169                                 org.ldk.structs.QueryChannelRange msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.QueryChannelRange(null, msg); }
170                                 if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
171                                 Result_NoneLightningErrorZ ret = arg.handle_query_channel_range(their_node_id, msg_hu_conv);
172                                 Reference.reachabilityFence(arg);
173                                 long result = ret == null ? 0 : ret.clone_ptr();
174                                 return result;
175                         }
176                         @Override public long handle_query_short_channel_ids(byte[] their_node_id, long msg) {
177                                 org.ldk.structs.QueryShortChannelIds msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.QueryShortChannelIds(null, msg); }
178                                 if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
179                                 Result_NoneLightningErrorZ ret = arg.handle_query_short_channel_ids(their_node_id, msg_hu_conv);
180                                 Reference.reachabilityFence(arg);
181                                 long result = ret == null ? 0 : ret.clone_ptr();
182                                 return result;
183                         }
184                         @Override public long provided_node_features() {
185                                 NodeFeatures ret = arg.provided_node_features();
186                                 Reference.reachabilityFence(arg);
187                                 long result = ret == null ? 0 : ret.clone_ptr();
188                                 return result;
189                         }
190                         @Override public long provided_init_features(byte[] their_node_id) {
191                                 InitFeatures ret = arg.provided_init_features(their_node_id);
192                                 Reference.reachabilityFence(arg);
193                                 long result = ret == null ? 0 : ret.clone_ptr();
194                                 return result;
195                         }
196                 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
197                 return impl_holder.held;
198         }
199
200         /**
201          * Gets the underlying MessageSendEventsProvider.
202          */
203         public MessageSendEventsProvider get_message_send_events_provider() {
204                 MessageSendEventsProvider res = new MessageSendEventsProvider(null, bindings.LDKRoutingMessageHandler_get_MessageSendEventsProvider(this.ptr));
205                 this.ptrs_to.add(res);
206                 return res;
207         }
208
209         /**
210          * Handle an incoming node_announcement message, returning true if it should be forwarded on,
211          * false or returning an Err otherwise.
212          */
213         public Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg) {
214                 long ret = bindings.RoutingMessageHandler_handle_node_announcement(this.ptr, msg == null ? 0 : msg.ptr);
215                 Reference.reachabilityFence(this);
216                 Reference.reachabilityFence(msg);
217                 if (ret >= 0 && ret <= 4096) { return null; }
218                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
219                 if (this != null) { this.ptrs_to.add(msg); };
220                 return ret_hu_conv;
221         }
222
223         /**
224          * Handle a channel_announcement message, returning true if it should be forwarded on, false
225          * or returning an Err otherwise.
226          */
227         public Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg) {
228                 long ret = bindings.RoutingMessageHandler_handle_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr);
229                 Reference.reachabilityFence(this);
230                 Reference.reachabilityFence(msg);
231                 if (ret >= 0 && ret <= 4096) { return null; }
232                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
233                 if (this != null) { this.ptrs_to.add(msg); };
234                 return ret_hu_conv;
235         }
236
237         /**
238          * Handle an incoming channel_update message, returning true if it should be forwarded on,
239          * false or returning an Err otherwise.
240          */
241         public Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg) {
242                 long ret = bindings.RoutingMessageHandler_handle_channel_update(this.ptr, msg == null ? 0 : msg.ptr);
243                 Reference.reachabilityFence(this);
244                 Reference.reachabilityFence(msg);
245                 if (ret >= 0 && ret <= 4096) { return null; }
246                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
247                 if (this != null) { this.ptrs_to.add(msg); };
248                 return ret_hu_conv;
249         }
250
251         /**
252          * Gets channel announcements and updates required to dump our routing table to a remote node,
253          * starting at the short_channel_id indicated by starting_point and including announcements
254          * for a single channel.
255          */
256         public Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ get_next_channel_announcement(long starting_point) {
257                 long ret = bindings.RoutingMessageHandler_get_next_channel_announcement(this.ptr, starting_point);
258                 Reference.reachabilityFence(this);
259                 Reference.reachabilityFence(starting_point);
260                 if (ret >= 0 && ret <= 4096) { return null; }
261                 org.ldk.structs.Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ ret_hu_conv = org.ldk.structs.Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ.constr_from_ptr(ret);
262                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
263                 return ret_hu_conv;
264         }
265
266         /**
267          * Gets a node announcement required to dump our routing table to a remote node, starting at
268          * the node *after* the provided pubkey and including up to one announcement immediately
269          * higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
270          * If None is provided for starting_point, we start at the first node.
271          * 
272          * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
273          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
274          */
275         @Nullable
276         public NodeAnnouncement get_next_node_announcement(@Nullable byte[] starting_point) {
277                 long ret = bindings.RoutingMessageHandler_get_next_node_announcement(this.ptr, InternalUtils.check_arr_len(starting_point, 33));
278                 Reference.reachabilityFence(this);
279                 Reference.reachabilityFence(starting_point);
280                 if (ret >= 0 && ret <= 4096) { return null; }
281                 org.ldk.structs.NodeAnnouncement ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeAnnouncement(null, ret); }
282                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
283                 return ret_hu_conv;
284         }
285
286         /**
287          * Called when a connection is established with a peer. This can be used to
288          * perform routing table synchronization using a strategy defined by the
289          * implementor.
290          */
291         public void peer_connected(byte[] their_node_id, Init init) {
292                 bindings.RoutingMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr);
293                 Reference.reachabilityFence(this);
294                 Reference.reachabilityFence(their_node_id);
295                 Reference.reachabilityFence(init);
296                 if (this != null) { this.ptrs_to.add(init); };
297         }
298
299         /**
300          * Handles the reply of a query we initiated to learn about channels
301          * for a given range of blocks. We can expect to receive one or more
302          * replies to a single query.
303          */
304         public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg) {
305                 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
306                 Reference.reachabilityFence(this);
307                 Reference.reachabilityFence(their_node_id);
308                 Reference.reachabilityFence(msg);
309                 if (ret >= 0 && ret <= 4096) { return null; }
310                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
311                 if (this != null) { this.ptrs_to.add(msg); };
312                 return ret_hu_conv;
313         }
314
315         /**
316          * Handles the reply of a query we initiated asking for routing gossip
317          * messages for a list of channels. We should receive this message when
318          * a node has completed its best effort to send us the pertaining routing
319          * gossip messages.
320          */
321         public Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg) {
322                 long ret = bindings.RoutingMessageHandler_handle_reply_short_channel_ids_end(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
323                 Reference.reachabilityFence(this);
324                 Reference.reachabilityFence(their_node_id);
325                 Reference.reachabilityFence(msg);
326                 if (ret >= 0 && ret <= 4096) { return null; }
327                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
328                 if (this != null) { this.ptrs_to.add(msg); };
329                 return ret_hu_conv;
330         }
331
332         /**
333          * Handles when a peer asks us to send a list of short_channel_ids
334          * for the requested range of blocks.
335          */
336         public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg) {
337                 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
338                 Reference.reachabilityFence(this);
339                 Reference.reachabilityFence(their_node_id);
340                 Reference.reachabilityFence(msg);
341                 if (ret >= 0 && ret <= 4096) { return null; }
342                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
343                 if (this != null) { this.ptrs_to.add(msg); };
344                 return ret_hu_conv;
345         }
346
347         /**
348          * Handles when a peer asks us to send routing gossip messages for a
349          * list of short_channel_ids.
350          */
351         public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg) {
352                 long ret = bindings.RoutingMessageHandler_handle_query_short_channel_ids(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
353                 Reference.reachabilityFence(this);
354                 Reference.reachabilityFence(their_node_id);
355                 Reference.reachabilityFence(msg);
356                 if (ret >= 0 && ret <= 4096) { return null; }
357                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
358                 if (this != null) { this.ptrs_to.add(msg); };
359                 return ret_hu_conv;
360         }
361
362         /**
363          * Gets the node feature flags which this handler itself supports. All available handlers are
364          * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
365          * which are broadcasted in our [`NodeAnnouncement`] message.
366          */
367         public NodeFeatures provided_node_features() {
368                 long ret = bindings.RoutingMessageHandler_provided_node_features(this.ptr);
369                 Reference.reachabilityFence(this);
370                 if (ret >= 0 && ret <= 4096) { return null; }
371                 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
372                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
373                 return ret_hu_conv;
374         }
375
376         /**
377          * Gets the init feature flags which should be sent to the given peer. All available handlers
378          * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
379          * which are sent in our [`Init`] message.
380          * 
381          * Note that this method is called before [`Self::peer_connected`].
382          */
383         public InitFeatures provided_init_features(byte[] their_node_id) {
384                 long ret = bindings.RoutingMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
385                 Reference.reachabilityFence(this);
386                 Reference.reachabilityFence(their_node_id);
387                 if (ret >= 0 && ret <= 4096) { return null; }
388                 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
389                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
390                 return ret_hu_conv;
391         }
392
393 }