c2ac732b4bfdca94b1a8137585b11c8086414dd6
[ldk-java] / src / main / java / org / ldk / structs / ProbabilisticScorer.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 /**
12  * [`Score`] implementation using channel success probability distributions.
13  * 
14  * Based on *Optimally Reliable & Cheap Payment Flows on the Lightning Network* by Rene Pickhardt
15  * and Stefan Richter [[1]]. Given the uncertainty of channel liquidity balances, probability
16  * distributions are defined based on knowledge learned from successful and unsuccessful attempts.
17  * Then the negative `log10` of the success probability is used to determine the cost of routing a
18  * specific HTLC amount through a channel.
19  * 
20  * Knowledge about channel liquidity balances takes the form of upper and lower bounds on the
21  * possible liquidity. Certainty of the bounds is decreased over time using a decay function. See
22  * [`ProbabilisticScoringParameters`] for details.
23  * 
24  * Since the scorer aims to learn the current channel liquidity balances, it works best for nodes
25  * with high payment volume or that actively probe the [`NetworkGraph`]. Nodes with low payment
26  * volume are more likely to experience failed payment paths, which would need to be retried.
27  * 
28  * # Note
29  * 
30  * Mixing the `no-std` feature between serialization and deserialization results in undefined
31  * behavior.
32  * 
33  * [1]: https://arxiv.org/abs/2107.05322
34  */
35 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
36 public class ProbabilisticScorer extends CommonBase {
37         ProbabilisticScorer(Object _dummy, long ptr) { super(ptr); }
38         @Override @SuppressWarnings("deprecation")
39         protected void finalize() throws Throwable {
40                 super.finalize();
41                 if (ptr != 0) { bindings.ProbabilisticScorer_free(ptr); }
42         }
43
44         /**
45          * Creates a new scorer using the given scoring parameters for sending payments from a node
46          * through a network graph.
47          */
48         public static ProbabilisticScorer of(ProbabilisticScoringParameters params, NetworkGraph network_graph, Logger logger) {
49                 long ret = bindings.ProbabilisticScorer_new(params == null ? 0 : params.ptr, network_graph == null ? 0 : network_graph.ptr, logger == null ? 0 : logger.ptr);
50                 Reference.reachabilityFence(params);
51                 Reference.reachabilityFence(network_graph);
52                 Reference.reachabilityFence(logger);
53                 if (ret >= 0 && ret <= 4096) { return null; }
54                 org.ldk.structs.ProbabilisticScorer ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.ProbabilisticScorer(null, ret); }
55                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
56                 ret_hu_conv.ptrs_to.add(params);
57                 ret_hu_conv.ptrs_to.add(network_graph);
58                 ret_hu_conv.ptrs_to.add(logger);
59                 return ret_hu_conv;
60         }
61
62         /**
63          * Dump the contents of this scorer into the configured logger.
64          * 
65          * Note that this writes roughly one line per channel for which we have a liquidity estimate,
66          * which may be a substantial amount of log output.
67          */
68         public void debug_log_liquidity_stats() {
69                 bindings.ProbabilisticScorer_debug_log_liquidity_stats(this.ptr);
70                 Reference.reachabilityFence(this);
71         }
72
73         /**
74          * Query the estimated minimum and maximum liquidity available for sending a payment over the
75          * channel with `scid` towards the given `target` node.
76          */
77         public Option_C2Tuple_u64u64ZZ estimated_channel_liquidity_range(long scid, NodeId target) {
78                 long ret = bindings.ProbabilisticScorer_estimated_channel_liquidity_range(this.ptr, scid, target == null ? 0 : target.ptr);
79                 Reference.reachabilityFence(this);
80                 Reference.reachabilityFence(scid);
81                 Reference.reachabilityFence(target);
82                 if (ret >= 0 && ret <= 4096) { return null; }
83                 org.ldk.structs.Option_C2Tuple_u64u64ZZ ret_hu_conv = org.ldk.structs.Option_C2Tuple_u64u64ZZ.constr_from_ptr(ret);
84                 ret_hu_conv.ptrs_to.add(this);
85                 this.ptrs_to.add(target);
86                 return ret_hu_conv;
87         }
88
89         /**
90          * Marks the node with the given `node_id` as banned, i.e.,
91          * it will be avoided during path finding.
92          */
93         public void add_banned(NodeId node_id) {
94                 bindings.ProbabilisticScorer_add_banned(this.ptr, node_id == null ? 0 : node_id.ptr);
95                 Reference.reachabilityFence(this);
96                 Reference.reachabilityFence(node_id);
97                 this.ptrs_to.add(node_id);
98         }
99
100         /**
101          * Removes the node with the given `node_id` from the list of nodes to avoid.
102          */
103         public void remove_banned(NodeId node_id) {
104                 bindings.ProbabilisticScorer_remove_banned(this.ptr, node_id == null ? 0 : node_id.ptr);
105                 Reference.reachabilityFence(this);
106                 Reference.reachabilityFence(node_id);
107                 this.ptrs_to.add(node_id);
108         }
109
110         /**
111          * Sets a manual penalty for the given node.
112          */
113         public void set_manual_penalty(NodeId node_id, long penalty) {
114                 bindings.ProbabilisticScorer_set_manual_penalty(this.ptr, node_id == null ? 0 : node_id.ptr, penalty);
115                 Reference.reachabilityFence(this);
116                 Reference.reachabilityFence(node_id);
117                 Reference.reachabilityFence(penalty);
118                 this.ptrs_to.add(node_id);
119         }
120
121         /**
122          * Removes the node with the given `node_id` from the list of manual penalties.
123          */
124         public void remove_manual_penalty(NodeId node_id) {
125                 bindings.ProbabilisticScorer_remove_manual_penalty(this.ptr, node_id == null ? 0 : node_id.ptr);
126                 Reference.reachabilityFence(this);
127                 Reference.reachabilityFence(node_id);
128                 this.ptrs_to.add(node_id);
129         }
130
131         /**
132          * Clears the list of manual penalties that are applied during path finding.
133          */
134         public void clear_manual_penalties() {
135                 bindings.ProbabilisticScorer_clear_manual_penalties(this.ptr);
136                 Reference.reachabilityFence(this);
137         }
138
139         /**
140          * Constructs a new Score which calls the relevant methods on this_arg.
141          * This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
142          */
143         public Score as_Score() {
144                 long ret = bindings.ProbabilisticScorer_as_Score(this.ptr);
145                 Reference.reachabilityFence(this);
146                 if (ret >= 0 && ret <= 4096) { return null; }
147                 Score ret_hu_conv = new Score(null, ret);
148                 ret_hu_conv.ptrs_to.add(this);
149                 return ret_hu_conv;
150         }
151
152         /**
153          * Serialize the ProbabilisticScorer object into a byte array which can be read by ProbabilisticScorer_read
154          */
155         public byte[] write() {
156                 byte[] ret = bindings.ProbabilisticScorer_write(this.ptr);
157                 Reference.reachabilityFence(this);
158                 return ret;
159         }
160
161         /**
162          * Read a ProbabilisticScorer from a byte array, created by ProbabilisticScorer_write
163          */
164         public static Result_ProbabilisticScorerDecodeErrorZ read(byte[] ser, ProbabilisticScoringParameters arg_a, NetworkGraph arg_b, Logger arg_c) {
165                 long ret = bindings.ProbabilisticScorer_read(ser, arg_a == null ? 0 : arg_a.ptr, arg_b == null ? 0 : arg_b.ptr, arg_c == null ? 0 : arg_c.ptr);
166                 Reference.reachabilityFence(ser);
167                 Reference.reachabilityFence(arg_a);
168                 Reference.reachabilityFence(arg_b);
169                 Reference.reachabilityFence(arg_c);
170                 if (ret >= 0 && ret <= 4096) { return null; }
171                 Result_ProbabilisticScorerDecodeErrorZ ret_hu_conv = Result_ProbabilisticScorerDecodeErrorZ.constr_from_ptr(ret);
172                 ret_hu_conv.ptrs_to.add(arg_a);
173                 ret_hu_conv.ptrs_to.add(arg_b);
174                 ret_hu_conv.ptrs_to.add(arg_c);
175                 return ret_hu_conv;
176         }
177
178 }