1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
10 * A trait to describe an object which can receive routing messages.
12 * # Implementor DoS Warnings
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.
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;
28 @Override @SuppressWarnings("deprecation")
29 protected void finalize() throws Throwable {
30 if (ptr != 0) { bindings.RoutingMessageHandler_free(ptr); } super.finalize();
33 public static interface RoutingMessageHandlerInterface {
35 * Handle an incoming node_announcement message, returning true if it should be forwarded on,
36 * false or returning an Err otherwise.
38 Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg);
40 * Handle a channel_announcement message, returning true if it should be forwarded on, false
41 * or returning an Err otherwise.
43 Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg);
45 * Handle an incoming channel_update message, returning true if it should be forwarded on,
46 * false or returning an Err otherwise.
48 Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg);
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.
54 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] get_next_channel_announcements(long starting_point, byte batch_amount);
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.
61 * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
63 NodeAnnouncement[] get_next_node_announcements(byte[] starting_point, byte batch_amount);
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
69 void sync_routing_table(byte[] their_node_id, Init init);
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.
75 Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg);
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
82 Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg);
84 * Handles when a peer asks us to send a list of short_channel_ids
85 * for the requested range of blocks.
87 Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg);
89 * Handles when a peer asks us to send routing gossip messages for a
90 * list of short_channel_ids.
92 Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg);
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 = null; if (msg < 0 || msg > 4096) { 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;
104 @Override public long handle_channel_announcement(long msg) {
105 ChannelAnnouncement msg_hu_conv = null; if (msg < 0 || msg > 4096) { 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;
110 @Override public long handle_channel_update(long msg) {
111 ChannelUpdate msg_hu_conv = null; if (msg < 0 || msg > 4096) { 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;
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;
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;
126 @Override public void sync_routing_table(byte[] their_node_id, long init) {
127 Init init_hu_conv = null; if (init < 0 || init > 4096) { init_hu_conv = new Init(null, init); }
128 arg.sync_routing_table(their_node_id, init_hu_conv);
130 @Override public long handle_reply_channel_range(byte[] their_node_id, long msg) {
131 ReplyChannelRange msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ReplyChannelRange(null, msg); }
132 msg_hu_conv.ptrs_to.add(this);
133 Result_NoneLightningErrorZ ret = arg.handle_reply_channel_range(their_node_id, msg_hu_conv);
134 long result = ret != null ? ret.ptr : 0;
137 @Override public long handle_reply_short_channel_ids_end(byte[] their_node_id, long msg) {
138 ReplyShortChannelIdsEnd msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new ReplyShortChannelIdsEnd(null, msg); }
139 msg_hu_conv.ptrs_to.add(this);
140 Result_NoneLightningErrorZ ret = arg.handle_reply_short_channel_ids_end(their_node_id, msg_hu_conv);
141 long result = ret != null ? ret.ptr : 0;
144 @Override public long handle_query_channel_range(byte[] their_node_id, long msg) {
145 QueryChannelRange msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new QueryChannelRange(null, msg); }
146 msg_hu_conv.ptrs_to.add(this);
147 Result_NoneLightningErrorZ ret = arg.handle_query_channel_range(their_node_id, msg_hu_conv);
148 long result = ret != null ? ret.ptr : 0;
151 @Override public long handle_query_short_channel_ids(byte[] their_node_id, long msg) {
152 QueryShortChannelIds msg_hu_conv = null; if (msg < 0 || msg > 4096) { msg_hu_conv = new QueryShortChannelIds(null, msg); }
153 msg_hu_conv.ptrs_to.add(this);
154 Result_NoneLightningErrorZ ret = arg.handle_query_short_channel_ids(their_node_id, msg_hu_conv);
155 long result = ret != null ? ret.ptr : 0;
158 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
159 return impl_holder.held;
163 * Gets the underlying MessageSendEventsProvider.
165 public MessageSendEventsProvider get_message_send_events_provider() {
166 MessageSendEventsProvider res = new MessageSendEventsProvider(null, bindings.LDKRoutingMessageHandler_get_MessageSendEventsProvider(this.ptr));
167 this.ptrs_to.add(res);
172 * Handle an incoming node_announcement message, returning true if it should be forwarded on,
173 * false or returning an Err otherwise.
175 public Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg) {
176 long ret = bindings.RoutingMessageHandler_handle_node_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
177 if (ret >= 0 && ret <= 4096) { return null; }
178 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
179 this.ptrs_to.add(msg);
184 * Handle a channel_announcement message, returning true if it should be forwarded on, false
185 * or returning an Err otherwise.
187 public Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg) {
188 long ret = bindings.RoutingMessageHandler_handle_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
189 if (ret >= 0 && ret <= 4096) { return null; }
190 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
191 this.ptrs_to.add(msg);
196 * Handle an incoming channel_update message, returning true if it should be forwarded on,
197 * false or returning an Err otherwise.
199 public Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg) {
200 long ret = bindings.RoutingMessageHandler_handle_channel_update(this.ptr, msg == null ? 0 : msg.ptr & ~1);
201 if (ret >= 0 && ret <= 4096) { return null; }
202 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
203 this.ptrs_to.add(msg);
208 * Gets a subset of the channel announcements and updates required to dump our routing table
209 * to a remote node, starting at the short_channel_id indicated by starting_point and
210 * including the batch_amount entries immediately higher in numerical value than starting_point.
212 public ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] get_next_channel_announcements(long starting_point, byte batch_amount) {
213 long[] ret = bindings.RoutingMessageHandler_get_next_channel_announcements(this.ptr, starting_point, batch_amount);
214 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[] ret_conv_59_arr = new ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ[ret.length];
215 for (int h = 0; h < ret.length; h++) {
216 long ret_conv_59 = ret[h];
217 ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ ret_conv_59_hu_conv = new ThreeTuple_ChannelAnnouncementChannelUpdateChannelUpdateZ(null, ret_conv_59);
218 ret_conv_59_hu_conv.ptrs_to.add(this);
219 ret_conv_59_arr[h] = ret_conv_59_hu_conv;
221 return ret_conv_59_arr;
225 * Gets a subset of the node announcements required to dump our routing table to a remote node,
226 * starting at the node *after* the provided publickey and including batch_amount entries
227 * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
228 * If None is provided for starting_point, we start at the first node.
230 * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
232 public NodeAnnouncement[] get_next_node_announcements(@Nullable byte[] starting_point, byte batch_amount) {
233 long[] ret = bindings.RoutingMessageHandler_get_next_node_announcements(this.ptr, starting_point, batch_amount);
234 NodeAnnouncement[] ret_conv_18_arr = new NodeAnnouncement[ret.length];
235 for (int s = 0; s < ret.length; s++) {
236 long ret_conv_18 = ret[s];
237 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); }
238 ret_conv_18_hu_conv.ptrs_to.add(this);
239 ret_conv_18_arr[s] = ret_conv_18_hu_conv;
241 return ret_conv_18_arr;
245 * Called when a connection is established with a peer. This can be used to
246 * perform routing table synchronization using a strategy defined by the
249 public void sync_routing_table(byte[] their_node_id, Init init) {
250 bindings.RoutingMessageHandler_sync_routing_table(this.ptr, their_node_id, init == null ? 0 : init.ptr & ~1);
251 this.ptrs_to.add(init);
255 * Handles the reply of a query we initiated to learn about channels
256 * for a given range of blocks. We can expect to receive one or more
257 * replies to a single query.
259 public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg) {
260 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
261 if (ret >= 0 && ret <= 4096) { return null; }
262 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
267 * Handles the reply of a query we initiated asking for routing gossip
268 * messages for a list of channels. We should receive this message when
269 * a node has completed its best effort to send us the pertaining routing
272 public Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg) {
273 long ret = bindings.RoutingMessageHandler_handle_reply_short_channel_ids_end(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
274 if (ret >= 0 && ret <= 4096) { return null; }
275 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
280 * Handles when a peer asks us to send a list of short_channel_ids
281 * for the requested range of blocks.
283 public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg) {
284 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
285 if (ret >= 0 && ret <= 4096) { return null; }
286 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
291 * Handles when a peer asks us to send routing gossip messages for a
292 * list of short_channel_ids.
294 public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg) {
295 long ret = bindings.RoutingMessageHandler_handle_query_short_channel_ids(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
296 if (ret >= 0 && ret <= 4096) { return null; }
297 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);