63da5612f36822bc72baaac32464514119a6a24c
[ldk-java] / src / main / java / org / ldk / structs / Score.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
11  * An interface used to score payment channels for path finding.
12  * 
13  * \tScoring is in terms of fees willing to be paid in order to avoid routing through a channel.
14  */
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;
23         }
24         @Override @SuppressWarnings("deprecation")
25         protected void finalize() throws Throwable {
26                 if (ptr != 0) { bindings.Score_free(ptr); } super.finalize();
27         }
28
29         public static interface ScoreInterface {
30                 /**
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`.
33                  * 
34                  * The channel's capacity (less any other MPP parts which are also being considered for use in
35                  * the same payment) is given by `channel_capacity_msat`. It may be guessed from various
36                  * sources or assumed from no data at all.
37                  * 
38                  * For hints provided in the invoice, we assume the channel has sufficient capacity to accept
39                  * the invoice's full amount, and provide a `channel_capacity_msat` of `None`. In all other
40                  * cases it is set to `Some`, even if we're guessing at the channel value.
41                  * 
42                  * Your code should be overflow-safe through a `channel_capacity_msat` of 21 million BTC.
43                  */
44                 long channel_penalty_msat(long short_channel_id, long send_amt_msat, Option_u64Z channel_capacity_msat, NodeId source, NodeId target);
45                 /**
46                  * Handles updating channel penalties after failing to route through a channel.
47                  */
48                 void payment_path_failed(RouteHop[] path, long short_channel_id);
49                 /**
50                  * Handles updating channel penalties after successfully routing along a path.
51                  */
52                 void payment_path_successful(RouteHop[] path);
53                 /**
54                  * Serialize the object into a byte array
55                  */
56                 byte[] write();
57         }
58         private static class LDKScoreHolder { Score held; }
59         public static Score new_impl(ScoreInterface arg) {
60                 final LDKScoreHolder impl_holder = new LDKScoreHolder();
61                 impl_holder.held = new Score(new bindings.LDKScore() {
62                         @Override public long channel_penalty_msat(long short_channel_id, long send_amt_msat, long channel_capacity_msat, long source, long target) {
63                                 Option_u64Z channel_capacity_msat_hu_conv = Option_u64Z.constr_from_ptr(channel_capacity_msat);
64                                 channel_capacity_msat_hu_conv.ptrs_to.add(this);
65                                 NodeId source_hu_conv = null; if (source < 0 || source > 4096) { source_hu_conv = new NodeId(null, source); }
66                                 NodeId target_hu_conv = null; if (target < 0 || target > 4096) { target_hu_conv = new NodeId(null, target); }
67                                 long ret = arg.channel_penalty_msat(short_channel_id, send_amt_msat, channel_capacity_msat_hu_conv, source_hu_conv, target_hu_conv);
68                                 return ret;
69                         }
70                         @Override public void payment_path_failed(long[] path, long short_channel_id) {
71                                 int path_conv_10_len = path.length;
72                                 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
73                                 for (int k = 0; k < path_conv_10_len; k++) {
74                                         long path_conv_10 = path[k];
75                                         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); }
76                                         path_conv_10_hu_conv.ptrs_to.add(this);
77                                         path_conv_10_arr[k] = path_conv_10_hu_conv;
78                                 }
79                                 arg.payment_path_failed(path_conv_10_arr, short_channel_id);
80                         }
81                         @Override public void payment_path_successful(long[] path) {
82                                 int path_conv_10_len = path.length;
83                                 RouteHop[] path_conv_10_arr = new RouteHop[path_conv_10_len];
84                                 for (int k = 0; k < path_conv_10_len; k++) {
85                                         long path_conv_10 = path[k];
86                                         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); }
87                                         path_conv_10_hu_conv.ptrs_to.add(this);
88                                         path_conv_10_arr[k] = path_conv_10_hu_conv;
89                                 }
90                                 arg.payment_path_successful(path_conv_10_arr);
91                         }
92                         @Override public byte[] write() {
93                                 byte[] ret = arg.write();
94                                 return ret;
95                         }
96                 });
97                 return impl_holder.held;
98         }
99         /**
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`.
102          * 
103          * The channel's capacity (less any other MPP parts which are also being considered for use in
104          * the same payment) is given by `channel_capacity_msat`. It may be guessed from various
105          * sources or assumed from no data at all.
106          * 
107          * For hints provided in the invoice, we assume the channel has sufficient capacity to accept
108          * the invoice's full amount, and provide a `channel_capacity_msat` of `None`. In all other
109          * cases it is set to `Some`, even if we're guessing at the channel value.
110          * 
111          * Your code should be overflow-safe through a `channel_capacity_msat` of 21 million BTC.
112          */
113         public long channel_penalty_msat(long short_channel_id, long send_amt_msat, Option_u64Z channel_capacity_msat, NodeId source, NodeId target) {
114                 long ret = bindings.Score_channel_penalty_msat(this.ptr, short_channel_id, send_amt_msat, channel_capacity_msat.ptr, source == null ? 0 : source.ptr & ~1, target == null ? 0 : target.ptr & ~1);
115                 Reference.reachabilityFence(this);
116                 Reference.reachabilityFence(short_channel_id);
117                 Reference.reachabilityFence(send_amt_msat);
118                 Reference.reachabilityFence(channel_capacity_msat);
119                 Reference.reachabilityFence(source);
120                 Reference.reachabilityFence(target);
121                 this.ptrs_to.add(source);
122                 this.ptrs_to.add(target);
123                 return ret;
124         }
125
126         /**
127          * Handles updating channel penalties after failing to route through a channel.
128          */
129         public void payment_path_failed(RouteHop[] path, long short_channel_id) {
130                 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);
131                 Reference.reachabilityFence(this);
132                 Reference.reachabilityFence(path);
133                 Reference.reachabilityFence(short_channel_id);
134         }
135
136         /**
137          * Handles updating channel penalties after successfully routing along a path.
138          */
139         public void payment_path_successful(RouteHop[] path) {
140                 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);
141                 Reference.reachabilityFence(this);
142                 Reference.reachabilityFence(path);
143         }
144
145         /**
146          * Serialize the object into a byte array
147          */
148         public byte[] write() {
149                 byte[] ret = bindings.Score_write(this.ptr);
150                 Reference.reachabilityFence(this);
151                 return ret;
152         }
153
154 }