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 * Handle some updates to the route graph that we learned due to an outbound failed payment.
52 void handle_htlc_fail_channel_update(HTLCFailChannelUpdate update);
54 * Gets a subset of the channel announcements and updates required to dump our routing table
55 * to a remote node, starting at the short_channel_id indicated by starting_point and
56 * including the batch_amount entries immediately higher in numerical value than starting_point.
58 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] get_next_channel_announcements(long starting_point, byte batch_amount);
60 * Gets a subset of the node announcements required to dump our routing table to a remote node,
61 * starting at the node *after* the provided publickey and including batch_amount entries
62 * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
63 * If None is provided for starting_point, we start at the first node.
65 * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
67 NodeAnnouncement[] get_next_node_announcements(byte[] starting_point, byte batch_amount);
69 * Called when a connection is established with a peer. This can be used to
70 * perform routing table synchronization using a strategy defined by the
73 void sync_routing_table(byte[] their_node_id, Init init);
75 * Handles the reply of a query we initiated to learn about channels
76 * for a given range of blocks. We can expect to receive one or more
77 * replies to a single query.
79 Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg);
81 * Handles the reply of a query we initiated asking for routing gossip
82 * messages for a list of channels. We should receive this message when
83 * a node has completed its best effort to send us the pertaining routing
86 Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg);
88 * Handles when a peer asks us to send a list of short_channel_ids
89 * for the requested range of blocks.
91 Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg);
93 * Handles when a peer asks us to send routing gossip messages for a
94 * list of short_channel_ids.
96 Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg);
98 private static class LDKRoutingMessageHandlerHolder { RoutingMessageHandler held; }
99 public static RoutingMessageHandler new_impl(RoutingMessageHandlerInterface arg, MessageSendEventsProvider.MessageSendEventsProviderInterface MessageSendEventsProvider_impl) {
100 final LDKRoutingMessageHandlerHolder impl_holder = new LDKRoutingMessageHandlerHolder();
101 impl_holder.held = new RoutingMessageHandler(new bindings.LDKRoutingMessageHandler() {
102 @Override public long handle_node_announcement(long msg) {
103 NodeAnnouncement msg_hu_conv = new NodeAnnouncement(null, msg);
104 Result_boolLightningErrorZ ret = arg.handle_node_announcement(msg_hu_conv);
105 long result = ret != null ? ret.ptr : 0;
108 @Override public long handle_channel_announcement(long msg) {
109 ChannelAnnouncement msg_hu_conv = new ChannelAnnouncement(null, msg);
110 Result_boolLightningErrorZ ret = arg.handle_channel_announcement(msg_hu_conv);
111 long result = ret != null ? ret.ptr : 0;
114 @Override public long handle_channel_update(long msg) {
115 ChannelUpdate msg_hu_conv = new ChannelUpdate(null, msg);
116 Result_boolLightningErrorZ ret = arg.handle_channel_update(msg_hu_conv);
117 long result = ret != null ? ret.ptr : 0;
120 @Override public void handle_htlc_fail_channel_update(long update) {
121 HTLCFailChannelUpdate update_hu_conv = HTLCFailChannelUpdate.constr_from_ptr(update);
122 arg.handle_htlc_fail_channel_update(update_hu_conv);
124 @Override public long[] get_next_channel_announcements(long starting_point, byte batch_amount) {
125 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] ret = arg.get_next_channel_announcements(starting_point, batch_amount);
126 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_63 -> bindings.C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_new(ret_conv_63.a == null ? 0 : ret_conv_63.a.ptr & ~1, ret_conv_63.b == null ? 0 : ret_conv_63.b.ptr & ~1, ret_conv_63.c == null ? 0 : ret_conv_63.c.ptr & ~1)).toArray() : null;
127 /* TODO 2 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate> */;
130 @Override public long[] get_next_node_announcements(byte[] starting_point, byte batch_amount) {
131 NodeAnnouncement[] ret = arg.get_next_node_announcements(starting_point, batch_amount);
132 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_18 -> ret_conv_18 == null ? 0 : ret_conv_18.ptr & ~1).toArray() : null;
133 /* TODO 2 NodeAnnouncement */;
136 @Override public void sync_routing_table(byte[] their_node_id, long init) {
137 Init init_hu_conv = new Init(null, init);
138 arg.sync_routing_table(their_node_id, init_hu_conv);
140 @Override public long handle_reply_channel_range(byte[] their_node_id, long msg) {
141 ReplyChannelRange msg_hu_conv = new ReplyChannelRange(null, msg);
142 msg_hu_conv.ptrs_to.add(this);
143 Result_NoneLightningErrorZ ret = arg.handle_reply_channel_range(their_node_id, msg_hu_conv);
144 long result = ret != null ? ret.ptr : 0;
147 @Override public long handle_reply_short_channel_ids_end(byte[] their_node_id, long msg) {
148 ReplyShortChannelIdsEnd msg_hu_conv = new ReplyShortChannelIdsEnd(null, msg);
149 msg_hu_conv.ptrs_to.add(this);
150 Result_NoneLightningErrorZ ret = arg.handle_reply_short_channel_ids_end(their_node_id, msg_hu_conv);
151 long result = ret != null ? ret.ptr : 0;
154 @Override public long handle_query_channel_range(byte[] their_node_id, long msg) {
155 QueryChannelRange msg_hu_conv = new QueryChannelRange(null, msg);
156 msg_hu_conv.ptrs_to.add(this);
157 Result_NoneLightningErrorZ ret = arg.handle_query_channel_range(their_node_id, msg_hu_conv);
158 long result = ret != null ? ret.ptr : 0;
161 @Override public long handle_query_short_channel_ids(byte[] their_node_id, long msg) {
162 QueryShortChannelIds 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 long result = ret != null ? ret.ptr : 0;
168 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
169 return impl_holder.held;
173 * Gets the underlying MessageSendEventsProvider.
175 public MessageSendEventsProvider get_message_send_events_provider() {
176 MessageSendEventsProvider res = new MessageSendEventsProvider(null, bindings.LDKRoutingMessageHandler_get_MessageSendEventsProvider(this.ptr));
177 this.ptrs_to.add(res);
182 * Handle an incoming node_announcement message, returning true if it should be forwarded on,
183 * false or returning an Err otherwise.
185 public Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg) {
186 long ret = bindings.RoutingMessageHandler_handle_node_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
187 if (ret < 1024) { return null; }
188 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
189 this.ptrs_to.add(msg);
194 * Handle a channel_announcement message, returning true if it should be forwarded on, false
195 * or returning an Err otherwise.
197 public Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg) {
198 long ret = bindings.RoutingMessageHandler_handle_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
199 if (ret < 1024) { return null; }
200 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
201 this.ptrs_to.add(msg);
206 * Handle an incoming channel_update message, returning true if it should be forwarded on,
207 * false or returning an Err otherwise.
209 public Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg) {
210 long ret = bindings.RoutingMessageHandler_handle_channel_update(this.ptr, msg == null ? 0 : msg.ptr & ~1);
211 if (ret < 1024) { return null; }
212 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
213 this.ptrs_to.add(msg);
218 * Handle some updates to the route graph that we learned due to an outbound failed payment.
220 public void handle_htlc_fail_channel_update(HTLCFailChannelUpdate update) {
221 bindings.RoutingMessageHandler_handle_htlc_fail_channel_update(this.ptr, update == null ? 0 : update.ptr & ~1);
222 this.ptrs_to.add(update);
226 * Gets a subset of the channel announcements and updates required to dump our routing table
227 * to a remote node, starting at the short_channel_id indicated by starting_point and
228 * including the batch_amount entries immediately higher in numerical value than starting_point.
230 public ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] get_next_channel_announcements(long starting_point, byte batch_amount) {
231 long[] ret = bindings.RoutingMessageHandler_get_next_channel_announcements(this.ptr, starting_point, batch_amount);
232 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] ret_conv_63_arr = new ThreeTuple[ret.length];
233 for (int l = 0; l < ret.length; l++) {
234 long ret_conv_63 = ret[l];
235 long ret_conv_63_a = bindings.LDKC3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_get_a(ret_conv_63);
236 ChannelAnnouncement ret_conv_63_a_hu_conv = new ChannelAnnouncement(null, ret_conv_63_a);
237 ret_conv_63_a_hu_conv.ptrs_to.add(this);;
238 long ret_conv_63_b = bindings.LDKC3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_get_b(ret_conv_63);
239 ChannelUpdate ret_conv_63_b_hu_conv = new ChannelUpdate(null, ret_conv_63_b);
240 ret_conv_63_b_hu_conv.ptrs_to.add(this);;
241 long ret_conv_63_c = bindings.LDKC3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_get_c(ret_conv_63);
242 ChannelUpdate ret_conv_63_c_hu_conv = new ChannelUpdate(null, ret_conv_63_c);
243 ret_conv_63_c_hu_conv.ptrs_to.add(this);;
244 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate> ret_conv_63_conv = new ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>(ret_conv_63_a_hu_conv, ret_conv_63_b_hu_conv, ret_conv_63_c_hu_conv, () -> {
245 bindings.C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_free(ret_conv_63);
247 ret_conv_63_a_hu_conv.ptrs_to.add(ret_conv_63_conv);
248 ret_conv_63_b_hu_conv.ptrs_to.add(ret_conv_63_conv);
249 ret_conv_63_c_hu_conv.ptrs_to.add(ret_conv_63_conv);
250 ret_conv_63_arr[l] = ret_conv_63_conv;
252 return ret_conv_63_arr;
256 * Gets a subset of the node announcements required to dump our routing table to a remote node,
257 * starting at the node *after* the provided publickey and including batch_amount entries
258 * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
259 * If None is provided for starting_point, we start at the first node.
261 * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
263 public NodeAnnouncement[] get_next_node_announcements(@Nullable byte[] starting_point, byte batch_amount) {
264 long[] ret = bindings.RoutingMessageHandler_get_next_node_announcements(this.ptr, starting_point, batch_amount);
265 NodeAnnouncement[] ret_conv_18_arr = new NodeAnnouncement[ret.length];
266 for (int s = 0; s < ret.length; s++) {
267 long ret_conv_18 = ret[s];
268 NodeAnnouncement ret_conv_18_hu_conv = new NodeAnnouncement(null, ret_conv_18);
269 ret_conv_18_hu_conv.ptrs_to.add(this);
270 ret_conv_18_arr[s] = ret_conv_18_hu_conv;
272 return ret_conv_18_arr;
276 * Called when a connection is established with a peer. This can be used to
277 * perform routing table synchronization using a strategy defined by the
280 public void sync_routing_table(byte[] their_node_id, Init init) {
281 bindings.RoutingMessageHandler_sync_routing_table(this.ptr, their_node_id, init == null ? 0 : init.ptr & ~1);
282 this.ptrs_to.add(init);
286 * Handles the reply of a query we initiated to learn about channels
287 * for a given range of blocks. We can expect to receive one or more
288 * replies to a single query.
290 public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg) {
291 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
292 if (ret < 1024) { return null; }
293 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
294 this.ptrs_to.add(msg);
299 * Handles the reply of a query we initiated asking for routing gossip
300 * messages for a list of channels. We should receive this message when
301 * a node has completed its best effort to send us the pertaining routing
304 public Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg) {
305 long ret = bindings.RoutingMessageHandler_handle_reply_short_channel_ids_end(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
306 if (ret < 1024) { return null; }
307 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
308 this.ptrs_to.add(msg);
313 * Handles when a peer asks us to send a list of short_channel_ids
314 * for the requested range of blocks.
316 public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg) {
317 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
318 if (ret < 1024) { return null; }
319 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
320 this.ptrs_to.add(msg);
325 * Handles when a peer asks us to send routing gossip messages for a
326 * list of short_channel_ids.
328 public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg) {
329 long ret = bindings.RoutingMessageHandler_handle_query_short_channel_ids(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
330 if (ret < 1024) { return null; }
331 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
332 this.ptrs_to.add(msg);