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;
12 * [`Score`] implementation using channel success probability distributions.
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.
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.
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.
30 * Mixing the `no-std` feature between serialization and deserialization results in undefined
33 * [1]: https://arxiv.org/abs/2107.05322
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 {
41 if (ptr != 0) { bindings.ProbabilisticScorer_free(ptr); }
45 * Creates a new scorer using the given scoring parameters for sending payments from a node
46 * through a network graph.
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 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
56 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(params); };
57 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(network_graph); };
58 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
63 * Dump the contents of this scorer into the configured logger.
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.
68 public void debug_log_liquidity_stats() {
69 bindings.ProbabilisticScorer_debug_log_liquidity_stats(this.ptr);
70 Reference.reachabilityFence(this);
74 * Query the estimated minimum and maximum liquidity available for sending a payment over the
75 * channel with `scid` towards the given `target` node.
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 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
85 if (this != null) { this.ptrs_to.add(target); };
90 * Marks the node with the given `node_id` as banned, i.e.,
91 * it will be avoided during path finding.
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 if (this != null) { this.ptrs_to.add(node_id); };
101 * Removes the node with the given `node_id` from the list of nodes to avoid.
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 if (this != null) { this.ptrs_to.add(node_id); };
111 * Sets a manual penalty for the given node.
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 if (this != null) { this.ptrs_to.add(node_id); };
122 * Removes the node with the given `node_id` from the list of manual penalties.
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 if (this != null) { this.ptrs_to.add(node_id); };
132 * Clears the list of manual penalties that are applied during path finding.
134 public void clear_manual_penalties() {
135 bindings.ProbabilisticScorer_clear_manual_penalties(this.ptr);
136 Reference.reachabilityFence(this);
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
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 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
153 * Serialize the ProbabilisticScorer object into a byte array which can be read by ProbabilisticScorer_read
155 public byte[] write() {
156 byte[] ret = bindings.ProbabilisticScorer_write(this.ptr);
157 Reference.reachabilityFence(this);
162 * Read a ProbabilisticScorer from a byte array, created by ProbabilisticScorer_write
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 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_a); };
173 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_b); };
174 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_c); };