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 defining behavior for routing a payment.
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class Router extends CommonBase {
15 final bindings.LDKRouter bindings_instance;
16 Router(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
17 private Router(bindings.LDKRouter arg, bindings.LDKMessageRouter MessageRouter) {
18 super(bindings.LDKRouter_new(arg, MessageRouter));
19 this.ptrs_to.add(arg);
20 this.ptrs_to.add(MessageRouter);
21 this.bindings_instance = arg;
23 @Override @SuppressWarnings("deprecation")
24 protected void finalize() throws Throwable {
25 if (ptr != 0) { bindings.Router_free(ptr); } super.finalize();
28 * Destroys the object, freeing associated resources. After this call, any access
29 * to this object may result in a SEGFAULT or worse.
31 * You should generally NEVER call this method. You should let the garbage collector
32 * do this for you when it finalizes objects. However, it may be useful for types
33 * which represent locks and should be closed immediately to avoid holding locks
36 public void destroy() {
37 if (ptr != 0) { bindings.Router_free(ptr); }
40 public static interface RouterInterface {
42 * Finds a [`Route`] for a payment between the given `payer` and a payee.
44 * The `payee` and the payment's value are given in [`RouteParameters::payment_params`]
45 * and [`RouteParameters::final_value_msat`], respectively.
47 * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
49 Result_RouteLightningErrorZ find_route(byte[] payer, RouteParameters route_params, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs);
51 * Finds a [`Route`] for a payment between the given `payer` and a payee.
53 * The `payee` and the payment's value are given in [`RouteParameters::payment_params`]
54 * and [`RouteParameters::final_value_msat`], respectively.
56 * Includes a [`PaymentHash`] and a [`PaymentId`] to be able to correlate the request with a specific
59 * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
61 Result_RouteLightningErrorZ find_route_with_id(byte[] payer, RouteParameters route_params, ChannelDetails[] first_hops, InFlightHtlcs inflight_htlcs, byte[] _payment_hash, byte[] _payment_id);
63 * Creates [`BlindedPath`]s for payment to the `recipient` node. The channels in `first_hops`
64 * are assumed to be with the `recipient`'s peers. The payment secret and any constraints are
67 Result_CVec_C2Tuple_BlindedPayInfoBlindedPathZZNoneZ create_blinded_payment_paths(byte[] recipient, ChannelDetails[] first_hops, ReceiveTlvs tlvs, long amount_msats);
69 private static class LDKRouterHolder { Router held; }
70 public static Router new_impl(RouterInterface arg, MessageRouter.MessageRouterInterface MessageRouter_impl) {
71 final LDKRouterHolder impl_holder = new LDKRouterHolder();
72 impl_holder.held = new Router(new bindings.LDKRouter() {
73 @Override public long find_route(byte[] payer, long route_params, long[] first_hops, long inflight_htlcs) {
74 org.ldk.structs.RouteParameters route_params_hu_conv = null; if (route_params < 0 || route_params > 4096) { route_params_hu_conv = new org.ldk.structs.RouteParameters(null, route_params); }
75 int first_hops_conv_16_len = first_hops.length;
76 ChannelDetails[] first_hops_conv_16_arr = new ChannelDetails[first_hops_conv_16_len];
77 if (first_hops != null) {
78 for (int q = 0; q < first_hops_conv_16_len; q++) {
79 long first_hops_conv_16 = first_hops[q];
80 org.ldk.structs.ChannelDetails first_hops_conv_16_hu_conv = null; if (first_hops_conv_16 < 0 || first_hops_conv_16 > 4096) { first_hops_conv_16_hu_conv = new org.ldk.structs.ChannelDetails(null, first_hops_conv_16); }
81 if (first_hops_conv_16_hu_conv != null) { first_hops_conv_16_hu_conv.ptrs_to.add(this); };
82 first_hops_conv_16_arr[q] = first_hops_conv_16_hu_conv;
85 org.ldk.structs.InFlightHtlcs inflight_htlcs_hu_conv = null; if (inflight_htlcs < 0 || inflight_htlcs > 4096) { inflight_htlcs_hu_conv = new org.ldk.structs.InFlightHtlcs(null, inflight_htlcs); }
86 if (inflight_htlcs_hu_conv != null) { inflight_htlcs_hu_conv.ptrs_to.add(this); };
87 Result_RouteLightningErrorZ ret = arg.find_route(payer, route_params_hu_conv, first_hops_conv_16_arr, inflight_htlcs_hu_conv);
88 Reference.reachabilityFence(arg);
89 long result = ret.clone_ptr();
92 @Override public long find_route_with_id(byte[] payer, long route_params, long[] first_hops, long inflight_htlcs, byte[] _payment_hash, byte[] _payment_id) {
93 org.ldk.structs.RouteParameters route_params_hu_conv = null; if (route_params < 0 || route_params > 4096) { route_params_hu_conv = new org.ldk.structs.RouteParameters(null, route_params); }
94 int first_hops_conv_16_len = first_hops.length;
95 ChannelDetails[] first_hops_conv_16_arr = new ChannelDetails[first_hops_conv_16_len];
96 if (first_hops != null) {
97 for (int q = 0; q < first_hops_conv_16_len; q++) {
98 long first_hops_conv_16 = first_hops[q];
99 org.ldk.structs.ChannelDetails first_hops_conv_16_hu_conv = null; if (first_hops_conv_16 < 0 || first_hops_conv_16 > 4096) { first_hops_conv_16_hu_conv = new org.ldk.structs.ChannelDetails(null, first_hops_conv_16); }
100 if (first_hops_conv_16_hu_conv != null) { first_hops_conv_16_hu_conv.ptrs_to.add(this); };
101 first_hops_conv_16_arr[q] = first_hops_conv_16_hu_conv;
104 org.ldk.structs.InFlightHtlcs inflight_htlcs_hu_conv = null; if (inflight_htlcs < 0 || inflight_htlcs > 4096) { inflight_htlcs_hu_conv = new org.ldk.structs.InFlightHtlcs(null, inflight_htlcs); }
105 if (inflight_htlcs_hu_conv != null) { inflight_htlcs_hu_conv.ptrs_to.add(this); };
106 Result_RouteLightningErrorZ ret = arg.find_route_with_id(payer, route_params_hu_conv, first_hops_conv_16_arr, inflight_htlcs_hu_conv, _payment_hash, _payment_id);
107 Reference.reachabilityFence(arg);
108 long result = ret.clone_ptr();
111 @Override public long create_blinded_payment_paths(byte[] recipient, long[] first_hops, long tlvs, long amount_msats) {
112 int first_hops_conv_16_len = first_hops.length;
113 ChannelDetails[] first_hops_conv_16_arr = new ChannelDetails[first_hops_conv_16_len];
114 for (int q = 0; q < first_hops_conv_16_len; q++) {
115 long first_hops_conv_16 = first_hops[q];
116 org.ldk.structs.ChannelDetails first_hops_conv_16_hu_conv = null; if (first_hops_conv_16 < 0 || first_hops_conv_16 > 4096) { first_hops_conv_16_hu_conv = new org.ldk.structs.ChannelDetails(null, first_hops_conv_16); }
117 if (first_hops_conv_16_hu_conv != null) { first_hops_conv_16_hu_conv.ptrs_to.add(this); };
118 first_hops_conv_16_arr[q] = first_hops_conv_16_hu_conv;
120 org.ldk.structs.ReceiveTlvs tlvs_hu_conv = null; if (tlvs < 0 || tlvs > 4096) { tlvs_hu_conv = new org.ldk.structs.ReceiveTlvs(null, tlvs); }
121 if (tlvs_hu_conv != null) { tlvs_hu_conv.ptrs_to.add(this); };
122 Result_CVec_C2Tuple_BlindedPayInfoBlindedPathZZNoneZ ret = arg.create_blinded_payment_paths(recipient, first_hops_conv_16_arr, tlvs_hu_conv, amount_msats);
123 Reference.reachabilityFence(arg);
124 long result = ret.clone_ptr();
127 }, MessageRouter.new_impl(MessageRouter_impl).bindings_instance);
128 return impl_holder.held;
132 * Gets the underlying MessageRouter.
134 public MessageRouter get_message_router() {
135 MessageRouter res = new MessageRouter(null, bindings.LDKRouter_get_MessageRouter(this.ptr));
136 res.ptrs_to.add(this);
141 * Finds a [`Route`] for a payment between the given `payer` and a payee.
143 * The `payee` and the payment's value are given in [`RouteParameters::payment_params`]
144 * and [`RouteParameters::final_value_msat`], respectively.
146 * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
148 public Result_RouteLightningErrorZ find_route(byte[] payer, org.ldk.structs.RouteParameters route_params, @Nullable ChannelDetails[] first_hops, org.ldk.structs.InFlightHtlcs inflight_htlcs) {
149 long ret = bindings.Router_find_route(this.ptr, InternalUtils.check_arr_len(payer, 33), route_params.ptr, first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16.ptr).toArray() : null, inflight_htlcs.ptr);
150 Reference.reachabilityFence(this);
151 Reference.reachabilityFence(payer);
152 Reference.reachabilityFence(route_params);
153 Reference.reachabilityFence(first_hops);
154 Reference.reachabilityFence(inflight_htlcs);
155 if (ret >= 0 && ret <= 4096) { return null; }
156 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
157 if (this != null) { this.ptrs_to.add(route_params); };
158 if (first_hops != null) { for (ChannelDetails first_hops_conv_16: first_hops) { if (this != null) { this.ptrs_to.add(first_hops_conv_16); }; } };
159 if (this != null) { this.ptrs_to.add(inflight_htlcs); };
164 * Finds a [`Route`] for a payment between the given `payer` and a payee.
166 * The `payee` and the payment's value are given in [`RouteParameters::payment_params`]
167 * and [`RouteParameters::final_value_msat`], respectively.
169 * Includes a [`PaymentHash`] and a [`PaymentId`] to be able to correlate the request with a specific
172 * Note that first_hops (or a relevant inner pointer) may be NULL or all-0s to represent None
174 public Result_RouteLightningErrorZ find_route_with_id(byte[] payer, org.ldk.structs.RouteParameters route_params, @Nullable ChannelDetails[] first_hops, org.ldk.structs.InFlightHtlcs inflight_htlcs, byte[] _payment_hash, byte[] _payment_id) {
175 long ret = bindings.Router_find_route_with_id(this.ptr, InternalUtils.check_arr_len(payer, 33), route_params.ptr, first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16.ptr).toArray() : null, inflight_htlcs.ptr, InternalUtils.check_arr_len(_payment_hash, 32), InternalUtils.check_arr_len(_payment_id, 32));
176 Reference.reachabilityFence(this);
177 Reference.reachabilityFence(payer);
178 Reference.reachabilityFence(route_params);
179 Reference.reachabilityFence(first_hops);
180 Reference.reachabilityFence(inflight_htlcs);
181 Reference.reachabilityFence(_payment_hash);
182 Reference.reachabilityFence(_payment_id);
183 if (ret >= 0 && ret <= 4096) { return null; }
184 Result_RouteLightningErrorZ ret_hu_conv = Result_RouteLightningErrorZ.constr_from_ptr(ret);
185 if (this != null) { this.ptrs_to.add(route_params); };
186 if (first_hops != null) { for (ChannelDetails first_hops_conv_16: first_hops) { if (this != null) { this.ptrs_to.add(first_hops_conv_16); }; } };
187 if (this != null) { this.ptrs_to.add(inflight_htlcs); };
192 * Creates [`BlindedPath`]s for payment to the `recipient` node. The channels in `first_hops`
193 * are assumed to be with the `recipient`'s peers. The payment secret and any constraints are
196 public Result_CVec_C2Tuple_BlindedPayInfoBlindedPathZZNoneZ create_blinded_payment_paths(byte[] recipient, ChannelDetails[] first_hops, org.ldk.structs.ReceiveTlvs tlvs, long amount_msats) {
197 long ret = bindings.Router_create_blinded_payment_paths(this.ptr, InternalUtils.check_arr_len(recipient, 33), first_hops != null ? Arrays.stream(first_hops).mapToLong(first_hops_conv_16 -> first_hops_conv_16.ptr).toArray() : null, tlvs.ptr, amount_msats);
198 Reference.reachabilityFence(this);
199 Reference.reachabilityFence(recipient);
200 Reference.reachabilityFence(first_hops);
201 Reference.reachabilityFence(tlvs);
202 Reference.reachabilityFence(amount_msats);
203 if (ret >= 0 && ret <= 4096) { return null; }
204 Result_CVec_C2Tuple_BlindedPayInfoBlindedPathZZNoneZ ret_hu_conv = Result_CVec_C2Tuple_BlindedPayInfoBlindedPathZZNoneZ.constr_from_ptr(ret);
205 for (ChannelDetails first_hops_conv_16: first_hops) { if (this != null) { this.ptrs_to.add(first_hops_conv_16); }; };
206 if (this != null) { this.ptrs_to.add(tlvs); };