1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
11 * A trait to describe an object which can receive routing messages.
13 * # Implementor DoS Warnings
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.
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;
29 @Override @SuppressWarnings("deprecation")
30 protected void finalize() throws Throwable {
31 if (ptr != 0) { bindings.RoutingMessageHandler_free(ptr); } super.finalize();
34 public static interface RoutingMessageHandlerInterface {
36 * Handle an incoming node_announcement message, returning true if it should be forwarded on,
37 * false or returning an Err otherwise.
39 Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg);
41 * Handle a channel_announcement message, returning true if it should be forwarded on, false
42 * or returning an Err otherwise.
44 Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg);
46 * Handle an incoming channel_update message, returning true if it should be forwarded on,
47 * false or returning an Err otherwise.
49 Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg);
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.
55 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] get_next_channel_announcements(long starting_point, byte batch_amount);
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.
62 * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
64 NodeAnnouncement[] get_next_node_announcements(byte[] starting_point, byte batch_amount);
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
70 void sync_routing_table(byte[] their_node_id, Init init);
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.
76 Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg);
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
83 Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg);
85 * Handles when a peer asks us to send a list of short_channel_ids
86 * for the requested range of blocks.
88 Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg);
90 * Handles when a peer asks us to send routing gossip messages for a
91 * list of short_channel_ids.
93 Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg);
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();
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();
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();
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;
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;
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);
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();
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();
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();
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();
159 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
160 return impl_holder.held;
164 * Gets the underlying MessageSendEventsProvider.
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);
173 * Handle an incoming node_announcement message, returning true if it should be forwarded on,
174 * false or returning an Err otherwise.
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);
187 * Handle a channel_announcement message, returning true if it should be forwarded on, false
188 * or returning an Err otherwise.
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);
201 * Handle an incoming channel_update message, returning true if it should be forwarded on,
202 * false or returning an Err otherwise.
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);
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.
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 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] ret_conv_59_arr = new ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[ret.length];
225 for (int h = 0; h < ret.length; h++) {
226 long ret_conv_59 = ret[h];
227 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ ret_conv_59_hu_conv = new ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ(null, ret_conv_59);
228 ret_conv_59_hu_conv.ptrs_to.add(this);
229 ret_conv_59_arr[h] = ret_conv_59_hu_conv;
231 return ret_conv_59_arr;
235 * Gets a subset of the node announcements required to dump our routing table to a remote node,
236 * starting at the node *after* the provided publickey and including batch_amount entries
237 * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
238 * If None is provided for starting_point, we start at the first node.
240 * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
242 public NodeAnnouncement[] get_next_node_announcements(@Nullable byte[] starting_point, byte batch_amount) {
243 long[] ret = bindings.RoutingMessageHandler_get_next_node_announcements(this.ptr, InternalUtils.check_arr_len(starting_point, 33), batch_amount);
244 Reference.reachabilityFence(this);
245 Reference.reachabilityFence(starting_point);
246 Reference.reachabilityFence(batch_amount);
247 NodeAnnouncement[] ret_conv_18_arr = new NodeAnnouncement[ret.length];
248 for (int s = 0; s < ret.length; s++) {
249 long ret_conv_18 = ret[s];
250 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); }
251 ret_conv_18_hu_conv.ptrs_to.add(this);
252 ret_conv_18_arr[s] = ret_conv_18_hu_conv;
254 return ret_conv_18_arr;
258 * Called when a connection is established with a peer. This can be used to
259 * perform routing table synchronization using a strategy defined by the
262 public void sync_routing_table(byte[] their_node_id, Init init) {
263 bindings.RoutingMessageHandler_sync_routing_table(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr & ~1);
264 Reference.reachabilityFence(this);
265 Reference.reachabilityFence(their_node_id);
266 Reference.reachabilityFence(init);
267 this.ptrs_to.add(init);
271 * Handles the reply of a query we initiated to learn about channels
272 * for a given range of blocks. We can expect to receive one or more
273 * replies to a single query.
275 public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg) {
276 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr & ~1);
277 Reference.reachabilityFence(this);
278 Reference.reachabilityFence(their_node_id);
279 Reference.reachabilityFence(msg);
280 if (ret >= 0 && ret <= 4096) { return null; }
281 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
286 * Handles the reply of a query we initiated asking for routing gossip
287 * messages for a list of channels. We should receive this message when
288 * a node has completed its best effort to send us the pertaining routing
291 public Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg) {
292 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);
293 Reference.reachabilityFence(this);
294 Reference.reachabilityFence(their_node_id);
295 Reference.reachabilityFence(msg);
296 if (ret >= 0 && ret <= 4096) { return null; }
297 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
302 * Handles when a peer asks us to send a list of short_channel_ids
303 * for the requested range of blocks.
305 public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg) {
306 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr & ~1);
307 Reference.reachabilityFence(this);
308 Reference.reachabilityFence(their_node_id);
309 Reference.reachabilityFence(msg);
310 if (ret >= 0 && ret <= 4096) { return null; }
311 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
316 * Handles when a peer asks us to send routing gossip messages for a
317 * list of short_channel_ids.
319 public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg) {
320 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);
321 Reference.reachabilityFence(this);
322 Reference.reachabilityFence(their_node_id);
323 Reference.reachabilityFence(msg);
324 if (ret >= 0 && ret <= 4096) { return null; }
325 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);