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 * An interface used to score payment channels for path finding.
13 * \tScoring is in terms of fees willing to be paid in order to avoid routing through a channel.
15 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
16 public class Score extends CommonBase {
17 final bindings.LDKScore bindings_instance;
18 Score(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
19 private Score(bindings.LDKScore arg) {
20 super(bindings.LDKScore_new(arg));
21 this.ptrs_to.add(arg);
22 this.bindings_instance = arg;
24 @Override @SuppressWarnings("deprecation")
25 protected void finalize() throws Throwable {
26 if (ptr != 0) { bindings.Score_free(ptr); } super.finalize();
29 public static interface ScoreInterface {
31 * Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
32 * given channel in the direction from `source` to `target`.
34 * The channel's capacity (less any other MPP parts that are also being considered for use in
35 * the same payment) is given by `capacity_msat`. It may be determined from various sources
36 * such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
37 * [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
38 * Thus, implementations should be overflow-safe.
40 long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target, ChannelUsage usage);
42 * Handles updating channel penalties after failing to route through a channel.
44 void payment_path_failed(RouteHop[] path, long short_channel_id);
46 * Handles updating channel penalties after successfully routing along a path.
48 void payment_path_successful(RouteHop[] path);
50 * Handles updating channel penalties after a probe over the given path failed.
52 void probe_failed(RouteHop[] path, long short_channel_id);
54 * Handles updating channel penalties after a probe over the given path succeeded.
56 void probe_successful(RouteHop[] path);
58 * Serialize the object into a byte array
62 private static class LDKScoreHolder { Score held; }
63 public static Score new_impl(ScoreInterface arg) {
64 final LDKScoreHolder impl_holder = new LDKScoreHolder();
65 impl_holder.held = new Score(new bindings.LDKScore() {
66 @Override public long channel_penalty_msat(long short_channel_id, long source, long target, long usage) {
67 org.ldk.structs.NodeId source_hu_conv = null; if (source < 0 || source > 4096) { source_hu_conv = new org.ldk.structs.NodeId(null, source); }
68 org.ldk.structs.NodeId target_hu_conv = null; if (target < 0 || target > 4096) { target_hu_conv = new org.ldk.structs.NodeId(null, target); }
69 org.ldk.structs.ChannelUsage usage_hu_conv = null; if (usage < 0 || usage > 4096) { usage_hu_conv = new org.ldk.structs.ChannelUsage(null, usage); }
70 if (usage_hu_conv != null) { usage_hu_conv.ptrs_to.add(this); };
71 long ret = arg.channel_penalty_msat(short_channel_id, source_hu_conv, target_hu_conv, usage_hu_conv);
72 Reference.reachabilityFence(arg);
75 @Override public void payment_path_failed(long[] path, long short_channel_id) {
76 int path_conv_10_len = path.length;
77 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
78 for (int k = 0; k < path_conv_10_len; k++) {
79 long path_conv_10 = path[k];
80 org.ldk.structs.RouteHop path_conv_10_hu_conv = null; if (path_conv_10 < 0 || path_conv_10 > 4096) { path_conv_10_hu_conv = new org.ldk.structs.RouteHop(null, path_conv_10); }
81 if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
82 path_conv_10_arr[k] = path_conv_10_hu_conv;
84 arg.payment_path_failed(path_conv_10_arr, short_channel_id);
85 Reference.reachabilityFence(arg);
87 @Override public void payment_path_successful(long[] path) {
88 int path_conv_10_len = path.length;
89 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
90 for (int k = 0; k < path_conv_10_len; k++) {
91 long path_conv_10 = path[k];
92 org.ldk.structs.RouteHop path_conv_10_hu_conv = null; if (path_conv_10 < 0 || path_conv_10 > 4096) { path_conv_10_hu_conv = new org.ldk.structs.RouteHop(null, path_conv_10); }
93 if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
94 path_conv_10_arr[k] = path_conv_10_hu_conv;
96 arg.payment_path_successful(path_conv_10_arr);
97 Reference.reachabilityFence(arg);
99 @Override public void probe_failed(long[] path, long short_channel_id) {
100 int path_conv_10_len = path.length;
101 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
102 for (int k = 0; k < path_conv_10_len; k++) {
103 long path_conv_10 = path[k];
104 org.ldk.structs.RouteHop path_conv_10_hu_conv = null; if (path_conv_10 < 0 || path_conv_10 > 4096) { path_conv_10_hu_conv = new org.ldk.structs.RouteHop(null, path_conv_10); }
105 if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
106 path_conv_10_arr[k] = path_conv_10_hu_conv;
108 arg.probe_failed(path_conv_10_arr, short_channel_id);
109 Reference.reachabilityFence(arg);
111 @Override public void probe_successful(long[] path) {
112 int path_conv_10_len = path.length;
113 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
114 for (int k = 0; k < path_conv_10_len; k++) {
115 long path_conv_10 = path[k];
116 org.ldk.structs.RouteHop path_conv_10_hu_conv = null; if (path_conv_10 < 0 || path_conv_10 > 4096) { path_conv_10_hu_conv = new org.ldk.structs.RouteHop(null, path_conv_10); }
117 if (path_conv_10_hu_conv != null) { path_conv_10_hu_conv.ptrs_to.add(this); };
118 path_conv_10_arr[k] = path_conv_10_hu_conv;
120 arg.probe_successful(path_conv_10_arr);
121 Reference.reachabilityFence(arg);
123 @Override public byte[] write() {
124 byte[] ret = arg.write();
125 Reference.reachabilityFence(arg);
129 return impl_holder.held;
132 * Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
133 * given channel in the direction from `source` to `target`.
135 * The channel's capacity (less any other MPP parts that are also being considered for use in
136 * the same payment) is given by `capacity_msat`. It may be determined from various sources
137 * such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
138 * [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
139 * Thus, implementations should be overflow-safe.
141 public long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target, ChannelUsage usage) {
142 long ret = bindings.Score_channel_penalty_msat(this.ptr, short_channel_id, source == null ? 0 : source.ptr, target == null ? 0 : target.ptr, usage == null ? 0 : usage.ptr);
143 Reference.reachabilityFence(this);
144 Reference.reachabilityFence(short_channel_id);
145 Reference.reachabilityFence(source);
146 Reference.reachabilityFence(target);
147 Reference.reachabilityFence(usage);
148 if (this != null) { this.ptrs_to.add(source); };
149 if (this != null) { this.ptrs_to.add(target); };
150 if (this != null) { this.ptrs_to.add(usage); };
155 * Handles updating channel penalties after failing to route through a channel.
157 public void payment_path_failed(RouteHop[] path, long short_channel_id) {
158 bindings.Score_payment_path_failed(this.ptr, path != null ? Arrays.stream(path).mapToLong(path_conv_10 -> path_conv_10 == null ? 0 : path_conv_10.ptr).toArray() : null, short_channel_id);
159 Reference.reachabilityFence(this);
160 Reference.reachabilityFence(path);
161 Reference.reachabilityFence(short_channel_id);
162 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
166 * Handles updating channel penalties after successfully routing along a path.
168 public void payment_path_successful(RouteHop[] path) {
169 bindings.Score_payment_path_successful(this.ptr, path != null ? Arrays.stream(path).mapToLong(path_conv_10 -> path_conv_10 == null ? 0 : path_conv_10.ptr).toArray() : null);
170 Reference.reachabilityFence(this);
171 Reference.reachabilityFence(path);
172 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
176 * Handles updating channel penalties after a probe over the given path failed.
178 public void probe_failed(RouteHop[] path, long short_channel_id) {
179 bindings.Score_probe_failed(this.ptr, path != null ? Arrays.stream(path).mapToLong(path_conv_10 -> path_conv_10 == null ? 0 : path_conv_10.ptr).toArray() : null, short_channel_id);
180 Reference.reachabilityFence(this);
181 Reference.reachabilityFence(path);
182 Reference.reachabilityFence(short_channel_id);
183 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
187 * Handles updating channel penalties after a probe over the given path succeeded.
189 public void probe_successful(RouteHop[] path) {
190 bindings.Score_probe_successful(this.ptr, path != null ? Arrays.stream(path).mapToLong(path_conv_10 -> path_conv_10 == null ? 0 : path_conv_10.ptr).toArray() : null);
191 Reference.reachabilityFence(this);
192 Reference.reachabilityFence(path);
193 for (RouteHop path_conv_10: path) { if (this != null) { this.ptrs_to.add(path_conv_10); }; };
197 * Serialize the object into a byte array
199 public byte[] write() {
200 byte[] ret = bindings.Score_write(this.ptr);
201 Reference.reachabilityFence(this);