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 * Destroys the object, freeing associated resources. After this call, any access
30 * to this object may result in a SEGFAULT or worse.
32 * You should generally NEVER call this method. You should let the garbage collector
33 * do this for you when it finalizes objects. However, it may be useful for types
34 * which represent locks and should be closed immediately to avoid holding locks
37 public void destroy() {
38 if (ptr != 0) { bindings.Score_free(ptr); }
41 public static interface ScoreInterface {
43 * Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
44 * given channel in the direction from `source` to `target`.
46 * The channel's capacity (less any other MPP parts that are also being considered for use in
47 * the same payment) is given by `capacity_msat`. It may be determined from various sources
48 * such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
49 * [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
50 * Thus, implementations should be overflow-safe.
52 long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target, ChannelUsage usage);
54 * Handles updating channel penalties after failing to route through a channel.
56 void payment_path_failed(Path path, long short_channel_id);
58 * Handles updating channel penalties after successfully routing along a path.
60 void payment_path_successful(Path path);
62 * Handles updating channel penalties after a probe over the given path failed.
64 void probe_failed(Path path, long short_channel_id);
66 * Handles updating channel penalties after a probe over the given path succeeded.
68 void probe_successful(Path path);
70 * Serialize the object into a byte array
74 private static class LDKScoreHolder { Score held; }
75 public static Score new_impl(ScoreInterface arg) {
76 final LDKScoreHolder impl_holder = new LDKScoreHolder();
77 impl_holder.held = new Score(new bindings.LDKScore() {
78 @Override public long channel_penalty_msat(long short_channel_id, long source, long target, long usage) {
79 org.ldk.structs.NodeId source_hu_conv = null; if (source < 0 || source > 4096) { source_hu_conv = new org.ldk.structs.NodeId(null, source); }
80 org.ldk.structs.NodeId target_hu_conv = null; if (target < 0 || target > 4096) { target_hu_conv = new org.ldk.structs.NodeId(null, target); }
81 org.ldk.structs.ChannelUsage usage_hu_conv = null; if (usage < 0 || usage > 4096) { usage_hu_conv = new org.ldk.structs.ChannelUsage(null, usage); }
82 if (usage_hu_conv != null) { usage_hu_conv.ptrs_to.add(this); };
83 long ret = arg.channel_penalty_msat(short_channel_id, source_hu_conv, target_hu_conv, usage_hu_conv);
84 Reference.reachabilityFence(arg);
87 @Override public void payment_path_failed(long path, long short_channel_id) {
88 org.ldk.structs.Path path_hu_conv = null; if (path < 0 || path > 4096) { path_hu_conv = new org.ldk.structs.Path(null, path); }
89 arg.payment_path_failed(path_hu_conv, short_channel_id);
90 Reference.reachabilityFence(arg);
92 @Override public void payment_path_successful(long path) {
93 org.ldk.structs.Path path_hu_conv = null; if (path < 0 || path > 4096) { path_hu_conv = new org.ldk.structs.Path(null, path); }
94 arg.payment_path_successful(path_hu_conv);
95 Reference.reachabilityFence(arg);
97 @Override public void probe_failed(long path, long short_channel_id) {
98 org.ldk.structs.Path path_hu_conv = null; if (path < 0 || path > 4096) { path_hu_conv = new org.ldk.structs.Path(null, path); }
99 arg.probe_failed(path_hu_conv, short_channel_id);
100 Reference.reachabilityFence(arg);
102 @Override public void probe_successful(long path) {
103 org.ldk.structs.Path path_hu_conv = null; if (path < 0 || path > 4096) { path_hu_conv = new org.ldk.structs.Path(null, path); }
104 arg.probe_successful(path_hu_conv);
105 Reference.reachabilityFence(arg);
107 @Override public byte[] write() {
108 byte[] ret = arg.write();
109 Reference.reachabilityFence(arg);
113 return impl_holder.held;
116 * Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
117 * given channel in the direction from `source` to `target`.
119 * The channel's capacity (less any other MPP parts that are also being considered for use in
120 * the same payment) is given by `capacity_msat`. It may be determined from various sources
121 * such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
122 * [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
123 * Thus, implementations should be overflow-safe.
125 public long channel_penalty_msat(long short_channel_id, org.ldk.structs.NodeId source, org.ldk.structs.NodeId target, org.ldk.structs.ChannelUsage usage) {
126 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);
127 Reference.reachabilityFence(this);
128 Reference.reachabilityFence(short_channel_id);
129 Reference.reachabilityFence(source);
130 Reference.reachabilityFence(target);
131 Reference.reachabilityFence(usage);
132 if (this != null) { this.ptrs_to.add(source); };
133 if (this != null) { this.ptrs_to.add(target); };
134 if (this != null) { this.ptrs_to.add(usage); };
139 * Handles updating channel penalties after failing to route through a channel.
141 public void payment_path_failed(org.ldk.structs.Path path, long short_channel_id) {
142 bindings.Score_payment_path_failed(this.ptr, path == null ? 0 : path.ptr, short_channel_id);
143 Reference.reachabilityFence(this);
144 Reference.reachabilityFence(path);
145 Reference.reachabilityFence(short_channel_id);
146 if (this != null) { this.ptrs_to.add(path); };
150 * Handles updating channel penalties after successfully routing along a path.
152 public void payment_path_successful(org.ldk.structs.Path path) {
153 bindings.Score_payment_path_successful(this.ptr, path == null ? 0 : path.ptr);
154 Reference.reachabilityFence(this);
155 Reference.reachabilityFence(path);
156 if (this != null) { this.ptrs_to.add(path); };
160 * Handles updating channel penalties after a probe over the given path failed.
162 public void probe_failed(org.ldk.structs.Path path, long short_channel_id) {
163 bindings.Score_probe_failed(this.ptr, path == null ? 0 : path.ptr, short_channel_id);
164 Reference.reachabilityFence(this);
165 Reference.reachabilityFence(path);
166 Reference.reachabilityFence(short_channel_id);
167 if (this != null) { this.ptrs_to.add(path); };
171 * Handles updating channel penalties after a probe over the given path succeeded.
173 public void probe_successful(org.ldk.structs.Path path) {
174 bindings.Score_probe_successful(this.ptr, path == null ? 0 : path.ptr);
175 Reference.reachabilityFence(this);
176 Reference.reachabilityFence(path);
177 if (this != null) { this.ptrs_to.add(path); };
181 * Serialize the object into a byte array
183 public byte[] write() {
184 byte[] ret = bindings.Score_write(this.ptr);
185 Reference.reachabilityFence(this);