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 & ~1, network_graph == null ? 0 : network_graph.ptr & ~1, 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(network_graph); };
57 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
62 * Dump the contents of this scorer into the configured logger.
64 * Note that this writes roughly one line per channel for which we have a liquidity estimate,
65 * which may be a substantial amount of log output.
67 public void debug_log_liquidity_stats() {
68 bindings.ProbabilisticScorer_debug_log_liquidity_stats(this.ptr);
69 Reference.reachabilityFence(this);
73 * Query the estimated minimum and maximum liquidity available for sending a payment over the
74 * channel with `scid` towards the given `target` node.
76 public Option_C2Tuple_u64u64ZZ estimated_channel_liquidity_range(long scid, NodeId target) {
77 long ret = bindings.ProbabilisticScorer_estimated_channel_liquidity_range(this.ptr, scid, target == null ? 0 : target.ptr & ~1);
78 Reference.reachabilityFence(this);
79 Reference.reachabilityFence(scid);
80 Reference.reachabilityFence(target);
81 if (ret >= 0 && ret <= 4096) { return null; }
82 org.ldk.structs.Option_C2Tuple_u64u64ZZ ret_hu_conv = org.ldk.structs.Option_C2Tuple_u64u64ZZ.constr_from_ptr(ret);
83 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
84 if (this != null) { this.ptrs_to.add(target); };
89 * Marks the node with the given `node_id` as banned, i.e.,
90 * it will be avoided during path finding.
92 public void add_banned(NodeId node_id) {
93 bindings.ProbabilisticScorer_add_banned(this.ptr, node_id == null ? 0 : node_id.ptr & ~1);
94 Reference.reachabilityFence(this);
95 Reference.reachabilityFence(node_id);
96 if (this != null) { this.ptrs_to.add(node_id); };
100 * Removes the node with the given `node_id` from the list of nodes to avoid.
102 public void remove_banned(NodeId node_id) {
103 bindings.ProbabilisticScorer_remove_banned(this.ptr, node_id == null ? 0 : node_id.ptr & ~1);
104 Reference.reachabilityFence(this);
105 Reference.reachabilityFence(node_id);
106 if (this != null) { this.ptrs_to.add(node_id); };
110 * Sets a manual penalty for the given node.
112 public void set_manual_penalty(NodeId node_id, long penalty) {
113 bindings.ProbabilisticScorer_set_manual_penalty(this.ptr, node_id == null ? 0 : node_id.ptr & ~1, penalty);
114 Reference.reachabilityFence(this);
115 Reference.reachabilityFence(node_id);
116 Reference.reachabilityFence(penalty);
117 if (this != null) { this.ptrs_to.add(node_id); };
121 * Removes the node with the given `node_id` from the list of manual penalties.
123 public void remove_manual_penalty(NodeId node_id) {
124 bindings.ProbabilisticScorer_remove_manual_penalty(this.ptr, node_id == null ? 0 : node_id.ptr & ~1);
125 Reference.reachabilityFence(this);
126 Reference.reachabilityFence(node_id);
127 if (this != null) { this.ptrs_to.add(node_id); };
131 * Clears the list of manual penalties that are applied during path finding.
133 public void clear_manual_penalties() {
134 bindings.ProbabilisticScorer_clear_manual_penalties(this.ptr);
135 Reference.reachabilityFence(this);
139 * Constructs a new Score which calls the relevant methods on this_arg.
140 * This copies the `inner` pointer in this_arg and thus the returned Score must be freed before this_arg is
142 public Score as_Score() {
143 long ret = bindings.ProbabilisticScorer_as_Score(this.ptr);
144 Reference.reachabilityFence(this);
145 if (ret >= 0 && ret <= 4096) { return null; }
146 Score ret_hu_conv = new Score(null, ret);
147 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
152 * Serialize the ProbabilisticScorer object into a byte array which can be read by ProbabilisticScorer_read
154 public byte[] write() {
155 byte[] ret = bindings.ProbabilisticScorer_write(this.ptr);
156 Reference.reachabilityFence(this);
161 * Read a ProbabilisticScorer from a byte array, created by ProbabilisticScorer_write
163 public static Result_ProbabilisticScorerDecodeErrorZ read(byte[] ser, ProbabilisticScoringParameters arg_a, NetworkGraph arg_b, Logger arg_c) {
164 long ret = bindings.ProbabilisticScorer_read(ser, arg_a == null ? 0 : arg_a.ptr & ~1, arg_b == null ? 0 : arg_b.ptr & ~1, arg_c == null ? 0 : arg_c.ptr);
165 Reference.reachabilityFence(ser);
166 Reference.reachabilityFence(arg_a);
167 Reference.reachabilityFence(arg_b);
168 Reference.reachabilityFence(arg_c);
169 if (ret >= 0 && ret <= 4096) { return null; }
170 Result_ProbabilisticScorerDecodeErrorZ ret_hu_conv = Result_ProbabilisticScorerDecodeErrorZ.constr_from_ptr(ret);
171 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_b); };
172 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_c); };