3f6f7d1a0b75b13780092d9eefa8355dd564b09c
[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                                 RouteHop[] path_conv_10_arr = new RouteHop[path.length];
72                                 for (int k = 0; k < path.length; k++) {
73                                         long path_conv_10 = path[k];
74                                         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); }
75                                         path_conv_10_hu_conv.ptrs_to.add(this);
76                                         path_conv_10_arr[k] = path_conv_10_hu_conv;
77                                 }
78                                 arg.payment_path_failed(path_conv_10_arr, short_channel_id);
79                         }
80                         @Override public void payment_path_successful(long[] path) {
81                                 RouteHop[] path_conv_10_arr = new RouteHop[path.length];
82                                 for (int k = 0; k < path.length; k++) {
83                                         long path_conv_10 = path[k];
84                                         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); }
85                                         path_conv_10_hu_conv.ptrs_to.add(this);
86                                         path_conv_10_arr[k] = path_conv_10_hu_conv;
87                                 }
88                                 arg.payment_path_successful(path_conv_10_arr);
89                         }
90                         @Override public byte[] write() {
91                                 byte[] ret = arg.write();
92                                 return ret;
93                         }
94                 });
95                 return impl_holder.held;
96         }
97         /**
98          * Returns the fee in msats willing to be paid to avoid routing `send_amt_msat` through the
99          * given channel in the direction from `source` to `target`.
100          * 
101          * The channel's capacity (less any other MPP parts which are also being considered for use in
102          * the same payment) is given by `channel_capacity_msat`. It may be guessed from various
103          * sources or assumed from no data at all.
104          * 
105          * For hints provided in the invoice, we assume the channel has sufficient capacity to accept
106          * the invoice's full amount, and provide a `channel_capacity_msat` of `None`. In all other
107          * cases it is set to `Some`, even if we're guessing at the channel value.
108          * 
109          * Your code should be overflow-safe through a `channel_capacity_msat` of 21 million BTC.
110          */
111         public long channel_penalty_msat(long short_channel_id, long send_amt_msat, Option_u64Z channel_capacity_msat, NodeId source, NodeId target) {
112                 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);
113                 Reference.reachabilityFence(this);
114                 Reference.reachabilityFence(short_channel_id);
115                 Reference.reachabilityFence(send_amt_msat);
116                 Reference.reachabilityFence(channel_capacity_msat);
117                 Reference.reachabilityFence(source);
118                 Reference.reachabilityFence(target);
119                 this.ptrs_to.add(source);
120                 this.ptrs_to.add(target);
121                 return ret;
122         }
123
124         /**
125          * Handles updating channel penalties after failing to route through a channel.
126          */
127         public void payment_path_failed(RouteHop[] path, long short_channel_id) {
128                 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);
129                 Reference.reachabilityFence(this);
130                 Reference.reachabilityFence(path);
131                 Reference.reachabilityFence(short_channel_id);
132         }
133
134         /**
135          * Handles updating channel penalties after successfully routing along a path.
136          */
137         public void payment_path_successful(RouteHop[] path) {
138                 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);
139                 Reference.reachabilityFence(this);
140                 Reference.reachabilityFence(path);
141         }
142
143         /**
144          * Serialize the object into a byte array
145          */
146         public byte[] write() {
147                 byte[] ret = bindings.Score_write(this.ptr);
148                 Reference.reachabilityFence(this);
149                 return ret;
150         }
151
152 }