1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
10 * An interface used to score payment channels for path finding.
12 * \tScoring is in terms of fees willing to be paid in order to avoid routing through a channel.
14 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
15 public class Score extends CommonBase {
16 final bindings.LDKScore bindings_instance;
17 Score(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
18 private Score(bindings.LDKScore arg) {
19 super(bindings.LDKScore_new(arg));
20 this.ptrs_to.add(arg);
21 this.bindings_instance = arg;
23 @Override @SuppressWarnings("deprecation")
24 protected void finalize() throws Throwable {
25 if (ptr != 0) { bindings.Score_free(ptr); } super.finalize();
28 public static interface ScoreInterface {
30 * Returns the fee in msats willing to be paid to avoid routing through the given channel
31 * in the direction from `source` to `target`.
33 long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target);
35 * Handles updating channel penalties after failing to route through a channel.
37 void payment_path_failed(RouteHop[] path, long short_channel_id);
39 * Serialize the object into a byte array
43 private static class LDKScoreHolder { Score held; }
44 public static Score new_impl(ScoreInterface arg) {
45 final LDKScoreHolder impl_holder = new LDKScoreHolder();
46 impl_holder.held = new Score(new bindings.LDKScore() {
47 @Override public long channel_penalty_msat(long short_channel_id, long source, long target) {
48 NodeId source_hu_conv = null; if (source < 0 || source > 4096) { source_hu_conv = new NodeId(null, source); }
49 NodeId target_hu_conv = null; if (target < 0 || target > 4096) { target_hu_conv = new NodeId(null, target); }
50 long ret = arg.channel_penalty_msat(short_channel_id, source_hu_conv, target_hu_conv);
53 @Override public void payment_path_failed(long[] path, long short_channel_id) {
54 RouteHop[] path_conv_10_arr = new RouteHop[path.length];
55 for (int k = 0; k < path.length; k++) {
56 long path_conv_10 = path[k];
57 RouteHop path_conv_10_hu_conv = null; if (path_conv_10 < 0 || path_conv_10 > 4096) { path_conv_10_hu_conv = new RouteHop(null, path_conv_10); }
58 path_conv_10_hu_conv.ptrs_to.add(this);
59 path_conv_10_arr[k] = path_conv_10_hu_conv;
61 arg.payment_path_failed(path_conv_10_arr, short_channel_id);
63 @Override public byte[] write() {
64 byte[] ret = arg.write();
68 return impl_holder.held;
71 * Returns the fee in msats willing to be paid to avoid routing through the given channel
72 * in the direction from `source` to `target`.
74 public long channel_penalty_msat(long short_channel_id, NodeId source, NodeId target) {
75 long ret = bindings.Score_channel_penalty_msat(this.ptr, short_channel_id, source == null ? 0 : source.ptr & ~1, target == null ? 0 : target.ptr & ~1);
76 this.ptrs_to.add(source);
77 this.ptrs_to.add(target);
82 * Handles updating channel penalties after failing to route through a channel.
84 public void payment_path_failed(RouteHop[] path, long short_channel_id) {
85 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);
89 * Serialize the object into a byte array
91 public byte[] write() {
92 byte[] ret = bindings.Score_write(this.ptr);