1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
9 * A trait to describe an object which can receive routing messages.
11 * # Implementor DoS Warnings
13 * For `gossip_queries` messages there are potential DoS vectors when handling
14 * inbound queries. Implementors using an on-disk network graph should be aware of
15 * repeated disk I/O for queries accessing different parts of the network graph.
17 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
18 public class RoutingMessageHandler extends CommonBase {
19 final bindings.LDKRoutingMessageHandler bindings_instance;
20 RoutingMessageHandler(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
21 private RoutingMessageHandler(bindings.LDKRoutingMessageHandler arg, bindings.LDKMessageSendEventsProvider MessageSendEventsProvider) {
22 super(bindings.LDKRoutingMessageHandler_new(arg, MessageSendEventsProvider));
23 this.ptrs_to.add(arg);
24 this.ptrs_to.add(MessageSendEventsProvider);
25 this.bindings_instance = arg;
27 @Override @SuppressWarnings("deprecation")
28 protected void finalize() throws Throwable {
29 if (ptr != 0) { bindings.RoutingMessageHandler_free(ptr); } super.finalize();
32 public static interface RoutingMessageHandlerInterface {
34 * Handle an incoming node_announcement message, returning true if it should be forwarded on,
35 * false or returning an Err otherwise.
37 Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg);
39 * Handle a channel_announcement message, returning true if it should be forwarded on, false
40 * or returning an Err otherwise.
42 Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg);
44 * Handle an incoming channel_update message, returning true if it should be forwarded on,
45 * false or returning an Err otherwise.
47 Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg);
49 * Handle some updates to the route graph that we learned due to an outbound failed payment.
51 void handle_htlc_fail_channel_update(HTLCFailChannelUpdate update);
53 * Gets a subset of the channel announcements and updates required to dump our routing table
54 * to a remote node, starting at the short_channel_id indicated by starting_point and
55 * including the batch_amount entries immediately higher in numerical value than starting_point.
57 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] get_next_channel_announcements(long starting_point, byte batch_amount);
59 * Gets a subset of the node announcements required to dump our routing table to a remote node,
60 * starting at the node *after* the provided publickey and including batch_amount entries
61 * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
62 * If None is provided for starting_point, we start at the first node.
64 * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
66 NodeAnnouncement[] get_next_node_announcements(byte[] starting_point, byte batch_amount);
68 * Called when a connection is established with a peer. This can be used to
69 * perform routing table synchronization using a strategy defined by the
72 void sync_routing_table(byte[] their_node_id, Init init);
74 * Handles the reply of a query we initiated to learn about channels
75 * for a given range of blocks. We can expect to receive one or more
76 * replies to a single query.
78 Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg);
80 * Handles the reply of a query we initiated asking for routing gossip
81 * messages for a list of channels. We should receive this message when
82 * a node has completed its best effort to send us the pertaining routing
85 Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg);
87 * Handles when a peer asks us to send a list of short_channel_ids
88 * for the requested range of blocks.
90 Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg);
92 * Handles when a peer asks us to send routing gossip messages for a
93 * list of short_channel_ids.
95 Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg);
97 private static class LDKRoutingMessageHandlerHolder { RoutingMessageHandler held; }
98 public static RoutingMessageHandler new_impl(RoutingMessageHandlerInterface arg, MessageSendEventsProvider.MessageSendEventsProviderInterface MessageSendEventsProvider_impl) {
99 final LDKRoutingMessageHandlerHolder impl_holder = new LDKRoutingMessageHandlerHolder();
100 impl_holder.held = new RoutingMessageHandler(new bindings.LDKRoutingMessageHandler() {
101 @Override public long handle_node_announcement(long msg) {
102 NodeAnnouncement msg_hu_conv = new NodeAnnouncement(null, msg);
103 Result_boolLightningErrorZ ret = arg.handle_node_announcement(msg_hu_conv);
104 long result = ret != null ? ret.ptr : 0;
107 @Override public long handle_channel_announcement(long msg) {
108 ChannelAnnouncement msg_hu_conv = new ChannelAnnouncement(null, msg);
109 Result_boolLightningErrorZ ret = arg.handle_channel_announcement(msg_hu_conv);
110 long result = ret != null ? ret.ptr : 0;
113 @Override public long handle_channel_update(long msg) {
114 ChannelUpdate msg_hu_conv = new ChannelUpdate(null, msg);
115 Result_boolLightningErrorZ ret = arg.handle_channel_update(msg_hu_conv);
116 long result = ret != null ? ret.ptr : 0;
119 @Override public void handle_htlc_fail_channel_update(long update) {
120 HTLCFailChannelUpdate update_hu_conv = HTLCFailChannelUpdate.constr_from_ptr(update);
121 arg.handle_htlc_fail_channel_update(update_hu_conv);
123 @Override public long[] get_next_channel_announcements(long starting_point, byte batch_amount) {
124 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] ret = arg.get_next_channel_announcements(starting_point, batch_amount);
125 long[] result = 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();
126 /* TODO 2 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate> */;
129 @Override public long[] get_next_node_announcements(byte[] starting_point, byte batch_amount) {
130 NodeAnnouncement[] ret = arg.get_next_node_announcements(starting_point, batch_amount);
131 long[] result = Arrays.stream(ret).mapToLong(ret_conv_18 -> ret_conv_18 == null ? 0 : ret_conv_18.ptr & ~1).toArray();
132 /* TODO 2 NodeAnnouncement */;
135 @Override public void sync_routing_table(byte[] their_node_id, long init) {
136 Init init_hu_conv = new Init(null, init);
137 arg.sync_routing_table(their_node_id, init_hu_conv);
139 @Override public long handle_reply_channel_range(byte[] their_node_id, long msg) {
140 ReplyChannelRange msg_hu_conv = new ReplyChannelRange(null, msg);
141 msg_hu_conv.ptrs_to.add(this);
142 Result_NoneLightningErrorZ ret = arg.handle_reply_channel_range(their_node_id, msg_hu_conv);
143 long result = ret != null ? ret.ptr : 0;
146 @Override public long handle_reply_short_channel_ids_end(byte[] their_node_id, long msg) {
147 ReplyShortChannelIdsEnd msg_hu_conv = new ReplyShortChannelIdsEnd(null, msg);
148 msg_hu_conv.ptrs_to.add(this);
149 Result_NoneLightningErrorZ ret = arg.handle_reply_short_channel_ids_end(their_node_id, msg_hu_conv);
150 long result = ret != null ? ret.ptr : 0;
153 @Override public long handle_query_channel_range(byte[] their_node_id, long msg) {
154 QueryChannelRange 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 long result = ret != null ? ret.ptr : 0;
160 @Override public long handle_query_short_channel_ids(byte[] their_node_id, long msg) {
161 QueryShortChannelIds msg_hu_conv = new QueryShortChannelIds(null, msg);
162 msg_hu_conv.ptrs_to.add(this);
163 Result_NoneLightningErrorZ ret = arg.handle_query_short_channel_ids(their_node_id, msg_hu_conv);
164 long result = ret != null ? ret.ptr : 0;
167 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
168 return impl_holder.held;
172 * Gets the underlying MessageSendEventsProvider.
174 public MessageSendEventsProvider get_message_send_events_provider() {
175 MessageSendEventsProvider res = new MessageSendEventsProvider(null, bindings.LDKRoutingMessageHandler_get_MessageSendEventsProvider(this.ptr));
176 this.ptrs_to.add(res);
181 * Handle an incoming node_announcement message, returning true if it should be forwarded on,
182 * false or returning an Err otherwise.
184 public Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg) {
185 long ret = bindings.RoutingMessageHandler_handle_node_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
186 if (ret < 1024) { return null; }
187 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
188 this.ptrs_to.add(msg);
193 * Handle a channel_announcement message, returning true if it should be forwarded on, false
194 * or returning an Err otherwise.
196 public Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg) {
197 long ret = bindings.RoutingMessageHandler_handle_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
198 if (ret < 1024) { return null; }
199 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
200 this.ptrs_to.add(msg);
205 * Handle an incoming channel_update message, returning true if it should be forwarded on,
206 * false or returning an Err otherwise.
208 public Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg) {
209 long ret = bindings.RoutingMessageHandler_handle_channel_update(this.ptr, msg == null ? 0 : msg.ptr & ~1);
210 if (ret < 1024) { return null; }
211 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
212 this.ptrs_to.add(msg);
217 * Handle some updates to the route graph that we learned due to an outbound failed payment.
219 public void handle_htlc_fail_channel_update(HTLCFailChannelUpdate update) {
220 bindings.RoutingMessageHandler_handle_htlc_fail_channel_update(this.ptr, update == null ? 0 : update.ptr & ~1);
221 this.ptrs_to.add(update);
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<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] 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 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] ret_conv_63_arr = new ThreeTuple[ret.length];
232 for (int l = 0; l < ret.length; l++) {
233 long ret_conv_63 = ret[l];
234 long ret_conv_63_a = bindings.LDKC3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_get_a(ret_conv_63);
235 ChannelAnnouncement ret_conv_63_a_hu_conv = new ChannelAnnouncement(null, ret_conv_63_a);
236 ret_conv_63_a_hu_conv.ptrs_to.add(this);;
237 long ret_conv_63_b = bindings.LDKC3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_get_b(ret_conv_63);
238 ChannelUpdate ret_conv_63_b_hu_conv = new ChannelUpdate(null, ret_conv_63_b);
239 ret_conv_63_b_hu_conv.ptrs_to.add(this);;
240 long ret_conv_63_c = bindings.LDKC3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_get_c(ret_conv_63);
241 ChannelUpdate ret_conv_63_c_hu_conv = new ChannelUpdate(null, ret_conv_63_c);
242 ret_conv_63_c_hu_conv.ptrs_to.add(this);;
243 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, () -> {
244 bindings.C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_free(ret_conv_63);
246 ret_conv_63_a_hu_conv.ptrs_to.add(ret_conv_63_conv);
247 ret_conv_63_b_hu_conv.ptrs_to.add(ret_conv_63_conv);
248 ret_conv_63_c_hu_conv.ptrs_to.add(ret_conv_63_conv);
249 ret_conv_63_arr[l] = ret_conv_63_conv;
251 return ret_conv_63_arr;
255 * Gets a subset of the node announcements required to dump our routing table to a remote node,
256 * starting at the node *after* the provided publickey and including batch_amount entries
257 * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
258 * If None is provided for starting_point, we start at the first node.
260 * Note that starting_point (or a relevant inner pointer) may be NULL or all-0s to represent None
262 public NodeAnnouncement[] get_next_node_announcements(@Nullable byte[] starting_point, byte batch_amount) {
263 long[] ret = bindings.RoutingMessageHandler_get_next_node_announcements(this.ptr, starting_point, batch_amount);
264 NodeAnnouncement[] ret_conv_18_arr = new NodeAnnouncement[ret.length];
265 for (int s = 0; s < ret.length; s++) {
266 long ret_conv_18 = ret[s];
267 NodeAnnouncement ret_conv_18_hu_conv = new NodeAnnouncement(null, ret_conv_18);
268 ret_conv_18_hu_conv.ptrs_to.add(this);
269 ret_conv_18_arr[s] = ret_conv_18_hu_conv;
271 return ret_conv_18_arr;
275 * Called when a connection is established with a peer. This can be used to
276 * perform routing table synchronization using a strategy defined by the
279 public void sync_routing_table(byte[] their_node_id, Init init) {
280 bindings.RoutingMessageHandler_sync_routing_table(this.ptr, their_node_id, init == null ? 0 : init.ptr & ~1);
281 this.ptrs_to.add(init);
285 * Handles the reply of a query we initiated to learn about channels
286 * for a given range of blocks. We can expect to receive one or more
287 * replies to a single query.
289 public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg) {
290 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
291 if (ret < 1024) { return null; }
292 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
293 this.ptrs_to.add(msg);
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, their_node_id, msg == null ? 0 : msg.ptr & ~1);
305 if (ret < 1024) { return null; }
306 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
307 this.ptrs_to.add(msg);
312 * Handles when a peer asks us to send a list of short_channel_ids
313 * for the requested range of blocks.
315 public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg) {
316 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
317 if (ret < 1024) { return null; }
318 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
319 this.ptrs_to.add(msg);
324 * Handles when a peer asks us to send routing gossip messages for a
325 * list of short_channel_ids.
327 public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg) {
328 long ret = bindings.RoutingMessageHandler_handle_query_short_channel_ids(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
329 if (ret < 1024) { return null; }
330 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
331 this.ptrs_to.add(msg);