Merge pull request #46 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 javax.annotation.Nullable;
8
9 /**
10  * A trait to describe an object which can receive routing messages.
11  * 
12  * # Implementor DoS Warnings
13  * 
14  * For `gossip_queries` messages there are potential DoS vectors when handling
15  * inbound queries. Implementors using an on-disk network graph should be aware of
16  * repeated disk I/O for queries accessing different parts of the network graph.
17  */
18 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
19 public class RoutingMessageHandler extends CommonBase {
20         final bindings.LDKRoutingMessageHandler bindings_instance;
21         RoutingMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
22         private RoutingMessageHandler(bindings.LDKRoutingMessageHandler arg, bindings.LDKMessageSendEventsProvider MessageSendEventsProvider) {
23                 super(bindings.LDKRoutingMessageHandler_new(arg, MessageSendEventsProvider));
24                 this.ptrs_to.add(arg);
25                 this.ptrs_to.add(MessageSendEventsProvider);
26                 this.bindings_instance = arg;
27         }
28         @Override @SuppressWarnings("deprecation")
29         protected void finalize() throws Throwable {
30                 if (ptr != 0) { bindings.RoutingMessageHandler_free(ptr); } super.finalize();
31         }
32
33         public static interface RoutingMessageHandlerInterface {
34                 /**
35                  * Handle an incoming node_announcement message, returning true if it should be forwarded on,
36                  * false or returning an Err otherwise.
37                  */
38                 Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg);
39                 /**
40                  * Handle a channel_announcement message, returning true if it should be forwarded on, false
41                  * or returning an Err otherwise.
42                  */
43                 Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg);
44                 /**
45                  * Handle an incoming channel_update message, returning true if it should be forwarded on,
46                  * false or returning an Err otherwise.
47                  */
48                 Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg);
49                 /**
50                  * Gets a subset of the channel announcements and updates required to dump our routing table
51                  * to a remote node, starting at the short_channel_id indicated by starting_point and
52                  * including the batch_amount entries immediately higher in numerical value than starting_point.
53                  */
54                 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] get_next_channel_announcements(long starting_point, byte batch_amount);
55                 /**
56                  * Gets a subset of the node announcements required to dump our routing table to a remote node,
57                  * starting at the node *after* the provided publickey and including batch_amount entries
58                  * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
59                  * If None is provided for starting_point, we start at the first node.
60                  * 
61                  * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
62                  */
63                 NodeAnnouncement[] get_next_node_announcements(byte[] starting_point, byte batch_amount);
64                 /**
65                  * Called when a connection is established with a peer. This can be used to
66                  * perform routing table synchronization using a strategy defined by the
67                  * implementor.
68                  */
69                 void sync_routing_table(byte[] their_node_id, Init init);
70                 /**
71                  * Handles the reply of a query we initiated to learn about channels
72                  * for a given range of blocks. We can expect to receive one or more
73                  * replies to a single query.
74                  */
75                 Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg);
76                 /**
77                  * Handles the reply of a query we initiated asking for routing gossip
78                  * messages for a list of channels. We should receive this message when
79                  * a node has completed its best effort to send us the pertaining routing
80                  * gossip messages.
81                  */
82                 Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg);
83                 /**
84                  * Handles when a peer asks us to send a list of short_channel_ids
85                  * for the requested range of blocks.
86                  */
87                 Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg);
88                 /**
89                  * Handles when a peer asks us to send routing gossip messages for a
90                  * list of short_channel_ids.
91                  */
92                 Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg);
93         }
94         private static class LDKRoutingMessageHandlerHolder { RoutingMessageHandler held; }
95         public static RoutingMessageHandler new_impl(RoutingMessageHandlerInterface arg, MessageSendEventsProvider.MessageSendEventsProviderInterface MessageSendEventsProvider_impl) {
96                 final LDKRoutingMessageHandlerHolder impl_holder = new LDKRoutingMessageHandlerHolder();
97                 impl_holder.held = new RoutingMessageHandler(new bindings.LDKRoutingMessageHandler() {
98                         @Override public long handle_node_announcement(long msg) {
99                                 NodeAnnouncement msg_hu_conv = new NodeAnnouncement(null, msg);
100                                 Result_boolLightningErrorZ ret = arg.handle_node_announcement(msg_hu_conv);
101                                 long result = ret != null ? ret.ptr : 0;
102                                 return result;
103                         }
104                         @Override public long handle_channel_announcement(long msg) {
105                                 ChannelAnnouncement msg_hu_conv = new ChannelAnnouncement(null, msg);
106                                 Result_boolLightningErrorZ ret = arg.handle_channel_announcement(msg_hu_conv);
107                                 long result = ret != null ? ret.ptr : 0;
108                                 return result;
109                         }
110                         @Override public long handle_channel_update(long msg) {
111                                 ChannelUpdate msg_hu_conv = new ChannelUpdate(null, msg);
112                                 Result_boolLightningErrorZ ret = arg.handle_channel_update(msg_hu_conv);
113                                 long result = ret != null ? ret.ptr : 0;
114                                 return result;
115                         }
116                         @Override public long[] get_next_channel_announcements(long starting_point, byte batch_amount) {
117                                 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] ret = arg.get_next_channel_announcements(starting_point, batch_amount);
118                                 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_59 -> ret_conv_59 != null ? ret_conv_59.ptr : 0).toArray() : null;
119                                 return result;
120                         }
121                         @Override public long[] get_next_node_announcements(byte[] starting_point, byte batch_amount) {
122                                 NodeAnnouncement[] ret = arg.get_next_node_announcements(starting_point, batch_amount);
123                                 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_18 -> ret_conv_18 == null ? 0 : ret_conv_18.ptr & ~1).toArray() : null;
124                                 for (NodeAnnouncement ret_conv_18: ret) { impl_holder.held.ptrs_to.add(ret_conv_18); };
125                                 return result;
126                         }
127                         @Override public void sync_routing_table(byte[] their_node_id, long init) {
128                                 Init 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 = 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 ? ret.ptr : 0;
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 = 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 ? ret.ptr : 0;
143                                 return result;
144                         }
145                         @Override public long handle_query_channel_range(byte[] their_node_id, long msg) {
146                                 QueryChannelRange 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 ? ret.ptr : 0;
150                                 return result;
151                         }
152                         @Override public long handle_query_short_channel_ids(byte[] their_node_id, long msg) {
153                                 QueryShortChannelIds 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 ? ret.ptr : 0;
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                 if (ret < 1024) { return null; }
179                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
180                 this.ptrs_to.add(msg);
181                 return ret_hu_conv;
182         }
183
184         /**
185          * Handle a channel_announcement message, returning true if it should be forwarded on, false
186          * or returning an Err otherwise.
187          */
188         public Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg) {
189                 long ret = bindings.RoutingMessageHandler_handle_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
190                 if (ret < 1024) { return null; }
191                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
192                 this.ptrs_to.add(msg);
193                 return ret_hu_conv;
194         }
195
196         /**
197          * Handle an incoming channel_update message, returning true if it should be forwarded on,
198          * false or returning an Err otherwise.
199          */
200         public Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg) {
201                 long ret = bindings.RoutingMessageHandler_handle_channel_update(this.ptr, msg == null ? 0 : msg.ptr & ~1);
202                 if (ret < 1024) { return null; }
203                 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
204                 this.ptrs_to.add(msg);
205                 return ret_hu_conv;
206         }
207
208         /**
209          * Gets a subset of the channel announcements and updates required to dump our routing table
210          * to a remote node, starting at the short_channel_id indicated by starting_point and
211          * including the batch_amount entries immediately higher in numerical value than starting_point.
212          */
213         public ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] get_next_channel_announcements(long starting_point, byte batch_amount) {
214                 long[] ret = bindings.RoutingMessageHandler_get_next_channel_announcements(this.ptr, starting_point, batch_amount);
215                 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] ret_conv_59_arr = new ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[ret.length];
216                 for (int h = 0; h < ret.length; h++) {
217                         long ret_conv_59 = ret[h];
218                         ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ ret_conv_59_hu_conv = new ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ(null, ret_conv_59);
219                         ret_conv_59_hu_conv.ptrs_to.add(this);
220                         ret_conv_59_arr[h] = ret_conv_59_hu_conv;
221                 }
222                 return ret_conv_59_arr;
223         }
224
225         /**
226          * Gets a subset of the node announcements required to dump our routing table to a remote node,
227          * starting at the node *after* the provided publickey and including batch_amount entries
228          * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
229          * If None is provided for starting_point, we start at the first node.
230          * 
231          * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
232          */
233         public NodeAnnouncement[] get_next_node_announcements(@Nullable byte[] starting_point, byte batch_amount) {
234                 long[] ret = bindings.RoutingMessageHandler_get_next_node_announcements(this.ptr, starting_point, batch_amount);
235                 NodeAnnouncement[] ret_conv_18_arr = new NodeAnnouncement[ret.length];
236                 for (int s = 0; s < ret.length; s++) {
237                         long ret_conv_18 = ret[s];
238                         NodeAnnouncement ret_conv_18_hu_conv = new NodeAnnouncement(null, ret_conv_18);
239                         ret_conv_18_hu_conv.ptrs_to.add(this);
240                         ret_conv_18_arr[s] = ret_conv_18_hu_conv;
241                 }
242                 return ret_conv_18_arr;
243         }
244
245         /**
246          * Called when a connection is established with a peer. This can be used to
247          * perform routing table synchronization using a strategy defined by the
248          * implementor.
249          */
250         public void sync_routing_table(byte[] their_node_id, Init init) {
251                 bindings.RoutingMessageHandler_sync_routing_table(this.ptr, their_node_id, init == null ? 0 : init.ptr & ~1);
252                 this.ptrs_to.add(init);
253         }
254
255         /**
256          * Handles the reply of a query we initiated to learn about channels
257          * for a given range of blocks. We can expect to receive one or more
258          * replies to a single query.
259          */
260         public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg) {
261                 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
262                 if (ret < 1024) { return null; }
263                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
264                 this.ptrs_to.add(msg);
265                 return ret_hu_conv;
266         }
267
268         /**
269          * Handles the reply of a query we initiated asking for routing gossip
270          * messages for a list of channels. We should receive this message when
271          * a node has completed its best effort to send us the pertaining routing
272          * gossip messages.
273          */
274         public Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg) {
275                 long ret = bindings.RoutingMessageHandler_handle_reply_short_channel_ids_end(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
276                 if (ret < 1024) { return null; }
277                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
278                 this.ptrs_to.add(msg);
279                 return ret_hu_conv;
280         }
281
282         /**
283          * Handles when a peer asks us to send a list of short_channel_ids
284          * for the requested range of blocks.
285          */
286         public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg) {
287                 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
288                 if (ret < 1024) { return null; }
289                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
290                 this.ptrs_to.add(msg);
291                 return ret_hu_conv;
292         }
293
294         /**
295          * Handles when a peer asks us to send routing gossip messages for a
296          * list of short_channel_ids.
297          */
298         public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg) {
299                 long ret = bindings.RoutingMessageHandler_handle_query_short_channel_ids(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
300                 if (ret < 1024) { return null; }
301                 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
302                 this.ptrs_to.add(msg);
303                 return ret_hu_conv;
304         }
305
306 }