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 * Serialize the object into a byte array
54 private static class LDKScoreHolder { Score held; }
55 public static Score new_impl(ScoreInterface arg) {
56 final LDKScoreHolder impl_holder = new LDKScoreHolder();
57 impl_holder.held = new Score(new bindings.LDKScore() {
58 @Override public long channel_penalty_msat(long short_channel_id, long source, long target, long usage) {
59 org.ldk.structs.NodeId source_hu_conv = null; if (source < 0 || source > 4096) { source_hu_conv = new org.ldk.structs.NodeId(null, source); }
60 org.ldk.structs.NodeId target_hu_conv = null; if (target < 0 || target > 4096) { target_hu_conv = new org.ldk.structs.NodeId(null, target); }
61 org.ldk.structs.ChannelUsage usage_hu_conv = null; if (usage < 0 || usage > 4096) { usage_hu_conv = new org.ldk.structs.ChannelUsage(null, usage); }
62 usage_hu_conv.ptrs_to.add(this);
63 long ret = arg.channel_penalty_msat(short_channel_id, source_hu_conv, target_hu_conv, usage_hu_conv);
64 Reference.reachabilityFence(arg);
67 @Override public void payment_path_failed(long[] path, long short_channel_id) {
68 int path_conv_10_len = path.length;
69 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
70 for (int k = 0; k < path_conv_10_len; k++) {
71 long path_conv_10 = path[k];
72 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); }
73 path_conv_10_hu_conv.ptrs_to.add(this);
74 path_conv_10_arr[k] = path_conv_10_hu_conv;
76 arg.payment_path_failed(path_conv_10_arr, short_channel_id);
77 Reference.reachabilityFence(arg);
79 @Override public void payment_path_successful(long[] path) {
80 int path_conv_10_len = path.length;
81 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
82 for (int k = 0; k < path_conv_10_len; k++) {
83 long path_conv_10 = path[k];
84 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); }
85 path_conv_10_hu_conv.ptrs_to.add(this);
86 path_conv_10_arr[k] = path_conv_10_hu_conv;
88 arg.payment_path_successful(path_conv_10_arr);
89 Reference.reachabilityFence(arg);
91 @Override public byte[] write() {
92 byte[] ret = arg.write();
93 Reference.reachabilityFence(arg);
97 return impl_holder.held;
100 * Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
101 * given channel in the direction from `source` to `target`.
103 * The channel's capacity (less any other MPP parts that are also being considered for use in
104 * the same payment) is given by `capacity_msat`. It may be determined from various sources
105 * such as a chain data, network gossip, or invoice hints. For invoice hints, a capacity near
106 * [`u64::max_value`] is given to indicate sufficient capacity for the invoice's full amount.
107 * Thus, implementations should be overflow-safe.
109 public long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target, ChannelUsage usage) {
110 long ret = bindings.Score_channel_penalty_msat(this.ptr, short_channel_id, source == null ? 0 : source.ptr & ~1, target == null ? 0 : target.ptr & ~1, usage == null ? 0 : usage.ptr & ~1);
111 Reference.reachabilityFence(this);
112 Reference.reachabilityFence(short_channel_id);
113 Reference.reachabilityFence(source);
114 Reference.reachabilityFence(target);
115 Reference.reachabilityFence(usage);
116 this.ptrs_to.add(source);
117 this.ptrs_to.add(target);
122 * Handles updating channel penalties after failing to route through a channel.
124 public void payment_path_failed(RouteHop[] path, long short_channel_id) {
125 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 & ~1).toArray() : null, short_channel_id);
126 Reference.reachabilityFence(this);
127 Reference.reachabilityFence(path);
128 Reference.reachabilityFence(short_channel_id);
132 * Handles updating channel penalties after successfully routing along a path.
134 public void payment_path_successful(RouteHop[] path) {
135 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 & ~1).toArray() : null);
136 Reference.reachabilityFence(this);
137 Reference.reachabilityFence(path);
141 * Serialize the object into a byte array
143 public byte[] write() {
144 byte[] ret = bindings.Score_write(this.ptr);
145 Reference.reachabilityFence(this);