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 peer_connected(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 Reference.reachabilityFence(arg);
103 long result = ret == null ? 0 : ret.clone_ptr();
106 @Override public long handle_channel_announcement(long msg) {
107 ChannelAnnouncement msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ChannelAnnouncement(null, msg); }
108 Result_boolLightningErrorZ ret = arg.handle_channel_announcement(msg_hu_conv);
109 Reference.reachabilityFence(arg);
110 long result = ret == null ? 0 : ret.clone_ptr();
113 @Override public long handle_channel_update(long msg) {
114 ChannelUpdate msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ChannelUpdate(null, msg); }
115 Result_boolLightningErrorZ ret = arg.handle_channel_update(msg_hu_conv);
116 Reference.reachabilityFence(arg);
117 long result = ret == null ? 0 : ret.clone_ptr();
120 @Override public long[] get_next_channel_announcements(long starting_point, byte batch_amount) {
121 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] ret = arg.get_next_channel_announcements(starting_point, batch_amount);
122 Reference.reachabilityFence(arg);
123 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_59 -> ret_conv_59 == null ? 0 : ret_conv_59.clone_ptr()).toArray() : null;
126 @Override public long[] get_next_node_announcements(byte[] starting_point, byte batch_amount) {
127 NodeAnnouncement[] ret = arg.get_next_node_announcements(starting_point, batch_amount);
128 Reference.reachabilityFence(arg);
129 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_18 -> ret_conv_18 == null ? 0 : ret_conv_18.clone_ptr()).toArray() : null;
132 @Override public void peer_connected(byte[] their_node_id, long init) {
133 Init init_hu_conv = null; if (init < 0 || init > 4096) { init_hu_conv = new Init(null, init); }
134 arg.peer_connected(their_node_id, init_hu_conv);
135 Reference.reachabilityFence(arg);
137 @Override public long handle_reply_channel_range(byte[] their_node_id, long msg) {
138 ReplyChannelRange msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ReplyChannelRange(null, msg); }
139 msg_hu_conv.ptrs_to.add(this);
140 Result_NoneLightningErrorZ ret = arg.handle_reply_channel_range(their_node_id, msg_hu_conv);
141 Reference.reachabilityFence(arg);
142 long result = ret == null ? 0 : ret.clone_ptr();
145 @Override public long handle_reply_short_channel_ids_end(byte[] their_node_id, long msg) {
146 ReplyShortChannelIdsEnd msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ReplyShortChannelIdsEnd(null, msg); }
147 msg_hu_conv.ptrs_to.add(this);
148 Result_NoneLightningErrorZ ret = arg.handle_reply_short_channel_ids_end(their_node_id, msg_hu_conv);
149 Reference.reachabilityFence(arg);
150 long result = ret == null ? 0 : ret.clone_ptr();
153 @Override public long handle_query_channel_range(byte[] their_node_id, long msg) {
154 QueryChannelRange msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new QueryChannelRange(null, msg); }
155 msg_hu_conv.ptrs_to.add(this);
156 Result_NoneLightningErrorZ ret = arg.handle_query_channel_range(their_node_id, msg_hu_conv);
157 Reference.reachabilityFence(arg);
158 long result = ret == null ? 0 : ret.clone_ptr();
161 @Override public long handle_query_short_channel_ids(byte[] their_node_id, long msg) {
162 QueryShortChannelIds msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new QueryShortChannelIds(null, msg); }
163 msg_hu_conv.ptrs_to.add(this);
164 Result_NoneLightningErrorZ ret = arg.handle_query_short_channel_ids(their_node_id, msg_hu_conv);
165 Reference.reachabilityFence(arg);
166 long result = ret == null ? 0 : ret.clone_ptr();
169 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
170 return impl_holder.held;
174 * Gets the underlying MessageSendEventsProvider.
176 public MessageSendEventsProvider get_message_send_events_provider() {
177 MessageSendEventsProvider res = new MessageSendEventsProvider(null, bindings.LDKRoutingMessageHandler_get_MessageSendEventsProvider(this.ptr));
178 this.ptrs_to.add(res);
183 * Handle an incoming node_announcement message, returning true if it should be forwarded on,
184 * false or returning an Err otherwise.
186 public Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg) {
187 long ret = bindings.RoutingMessageHandler_handle_node_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
188 Reference.reachabilityFence(this);
189 Reference.reachabilityFence(msg);
190 if (ret >= 0 && ret <= 4096) { return null; }
191 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
192 this.ptrs_to.add(msg);
197 * Handle a channel_announcement message, returning true if it should be forwarded on, false
198 * or returning an Err otherwise.
200 public Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg) {
201 long ret = bindings.RoutingMessageHandler_handle_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
202 Reference.reachabilityFence(this);
203 Reference.reachabilityFence(msg);
204 if (ret >= 0 && ret <= 4096) { return null; }
205 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
206 this.ptrs_to.add(msg);
211 * Handle an incoming channel_update message, returning true if it should be forwarded on,
212 * false or returning an Err otherwise.
214 public Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg) {
215 long ret = bindings.RoutingMessageHandler_handle_channel_update(this.ptr, msg == null ? 0 : msg.ptr & ~1);
216 Reference.reachabilityFence(this);
217 Reference.reachabilityFence(msg);
218 if (ret >= 0 && ret <= 4096) { return null; }
219 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
220 this.ptrs_to.add(msg);
225 * Gets a subset of the channel announcements and updates required to dump our routing table
226 * to a remote node, starting at the short_channel_id indicated by starting_point and
227 * including the batch_amount entries immediately higher in numerical value than starting_point.
229 public ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] get_next_channel_announcements(long starting_point, byte batch_amount) {
230 long[] ret = bindings.RoutingMessageHandler_get_next_channel_announcements(this.ptr, starting_point, batch_amount);
231 Reference.reachabilityFence(this);
232 Reference.reachabilityFence(starting_point);
233 Reference.reachabilityFence(batch_amount);
234 int ret_conv_59_len = ret.length;
235 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] ret_conv_59_arr = new ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[ret_conv_59_len];
236 for (int h = 0; h < ret_conv_59_len; h++) {
237 long ret_conv_59 = ret[h];
238 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ ret_conv_59_hu_conv = new ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ(null, ret_conv_59);
239 ret_conv_59_hu_conv.ptrs_to.add(this);
240 ret_conv_59_arr[h] = ret_conv_59_hu_conv;
242 return ret_conv_59_arr;
246 * Gets a subset of the node announcements required to dump our routing table to a remote node,
247 * starting at the node *after* the provided publickey and including batch_amount entries
248 * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
249 * If None is provided for starting_point, we start at the first node.
251 * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
253 public NodeAnnouncement[] get_next_node_announcements(@Nullable byte[] starting_point, byte batch_amount) {
254 long[] ret = bindings.RoutingMessageHandler_get_next_node_announcements(this.ptr, InternalUtils.check_arr_len(starting_point, 33), batch_amount);
255 Reference.reachabilityFence(this);
256 Reference.reachabilityFence(starting_point);
257 Reference.reachabilityFence(batch_amount);
258 int ret_conv_18_len = ret.length;
259 NodeAnnouncement[] ret_conv_18_arr = new NodeAnnouncement[ret_conv_18_len];
260 for (int s = 0; s < ret_conv_18_len; s++) {
261 long ret_conv_18 = ret[s];
262 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); }
263 ret_conv_18_hu_conv.ptrs_to.add(this);
264 ret_conv_18_arr[s] = ret_conv_18_hu_conv;
266 return ret_conv_18_arr;
270 * Called when a connection is established with a peer. This can be used to
271 * perform routing table synchronization using a strategy defined by the
274 public void peer_connected(byte[] their_node_id, Init init) {
275 bindings.RoutingMessageHandler_peer_connected(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), init == null ? 0 : init.ptr & ~1);
276 Reference.reachabilityFence(this);
277 Reference.reachabilityFence(their_node_id);
278 Reference.reachabilityFence(init);
279 this.ptrs_to.add(init);
283 * Handles the reply of a query we initiated to learn about channels
284 * for a given range of blocks. We can expect to receive one or more
285 * replies to a single query.
287 public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg) {
288 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr & ~1);
289 Reference.reachabilityFence(this);
290 Reference.reachabilityFence(their_node_id);
291 Reference.reachabilityFence(msg);
292 if (ret >= 0 && ret <= 4096) { return null; }
293 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
298 * Handles the reply of a query we initiated asking for routing gossip
299 * messages for a list of channels. We should receive this message when
300 * a node has completed its best effort to send us the pertaining routing
303 public Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg) {
304 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);
305 Reference.reachabilityFence(this);
306 Reference.reachabilityFence(their_node_id);
307 Reference.reachabilityFence(msg);
308 if (ret >= 0 && ret <= 4096) { return null; }
309 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
314 * Handles when a peer asks us to send a list of short_channel_ids
315 * for the requested range of blocks.
317 public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg) {
318 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, InternalUtils.check_arr_len(their_node_id, 33), msg == null ? 0 : msg.ptr & ~1);
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);
328 * Handles when a peer asks us to send routing gossip messages for a
329 * list of short_channel_ids.
331 public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg) {
332 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);
333 Reference.reachabilityFence(this);
334 Reference.reachabilityFence(their_node_id);
335 Reference.reachabilityFence(msg);
336 if (ret >= 0 && ret <= 4096) { return null; }
337 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);