094779c6005025019b7b771cdda2f93ad95ae526
[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                  * May return an `Err(())` if the features the peer supports are not sufficient to communicate
72                  * with us. Implementors should be somewhat conservative about doing so, however, as other
73                  * message handlers may still wish to communicate with this peer.
74                  */
75                 Result_NoneNoneZ peer_connected(byte[] their_node_id, Init init);
76                 /**
77                  * Handles the reply of a query we initiated to learn about channels
78                  * for a given range of blocks. We can expect to receive one or more
79                  * replies to a single query.
80                  */
81                 Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg);
82                 /**
83                  * Handles the reply of a query we initiated asking for routing gossip
84                  * messages for a list of channels. We should receive this message when
85                  * a node has completed its best effort to send us the pertaining routing
86                  * gossip messages.
87                  */
88                 Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg);
89                 /**
90                  * Handles when a peer asks us to send a list of short_channel_ids
91                  * for the requested range of blocks.
92                  */
93                 Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg);
94                 /**
95                  * Handles when a peer asks us to send routing gossip messages for a
96                  * list of short_channel_ids.
97                  */
98                 Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg);
99                 /**
100                  * Gets the node feature flags which this handler itself supports. All available handlers are
101                  * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
102                  * which are broadcasted in our [`NodeAnnouncement`] message.
103                  */
104                 NodeFeatures provided_node_features();
105                 /**
106                  * Gets the init feature flags which should be sent to the given peer. All available handlers
107                  * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
108                  * which are sent in our [`Init`] message.
109                  * 
110                  * Note that this method is called before [`Self::peer_connected`].
111                  */
112                 InitFeatures provided_init_features(byte[] their_node_id);
113         }
114         private static class LDKRoutingMessageHandlerHolder { RoutingMessageHandler held; }
115         public static RoutingMessageHandler new_impl(RoutingMessageHandlerInterface arg, MessageSendEventsProvider.MessageSendEventsProviderInterface MessageSendEventsProvider_impl) {
116                 final LDKRoutingMessageHandlerHolder impl_holder = new LDKRoutingMessageHandlerHolder();
117                 impl_holder.held = new RoutingMessageHandler(new bindings.LDKRoutingMessageHandler() {
118                         @Override public long handle_node_announcement(long msg) {
119                                 org.ldk.structs.NodeAnnouncement msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.NodeAnnouncement(null, msg); }
120                                 Result_boolLightningErrorZ ret = arg.handle_node_announcement(msg_hu_conv);
121                                 Reference.reachabilityFence(arg);
122                                 long result = ret == null ? 0 : ret.clone_ptr();
123                                 return result;
124                         }
125                         @Override public long handle_channel_announcement(long msg) {
126                                 org.ldk.structs.ChannelAnnouncement msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.ChannelAnnouncement(null, msg); }
127                                 Result_boolLightningErrorZ ret = arg.handle_channel_announcement(msg_hu_conv);
128                                 Reference.reachabilityFence(arg);
129                                 long result = ret == null ? 0 : ret.clone_ptr();
130                                 return result;
131                         }
132                         @Override public long handle_channel_update(long msg) {
133                                 org.ldk.structs.ChannelUpdate msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.ChannelUpdate(null, msg); }
134                                 Result_boolLightningErrorZ ret = arg.handle_channel_update(msg_hu_conv);
135                                 Reference.reachabilityFence(arg);
136                                 long result = ret == null ? 0 : ret.clone_ptr();
137                                 return result;
138                         }
139                         @Override public long get_next_channel_announcement(long starting_point) {
140                                 Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ ret = arg.get_next_channel_announcement(starting_point);
141                                 Reference.reachabilityFence(arg);
142                                 long result = ret == null ? 0 : ret.clone_ptr();
143                                 return result;
144                         }
145                         @Override public long get_next_node_announcement(byte[] starting_point) {
146                                 NodeAnnouncement ret = arg.get_next_node_announcement(starting_point);
147                                 Reference.reachabilityFence(arg);
148                                 long result = ret == null ? 0 : ret.clone_ptr();
149                                 return result;
150                         }
151                         @Override public long peer_connected(byte[] their_node_id, long init) {
152                                 org.ldk.structs.Init init_hu_conv = null; if (init < 0 || init > 4096) { init_hu_conv = new org.ldk.structs.Init(null, init); }
153                                 Result_NoneNoneZ ret = arg.peer_connected(their_node_id, init_hu_conv);
154                                 Reference.reachabilityFence(arg);
155                                 long result = ret == null ? 0 : ret.clone_ptr();
156                                 return result;
157                         }
158                         @Override public long handle_reply_channel_range(byte[] their_node_id, long msg) {
159                                 org.ldk.structs.ReplyChannelRange msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.ReplyChannelRange(null, msg); }
160                                 if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
161                                 Result_NoneLightningErrorZ ret = arg.handle_reply_channel_range(their_node_id, msg_hu_conv);
162                                 Reference.reachabilityFence(arg);
163                                 long result = ret == null ? 0 : ret.clone_ptr();
164                                 return result;
165                         }
166                         @Override public long handle_reply_short_channel_ids_end(byte[] their_node_id, long msg) {
167                                 org.ldk.structs.ReplyShortChannelIdsEnd msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.ReplyShortChannelIdsEnd(null, msg); }
168                                 if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
169                                 Result_NoneLightningErrorZ ret = arg.handle_reply_short_channel_ids_end(their_node_id, msg_hu_conv);
170                                 Reference.reachabilityFence(arg);
171                                 long result = ret == null ? 0 : ret.clone_ptr();
172                                 return result;
173                         }
174                         @Override public long handle_query_channel_range(byte[] their_node_id, long msg) {
175                                 org.ldk.structs.QueryChannelRange msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.QueryChannelRange(null, msg); }
176                                 if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
177                                 Result_NoneLightningErrorZ ret = arg.handle_query_channel_range(their_node_id, msg_hu_conv);
178                                 Reference.reachabilityFence(arg);
179                                 long result = ret == null ? 0 : ret.clone_ptr();
180                                 return result;
181                         }
182                         @Override public long handle_query_short_channel_ids(byte[] their_node_id, long msg) {
183                                 org.ldk.structs.QueryShortChannelIds msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new org.ldk.structs.QueryShortChannelIds(null, msg); }
184                                 if (msg_hu_conv != null) { msg_hu_conv.ptrs_to.add(this); };
185                                 Result_NoneLightningErrorZ ret = arg.handle_query_short_channel_ids(their_node_id, msg_hu_conv);
186                                 Reference.reachabilityFence(arg);
187                                 long result = ret == null ? 0 : ret.clone_ptr();
188                                 return result;
189                         }
190                         @Override public long provided_node_features() {
191                                 NodeFeatures ret = arg.provided_node_features();
192                                 Reference.reachabilityFence(arg);
193                                 long result = ret == null ? 0 : ret.clone_ptr();
194                                 return result;
195                         }
196                         @Override public long provided_init_features(byte[] their_node_id) {
197                                 InitFeatures ret = arg.provided_init_features(their_node_id);
198                                 Reference.reachabilityFence(arg);
199                                 long result = ret == null ? 0 : ret.clone_ptr();
200                                 return result;
201                         }
202                 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
203                 return impl_holder.held;
204         }
205
206         /**
207          * Gets the underlying MessageSendEventsProvider.
208          */
209         public MessageSendEventsProvider get_message_send_events_provider() {
210                 MessageSendEventsProvider res = new MessageSendEventsProvider(null, bindings.LDKRoutingMessageHandler_get_MessageSendEventsProvider(this.ptr));
211                 this.ptrs_to.add(res);
212                 return res;
213         }
214
215         /**
216          * Handle an incoming node_announcement message, returning true if it should be forwarded on,
217          * false or returning an Err otherwise.
218          */
219         public Result_boolLightningErrorZ handle_node_announcement(org.ldk.structs.NodeAnnouncement msg) {
220                 long ret = bindings.RoutingMessageHandler_handle_node_announcement(this.ptr, msg == null ? 0 : msg.ptr);
221                 Reference.reachabilityFence(this);
222                 Reference.reachabilityFence(msg);
223                 if (ret >= 0 && ret <= 4096) { return null; }
224                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
225                 if (this != null) { this.ptrs_to.add(msg); };
226                 return ret_hu_conv;
227         }
228
229         /**
230          * Handle a channel_announcement message, returning true if it should be forwarded on, false
231          * or returning an Err otherwise.
232          */
233         public Result_boolLightningErrorZ handle_channel_announcement(org.ldk.structs.ChannelAnnouncement msg) {
234                 long ret = bindings.RoutingMessageHandler_handle_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr);
235                 Reference.reachabilityFence(this);
236                 Reference.reachabilityFence(msg);
237                 if (ret >= 0 && ret <= 4096) { return null; }
238                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
239                 if (this != null) { this.ptrs_to.add(msg); };
240                 return ret_hu_conv;
241         }
242
243         /**
244          * Handle an incoming channel_update message, returning true if it should be forwarded on,
245          * false or returning an Err otherwise.
246          */
247         public Result_boolLightningErrorZ handle_channel_update(org.ldk.structs.ChannelUpdate msg) {
248                 long ret = bindings.RoutingMessageHandler_handle_channel_update(this.ptr, msg == null ? 0 : msg.ptr);
249                 Reference.reachabilityFence(this);
250                 Reference.reachabilityFence(msg);
251                 if (ret >= 0 && ret <= 4096) { return null; }
252                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
253                 if (this != null) { this.ptrs_to.add(msg); };
254                 return ret_hu_conv;
255         }
256
257         /**
258          * Gets channel announcements and updates required to dump our routing table to a remote node,
259          * starting at the short_channel_id indicated by starting_point and including announcements
260          * for a single channel.
261          */
262         public Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ get_next_channel_announcement(long starting_point) {
263                 long ret = bindings.RoutingMessageHandler_get_next_channel_announcement(this.ptr, starting_point);
264                 Reference.reachabilityFence(this);
265                 Reference.reachabilityFence(starting_point);
266                 if (ret >= 0 && ret <= 4096) { return null; }
267                 org.ldk.structs.Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ ret_hu_conv = org.ldk.structs.Option_C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZZ.constr_from_ptr(ret);
268                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
269                 return ret_hu_conv;
270         }
271
272         /**
273          * Gets a node announcement required to dump our routing table to a remote node, starting at
274          * the node *after* the provided pubkey and including up to one announcement immediately
275          * higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
276          * If None is provided for starting_point, we start at the first node.
277          * 
278          * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
279          * Note that the return value (or a relevant inner pointer) may be NULL or all-0s to represent None
280          */
281         @Nullable
282         public NodeAnnouncement get_next_node_announcement(@Nullable byte[] starting_point) {
283                 long ret = bindings.RoutingMessageHandler_get_next_node_announcement(this.ptr, InternalUtils.check_arr_len(starting_point, 33));
284                 Reference.reachabilityFence(this);
285                 Reference.reachabilityFence(starting_point);
286                 if (ret >= 0 && ret <= 4096) { return null; }
287                 org.ldk.structs.NodeAnnouncement ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeAnnouncement(null, ret); }
288                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
289                 return ret_hu_conv;
290         }
291
292         /**
293          * Called when a connection is established with a peer. This can be used to
294          * perform routing table synchronization using a strategy defined by the
295          * implementor.
296          * 
297          * May return an `Err(())` if the features the peer supports are not sufficient to communicate
298          * with us. Implementors should be somewhat conservative about doing so, however, as other
299          * message handlers may still wish to communicate with this peer.
300          */
301         public Result_NoneNoneZ peer_connected(byte[] their_node_id, org.ldk.structs.Init init) {
302                 long ret = bindings.RoutingMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr);
303                 Reference.reachabilityFence(this);
304                 Reference.reachabilityFence(their_node_id);
305                 Reference.reachabilityFence(init);
306                 if (ret >= 0 && ret <= 4096) { return null; }
307                 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
308                 if (this != null) { this.ptrs_to.add(init); };
309                 return ret_hu_conv;
310         }
311
312         /**
313          * Handles the reply of a query we initiated to learn about channels
314          * for a given range of blocks. We can expect to receive one or more
315          * replies to a single query.
316          */
317         public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, org.ldk.structs.ReplyChannelRange msg) {
318                 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
319                 Reference.reachabilityFence(this);
320                 Reference.reachabilityFence(their_node_id);
321                 Reference.reachabilityFence(msg);
322                 if (ret >= 0 && ret <= 4096) { return null; }
323                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
324                 if (this != null) { this.ptrs_to.add(msg); };
325                 return ret_hu_conv;
326         }
327
328         /**
329          * Handles the reply of a query we initiated asking for routing gossip
330          * messages for a list of channels. We should receive this message when
331          * a node has completed its best effort to send us the pertaining routing
332          * gossip messages.
333          */
334         public Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, org.ldk.structs.ReplyShortChannelIdsEnd msg) {
335                 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);
336                 Reference.reachabilityFence(this);
337                 Reference.reachabilityFence(their_node_id);
338                 Reference.reachabilityFence(msg);
339                 if (ret >= 0 && ret <= 4096) { return null; }
340                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
341                 if (this != null) { this.ptrs_to.add(msg); };
342                 return ret_hu_conv;
343         }
344
345         /**
346          * Handles when a peer asks us to send a list of short_channel_ids
347          * for the requested range of blocks.
348          */
349         public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, org.ldk.structs.QueryChannelRange msg) {
350                 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
351                 Reference.reachabilityFence(this);
352                 Reference.reachabilityFence(their_node_id);
353                 Reference.reachabilityFence(msg);
354                 if (ret >= 0 && ret <= 4096) { return null; }
355                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
356                 if (this != null) { this.ptrs_to.add(msg); };
357                 return ret_hu_conv;
358         }
359
360         /**
361          * Handles when a peer asks us to send routing gossip messages for a
362          * list of short_channel_ids.
363          */
364         public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, org.ldk.structs.QueryShortChannelIds msg) {
365                 long ret = bindings.RoutingMessageHandler_handle_query_short_channel_ids(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr);
366                 Reference.reachabilityFence(this);
367                 Reference.reachabilityFence(their_node_id);
368                 Reference.reachabilityFence(msg);
369                 if (ret >= 0 && ret <= 4096) { return null; }
370                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
371                 if (this != null) { this.ptrs_to.add(msg); };
372                 return ret_hu_conv;
373         }
374
375         /**
376          * Gets the node feature flags which this handler itself supports. All available handlers are
377          * queried similarly and their feature flags are OR'd together to form the [`NodeFeatures`]
378          * which are broadcasted in our [`NodeAnnouncement`] message.
379          */
380         public NodeFeatures provided_node_features() {
381                 long ret = bindings.RoutingMessageHandler_provided_node_features(this.ptr);
382                 Reference.reachabilityFence(this);
383                 if (ret >= 0 && ret <= 4096) { return null; }
384                 org.ldk.structs.NodeFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.NodeFeatures(null, ret); }
385                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
386                 return ret_hu_conv;
387         }
388
389         /**
390          * Gets the init feature flags which should be sent to the given peer. All available handlers
391          * are queried similarly and their feature flags are OR'd together to form the [`InitFeatures`]
392          * which are sent in our [`Init`] message.
393          * 
394          * Note that this method is called before [`Self::peer_connected`].
395          */
396         public InitFeatures provided_init_features(byte[] their_node_id) {
397                 long ret = bindings.RoutingMessageHandler_provided_init_features(this.ptr, InternalUtils.check_arr_len(their_node_id, 33));
398                 Reference.reachabilityFence(this);
399                 Reference.reachabilityFence(their_node_id);
400                 if (ret >= 0 && ret <= 4096) { return null; }
401                 org.ldk.structs.InitFeatures ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InitFeatures(null, ret); }
402                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
403                 return ret_hu_conv;
404         }
405
406 }