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 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 = new NodeAnnouncement(null, msg);
101 Result_boolLightningErrorZ ret = arg.handle_node_announcement(msg_hu_conv);
102 long result = ret != null ? ret.ptr : 0;
105 @Override public long handle_channel_announcement(long msg) {
106 ChannelAnnouncement msg_hu_conv = new ChannelAnnouncement(null, msg);
107 Result_boolLightningErrorZ ret = arg.handle_channel_announcement(msg_hu_conv);
108 long result = ret != null ? ret.ptr : 0;
111 @Override public long handle_channel_update(long msg) {
112 ChannelUpdate msg_hu_conv = new ChannelUpdate(null, msg);
113 Result_boolLightningErrorZ ret = arg.handle_channel_update(msg_hu_conv);
114 long result = ret != null ? ret.ptr : 0;
117 @Override public void handle_htlc_fail_channel_update(long update) {
118 HTLCFailChannelUpdate update_hu_conv = HTLCFailChannelUpdate.constr_from_ptr(update);
119 arg.handle_htlc_fail_channel_update(update_hu_conv);
121 @Override public long[] get_next_channel_announcements(long starting_point, byte batch_amount) {
122 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] ret = arg.get_next_channel_announcements(starting_point, batch_amount);
123 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();
124 /* TODO 2 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate> */;
127 @Override public long[] get_next_node_announcements(byte[] starting_point, byte batch_amount) {
128 NodeAnnouncement[] ret = arg.get_next_node_announcements(starting_point, batch_amount);
129 long[] result = Arrays.stream(ret).mapToLong(ret_conv_18 -> ret_conv_18 == null ? 0 : ret_conv_18.ptr & ~1).toArray();
130 /* TODO 2 NodeAnnouncement */;
133 @Override public void sync_routing_table(byte[] their_node_id, long init) {
134 Init init_hu_conv = new Init(null, init);
135 arg.sync_routing_table(their_node_id, init_hu_conv);
137 @Override public long handle_reply_channel_range(byte[] their_node_id, long msg) {
138 ReplyChannelRange 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 long result = ret != null ? ret.ptr : 0;
144 @Override public long handle_reply_short_channel_ids_end(byte[] their_node_id, long msg) {
145 ReplyShortChannelIdsEnd msg_hu_conv = new ReplyShortChannelIdsEnd(null, msg);
146 msg_hu_conv.ptrs_to.add(this);
147 Result_NoneLightningErrorZ ret = arg.handle_reply_short_channel_ids_end(their_node_id, msg_hu_conv);
148 long result = ret != null ? ret.ptr : 0;
151 @Override public long handle_query_channel_range(byte[] their_node_id, long msg) {
152 QueryChannelRange msg_hu_conv = new QueryChannelRange(null, msg);
153 msg_hu_conv.ptrs_to.add(this);
154 Result_NoneLightningErrorZ ret = arg.handle_query_channel_range(their_node_id, msg_hu_conv);
155 long result = ret != null ? ret.ptr : 0;
158 @Override public long handle_query_short_channel_ids(byte[] their_node_id, long msg) {
159 QueryShortChannelIds msg_hu_conv = new QueryShortChannelIds(null, msg);
160 msg_hu_conv.ptrs_to.add(this);
161 Result_NoneLightningErrorZ ret = arg.handle_query_short_channel_ids(their_node_id, msg_hu_conv);
162 long result = ret != null ? ret.ptr : 0;
165 }, MessageSendEventsProvider.new_impl(MessageSendEventsProvider_impl).bindings_instance);
166 return impl_holder.held;
169 * Handle an incoming node_announcement message, returning true if it should be forwarded on,
170 * false or returning an Err otherwise.
172 public Result_boolLightningErrorZ handle_node_announcement(NodeAnnouncement msg) {
173 long ret = bindings.RoutingMessageHandler_handle_node_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
174 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
175 this.ptrs_to.add(msg);
180 * Handle a channel_announcement message, returning true if it should be forwarded on, false
181 * or returning an Err otherwise.
183 public Result_boolLightningErrorZ handle_channel_announcement(ChannelAnnouncement msg) {
184 long ret = bindings.RoutingMessageHandler_handle_channel_announcement(this.ptr, msg == null ? 0 : msg.ptr & ~1);
185 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
186 this.ptrs_to.add(msg);
191 * Handle an incoming channel_update message, returning true if it should be forwarded on,
192 * false or returning an Err otherwise.
194 public Result_boolLightningErrorZ handle_channel_update(ChannelUpdate msg) {
195 long ret = bindings.RoutingMessageHandler_handle_channel_update(this.ptr, msg == null ? 0 : msg.ptr & ~1);
196 Result_boolLightningErrorZ ret_hu_conv = Result_boolLightningErrorZ.constr_from_ptr(ret);
197 this.ptrs_to.add(msg);
202 * Handle some updates to the route graph that we learned due to an outbound failed payment.
204 public void handle_htlc_fail_channel_update(HTLCFailChannelUpdate update) {
205 bindings.RoutingMessageHandler_handle_htlc_fail_channel_update(this.ptr, update == null ? 0 : update.ptr & ~1);
206 this.ptrs_to.add(update);
210 * Gets a subset of the channel announcements and updates required to dump our routing table
211 * to a remote node, starting at the short_channel_id indicated by starting_point and
212 * including the batch_amount entries immediately higher in numerical value than starting_point.
214 public ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] get_next_channel_announcements(long starting_point, byte batch_amount) {
215 long[] ret = bindings.RoutingMessageHandler_get_next_channel_announcements(this.ptr, starting_point, batch_amount);
216 ThreeTuple<ChannelAnnouncement, ChannelUpdate, ChannelUpdate>[] ret_conv_63_arr = new ThreeTuple[ret.length];
217 for (int l = 0; l < ret.length; l++) {
218 long ret_conv_63 = ret[l];
219 long ret_conv_63_a = bindings.LDKC3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_get_a(ret_conv_63);
220 ChannelAnnouncement ret_conv_63_a_hu_conv = new ChannelAnnouncement(null, ret_conv_63_a);
221 ret_conv_63_a_hu_conv.ptrs_to.add(this);;
222 long ret_conv_63_b = bindings.LDKC3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_get_b(ret_conv_63);
223 ChannelUpdate ret_conv_63_b_hu_conv = new ChannelUpdate(null, ret_conv_63_b);
224 ret_conv_63_b_hu_conv.ptrs_to.add(this);;
225 long ret_conv_63_c = bindings.LDKC3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_get_c(ret_conv_63);
226 ChannelUpdate ret_conv_63_c_hu_conv = new ChannelUpdate(null, ret_conv_63_c);
227 ret_conv_63_c_hu_conv.ptrs_to.add(this);;
228 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, () -> {
229 bindings.C3Tuple_ChannelAnnouncementChannelUpdateChannelUpdateZ_free(ret_conv_63);
231 ret_conv_63_a_hu_conv.ptrs_to.add(ret_conv_63_conv);
232 ret_conv_63_b_hu_conv.ptrs_to.add(ret_conv_63_conv);
233 ret_conv_63_c_hu_conv.ptrs_to.add(ret_conv_63_conv);
234 ret_conv_63_arr[l] = ret_conv_63_conv;
236 return ret_conv_63_arr;
240 * Gets a subset of the node announcements required to dump our routing table to a remote node,
241 * starting at the node *after* the provided publickey and including batch_amount entries
242 * immediately higher (as defined by <PublicKey as Ord>::cmp) than starting_point.
243 * If None is provided for starting_point, we start at the first node.
245 public NodeAnnouncement[] get_next_node_announcements(byte[] starting_point, byte batch_amount) {
246 long[] ret = bindings.RoutingMessageHandler_get_next_node_announcements(this.ptr, starting_point, 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 = 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, their_node_id, init == null ? 0 : init.ptr & ~1);
264 this.ptrs_to.add(init);
268 * Handles the reply of a query we initiated to learn about channels
269 * for a given range of blocks. We can expect to receive one or more
270 * replies to a single query.
272 public Result_NoneLightningErrorZ handle_reply_channel_range(byte[] their_node_id, ReplyChannelRange msg) {
273 long ret = bindings.RoutingMessageHandler_handle_reply_channel_range(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
274 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
275 this.ptrs_to.add(msg);
280 * Handles the reply of a query we initiated asking for routing gossip
281 * messages for a list of channels. We should receive this message when
282 * a node has completed its best effort to send us the pertaining routing
285 public Result_NoneLightningErrorZ handle_reply_short_channel_ids_end(byte[] their_node_id, ReplyShortChannelIdsEnd msg) {
286 long ret = bindings.RoutingMessageHandler_handle_reply_short_channel_ids_end(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
287 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
288 this.ptrs_to.add(msg);
293 * Handles when a peer asks us to send a list of short_channel_ids
294 * for the requested range of blocks.
296 public Result_NoneLightningErrorZ handle_query_channel_range(byte[] their_node_id, QueryChannelRange msg) {
297 long ret = bindings.RoutingMessageHandler_handle_query_channel_range(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
298 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
299 this.ptrs_to.add(msg);
304 * Handles when a peer asks us to send routing gossip messages for a
305 * list of short_channel_ids.
307 public Result_NoneLightningErrorZ handle_query_short_channel_ids(byte[] their_node_id, QueryShortChannelIds msg) {
308 long ret = bindings.RoutingMessageHandler_handle_query_short_channel_ids(this.ptr, their_node_id, msg == null ? 0 : msg.ptr & ~1);
309 Result_NoneLightningErrorZ ret_hu_conv = Result_NoneLightningErrorZ.constr_from_ptr(ret);
310 this.ptrs_to.add(msg);