040eb644acf91382a4b6ea9cdc00588af6c5e553
[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 a subset of the channel announcements and updates required to dump our routing table
52                  * to a remote node, starting at the short_channel_id indicated by starting_point and
53                  * including the batch_amount entries immediately higher in numerical value than starting_point.
54                  */
55                 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] get_next_channel_announcements(long starting_point, byte batch_amount);
56                 /**
57                  * Gets a subset of the node announcements required to dump our routing table to a remote node,
58                  * starting at the node *after* the provided publickey and including batch_amount entries
59                  * immediately 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                  */
64                 NodeAnnouncement[] get_next_node_announcements(byte[] starting_point, byte batch_amount);
65                 /**
66                  * Called when a connection is established with a peer. This can be used to
67                  * perform routing table synchronization using a strategy defined by the
68                  * implementor.
69                  */
70                 void sync_routing_table(byte[] their_node_id, Init init);
71                 /**
72                  * Handles the reply of a query we initiated to learn about channels
73                  * for a given range of blocks. We can expect to receive one or more
74                  * replies to a single query.
75                  */
76                 Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg);
77                 /**
78                  * Handles the reply of a query we initiated asking for routing gossip
79                  * messages for a list of channels. We should receive this message when
80                  * a node has completed its best effort to send us the pertaining routing
81                  * gossip messages.
82                  */
83                 Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg);
84                 /**
85                  * Handles when a peer asks us to send a list of short_channel_ids
86                  * for the requested range of blocks.
87                  */
88                 Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg);
89                 /**
90                  * Handles when a peer asks us to send routing gossip messages for a
91                  * list of short_channel_ids.
92                  */
93                 Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg);
94         }
95         private static class LDKRoutingMessageHandlerHolder { RoutingMessageHandler held; }
96         public static RoutingMessageHandler new_impl(RoutingMessageHandlerInterface arg, MessageSendEventsProvider.MessageSendEventsProviderInterface MessageSendEventsProvider_impl) {
97                 final LDKRoutingMessageHandlerHolder impl_holder = new LDKRoutingMessageHandlerHolder();
98                 impl_holder.held = new RoutingMessageHandler(new bindings.LDKRoutingMessageHandler() {
99                         @Override public long handle_node_announcement(long msg) {
100                                 NodeAnnouncement msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new NodeAnnouncement(null, msg); }
101                                 Result_boolLightningErrorZ ret = arg.handle_node_announcement(msg_hu_conv);
102                                 long result = ret == null ? 0 : ret.clone_ptr();
103                                 return result;
104                         }
105                         @Override public long handle_channel_announcement(long msg) {
106                                 ChannelAnnouncement msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ChannelAnnouncement(null, msg); }
107                                 Result_boolLightningErrorZ ret = arg.handle_channel_announcement(msg_hu_conv);
108                                 long result = ret == null ? 0 : ret.clone_ptr();
109                                 return result;
110                         }
111                         @Override public long handle_channel_update(long msg) {
112                                 ChannelUpdate msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ChannelUpdate(null, msg); }
113                                 Result_boolLightningErrorZ ret = arg.handle_channel_update(msg_hu_conv);
114                                 long result = ret == null ? 0 : ret.clone_ptr();
115                                 return result;
116                         }
117                         @Override public long[] get_next_channel_announcements(long starting_point, byte batch_amount) {
118                                 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] ret = arg.get_next_channel_announcements(starting_point, batch_amount);
119                                 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_59 -> ret_conv_59 == null ? 0 : ret_conv_59.clone_ptr()).toArray() : null;
120                                 return result;
121                         }
122                         @Override public long[] get_next_node_announcements(byte[] starting_point, byte batch_amount) {
123                                 NodeAnnouncement[] ret = arg.get_next_node_announcements(starting_point, batch_amount);
124                                 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_18 -> ret_conv_18 == null ? 0 : ret_conv_18.clone_ptr()).toArray() : null;
125                                 return result;
126                         }
127                         @Override public void sync_routing_table(byte[] their_node_id, long init) {
128                                 Init init_hu_conv = null; if (init < 0 || init > 4096) { init_hu_conv = new Init(null, init); }
129                                 arg.sync_routing_table(their_node_id, init_hu_conv);
130                         }
131                         @Override public long handle_reply_channel_range(byte[] their_node_id, long msg) {
132                                 ReplyChannelRange msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ReplyChannelRange(null, msg); }
133                                 msg_hu_conv.ptrs_to.add(this);
134                                 Result_NoneLightningErrorZ ret = arg.handle_reply_channel_range(their_node_id, msg_hu_conv);
135                                 long result = ret == null ? 0 : ret.clone_ptr();
136                                 return result;
137                         }
138                         @Override public long handle_reply_short_channel_ids_end(byte[] their_node_id, long msg) {
139                                 ReplyShortChannelIdsEnd msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ReplyShortChannelIdsEnd(null, msg); }
140                                 msg_hu_conv.ptrs_to.add(this);
141                                 Result_NoneLightningErrorZ ret = arg.handle_reply_short_channel_ids_end(their_node_id, msg_hu_conv);
142                                 long result = ret == null ? 0 : ret.clone_ptr();
143                                 return result;
144                         }
145                         @Override public long handle_query_channel_range(byte[] their_node_id, long msg) {
146                                 QueryChannelRange msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new QueryChannelRange(null, msg); }
147                                 msg_hu_conv.ptrs_to.add(this);
148                                 Result_NoneLightningErrorZ ret = arg.handle_query_channel_range(their_node_id, msg_hu_conv);
149                                 long result = ret == null ? 0 : ret.clone_ptr();
150                                 return result;
151                         }
152                         @Override public long handle_query_short_channel_ids(byte[] their_node_id, long msg) {
153                                 QueryShortChannelIds msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new QueryShortChannelIds(null, msg); }
154                                 msg_hu_conv.ptrs_to.add(this);
155                                 Result_NoneLightningErrorZ ret = arg.handle_query_short_channel_ids(their_node_id, msg_hu_conv);
156                                 long result = ret == null ? 0 : ret.clone_ptr();
157                                 return result;
158                         }
159                 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
160                 return impl_holder.held;
161         }
162
163         /**
164          * Gets the underlying MessageSendEventsProvider.
165          */
166         public MessageSendEventsProvider get_message_send_events_provider() {
167                 MessageSendEventsProvider res = new MessageSendEventsProvider(null, bindings.LDKRoutingMessageHandler_get_MessageSendEventsProvider(this.ptr));
168                 this.ptrs_to.add(res);
169                 return res;
170         }
171
172         /**
173          * Handle an incoming node_announcement message, returning true if it should be forwarded on,
174          * false or returning an Err otherwise.
175          */
176         public Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg) {
177                 long ret = bindings.RoutingMessageHandler_handle_node_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
178                 Reference.reachabilityFence(this);
179                 Reference.reachabilityFence(msg);
180                 if (ret >= 0 && ret <= 4096) { return null; }
181                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
182                 this.ptrs_to.add(msg);
183                 return ret_hu_conv;
184         }
185
186         /**
187          * Handle a channel_announcement message, returning true if it should be forwarded on, false
188          * or returning an Err otherwise.
189          */
190         public Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg) {
191                 long ret = bindings.RoutingMessageHandler_handle_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
192                 Reference.reachabilityFence(this);
193                 Reference.reachabilityFence(msg);
194                 if (ret >= 0 && ret <= 4096) { return null; }
195                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
196                 this.ptrs_to.add(msg);
197                 return ret_hu_conv;
198         }
199
200         /**
201          * Handle an incoming channel_update message, returning true if it should be forwarded on,
202          * false or returning an Err otherwise.
203          */
204         public Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg) {
205                 long ret = bindings.RoutingMessageHandler_handle_channel_update(this.ptr, msg == null ? 0 : msg.ptr & ~1);
206                 Reference.reachabilityFence(this);
207                 Reference.reachabilityFence(msg);
208                 if (ret >= 0 && ret <= 4096) { return null; }
209                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
210                 this.ptrs_to.add(msg);
211                 return ret_hu_conv;
212         }
213
214         /**
215          * Gets a subset of the channel announcements and updates required to dump our routing table
216          * to a remote node, starting at the short_channel_id indicated by starting_point and
217          * including the batch_amount entries immediately higher in numerical value than starting_point.
218          */
219         public ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] get_next_channel_announcements(long starting_point, byte batch_amount) {
220                 long[] ret = bindings.RoutingMessageHandler_get_next_channel_announcements(this.ptr, starting_point, batch_amount);
221                 Reference.reachabilityFence(this);
222                 Reference.reachabilityFence(starting_point);
223                 Reference.reachabilityFence(batch_amount);
224                 int ret_conv_59_len = ret.length;
225                 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] ret_conv_59_arr = new ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[ret_conv_59_len];
226                 for (int h = 0; h < ret_conv_59_len; h++) {
227                         long ret_conv_59 = ret[h];
228                         ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ ret_conv_59_hu_conv = new ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ(null, ret_conv_59);
229                         ret_conv_59_hu_conv.ptrs_to.add(this);
230                         ret_conv_59_arr[h] = ret_conv_59_hu_conv;
231                 }
232                 return ret_conv_59_arr;
233         }
234
235         /**
236          * Gets a subset of the node announcements required to dump our routing table to a remote node,
237          * starting at the node *after* the provided publickey and including batch_amount entries
238          * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
239          * If None is provided for starting_point, we start at the first node.
240          * 
241          * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
242          */
243         public NodeAnnouncement[] get_next_node_announcements(@Nullable byte[] starting_point, byte batch_amount) {
244                 long[] ret = bindings.RoutingMessageHandler_get_next_node_announcements(this.ptr, InternalUtils.check_arr_len(starting_point, 33), batch_amount);
245                 Reference.reachabilityFence(this);
246                 Reference.reachabilityFence(starting_point);
247                 Reference.reachabilityFence(batch_amount);
248                 int ret_conv_18_len = ret.length;
249                 NodeAnnouncement[] ret_conv_18_arr = new NodeAnnouncement[ret_conv_18_len];
250                 for (int s = 0; s < ret_conv_18_len; s++) {
251                         long ret_conv_18 = ret[s];
252                         NodeAnnouncement ret_conv_18_hu_conv = null; if (ret_conv_18 < 0 || ret_conv_18 > 4096) { ret_conv_18_hu_conv = new NodeAnnouncement(null, ret_conv_18); }
253                         ret_conv_18_hu_conv.ptrs_to.add(this);
254                         ret_conv_18_arr[s] = ret_conv_18_hu_conv;
255                 }
256                 return ret_conv_18_arr;
257         }
258
259         /**
260          * Called when a connection is established with a peer. This can be used to
261          * perform routing table synchronization using a strategy defined by the
262          * implementor.
263          */
264         public void sync_routing_table(byte[] their_node_id, Init init) {
265                 bindings.RoutingMessageHandler_sync_routing_table(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr & ~1);
266                 Reference.reachabilityFence(this);
267                 Reference.reachabilityFence(their_node_id);
268                 Reference.reachabilityFence(init);
269                 this.ptrs_to.add(init);
270         }
271
272         /**
273          * Handles the reply of a query we initiated to learn about channels
274          * for a given range of blocks. We can expect to receive one or more
275          * replies to a single query.
276          */
277         public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg) {
278                 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr & ~1);
279                 Reference.reachabilityFence(this);
280                 Reference.reachabilityFence(their_node_id);
281                 Reference.reachabilityFence(msg);
282                 if (ret >= 0 && ret <= 4096) { return null; }
283                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
284                 return ret_hu_conv;
285         }
286
287         /**
288          * Handles the reply of a query we initiated asking for routing gossip
289          * messages for a list of channels. We should receive this message when
290          * a node has completed its best effort to send us the pertaining routing
291          * gossip messages.
292          */
293         public Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg) {
294                 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 & ~1);
295                 Reference.reachabilityFence(this);
296                 Reference.reachabilityFence(their_node_id);
297                 Reference.reachabilityFence(msg);
298                 if (ret >= 0 && ret <= 4096) { return null; }
299                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
300                 return ret_hu_conv;
301         }
302
303         /**
304          * Handles when a peer asks us to send a list of short_channel_ids
305          * for the requested range of blocks.
306          */
307         public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg) {
308                 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr & ~1);
309                 Reference.reachabilityFence(this);
310                 Reference.reachabilityFence(their_node_id);
311                 Reference.reachabilityFence(msg);
312                 if (ret >= 0 && ret <= 4096) { return null; }
313                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
314                 return ret_hu_conv;
315         }
316
317         /**
318          * Handles when a peer asks us to send routing gossip messages for a
319          * list of short_channel_ids.
320          */
321         public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg) {
322                 long ret = bindings.RoutingMessageHandler_handle_query_short_channel_ids(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr & ~1);
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                 return ret_hu_conv;
329         }
330
331 }